Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-otf committed Jan 30, 2025
1 parent a7fffd8 commit d39a6ea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
9 changes: 3 additions & 6 deletions pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ use frame_support::pallet_macros::pallet_section;
/// This can later be imported into the pallet using [`import_section`].
#[pallet_section]
mod hooks {
#[cfg(feature = "try-runtime")]
use crate::utils::try_state::TryState;

// ================
// ==== Hooks =====
// ================
Expand Down Expand Up @@ -85,9 +82,9 @@ mod hooks {

#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), sp_runtime::TryRuntimeError> {
TryState::<T>::check_total_issuance()?;
// Disabled: https://github.com/opentensor/subtensor/pull/1166
// TryState::<T>::check_total_stake()?;
Self::check_total_issuance()?;
// Disabled: https://github.com/opentensor/subtensor/pull/1166
// Self::check_total_stake()?;
Ok(())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ pub mod initialise_total_issuance {
use frame_support::pallet_prelude::Weight;
use frame_support::traits::OnRuntimeUpgrade;

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

pub struct Migration<T: Config>(PhantomData<T>);
Expand All @@ -80,7 +78,7 @@ pub mod initialise_total_issuance {
#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
// Verify that all accounting invariants are satisfied after the migration
TryState::<T>::check_total_issuance()?;
crate::Pallet::<T>::check_total_issuance()?;
Ok(())
}
}
Expand Down
32 changes: 17 additions & 15 deletions pallets/subtensor/src/utils/try_state.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use core::marker::PhantomData;
use frame_support::traits::fungible::Inspect;
use frame_support::traits::fungible::Inspect;

use crate::subnets::subnet::POOL_INITIAL_TAO;
use super::*;
use crate::subnets::subnet::POOL_INITIAL_TAO;

pub(crate) struct TryState<T: Config>(PhantomData<T>);

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

// Get the total currency issuance
let currency_issuance = T::Currency::total_issuance();
Expand Down Expand Up @@ -40,31 +37,36 @@ impl<T: Config> TryState<T> {
"TotalIssuance diff greater than allowable delta",
);

Ok(())
Ok(())
}

/// Checks the sum of all stakes matches the [`TotalStake`].
/// Checks the sum of all stakes matches the [`TotalStake`].
#[allow(dead_code)]
pub(crate) fn check_total_stake() -> Result<(), sp_runtime::TryRuntimeError> {
// Calculate the total staked amount
let total_staked = SubnetTAO::<T>::iter().fold(0u64, |acc, (netuid, stake)| {
let acc = acc.saturating_add(stake);

if netuid == Pallet::<T>::get_root_netuid() {
if netuid == Self::get_root_netuid() {
// root network doesn't have initial pool TAO
acc
} else {
acc.saturating_sub(POOL_INITIAL_TAO)
}
});

log::warn!("total_staked: {}, TotalStake: {}", total_staked, TotalStake::<T>::get());
log::warn!(
"total_staked: {}, TotalStake: {}",
total_staked,
TotalStake::<T>::get()
);

// Verify that the calculated total stake matches the stored TotalStake
ensure!(
total_staked == TotalStake::<T>::get(),
"TotalStake does not match total staked",
);

Ok(())
}
Ok(())
}
}
2 changes: 1 addition & 1 deletion scripts/try-runtime-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ do_try_runtime() {
fi

eval "try-runtime --runtime $runtime_wasm_path on-runtime-upgrade \
--disable-spec-version-check --disable-idempotency-checks --checks=all \
--no-weight-warnings --disable-spec-version-check --disable-idempotency-checks --checks=all \
$chain_state"
}

Expand Down

0 comments on commit d39a6ea

Please sign in to comment.