Skip to content

Commit c8539f1

Browse files
committed
update version
2 parents 62f8189 + 0a92a5e commit c8539f1

File tree

12 files changed

+492
-53
lines changed

12 files changed

+492
-53
lines changed

pallets/commitments/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ pub mod pallet {
120120
CommitmentSetRateLimitExceeded,
121121
/// Space Limit Exceeded for the current interval
122122
SpaceLimitExceeded,
123+
/// Indicates that unreserve returned a leftover, which is unexpected.
124+
UnexpectedUnreserveLeftover,
123125
}
124126

125127
#[pallet::type_value]
@@ -266,7 +268,9 @@ pub mod pallet {
266268
if old_deposit > id.deposit {
267269
let err_amount =
268270
T::Currency::unreserve(&who, old_deposit.saturating_sub(id.deposit));
269-
debug_assert!(err_amount.is_zero());
271+
if !err_amount.is_zero() {
272+
return Err(Error::<T>::UnexpectedUnreserveLeftover.into());
273+
}
270274
}
271275

272276
<CommitmentOf<T>>::insert(netuid, &who, id);

pallets/commitments/src/tests.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ use sp_std::prelude::*;
44
#[cfg(test)]
55
use crate::{
66
CommitmentInfo, CommitmentOf, Config, Data, Error, Event, MaxSpace, Pallet, RateLimit,
7-
RevealedCommitments, TimelockedIndex,
7+
Registration, RevealedCommitments, TimelockedIndex,
88
mock::{
9-
DRAND_QUICKNET_SIG_HEX, RuntimeEvent, RuntimeOrigin, Test, insert_drand_pulse,
9+
Balances, DRAND_QUICKNET_SIG_HEX, RuntimeEvent, RuntimeOrigin, Test, insert_drand_pulse,
1010
new_test_ext, produce_ciphertext,
1111
},
1212
};
1313
use frame_support::pallet_prelude::Hooks;
14-
use frame_support::{BoundedVec, assert_noop, assert_ok, traits::Get};
14+
use frame_support::{
15+
BoundedVec, assert_noop, assert_ok,
16+
traits::{Currency, Get, ReservableCurrency},
17+
};
1518
use frame_system::Pallet as System;
1619

1720
#[allow(clippy::indexing_slicing)]
@@ -1220,3 +1223,42 @@ fn on_initialize_reveals_matured_timelocks() {
12201223
}
12211224
});
12221225
}
1226+
1227+
#[test]
1228+
fn set_commitment_unreserve_leftover_fails() {
1229+
new_test_ext().execute_with(|| {
1230+
use frame_system::RawOrigin;
1231+
1232+
let netuid = 999;
1233+
let who = 99;
1234+
1235+
Balances::make_free_balance_be(&who, 10_000);
1236+
1237+
let fake_deposit = 100;
1238+
let dummy_info = CommitmentInfo {
1239+
fields: BoundedVec::try_from(vec![]).expect("empty fields is fine"),
1240+
};
1241+
let registration = Registration {
1242+
deposit: fake_deposit,
1243+
info: dummy_info,
1244+
block: 0u64.into(),
1245+
};
1246+
1247+
CommitmentOf::<Test>::insert(netuid, who, registration);
1248+
1249+
assert_ok!(Balances::reserve(&who, fake_deposit));
1250+
assert_eq!(Balances::reserved_balance(who), 100);
1251+
1252+
Balances::unreserve(&who, 10_000);
1253+
assert_eq!(Balances::reserved_balance(who), 0);
1254+
1255+
let commit_small = Box::new(CommitmentInfo {
1256+
fields: BoundedVec::try_from(vec![]).expect("no fields is fine"),
1257+
});
1258+
1259+
assert_noop!(
1260+
Pallet::<Test>::set_commitment(RawOrigin::Signed(who).into(), netuid, commit_small),
1261+
Error::<Test>::UnexpectedUnreserveLeftover
1262+
);
1263+
});
1264+
}

pallets/subtensor/src/macros/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,7 @@ mod errors {
201201
NeedWaitingMoreBlocksToStarCall,
202202
/// Not enough AlphaOut on the subnet to recycle
203203
NotEnoughAlphaOutToRecycle,
204+
/// Cannot burn or recycle TAO from root subnet
205+
CannotBurnOrRecycleOnRootSubnet,
204206
}
205207
}

