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 ;
16
2
use frame_support:: traits:: fungible:: Inspect ;
17
3
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 :: * ;
31
6
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 > ) ;
37
8
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 > {
38
13
// 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 ( ) ;
40
15
41
16
// Get the total currency issuance
42
17
let currency_issuance = T :: Currency :: total_issuance ( ) ;
@@ -59,11 +34,37 @@ impl<T: Config> Pallet<T> {
59
34
expected_total_issuance. checked_sub ( total_issuance)
60
35
}
61
36
. expect ( "LHS > RHS" ) ;
37
+
62
38
ensure ! (
63
39
diff <= delta,
64
40
"TotalIssuance diff greater than allowable delta" ,
65
41
) ;
66
42
67
- Ok ( ( ) )
43
+ Ok ( ( ) )
68
44
}
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
+ }
69
70
}
0 commit comments