Skip to content

Commit 7385f10

Browse files
committed
add migrate_commit_reveal_2
1 parent 75ccbf1 commit 7385f10

File tree

4 files changed

+172
-3
lines changed

4 files changed

+172
-3
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ mod hooks {
7070
// Storage version v8 -> v9
7171
.saturating_add(migrations::migrate_fix_total_coldkey_stake::migrate_fix_total_coldkey_stake::<T>())
7272
// Migrate Delegate Ids on chain
73-
.saturating_add(migrations::migrate_chain_identity::migrate_set_hotkey_identities::<T>());
73+
.saturating_add(migrations::migrate_chain_identity::migrate_set_hotkey_identities::<T>())
74+
// Migrate Commit-Reval 2.0
75+
.saturating_add(migrations::migrate_commit_reveal_v2::migrate_commit_reveal_2::<T>());
7476
weight
7577
}
7678

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use super::*;
2+
use crate::HasMigrationRun;
3+
use frame_support::{traits::Get, weights::Weight};
4+
use scale_info::prelude::string::String;
5+
use sp_io::{storage::clear_prefix, KillStorageResult};
6+
7+
pub fn migrate_commit_reveal_2<T: Config>() -> Weight {
8+
let migration_name = b"migrate_commit_reveal_2".to_vec();
9+
let mut weight = T::DbWeight::get().reads(1);
10+
11+
if HasMigrationRun::<T>::get(&migration_name) {
12+
log::info!(
13+
"Migration '{:?}' has already run. Skipping.",
14+
migration_name
15+
);
16+
return weight;
17+
}
18+
19+
log::info!(
20+
"Running migration '{}'",
21+
String::from_utf8_lossy(&migration_name)
22+
);
23+
24+
// ------------------------------
25+
// Step 1: Remove WeightCommitRevealInterval entries
26+
// ------------------------------
27+
28+
const WEIGHT_COMMIT_REVEAL_INTERVAL_PREFIX: &[u8] =
29+
b"pallet_subtensor::WeightCommitRevealInterval";
30+
let removal_results = clear_prefix(WEIGHT_COMMIT_REVEAL_INTERVAL_PREFIX, Some(u32::MAX));
31+
32+
let removed_entries_count = match removal_results {
33+
KillStorageResult::AllRemoved(removed) => removed as u64,
34+
KillStorageResult::SomeRemaining(removed) => {
35+
log::info!("Failed To Remove Some Items During migrate_commit_reveal_v2",);
36+
removed as u64
37+
}
38+
};
39+
40+
weight = weight.saturating_add(T::DbWeight::get().writes(removed_entries_count));
41+
42+
log::info!(
43+
"Removed {:?} entries from WeightCommitRevealInterval.",
44+
removed_entries_count
45+
);
46+
47+
// ------------------------------
48+
// Step 2: Remove WeightCommits entries
49+
// ------------------------------
50+
51+
const WEIGHT_COMMITS_PREFIX: &[u8] = b"pallet_subtensor::WeightCommits";
52+
let removal_results_commits = clear_prefix(WEIGHT_COMMITS_PREFIX, Some(u32::MAX));
53+
54+
let removed_commits_entries = match removal_results_commits {
55+
KillStorageResult::AllRemoved(removed) => removed as u64,
56+
KillStorageResult::SomeRemaining(removed) => {
57+
log::info!("Failed To Remove Some Items During migrate_commit_reveal_v2",);
58+
removed as u64
59+
}
60+
};
61+
62+
weight = weight.saturating_add(T::DbWeight::get().writes(removed_commits_entries));
63+
64+
log::info!(
65+
"Removed {} entries from WeightCommits.",
66+
removed_commits_entries
67+
);
68+
69+
// ------------------------------
70+
// Step 3: Mark Migration as Completed
71+
// ------------------------------
72+
73+
HasMigrationRun::<T>::insert(&migration_name, true);
74+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
75+
76+
log::info!(
77+
"Migration '{:?}' completed successfully.",
78+
String::from_utf8_lossy(&migration_name)
79+
);
80+
81+
weight
82+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
pub mod migrate_chain_identity;
3+
pub mod migrate_commit_reveal_v2;
34
pub mod migrate_create_root_network;
45
pub mod migrate_delete_subnet_21;
56
pub mod migrate_delete_subnet_3;

pallets/subtensor/tests/migration.rs

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
#![allow(unused, clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)]
22
mod mock;
3-
use frame_support::{assert_ok, weights::Weight};
3+
use codec::{Decode, Encode};
4+
use frame_support::{
5+
assert_ok,
6+
storage::unhashed::{get_raw, put_raw},
7+
traits::{StorageInstance, StoredMap},
8+
weights::Weight,
9+
StorageHasher, Twox64Concat,
10+
};
411
use frame_system::Config;
512
use mock::*;
613
use pallet_subtensor::*;
7-
use sp_core::U256;
14+
use sp_core::{H256, U256};
15+
use sp_runtime::traits::Zero;
816

917
#[test]
1018
fn test_initialise_ti() {
@@ -430,3 +438,79 @@ fn run_migration_and_check(migration_name: &'static str) -> frame_support::weigh
430438
// Return the weight of the executed migration
431439
weight
432440
}
441+
442+
#[test]
443+
fn test_migrate_commit_reveal_2() {
444+
new_test_ext(1).execute_with(|| {
445+
// ------------------------------
446+
// Step 1: Simulate Old Storage Entries
447+
// ------------------------------
448+
const MIGRATION_NAME: &str = "migrate_commit_reveal_2";
449+
const WEIGHT_COMMIT_REVEAL_INTERVAL_PREFIX: &[u8] =
450+
b"pallet_subtensor::WeightCommitRevealInterval";
451+
const WEIGHT_COMMITS_PREFIX: &[u8] = b"pallet_subtensor::WeightCommits";
452+
453+
let netuid: u16 = 1;
454+
let interval_value: u64 = 50u64;
455+
456+
let mut interval_key = WEIGHT_COMMIT_REVEAL_INTERVAL_PREFIX.to_vec();
457+
interval_key.extend_from_slice(&netuid.encode());
458+
459+
put_raw(&interval_key, &interval_value.encode());
460+
461+
let test_account: U256 = U256::from(1);
462+
463+
let mut commit_key = WEIGHT_COMMITS_PREFIX.to_vec();
464+
commit_key.extend_from_slice(&Twox64Concat::hash(&netuid.encode()));
465+
commit_key.extend_from_slice(&Twox64Concat::hash(&test_account.encode()));
466+
467+
let commit_value: (H256, u64) = (H256::from_low_u64_be(42), 100);
468+
put_raw(&commit_key, &commit_value.encode());
469+
470+
let stored_interval = get_raw(&interval_key).expect("Expected to get a value");
471+
assert_eq!(
472+
u64::decode(&mut &stored_interval[..]).expect("Failed to decode interval value"),
473+
interval_value
474+
);
475+
476+
let stored_commit = get_raw(&commit_key).expect("Expected to get a value");
477+
assert_eq!(
478+
<(H256, u64)>::decode(&mut &stored_commit[..]).expect("Failed to decode commit value"),
479+
commit_value
480+
);
481+
482+
assert!(
483+
!HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
484+
"Migration should not have run yet"
485+
);
486+
487+
// ------------------------------
488+
// Step 2: Run the Migration
489+
// ------------------------------
490+
let weight =
491+
pallet_subtensor::migrations::migrate_commit_reveal_v2::migrate_commit_reveal_2::<Test>(
492+
);
493+
494+
assert!(
495+
HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
496+
"Migration should be marked as run"
497+
);
498+
499+
// ------------------------------
500+
// Step 3: Verify Migration Effects
501+
// ------------------------------
502+
let stored_interval_after = get_raw(&interval_key);
503+
assert!(
504+
stored_interval_after.is_none(),
505+
"WeightCommitRevealInterval should be cleared"
506+
);
507+
508+
let stored_commit_after = get_raw(&commit_key);
509+
assert!(
510+
stored_commit_after.is_none(),
511+
"WeightCommits entry should be cleared"
512+
);
513+
514+
assert!(!weight.is_zero(), "Migration weight should be non-zero");
515+
});
516+
}

0 commit comments

Comments
 (0)