Skip to content

Commit 8d5de80

Browse files
committed
fix storage item migration keys
1 parent 7385f10 commit 8d5de80

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

pallets/subtensor/src/migrations/migrate_commit_reveal_v2.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22
use crate::HasMigrationRun;
33
use frame_support::{traits::Get, weights::Weight};
44
use scale_info::prelude::string::String;
5-
use sp_io::{storage::clear_prefix, KillStorageResult};
5+
use sp_io::{hashing::twox_128, storage::clear_prefix, KillStorageResult};
66

77
pub fn migrate_commit_reveal_2<T: Config>() -> Weight {
88
let migration_name = b"migrate_commit_reveal_2".to_vec();
@@ -25,14 +25,17 @@ pub fn migrate_commit_reveal_2<T: Config>() -> Weight {
2525
// Step 1: Remove WeightCommitRevealInterval entries
2626
// ------------------------------
2727

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));
28+
let mut weight_commit_reveal_interval_prefix = Vec::new();
29+
weight_commit_reveal_interval_prefix.extend_from_slice(&twox_128("SubtensorModule".as_bytes()));
30+
weight_commit_reveal_interval_prefix
31+
.extend_from_slice(&twox_128("WeightCommitRevealInterval".as_bytes()));
32+
33+
let removal_results = clear_prefix(&weight_commit_reveal_interval_prefix, Some(u32::MAX));
3134

3235
let removed_entries_count = match removal_results {
3336
KillStorageResult::AllRemoved(removed) => removed as u64,
3437
KillStorageResult::SomeRemaining(removed) => {
35-
log::info!("Failed To Remove Some Items During migrate_commit_reveal_v2",);
38+
log::info!("Failed To Remove Some Items During migrate_commit_reveal_v2");
3639
removed as u64
3740
}
3841
};
@@ -48,13 +51,16 @@ pub fn migrate_commit_reveal_2<T: Config>() -> Weight {
4851
// Step 2: Remove WeightCommits entries
4952
// ------------------------------
5053

51-
const WEIGHT_COMMITS_PREFIX: &[u8] = b"pallet_subtensor::WeightCommits";
52-
let removal_results_commits = clear_prefix(WEIGHT_COMMITS_PREFIX, Some(u32::MAX));
54+
let mut weight_commits_prefix = Vec::new();
55+
weight_commits_prefix.extend_from_slice(&twox_128("SubtensorModule".as_bytes()));
56+
weight_commits_prefix.extend_from_slice(&twox_128("WeightCommits".as_bytes()));
57+
58+
let removal_results_commits = clear_prefix(&weight_commits_prefix, Some(u32::MAX));
5359

5460
let removed_commits_entries = match removal_results_commits {
5561
KillStorageResult::AllRemoved(removed) => removed as u64,
5662
KillStorageResult::SomeRemaining(removed) => {
57-
log::info!("Failed To Remove Some Items During migrate_commit_reveal_v2",);
63+
log::info!("Failed To Remove Some Items During migrate_commit_reveal_v2");
5864
removed as u64
5965
}
6066
};

pallets/subtensor/tests/migration.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use frame_system::Config;
1212
use mock::*;
1313
use pallet_subtensor::*;
1414
use sp_core::{H256, U256};
15+
use sp_io::hashing::twox_128;
1516
use sp_runtime::traits::Zero;
1617

1718
#[test]
@@ -446,23 +447,36 @@ fn test_migrate_commit_reveal_2() {
446447
// Step 1: Simulate Old Storage Entries
447448
// ------------------------------
448449
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";
450+
451+
let pallet_prefix = twox_128("SubtensorModule".as_bytes());
452+
let storage_prefix_interval = twox_128("WeightCommitRevealInterval".as_bytes());
453+
let storage_prefix_commits = twox_128("WeightCommits".as_bytes());
452454

453455
let netuid: u16 = 1;
454456
let interval_value: u64 = 50u64;
455457

456-
let mut interval_key = WEIGHT_COMMIT_REVEAL_INTERVAL_PREFIX.to_vec();
458+
// Construct the full key for WeightCommitRevealInterval
459+
let mut interval_key = Vec::new();
460+
interval_key.extend_from_slice(&pallet_prefix);
461+
interval_key.extend_from_slice(&storage_prefix_interval);
457462
interval_key.extend_from_slice(&netuid.encode());
458463

459464
put_raw(&interval_key, &interval_value.encode());
460465

461466
let test_account: U256 = U256::from(1);
462467

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()));
468+
// Construct the full key for WeightCommits (DoubleMap)
469+
let mut commit_key = Vec::new();
470+
commit_key.extend_from_slice(&pallet_prefix);
471+
commit_key.extend_from_slice(&storage_prefix_commits);
472+
473+
// First key (netuid) hashed with Twox64Concat
474+
let netuid_hashed = Twox64Concat::hash(&netuid.encode());
475+
commit_key.extend_from_slice(&netuid_hashed);
476+
477+
// Second key (account) hashed with Twox64Concat
478+
let account_hashed = Twox64Concat::hash(&test_account.encode());
479+
commit_key.extend_from_slice(&account_hashed);
466480

467481
let commit_value: (H256, u64) = (H256::from_low_u64_be(42), 100);
468482
put_raw(&commit_key, &commit_value.encode());

0 commit comments

Comments
 (0)