Skip to content

Commit be2e7a7

Browse files
committed
Merge branch 'devnet-ready' of https://github.com/opentensor/subtensor into devnet-ready
2 parents 3c45a89 + 55410a3 commit be2e7a7

File tree

22 files changed

+1257
-350
lines changed

22 files changed

+1257
-350
lines changed

pallets/admin-utils/src/tests/mod.rs

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -931,136 +931,6 @@ mod sudo_set_nominator_min_required_stake {
931931
);
932932
});
933933
}
934-
935-
#[test]
936-
fn clears_staker_nominations_below_min() {
937-
new_test_ext().execute_with(|| {
938-
System::set_block_number(1);
939-
940-
// Create accounts.
941-
let netuid = 1;
942-
let hot1 = U256::from(1);
943-
let hot2 = U256::from(2);
944-
let cold1 = U256::from(3);
945-
let cold2 = U256::from(4);
946-
947-
// SubtensorModule::set_target_stakes_per_interval(10);
948-
// Register network.
949-
add_network(netuid, 0);
950-
951-
// Register hot1.
952-
register_ok_neuron(netuid, hot1, cold1, 0);
953-
assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot1), cold1);
954-
955-
// Register hot2.
956-
register_ok_neuron(netuid, hot2, cold2, 0);
957-
assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot2), cold2);
958-
959-
// Add stake cold1 --> hot1 (non delegation.)
960-
SubtensorModule::add_balance_to_coldkey_account(&cold1, 5);
961-
assert_ok!(SubtensorModule::add_stake(
962-
<<Test as Config>::RuntimeOrigin>::signed(cold1),
963-
hot1,
964-
netuid,
965-
1
966-
));
967-
assert_eq!(
968-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold1, netuid),
969-
1
970-
);
971-
assert_eq!(Balances::free_balance(cold1), 4);
972-
973-
// Add stake cold2 --> hot1 (is delegation.)
974-
SubtensorModule::add_balance_to_coldkey_account(&cold2, 5);
975-
assert_ok!(SubtensorModule::add_stake(
976-
<<Test as Config>::RuntimeOrigin>::signed(cold2),
977-
hot1,
978-
netuid,
979-
1
980-
));
981-
assert_eq!(
982-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold2, netuid),
983-
1
984-
);
985-
assert_eq!(Balances::free_balance(cold2), 4);
986-
987-
// Add stake cold1 --> hot2
988-
SubtensorModule::add_balance_to_coldkey_account(&cold1, 5);
989-
assert_ok!(SubtensorModule::add_stake(
990-
<<Test as Config>::RuntimeOrigin>::signed(cold1),
991-
hot2,
992-
netuid,
993-
1
994-
));
995-
assert_eq!(
996-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold1, netuid),
997-
1
998-
);
999-
assert_eq!(Balances::free_balance(cold1), 8);
1000-
1001-
// Add stake cold2 --> hot2
1002-
SubtensorModule::add_balance_to_coldkey_account(&cold2, 5);
1003-
assert_ok!(SubtensorModule::add_stake(
1004-
<<Test as Config>::RuntimeOrigin>::signed(cold2),
1005-
hot2,
1006-
netuid,
1007-
1
1008-
));
1009-
assert_eq!(
1010-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold2, netuid),
1011-
1
1012-
);
1013-
assert_eq!(Balances::free_balance(cold2), 8);
1014-
1015-
// Set min stake to 0 (noop)
1016-
assert_ok!(AdminUtils::sudo_set_nominator_min_required_stake(
1017-
<<Test as Config>::RuntimeOrigin>::root(),
1018-
0u64
1019-
));
1020-
assert_eq!(
1021-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold1, netuid),
1022-
1
1023-
);
1024-
assert_eq!(
1025-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold1, netuid),
1026-
1
1027-
);
1028-
assert_eq!(
1029-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold2, netuid),
1030-
1
1031-
);
1032-
assert_eq!(
1033-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold2, netuid),
1034-
1
1035-
);
1036-
1037-
// Set min nomination to 10: should clear (cold2, hot1) and (cold1, hot2).
1038-
assert_ok!(AdminUtils::sudo_set_nominator_min_required_stake(
1039-
<<Test as Config>::RuntimeOrigin>::root(),
1040-
10u64
1041-
));
1042-
assert_eq!(
1043-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold1, netuid),
1044-
1
1045-
);
1046-
assert_eq!(
1047-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold1, netuid),
1048-
0
1049-
);
1050-
assert_eq!(
1051-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold2, netuid),
1052-
0
1053-
);
1054-
assert_eq!(
1055-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot2, &cold2, netuid),
1056-
1
1057-
);
1058-
1059-
// Balances have been added back into accounts.
1060-
assert_eq!(Balances::free_balance(cold1), 9);
1061-
assert_eq!(Balances::free_balance(cold2), 9);
1062-
});
1063-
}
1064934
}
1065935

