Skip to content

Commit f8d5d4d

Browse files
committed
Fix rao migration did not update TotalIssuance
1 parent 3678d4c commit f8d5d4d

File tree

3 files changed

+52
-43
lines changed

3 files changed

+52
-43
lines changed

pallets/subtensor/src/migrations/migrate_init_total_issuance.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
2-
use frame_support::pallet_prelude::OptionQuery;
3-
use frame_support::{pallet_prelude::Identity, storage_alias};
2+
use frame_support::pallet_prelude::{Identity, OptionQuery, Weight};
3+
use frame_support::storage_alias;
44
use sp_std::vec::Vec;
55

66
// TODO: Implement comprehensive tests for this migration
@@ -14,6 +14,43 @@ pub mod deprecated_loaded_emission_format {
1414
StorageMap<Pallet<T>, Identity, u16, Vec<(AccountIdOf<T>, u64)>, OptionQuery>;
1515
}
1616

17+
pub(crate) fn migrate_init_total_issuance<T: Config>() -> Weight {
18+
// Calculate the total locked tokens across all subnets
19+
let subnets_len = crate::SubnetLocked::<T>::iter().count() as u64;
20+
let total_subnet_locked: u64 =
21+
crate::SubnetLocked::<T>::iter().fold(0, |acc, (_, v)| acc.saturating_add(v));
22+
23+
// Retrieve the total balance of all accounts
24+
let total_account_balances = <<T as crate::Config>::Currency as fungible::Inspect<
25+
<T as frame_system::Config>::AccountId,
26+
>>::total_issuance();
27+
28+
// Get the total stake from the system
29+
let total_stake = crate::TotalStake::<T>::get();
30+
31+
// Retrieve the previous total issuance for logging purposes
32+
let prev_total_issuance = crate::TotalIssuance::<T>::get();
33+
34+
// Calculate the new total issuance
35+
let new_total_issuance = total_account_balances
36+
.saturating_add(total_stake)
37+
.saturating_add(total_subnet_locked);
38+
39+
// Update the total issuance in storage
40+
crate::TotalIssuance::<T>::put(new_total_issuance);
41+
42+
// Log the change in total issuance
43+
log::info!(
44+
"Subtensor Pallet Total Issuance Updated: previous: {:?}, new: {:?}",
45+
prev_total_issuance,
46+
new_total_issuance
47+
);
48+
49+
// Return the weight of the operation
50+
// We performed subnets_len + 5 reads and 1 write
51+
<T as frame_system::Config>::DbWeight::get().reads_writes(subnets_len.saturating_add(5), 1)
52+
}
53+
1754
pub mod initialise_total_issuance {
1855
use frame_support::pallet_prelude::Weight;
1956
use frame_support::traits::{fungible, OnRuntimeUpgrade};
@@ -33,41 +70,7 @@ pub mod initialise_total_issuance {
3370
///
3471
/// Returns the weight of the migration operation.
3572
fn on_runtime_upgrade() -> Weight {
36-
// Calculate the total locked tokens across all subnets
37-
let subnets_len = crate::SubnetLocked::<T>::iter().count() as u64;
38-
let total_subnet_locked: u64 =
39-
crate::SubnetLocked::<T>::iter().fold(0, |acc, (_, v)| acc.saturating_add(v));
40-
41-
// Retrieve the total balance of all accounts
42-
let total_account_balances = <<T as crate::Config>::Currency as fungible::Inspect<
43-
<T as frame_system::Config>::AccountId,
44-
>>::total_issuance();
45-
46-
// Get the total stake from the system
47-
let total_stake = crate::TotalStake::<T>::get();
48-
49-
// Retrieve the previous total issuance for logging purposes
50-
let prev_total_issuance = crate::TotalIssuance::<T>::get();
51-
52-
// Calculate the new total issuance
53-
let new_total_issuance = total_account_balances
54-
.saturating_add(total_stake)
55-
.saturating_add(total_subnet_locked);
56-
57-
// Update the total issuance in storage
58-
crate::TotalIssuance::<T>::put(new_total_issuance);
59-
60-
// Log the change in total issuance
61-
log::info!(
62-
"Subtensor Pallet Total Issuance Updated: previous: {:?}, new: {:?}",
63-
prev_total_issuance,
64-
new_total_issuance
65-
);
66-
67-
// Return the weight of the operation
68-
// We performed subnets_len + 5 reads and 1 write
69-
<T as frame_system::Config>::DbWeight::get()
70-
.reads_writes(subnets_len.saturating_add(5), 1)
73+
super::migrate_init_total_issuance::<T>()
7174
}
7275

7376
/// Performs post-upgrade checks to ensure the migration was successful.

pallets/subtensor/src/migrations/migrate_rao.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use super::*;
21
use alloc::string::String;
2+
33
use frame_support::IterableStorageMap;
44
use frame_support::{traits::Get, weights::Weight};
5-
use log;
65
use sp_runtime::format;
76
use substrate_fixed::types::U64F64;
87

8+
use super::*;
9+
use crate::subnets::subnet::POOL_INITIAL_TAO;
10+
911
pub fn migrate_rao<T: Config>() -> Weight {
1012
let migration_name = b"migrate_rao".to_vec();
1113

@@ -69,12 +71,12 @@ pub fn migrate_rao<T: Config>() -> Weight {
6971
TokenSymbol::<T>::insert(netuid, Pallet::<T>::get_symbol_for_subnet(0));
7072
continue;
7173
}
72-
let owner: T::AccountId = SubnetOwner::<T>::get(netuid);
73-
let lock: u64 = SubnetLocked::<T>::get(netuid);
74+
let owner = SubnetOwner::<T>::get(netuid);
75+
let lock = SubnetLocked::<T>::get(netuid);
7476

7577
// Put initial TAO from lock into subnet TAO and produce numerically equal amount of Alpha
7678
// The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO.
77-
let pool_initial_tao = 100_000_000_000.min(lock.max(1));
79+
let pool_initial_tao = POOL_INITIAL_TAO.min(lock.max(1));
7880

7981
let remaining_lock = lock.saturating_sub(pool_initial_tao);
8082
// Refund the owner for the remaining lock.
@@ -127,6 +129,10 @@ pub fn migrate_rao<T: Config>() -> Weight {
127129
// TargetStakesPerInterval::<T>::put(10); (DEPRECATED)
128130
}
129131

132+
// update `TotalIssuance`, because currency issuance (`T::Currency`) has changed due to lock
133+
// refunds above
134+
weight = weight.saturating_add(migrate_init_total_issuance::migrate_init_total_issuance::<T>());
135+
130136
// Mark the migration as completed
131137
HasMigrationRun::<T>::insert(&migration_name, true);
132138
weight = weight.saturating_add(T::DbWeight::get().writes(1));

scripts/try-runtime-upgrade.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ do_try_runtime() {
4949
fi
5050

5151
eval "try-runtime --runtime $runtime_wasm_path on-runtime-upgrade \
52-
--no-weight-warnings --disable-spec-version-check --disable-idempotency-checks \
52+
--no-weight-warnings --disable-spec-version-check --disable-idempotency-checks --checks=all \
5353
$chain_state"
5454
}
5555

0 commit comments

Comments
 (0)