pallets/subtensor/src/macros/events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ mod events {
1414
/// a network is removed.
1515
NetworkRemoved(u16),
1616
/// stake has been transferred from the a coldkey account onto the hotkey staking account.
17-
StakeAdded(T::AccountId, T::AccountId, u64, u64, u16),
17+
StakeAdded(T::AccountId, T::AccountId, u64, u64, u16, u64),
1818
/// stake has been removed from the hotkey staking account onto the coldkey account.
19-
StakeRemoved(T::AccountId, T::AccountId, u64, u64, u16),
19+
StakeRemoved(T::AccountId, T::AccountId, u64, u64, u16, u64),
2020
/// stake has been moved from origin (hotkey, subnet ID) to destination (hotkey, subnet ID) of this amount (in TAO).
2121
StakeMoved(T::AccountId, T::AccountId, u16, T::AccountId, u16, u64),
2222
/// a caller successfully sets their weights on a subnetwork.

pallets/subtensor/src/staking/recycle_alpha.rs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,27 @@ impl<T: Config> Pallet<T> {
2020
amount: u64,
2121
netuid: u16,
2222
) -> DispatchResult {
23-
let coldkey = ensure_signed(origin)?;
23+
let coldkey: T::AccountId = ensure_signed(origin)?;
2424

2525
ensure!(
2626
Self::if_subnet_exist(netuid),
2727
Error::<T>::SubNetworkDoesNotExist
2828
);
2929

3030
ensure!(
31-
Self::coldkey_owns_hotkey(&coldkey, &hotkey),
32-
Error::<T>::NonAssociatedColdKey
31+
netuid != Self::get_root_netuid(),
32+
Error::<T>::CannotBurnOrRecycleOnRootSubnet
3333
);
3434

35+
// Ensure that the hotkey account exists this is only possible through registration.
3536
ensure!(
36-
TotalHotkeyAlpha::<T>::get(&hotkey, netuid) >= amount,
37+
Self::hotkey_account_exists(&hotkey),
38+
Error::<T>::HotKeyAccountNotExists
39+
);
40+
41+
// Ensure that the hotkey has enough stake to withdraw.
42+
ensure!(
43+
Self::has_enough_stake_on_subnet(&hotkey, &coldkey, netuid, amount),
3744
Error::<T>::NotEnoughStakeToWithdraw
3845
);
3946

@@ -42,19 +49,22 @@ impl<T: Config> Pallet<T> {
4249
Error::<T>::InsufficientLiquidity
4350
);
4451

45-
if TotalHotkeyAlpha::<T>::mutate(&hotkey, netuid, |v| {
46-
*v = v.saturating_sub(amount);
47-
*v
48-
}) == 0
49-
{
50-
TotalHotkeyAlpha::<T>::remove(&hotkey, netuid);
51-
}
52+
// Deduct from the coldkey's stake.
53+
let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
54+
&hotkey, &coldkey, netuid, amount,
55+
);
5256

57+
// Recycle means we should decrease the alpha issuance tracker.
5358
SubnetAlphaOut::<T>::mutate(netuid, |total| {
54-
*total = total.saturating_sub(amount);
59+
*total = total.saturating_sub(actual_alpha_decrease);
5560
});
5661

57-
Self::deposit_event(Event::AlphaRecycled(coldkey, hotkey, amount, netuid));
62+
Self::deposit_event(Event::AlphaRecycled(
63+
coldkey,
64+
hotkey,
65+
actual_alpha_decrease,
66+
netuid,
67+
));
5868

5969
Ok(())
6070
}
@@ -85,12 +95,19 @@ impl<T: Config> Pallet<T> {
8595
);
8696

8797
ensure!(
88-
Self::coldkey_owns_hotkey(&coldkey, &hotkey),
89-
Error::<T>::NonAssociatedColdKey
98+
netuid != Self::get_root_netuid(),
99+
Error::<T>::CannotBurnOrRecycleOnRootSubnet
90100
);
91101

