mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 13:27:57 +00:00
frame-support-test: migrate tests from decl_* macros to the new pallet macros (#12445)
* frame-support: migrate some tests from decl macros to new pallet attribute macros * Remove useless type alias * Remove useless type alias * frame-support-test: migrate old decl_macros to new pallet attribute macros * fmt Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix features Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Remove deprecated stuff Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update UI tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix UI test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update UI tests Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Cleanup Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: parity-processbot <>
This commit is contained in:
@@ -16,65 +16,166 @@
|
||||
// limitations under the License.
|
||||
|
||||
use codec::Encode;
|
||||
use frame_support::{
|
||||
storage::unhashed, StorageDoubleMap, StorageMap, StoragePrefixedMap, StorageValue,
|
||||
};
|
||||
use frame_support::{storage::unhashed, StoragePrefixedMap};
|
||||
use sp_core::sr25519;
|
||||
use sp_io::{
|
||||
hashing::{blake2_128, twox_128, twox_64},
|
||||
TestExternalities,
|
||||
};
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::{BlakeTwo256, Verify},
|
||||
};
|
||||
|
||||
#[frame_support::pallet]
|
||||
mod no_instance {
|
||||
pub trait Config: frame_support_test::Config {}
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(PhantomData<T>);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {}
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Value<T> = StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Map<T> = StorageMap<_, Blake2_128Concat, u32, u32, ValueQuery>;
|
||||
#[pallet::storage]
|
||||
pub type Map2<T> = StorageMap<_, Twox64Concat, u32, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap<T> =
|
||||
StorageDoubleMap<_, Blake2_128Concat, u32, Blake2_128Concat, u32, u32, ValueQuery>;
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap2<T> =
|
||||
StorageDoubleMap<_, Twox64Concat, u32, Twox64Concat, u32, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn test_generic_value)]
|
||||
pub type TestGenericValue<T: Config> = StorageValue<_, T::BlockNumber, OptionQuery>;
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn foo2)]
|
||||
pub type TestGenericDoubleMap<T: Config> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
u32,
|
||||
Blake2_128Concat,
|
||||
T::BlockNumber,
|
||||
u32,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
pub struct GenesisConfig<T: Config> {
|
||||
pub value: u32,
|
||||
pub test_generic_value: T::BlockNumber,
|
||||
pub test_generic_double_map: Vec<(u32, T::BlockNumber, u32)>,
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as FinalKeysNone {
|
||||
pub Value config(value): u32;
|
||||
impl<T: Config> Default for GenesisConfig<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: Default::default(),
|
||||
test_generic_value: Default::default(),
|
||||
test_generic_double_map: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub Map: map hasher(blake2_128_concat) u32 => u32;
|
||||
pub Map2: map hasher(twox_64_concat) u32 => u32;
|
||||
|
||||
pub DoubleMap: double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) u32 => u32;
|
||||
pub DoubleMap2: double_map hasher(twox_64_concat) u32, hasher(twox_64_concat) u32 => u32;
|
||||
|
||||
pub TestGenericValue get(fn test_generic_value) config(): Option<T::BlockNumber>;
|
||||
pub TestGenericDoubleMap get(fn foo2) config(test_generic_double_map):
|
||||
double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) T::BlockNumber => Option<u32>;
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
|
||||
fn build(&self) {
|
||||
<Value<T>>::put(self.value);
|
||||
<TestGenericValue<T>>::put(&self.test_generic_value);
|
||||
for (k1, k2, v) in &self.test_generic_double_map {
|
||||
<TestGenericDoubleMap<T>>::insert(k1, k2, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[frame_support::pallet]
|
||||
mod instance {
|
||||
pub trait Config<I = DefaultInstance>: frame_support_test::Config {}
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support_test as frame_system;
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config<I>, I: Instance = DefaultInstance>
|
||||
for enum Call where origin: T::RuntimeOrigin, system=frame_support_test {}
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config<I: 'static = ()>: frame_system::Config {}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {}
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Value<T: Config<I>, I: 'static = ()> = StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Map<T: Config<I>, I: 'static = ()> =
|
||||
StorageMap<_, Blake2_128Concat, u32, u32, ValueQuery>;
|
||||
#[pallet::storage]
|
||||
pub type Map2<T: Config<I>, I: 'static = ()> =
|
||||
StorageMap<_, Twox64Concat, u32, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap<T: Config<I>, I: 'static = ()> =
|
||||
StorageDoubleMap<_, Blake2_128Concat, u32, Blake2_128Concat, u32, u32, ValueQuery>;
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap2<T: Config<I>, I: 'static = ()> =
|
||||
StorageDoubleMap<_, Twox64Concat, u32, Twox64Concat, u32, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn test_generic_value)]
|
||||
pub type TestGenericValue<T: Config<I>, I: 'static = ()> =
|
||||
StorageValue<_, T::BlockNumber, OptionQuery>;
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn foo2)]
|
||||
pub type TestGenericDoubleMap<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
u32,
|
||||
Blake2_128Concat,
|
||||
T::BlockNumber,
|
||||
u32,
|
||||
ValueQuery,
|
||||
>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
|
||||
pub value: u32,
|
||||
pub test_generic_value: T::BlockNumber,
|
||||
pub test_generic_double_map: Vec<(u32, T::BlockNumber, u32)>,
|
||||
pub phantom: PhantomData<I>,
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config<I>, I: Instance = DefaultInstance>
|
||||
as FinalKeysSome
|
||||
{
|
||||
pub Value config(value): u32;
|
||||
|
||||
pub Map: map hasher(blake2_128_concat) u32 => u32;
|
||||
pub Map2: map hasher(twox_64_concat) u32 => u32;
|
||||
|
||||
pub DoubleMap: double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) u32 => u32;
|
||||
pub DoubleMap2: double_map hasher(twox_64_concat) u32, hasher(twox_64_concat) u32 => u32;
|
||||
|
||||
pub TestGenericValue get(fn test_generic_value) config(): Option<T::BlockNumber>;
|
||||
pub TestGenericDoubleMap get(fn foo2) config(test_generic_double_map):
|
||||
double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) T::BlockNumber => Option<u32>;
|
||||
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
value: Default::default(),
|
||||
test_generic_value: Default::default(),
|
||||
test_generic_double_map: Default::default(),
|
||||
phantom: Default::default(),
|
||||
}
|
||||
}
|
||||
add_extra_genesis {
|
||||
// See `decl_storage` limitation.
|
||||
config(phantom): core::marker::PhantomData<I>;
|
||||
}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config<I>, I: 'static> GenesisBuild<T, I> for GenesisConfig<T, I> {
|
||||
fn build(&self) {
|
||||
<Value<T, I>>::put(self.value);
|
||||
<TestGenericValue<T, I>>::put(&self.test_generic_value);
|
||||
for (k1, k2, v) in &self.test_generic_double_map {
|
||||
<TestGenericDoubleMap<T, I>>::insert(k1, k2, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,107 +192,144 @@ fn blake2_128_concat(d: &[u8]) -> Vec<u8> {
|
||||
v
|
||||
}
|
||||
|
||||
pub type BlockNumber = u32;
|
||||
pub type Signature = sr25519::Signature;
|
||||
pub type AccountId = <Signature as Verify>::Signer;
|
||||
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
|
||||
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, Signature, ()>;
|
||||
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Runtime
|
||||
where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic,
|
||||
{
|
||||
System: frame_support_test,
|
||||
FinalKeysNone: no_instance,
|
||||
FinalKeysSome: instance,
|
||||
Instance2FinalKeysSome: instance::<Instance2>,
|
||||
}
|
||||
);
|
||||
|
||||
impl frame_support_test::Config for Runtime {
|
||||
type BlockNumber = BlockNumber;
|
||||
type AccountId = AccountId;
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type RuntimeOrigin = RuntimeOrigin;
|
||||
type RuntimeCall = RuntimeCall;
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PalletInfo = PalletInfo;
|
||||
type DbWeight = ();
|
||||
}
|
||||
|
||||
impl no_instance::Config for Runtime {}
|
||||
|
||||
impl instance::Config for Runtime {}
|
||||
impl instance::Config<instance::Instance2> for Runtime {}
|
||||
|
||||
#[test]
|
||||
fn final_keys_no_instance() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
no_instance::Value::put(1);
|
||||
<no_instance::Value<Runtime>>::put(1);
|
||||
let k = [twox_128(b"FinalKeysNone"), twox_128(b"Value")].concat();
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(1u32));
|
||||
|
||||
no_instance::Map::insert(1, 2);
|
||||
<no_instance::Map<Runtime>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"FinalKeysNone"), twox_128(b"Map")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<no_instance::Map>::final_prefix());
|
||||
assert_eq!(&k[..32], &<no_instance::Map<Runtime>>::final_prefix());
|
||||
|
||||
no_instance::Map2::insert(1, 2);
|
||||
<no_instance::Map2<Runtime>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"FinalKeysNone"), twox_128(b"Map2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<no_instance::Map2>::final_prefix());
|
||||
assert_eq!(&k[..32], &<no_instance::Map2<Runtime>>::final_prefix());
|
||||
|
||||
no_instance::DoubleMap::insert(&1, &2, &3);
|
||||
<no_instance::DoubleMap<Runtime>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"FinalKeysNone"), twox_128(b"DoubleMap")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
k.extend(2u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<no_instance::DoubleMap>::final_prefix());
|
||||
assert_eq!(&k[..32], &<no_instance::DoubleMap<Runtime>>::final_prefix());
|
||||
|
||||
no_instance::DoubleMap2::insert(&1, &2, &3);
|
||||
<no_instance::DoubleMap2<Runtime>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"FinalKeysNone"), twox_128(b"DoubleMap2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
k.extend(2u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<no_instance::DoubleMap2>::final_prefix());
|
||||
assert_eq!(&k[..32], &<no_instance::DoubleMap2<Runtime>>::final_prefix());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn final_keys_default_instance() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
<instance::Value<instance::DefaultInstance>>::put(1);
|
||||
<instance::Value<Runtime>>::put(1);
|
||||
let k = [twox_128(b"FinalKeysSome"), twox_128(b"Value")].concat();
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(1u32));
|
||||
|
||||
<instance::Map<instance::DefaultInstance>>::insert(1, 2);
|
||||
<instance::Map<Runtime>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"FinalKeysSome"), twox_128(b"Map")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<instance::Map<instance::DefaultInstance>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::Map<Runtime>>::final_prefix());
|
||||
|
||||
<instance::Map2<instance::DefaultInstance>>::insert(1, 2);
|
||||
<instance::Map2<Runtime>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"FinalKeysSome"), twox_128(b"Map2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<instance::Map2<instance::DefaultInstance>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::Map2<Runtime>>::final_prefix());
|
||||
|
||||
<instance::DoubleMap<instance::DefaultInstance>>::insert(&1, &2, &3);
|
||||
<instance::DoubleMap<Runtime>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"FinalKeysSome"), twox_128(b"DoubleMap")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
k.extend(2u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap<instance::DefaultInstance>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap<Runtime>>::final_prefix());
|
||||
|
||||
<instance::DoubleMap2<instance::DefaultInstance>>::insert(&1, &2, &3);
|
||||
<instance::DoubleMap2<Runtime>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"FinalKeysSome"), twox_128(b"DoubleMap2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
k.extend(2u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap2<instance::DefaultInstance>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap2<Runtime>>::final_prefix());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn final_keys_instance_2() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
<instance::Value<instance::Instance2>>::put(1);
|
||||
<instance::Value<Runtime, instance::Instance2>>::put(1);
|
||||
let k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"Value")].concat();
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(1u32));
|
||||
|
||||
<instance::Map<instance::Instance2>>::insert(1, 2);
|
||||
<instance::Map<Runtime, instance::Instance2>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"Map")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<instance::Map<instance::Instance2>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::Map<Runtime, instance::Instance2>>::final_prefix());
|
||||
|
||||
<instance::Map2<instance::Instance2>>::insert(1, 2);
|
||||
<instance::Map2<Runtime, instance::Instance2>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"Map2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<instance::Map2<instance::Instance2>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::Map2<Runtime, instance::Instance2>>::final_prefix());
|
||||
|
||||
<instance::DoubleMap<instance::Instance2>>::insert(&1, &2, &3);
|
||||
<instance::DoubleMap<Runtime, instance::Instance2>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"DoubleMap")].concat();
|
||||
k.extend(1u32.using_encoded(blake2_128_concat));
|
||||
k.extend(2u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap<instance::Instance2>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap<Runtime, instance::Instance2>>::final_prefix());
|
||||
|
||||
<instance::DoubleMap2<instance::Instance2>>::insert(&1, &2, &3);
|
||||
<instance::DoubleMap2<Runtime, instance::Instance2>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"Instance2FinalKeysSome"), twox_128(b"DoubleMap2")].concat();
|
||||
k.extend(1u32.using_encoded(twox_64_concat));
|
||||
k.extend(2u32.using_encoded(twox_64_concat));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap2<instance::Instance2>>::final_prefix());
|
||||
assert_eq!(&k[..32], &<instance::DoubleMap2<Runtime, instance::Instance2>>::final_prefix());
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user