Skip to content

Commit e8cee05

Browse files
committed
error cases tests + clean up
1 parent 949e936 commit e8cee05

File tree

3 files changed

+105
-46
lines changed

3 files changed

+105
-46
lines changed

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,7 @@ mod dispatches {
23502350
let when = existing.0;
23512351
ensure!(
23522352
now > when + delay,
2353-
Error::<T>::ColdkeySwapReannouncedTooEarly
2353+
Error::<T>::ColdKeySwapReannouncedTooEarly
23542354
);
23552355
}
23562356

@@ -2371,11 +2371,11 @@ mod dispatches {
23712371
let who = ensure_signed(origin)?;
23722372

23732373
let (when, new_coldkey) = ColdkeySwapAnnouncements::<T>::take(who.clone())
2374-
.ok_or(Error::<T>::ColdkeySwapAnnouncementNotFound)?;
2374+
.ok_or(Error::<T>::ColdKeySwapAnnouncementNotFound)?;
23752375

23762376
let now = <frame_system::Pallet<T>>::block_number();
23772377
let delay = ColdkeySwapScheduleDuration::<T>::get();
2378-
ensure!(now > when + delay, Error::<T>::ColdkeySwapTooEarly);
2378+
ensure!(now > when + delay, Error::<T>::ColdKeySwapTooEarly);
23792379

23802380
Self::do_swap_coldkey(&who, &new_coldkey)?;
23812381

pallets/subtensor/src/macros/errors.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ mod errors {
138138
ColdKeyAlreadyAssociated,
139139
/// The coldkey balance is not enough to pay for the swap
140140
NotEnoughBalanceToPaySwapColdKey,
141-
/// The coldkey is in arbitration
142-
ColdkeyIsInArbitration,
143141
/// Attempting to set an invalid child for a hotkey on a network.
144142
InvalidChild,
145143
/// Duplicate child when setting children.
@@ -151,11 +149,11 @@ mod errors {
151149
/// Default transaction rate limit exceeded.
152150
TxRateLimitExceeded,
153151
/// Coldkey swap announcement not found
154-
ColdkeySwapAnnouncementNotFound,
152+
ColdKeySwapAnnouncementNotFound,
155153
/// Coldkey swap too early.
156-
ColdkeySwapTooEarly,
154+
ColdKeySwapTooEarly,
157155
/// Coldkey swap reannounced too early.
158-
ColdkeySwapReannouncedTooEarly,
156+
ColdKeySwapReannouncedTooEarly,
159157
/// New coldkey is hotkey
160158
NewColdKeyIsHotkey,
161159
/// Childkey take is invalid.

pallets/subtensor/src/tests/swap_coldkey.rs

Lines changed: 99 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::*;
2929
use crate::{Call, ColdkeySwapScheduleDuration, Error};
3030

3131
#[test]
32-
fn test_announce_coldkey_swap_with_no_announcement_works() {
32+
fn test_announce_coldkey_swap_works() {
3333
new_test_ext(1).execute_with(|| {
3434
let who = U256::from(1);
3535
let new_coldkey = U256::from(2);
@@ -121,7 +121,7 @@ fn test_announce_coldkey_swap_with_existing_announcement_not_past_delay_fails()
121121
RuntimeOrigin::signed(who.clone()),
122122
new_coldkey_2,
123123
),
124-
Error::<Test>::ColdkeySwapReannouncedTooEarly
124+
Error::<Test>::ColdKeySwapReannouncedTooEarly
125125
);
126126
});
127127
}
@@ -143,6 +143,103 @@ fn test_announce_coldkey_swap_with_bad_origin_fails() {
143143
});
144144
}
145145

