Skip to content

Commit adb344c

Browse files
committed
Add minimum stake to add_stake
1 parent 752abc2 commit adb344c

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,12 @@ pub mod pallet {
701701
1_000_000_000_000
702702
}
703703

704+
#[pallet::type_value]
705+
/// Default minimum stake.
706+
pub fn DefaultMinStake<T: Config>() -> u64 {
707+
1_000
708+
}
709+
704710
#[pallet::type_value]
705711
/// Default unicode vector for tau symbol.
706712
pub fn DefaultUnicodeVecU8<T: Config>() -> Vec<u8> {

pallets/subtensor/src/macros/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,7 @@ mod errors {
183183
InputLengthsUnequal,
184184
/// A transactor exceeded the rate limit for setting weights.
185185
CommittingWeightsTooFast,
186+
/// Stake amount is too low.
187+
AmountTooLow,
186188
}
187189
}

pallets/subtensor/src/staking/add_stake.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use sp_core::Get;
23

34
impl<T: Config> Pallet<T> {
45
/// ---- The implementation for the extrinsic add_stake: Adds stake to a hotkey account.
@@ -61,6 +62,12 @@ impl<T: Config> Pallet<T> {
6162
Error::<T>::HotKeyAccountNotExists
6263
);
6364

65+
// Ensure stake_to_ve_added is at least DefaultMinStake
66+
ensure!(
67+
stake_to_be_added >= DefaultMinStake::<T>::get(),
68+
Error::<T>::AmountTooLow
69+
);
70+
6471
// 5. Ensure the remove operation from the coldkey is a success.
6572
let tao_staked: u64 =
6673
Self::remove_balance_from_coldkey_account(&coldkey, stake_to_be_added)?;

pallets/subtensor/src/tests/staking.rs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,60 +1114,82 @@ fn test_clear_small_nominations() {
11141114
assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot2), cold2);
11151115

11161116
// Add stake cold1 --> hot1 (non delegation.)
1117-
SubtensorModule::add_balance_to_coldkey_account(&cold1, 5);
1117+
SubtensorModule::add_balance_to_coldkey_account(&cold1, 1_000);
11181118
assert_ok!(SubtensorModule::add_stake(
11191119
RuntimeOrigin::signed(cold1),
11201120
hot1,
11211121
netuid,
1122-
1
1122+
1_000
1123+
));
1124+
assert_ok!(SubtensorModule::remove_stake(
1125+
RuntimeOrigin::signed(cold1),
1126+
hot1,
1127+
netuid,
1128+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold1, netuid) - 1
11231129
));
11241130
assert_eq!(
11251131
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold1, netuid),
11261132
1
11271133
);
1128-
assert_eq!(Balances::free_balance(cold1), 4);
11291134

11301135
// Add stake cold2 --> hot1 (is delegation.)
1131-
SubtensorModule::add_balance_to_coldkey_account(&cold2, 5);
1136+
SubtensorModule::add_balance_to_coldkey_account(&cold2, 1_000);
11321137
assert_ok!(SubtensorModule::add_stake(
11331138
RuntimeOrigin::signed(cold2),
11341139
hot1,
11351140
netuid,
1136-
1
1141+
1_000
1142+
));
1143+
assert_ok!(SubtensorModule::remove_stake(
1144+
RuntimeOrigin::signed(cold2),
1145+
hot1,
1146+
netuid,
1147+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold2, netuid) - 1
11371148
));
11381149
assert_eq!(
11391150
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold2, netuid),
11401151
1
11411152
);
1142-
assert_eq!(Balances::free_balance(cold2), 4);
11431153

