Skip to main content

gear_common/
pallet_tests.rs

1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4//! Module contains macros that help to implement Config type
5//! for various pallets of Substrate.
6//! All used types should be in scope.
7
8use frame_support::{pallet_prelude::*, sp_runtime::Perbill, weights::RuntimeDbWeight};
9use frame_system::limits::BlockWeights;
10
11#[macro_export]
12macro_rules! impl_pallet_balances {
13    ($( $tokens:tt )*) => {
14        #[allow(dead_code)]
15        type BalancesConfigDustRemoval = ();
16
17        mod pallet_tests_balances_config_impl {
18            use super::*;
19
20            $crate::impl_pallet_balances_inner!($( $tokens )*);
21        }
22    };
23}
24
25#[macro_export]
26macro_rules! impl_pallet_balances_inner {
27    ($runtime:ty) => {
28        impl pallet_balances::Config for $runtime {
29            type MaxLocks = ();
30            type MaxFreezes = ConstU32<1>;
31            type MaxReserves = ();
32            type RuntimeFreezeReason = RuntimeFreezeReason;
33            type FreezeIdentifier = RuntimeFreezeReason;
34            type RuntimeHoldReason = RuntimeHoldReason;
35            type ReserveIdentifier = [u8; 8];
36            type Balance = Balance;
37            type DustRemoval = BalancesConfigDustRemoval;
38            type RuntimeEvent = RuntimeEvent;
39            type ExistentialDeposit = ExistentialDeposit;
40            type AccountStore = System;
41            type WeightInfo = ();
42        }
43    };
44
45    ($runtime:ty, DustRemoval = $dust_removal:ty $(, $( $rest:tt )*)?) => {
46        type BalancesConfigDustRemoval = $dust_removal;
47
48        $crate::impl_pallet_balances_inner!($runtime $(, $( $rest )*)?);
49    };
50}
51
52pub const NORMAL_DISPATCH_WEIGHT_RATIO: Perbill = Perbill::from_percent(75);
53pub const MAX_BLOCK: u64 = 250_000_000_000;
54
55frame_support::parameter_types! {
56    pub RuntimeBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults(
57        Weight::from_parts(MAX_BLOCK, u64::MAX),
58        NORMAL_DISPATCH_WEIGHT_RATIO,
59    );
60    pub const SS58Prefix: u8 = 42;
61    pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 1_110, write: 2_300 };
62    pub const MinimumPeriod: u64 = 500;
63}
64
65#[macro_export]
66macro_rules! impl_pallet_system {
67    ($( $tokens:tt )*) => {
68        #[allow(dead_code)]
69        type SystemConfigDbWeight = $crate::pallet_tests::DbWeight;
70        #[allow(dead_code)]
71        type SystemConfigBlockWeights = $crate::pallet_tests::RuntimeBlockWeights;
72
73        mod pallet_tests_system_config_impl {
74            use super::*;
75
76            $crate::impl_pallet_system_inner!($( $tokens )*);
77        }
78    };
79}
80
81#[macro_export]
82macro_rules! impl_pallet_system_inner {
83    ($runtime:ty$(,)?) => {
84        impl frame_system::Config for $runtime {
85            type BaseCallFilter = frame_support::traits::Everything;
86            type BlockWeights = SystemConfigBlockWeights;
87            type BlockLength = ();
88            type DbWeight = SystemConfigDbWeight;
89            type RuntimeOrigin = RuntimeOrigin;
90            type RuntimeCall = RuntimeCall;
91            type Nonce = u64;
92            type Hash = H256;
93            type Hashing = BlakeTwo256;
94            type AccountId = AccountId;
95            type Lookup = IdentityLookup<Self::AccountId>;
96            type Block = Block;
97            type RuntimeEvent = RuntimeEvent;
98            type BlockHashCount = BlockHashCount;
99            type RuntimeTask = ();
100            type Version = ();
101            type PalletInfo = PalletInfo;
102            type AccountData = pallet_balances::AccountData<Balance>;
103            type OnNewAccount = ();
104            type OnKilledAccount = ();
105            type SystemWeightInfo = ();
106            type SS58Prefix = $crate::pallet_tests::SS58Prefix;
107            type OnSetCode = ();
108            type MaxConsumers = frame_support::traits::ConstU32<16>;
109            type MultiBlockMigrator = ();
110            type SingleBlockMigrations = ();
111            type PreInherents = ();
112            type PostInherents = ();
113            type PostTransactions = ();
114        }
115    };
116
117    ($runtime:ty, DbWeight = $db_weight:ty $(, $( $rest:tt )*)?) => {
118        type SystemConfigDbWeight = $db_weight;
119
120        $crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?);
121    };
122
123    ($runtime:ty, BlockWeights = $block_weights:ty $(, $( $rest:tt )*)?) => {
124        type SystemConfigBlockWeights = $block_weights;
125
126        $crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?);
127    };
128}
129
130#[macro_export]
131macro_rules! impl_pallet_timestamp {
132    ($runtime:ty) => {
133        impl pallet_timestamp::Config for Test {
134            type Moment = u64;
135            type OnTimestampSet = ();
136            type MinimumPeriod = $crate::pallet_tests::MinimumPeriod;
137            type WeightInfo = ();
138        }
139    };
140}
141
142#[macro_export]
143macro_rules! impl_pallet_authorship {
144    ($( $tokens:tt )*) => {
145        #[allow(dead_code)]
146        pub struct FixedBlockAuthor;
147
148        impl FindAuthor<AccountId> for FixedBlockAuthor {
149            fn find_author<'a, I: 'a>(_: I) -> Option<AccountId> {
150                Some(BLOCK_AUTHOR)
151            }
152        }
153
154        #[allow(dead_code)]
155        type AuthorshipFindAuthor = FixedBlockAuthor;
156        #[allow(dead_code)]
157        type AuthorshipEventHandler = ();
158
159        mod pallet_tests_authorship_config_impl {
160            use super::*;
161
162            $crate::impl_pallet_authorship_inner!($( $tokens )*);
163        }
164    };
165}
166
167#[macro_export]
168macro_rules! impl_pallet_authorship_inner {
169    ($runtime:ty$(,)?) => {
170        impl pallet_authorship::Config for $runtime {
171            type FindAuthor = AuthorshipFindAuthor;
172            type EventHandler = AuthorshipEventHandler;
173        }
174    };
175
176    ($runtime:ty, FindAuthor = $find_author:ty $(, $( $rest:tt )*)?) => {
177        type AuthorshipFindAuthor = $find_author;
178
179        $crate::impl_pallet_authorship_inner!($runtime, $($( $rest )*)?);
180    };
181
182    ($runtime:ty, EventHandler = $event_handler:ty $(, $( $rest:tt )*)?) => {
183        type AuthorshipEventHandler = $event_handler;
184
185        $crate::impl_pallet_authorship_inner!($runtime, $($( $rest )*)?);
186    };
187}
188
189#[macro_export]
190macro_rules! impl_pallet_staking {
191    ($( $tokens:tt )*) => {
192        #[allow(dead_code)]
193        pub struct DummyEraPayout;
194        impl pallet_staking::EraPayout<u128> for DummyEraPayout {
195            fn era_payout(
196                _total_staked: u128,
197                total_issuance: u128,
198                _era_duration_millis: u64,
199            ) -> (u128, u128) {
200                // At each era have 1% `total_issuance` increase
201                (Permill::from_percent(1) * total_issuance, 0)
202            }
203        }
204
205        type DataProviderInfo = (
206            AccountId,
207            BlockNumber,
208            pallet_staking::Pallet<Test>,
209            ConstU32<100>,
210        );
211
212        #[allow(dead_code)]
213        type StakingConfigEraPayout = DummyEraPayout;
214        #[allow(dead_code)]
215        type StakingConfigSlash = ();
216        #[allow(dead_code)]
217        type StakingConfigReward = ();
218        #[allow(dead_code)]
219        type StakingConfigNextNewSession = ();
220        #[allow(dead_code)]
221        type StakingConfigElectionProvider =
222            frame_election_provider_support::NoElection<DataProviderInfo>;
223        #[allow(dead_code)]
224        type StakingConfigGenesisElectionProvider =
225            frame_election_provider_support::NoElection<DataProviderInfo>;
226
227        mod pallet_tests_staking_config_impl {
228            use super::*;
229
230            $crate::impl_pallet_staking_inner!($( $tokens )*);
231        }
232    };
233}
234
235#[macro_export]
236macro_rules! impl_pallet_staking_inner {
237    ($runtime:ty$(,)?) => {
238        parameter_types! {
239            // 6 sessions in an era
240            pub const SessionsPerEra: u32 = 6;
241            // 8 eras for unbonding
242            pub const BondingDuration: u32 = 8;
243            pub const SlashDeferDuration: u32 = 7;
244            pub const MaxExposurePageSize: u32 = 256;
245            pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17);
246            pub const HistoryDepth: u32 = 84;
247            pub const MaxNominations: u32 = 16;
248            pub const MaxControllersInDeprecationBatch: u32 = 5900;
249        }
250
251        impl pallet_staking::Config for Test {
252            type Currency = Balances;
253            type UnixTime = Timestamp;
254            type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
255            type CurrencyToVote = ();
256            type ElectionProvider = StakingConfigElectionProvider;
257            type GenesisElectionProvider = StakingConfigGenesisElectionProvider;
258            type RewardRemainder = ();
259            type RuntimeEvent = RuntimeEvent;
260            type Slash = StakingConfigSlash;
261            type Reward = StakingConfigReward;
262            type SessionsPerEra = SessionsPerEra;
263            type BondingDuration = BondingDuration;
264            type SlashDeferDuration = SlashDeferDuration;
265            type AdminOrigin = frame_system::EnsureRoot<AccountId>;
266            type SessionInterface = ();
267            type EraPayout = StakingConfigEraPayout;
268            type NextNewSession = StakingConfigNextNewSession;
269            type MaxExposurePageSize = MaxExposurePageSize;
270            type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
271            type TargetList = pallet_staking::UseValidatorsMap<Self>;
272            type NominationsQuota = pallet_staking::FixedNominationsQuota<16>;
273            type MaxUnlockingChunks = ConstU32<32>;
274            type HistoryDepth = HistoryDepth;
275            type EventListeners = ();
276            type WeightInfo = ();
277            type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
278            type MaxControllersInDeprecationBatch = MaxControllersInDeprecationBatch;
279            type DisablingStrategy = pallet_staking::UpToLimitDisablingStrategy;
280        }
281    };
282
283    ($runtime:ty, EraPayout = $era_payout:ty $(, $( $rest:tt )*)?) => {
284        type StakingConfigEraPayout = $era_payout;
285
286        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
287    };
288
289    ($runtime:ty, Slash = $slash:ty $(, $( $rest:tt )*)?) => {
290        type StakingConfigSlash = $slash;
291
292        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
293    };
294
295    ($runtime:ty, Reward = $reward:ty $(, $( $rest:tt )*)?) => {
296        type StakingConfigReward = $reward;
297
298        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
299    };
300
301    ($runtime:ty, NextNewSession = $next_new_session:ty $(, $( $rest:tt )*)?) => {
302        type StakingConfigNextNewSession = $next_new_session;
303
304        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
305    };
306
307    ($runtime:ty, ElectionProvider = $election_provider:ty $(, $( $rest:tt )*)?) => {
308        type StakingConfigElectionProvider = $election_provider;
309
310        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
311    };
312
313    (
314        $runtime:ty, GenesisElectionProvider = $genesis_election_provider:ty $(, $( $rest:tt )*)?
315    ) => {
316        type StakingConfigGenesisElectionProvider = $genesis_election_provider;
317
318        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
319    };
320}