Skip to content

Commit f660aea

Browse files
committed
Fix check_total_issuance
1 parent f8dad68 commit f660aea

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

pallets/subtensor/src/utils/try_state.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,59 @@ use super::*;
55
impl<T: Config> Pallet<T> {
66
/// Checks [`TotalIssuance`] equals the sum of currency issuance, total stake, and total subnet
77
/// locked.
8-
#[allow(clippy::expect_used)]
8+
#[allow(clippy::arithmetic_side_effects, clippy::expect_used)]
99
pub(crate) fn check_total_issuance() -> Result<(), sp_runtime::TryRuntimeError> {
1010
// Get the total currency issuance
11-
let currency_issuance = <T as Config>::Currency::total_issuance();
11+
let mut currency_issuance = u64::from(<T as Config>::Currency::total_issuance()) as i128;
12+
let total_issuance = u64::from(TotalIssuance::<T>::get()) as i128;
1213

1314
log::info!("=== Try runtime check_total_issuance ===");
1415
log::info!(" currency_issuance: {}", currency_issuance);
16+
log::info!(" total_issuance: {}", total_issuance);
1517

1618
// If balances total issuance is greater than 21M, we're on devnet or testnet, ignore
1719
// this check, TI is off for multiple reasons.
18-
if currency_issuance > 21_000_000_000_000_000_u64.into() {
20+
if currency_issuance > 21_000_000_000_000_000_i128 {
1921
return Ok(());
2022
}
2123

24+
// If there's an exact match, it means we are past imbalances upgrade
25+
if currency_issuance == total_issuance {
26+
return Ok(());
27+
}
28+
29+
// Effect from migrate_total_issuance adjustment diff
30+
currency_issuance =
31+
u64::from(SubnetTAO::<T>::iter().fold(TaoBalance::ZERO, |acc, (_, v)| acc + v)) as i128;
32+
2233
// Calculate total SubnetLock
23-
let mut total_locked = TaoBalance::ZERO;
34+
let mut total_locked = 0_i128;
2435
let initial_pool_tao = NetworkMinLockCost::<T>::get();
2536
SubnetLocked::<T>::iter().for_each(|(netuid, tao)| {
2637
if Pallet::<T>::get_subnet_account_id(netuid).is_some() {
27-
let tao_lock = tao.saturating_sub(initial_pool_tao);
28-
total_locked = total_locked.saturating_add(tao_lock);
38+
let tao_lock = tao - initial_pool_tao;
39+
total_locked += u64::from(tao_lock) as i128;
2940
}
3041
});
3142
log::info!(" total_locked: {}", total_locked);
3243

3344
// Calculate the expected total issuance
34-
let mut total_stake = TaoBalance::ZERO;
45+
let mut total_stake = 0_i128;
3546
SubnetTAO::<T>::iter().for_each(|(netuid, tao)| {
3647
if Pallet::<T>::get_subnet_account_id(netuid).is_some() {
37-
total_stake = total_stake.saturating_add(tao);
48+
total_stake += u64::from(tao) as i128;
3849
}
3950
});
4051
log::info!(" total stake: {}", total_stake);
41-
let expected_total_issuance = currency_issuance
42-
.saturating_add(total_stake)
43-
.saturating_add(total_locked);
52+
let expected_total_issuance = currency_issuance + total_stake + total_locked;
4453
log::info!(" expected_total_issuance: {}", expected_total_issuance);
4554

4655
// Verify the diff between calculated TI and actual TI is less than delta
4756
//
4857
// These values can be off slightly due to float rounding errors.
4958
// They are corrected every runtime upgrade.
50-
let delta = TaoBalance::from(1000);
51-
let total_issuance = TotalIssuance::<T>::get();
52-
log::info!(" total_issuance: {}", total_issuance);
53-
54-
let diff = if total_issuance > expected_total_issuance {
55-
total_issuance.checked_sub(&expected_total_issuance)
56-
} else {
57-
expected_total_issuance.checked_sub(&total_issuance)
58-
}
59-
.expect("LHS > RHS");
60-
59+
let delta = 1000_i128;
60+
let diff = (total_issuance - expected_total_issuance).abs();
6161
if diff > delta {
6262
log::error!(
6363
"expected_total_issuance: {} != total_issuance: {}",

0 commit comments

Comments
 (0)