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,17 +278,25 @@ 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 ) ] ) ;
288290 Stake :: < Test > :: insert ( U256 :: from ( 1 ) , U256 :: from ( 0 ) , 10000 ) ;
289291 Stake :: < Test > :: insert ( U256 :: from ( 2 ) , U256 :: from ( 0 ) , 10000 ) ;
290292 Stake :: < Test > :: insert ( U256 :: from ( 3 ) , U256 :: from ( 0 ) , 10000 ) ;
291- pallet_subtensor:: migration:: do_migrate_fix_total_coldkey_stake :: < Test > ( ) ;
293+
294+ let weight = run_migration_and_check ( migration_name) ;
295+ assert ! ( weight != Weight :: zero( ) ) ;
296+ assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 30000 ) ;
297+
298+ let second_weight = run_migration_and_check ( migration_name) ;
299+ assert_eq ! ( second_weight, Weight :: zero( ) ) ;
292300 assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 30000 ) ;
293301 } )
294302}
@@ -297,13 +305,16 @@ fn test_migrate_fix_total_coldkey_stake() {
297305#[ test]
298306fn test_migrate_fix_total_coldkey_stake_value_already_in_total ( ) {
299307 new_test_ext ( 1 ) . execute_with ( || {
308+ let migration_name = "fix_total_coldkey_stake_v7" ;
300309 let coldkey = U256 :: from ( 0 ) ;
301310 TotalColdkeyStake :: < Test > :: insert ( coldkey, 100000000 ) ;
302311 StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
303312 Stake :: < Test > :: insert ( U256 :: from ( 1 ) , U256 :: from ( 0 ) , 10000 ) ;
304313 Stake :: < Test > :: insert ( U256 :: from ( 2 ) , U256 :: from ( 0 ) , 10000 ) ;
305314 Stake :: < Test > :: insert ( U256 :: from ( 3 ) , U256 :: from ( 0 ) , 10000 ) ;
306- pallet_subtensor:: migration:: do_migrate_fix_total_coldkey_stake :: < Test > ( ) ;
315+
316+ let weight = run_migration_and_check ( migration_name) ;
317+ assert ! ( weight != Weight :: zero( ) ) ;
307318 assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 30000 ) ;
308319 } )
309320}
@@ -312,12 +323,15 @@ fn test_migrate_fix_total_coldkey_stake_value_already_in_total() {
312323#[ test]
313324fn test_migrate_fix_total_coldkey_stake_no_entry ( ) {
314325 new_test_ext ( 1 ) . execute_with ( || {
326+ let migration_name = "fix_total_coldkey_stake_v7" ;
315327 let coldkey = U256 :: from ( 0 ) ;
316328 StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
317329 Stake :: < Test > :: insert ( U256 :: from ( 1 ) , U256 :: from ( 0 ) , 10000 ) ;
318330 Stake :: < Test > :: insert ( U256 :: from ( 2 ) , U256 :: from ( 0 ) , 10000 ) ;
319331 Stake :: < Test > :: insert ( U256 :: from ( 3 ) , U256 :: from ( 0 ) , 10000 ) ;
320- pallet_subtensor:: migration:: do_migrate_fix_total_coldkey_stake :: < Test > ( ) ;
332+
333+ let weight = run_migration_and_check ( migration_name) ;
334+ assert ! ( weight != Weight :: zero( ) ) ;
321335 assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 30000 ) ;
322336 } )
323337}
@@ -326,10 +340,13 @@ fn test_migrate_fix_total_coldkey_stake_no_entry() {
326340#[ test]
327341fn test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys ( ) {
328342 new_test_ext ( 1 ) . execute_with ( || {
343+ let migration_name = "fix_total_coldkey_stake_v7" ;
329344 let coldkey = U256 :: from ( 0 ) ;
330345 TotalColdkeyStake :: < Test > :: insert ( coldkey, 100000000 ) ;
331346 StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
332- pallet_subtensor:: migration:: do_migrate_fix_total_coldkey_stake :: < Test > ( ) ;
347+
348+ let weight = run_migration_and_check ( migration_name) ;
349+ assert ! ( weight != Weight :: zero( ) ) ;
333350 assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 0 ) ;
334351 } )
335352}
@@ -338,12 +355,53 @@ fn test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys() {
338355#[ test]
339356fn test_migrate_fix_total_coldkey_stake_one_hotkey_stake_missing ( ) {
340357 new_test_ext ( 1 ) . execute_with ( || {
358+ let migration_name = "fix_total_coldkey_stake_v7" ;
341359 let coldkey = U256 :: from ( 0 ) ;
342360 TotalColdkeyStake :: < Test > :: insert ( coldkey, 100000000 ) ;
343361 StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
344362 Stake :: < Test > :: insert ( U256 :: from ( 1 ) , U256 :: from ( 0 ) , 10000 ) ;
345363 Stake :: < Test > :: insert ( U256 :: from ( 2 ) , U256 :: from ( 0 ) , 10000 ) ;
346- pallet_subtensor:: migration:: do_migrate_fix_total_coldkey_stake :: < Test > ( ) ;
364+
365+ let weight = run_migration_and_check ( migration_name) ;
366+ assert ! ( weight != Weight :: zero( ) ) ;
347367 assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 20000 ) ;
348368 } )
349369}
370+
371+ // New test to check if migration runs only once
372+ #[ test]
373+ fn test_migrate_fix_total_coldkey_stake_runs_once ( ) {
374+ new_test_ext ( 1 ) . execute_with ( || {
375+ let migration_name = "fix_total_coldkey_stake_v7" ;
376+ let coldkey = U256 :: from ( 0 ) ;
377+ TotalColdkeyStake :: < Test > :: insert ( coldkey, 0 ) ;
378+ StakingHotkeys :: < Test > :: insert ( coldkey, vec ! [ U256 :: from( 1 ) , U256 :: from( 2 ) , U256 :: from( 3 ) ] ) ;
379+ Stake :: < Test > :: insert ( U256 :: from ( 1 ) , U256 :: from ( 0 ) , 10000 ) ;
380+ Stake :: < Test > :: insert ( U256 :: from ( 2 ) , U256 :: from ( 0 ) , 10000 ) ;
381+ Stake :: < Test > :: insert ( U256 :: from ( 3 ) , U256 :: from ( 0 ) , 10000 ) ;
382+
383+ // First run
384+ let first_weight = run_migration_and_check ( migration_name) ;
385+ assert ! ( first_weight != Weight :: zero( ) ) ;
386+ assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 30000 ) ;
387+
388+ // Second run
389+ let second_weight = run_migration_and_check ( migration_name) ;
390+ assert_eq ! ( second_weight, Weight :: zero( ) ) ;
391+ assert_eq ! ( TotalColdkeyStake :: <Test >:: get( coldkey) , 30000 ) ;
392+ } )
393+ }
394+
395+ fn run_migration_and_check ( migration_name : & ' static str ) -> frame_support:: weights:: Weight {
396+ // Execute the migration and store its weight
397+ let weight: frame_support:: weights:: Weight =
398+ pallet_subtensor:: migration:: migrate_fix_total_coldkey_stake :: < Test > ( ) ;
399+
400+ // Check if the migration has been marked as completed
401+ assert ! ( HasMigrationRun :: <Test >:: get(
402+ migration_name. as_bytes( ) . to_vec( )
403+ ) ) ;
404+
405+ // Return the weight of the executed migration
406+ weight
407+ }
0 commit comments