1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// This file is part of Gear.

// Copyright (C) 2023-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Module contains macros that help to implement Config type
//! for various pallets of Substrate.
//! All used types should be in scope.

use frame_support::{pallet_prelude::*, sp_runtime::Perbill, weights::RuntimeDbWeight};
use frame_system::limits::BlockWeights;

#[macro_export]
macro_rules! impl_pallet_balances {
    ($( $tokens:tt )*) => {
        #[allow(dead_code)]
        type BalancesConfigDustRemoval = ();

        mod pallet_tests_balances_config_impl {
            use super::*;

            $crate::impl_pallet_balances_inner!($( $tokens )*);
        }
    };
}

#[macro_export]
macro_rules! impl_pallet_balances_inner {
    ($runtime:ty) => {
        impl pallet_balances::Config for $runtime {
            type MaxLocks = ();
            type MaxHolds = ConstU32<1>;
            type MaxFreezes = ConstU32<1>;
            type MaxReserves = ();
            type RuntimeFreezeReason = RuntimeFreezeReason;
            type FreezeIdentifier = RuntimeFreezeReason;
            type RuntimeHoldReason = RuntimeHoldReason;
            type ReserveIdentifier = [u8; 8];
            type Balance = Balance;
            type DustRemoval = BalancesConfigDustRemoval;
            type RuntimeEvent = RuntimeEvent;
            type ExistentialDeposit = ExistentialDeposit;
            type AccountStore = System;
            type WeightInfo = ();
        }
    };

    ($runtime:ty, DustRemoval = $dust_removal:ty $(, $( $rest:tt )*)?) => {
        type BalancesConfigDustRemoval = $dust_removal;

        $crate::impl_pallet_balances_inner!($runtime $(, $( $rest )*)?);
    };
}

pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
pub const MAX_BLOCK: u64 = 250_000_000_000;

frame_support::parameter_types! {
    pub RuntimeBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults(
        Weight::from_parts(MAX_BLOCK, u64::MAX),
        NORMAL_DISPATCH_RATIO,
    );
    pub const SS58Prefix: u8 = 42;
    pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 1_110, write: 2_300 };
    pub const MinimumPeriod: u64 = 500;
}

#[macro_export]
macro_rules! impl_pallet_system {
    ($( $tokens:tt )*) => {
        #[allow(dead_code)]
        type SystemConfigDbWeight = $crate::pallet_tests::DbWeight;
        #[allow(dead_code)]
        type SystemConfigBlockWeights = $crate::pallet_tests::RuntimeBlockWeights;

        mod pallet_tests_system_config_impl {
            use super::*;

            $crate::impl_pallet_system_inner!($( $tokens )*);
        }
    };
}

#[macro_export]
macro_rules! impl_pallet_system_inner {
    ($runtime:ty$(,)?) => {
        impl frame_system::Config for $runtime {
            type BaseCallFilter = frame_support::traits::Everything;
            type BlockWeights = SystemConfigBlockWeights;
            type BlockLength = ();
            type DbWeight = SystemConfigDbWeight;
            type RuntimeOrigin = RuntimeOrigin;
            type RuntimeCall = RuntimeCall;
            type Nonce = u64;
            type Hash = H256;
            type Hashing = BlakeTwo256;
            type AccountId = AccountId;
            type Lookup = IdentityLookup<Self::AccountId>;
            type Block = Block;
            type RuntimeEvent = RuntimeEvent;
            type BlockHashCount = BlockHashCount;
            type Version = ();
            type PalletInfo = PalletInfo;
            type AccountData = pallet_balances::AccountData<Balance>;
            type OnNewAccount = ();
            type OnKilledAccount = ();
            type SystemWeightInfo = ();
            type SS58Prefix = $crate::pallet_tests::SS58Prefix;
            type OnSetCode = ();
            type MaxConsumers = frame_support::traits::ConstU32<16>;
        }
    };

    ($runtime:ty, DbWeight = $db_weight:ty $(, $( $rest:tt )*)?) => {
        type SystemConfigDbWeight = $db_weight;

        $crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?);
    };

    ($runtime:ty, BlockWeights = $block_weights:ty $(, $( $rest:tt )*)?) => {
        type SystemConfigBlockWeights = $block_weights;

        $crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?);
    };
}

#[macro_export]
macro_rules! impl_pallet_timestamp {
    ($runtime:ty) => {
        impl pallet_timestamp::Config for Test {
            type Moment = u64;
            type OnTimestampSet = ();
            type MinimumPeriod = $crate::pallet_tests::MinimumPeriod;
            type WeightInfo = ();
        }
    };
}

#[macro_export]
macro_rules! impl_pallet_authorship {
    ($( $tokens:tt )*) => {
        #[allow(dead_code)]
        pub struct FixedBlockAuthor;

        impl FindAuthor<AccountId> for FixedBlockAuthor {
            fn find_author<'a, I: 'a>(_: I) -> Option<AccountId> {
                Some(BLOCK_AUTHOR)
            }
        }

        #[allow(dead_code)]
        type AuthorshipFindAuthor = FixedBlockAuthor;
        #[allow(dead_code)]
        type AuthorshipEventHandler = ();

        mod pallet_tests_authorship_config_impl {
            use super::*;

            $crate::impl_pallet_authorship_inner!($( $tokens )*);
        }
    };
}

