Skip to content

Commit ba00c23

Browse files
constconst
authored andcommitted
add stake balance as equal
1 parent 4a97e79 commit ba00c23

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

pallets/subtensor/src/swap.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ impl<T: Config> Pallet<T> {
199199
}
200200
}
201201

202+
pub fn meets_min_allowed_coldkey_balance( coldkey: &T::AccountId ) -> bool {
203+
let all_staked_keys: Vec<T::AccountId> = StakingHotkeys::<T>::get(coldkey);
204+
let mut total_staking_balance: u64 = 0;
205+
for hotkey in all_staked_keys {
206+
total_staking_balance += Self::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey);
207+
}
208+
total_staking_balance += Self::get_coldkey_balance(&coldkey);
209+
total_staking_balance >= MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP
210+
}
211+
202212
/// Schedules a coldkey swap to a new coldkey with arbitration.
203213
///
204214
/// # Arguments
@@ -238,7 +248,7 @@ impl<T: Config> Pallet<T> {
238248

239249
// Check minimum amount of TAO (1 TAO)
240250
ensure!(
241-
Self::get_coldkey_balance(old_coldkey) >= MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP,
251+
Self::meets_min_allowed_coldkey_balance(&old_coldkey),
242252
Error::<T>::InsufficientBalanceToPerformColdkeySwap
243253
);
244254

pallets/subtensor/tests/staking.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,3 +3980,41 @@ fn next_block() {
39803980
System::on_initialize(System::block_number());
39813981
SubtensorModule::on_initialize(System::block_number());
39823982
}
3983+
3984+
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test staking -- test_coldkey_meets_enough --exact --nocapture
3985+
#[test]
3986+
fn test_coldkey_meets_enough() {
3987+
new_test_ext(1).execute_with(|| {
3988+
let coldkey = U256::from(1);
3989+
let new_coldkey = U256::from(2);
3990+
let hotkey = U256::from(2);
3991+
let netuid = 1u16;
3992+
add_network(netuid, 13, 0);
3993+
register_ok_neuron(netuid, hotkey, coldkey, 0);
3994+
let current_block = SubtensorModule::get_current_block_as_u64();
3995+
let (work1, nonce1) = generate_valid_pow(&coldkey, current_block, U256::from(10_000_000u64));
3996+
assert_err!(
3997+
SubtensorModule::do_schedule_coldkey_swap(
3998+
&coldkey.clone(),
3999+
&new_coldkey,
4000+
work1.to_fixed_bytes().to_vec(),
4001+
current_block,
4002+
nonce1
4003+
),
4004+
Error::<Test>::InsufficientBalanceToPerformColdkeySwap
4005+
);
4006+
SubtensorModule::add_balance_to_coldkey_account(
4007+
&coldkey,
4008+
MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP,
4009+
);
4010+
assert_ok!(SubtensorModule::do_schedule_coldkey_swap(
4011+
&coldkey.clone(),
4012+
&new_coldkey,
4013+
work1.to_fixed_bytes().to_vec(),
4014+
current_block,
4015+
nonce1
4016+
));
4017+
4018+
4019+
});
4020+
}

pallets/subtensor/tests/swap.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,20 +1367,12 @@ fn test_coldkey_has_associated_hotkeys() {
13671367
// Setup initial state
13681368
add_network(netuid, 13, 0);
13691369
register_ok_neuron(netuid, hotkey, coldkey, 0);
1370+
SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1000);
13701371

1371-
// Check if coldkey has associated hotkeys
1372-
assert!(SubtensorModule::coldkey_has_associated_hotkeys(&coldkey));
1373-
1374-
// Check for a coldkey without associated hotkeys
1375-
let unassociated_coldkey = U256::from(3);
1376-
assert!(!SubtensorModule::coldkey_has_associated_hotkeys(
1377-
&unassociated_coldkey
1378-
));
13791372
});
13801373
}
13811374

13821375

1383-
13841376
// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test swap -- test_coldkey_swap_total --exact --nocapture
13851377
#[test]
13861378
fn test_coldkey_swap_total() {

0 commit comments

Comments
 (0)