frame-support: migrate some tests from decl_* macros to the new pallet macros (#12401)

* frame-support: migrate some tests from decl macros to new pallet attribute macros

* Remove useless type alias

* Remove useless type alias

* Work around for rust issue 52234

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Use polkadot-compatible paste version

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix crate access and add tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Typo

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:
Qinxuan Chen
2023-04-25 19:58:21 +08:00
committed by GitHub
parent c11cb1b274
commit b06748e2ab
10 changed files with 496 additions and 322 deletions
+3 -2
View File
@@ -7239,9 +7239,9 @@ dependencies = [
[[package]]
name = "paste"
version = "1.0.11"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba"
checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79"
[[package]]
name = "pbkdf2"
@@ -10503,6 +10503,7 @@ dependencies = [
"merlin",
"parity-scale-codec",
"parking_lot 0.12.1",
"paste",
"primitive-types",
"rand 0.8.5",
"regex",
+128 -63
View File
@@ -3532,18 +3532,110 @@ mod tests {
#[allow(dead_code)]
mod weight_tests {
use super::{tests::*, *};
use sp_core::{parameter_types, Get};
use sp_core::parameter_types;
use sp_runtime::{generic, traits::BlakeTwo256};
use sp_weights::RuntimeDbWeight;
pub trait Config: 'static {
type RuntimeOrigin;
type Balance;
type BlockNumber;
type DbWeight: Get<RuntimeDbWeight>;
type PalletInfo: crate::traits::PalletInfo;
pub use self::frame_system::{Call, Config, Pallet};
#[crate::pallet(dev_mode)]
pub mod frame_system {
use super::{frame_system, frame_system::pallet_prelude::*};
pub use crate::dispatch::RawOrigin;
use crate::pallet_prelude::*;
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: 'static {
type BlockNumber: Parameter + Default + MaxEncodedLen;
type AccountId;
type Balance;
type BaseCallFilter: crate::traits::Contains<Self::RuntimeCall>;
type RuntimeOrigin;
type RuntimeCall;
type PalletInfo: crate::traits::PalletInfo;
type DbWeight: Get<crate::weights::RuntimeDbWeight>;
}
#[pallet::error]
pub enum Error<T> {
/// Required by construct_runtime
CallFiltered,
}
#[pallet::origin]
pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;
#[pallet::call]
impl<T: Config> Pallet<T> {
// no arguments, fixed weight
#[pallet::weight(1000)]
pub fn f00(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}
#[pallet::weight((1000, DispatchClass::Mandatory))]
pub fn f01(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}
#[pallet::weight((1000, Pays::No))]
pub fn f02(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}
#[pallet::weight((1000, DispatchClass::Operational, Pays::No))]
pub fn f03(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}
// weight = a x 10 + b
#[pallet::weight(((_a * 10 + _eb * 1) as u64, DispatchClass::Normal, Pays::Yes))]
pub fn f11(_origin: OriginFor<T>, _a: u32, _eb: u32) -> DispatchResult {
unimplemented!();
}
#[pallet::weight((0, DispatchClass::Operational, Pays::Yes))]
pub fn f12(_origin: OriginFor<T>, _a: u32, _eb: u32) -> DispatchResult {
unimplemented!();
}
#[pallet::weight(T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + Weight::from_all(10_000))]
pub fn f20(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}
#[pallet::weight(T::DbWeight::get().reads_writes(6, 5) + Weight::from_all(40_000))]
pub fn f21(_origin: OriginFor<T>) -> DispatchResult {
unimplemented!();
}
}
pub mod pallet_prelude {
pub type OriginFor<T> = <T as super::Config>::RuntimeOrigin;
}
}
pub struct TraitImpl {}
type BlockNumber = u32;
type AccountId = u32;
type Balance = u32;
type Header = generic::Header<BlockNumber, BlakeTwo256>;
type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, (), ()>;
type Block = generic::Block<Header, UncheckedExtrinsic>;
crate::construct_runtime!(
pub enum Runtime
where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: self::frame_system,
}
);
parameter_types! {
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
@@ -3552,92 +3644,65 @@ mod weight_tests {
};
}
impl Config for TraitImpl {
type RuntimeOrigin = u32;
type BlockNumber = u32;
type Balance = u32;
impl Config for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Balance = Balance;
type BaseCallFilter = crate::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type DbWeight = DbWeight;
type PalletInfo = crate::tests::PanicPalletInfo;
}
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {
// no arguments, fixed weight
#[weight = 1000]
fn f00(_origin) { unimplemented!(); }
#[weight = (1000, DispatchClass::Mandatory)]
fn f01(_origin) { unimplemented!(); }
#[weight = (1000, Pays::No)]
fn f02(_origin) { unimplemented!(); }
#[weight = (1000, DispatchClass::Operational, Pays::No)]
fn f03(_origin) { unimplemented!(); }
// weight = a x 10 + b
#[weight = ((_a * 10 + _eb * 1) as u64, DispatchClass::Normal, Pays::Yes)]
fn f11(_origin, _a: u32, _eb: u32) { unimplemented!(); }
#[weight = (0, DispatchClass::Operational, Pays::Yes)]
fn f12(_origin, _a: u32, _eb: u32) { unimplemented!(); }
#[weight = T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + Weight::from_parts(10_000, 0)]
fn f20(_origin) { unimplemented!(); }
#[weight = T::DbWeight::get().reads_writes(6, 5) + Weight::from_parts(40_000, 0)]
fn f21(_origin) { unimplemented!(); }
}
type PalletInfo = PalletInfo;
}
#[test]
fn weights_are_correct() {
// #[weight = 1000]
let info = Call::<TraitImpl>::f00 {}.get_dispatch_info();
// #[pallet::weight(1000)]
let info = Call::<Runtime>::f00 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(1000, 0));
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::Yes);
// #[weight = (1000, DispatchClass::Mandatory)]
let info = Call::<TraitImpl>::f01 {}.get_dispatch_info();
// #[pallet::weight((1000, DispatchClass::Mandatory))]
let info = Call::<Runtime>::f01 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(1000, 0));
assert_eq!(info.class, DispatchClass::Mandatory);
assert_eq!(info.pays_fee, Pays::Yes);
// #[weight = (1000, Pays::No)]
let info = Call::<TraitImpl>::f02 {}.get_dispatch_info();
// #[pallet::weight((1000, Pays::No))]
let info = Call::<Runtime>::f02 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(1000, 0));
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::No);
// #[weight = (1000, DispatchClass::Operational, Pays::No)]
let info = Call::<TraitImpl>::f03 {}.get_dispatch_info();
// #[pallet::weight((1000, DispatchClass::Operational, Pays::No))]
let info = Call::<Runtime>::f03 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(1000, 0));
assert_eq!(info.class, DispatchClass::Operational);
assert_eq!(info.pays_fee, Pays::No);
// #[weight = ((_a * 10 + _eb * 1) as Weight, DispatchClass::Normal, Pays::Yes)]
let info = Call::<TraitImpl>::f11 { _a: 13, _eb: 20 }.get_dispatch_info();
// #[pallet::weight(((_a * 10 + _eb * 1) as u64, DispatchClass::Normal, Pays::Yes))]
let info = Call::<Runtime>::f11 { a: 13, eb: 20 }.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(150, 0)); // 13*10 + 20
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::Yes);
// #[weight = (0, DispatchClass::Operational, Pays::Yes)]
let info = Call::<TraitImpl>::f12 { _a: 10, _eb: 20 }.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(0, 0));
// #[pallet::weight((0, DispatchClass::Operational, Pays::Yes))]
let info = Call::<Runtime>::f12 { a: 10, eb: 20 }.get_dispatch_info();
assert_eq!(info.weight, Weight::zero());
assert_eq!(info.class, DispatchClass::Operational);
assert_eq!(info.pays_fee, Pays::Yes);
// #[weight = T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) + 10_000]
let info = Call::<TraitImpl>::f20 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(12300, 0)); // 100*3 + 1000*2 + 10_1000
// #[pallet::weight(T::DbWeight::get().reads(3) + T::DbWeight::get().writes(2) +
// Weight::from_all(10_000))]
let info = Call::<Runtime>::f20 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(12300, 10000)); // 100*3 + 1000*2 + 10_1000
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::Yes);
// #[weight = T::DbWeight::get().reads_writes(6, 5) + 40_000]
let info = Call::<TraitImpl>::f21 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(45600, 0)); // 100*6 + 1000*5 + 40_1000
// #[pallet::weight(T::DbWeight::get().reads_writes(6, 5) + Weight::from_all(40_000))]
let info = Call::<Runtime>::f21 {}.get_dispatch_info();
assert_eq!(info.weight, Weight::from_parts(45600, 40000)); // 100*6 + 1000*5 + 40_1000
assert_eq!(info.class, DispatchClass::Normal);
assert_eq!(info.pays_fee, Pays::Yes);
}
+156 -85
View File
@@ -829,77 +829,153 @@ pub mod tests {
PalletStorageMetadataIR, StorageEntryMetadataIR, StorageEntryModifierIR,
StorageEntryTypeIR, StorageHasherIR,
};
use codec::{Codec, EncodeLike};
use frame_support::traits::CrateVersion;
use sp_io::{MultiRemovalResults, TestExternalities};
use sp_runtime::{generic, traits::BlakeTwo256, BuildStorage};
use sp_std::result;
/// A PalletInfo implementation which just panics.
pub struct PanicPalletInfo;
pub use self::frame_system::{Config, Pallet};
impl crate::traits::PalletInfo for PanicPalletInfo {
fn index<P: 'static>() -> Option<usize> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
#[pallet]
pub mod frame_system {
#[allow(unused)]
use super::{frame_system, frame_system::pallet_prelude::*};
pub use crate::dispatch::RawOrigin;
use crate::pallet_prelude::*;
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: 'static {
type BlockNumber: Parameter + Default + MaxEncodedLen;
type AccountId;
type BaseCallFilter: crate::traits::Contains<Self::RuntimeCall>;
type RuntimeOrigin;
type RuntimeCall;
type PalletInfo: crate::traits::PalletInfo;
type DbWeight: Get<crate::weights::RuntimeDbWeight>;
}
fn name<P: 'static>() -> Option<&'static str> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
#[pallet::error]
pub enum Error<T> {
/// Required by construct_runtime
CallFiltered,
}
fn module_name<P: 'static>() -> Option<&'static str> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
#[pallet::origin]
pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::storage]
pub type Data<T> = StorageMap<_, Twox64Concat, u32, u64, ValueQuery>;
#[pallet::storage]
pub type OptionLinkedMap<T> = StorageMap<_, Blake2_128Concat, u32, u32, OptionQuery>;
#[pallet::storage]
#[pallet::getter(fn generic_data)]
pub type GenericData<T: Config> =
StorageMap<_, Identity, T::BlockNumber, T::BlockNumber, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn generic_data2)]
pub type GenericData2<T: Config> =
StorageMap<_, Blake2_128Concat, T::BlockNumber, T::BlockNumber, OptionQuery>;
#[pallet::storage]
pub type DataDM<T> =
StorageDoubleMap<_, Twox64Concat, u32, Blake2_128Concat, u32, u64, ValueQuery>;
#[pallet::storage]
pub type GenericDataDM<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::BlockNumber,
Identity,
T::BlockNumber,
T::BlockNumber,
ValueQuery,
>;
#[pallet::storage]
pub type GenericData2DM<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::BlockNumber,
Twox64Concat,
T::BlockNumber,
T::BlockNumber,
OptionQuery,
>;
#[pallet::storage]
#[pallet::unbounded]
pub type AppendableDM<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
u32,
Blake2_128Concat,
T::BlockNumber,
Vec<u32>,
ValueQuery,
>;
#[pallet::genesis_config]
pub struct GenesisConfig {
pub data: Vec<(u32, u64)>,
pub test_config: Vec<(u32, u32, u64)>,
}
fn crate_version<P: 'static>() -> Option<CrateVersion> {
unimplemented!("PanicPalletInfo mustn't be triggered by tests");
impl Default for GenesisConfig {
fn default() -> Self {
Self { data: vec![(15u32, 42u64)], test_config: vec![(15u32, 16u32, 42u64)] }
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
for (k, v) in &self.data {
<Data<T>>::insert(k, v);
}
for (k1, k2, v) in &self.test_config {
<DataDM<T>>::insert(k1, k2, v);
}
}
}
pub mod pallet_prelude {
pub type OriginFor<T> = <T as super::Config>::RuntimeOrigin;
}
}
pub trait Config: 'static {
type BlockNumber: Codec + EncodeLike + Default + TypeInfo;
type RuntimeOrigin;
type PalletInfo: crate::traits::PalletInfo;
type DbWeight: crate::traits::Get<crate::weights::RuntimeDbWeight>;
}
type BlockNumber = u32;
type AccountId = u32;
type Header = generic::Header<BlockNumber, BlakeTwo256>;
type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, (), ()>;
type Block = generic::Block<Header, UncheckedExtrinsic>;
mod module {
#![allow(dead_code)]
use super::Config;
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {}
crate::construct_runtime!(
pub enum Runtime
where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: self::frame_system,
}
}
);
use self::module::Module;
decl_storage! {
trait Store for Module<T: Config> as Test {
pub Value get(fn value): u64;
pub Data get(fn data) build(|_| vec![(15u32, 42u64)]):
map hasher(twox_64_concat) u32 => u64;
pub OptionLinkedMap: map hasher(blake2_128_concat) u32 => Option<u32>;
pub GenericData get(fn generic_data):
map hasher(identity) T::BlockNumber => T::BlockNumber;
pub GenericData2 get(fn generic_data2):
map hasher(blake2_128_concat) T::BlockNumber => Option<T::BlockNumber>;
pub DataDM config(test_config) build(|_| vec![(15u32, 16u32, 42u64)]):
double_map hasher(twox_64_concat) u32, hasher(blake2_128_concat) u32 => u64;
pub GenericDataDM:
double_map hasher(blake2_128_concat) T::BlockNumber, hasher(identity) T::BlockNumber
=> T::BlockNumber;
pub GenericData2DM:
double_map hasher(blake2_128_concat) T::BlockNumber, hasher(twox_64_concat) T::BlockNumber
=> Option<T::BlockNumber>;
pub AppendableDM:
double_map hasher(blake2_128_concat) u32, hasher(blake2_128_concat) T::BlockNumber => Vec<u32>;
}
}
struct Test;
impl Config for Test {
type BlockNumber = u32;
type RuntimeOrigin = u32;
type PalletInfo = PanicPalletInfo;
impl Config for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type BaseCallFilter = crate::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type PalletInfo = PalletInfo;
type DbWeight = ();
}
@@ -907,8 +983,6 @@ pub mod tests {
GenesisConfig::default().build_storage().unwrap().into()
}
type Map = Data;
trait Sorted {
fn sorted(self) -> Self;
}
@@ -925,15 +999,15 @@ pub mod tests {
new_test_ext().execute_with(|| {
#[crate::storage_alias]
type GenericData2<T> = StorageMap<
Test,
System,
Blake2_128Concat,
<T as Config>::BlockNumber,
<T as Config>::BlockNumber,
>;
assert_eq!(Module::<Test>::generic_data2(5), None);
GenericData2::<Test>::insert(5, 5);
assert_eq!(Module::<Test>::generic_data2(5), Some(5));
assert_eq!(Pallet::<Runtime>::generic_data2(5), None);
GenericData2::<Runtime>::insert(5, 5);
assert_eq!(Pallet::<Runtime>::generic_data2(5), Some(5));
/// Some random docs that ensure that docs are accepted
#[crate::storage_alias]
@@ -1004,6 +1078,8 @@ pub mod tests {
#[test]
fn map_issue_3318() {
new_test_ext().execute_with(|| {
type OptionLinkedMap = self::frame_system::OptionLinkedMap<Runtime>;
OptionLinkedMap::insert(1, 1);
assert_eq!(OptionLinkedMap::get(1), Some(1));
OptionLinkedMap::insert(1, 2);
@@ -1014,6 +1090,8 @@ pub mod tests {
#[test]
fn map_swap_works() {
new_test_ext().execute_with(|| {
type OptionLinkedMap = self::frame_system::OptionLinkedMap<Runtime>;
OptionLinkedMap::insert(0, 0);
OptionLinkedMap::insert(1, 1);
OptionLinkedMap::insert(2, 2);
@@ -1043,6 +1121,8 @@ pub mod tests {
#[test]
fn double_map_swap_works() {
new_test_ext().execute_with(|| {
type DataDM = self::frame_system::DataDM<Runtime>;
DataDM::insert(0, 1, 1);
DataDM::insert(1, 0, 2);
DataDM::insert(1, 1, 3);
@@ -1075,6 +1155,8 @@ pub mod tests {
#[test]
fn map_basic_insert_remove_should_work() {
new_test_ext().execute_with(|| {
type Map = self::frame_system::Data<Runtime>;
// initialized during genesis
assert_eq!(Map::get(&15u32), 42u64);
@@ -1101,6 +1183,8 @@ pub mod tests {
#[test]
fn map_iteration_should_work() {
new_test_ext().execute_with(|| {
type Map = self::frame_system::Data<Runtime>;
assert_eq!(Map::iter().collect::<Vec<_>>().sorted(), vec![(15, 42)]);
// insert / remove
let key = 17u32;
@@ -1154,7 +1238,7 @@ pub mod tests {
fn double_map_basic_insert_remove_remove_prefix_with_commit_should_work() {
let key1 = 17u32;
let key2 = 18u32;
type DoubleMap = DataDM;
type DoubleMap = self::frame_system::DataDM<Runtime>;
let mut e = new_test_ext();
e.execute_with(|| {
// initialized during genesis
@@ -1199,7 +1283,7 @@ pub mod tests {
new_test_ext().execute_with(|| {
let key1 = 17u32;
let key2 = 18u32;
type DoubleMap = DataDM;
type DoubleMap = self::frame_system::DataDM<Runtime>;
// initialized during genesis
assert_eq!(DoubleMap::get(&15u32, &16u32), 42u64);
@@ -1247,7 +1331,7 @@ pub mod tests {
#[test]
fn double_map_append_should_work() {
new_test_ext().execute_with(|| {
type DoubleMap = AppendableDM<Test>;
type DoubleMap = self::frame_system::AppendableDM<Runtime>;
let key1 = 17u32;
let key2 = 18u32;
@@ -1261,7 +1345,7 @@ pub mod tests {
#[test]
fn double_map_mutate_exists_should_work() {
new_test_ext().execute_with(|| {
type DoubleMap = DataDM;
type DoubleMap = self::frame_system::DataDM<Runtime>;
let (key1, key2) = (11, 13);
@@ -1278,8 +1362,8 @@ pub mod tests {
#[test]
fn double_map_try_mutate_exists_should_work() {
new_test_ext().execute_with(|| {
type DoubleMap = DataDM;
type TestResult = result::Result<(), &'static str>;
type DoubleMap = self::frame_system::DataDM<Runtime>;
type TestResult = Result<(), &'static str>;
let (key1, key2) = (11, 13);
@@ -1310,15 +1394,8 @@ pub mod tests {
fn expected_metadata() -> PalletStorageMetadataIR {
PalletStorageMetadataIR {
prefix: "Test",
prefix: "System",
entries: vec![
StorageEntryMetadataIR {
name: "Value",
modifier: StorageEntryModifierIR::Default,
ty: StorageEntryTypeIR::Plain(scale_info::meta_type::<u64>()),
default: vec![0, 0, 0, 0, 0, 0, 0, 0],
docs: vec![],
},
StorageEntryMetadataIR {
name: "Data",
modifier: StorageEntryModifierIR::Default,
@@ -1422,7 +1499,7 @@ pub mod tests {
#[test]
fn store_metadata() {
let metadata = Module::<Test>::storage_metadata();
let metadata = Pallet::<Runtime>::storage_metadata();
pretty_assertions::assert_eq!(expected_metadata(), metadata);
}
@@ -1441,12 +1518,6 @@ pub mod tests {
assert_eq!(300, StorageParameter::get());
})
}
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub static Members: Vec<u64> = vec![];
pub const Foo: Option<u64> = None;
}
}
/// Prelude to be used alongside pallet macro, for ease of use.
@@ -514,43 +514,12 @@ where
mod test_iterators {
use crate::{
hash::StorageHasher,
storage::{generator::StorageDoubleMap, unhashed, IterableStorageDoubleMap},
storage::{
generator::{tests::*, StorageDoubleMap},
unhashed,
},
};
use codec::{Decode, Encode};
pub trait Config: 'static {
type RuntimeOrigin;
type BlockNumber;
type PalletInfo: crate::traits::PalletInfo;
type DbWeight: crate::traits::Get<crate::weights::RuntimeDbWeight>;
}
crate::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {}
}
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
struct NoDef(u32);
crate::decl_storage! {
trait Store for Module<T: Config> as Test {
DoubleMap: double_map hasher(blake2_128_concat) u16, hasher(twox_64_concat) u32 => u64;
}
}
fn key_before_prefix(mut prefix: Vec<u8>) -> Vec<u8> {
let last = prefix.iter_mut().last().unwrap();
assert!(*last != 0, "mock function not implemented for this prefix");
*last -= 1;
prefix
}
fn key_after_prefix(mut prefix: Vec<u8>) -> Vec<u8> {
let last = prefix.iter_mut().last().unwrap();
assert!(*last != 255, "mock function not implemented for this prefix");
*last += 1;
prefix
}
use codec::Encode;
#[test]
fn double_map_iter_from() {
@@ -589,6 +558,8 @@ mod test_iterators {
#[test]
fn double_map_reversible_reversible_iteration() {
sp_io::TestExternalities::default().execute_with(|| {
type DoubleMap = self::frame_system::DoubleMap<Runtime>;
// All map iterator
let prefix = DoubleMap::prefix_hash();
@@ -349,43 +349,12 @@ impl<K: FullEncode, V: FullCodec, G: StorageMap<K, V>> storage::StorageMap<K, V>
mod test_iterators {
use crate::{
hash::StorageHasher,
storage::{generator::StorageMap, unhashed, IterableStorageMap},
storage::{
generator::{tests::*, StorageMap},
unhashed,
},
};
use codec::{Decode, Encode};
pub trait Config: 'static {
type RuntimeOrigin;
type BlockNumber;
type PalletInfo: crate::traits::PalletInfo;
type DbWeight: crate::traits::Get<crate::weights::RuntimeDbWeight>;
}
crate::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {}
}
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
struct NoDef(u32);
crate::decl_storage! {
trait Store for Module<T: Config> as Test {
Map: map hasher(blake2_128_concat) u16 => u64;
}
}
fn key_before_prefix(mut prefix: Vec<u8>) -> Vec<u8> {
let last = prefix.iter_mut().last().unwrap();
assert!(*last != 0, "mock function not implemented for this prefix");
*last -= 1;
prefix
}
fn key_after_prefix(mut prefix: Vec<u8>) -> Vec<u8> {
let last = prefix.iter_mut().last().unwrap();
assert!(*last != 255, "mock function not implemented for this prefix");
*last += 1;
prefix
}
use codec::Encode;
#[test]
fn map_iter_from() {
@@ -413,6 +382,8 @@ mod test_iterators {
#[test]
fn map_reversible_reversible_iteration() {
sp_io::TestExternalities::default().execute_with(|| {
type Map = self::frame_system::Map<Runtime>;
// All map iterator
let prefix = Map::prefix_hash();
@@ -35,47 +35,123 @@ pub use nmap::StorageNMap;
pub use value::StorageValue;
#[cfg(test)]
#[allow(dead_code)]
mod tests {
use crate::{
assert_noop, assert_ok,
storage::{generator::StorageValue, unhashed, IterableStorageMap},
};
use codec::Encode;
use sp_io::TestExternalities;
use sp_runtime::{generic, traits::BlakeTwo256, BuildStorage};
struct Runtime;
use crate::{
assert_noop, assert_ok,
storage::{generator::StorageValue, unhashed},
};
pub trait Config: 'static {
type RuntimeOrigin;
type BlockNumber;
type PalletInfo: crate::traits::PalletInfo;
type DbWeight: crate::traits::Get<crate::weights::RuntimeDbWeight>;
#[crate::pallet]
pub mod frame_system {
#[allow(unused)]
use super::{frame_system, frame_system::pallet_prelude::*};
pub use crate::dispatch::RawOrigin;
use crate::pallet_prelude::*;
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
pub trait Config: 'static {
type BlockNumber;
type AccountId;
type BaseCallFilter: crate::traits::Contains<Self::RuntimeCall>;
type RuntimeOrigin;
type RuntimeCall;
type PalletInfo: crate::traits::PalletInfo;
type DbWeight: Get<crate::weights::RuntimeDbWeight>;
}
#[pallet::origin]
pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;
#[pallet::error]
pub enum Error<T> {
/// Required by construct_runtime
CallFiltered,
}
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::storage]
pub type Value<T> = StorageValue<_, (u64, u64), ValueQuery>;
#[pallet::storage]
pub type Map<T> = StorageMap<_, Blake2_128Concat, u16, u64, ValueQuery>;
#[pallet::storage]
pub type NumberMap<T> = StorageMap<_, Identity, u32, u64, ValueQuery>;
#[pallet::storage]
pub type DoubleMap<T> =
StorageDoubleMap<_, Blake2_128Concat, u16, Twox64Concat, u32, u64, ValueQuery>;
#[pallet::storage]
pub type NMap<T> = StorageNMap<
_,
(storage::Key<Blake2_128Concat, u16>, storage::Key<Twox64Concat, u32>),
u64,
ValueQuery,
>;
pub mod pallet_prelude {
pub type OriginFor<T> = <T as super::Config>::RuntimeOrigin;
}
}
impl Config for Runtime {
type RuntimeOrigin = u32;
type BlockNumber = u32;
type PalletInfo = crate::tests::PanicPalletInfo;
type BlockNumber = u32;
type AccountId = u32;
type Header = generic::Header<BlockNumber, BlakeTwo256>;
type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, RuntimeCall, (), ()>;
type Block = generic::Block<Header, UncheckedExtrinsic>;
crate::construct_runtime!(
pub enum Runtime
where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: self::frame_system,
}
);
impl self::frame_system::Config for Runtime {
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type BaseCallFilter = crate::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type PalletInfo = PalletInfo;
type DbWeight = ();
}
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {}
pub fn key_before_prefix(mut prefix: Vec<u8>) -> Vec<u8> {
let last = prefix.iter_mut().last().unwrap();
assert_ne!(*last, 0, "mock function not implemented for this prefix");
*last -= 1;
prefix
}
crate::decl_storage! {
trait Store for Module<T: Config> as Runtime {
Value get(fn value) config(): (u64, u64);
NumberMap: map hasher(identity) u32 => u64;
DoubleMap: double_map hasher(identity) u32, hasher(identity) u32 => u64;
}
pub fn key_after_prefix(mut prefix: Vec<u8>) -> Vec<u8> {
let last = prefix.iter_mut().last().unwrap();
assert_ne!(*last, 255, "mock function not implemented for this prefix");
*last += 1;
prefix
}
#[test]
fn value_translate_works() {
let t = GenesisConfig::default().build_storage().unwrap();
TestExternalities::new(t).execute_with(|| {
type Value = self::frame_system::Value<Runtime>;
// put the old value `1111u32` in the storage.
let key = Value::storage_value_final_key();
unhashed::put_raw(&key, &1111u32.encode());
@@ -96,7 +172,9 @@ mod tests {
fn map_translate_works() {
let t = GenesisConfig::default().build_storage().unwrap();
TestExternalities::new(t).execute_with(|| {
// start with a map of u32 -> u32.
type NumberMap = self::frame_system::NumberMap<Runtime>;
// start with a map of u32 -> u64.
for i in 0u32..100u32 {
unhashed::put(&NumberMap::hashed_key_for(&i), &(i as u64));
}
@@ -125,6 +203,10 @@ mod tests {
fn try_mutate_works() {
let t = GenesisConfig::default().build_storage().unwrap();
TestExternalities::new(t).execute_with(|| {
type Value = self::frame_system::Value<Runtime>;
type NumberMap = self::frame_system::NumberMap<Runtime>;
type DoubleMap = self::frame_system::DoubleMap<Runtime>;
assert_eq!(Value::get(), (0, 0));
assert_eq!(NumberMap::get(0), 0);
assert_eq!(DoubleMap::get(0, 0), 0);
@@ -462,43 +462,12 @@ impl<K: ReversibleKeyGenerator, V: FullCodec, G: StorageNMap<K, V>>
mod test_iterators {
use crate::{
hash::StorageHasher,
storage::{generator::StorageNMap, unhashed, IterableStorageNMap},
storage::{
generator::{tests::*, StorageNMap},
unhashed,
},
};
use codec::{Decode, Encode};
pub trait Config: 'static {
type RuntimeOrigin;
type BlockNumber;
type PalletInfo: crate::traits::PalletInfo;
type DbWeight: crate::traits::Get<crate::weights::RuntimeDbWeight>;
}
crate::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::RuntimeOrigin, system=self {}
}
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
struct NoDef(u32);
crate::decl_storage! {
trait Store for Module<T: Config> as Test {
NMap: nmap hasher(blake2_128_concat) u16, hasher(twox_64_concat) u32 => u64;
}
}
fn key_before_prefix(mut prefix: Vec<u8>) -> Vec<u8> {
let last = prefix.iter_mut().last().unwrap();
assert!(*last != 0, "mock function not implemented for this prefix");
*last -= 1;
prefix
}
fn key_after_prefix(mut prefix: Vec<u8>) -> Vec<u8> {
let last = prefix.iter_mut().last().unwrap();
assert!(*last != 255, "mock function not implemented for this prefix");
*last += 1;
prefix
}
use codec::Encode;
#[test]
fn n_map_iter_from() {
@@ -545,22 +514,18 @@ mod test_iterators {
#[test]
fn n_map_double_map_identical_key() {
sp_io::TestExternalities::default().execute_with(|| {
use crate::hash::{Blake2_128Concat, Twox64Concat};
type NMap = self::frame_system::NMap<Runtime>;
NMap::insert((1, 2), 50);
let key_hash = NMap::hashed_key_for((1, 2));
{
#[crate::storage_alias]
type NMap = StorageDoubleMap<
Test,
crate::Blake2_128Concat,
u16,
crate::Twox64Concat,
u32,
u64,
>;
type NMap = StorageDoubleMap<System, Blake2_128Concat, u16, Twox64Concat, u32, u64>;
let value = NMap::get(1, 2).unwrap();
assert_eq!(value, 50);
assert_eq!(NMap::get(1, 2), Some(50));
assert_eq!(NMap::hashed_key_for(1, 2), key_hash);
}
});
@@ -569,6 +534,8 @@ mod test_iterators {
#[test]
fn n_map_reversible_reversible_iteration() {
sp_io::TestExternalities::default().execute_with(|| {
type NMap = self::frame_system::NMap<Runtime>;
// All map iterator
let prefix = NMap::prefix_hash();
+1
View File
@@ -39,6 +39,7 @@ futures = { version = "0.3.21", optional = true }
dyn-clonable = { version = "0.9.0", optional = true }
thiserror = { version = "1.0.30", optional = true }
bitflags = "1.3"
paste = "1.0.7"
# full crypto
array-bytes = { version = "4.1", optional = true }
+55 -30
View File
@@ -53,6 +53,7 @@ pub mod hashing;
pub use hashing::{blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64};
pub mod crypto;
pub mod hexdisplay;
pub use paste;
pub mod defer;
pub mod ecdsa;
@@ -403,38 +404,62 @@ pub const MAX_POSSIBLE_ALLOCATION: u32 = 33554432; // 2^25 bytes, 32 MiB
#[rustfmt::skip]
macro_rules! generate_feature_enabled_macro {
( $macro_name:ident, $feature_name:meta, $d:tt ) => {
/// Enable/disable the given code depending on
#[doc = concat!("`", stringify!($feature_name), "`")]
/// being enabled for the crate or not.
///
/// # Example
///
/// ```nocompile
/// // Will add the code depending on the feature being enabled or not.
#[doc = concat!(stringify!($macro_name), "!( println!(\"Hello\") )")]
/// ```
#[cfg($feature_name)]
#[macro_export]
macro_rules! $macro_name {
( $d ( $d input:tt )* ) => {
$d ( $d input )*
$crate::paste::paste!{
/// Enable/disable the given code depending on
#[doc = concat!("`", stringify!($feature_name), "`")]
/// being enabled for the crate or not.
///
/// # Example
///
/// ```nocompile
/// // Will add the code depending on the feature being enabled or not.
#[doc = concat!(stringify!($macro_name), "!( println!(\"Hello\") )")]
/// ```
#[cfg($feature_name)]
#[macro_export]
macro_rules! [<_ $macro_name>] {
( $d ( $d input:tt )* ) => {
$d ( $d input )*
}
}
}
/// Enable/disable the given code depending on
#[doc = concat!("`", stringify!($feature_name), "`")]
/// being enabled for the crate or not.
///
/// # Example
///
/// ```nocompile
/// // Will add the code depending on the feature being enabled or not.
#[doc = concat!(stringify!($macro_name), "!( println!(\"Hello\") )")]
/// ```
#[cfg(not($feature_name))]
#[macro_export]
macro_rules! $macro_name {
( $d ( $d input:tt )* ) => {};
/// Enable/disable the given code depending on
#[doc = concat!("`", stringify!($feature_name), "`")]
/// being enabled for the crate or not.
///
/// # Example
///
/// ```nocompile
/// // Will add the code depending on the feature being enabled or not.
#[doc = concat!(stringify!($macro_name), "!( println!(\"Hello\") )")]
/// ```
#[cfg(not($feature_name))]
#[macro_export]
macro_rules! [<_ $macro_name>] {
( $d ( $d input:tt )* ) => {};
}
// Work around for: <https://github.com/rust-lang/rust/pull/52234>
#[doc(hidden)]
pub use [<_ $macro_name>] as $macro_name;
}
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn generate_feature_enabled_macro_panics() {
generate_feature_enabled_macro!(if_test, test, $);
if_test!(panic!("This should panic"));
}
#[test]
fn generate_feature_enabled_macro_works() {
generate_feature_enabled_macro!(if_not_test, not(test), $);
if_not_test!(panic!("This should not panic"));
}
}
+20
View File
@@ -1054,3 +1054,23 @@ mod tests {
});
}
}
// NOTE: we have to test the sp_core stuff also from a different crate to check that the macro
// can access the sp_core crate.
#[cfg(test)]
mod sp_core_tests {
use super::*;
#[test]
#[should_panic]
fn generate_feature_enabled_macro_panics() {
sp_core::generate_feature_enabled_macro!(if_test, test, $);
if_test!(panic!("This should panic"));
}
#[test]
fn generate_feature_enabled_macro_works() {
sp_core::generate_feature_enabled_macro!(if_not_test, not(test), $);
if_not_test!(panic!("This should not panic"));
}
}