Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subnet volume migration to fix try-runtime #1222

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading