Skip to content

Commit 496dfed

Browse files
authored
Migrated mock to construct_runtime macro (#369)
1 parent 15b3818 commit 496dfed

File tree

3 files changed

+57
-33
lines changed

3 files changed

+57
-33
lines changed

authority/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl frame_system::Config for Runtime {
4343
type BlockWeights = ();
4444
type BlockLength = ();
4545
type Version = ();
46-
type PalletInfo = ();
46+
type PalletInfo = PalletInfo;
4747
type AccountData = ();
4848
type OnNewAccount = ();
4949
type OnKilledAccount = ();

benchmarking/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authors = ["Laminar Developers <[email protected]>"]
88
edition = "2018"
99

1010
[dependencies]
11+
serde = { version = "1.0.111", optional = true }
1112
paste = "0.1.16"
1213
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
1314
sp-api = { version = "2.0.1", default-features = false }
@@ -25,6 +26,7 @@ hex-literal = "0.2.1"
2526
[features]
2627
default = [ "std" ]
2728
std = [
29+
"serde",
2830
"codec/std",
2931
"sp-runtime-interface/std",
3032
"sp-runtime/std",

benchmarking/src/tests.rs

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,72 @@
44

55
use super::*;
66
use frame_benchmarking::account;
7-
use frame_support::{
8-
assert_err, assert_ok, decl_module, decl_storage, dispatch::DispatchResult, ensure, impl_outer_origin,
9-
};
10-
use frame_system::{ensure_none, ensure_signed, RawOrigin};
7+
use frame_support::{assert_err, assert_ok, construct_runtime, ensure};
8+
use frame_system::RawOrigin;
119
use sp_runtime::{
1210
testing::{Header, H256},
1311
traits::{BlakeTwo256, IdentityLookup},
1412
};
1513
use sp_std::prelude::*;
1614

17-
decl_storage! {
18-
trait Store for Module<T: Config> as Test {
19-
Value get(fn value): Option<u32>;
15+
mod test {
16+
use frame_support::{decl_module, decl_storage, dispatch::DispatchResult};
17+
use frame_system::{ensure_none, ensure_signed};
18+
use sp_std::prelude::*;
19+
20+
pub trait Config: frame_system::Config {
21+
type Event;
22+
type BlockNumber;
2023
}
21-
}
2224

23-
decl_module! {
24-
pub struct Module<T: Config> for enum Call where origin: T::Origin {
25-
#[weight = 0]
26-
fn set_value(origin, n: u32) -> DispatchResult {
27-
let _sender = ensure_signed(origin)?;
28-
Value::put(n);
29-
Ok(())
25+
decl_storage! {
26+
trait Store for Module<T: Config> as Test {
27+
pub Value get(fn value) config(): Option<u32>;
3028
}
29+
}
3130

32-
#[weight = 0]
33-
fn dummy(origin, _n: u32) -> DispatchResult {
34-
let _sender = ensure_none(origin)?;
35-
Ok(())
31+
decl_module! {
32+
pub struct Module<T: Config> for enum Call where origin: T::Origin {
33+
#[weight = 0]
34+
fn set_value(origin, n: u32) -> DispatchResult {
35+
let _sender = ensure_signed(origin)?;
36+
Value::put(n);
37+
Ok(())
38+
}
39+
40+
#[weight = 0]
41+
fn dummy(origin, _n: u32) -> DispatchResult {
42+
let _sender = ensure_none(origin)?;
43+
Ok(())
44+
}
3645
}
3746
}
3847
}
3948

40-
impl_outer_origin! {
41-
pub enum Origin for Test {}
42-
}
43-
4449
pub trait Config: frame_system::Config {
4550
type Event;
4651
type BlockNumber;
4752
}
4853

4954
type AccountId = u128;
5055

51-
#[derive(Clone, Eq, PartialEq)]
52-
pub struct Test;
53-
5456
impl frame_system::Config for Test {
5557
type Origin = Origin;
5658
type Index = u64;
5759
type BlockNumber = u64;
5860
type Hash = H256;
59-
type Call = ();
61+
type Call = Call;
6062
type Hashing = BlakeTwo256;
6163
type AccountId = AccountId;
6264
type Lookup = IdentityLookup<Self::AccountId>;
6365
type Header = Header;
64-
type Event = ();
66+
type Event = Event;
6567
type BlockHashCount = ();
6668
type DbWeight = ();
6769
type BlockWeights = ();
6870
type BlockLength = ();
6971
type Version = ();
70-
type PalletInfo = ();
72+
type PalletInfo = PalletInfo;
7173
type AccountData = ();
7274
type OnNewAccount = ();
7375
type OnKilledAccount = ();
@@ -76,11 +78,31 @@ impl frame_system::Config for Test {
7678
type SS58Prefix = ();
7779
}
7880

81+
impl tests::test::Config for Test {
82+
type Event = Event;
83+
type BlockNumber = u32;
84+
}
85+
7986
impl Config for Test {
80-
type Event = ();
87+
type Event = Event;
8188
type BlockNumber = u32;
8289
}
8390

91+
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
92+
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, u32, ()>;
93+
94+
construct_runtime!(
95+
pub enum Test where
96+
Block = Block,
97+
NodeBlock = Block,
98+
UncheckedExtrinsic = UncheckedExtrinsic,
99+
{
100+
System: frame_system::{Module, Call, Storage, Config, Event<T>},
101+
Pallet: test::{Module, Call, Storage, Config},
102+
103+
}
104+
);
105+
84106
// This function basically just builds a genesis storage key/value store
85107
// according to our desired mockup.
86108
fn new_test_ext() -> sp_io::TestExternalities {
@@ -91,7 +113,7 @@ fn new_test_ext() -> sp_io::TestExternalities {
91113
}
92114

93115
runtime_benchmarks! {
94-
{ Test, self }
116+
{ Test, test }
95117

96118
_ {
97119
// Define a common range for `b`.
@@ -103,7 +125,7 @@ runtime_benchmarks! {
103125
let caller = account::<AccountId>("caller", 0, 0);
104126
}: _ (RawOrigin::Signed(caller), b.into())
105127
verify {
106-
assert_eq!(Value::get(), Some(b));
128+
assert_eq!(Pallet::value(), Some(b));
107129
}
108130

109131
other_name {

0 commit comments

Comments
 (0)