11#![ allow( clippy:: unwrap_used) ]
22
33mod mock;
4- use frame_support:: assert_ok;
4+ use frame_support:: { assert_ok, weights :: Weight } ;
55use frame_system:: Config ;
66use mock:: * ;
77use pallet_subtensor:: * ;
@@ -278,10 +278,12 @@ fn test_migration_delete_subnet_21() {
278278 } )
279279}
280280
281+ // SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake --exact --nocapture
281282// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake --exact --nocapture
282283#[ test]
283284fn test_migrate_fix_total_coldkey_stake ( ) {
284285 new_test_ext ( 1 ) . execute_with ( || {
286+ let migration_name = "fix_total_coldkey_stake_v7" ;
285287 let coldkey = U256 :: from ( 0 ) ;
286288 TotalColdkeyStake :: < Test > :: insert ( coldkey, 0 ) ;
287289 StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
@@ -297,6 +299,7 @@ fn test_migrate_fix_total_coldkey_stake() {
297299#[ test]
298300fn test_migrate_fix_total_coldkey_stake_value_already_in_total ( ) {
299301 new_test_ext ( 1 ) . execute_with ( || {
302+ let migration_name = "fix_total_coldkey_stake_v7" ;
300303 let coldkey = U256 :: from ( 0 ) ;
301304 TotalColdkeyStake :: < Test > :: insert ( coldkey, 100000000 ) ;
302305 StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
@@ -312,6 +315,7 @@ fn test_migrate_fix_total_coldkey_stake_value_already_in_total() {
312315#[ test]
313316fn test_migrate_fix_total_coldkey_stake_no_entry ( ) {
314317 new_test_ext ( 1 ) . execute_with ( || {
318+ let migration_name = "fix_total_coldkey_stake_v7" ;
315319 let coldkey = U256 :: from ( 0 ) ;
316320 StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
317321 Stake :: < Test > :: insert ( U256 :: from ( 1 ) , U256 :: from ( 0 ) , 10000 ) ;
@@ -326,6 +330,7 @@ fn test_migrate_fix_total_coldkey_stake_no_entry() {
326330#[ test]
327331fn test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys ( ) {
328332 new_test_ext ( 1 ) . execute_with ( || {
333+ let migration_name = "fix_total_coldkey_stake_v7" ;
329334 let coldkey = U256 :: from ( 0 ) ;
330335 TotalColdkeyStake :: < Test > :: insert ( coldkey, 100000000 ) ;
331336 StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
@@ -338,6 +343,7 @@ fn test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys() {
338343#[ test]
339344fn test_migrate_fix_total_coldkey_stake_one_hotkey_stake_missing ( ) {
340345 new_test_ext ( 1 ) . execute_with ( || {
346+ let migration_name = "fix_total_coldkey_stake_v7" ;
341347 let coldkey = U256 :: from ( 0 ) ;
342348 TotalColdkeyStake :: < Test > :: insert ( coldkey, 100000000 ) ;
343349 StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
@@ -347,3 +353,58 @@ fn test_migrate_fix_total_coldkey_stake_one_hotkey_stake_missing() {
347353 assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 20000 ) ;
348354 } )
349355}
356+
357+ // New test to check if migration runs only once
358+ #[ test]
359+ fn test_migrate_fix_total_coldkey_stake_runs_once ( ) {
360+ new_test_ext ( 1 ) . execute_with ( || {
361+ let migration_name = "fix_total_coldkey_stake_v7" ;
362+ let coldkey = U256 :: from ( 0 ) ;
363+ TotalColdkeyStake :: < Test > :: insert ( coldkey, 0 ) ;
364+ StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
365+ Stake :: < Test > :: insert ( U256 :: from ( 1 ) , U256 :: from ( 0 ) , 10000 ) ;
366+ Stake :: < Test > :: insert ( U256 :: from ( 2 ) , U256 :: from ( 0 ) , 10000 ) ;
367+ Stake :: < Test > :: insert ( U256 :: from ( 3 ) , U256 :: from ( 0 ) , 10000 ) ;
368+
369+ // First run
370+ let first_weight = run_migration_and_check ( migration_name) ;
371+ assert ! ( first_weight != Weight :: zero( ) ) ;
372+ assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 30000 ) ;
373+
374+ // Second run
375+ let second_weight = run_migration_and_check ( migration_name) ;
376+ assert_eq ! ( second_weight, Weight :: zero( ) ) ;
377+ assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 30000 ) ;
378+ } )
379+ }
380+
381+ // SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake_starts_with_value_no_stake_map_entries --exact --nocapture
382+ #[ test]
383+ fn test_migrate_fix_total_coldkey_stake_starts_with_value_no_stake_map_entries ( ) {
384+ new_test_ext ( 1 ) . execute_with ( || {
385+ let migration_name = "fix_total_coldkey_stake_v7" ;
386+ let coldkey = U256 :: from ( 0 ) ;
387+ TotalColdkeyStake :: < Test > :: insert ( coldkey, 123_456_789 ) ;
388+
389+ // Notably, coldkey has no stake map or staking_hotkeys map entries
390+
391+ let weight = run_migration_and_check ( migration_name) ;
392+ assert ! ( weight != Weight :: zero( ) ) ;
393+ // Therefore 0
394+ assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 0 ) ;
395+ } )
396+ }
397+
398+ fn run_migration_and_check ( migration_name : & ' static str ) -> frame_support:: weights:: Weight {
399+ // Execute the migration and store its weight
400+ let weight: frame_support:: weights:: Weight =
401+ pallet_subtensor:: migration:: migrate_fix_total_coldkey_stake :: < Test > ( ) ;
402+
403+ // Check if the migration has been marked as completed
404+ assert ! ( HasMigrationRun :: <Test >:: get(
405+ migration_name. as_bytes( ) . to_vec( )
406+ ) ) ;
407+
408+ // Return the weight of the executed migration
409+ weight
410+ }
0 commit comments