146+
#[test]
147+
fn test_swap_coldkey_announced_too_early_fails() {
148+
new_test_ext(1).execute_with(|| {
149+
let who = U256::from(1);
150+
let new_coldkey = U256::from(2);
151+
152+
assert_ok!(SubtensorModule::announce_coldkey_swap(
153+
RuntimeOrigin::signed(who.clone()),
154+
new_coldkey,
155+
));
156+
157+
assert_noop!(
158+
SubtensorModule::swap_coldkey_announced(
159+
<Test as frame_system::Config>::RuntimeOrigin::signed(who)
160+
),
161+
Error::<Test>::ColdKeySwapTooEarly
162+
);
163+
})
164+
}
165+
166+
#[test]
167+
fn test_swap_coldkey_announced_with_already_associated_coldkey_fails() {
168+
new_test_ext(1).execute_with(|| {
169+
let who = U256::from(1);
170+
let new_coldkey = U256::from(2);
171+
let hotkey = U256::from(3);
172+
173+
assert_ok!(SubtensorModule::announce_coldkey_swap(
174+
RuntimeOrigin::signed(who.clone()),
175+
new_coldkey,
176+
));
177+
178+
let now = System::block_number();
179+
let delay = ColdkeySwapScheduleDuration::<Test>::get() + 1;
180+
System::run_to_block::<AllPalletsWithSystem>(now + delay);
181+
182+
let swap_cost = SubtensorModule::get_key_swap_cost();
183+
SubtensorModule::add_balance_to_coldkey_account(&who, swap_cost.to_u64());
184+
185+
SubtensorModule::create_account_if_non_existent(&new_coldkey, &hotkey);
186+
187+
assert_noop!(
188+
SubtensorModule::swap_coldkey_announced(
189+
<Test as frame_system::Config>::RuntimeOrigin::signed(who)
190+
),
191+
Error::<Test>::ColdKeyAlreadyAssociated
192+
);
193+
})
194+
}
195+
196+
#[test]
197+
fn test_swap_coldkey_announced_using_registered_hotkey_fails() {
198+
new_test_ext(1).execute_with(|| {
199+
let who = U256::from(1);
200+
let new_coldkey = U256::from(2);
201+
let hotkey = U256::from(3);
202+
203+
assert_ok!(SubtensorModule::announce_coldkey_swap(
204+
RuntimeOrigin::signed(who.clone()),
205+
hotkey.clone(),
206+
));
207+
208+
let now = System::block_number();
209+
let delay = ColdkeySwapScheduleDuration::<Test>::get() + 1;
210+
System::run_to_block::<AllPalletsWithSystem>(now + delay);
211+
212+
let swap_cost = SubtensorModule::get_key_swap_cost();
213+
SubtensorModule::add_balance_to_coldkey_account(&who, swap_cost.to_u64());
214+
215+
SubtensorModule::create_account_if_non_existent(&new_coldkey, &hotkey);
216+
217+
assert_noop!(
218+
SubtensorModule::swap_coldkey_announced(
219+
<Test as frame_system::Config>::RuntimeOrigin::signed(who)
220+
),
221+
Error::<Test>::NewColdKeyIsHotkey
222+
);
223+
})
224+
}
225+
226+
#[test]
227+
fn test_swap_coldkey_with_not_enough_balance_to_pay_swap_cost_fails() {
228+
new_test_ext(1).execute_with(|| {
229+
let who = U256::from(1);
230+
let new_coldkey = U256::from(2);
231+
232+
let now = System::block_number();
233+
let delay = ColdkeySwapScheduleDuration::<Test>::get() + 1;
234+
System::run_to_block::<AllPalletsWithSystem>(now + delay);
235+
236+
assert_noop!(
237+
SubtensorModule::do_swap_coldkey(&who, &new_coldkey),
238+
Error::<Test>::NotEnoughBalanceToPaySwapColdKey
239+
);
240+
});
241+
}
242+
146243
#[test]
147244
fn test_swap_subnet_owner() {
148245
new_test_ext(1).execute_with(|| {
@@ -662,28 +759,6 @@ fn test_swap_concurrent_modifications() {
662759
});
663760
}
664761

665-
#[test]
666-
fn test_swap_with_invalid_subnet_ownership() {
667-
new_test_ext(1).execute_with(|| {
668-
let old_coldkey = U256::from(1);
669-
let new_coldkey = U256::from(2);
670-
let netuid = NetUid::from(1u16);
671-
672-
SubnetOwner::<Test>::insert(netuid, old_coldkey);
673-
674-
// Simulate an invalid state where the subnet owner doesn't match the old_coldkey
675-
SubnetOwner::<Test>::insert(netuid, U256::from(3));
676-
677-
let swap_cost = SubtensorModule::get_key_swap_cost();
678-
SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, swap_cost.to_u64());
679-
680-
assert_ok!(SubtensorModule::do_swap_coldkey(&old_coldkey, &new_coldkey,));
681-
682-
// The swap should not affect the mismatched subnet ownership
683-
assert_eq!(SubnetOwner::<Test>::get(netuid), U256::from(3));
684-
});
685-
}
686-
687762
#[test]
688763
fn test_do_swap_coldkey_success() {
689764
new_test_ext(1).execute_with(|| {
@@ -1917,20 +1992,6 @@ fn test_coldkey_swap_no_identity_no_changes_newcoldkey_exists() {
19171992
});
19181993
}
19191994

1920-
#[test]
1921-
fn test_cant_schedule_swap_without_enough_to_burn() {
1922-
new_test_ext(1).execute_with(|| {
1923-
let old_coldkey = U256::from(3);
1924-
let new_coldkey = U256::from(4);
1925-
let hotkey = U256::from(5);
1926-
1927-
assert_noop!(
1928-
SubtensorModule::do_swap_coldkey(&old_coldkey, &new_coldkey),
1929-
Error::<Test>::NotEnoughBalanceToPaySwapColdKey
1930-
);
1931-
});
1932-
}
1933-
19341995
#[test]
19351996
fn test_coldkey_in_swap_schedule_prevents_funds_usage() {
19361997
// Testing the signed extension validate function

0 commit comments

Comments
 (0)