@@ -16,6 +16,7 @@ use frame_support::traits::schedule::v3::Named as ScheduleNamed;
1616use frame_support:: { assert_err, assert_noop, assert_ok} ;
1717use frame_system:: { Config , RawOrigin } ;
1818use sp_core:: { Get , H256 , U256 } ;
19+ use sp_runtime:: traits:: Hash ;
1920use sp_runtime:: traits:: { DispatchInfoOf , DispatchTransaction , TransactionExtension } ;
2021use sp_runtime:: { DispatchError , traits:: TxBaseImplication } ;
2122use substrate_fixed:: types:: U96F32 ;
@@ -33,24 +34,25 @@ fn test_announce_coldkey_swap_works() {
3334 new_test_ext ( 1 ) . execute_with ( || {
3435 let who = U256 :: from ( 1 ) ;
3536 let new_coldkey = U256 :: from ( 2 ) ;
37+ let new_coldkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey) ;
3638
3739 assert_eq ! ( ColdkeySwapAnnouncements :: <Test >:: iter( ) . count( ) , 0 ) ;
3840
3941 assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
4042 RuntimeOrigin :: signed( who. clone( ) ) ,
41- new_coldkey ,
43+ new_coldkey_hash ,
4244 ) ) ;
4345
4446 let now = System :: block_number ( ) ;
4547 assert_eq ! (
4648 ColdkeySwapAnnouncements :: <Test >:: iter( ) . collect:: <Vec <_>>( ) ,
47- vec![ ( who. clone( ) , ( now, new_coldkey ) ) ]
49+ vec![ ( who. clone( ) , ( now, new_coldkey_hash ) ) ]
4850 ) ;
4951 assert_eq ! (
5052 last_event( ) ,
5153 RuntimeEvent :: SubtensorModule ( Event :: ColdkeySwapAnnounced {
5254 who,
53- new_coldkey ,
55+ new_coldkey_hash ,
5456 block_number: now,
5557 } )
5658 ) ;
@@ -62,33 +64,35 @@ fn test_announce_coldkey_swap_with_existing_announcement_past_delay_works() {
6264 new_test_ext ( 1 ) . execute_with ( || {
6365 let who = U256 :: from ( 1 ) ;
6466 let new_coldkey = U256 :: from ( 2 ) ;
67+ let new_coldkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey) ;
6568 let new_coldkey_2 = U256 :: from ( 3 ) ;
69+ let new_coldkey_2_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey_2) ;
6670
6771 assert_eq ! ( ColdkeySwapAnnouncements :: <Test >:: iter( ) . count( ) , 0 ) ;
6872
6973 assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
7074 RuntimeOrigin :: signed( who. clone( ) ) ,
71- new_coldkey ,
75+ new_coldkey_hash ,
7276 ) ) ;
7377
7478 let now = System :: block_number ( ) ;
7579 assert_eq ! (
7680 ColdkeySwapAnnouncements :: <Test >:: iter( ) . collect:: <Vec <_>>( ) ,
77- vec![ ( who. clone( ) , ( now, new_coldkey ) ) ]
81+ vec![ ( who. clone( ) , ( now, new_coldkey_hash ) ) ]
7882 ) ;
7983
8084 let delay = ColdkeySwapScheduleDuration :: < Test > :: get ( ) + 1 ;
8185 System :: run_to_block :: < AllPalletsWithSystem > ( now + delay) ;
8286
8387 assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
8488 RuntimeOrigin :: signed( who. clone( ) ) ,
85- new_coldkey_2 ,
89+ new_coldkey_2_hash ,
8690 ) ) ;
8791
8892 let now = System :: block_number ( ) ;
8993 assert_eq ! (
9094 ColdkeySwapAnnouncements :: <Test >:: iter( ) . collect:: <Vec <_>>( ) ,
91- vec![ ( who. clone( ) , ( now, new_coldkey_2 ) ) ]
95+ vec![ ( who. clone( ) , ( now, new_coldkey_2_hash ) ) ]
9296 ) ;
9397 } ) ;
9498}
@@ -97,14 +101,15 @@ fn test_announce_coldkey_swap_with_existing_announcement_past_delay_works() {
97101fn test_announce_coldkey_swap_with_bad_origin_fails ( ) {
98102 new_test_ext ( 1 ) . execute_with ( || {
99103 let new_coldkey = U256 :: from ( 1 ) ;
104+ let new_coldkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey) ;
100105
101106 assert_noop ! (
102- SubtensorModule :: announce_coldkey_swap( RuntimeOrigin :: none( ) , new_coldkey ) ,
107+ SubtensorModule :: announce_coldkey_swap( RuntimeOrigin :: none( ) , new_coldkey_hash ) ,
103108 BadOrigin
104109 ) ;
105110
106111 assert_noop ! (
107- SubtensorModule :: announce_coldkey_swap( RuntimeOrigin :: root( ) , new_coldkey ) ,
112+ SubtensorModule :: announce_coldkey_swap( RuntimeOrigin :: root( ) , new_coldkey_hash ) ,
108113 BadOrigin
109114 ) ;
110115 } ) ;
@@ -115,19 +120,21 @@ fn test_announce_coldkey_swap_with_existing_announcement_not_past_delay_fails()
115120 new_test_ext ( 1 ) . execute_with ( || {
116121 let who = U256 :: from ( 1 ) ;
117122 let new_coldkey = U256 :: from ( 2 ) ;
123+ let new_coldkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey) ;
118124 let new_coldkey_2 = U256 :: from ( 3 ) ;
125+ let new_coldkey_2_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey_2) ;
119126
120127 assert_eq ! ( ColdkeySwapAnnouncements :: <Test >:: iter( ) . count( ) , 0 ) ;
121128
122129 assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
123130 RuntimeOrigin :: signed( who. clone( ) ) ,
124- new_coldkey ,
131+ new_coldkey_hash ,
125132 ) ) ;
126133
127134 let now = System :: block_number ( ) ;
128135 assert_eq ! (
129136 ColdkeySwapAnnouncements :: <Test >:: iter( ) . collect:: <Vec <_>>( ) ,
130- vec![ ( who. clone( ) , ( now, new_coldkey ) ) ]
137+ vec![ ( who. clone( ) , ( now, new_coldkey_hash ) ) ]
131138 ) ;
132139
133140 let unmet_delay = ColdkeySwapScheduleDuration :: < Test > :: get ( ) ;
@@ -136,7 +143,7 @@ fn test_announce_coldkey_swap_with_existing_announcement_not_past_delay_fails()
136143 assert_noop ! (
137144 SubtensorModule :: announce_coldkey_swap(
138145 RuntimeOrigin :: signed( who. clone( ) ) ,
139- new_coldkey_2 ,
146+ new_coldkey_2_hash ,
140147 ) ,
141148 Error :: <Test >:: ColdKeySwapReannouncedTooEarly
142149 ) ;
@@ -148,6 +155,7 @@ fn test_swap_coldkey_announced_works() {
148155 new_test_ext ( 1 ) . execute_with ( || {
149156 let who = U256 :: from ( 1 ) ;
150157 let new_coldkey = U256 :: from ( 2 ) ;
158+ let new_coldkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey) ;
151159 let hotkey1 = U256 :: from ( 1001 ) ;
152160 let hotkey2 = U256 :: from ( 1002 ) ;
153161 let hotkey3 = U256 :: from ( 1003 ) ;
@@ -167,11 +175,11 @@ fn test_swap_coldkey_announced_works() {
167175 // Announce the coldkey swap
168176 assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
169177 RuntimeOrigin :: signed( who. clone( ) ) ,
170- new_coldkey ,
178+ new_coldkey_hash ,
171179 ) ) ;
172180 assert_eq ! (
173- ColdkeySwapAnnouncements :: <Test >:: get ( who ) ,
174- Some ( ( now, new_coldkey ) )
181+ ColdkeySwapAnnouncements :: <Test >:: iter ( ) . collect :: < Vec <_>> ( ) ,
182+ vec! [ ( who . clone ( ) , ( now, new_coldkey_hash ) ) ]
175183 ) ;
176184
177185 // Run some blocks for the announcement to be past the delay
@@ -256,7 +264,8 @@ fn test_swap_coldkey_announced_works() {
256264 let total_stake_before = SubtensorModule :: get_total_stake ( ) ;
257265
258266 assert_ok ! ( SubtensorModule :: swap_coldkey_announced(
259- <Test as frame_system:: Config >:: RuntimeOrigin :: signed( who)
267+ <Test as frame_system:: Config >:: RuntimeOrigin :: signed( who) ,
268+ new_coldkey
260269 ) ) ;
261270
262271 // Ensure the announcement has been consumed
@@ -416,12 +425,12 @@ fn test_swap_coldkey_announced_with_bad_origin_fails() {
416425 let new_coldkey = U256 :: from ( 2 ) ;
417426
418427 assert_noop ! (
419- SubtensorModule :: swap_coldkey_announced( RuntimeOrigin :: none( ) ) ,
428+ SubtensorModule :: swap_coldkey_announced( RuntimeOrigin :: none( ) , new_coldkey ) ,
420429 BadOrigin
421430 ) ;
422431
423432 assert_noop ! (
424- SubtensorModule :: swap_coldkey_announced( RuntimeOrigin :: root( ) ) ,
433+ SubtensorModule :: swap_coldkey_announced( RuntimeOrigin :: root( ) , new_coldkey ) ,
425434 BadOrigin
426435 ) ;
427436 } ) ;
@@ -434,26 +443,48 @@ fn test_swap_coldkey_announced_without_announcement_fails() {
434443 let new_coldkey = U256 :: from ( 2 ) ;
435444
436445 assert_noop ! (
437- SubtensorModule :: swap_coldkey_announced( RuntimeOrigin :: signed( who) ) ,
446+ SubtensorModule :: swap_coldkey_announced( RuntimeOrigin :: signed( who) , new_coldkey ) ,
438447 Error :: <Test >:: ColdKeySwapAnnouncementNotFound
439448 ) ;
440449 } )
441450}
442451
452+ #[ test]
453+ fn test_swap_coldkey_announced_with_mismatched_coldkey_hash_fails ( ) {
454+ new_test_ext ( 1 ) . execute_with ( || {
455+ let who = U256 :: from ( 1 ) ;
456+ let new_coldkey = U256 :: from ( 2 ) ;
457+ let new_coldkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey) ;
458+ let other_coldkey = U256 :: from ( 3 ) ;
459+
460+ assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
461+ RuntimeOrigin :: signed( who. clone( ) ) ,
462+ new_coldkey_hash,
463+ ) ) ;
464+
465+ assert_noop ! (
466+ SubtensorModule :: swap_coldkey_announced( RuntimeOrigin :: signed( who) , other_coldkey) ,
467+ Error :: <Test >:: AnnouncedColdkeyHashDoesNotMatch
468+ ) ;
469+ } )
470+ }
471+
443472#[ test]
444473fn test_swap_coldkey_announced_too_early_fails ( ) {
445474 new_test_ext ( 1 ) . execute_with ( || {
446475 let who = U256 :: from ( 1 ) ;
447476 let new_coldkey = U256 :: from ( 2 ) ;
477+ let new_coldkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey) ;
448478
449479 assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
450480 RuntimeOrigin :: signed( who. clone( ) ) ,
451- new_coldkey ,
481+ new_coldkey_hash ,
452482 ) ) ;
453483
454484 assert_noop ! (
455485 SubtensorModule :: swap_coldkey_announced(
456- <Test as frame_system:: Config >:: RuntimeOrigin :: signed( who)
486+ <Test as frame_system:: Config >:: RuntimeOrigin :: signed( who) ,
487+ new_coldkey
457488 ) ,
458489 Error :: <Test >:: ColdKeySwapTooEarly
459490 ) ;
@@ -465,11 +496,12 @@ fn test_swap_coldkey_announced_with_already_associated_coldkey_fails() {
465496 new_test_ext ( 1 ) . execute_with ( || {
466497 let who = U256 :: from ( 1 ) ;
467498 let new_coldkey = U256 :: from ( 2 ) ;
499+ let new_coldkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey) ;
468500 let hotkey = U256 :: from ( 3 ) ;
469501
470502 assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
471503 RuntimeOrigin :: signed( who. clone( ) ) ,
472- new_coldkey ,
504+ new_coldkey_hash ,
473505 ) ) ;
474506
475507 let now = System :: block_number ( ) ;
@@ -483,7 +515,8 @@ fn test_swap_coldkey_announced_with_already_associated_coldkey_fails() {
483515
484516 assert_noop ! (
485517 SubtensorModule :: swap_coldkey_announced(
486- <Test as frame_system:: Config >:: RuntimeOrigin :: signed( who)
518+ <Test as frame_system:: Config >:: RuntimeOrigin :: signed( who) ,
519+ new_coldkey
487520 ) ,
488521 Error :: <Test >:: ColdKeyAlreadyAssociated
489522 ) ;
@@ -496,10 +529,11 @@ fn test_swap_coldkey_announced_with_hotkey_fails() {
496529 let who = U256 :: from ( 1 ) ;
497530 let new_coldkey = U256 :: from ( 2 ) ;
498531 let hotkey = U256 :: from ( 3 ) ;
532+ let hotkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & hotkey) ;
499533
500534 assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
501535 RuntimeOrigin :: signed( who. clone( ) ) ,
502- hotkey . clone ( ) ,
536+ hotkey_hash ,
503537 ) ) ;
504538
505539 let now = System :: block_number ( ) ;
@@ -513,7 +547,8 @@ fn test_swap_coldkey_announced_with_hotkey_fails() {
513547
514548 assert_noop ! (
515549 SubtensorModule :: swap_coldkey_announced(
516- <Test as frame_system:: Config >:: RuntimeOrigin :: signed( who)
550+ <Test as frame_system:: Config >:: RuntimeOrigin :: signed( who) ,
551+ hotkey
517552 ) ,
518553 Error :: <Test >:: NewColdKeyIsHotkey
519554 ) ;
@@ -1257,6 +1292,7 @@ fn test_subtensor_extension_rejects_any_call_that_is_not_swap_coldkey_announced(
12571292 let netuid = NetUid :: from ( 1 ) ;
12581293 let who = U256 :: from ( 0 ) ;
12591294 let new_coldkey = U256 :: from ( 1 ) ;
1295+ let new_coldkey_hash = <Test as frame_system:: Config >:: Hashing :: hash_of ( & new_coldkey) ;
12601296 let hotkey = U256 :: from ( 2 ) ;
12611297 let stake = DefaultMinStake :: < Test > :: get ( ) . to_u64 ( ) ;
12621298 assert_ne ! ( hotkey, who) ;
@@ -1274,7 +1310,7 @@ fn test_subtensor_extension_rejects_any_call_that_is_not_swap_coldkey_announced(
12741310 // Schedule the coldkey for a swap
12751311 assert_ok ! ( SubtensorModule :: announce_coldkey_swap(
12761312 <Test as frame_system:: Config >:: RuntimeOrigin :: signed( who) ,
1277- new_coldkey ,
1313+ new_coldkey_hash ,
12781314 ) ) ;
12791315 assert ! ( ColdkeySwapAnnouncements :: <Test >:: contains_key( who) ) ;
12801316
@@ -1360,7 +1396,8 @@ fn test_subtensor_extension_rejects_any_call_that_is_not_swap_coldkey_announced(
13601396 }
13611397
13621398 // Swap coldkey announced should succeed
1363- let call = RuntimeCall :: SubtensorModule ( SubtensorCall :: swap_coldkey_announced { } ) ;
1399+ let call =
1400+ RuntimeCall :: SubtensorModule ( SubtensorCall :: swap_coldkey_announced { new_coldkey } ) ;
13641401 let info = call. get_dispatch_info ( ) ;
13651402 let ext = SubtensorTransactionExtension :: < Test > :: new ( ) ;
13661403 assert_ok ! ( ext. dispatch_transaction( RuntimeOrigin :: signed( who) . into( ) , call, & info, 0 , 0 ) ) ;
@@ -1403,8 +1440,4 @@ fn test_schedule_swap_coldkey_deprecated() {
14031440 } ) ;
14041441}
14051442
1406- // TEST STAKING HOTKEY ARE ADDITIVE TO THE EXISTING ONES
1407-
1408- // TEST HOTKEYS OWNERSHIP IS ADDITIVE TO THE EXISTING ONES
1409-
14101443// TEST TRANSFER ROOT CLAIM WITH NEW KEYS + ROOT CASE
0 commit comments