Skip to content

Commit 4a777df

Browse files
constconst
authored andcommitted
merge
2 parents 8eb7bd9 + af453f4 commit 4a777df

File tree

16 files changed

+3219
-2780
lines changed

16 files changed

+3219
-2780
lines changed

Cargo.lock

Lines changed: 277 additions & 208 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 70 additions & 69 deletions
Large diffs are not rendered by default.

justfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ lint:
4747
@echo "Running cargo clippy with automatic fixes on potentially dirty code..."
4848
just clippy-fix
4949
@echo "Running cargo clippy..."
50-
just clippy
50+
just clippy
51+
52+
production:
53+
@echo "Running cargo build with metadata-hash generation..."
54+
cargo +{{RUSTV}} build --profile production --features="runtime-benchmarks metadata-hash"

node/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ sp-io = { workspace = true }
5454
sp-timestamp = { workspace = true }
5555
sp-inherents = { workspace = true }
5656
sp-keyring = { workspace = true }
57+
frame-metadata-hash-extension = { workspace = true }
5758
frame-system = { workspace = true }
5859
pallet-transaction-payment = { workspace = true }
5960
pallet-commitments = { path = "../pallets/commitments" }

node/src/benchmarking.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub fn create_benchmark_extrinsic(
136136
pallet_transaction_payment::ChargeTransactionPayment::<runtime::Runtime>::from(0),
137137
pallet_subtensor::SubtensorSignedExtension::<runtime::Runtime>::new(),
138138
pallet_commitments::CommitmentsSignedExtension::<runtime::Runtime>::new(),
139+
frame_metadata_hash_extension::CheckMetadataHash::<runtime::Runtime>::new(true),
139140
);
140141

141142
let raw_payload = runtime::SignedPayload::from_raw(
@@ -152,6 +153,7 @@ pub fn create_benchmark_extrinsic(
152153
(),
153154
(),
154155
(),
156+
None,
155157
),
156158
);
157159
let signature = raw_payload.using_encoded(|e| sender.sign(e));

pallets/subtensor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub mod pallet {
7878

7979
/// Tracks version for migrations. Should be monotonic with respect to the
8080
/// order of migrations. (i.e. always increasing)
81-
const STORAGE_VERSION: StorageVersion = StorageVersion::new(6);
81+
const STORAGE_VERSION: StorageVersion = StorageVersion::new(7);
8282

8383
/// Minimum balance required to perform a coldkey swap
8484
pub const MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP: u64 = 100_000_000; // 0.1 TAO in RAO

pallets/subtensor/src/swap/swap_coldkey.rs

Lines changed: 337 additions & 265 deletions
Large diffs are not rendered by default.

pallets/subtensor/src/swap/swap_hotkey.rs

Lines changed: 199 additions & 304 deletions
Large diffs are not rendered by default.

pallets/subtensor/tests/migration.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::unwrap_used)]
22

33
mod mock;
4-
use frame_support::assert_ok;
4+
use frame_support::{assert_ok, weights::Weight};
55
use frame_system::Config;
66
use mock::*;
77
use pallet_subtensor::*;
@@ -278,10 +278,12 @@ fn test_migration_delete_subnet_21() {
278278
})
279279
}
280280