11441154
// Add stake cold1 --> hot2 (non delegation.)
1145-
SubtensorModule::add_balance_to_coldkey_account(&cold1, 5);
1155+
SubtensorModule::add_balance_to_coldkey_account(&cold1, 1_000);
11461156
assert_ok!(SubtensorModule::add_stake(
11471157
RuntimeOrigin::signed(cold1),
11481158
hot2,
11491159
netuid,
1150-
1
1160+
1_000
1161+
));
1162+
assert_ok!(SubtensorModule::remove_stake(
1163+
RuntimeOrigin::signed(cold1),
1164+
hot2,
1165+
netuid,
1166+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold1, netuid) - 1
11511167
));
11521168
assert_eq!(
11531169
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold1, netuid),
11541170
1
11551171
);
1156-
assert_eq!(Balances::free_balance(cold1), 8);
1172+
let balance1_before_cleaning = Balances::free_balance(cold1);
11571173

11581174
// Add stake cold2 --> hot2 (is delegation.)
1159-
SubtensorModule::add_balance_to_coldkey_account(&cold2, 5);
1175+
SubtensorModule::add_balance_to_coldkey_account(&cold2, 1_000);
11601176
assert_ok!(SubtensorModule::add_stake(
11611177
RuntimeOrigin::signed(cold2),
11621178
hot2,
11631179
netuid,
1164-
1
1180+
1_000
1181+
));
1182+
assert_ok!(SubtensorModule::remove_stake(
1183+
RuntimeOrigin::signed(cold2),
1184+
hot2,
1185+
netuid,
1186+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold2, netuid) - 1
11651187
));
11661188
assert_eq!(
11671189
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold2, netuid),
11681190
1
11691191
);
1170-
assert_eq!(Balances::free_balance(cold2), 8);
1192+
let balance2_before_cleaning = Balances::free_balance(cold2);
11711193

11721194
// Run clear all small nominations when min stake is zero (noop)
11731195
SubtensorModule::set_nominator_min_required_stake(0);
@@ -1218,8 +1240,10 @@ fn test_clear_small_nominations() {
12181240
);
12191241

12201242
// Balances have been added back into accounts.
1221-
assert_eq!(Balances::free_balance(cold1), 9);
1222-
assert_eq!(Balances::free_balance(cold2), 9);
1243+
let balance1_after_cleaning = Balances::free_balance(cold1);
1244+
let balance2_after_cleaning = Balances::free_balance(cold2);
1245+
assert_eq!(balance1_before_cleaning + 1, balance1_after_cleaning);
1246+
assert_eq!(balance2_before_cleaning + 1, balance2_after_cleaning);
12231247

12241248
assert_eq!(
12251249
TotalHotkeyAlpha::<Test>::get(hot2, netuid),
@@ -1644,7 +1668,7 @@ fn test_get_total_delegated_stake_single_delegator() {
16441668
let delegate_coldkey = U256::from(1);
16451669
let delegate_hotkey = U256::from(2);
16461670
let delegator = U256::from(3);
1647-
let stake_amount = 999;
1671+
let stake_amount = 1_999;
16481672
let existential_deposit = ExistentialDeposit::get();
16491673
let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
16501674

@@ -1753,8 +1777,8 @@ fn test_get_total_delegated_stake_exclude_owner_stake() {
17531777
let delegate_coldkey = U256::from(1);
17541778
let delegate_hotkey = U256::from(2);
17551779
let delegator = U256::from(3);
1756-
let owner_stake = 1000;
1757-
let delegator_stake = 999;
1780+
let owner_stake = 1_000_000;
1781+
let delegator_stake = 999_999;
17581782

17591783
let netuid = add_dynamic_network(&delegate_hotkey, &delegate_coldkey);
17601784

@@ -1792,10 +1816,10 @@ fn test_get_total_delegated_stake_exclude_owner_stake() {
17921816
let actual_delegated_stake =
17931817
SubtensorModule::get_total_stake_for_coldkey(&delegate_coldkey);
17941818

1795-
assert_eq!(
1796-
actual_delegated_stake, expected_delegated_stake,
1797-
"Delegated stake should exclude owner's stake. Expected: {}, Actual: {}",
1798-
expected_delegated_stake, actual_delegated_stake
1819+
assert_abs_diff_eq!(
1820+
actual_delegated_stake,
1821+
expected_delegated_stake,
1822+
epsilon = 100
17991823
);
18001824
});
18011825
}

0 commit comments

Comments
 (0)