#[macro_export]
macro_rules! impl_pallet_authorship_inner {
    ($runtime:ty$(,)?) => {
        impl pallet_authorship::Config for $runtime {
            type FindAuthor = AuthorshipFindAuthor;
            type EventHandler = AuthorshipEventHandler;
        }
    };

    ($runtime:ty, FindAuthor = $find_author:ty $(, $( $rest:tt )*)?) => {
        type AuthorshipFindAuthor = $find_author;

        $crate::impl_pallet_authorship_inner!($runtime, $($( $rest )*)?);
    };

    ($runtime:ty, EventHandler = $event_handler:ty $(, $( $rest:tt )*)?) => {
        type AuthorshipEventHandler = $event_handler;

        $crate::impl_pallet_authorship_inner!($runtime, $($( $rest )*)?);
    };
}

#[macro_export]
macro_rules! impl_pallet_staking {
    ($( $tokens:tt )*) => {
        #[allow(dead_code)]
        pub struct DummyEraPayout;
        impl pallet_staking::EraPayout<u128> for DummyEraPayout {
            fn era_payout(
                _total_staked: u128,
                total_issuance: u128,
                _era_duration_millis: u64,
            ) -> (u128, u128) {
                // At each era have 1% `total_issuance` increase
                (Permill::from_percent(1) * total_issuance, 0)
            }
        }

        type DataProviderInfo = (
            AccountId,
            BlockNumber,
            pallet_staking::Pallet<Test>,
            ConstU32<100>,
        );

        #[allow(dead_code)]
        type StakingConfigEraPayout = DummyEraPayout;
        #[allow(dead_code)]
        type StakingConfigSlash = ();
        #[allow(dead_code)]
        type StakingConfigReward = ();
        #[allow(dead_code)]
        type StakingConfigNextNewSession = ();
        #[allow(dead_code)]
        type StakingConfigElectionProvider =
            frame_election_provider_support::NoElection<DataProviderInfo>;
        #[allow(dead_code)]
        type StakingConfigGenesisElectionProvider =
            frame_election_provider_support::NoElection<DataProviderInfo>;

        mod pallet_tests_staking_config_impl {
            use super::*;

            $crate::impl_pallet_staking_inner!($( $tokens )*);
        }
    };
}

#[macro_export]
macro_rules! impl_pallet_staking_inner {
    ($runtime:ty$(,)?) => {
        parameter_types! {
            // 6 sessions in an era
            pub const SessionsPerEra: u32 = 6;
            // 8 eras for unbonding
            pub const BondingDuration: u32 = 8;
            pub const SlashDeferDuration: u32 = 7;
            pub const MaxExposurePageSize: u32 = 512;
            pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17);
            pub const HistoryDepth: u32 = 84;
            pub const MaxNominations: u32 = 16;
        }

        impl pallet_staking::Config for Test {
            type Currency = Balances;
            type UnixTime = Timestamp;
            type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
            type CurrencyToVote = ();
            type ElectionProvider = StakingConfigElectionProvider;
            type GenesisElectionProvider = StakingConfigGenesisElectionProvider;
            type RewardRemainder = ();
            type RuntimeEvent = RuntimeEvent;
            type Slash = StakingConfigSlash;
            type Reward = StakingConfigReward;
            type SessionsPerEra = SessionsPerEra;
            type BondingDuration = BondingDuration;
            type SlashDeferDuration = SlashDeferDuration;
            type AdminOrigin = frame_system::EnsureRoot<AccountId>;
            type SessionInterface = ();
            type EraPayout = StakingConfigEraPayout;
            type NextNewSession = StakingConfigNextNewSession;
            type MaxExposurePageSize = MaxExposurePageSize;
            type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
            type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
            type TargetList = pallet_staking::UseValidatorsMap<Self>;
            type NominationsQuota = pallet_staking::FixedNominationsQuota<16>;
            type MaxUnlockingChunks = ConstU32<32>;
            type HistoryDepth = HistoryDepth;
            type EventListeners = ();
            type WeightInfo = ();
            type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
        }
    };

    ($runtime:ty, EraPayout = $era_payout:ty $(, $( $rest:tt )*)?) => {
        type StakingConfigEraPayout = $era_payout;

        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
    };

    ($runtime:ty, Slash = $slash:ty $(, $( $rest:tt )*)?) => {
        type StakingConfigSlash = $slash;

        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
    };

    ($runtime:ty, Reward = $reward:ty $(, $( $rest:tt )*)?) => {
        type StakingConfigReward = $reward;

        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
    };

    ($runtime:ty, NextNewSession = $next_new_session:ty $(, $( $rest:tt )*)?) => {
        type StakingConfigNextNewSession = $next_new_session;

        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
    };

    ($runtime:ty, ElectionProvider = $election_provider:ty $(, $( $rest:tt )*)?) => {
        type StakingConfigElectionProvider = $election_provider;

        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
    };

    (
        $runtime:ty, GenesisElectionProvider = $genesis_election_provider:ty $(, $( $rest:tt )*)?
    ) => {
        type StakingConfigGenesisElectionProvider = $genesis_election_provider;

        $crate::impl_pallet_staking_inner!($runtime, $($( $rest )*)?);
    };
}