From 496dfedf2ede3edb59b0618a3e9d30852b86c1b0 Mon Sep 17 00:00:00 2001 From: brettkolodny <brettkolodny@gmail.com> Date: Thu, 11 Feb 2021 21:46:17 -0500 Subject: [PATCH] Migrated mock to construct_runtime macro (#369) --- authority/src/mock.rs | 2 +- benchmarking/Cargo.toml | 2 + benchmarking/src/tests.rs | 86 ++++++++++++++++++++++++--------------- 3 files changed, 57 insertions(+), 33 deletions(-) diff --git a/authority/src/mock.rs b/authority/src/mock.rs index 0035cb9..2549a66 100644 --- a/authority/src/mock.rs +++ b/authority/src/mock.rs @@ -43,7 +43,7 @@ impl frame_system::Config for Runtime { type BlockWeights = (); type BlockLength = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); diff --git a/benchmarking/Cargo.toml b/benchmarking/Cargo.toml index 2d24f05..675cf5e 100644 --- a/benchmarking/Cargo.toml +++ b/benchmarking/Cargo.toml @@ -8,6 +8,7 @@ authors = ["Laminar Developers <hello@laminar.one>"] edition = "2018" [dependencies] +serde = { version = "1.0.111", optional = true } paste = "0.1.16" codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false } sp-api = { version = "2.0.1", default-features = false } @@ -25,6 +26,7 @@ hex-literal = "0.2.1" [features] default = [ "std" ] std = [ + "serde", "codec/std", "sp-runtime-interface/std", "sp-runtime/std", diff --git a/benchmarking/src/tests.rs b/benchmarking/src/tests.rs index e6d3ee8..2c015f3 100644 --- a/benchmarking/src/tests.rs +++ b/benchmarking/src/tests.rs @@ -4,43 +4,48 @@ use super::*; use frame_benchmarking::account; -use frame_support::{ - assert_err, assert_ok, decl_module, decl_storage, dispatch::DispatchResult, ensure, impl_outer_origin, -}; -use frame_system::{ensure_none, ensure_signed, RawOrigin}; +use frame_support::{assert_err, assert_ok, construct_runtime, ensure}; +use frame_system::RawOrigin; use sp_runtime::{ testing::{Header, H256}, traits::{BlakeTwo256, IdentityLookup}, }; use sp_std::prelude::*; -decl_storage! { - trait Store for Module<T: Config> as Test { - Value get(fn value): Option<u32>; +mod test { + use frame_support::{decl_module, decl_storage, dispatch::DispatchResult}; + use frame_system::{ensure_none, ensure_signed}; + use sp_std::prelude::*; + + pub trait Config: frame_system::Config { + type Event; + type BlockNumber; } -} -decl_module! { - pub struct Module<T: Config> for enum Call where origin: T::Origin { - #[weight = 0] - fn set_value(origin, n: u32) -> DispatchResult { - let _sender = ensure_signed(origin)?; - Value::put(n); - Ok(()) + decl_storage! { + trait Store for Module<T: Config> as Test { + pub Value get(fn value) config(): Option<u32>; } + } - #[weight = 0] - fn dummy(origin, _n: u32) -> DispatchResult { - let _sender = ensure_none(origin)?; - Ok(()) + decl_module! { + pub struct Module<T: Config> for enum Call where origin: T::Origin { + #[weight = 0] + fn set_value(origin, n: u32) -> DispatchResult { + let _sender = ensure_signed(origin)?; + Value::put(n); + Ok(()) + } + + #[weight = 0] + fn dummy(origin, _n: u32) -> DispatchResult { + let _sender = ensure_none(origin)?; + Ok(()) + } } } } -impl_outer_origin! { - pub enum Origin for Test {} -} - pub trait Config: frame_system::Config { type Event; type BlockNumber; @@ -48,26 +53,23 @@ pub trait Config: frame_system::Config { type AccountId = u128; -#[derive(Clone, Eq, PartialEq)] -pub struct Test; - impl frame_system::Config for Test { type Origin = Origin; type Index = u64; type BlockNumber = u64; type Hash = H256; - type Call = (); + type Call = Call; type Hashing = BlakeTwo256; type AccountId = AccountId; type Lookup = IdentityLookup<Self::AccountId>; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = (); type DbWeight = (); type BlockWeights = (); type BlockLength = (); type Version = (); - type PalletInfo = (); + type PalletInfo = PalletInfo; type AccountData = (); type OnNewAccount = (); type OnKilledAccount = (); @@ -76,11 +78,31 @@ impl frame_system::Config for Test { type SS58Prefix = (); } +impl tests::test::Config for Test { + type Event = Event; + type BlockNumber = u32; +} + impl Config for Test { - type Event = (); + type Event = Event; type BlockNumber = u32; } +pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>; +pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, u32, ()>; + +construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Storage, Config, Event<T>}, + Pallet: test::{Module, Call, Storage, Config}, + + } +); + // This function basically just builds a genesis storage key/value store // according to our desired mockup. fn new_test_ext() -> sp_io::TestExternalities { @@ -91,7 +113,7 @@ fn new_test_ext() -> sp_io::TestExternalities { } runtime_benchmarks! { - { Test, self } + { Test, test } _ { // Define a common range for `b`. @@ -103,7 +125,7 @@ runtime_benchmarks! { let caller = account::<AccountId>("caller", 0, 0); }: _ (RawOrigin::Signed(caller), b.into()) verify { - assert_eq!(Value::get(), Some(b)); + assert_eq!(Pallet::value(), Some(b)); } other_name { -- GitLab