Skip to content

Commit a7fffd8

Browse files
committed
Refactor TryState
1 parent f8d5d4d commit a7fffd8

File tree

5 files changed

+48
-40
lines changed

5 files changed

+48
-40
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use frame_support::pallet_macros::pallet_section;
44
/// This can later be imported into the pallet using [`import_section`].
55
#[pallet_section]
66
mod hooks {
7+
#[cfg(feature = "try-runtime")]
8+
use crate::utils::try_state::TryState;
9+
710
// ================
811
// ==== Hooks =====
912
// ================
@@ -82,7 +85,9 @@ mod hooks {
8285

8386
#[cfg(feature = "try-runtime")]
8487
fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
85-
Self::check_accounting_invariants()?;
88+
TryState::<T>::check_total_issuance()?;
89+
// Disabled: https://github.com/opentensor/subtensor/pull/1166
90+
// TryState::<T>::check_total_stake()?;
8691
Ok(())
8792
}
8893
}

pallets/subtensor/src/migrations/migrate_init_total_issuance.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ pub(crate) fn migrate_init_total_issuance<T: Config>() -> Weight {
5353

5454
pub mod initialise_total_issuance {
5555
use frame_support::pallet_prelude::Weight;
56-
use frame_support::traits::{fungible, OnRuntimeUpgrade};
57-
use sp_core::Get;
56+
use frame_support::traits::OnRuntimeUpgrade;
5857

58+
#[cfg(feature = "try-runtime")]
59+
use crate::utils::try_state::TryState;
5960
use crate::*;
6061

6162
pub struct Migration<T: Config>(PhantomData<T>);
@@ -79,7 +80,7 @@ pub mod initialise_total_issuance {
7980
#[cfg(feature = "try-runtime")]
8081
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
8182
// Verify that all accounting invariants are satisfied after the migration
82-
crate::Pallet::<T>::check_accounting_invariants()?;
83+
TryState::<T>::check_total_issuance()?;
8384
Ok(())
8485
}
8586
}

pallets/subtensor/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ use super::*;
22
pub mod identity;
33
pub mod misc;
44
pub mod rate_limiting;
5+
#[cfg(feature = "try-runtime")]
56
pub mod try_state;
Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,17 @@
1-
use super::*;
2-
3-
impl<T: Config> Pallet<T> {
4-
/// Checks if the accounting invariants for [`TotalStake`], [`TotalSubnetLocked`], and [`TotalIssuance`] are correct.
5-
///
6-
/// This function verifies that:
7-
/// 1. The sum of all stakes matches the [`TotalStake`].
8-
/// 2. The [`TotalSubnetLocked`] is correctly calculated.
9-
/// 3. The [`TotalIssuance`] equals the sum of currency issuance, total stake, and total subnet locked.
10-
///
11-
/// # Returns
12-
///
13-
/// Returns `Ok(())` if all invariants are correct, otherwise returns an error.
14-
#[cfg(feature = "try-runtime")]
15-
pub fn check_accounting_invariants() -> Result<(), sp_runtime::TryRuntimeError> {
1+
use core::marker::PhantomData;
162
use frame_support::traits::fungible::Inspect;
173

18-
// Disabled: https://github.com/opentensor/subtensor/pull/1166
19-
//
20-
// // Calculate the total staked amount
21-
// let total_staked = SubnetTAO::<T>::iter().fold(0u64, |acc, (netuid, stake)| {
22-
// let acc = acc.saturating_add(stake);
23-
24-
// if netuid == Self::get_root_netuid() {
25-
// // root network doesn't have initial pool TAO
26-
// acc
27-
// } else {
28-
// acc.saturating_sub(POOL_INITIAL_TAO)
29-
// }
30-
// });
4+
use crate::subnets::subnet::POOL_INITIAL_TAO;
5+
use super::*;
316

32-
// // Verify that the calculated total stake matches the stored TotalStake
33-
// ensure!(
34-
// total_staked == TotalStake::<T>::get(),
35-
// "TotalStake does not match total staked",
36-
// );
7+
pub(crate) struct TryState<T: Config>(PhantomData<T>);
378

9+
impl<T: Config> TryState<T> {
10+
/// Checks [`TotalIssuance`] equals the sum of currency issuance, total stake, and total subnet
11+
/// locked.
12+
pub(crate) fn check_total_issuance() -> Result<(), sp_runtime::TryRuntimeError> {
3813
// Get the total subnet locked amount
39-
let total_subnet_locked = Self::get_total_subnet_locked();
14+
let total_subnet_locked = Pallet::<T>::get_total_subnet_locked();
4015

4116
// Get the total currency issuance
4217
let currency_issuance = T::Currency::total_issuance();
@@ -59,11 +34,37 @@ impl<T: Config> Pallet<T> {
5934
expected_total_issuance.checked_sub(total_issuance)
6035
}
6136
.expect("LHS > RHS");
37+
6238
ensure!(
6339
diff <= delta,
6440
"TotalIssuance diff greater than allowable delta",
6541
);
6642

67-
Ok(())
43+
Ok(())
6844
}
45+
46+
/// Checks the sum of all stakes matches the [`TotalStake`].
47+
pub(crate) fn check_total_stake() -> Result<(), sp_runtime::TryRuntimeError> {
48+
// Calculate the total staked amount
49+
let total_staked = SubnetTAO::<T>::iter().fold(0u64, |acc, (netuid, stake)| {
50+
let acc = acc.saturating_add(stake);
51+
52+
if netuid == Pallet::<T>::get_root_netuid() {
53+
// root network doesn't have initial pool TAO
54+
acc
55+
} else {
56+
acc.saturating_sub(POOL_INITIAL_TAO)
57+
}
58+
});
59+
60+
log::warn!("total_staked: {}, TotalStake: {}", total_staked, TotalStake::<T>::get());
61+
62+
// Verify that the calculated total stake matches the stored TotalStake
63+
ensure!(
64+
total_staked == TotalStake::<T>::get(),
65+
"TotalStake does not match total staked",
66+
);
67+
68+
Ok(())
69+
}
6970
}

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 --checks=all \
52+
--disable-spec-version-check --disable-idempotency-checks --checks=all \
5353
$chain_state"
5454
}
5555

0 commit comments

Comments
 (0)