102+
// Ensure that the hotkey account exists this is only possible through registration.
92103
ensure!(
93-
TotalHotkeyAlpha::<T>::get(&hotkey, netuid) >= amount,
104+
Self::hotkey_account_exists(&hotkey),
105+
Error::<T>::HotKeyAccountNotExists
106+
);
107+
108+
// Ensure that the hotkey has enough stake to withdraw.
109+
ensure!(
110+
Self::has_enough_stake_on_subnet(&hotkey, &coldkey, netuid, amount),
94111
Error::<T>::NotEnoughStakeToWithdraw
95112
);
96113

@@ -99,16 +116,20 @@ impl<T: Config> Pallet<T> {
99116
Error::<T>::InsufficientLiquidity
100117
);
101118

102-
if TotalHotkeyAlpha::<T>::mutate(&hotkey, netuid, |v| {
103-
*v = v.saturating_sub(amount);
104-
*v
105-
}) == 0
106-
{
107-
TotalHotkeyAlpha::<T>::remove(&hotkey, netuid);
108-
}
119+
// Deduct from the coldkey's stake.
120+
let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
121+
&hotkey, &coldkey, netuid, amount,
122+
);
123+
124+
// This is a burn, so we don't need to update AlphaOut.
109125

110126
// Deposit event
111-
Self::deposit_event(Event::AlphaBurned(coldkey, hotkey, amount, netuid));
127+
Self::deposit_event(Event::AlphaBurned(
128+
coldkey,
129+
hotkey,
130+
actual_alpha_decrease,
131+
netuid,
132+
));
112133

113134
Ok(())
114135
}

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -794,14 +794,16 @@ impl<T: Config> Pallet<T> {
794794
tao_unstaked,
795795
actual_alpha_decrease,
796796
netuid,
797+
actual_fee,
797798
));
798799
log::debug!(
799-
"StakeRemoved( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?} )",
800+
"StakeRemoved( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?}, fee: {:?} )",
800801
coldkey.clone(),
801802
hotkey.clone(),
802803
tao_unstaked,
803804
actual_alpha_decrease,
804-
netuid
805+
netuid,
806+
actual_fee
805807
);
806808

807809
// Step 6: Return the amount of TAO unstaked.
@@ -857,14 +859,16 @@ impl<T: Config> Pallet<T> {
857859
tao_staked,
858860
actual_alpha,
859861
netuid,
862+
actual_fee,
860863
));
861864
log::debug!(
862-
"StakeAdded( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?} )",
865+
"StakeAdded( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?}, fee: {:?} )",
863866
coldkey.clone(),
864867
hotkey.clone(),
865868
tao_staked,
866869
actual_alpha,
867-
netuid
870+
netuid,
871+
actual_fee
868872
);
869873

870874
// Step 7: Return the amount of alpha staked

pallets/subtensor/src/subnets/subnet.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl<T: Config> Pallet<T> {
239239
netuid_to_register,
240240
mechid
241241
);
242-
Self::deposit_event(Event::NetworkAdded(netuid_to_register, 0));
242+
Self::deposit_event(Event::NetworkAdded(netuid_to_register, mechid));
243243

244244
// --- 17. Return success.
245245
Ok(())
@@ -272,7 +272,6 @@ impl<T: Config> Pallet<T> {
272272
Self::set_target_registrations_per_interval(netuid, 1);
273273
Self::set_adjustment_alpha(netuid, 17_893_341_751_498_265_066); // 18_446_744_073_709_551_615 * 0.97 = 17_893_341_751_498_265_066
274274
Self::set_immunity_period(netuid, 5000);
275-
Self::set_min_burn(netuid, 1);
276275
Self::set_min_difficulty(netuid, u64::MAX);
277276
Self::set_max_difficulty(netuid, u64::MAX);
278277

pallets/subtensor/src/tests/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ parameter_types! {
152152
pub const InitialTxDelegateTakeRateLimit: u64 = 1; // 1 block take rate limit for testing
153153
pub const InitialTxChildKeyTakeRateLimit: u64 = 1; // 1 block take rate limit for testing
154154
pub const InitialBurn: u64 = 0;
155-
pub const InitialMinBurn: u64 = 0;
155+
pub const InitialMinBurn: u64 = 500_000;
156156
pub const InitialMaxBurn: u64 = 1_000_000_000;
157157
pub const InitialValidatorPruneLen: u64 = 0;
158158
pub const InitialScalingLawPower: u16 = 50;

0 commit comments

Comments
 (0)