1
1
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;
4
4
use sp_std:: vec:: Vec ;
5
5
6
6
// TODO: Implement comprehensive tests for this migration
@@ -14,10 +14,46 @@ pub mod deprecated_loaded_emission_format {
14
14
StorageMap < Pallet < T > , Identity , u16 , Vec < ( AccountIdOf < T > , u64 ) > , OptionQuery > ;
15
15
}
16
16
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
+
17
54
pub mod initialise_total_issuance {
18
55
use frame_support:: pallet_prelude:: Weight ;
19
- use frame_support:: traits:: { fungible, OnRuntimeUpgrade } ;
20
- use sp_core:: Get ;
56
+ use frame_support:: traits:: OnRuntimeUpgrade ;
21
57
22
58
use crate :: * ;
23
59
@@ -33,41 +69,7 @@ pub mod initialise_total_issuance {
33
69
///
34
70
/// Returns the weight of the migration operation.
35
71
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 )
72
+ super :: migrate_init_total_issuance :: < T > ( )
71
73
}
72
74
73
75
/// Performs post-upgrade checks to ensure the migration was successful.
@@ -76,7 +78,7 @@ pub mod initialise_total_issuance {
76
78
#[ cfg( feature = "try-runtime" ) ]
77
79
fn post_upgrade ( _state : Vec < u8 > ) -> Result < ( ) , sp_runtime:: TryRuntimeError > {
78
80
// Verify that all accounting invariants are satisfied after the migration
79
- crate :: Pallet :: < T > :: check_accounting_invariants ( ) ?;
81
+ crate :: Pallet :: < T > :: check_total_issuance ( ) ?;
80
82
Ok ( ( ) )
81
83
}
82
84
}
0 commit comments