gear_common/
pallet_tests.rs

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