Skip to content

Commit

Permalink
Merge pull request #1222 from opentensor/fix/migration-subnet-volume
Browse files Browse the repository at this point in the history
Add subnet volume migration to fix try-runtime
  • Loading branch information
sam0x17 authored Feb 3, 2025
2 parents 97be330 + ca17411 commit d75930f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ mod hooks {
// Migrate to RAO
.saturating_add(migrations::migrate_rao::migrate_rao::<T>())
// Fix the IsNetworkMember map to be consistent with other storage maps
.saturating_add(migrations::migrate_fix_is_network_member::migrate_fix_is_network_member::<T>());
.saturating_add(migrations::migrate_fix_is_network_member::migrate_fix_is_network_member::<T>())
.saturating_add(migrations::migrate_subnet_volume::migrate_subnet_volume::<T>());
weight
}

Expand Down
33 changes: 33 additions & 0 deletions pallets/subtensor/src/migrations/migrate_subnet_volume.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use super::*;
use alloc::string::String;
use frame_support::{traits::Get, weights::Weight};

pub fn migrate_subnet_volume<T: Config>() -> Weight {
let migration_name = b"migrate_subnet_volume".to_vec();

// Initialize the weight with one read operation.
let weight = T::DbWeight::get().reads(1);

// Check if the migration has already run
if HasMigrationRun::<T>::get(&migration_name) {
log::info!(
"Migration '{:?}' has already run. Skipping.",
migration_name
);
return weight;
}
log::info!(
"Running migration '{}'",
String::from_utf8_lossy(&migration_name)
);

let mut migrated = 0u64;

SubnetVolume::<T>::translate::<u64, _>(|_key, old_value| {
migrated = migrated.saturating_add(1);
Some(old_value as u128) // Convert and store as u128
});

log::info!("Migrated {} entries in SubnetVolume", migrated);
weight.saturating_add(T::DbWeight::get().reads_writes(migrated, migrated))
}
1 change: 1 addition & 0 deletions pallets/subtensor/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod migrate_populate_owned_hotkeys;
pub mod migrate_populate_staking_hotkeys;
pub mod migrate_rao;
pub mod migrate_stake_threshold;
pub mod migrate_subnet_volume;
pub mod migrate_to_v1_separate_emission;
pub mod migrate_to_v2_fixed_total_stake;
pub mod migrate_total_issuance;
Expand Down
41 changes: 40 additions & 1 deletion pallets/subtensor/src/tests/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::*;
use codec::{Decode, Encode};
use frame_support::{
assert_ok,
storage::unhashed::{get_raw, put_raw},
storage::unhashed::{get, get_raw, put, put_raw},
traits::{StorageInstance, StoredMap},
weights::Weight,
StorageHasher, Twox64Concat,
Expand Down Expand Up @@ -711,3 +711,42 @@ fn test_migrate_rao() {
);
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::migration::test_migrate_subnet_volume --exact --show-output
#[test]
fn test_migrate_subnet_volume() {
new_test_ext(1).execute_with(|| {
// Setup initial state
let netuid_1: u16 = 1;
add_network(netuid_1, 1, 0);

// SubnetValue for netuid 1 key
let old_key: [u8; 34] = hex_literal::hex!(
"658faa385070e074c85bf6b568cf05553c3226e141696000b4b239c65bc2b2b40100"
);

// Old value in u64 format
let old_value: u64 = 123_456_789_000_u64;
put::<u64>(&old_key, &old_value); // Store as u64

// Ensure it is stored as `u64`
assert_eq!(get::<u64>(&old_key), Some(old_value));

// Run migration
crate::migrations::migrate_subnet_volume::migrate_subnet_volume::<Test>();

// Verify the value is now stored as `u128`
let new_value: Option<u128> = get(&old_key);
let new_value_as_subnet_volume = SubnetVolume::<Test>::get(netuid_1);
assert_eq!(new_value, Some(old_value as u128));
assert_eq!(new_value_as_subnet_volume, old_value as u128);

// Ensure migration does not break when running twice
let weight_second_run =
crate::migrations::migrate_subnet_volume::migrate_subnet_volume::<Test>();

// Verify the value is still stored as `u128`
let new_value: Option<u128> = get(&old_key);
assert_eq!(new_value, Some(old_value as u128));
});
}
3 changes: 2 additions & 1 deletion scripts/try-runtime-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
set -eou pipefail

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

parse_args() {
Expand Down Expand Up @@ -50,6 +50,7 @@ do_try_runtime() {

eval "try-runtime --runtime $runtime_wasm_path on-runtime-upgrade \
--no-weight-warnings --disable-spec-version-check --disable-idempotency-checks --checks=all \
--blocktime 12000 \
$chain_state"
}

Expand Down

0 comments on commit d75930f

Please sign in to comment.