This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
forked from JoshOrndorff/substrate-node-template
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmock.rs
227 lines (187 loc) · 6.74 KB
/
mock.rs
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
use frame_support::{parameter_types, traits::Contains};
use frame_system as system;
use frame_system::EnsureRoot;
use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
use sp_std::cell::RefCell;
use pallet_locker_mirror::{BalanceOf, LockedInfoOf};
pub use crate as pallet_free_calls;
use crate::config::{ConfigHash, RateLimiterConfig, WindowConfig};
use crate::max_quota_percentage;
use crate::quota::NumberOfCalls;
use crate::test_pallet;
pub(crate) type AccountId = subsocial_primitives::AccountId;
pub(crate) type BlockNumber = subsocial_primitives::BlockNumber;
pub(crate) type Balance = subsocial_primitives::Balance;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
frame_support::construct_runtime!(
pub enum Test where
Block = subsocial_primitives::Block,
NodeBlock = subsocial_primitives::Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: system::{Pallet, Call, Config, Storage, Event<T>},
FreeCalls: pallet_free_calls::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
LockerMirror: pallet_locker_mirror::{Pallet, Call, Storage, Event<T>},
TestPallet: test_pallet::{Pallet, Call, Storage, Event<T>},
}
);
parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
pub const SS58Prefix: u8 = 28;
}
pub struct TestBaseCallFilter;
impl Contains<Call> for TestBaseCallFilter {
fn contains(c: &Call) -> bool {
match *c {
Call::FreeCalls(_) => true,
// For benchmarking, this acts as a noop call
Call::System(frame_system::Call::remark { .. }) => true,
// For tests
Call::TestPallet(_) => true,
_ => false,
}
}
}
impl system::Config for Test {
type BaseCallFilter = TestBaseCallFilter;
type BlockWeights = ();
type BlockLength = ();
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = BlockNumber;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = subsocial_primitives::Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
}
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}
impl pallet_balances::Config for Test {
type Balance = Balance;
type DustRemoval = ();
type Event = Event;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = ();
}
impl pallet_locker_mirror::Config for Test {
type Event = Event;
type Currency = Balances;
type OracleOrigin = EnsureRoot<AccountId>;
type WeightInfo = ();
}
impl test_pallet::Config for Test {
type Event = Event;
}
////// Free Call Dependencies
type CallFilterFn = fn(&Call) -> bool;
static DEFAULT_CALL_FILTER_FN: CallFilterFn = |_| true;
type QuotaCalculationFn<T> = fn(<T as frame_system::Config>::BlockNumber, Option<LockedInfoOf<T>>) -> Option<NumberOfCalls>;
static DEFAULT_QUOTA_CALCULATION_FN: QuotaCalculationFn<Test> = |_, _| {
return Some(10);
};
pub static DEFAULT_CONFIG_HASH: ConfigHash = 0;
pub static DEFAULT_WINDOWS_CONFIG: [WindowConfig<BlockNumber>; 1] = [
WindowConfig::new(10, max_quota_percentage!(100)),
];
parameter_types! {
pub static TestRateLimiterConfig: RateLimiterConfig<BlockNumber> = RateLimiterConfig::new(
DEFAULT_WINDOWS_CONFIG.to_vec(),
DEFAULT_CONFIG_HASH,
);
}
thread_local! {
pub static CALL_FILTER: RefCell<CallFilterFn> = RefCell::new(DEFAULT_CALL_FILTER_FN);
pub static QUOTA_CALCULATION: RefCell<QuotaCalculationFn<Test>> = RefCell::new(DEFAULT_QUOTA_CALCULATION_FN);
}
pub struct TestCallFilter;
impl Contains<Call> for TestCallFilter {
fn contains(call: &Call) -> bool {
CALL_FILTER.with(|filter| filter.borrow()(call))
}
}
pub struct TestQuotaCalculation;
impl pallet_free_calls::quota_strategy::MaxQuotaCalculationStrategy<<Test as frame_system::Config>::BlockNumber, BalanceOf<Test>> for TestQuotaCalculation {
fn calculate(
current_block: <Test as frame_system::Config>::BlockNumber,
locked_info: Option<LockedInfoOf<Test>>
) -> Option<NumberOfCalls> {
QUOTA_CALCULATION.with(|strategy| strategy.borrow()(current_block, locked_info))
}
}
impl pallet_free_calls::Config for Test {
type Event = Event;
type Call = Call;
type RateLimiterConfig = TestRateLimiterConfig;
type CallFilter = TestCallFilter;
type WeightInfo = ();
type MaxQuotaCalculationStrategy = TestQuotaCalculation;
}
pub struct ExtBuilder {
call_filter: CallFilterFn,
quota_calculation: QuotaCalculationFn<Test>,
windows_config: Vec<WindowConfig<BlockNumber>>,
config_hash: ConfigHash,
}
impl Default for ExtBuilder {
fn default() -> Self {
Self {
call_filter: DEFAULT_CALL_FILTER_FN,
quota_calculation: DEFAULT_QUOTA_CALCULATION_FN,
windows_config: DEFAULT_WINDOWS_CONFIG.to_vec(),
config_hash: DEFAULT_CONFIG_HASH,
}
}
}
impl ExtBuilder {
pub fn call_filter(mut self, call_filter: CallFilterFn) -> Self {
self.call_filter = call_filter;
self
}
pub fn quota_calculation(mut self, quota_calculation: QuotaCalculationFn<Test>) -> Self {
self.quota_calculation = quota_calculation;
self
}
pub fn windows_config(mut self, windows_config: Vec<WindowConfig<BlockNumber>>) -> Self {
self.windows_config = windows_config;
self
}
pub fn config_hash(mut self, config_hash: ConfigHash) -> Self {
self.config_hash = config_hash;
self
}
pub fn set_configs(&self) {
CALL_FILTER.with(|filter| *filter.borrow_mut() = self.call_filter);
QUOTA_CALCULATION.with(|calc| *calc.borrow_mut() = self.quota_calculation);
TEST_RATE_LIMITER_CONFIG.with(|configs| *configs.borrow_mut() = RateLimiterConfig::new(self.windows_config.clone(), self.config_hash));
}
pub fn build(self) -> TestExternalities {
self.set_configs();
let storage = &mut system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap();
let mut ext = TestExternalities::from(storage.clone());
ext.execute_with(|| <frame_system::Pallet<Test>>::set_block_number(1));
ext
}
}