1066936
#[test]

pallets/subtensor/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,10 @@ pub mod pallet {
696696
}
697697

698698
#[pallet::type_value]
699-
/// Default minimum stake for setting childkeys.
700-
pub fn DefaultChildkeysMinStake<T: Config>() -> u64 {
701-
1_000_000_000_000
699+
/// Default minimum stake.
700+
/// 2M rao matches $1 at $500/TAO
701+
pub fn DefaultMinStake<T: Config>() -> u64 {
702+
2_000_000
702703
}
703704

704705
#[pallet::type_value]

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,5 +1555,46 @@ mod dispatches {
15551555
pub fn unstake_all_alpha(origin: OriginFor<T>, hotkey: T::AccountId) -> DispatchResult {
15561556
Self::do_unstake_all_alpha(origin, hotkey)
15571557
}
1558+
1559+
/// ---- The implementation for the extrinsic move_stake: Moves specified amount of stake from a hotkey to another across subnets.
1560+
///
1561+
/// # Args:
1562+
/// * `origin` - (<T as frame_system::Config>::Origin):
1563+
/// - The signature of the caller's coldkey.
1564+
///
1565+
/// * `origin_hotkey` (T::AccountId):
1566+
/// - The hotkey account to move stake from.
1567+
///
1568+
/// * `destination_hotkey` (T::AccountId):
1569+
/// - The hotkey account to move stake to.
1570+
///
1571+
/// * `origin_netuid` (T::AccountId):
1572+
/// - The subnet ID to move stake from.
1573+
///
1574+
/// * `destination_netuid` (T::AccountId):
1575+
/// - The subnet ID to move stake to.
1576+
///
1577+
/// * `alpha_amount` (T::AccountId):
1578+
/// - The alpha stake amount to move.
1579+
///
1580+
#[pallet::call_index(85)]
1581+
#[pallet::weight((Weight::from_parts(3_000_000, 0).saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Operational, Pays::No))]
1582+
pub fn move_stake(
1583+
origin: T::RuntimeOrigin,
1584+
origin_hotkey: T::AccountId,
1585+
destination_hotkey: T::AccountId,
1586+
origin_netuid: u16,
1587+
destination_netuid: u16,
1588+
alpha_amount: u64,
1589+
) -> DispatchResult {
1590+
Self::do_move_stake(
1591+
origin,
1592+
origin_hotkey,
1593+
destination_hotkey,
1594+
origin_netuid,
1595+
destination_netuid,
1596+
alpha_amount,
1597+
)
1598+
}
15581599
}
15591600
}

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/macros/events.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ mod events {
1717
StakeAdded(T::AccountId, T::AccountId, u64, u64, u16),
1818
/// stake has been removed from the hotkey staking account onto the coldkey account.
1919
StakeRemoved(T::AccountId, T::AccountId, u64, u64, u16),
20+
/// stake has been moved from origin (hotkey, subnet ID) to destination (hotkey, subnet ID) of this amount (in TAO).
21+
StakeMoved(T::AccountId, T::AccountId, u16, T::AccountId, u16, u64),
2022
/// a caller successfully sets their weights on a subnetwork.
2123
WeightsSet(u16, u16),
2224
/// a new neuron account has been registered to the chain.

pallets/subtensor/src/rpc_info/stake_info.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ impl<T: Config> Pallet<T> {
3535
let alpha: u64 = Self::get_stake_for_hotkey_and_coldkey_on_subnet(
3636
hotkey_i, coldkey_i, *netuid_i,
3737
);
38+
if alpha == 0 {
39+
continue;
40+
}
3841
let emission: u64 = AlphaDividendsPerSubnet::<T>::get(*netuid_i, &hotkey_i);
3942
let is_registered: bool =
4043
Self::is_hotkey_registered_on_network(*netuid_i, hotkey_i);

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_be_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/staking/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod add_stake;
33
pub mod decrease_take;
44
pub mod helpers;
55
pub mod increase_take;
6+
pub mod move_stake;
67
pub mod remove_stake;
78
pub mod set_children;
89
pub mod stake_utils;

pallets/subtensor/src/staking/move_stake.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
// use substrate_fixed::types::I96F32;
2+
use sp_core::Get;
33

44
impl<T: Config> Pallet<T> {
55
/// Moves stake from one hotkey to another across subnets.
@@ -57,7 +57,11 @@ impl<T: Config> Pallet<T> {
5757
);
5858

5959
// --- 6. Get the current alpha stake for the origin hotkey-coldkey pair in the origin subnet
60-
let origin_alpha = Self::get_stake_for_hotkey_and_coldkey_on_subnet( &origin_hotkey, &coldkey, origin_netuid );
60+
let origin_alpha = Self::get_stake_for_hotkey_and_coldkey_on_subnet(
61+
&origin_hotkey,
62+
&coldkey,
63+
origin_netuid,
64+
);
6165
ensure!(
6266
alpha_amount <= origin_alpha,
6367
Error::<T>::NotEnoughStakeToWithdraw
@@ -71,6 +75,12 @@ impl<T: Config> Pallet<T> {
7175
alpha_amount,
7276
);
7377

78+
// Ensure origin_tao is at least DefaultMinStake
79+
ensure!(
80+
origin_tao >= DefaultMinStake::<T>::get(),
81+
Error::<T>::AmountTooLow
82+
);
83+
7484
// --- 8. Stake the resulting TAO into the destination subnet for the destination hotkey
7585
Self::stake_into_subnet(
7686
&destination_hotkey.clone(),
@@ -94,6 +104,7 @@ impl<T: Config> Pallet<T> {
94104
origin_netuid,
95105
destination_hotkey,
96106
destination_netuid,
107+
origin_tao,
97108
));
98109

99110
// -- 10. Ok and return.

pallets/subtensor/src/tests/children.rs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,28 +3058,34 @@ fn test_childkey_multiple_parents_emission() {
30583058

30593059
// Register neurons and add initial stakes
30603060
let initial_stakes: Vec<(bool, U256, U256, u64)> = vec![
3061-
(false, coldkey_parent1, parent1, 200_000),
3062-
(true, coldkey_parent2, parent2, 150_000),
3063-
(true, coldkey_child, child, 20_000),
3064-
(true, coldkey_weight_setter, weight_setter, 100_000),
3061+
(false, coldkey_parent1, parent1, 200_000_000),
3062+
(true, coldkey_parent2, parent2, 150_000_000),
3063+
(true, coldkey_child, child, 20_000_000),
3064+
(true, coldkey_weight_setter, weight_setter, 100_000_000),
30653065
];
30663066

3067-
for (register, coldkey, hotkey, stake) in initial_stakes.iter() {
3068-
SubtensorModule::add_balance_to_coldkey_account(coldkey, *stake);
3069-
if *register {
3070-
// Register a neuron
3071-
register_ok_neuron(netuid, *hotkey, *coldkey, 0);
3072-
} else {
3073-
// Just create hotkey account
3074-
SubtensorModule::create_account_if_non_existent(coldkey, hotkey);
3075-
}
3076-
assert_ok!(SubtensorModule::add_stake(
3077-
RuntimeOrigin::signed(*coldkey),
3078-
*hotkey,
3079-
netuid,
3080-
*stake
3081-
));
3082-
}
3067+
let initial_actual_stakes: Vec<u64> = initial_stakes
3068+
.iter()
3069+
.map(|(register, coldkey, hotkey, stake)| {
3070+
SubtensorModule::add_balance_to_coldkey_account(coldkey, *stake);
3071+
if *register {
3072+
// Register a neuron
3073+
register_ok_neuron(netuid, *hotkey, *coldkey, 0);
3074+
} else {
3075+
// Just create hotkey account
3076+
SubtensorModule::create_account_if_non_existent(coldkey, hotkey);
3077+
}
3078+
assert_ok!(SubtensorModule::add_stake(
3079+
RuntimeOrigin::signed(*coldkey),
3080+
*hotkey,
3081+
netuid,
3082+
*stake
3083+
));
3084+
3085+
// Return actual stake
3086+
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid)
3087+
})
3088+
.collect();
30833089

30843090
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
30853091
step_block(2);
@@ -3152,23 +3158,26 @@ fn test_childkey_multiple_parents_emission() {
31523158
);
31533159

31543160
assert!(
3155-
parent1_stake > 200_000,
3161+
parent1_stake > initial_actual_stakes[0],
31563162
"Parent1 should have received emission"
31573163
);
31583164
assert!(
3159-
parent2_stake > 150_000,
3165+
parent2_stake > initial_actual_stakes[1],
31603166
"Parent2 should have received emission"
31613167
);
3162-
assert!(child_stake > 20_000, "Child should have received emission");
31633168
assert!(
3164-
weight_setter_stake > 100_000,
3169+
child_stake > initial_actual_stakes[2],
3170+
"Child should have received emission"
3171+
);
3172+
assert!(
3173+
weight_setter_stake > initial_actual_stakes[3],
31653174
"Weight setter should have received emission"
31663175
);
31673176

31683177
// Check individual stake increases
3169-
let parent1_stake_increase = parent1_stake - 200_000;
3170-
let parent2_stake_increase = parent2_stake - 150_000;
3171-
let child_stake_increase = child_stake - 20_000;
3178+
let parent1_stake_increase = parent1_stake - initial_actual_stakes[0];
3179+
let parent2_stake_increase = parent2_stake - initial_actual_stakes[1];
3180+
let child_stake_increase = child_stake - initial_actual_stakes[2];
31723181

31733182
log::debug!(
31743183
"Stake increases - Parent1: {}, Parent2: {}, Child: {}",
@@ -3192,12 +3201,13 @@ fn test_childkey_multiple_parents_emission() {
31923201
);
31933202

31943203
// Check that the total stake has increased by the emission amount
3204+
// Allow 1% slippage
31953205
let total_stake = parent1_stake + parent2_stake + child_stake + weight_setter_stake;
3196-
let initial_total_stake: u64 = initial_stakes.iter().map(|(_, _, _, stake)| stake).sum();
3206+
let initial_total_stake: u64 = initial_actual_stakes.iter().sum::<u64>();
31973207
assert_abs_diff_eq!(
31983208
total_stake,
3199-
initial_total_stake + total_emission - 2,
3200-
epsilon = total_emission / 10_000
3209+
initial_total_stake + total_emission,
3210+
epsilon = total_emission / 100
32013211
);
32023212
});
32033213
}

0 commit comments

Comments
 (0)