Skip to content

Commit d75930f

Browse files
authored
Merge pull request #1222 from opentensor/fix/migration-subnet-volume
Add subnet volume migration to fix try-runtime
2 parents 97be330 + ca17411 commit d75930f

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ mod hooks {
7676
// Migrate to RAO
7777
.saturating_add(migrations::migrate_rao::migrate_rao::<T>())
7878
// Fix the IsNetworkMember map to be consistent with other storage maps
79-
.saturating_add(migrations::migrate_fix_is_network_member::migrate_fix_is_network_member::<T>());
79+
.saturating_add(migrations::migrate_fix_is_network_member::migrate_fix_is_network_member::<T>())
80+
.saturating_add(migrations::migrate_subnet_volume::migrate_subnet_volume::<T>());
8081
weight
8182
}
8283

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use super::*;
2+
use alloc::string::String;
3+
use frame_support::{traits::Get, weights::Weight};
4+
5+
pub fn migrate_subnet_volume<T: Config>() -> Weight {
6+
let migration_name = b"migrate_subnet_volume".to_vec();
7+
8+
// Initialize the weight with one read operation.
9+
let weight = T::DbWeight::get().reads(1);
10+
11+
// Check if the migration has already run
12+
if HasMigrationRun::<T>::get(&migration_name) {
13+
log::info!(
14+
"Migration '{:?}' has already run. Skipping.",
15+
migration_name
16+
);
17+
return weight;
18+
}
19+
log::info!(
20+
"Running migration '{}'",
21+
String::from_utf8_lossy(&migration_name)
22+
);
23+
24+
let mut migrated = 0u64;
25+
26+
SubnetVolume::<T>::translate::<u64, _>(|_key, old_value| {
27+
migrated = migrated.saturating_add(1);
28+
Some(old_value as u128) // Convert and store as u128
29+
});
30+
31+
log::info!("Migrated {} entries in SubnetVolume", migrated);
32+
weight.saturating_add(T::DbWeight::get().reads_writes(migrated, migrated))
33+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod migrate_populate_owned_hotkeys;
1111
pub mod migrate_populate_staking_hotkeys;
1212
pub mod migrate_rao;
1313
pub mod migrate_stake_threshold;
14+
pub mod migrate_subnet_volume;
1415
pub mod migrate_to_v1_separate_emission;
1516
pub mod migrate_to_v2_fixed_total_stake;
1617
pub mod migrate_total_issuance;

pallets/subtensor/src/tests/migration.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::*;
55
use codec::{Decode, Encode};
66
use frame_support::{
77
assert_ok,
8-
storage::unhashed::{get_raw, put_raw},
8+
storage::unhashed::{get, get_raw, put, put_raw},
99
traits::{StorageInstance, StoredMap},
1010
weights::Weight,
1111
StorageHasher, Twox64Concat,
@@ -711,3 +711,42 @@ fn test_migrate_rao() {
711711
);
712712
});
713713
}
714+
715+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::migration::test_migrate_subnet_volume --exact --show-output
716+
#[test]
717+
fn test_migrate_subnet_volume() {
718+
new_test_ext(1).execute_with(|| {
719+
// Setup initial state
720+
let netuid_1: u16 = 1;
721+
add_network(netuid_1, 1, 0);
722+
723+
// SubnetValue for netuid 1 key
724+
let old_key: [u8; 34] = hex_literal::hex!(
725+
"658faa385070e074c85bf6b568cf05553c3226e141696000b4b239c65bc2b2b40100"
726+
);
727+
728+
// Old value in u64 format
729+
let old_value: u64 = 123_456_789_000_u64;
730+
put::<u64>(&old_key, &old_value); // Store as u64
731+
732+
// Ensure it is stored as `u64`
733+
assert_eq!(get::<u64>(&old_key), Some(old_value));
734+
735+
// Run migration
736+
crate::migrations::migrate_subnet_volume::migrate_subnet_volume::<Test>();
737+
738+
// Verify the value is now stored as `u128`
739+
let new_value: Option<u128> = get(&old_key);
740+
let new_value_as_subnet_volume = SubnetVolume::<Test>::get(netuid_1);
741+
assert_eq!(new_value, Some(old_value as u128));
742+
assert_eq!(new_value_as_subnet_volume, old_value as u128);
743+
744+
// Ensure migration does not break when running twice
745+
let weight_second_run =
746+
crate::migrations::migrate_subnet_volume::migrate_subnet_volume::<Test>();
747+
748+
// Verify the value is still stored as `u128`
749+
let new_value: Option<u128> = get(&old_key);
750+
assert_eq!(new_value, Some(old_value as u128));
751+
});
752+
}

scripts/try-runtime-upgrade.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
set -eou pipefail
1313

1414
runtime_wasm_path="./target/release/wbuild/node-subtensor-runtime/node_subtensor_runtime.compact.wasm"
15-
live_chain_url="wss://dev.chain.opentensor.ai:443"
15+
live_chain_url="wss://dev.chain.opentensor.ai"
1616
snapshot_path=""
1717

1818
parse_args() {
@@ -50,6 +50,7 @@ do_try_runtime() {
5050

5151
eval "try-runtime --runtime $runtime_wasm_path on-runtime-upgrade \
5252
--no-weight-warnings --disable-spec-version-check --disable-idempotency-checks --checks=all \
53+
--blocktime 12000 \
5354
$chain_state"
5455
}
5556

0 commit comments

Comments
 (0)