281+
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake --exact --nocapture
281282
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake --exact --nocapture
282283
#[test]
283284
fn test_migrate_fix_total_coldkey_stake() {
284285
new_test_ext(1).execute_with(|| {
286+
let migration_name = "fix_total_coldkey_stake_v7";
285287
let coldkey = U256::from(0);
286288
TotalColdkeyStake::<Test>::insert(coldkey, 0);
287289
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
@@ -297,6 +299,7 @@ fn test_migrate_fix_total_coldkey_stake() {
297299
#[test]
298300
fn test_migrate_fix_total_coldkey_stake_value_already_in_total() {
299301
new_test_ext(1).execute_with(|| {
302+
let migration_name = "fix_total_coldkey_stake_v7";
300303
let coldkey = U256::from(0);
301304
TotalColdkeyStake::<Test>::insert(coldkey, 100000000);
302305
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
@@ -312,6 +315,7 @@ fn test_migrate_fix_total_coldkey_stake_value_already_in_total() {
312315
#[test]
313316
fn test_migrate_fix_total_coldkey_stake_no_entry() {
314317
new_test_ext(1).execute_with(|| {
318+
let migration_name = "fix_total_coldkey_stake_v7";
315319
let coldkey = U256::from(0);
316320
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
317321
Stake::<Test>::insert(U256::from(1), U256::from(0), 10000);
@@ -326,6 +330,7 @@ fn test_migrate_fix_total_coldkey_stake_no_entry() {
326330
#[test]
327331
fn test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys() {
328332
new_test_ext(1).execute_with(|| {
333+
let migration_name = "fix_total_coldkey_stake_v7";
329334
let coldkey = U256::from(0);
330335
TotalColdkeyStake::<Test>::insert(coldkey, 100000000);
331336
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
@@ -338,6 +343,7 @@ fn test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys() {
338343
#[test]
339344
fn test_migrate_fix_total_coldkey_stake_one_hotkey_stake_missing() {
340345
new_test_ext(1).execute_with(|| {
346+
let migration_name = "fix_total_coldkey_stake_v7";
341347
let coldkey = U256::from(0);
342348
TotalColdkeyStake::<Test>::insert(coldkey, 100000000);
343349
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
@@ -347,3 +353,58 @@ fn test_migrate_fix_total_coldkey_stake_one_hotkey_stake_missing() {
347353
assert_eq!(TotalColdkeyStake::<Test>::get(coldkey), 20000);
348354
})
349355
}
356+
357+
// New test to check if migration runs only once
358+
#[test]
359+
fn test_migrate_fix_total_coldkey_stake_runs_once() {
360+
new_test_ext(1).execute_with(|| {
361+
let migration_name = "fix_total_coldkey_stake_v7";
362+
let coldkey = U256::from(0);
363+
TotalColdkeyStake::<Test>::insert(coldkey, 0);
364+
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
365+
Stake::<Test>::insert(U256::from(1), U256::from(0), 10000);
366+
Stake::<Test>::insert(U256::from(2), U256::from(0), 10000);
367+
Stake::<Test>::insert(U256::from(3), U256::from(0), 10000);
368+
369+
// First run
370+
let first_weight = run_migration_and_check(migration_name);
371+
assert!(first_weight != Weight::zero());
372+
assert_eq!(TotalColdkeyStake::<Test>::get(coldkey), 30000);
373+
374+
// Second run
375+
let second_weight = run_migration_and_check(migration_name);
376+
assert_eq!(second_weight, Weight::zero());
377+
assert_eq!(TotalColdkeyStake::<Test>::get(coldkey), 30000);
378+
})
379+
}
380+
381+
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_starts_with_value_no_stake_map_entries --exact --nocapture
382+
#[test]
383+
fn test_migrate_fix_total_coldkey_stake_starts_with_value_no_stake_map_entries() {
384+
new_test_ext(1).execute_with(|| {
385+
let migration_name = "fix_total_coldkey_stake_v7";
386+
let coldkey = U256::from(0);
387+
TotalColdkeyStake::<Test>::insert(coldkey, 123_456_789);
388+
389+
// Notably, coldkey has no stake map or staking_hotkeys map entries
390+
391+
let weight = run_migration_and_check(migration_name);
392+
assert!(weight != Weight::zero());
393+
// Therefore 0
394+
assert_eq!(TotalColdkeyStake::<Test>::get(coldkey), 0);
395+
})
396+
}
397+
398+
fn run_migration_and_check(migration_name: &'static str) -> frame_support::weights::Weight {
399+
// Execute the migration and store its weight
400+
let weight: frame_support::weights::Weight =
401+
pallet_subtensor::migration::migrate_fix_total_coldkey_stake::<Test>();
402+
403+
// Check if the migration has been marked as completed
404+
assert!(HasMigrationRun::<Test>::get(
405+
migration_name.as_bytes().to_vec()
406+
));
407+
408+
// Return the weight of the executed migration
409+
weight
410+
}

0 commit comments

Comments
 (0)