Skip to content

Commit 6bd03ae

Browse files
authored
Merge pull request #1966 from opentensor/fix/root-reserves-migration
Fix/root reserves migration
2 parents 0d65e77 + b29bc7f commit 6bd03ae

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ mod dispatches {
693693
/// - Attempting to set prometheus information withing the rate limit min.
694694
///
695695
#[pallet::call_index(4)]
696-
#[pallet::weight((Weight::from_parts(43_680_000, 0)
696+
#[pallet::weight((Weight::from_parts(33_010_000, 0)
697697
.saturating_add(T::DbWeight::get().reads(4))
698698
.saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Normal, Pays::No))]
699699
pub fn serve_axon(
@@ -2201,7 +2201,7 @@ mod dispatches {
22012201
/// * commit_reveal_version (`u16`):
22022202
/// - The client (bittensor-drand) version
22032203
#[pallet::call_index(113)]
2204-
#[pallet::weight((Weight::from_parts(81_920_000, 0)
2204+
#[pallet::weight((Weight::from_parts(64_530_000, 0)
22052205
.saturating_add(T::DbWeight::get().reads(7_u64))
22062206
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
22072207
pub fn commit_timelocked_weights(

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ mod hooks {
131131
// Migrate CRV3 add commit_block
132132
.saturating_add(migrations::migrate_crv3_commits_add_block::migrate_crv3_commits_add_block::<T>())
133133
//Migrate CRV3 to TimelockedCommits
134-
.saturating_add(migrations::migrate_crv3_v2_to_timelocked::migrate_crv3_v2_to_timelocked::<T>());
134+
.saturating_add(migrations::migrate_crv3_v2_to_timelocked::migrate_crv3_v2_to_timelocked::<T>())
135+
// Migrate to fix root counters
136+
.saturating_add(migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::<T>());
135137
weight
136138
}
137139

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use super::migrate_init_total_issuance::migrate_init_total_issuance;
2+
use super::*;
3+
use alloc::string::String;
4+
5+
pub fn migrate_fix_root_tao_and_alpha_in<T: Config>() -> Weight {
6+
let migration_name = b"migrate_fix_root_tao_and_alpha_in".to_vec();
7+
let mut weight = T::DbWeight::get().reads(1);
8+
9+
if HasMigrationRun::<T>::get(&migration_name) {
10+
log::info!(
11+
"Migration '{:?}' has already run. Skipping.",
12+
String::from_utf8_lossy(&migration_name)
13+
);
14+
return weight;
15+
}
16+
17+
log::info!(
18+
"Running migration '{}'",
19+
String::from_utf8_lossy(&migration_name)
20+
);
21+
22+
// Update counters (unstaked more than stake)
23+
let total_staked = 2_109_761_275_100_688_u64;
24+
let total_unstaked = 2_179_659_173_851_658_u64;
25+
let reserve_diff = total_unstaked.saturating_sub(total_staked);
26+
let volume_diff = (total_unstaked as u128).saturating_add(total_staked as u128);
27+
SubnetTAO::<T>::mutate(NetUid::ROOT, |amount| {
28+
*amount = amount.saturating_sub(TaoCurrency::from(reserve_diff));
29+
});
30+
SubnetAlphaIn::<T>::mutate(NetUid::ROOT, |amount| {
31+
*amount = amount.saturating_add(AlphaCurrency::from(reserve_diff));
32+
});
33+
SubnetAlphaOut::<T>::mutate(NetUid::ROOT, |amount| {
34+
*amount = amount.saturating_sub(AlphaCurrency::from(reserve_diff));
35+
});
36+
SubnetVolume::<T>::mutate(NetUid::ROOT, |amount| {
37+
*amount = amount.saturating_add(volume_diff);
38+
});
39+
TotalStake::<T>::mutate(|amount| {
40+
*amount = amount.saturating_sub(TaoCurrency::from(reserve_diff));
41+
});
42+
43+
weight = weight.saturating_add(T::DbWeight::get().writes(5));
44+
45+
HasMigrationRun::<T>::insert(&migration_name, true);
46+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
47+
48+
log::info!(
49+
target: "runtime",
50+
"Migration '{}' completed successfully.",
51+
String::from_utf8_lossy(&migration_name)
52+
);
53+
54+
// We need to run the total issuance migration to update the total issuance
55+
// when the root subnet TAO has been updated.
56+
migrate_init_total_issuance::<T>().saturating_add(weight)
57+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod migrate_delete_subnet_3;
1515
pub mod migrate_disable_commit_reveal;
1616
pub mod migrate_fix_is_network_member;
1717
pub mod migrate_fix_root_subnet_tao;
18+
pub mod migrate_fix_root_tao_and_alpha_in;
1819
pub mod migrate_identities_v2;
1920
pub mod migrate_init_total_issuance;
2021
pub mod migrate_orphaned_storage_items;

pallets/subtensor/src/tests/migration.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn test_migration_transfer_nets_to_foundation() {
6464
add_network(11.into(), 1, 0);
6565

6666
log::info!("{:?}", SubtensorModule::get_subnet_owner(1.into()));
67-
//assert_eq!(SubtensorModule::<T>::get_subnet_owner(1), );
67+
//assert_eq!(SubtensorModule::<Test>::get_subnet_owner(1), );
6868

6969
// Run the migration to transfer ownership
7070
let hex =
@@ -862,6 +862,45 @@ fn test_migrate_fix_root_subnet_tao() {
862862
});
863863
}
864864

865+
// cargo test --package pallet-subtensor --lib -- tests::migration::test_migrate_fix_root_tao_and_alpha_in --exact --show-output
866+
#[test]
867+
fn test_migrate_fix_root_tao_and_alpha_in() {
868+
new_test_ext(1).execute_with(|| {
869+
const MIGRATION_NAME: &str = "migrate_fix_root_tao_and_alpha_in";
870+
871+
// Set counters initially
872+
let initial_value = 1_000_000_000_000;
873+
SubnetTAO::<Test>::insert(NetUid::ROOT, TaoCurrency::from(initial_value));
874+
SubnetAlphaIn::<Test>::insert(NetUid::ROOT, AlphaCurrency::from(initial_value));
875+
SubnetAlphaOut::<Test>::insert(NetUid::ROOT, AlphaCurrency::from(initial_value));
876+
SubnetVolume::<Test>::insert(NetUid::ROOT, initial_value as u128);
877+
TotalStake::<Test>::set(TaoCurrency::from(initial_value));
878+
879+
assert!(
880+
!HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
881+
"Migration should not have run yet"
882+
);
883+
884+
// Run the migration
885+
let weight =
886+
crate::migrations::migrate_fix_root_tao_and_alpha_in::migrate_fix_root_tao_and_alpha_in::<Test>();
887+
888+
// Verify the migration ran correctly
889+
assert!(
890+
HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
891+
"Migration should be marked as run"
892+
);
893+
assert!(!weight.is_zero(), "Migration weight should be non-zero");
894+
895+
// Verify counters have changed
896+
assert!(SubnetTAO::<Test>::get(NetUid::ROOT) != initial_value.into());
897+
assert!(SubnetAlphaIn::<Test>::get(NetUid::ROOT) != initial_value.into());
898+
assert!(SubnetAlphaOut::<Test>::get(NetUid::ROOT) != initial_value.into());
899+
assert!(SubnetVolume::<Test>::get(NetUid::ROOT) != initial_value as u128);
900+
assert!(TotalStake::<Test>::get() != initial_value.into());
901+
});
902+
}
903+
865904
#[test]
866905
fn test_migrate_subnet_symbols() {
867906
new_test_ext(1).execute_with(|| {

0 commit comments

Comments
 (0)