-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathmock.rs
409 lines (372 loc) · 15.2 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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#![allow(clippy::arithmetic_side_effects, clippy::unwrap_used)]
use frame_support::{
assert_ok, derive_impl, parameter_types,
traits::{Everything, Hooks, PrivilegeCmp},
weights,
};
use frame_system as system;
use frame_system::{limits, EnsureNever, EnsureRoot};
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::U256;
use sp_core::{ConstU64, H256};
use sp_runtime::{
testing::TestXt,
traits::{BlakeTwo256, ConstU32, IdentityLookup},
BuildStorage, KeyTypeId, Perbill,
};
use sp_std::cmp::Ordering;
use sp_weights::Weight;
type Block = frame_system::mocking::MockBlock<Test>;
// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test {
System: frame_system = 1,
Balances: pallet_balances = 2,
AdminUtils: crate = 3,
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
EVMChainId: pallet_evm_chain_id = 7,
}
);
#[allow(dead_code)]
pub type SubtensorCall = pallet_subtensor::Call<Test>;
#[allow(dead_code)]
pub type SubtensorEvent = pallet_subtensor::Event<Test>;
#[allow(dead_code)]
pub type BalanceCall = pallet_balances::Call<Test>;
#[allow(dead_code)]
pub type TestRuntimeCall = frame_system::Call<Test>;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}
#[allow(dead_code)]
pub type AccountId = U256;
// The address format for describing accounts.
#[allow(dead_code)]
pub type Address = AccountId;
// Balance of an account.
#[allow(dead_code)]
pub type Balance = u64;
// An index to a block.
#[allow(dead_code)]
pub type BlockNumber = u64;
pub type TestAuthId = test_crypto::TestAuthId;
pub type Index = u64;
pub type UncheckedExtrinsic = TestXt<RuntimeCall, ()>;
parameter_types! {
pub const InitialMinAllowedWeights: u16 = 0;
pub const InitialEmissionValue: u16 = 0;
pub const InitialMaxWeightsLimit: u16 = u16::MAX;
pub BlockWeights: limits::BlockWeights = limits::BlockWeights::simple_max(weights::Weight::from_parts(1024, 0));
pub const ExistentialDeposit: Balance = 1;
pub const TransactionByteFee: Balance = 100;
pub const SDebug:u64 = 1;
pub const InitialRho: u16 = 30;
pub const InitialKappa: u16 = 32_767;
pub const InitialTempo: u16 = 0;
pub const SelfOwnership: u64 = 2;
pub const InitialImmunityPeriod: u16 = 2;
pub const InitialMaxAllowedUids: u16 = 2;
pub const InitialBondsMovingAverage: u64 = 900_000;
pub const InitialStakePruningMin: u16 = 0;
pub const InitialFoundationDistribution: u64 = 0;
pub const InitialDefaultDelegateTake: u16 = 11_796; // 18% honest number.
pub const InitialMinDelegateTake: u16 = 5_898; // 9%;
pub const InitialDefaultChildKeyTake: u16 = 0; // Allow 0 %
pub const InitialMinChildKeyTake: u16 = 0; // Allow 0 %
pub const InitialMaxChildKeyTake: u16 = 11_796; // 18 %;
pub const InitialWeightsVersionKey: u16 = 0;
pub const InitialServingRateLimit: u64 = 0; // No limit.
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing
pub const InitialTxDelegateTakeRateLimit: u64 = 0; // Disable rate limit for testing
pub const InitialTxChildKeyTakeRateLimit: u64 = 0; // Disable rate limit for testing
pub const InitialBurn: u64 = 0;
pub const InitialMinBurn: u64 = 0;
pub const InitialMaxBurn: u64 = 1_000_000_000;
pub const InitialValidatorPruneLen: u64 = 0;
pub const InitialScalingLawPower: u16 = 50;
pub const InitialMaxAllowedValidators: u16 = 100;
pub const InitialIssuance: u64 = 0;
pub const InitialDifficulty: u64 = 10000;
pub const InitialActivityCutoff: u16 = 5000;
pub const InitialAdjustmentInterval: u16 = 100;
pub const InitialAdjustmentAlpha: u64 = 0; // no weight to previous value.
pub const InitialMaxRegistrationsPerBlock: u16 = 3;
pub const InitialTargetRegistrationsPerInterval: u16 = 2;
pub const InitialPruningScore : u16 = u16::MAX;
pub const InitialRegistrationRequirement: u16 = u16::MAX; // Top 100%
pub const InitialMinDifficulty: u64 = 1;
pub const InitialMaxDifficulty: u64 = u64::MAX;
pub const InitialRAORecycledForRegistration: u64 = 0;
pub const InitialSenateRequiredStakePercentage: u64 = 2; // 2 percent of total stake
pub const InitialNetworkImmunityPeriod: u64 = 7200 * 7;
pub const InitialNetworkMinAllowedUids: u16 = 128;
pub const InitialNetworkMinLockCost: u64 = 100_000_000_000;
pub const InitialSubnetOwnerCut: u16 = 0; // 0%. 100% of rewards go to validators + miners.
pub const InitialNetworkLockReductionInterval: u64 = 2; // 2 blocks.
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
pub const InitialNetworkRateLimit: u64 = 0;
pub const InitialTargetStakesPerInterval: u16 = 1;
pub const InitialKeySwapCost: u64 = 1_000_000_000;
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
pub const InitialHotkeyEmissionTempo: u64 = 1;
pub const InitialNetworkMaxStake: u64 = u64::MAX; // Maximum possible value for u64, this make the make stake infinity
pub const InitialColdkeySwapScheduleDuration: u64 = 5 * 24 * 60 * 60 / 12; // 5 days
pub const InitialDissolveNetworkScheduleDuration: u64 = 5 * 24 * 60 * 60 / 12; // 5 days
}
impl pallet_subtensor::Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type Currency = Balances;
type InitialIssuance = InitialIssuance;
type SudoRuntimeCall = TestRuntimeCall;
type CouncilOrigin = EnsureNever<AccountId>;
type SenateMembers = ();
type TriumvirateInterface = ();
type Scheduler = Scheduler;
type InitialMinAllowedWeights = InitialMinAllowedWeights;
type InitialEmissionValue = InitialEmissionValue;
type InitialMaxWeightsLimit = InitialMaxWeightsLimit;
type InitialTempo = InitialTempo;
type InitialDifficulty = InitialDifficulty;
type InitialAdjustmentInterval = InitialAdjustmentInterval;
type InitialAdjustmentAlpha = InitialAdjustmentAlpha;
type InitialTargetRegistrationsPerInterval = InitialTargetRegistrationsPerInterval;
type InitialRho = InitialRho;
type InitialKappa = InitialKappa;
type InitialMaxAllowedUids = InitialMaxAllowedUids;
type InitialValidatorPruneLen = InitialValidatorPruneLen;
type InitialScalingLawPower = InitialScalingLawPower;
type InitialImmunityPeriod = InitialImmunityPeriod;
type InitialActivityCutoff = InitialActivityCutoff;
type InitialMaxRegistrationsPerBlock = InitialMaxRegistrationsPerBlock;
type InitialPruningScore = InitialPruningScore;
type InitialBondsMovingAverage = InitialBondsMovingAverage;
type InitialMaxAllowedValidators = InitialMaxAllowedValidators;
type InitialDefaultDelegateTake = InitialDefaultDelegateTake;
type InitialMinDelegateTake = InitialMinDelegateTake;
type InitialDefaultChildKeyTake = InitialDefaultChildKeyTake;
type InitialMinChildKeyTake = InitialMinChildKeyTake;
type InitialMaxChildKeyTake = InitialMaxChildKeyTake;
type InitialWeightsVersionKey = InitialWeightsVersionKey;
type InitialMaxDifficulty = InitialMaxDifficulty;
type InitialMinDifficulty = InitialMinDifficulty;
type InitialServingRateLimit = InitialServingRateLimit;
type InitialTxRateLimit = InitialTxRateLimit;
type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit;
type InitialTxChildKeyTakeRateLimit = InitialTxChildKeyTakeRateLimit;
type InitialBurn = InitialBurn;
type InitialMaxBurn = InitialMaxBurn;
type InitialMinBurn = InitialMinBurn;
type InitialRAORecycledForRegistration = InitialRAORecycledForRegistration;
type InitialSenateRequiredStakePercentage = InitialSenateRequiredStakePercentage;
type InitialNetworkImmunityPeriod = InitialNetworkImmunityPeriod;
type InitialNetworkMinAllowedUids = InitialNetworkMinAllowedUids;
type InitialNetworkMinLockCost = InitialNetworkMinLockCost;
type InitialSubnetOwnerCut = InitialSubnetOwnerCut;
type InitialNetworkLockReductionInterval = InitialNetworkLockReductionInterval;
type InitialSubnetLimit = InitialSubnetLimit;
type InitialNetworkRateLimit = InitialNetworkRateLimit;
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
type KeySwapCost = InitialKeySwapCost;
type AlphaHigh = InitialAlphaHigh;
type AlphaLow = InitialAlphaLow;
type LiquidAlphaOn = InitialLiquidAlphaOn;
type InitialHotkeyEmissionTempo = InitialHotkeyEmissionTempo;
type InitialNetworkMaxStake = InitialNetworkMaxStake;
type Preimages = ();
type InitialColdkeySwapScheduleDuration = InitialColdkeySwapScheduleDuration;
type InitialDissolveNetworkScheduleDuration = InitialDissolveNetworkScheduleDuration;
}
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = U256;
type Lookup = IdentityLookup<Self::AccountId>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type Block = Block;
type Nonce = u64;
}
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type Balance = u64;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ConstU64<1>;
type AccountStore = System;
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type RuntimeHoldReason = ();
}
pub struct OriginPrivilegeCmp;
impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
fn cmp_privilege(_left: &OriginCaller, _right: &OriginCaller) -> Option<Ordering> {
None
}
}
impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AuthorityId = AuraId;
type MaxAuthorities = ConstU32<32>;
type Aura = ();
type Balance = Balance;
type WeightInfo = ();
}
parameter_types! {
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) *
BlockWeights::get().max_block;
pub const MaxScheduledPerBlock: u32 = 50;
pub const NoPreimagePostponement: Option<u32> = Some(10);
}
impl pallet_scheduler::Config for Test {
type RuntimeOrigin = RuntimeOrigin;
type RuntimeEvent = RuntimeEvent;
type PalletsOrigin = OriginCaller;
type RuntimeCall = RuntimeCall;
type MaximumWeight = MaximumSchedulerWeight;
type ScheduleOrigin = EnsureRoot<AccountId>;
type MaxScheduledPerBlock = MaxScheduledPerBlock;
type WeightInfo = pallet_scheduler::weights::SubstrateWeight<Test>;
type OriginPrivilegeCmp = OriginPrivilegeCmp;
type Preimages = ();
}
impl pallet_evm_chain_id::Config for Test {}
impl pallet_drand::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_drand::weights::SubstrateWeight<Test>;
type AuthorityId = TestAuthId;
type Verifier = pallet_drand::verifier::QuicknetVerifier;
type UnsignedPriority = ConstU64<{ 1 << 20 }>;
type HttpFetchTimeout = ConstU64<1_000>;
}
impl frame_system::offchain::SigningTypes for Test {
type Public = test_crypto::Public;
type Signature = test_crypto::Signature;
}
pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"test");
mod test_crypto {
use super::KEY_TYPE;
use sp_core::sr25519::{Public as Sr25519Public, Signature as Sr25519Signature};
use sp_core::U256;
use sp_runtime::{
app_crypto::{app_crypto, sr25519},
traits::IdentifyAccount,
};
app_crypto!(sr25519, KEY_TYPE);
pub struct TestAuthId;
impl frame_system::offchain::AppCrypto<Public, Signature> for TestAuthId {
type RuntimeAppPublic = Public;
type GenericSignature = Sr25519Signature;
type GenericPublic = Sr25519Public;
}
impl IdentifyAccount for Public {
type AccountId = U256;
fn into_account(self) -> U256 {
let mut bytes = [0u8; 32];
bytes.copy_from_slice(self.as_ref());
U256::from_big_endian(&bytes)
}
}
}
impl frame_system::offchain::CreateSignedTransaction<pallet_drand::Call<Test>> for Test {
fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: RuntimeCall,
_public: Self::Public,
_account: Self::AccountId,
nonce: Index,
) -> Option<(
RuntimeCall,
<UncheckedExtrinsic as sp_runtime::traits::Extrinsic>::SignaturePayload,
)> {
Some((call, (nonce, ())))
}
}
impl<C> frame_system::offchain::SendTransactionTypes<C> for Test
where
RuntimeCall: From<C>,
{
type Extrinsic = UncheckedExtrinsic;
type OverarchingCall = RuntimeCall;
}
// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
sp_tracing::try_init_simple();
let t = frame_system::GenesisConfig::<Test>::default()
.build_storage()
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
#[allow(dead_code)]
pub(crate) fn run_to_block(n: u64) {
while System::block_number() < n {
SubtensorModule::on_finalize(System::block_number());
System::on_finalize(System::block_number());
System::set_block_number(System::block_number() + 1);
System::on_initialize(System::block_number());
SubtensorModule::on_initialize(System::block_number());
}
}
#[allow(dead_code)]
pub fn register_ok_neuron(
netuid: u16,
hotkey_account_id: U256,
coldkey_account_id: U256,
start_nonce: u64,
) {
let block_number: u64 = SubtensorModule::get_current_block_as_u64();
let (nonce, work): (u64, Vec<u8>) = SubtensorModule::create_work_for_block_number(
netuid,
block_number,
start_nonce,
&hotkey_account_id,
);
let result = SubtensorModule::register(
<<Test as frame_system::Config>::RuntimeOrigin>::signed(hotkey_account_id),
netuid,
block_number,
nonce,
work,
hotkey_account_id,
coldkey_account_id,
);
assert_ok!(result);
log::info!(
"Register ok neuron: netuid: {:?}, coldkey: {:?}, hotkey: {:?}",
netuid,
hotkey_account_id,
coldkey_account_id
);
}
#[allow(dead_code)]
pub fn add_network(netuid: u16, tempo: u16) {
SubtensorModule::init_new_network(netuid, tempo);
SubtensorModule::set_network_registration_allowed(netuid, true);
SubtensorModule::set_network_pow_registration_allowed(netuid, true);
}