Skip to content

Commit e47fa2d

Browse files
committed
Merge remote-tracking branch 'origin/devnet-ready' into fix/recycle-burn-ext
2 parents 34c60ef + 340060c commit e47fa2d

File tree

8 files changed

+111
-19
lines changed

8 files changed

+111
-19
lines changed

pallets/commitments/src/lib.rs

+5-1
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

+45-3
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/events.rs

+2-2
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/stake_utils.rs

+8-4
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

+1-2
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

+1-1
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;

pallets/subtensor/src/tests/registration.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(clippy::unwrap_used)]
22

3+
use approx::assert_abs_diff_eq;
34
use frame_support::traits::Currency;
45

56
use super::mock::*;
@@ -535,11 +536,11 @@ fn test_burn_adjustment() {
535536
new_test_ext(1).execute_with(|| {
536537
let netuid: u16 = 1;
537538
let tempo: u16 = 13;
538-
let burn_cost: u64 = 1000;
539+
let init_burn_cost: u64 = InitialMinBurn::get() + 10_000;
539540
let adjustment_interval = 1;
540541
let target_registrations_per_interval = 1;
541542
add_network(netuid, tempo, 0);
542-
SubtensorModule::set_burn(netuid, burn_cost);
543+
SubtensorModule::set_burn(netuid, init_burn_cost);
543544
SubtensorModule::set_adjustment_interval(netuid, adjustment_interval);
544545
SubtensorModule::set_adjustment_alpha(netuid, 58000); // Set to old value.
545546
SubtensorModule::set_target_registrations_per_interval(
@@ -550,7 +551,7 @@ fn test_burn_adjustment() {
550551
// Register key 1.
551552
let hotkey_account_id_1 = U256::from(1);
552553
let coldkey_account_id_1 = U256::from(1);
553-
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id_1, 10000);
554+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id_1, init_burn_cost);
554555
assert_ok!(SubtensorModule::burned_register(
555556
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id_1),
556557
netuid,
@@ -560,7 +561,7 @@ fn test_burn_adjustment() {
560561
// Register key 2.
561562
let hotkey_account_id_2 = U256::from(2);
562563
let coldkey_account_id_2 = U256::from(2);
563-
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id_2, 10000);
564+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id_2, init_burn_cost);
564565
assert_ok!(SubtensorModule::burned_register(
565566
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id_2),
566567
netuid,
@@ -571,8 +572,13 @@ fn test_burn_adjustment() {
571572
// Step the block and trigger the adjustment.
572573
step_block(1);
573574

574-
// Check the adjusted burn.
575-
assert_eq!(SubtensorModule::get_burn_as_u64(netuid), 1500);
575+
// Check the adjusted burn is above the initial min burn.
576+
assert!(SubtensorModule::get_burn_as_u64(netuid) > init_burn_cost);
577+
assert_abs_diff_eq!(
578+
SubtensorModule::get_burn_as_u64(netuid),
579+
init_burn_cost.saturating_mul(3).saturating_div(2), // 1.5x
580+
epsilon = 1000
581+
);
576582
});
577583
}
578584

pallets/subtensor/src/tests/subnet.rs

+37
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,40 @@ fn test_do_start_call_ok_with_same_block_number_after_coinbase() {
222222
}
223223
});
224224
}
225+
226+
#[test]
227+
fn test_register_network_min_burn_at_default() {
228+
new_test_ext(1).execute_with(|| {
229+
let sn_owner_coldkey = U256::from(0);
230+
let sn_owner_hotkey = U256::from(1);
231+
let cost = SubtensorModule::get_network_lock_cost();
232+
233+
// Give coldkey enough for lock
234+
SubtensorModule::add_balance_to_coldkey_account(&sn_owner_coldkey, cost + 10_000_000_000);
235+
236+
// Register network
237+
assert_ok!(SubtensorModule::register_network(
238+
<<Test as Config>::RuntimeOrigin>::signed(sn_owner_coldkey),
239+
sn_owner_hotkey
240+
));
241+
// Get last events
242+
let events = System::events();
243+
let min_burn_event = events
244+
.iter()
245+
.filter(|event| {
246+
matches!(
247+
event.event,
248+
RuntimeEvent::SubtensorModule(Event::<Test>::NetworkAdded(..))
249+
)
250+
})
251+
.last();
252+
253+
let netuid = match min_burn_event.map(|event| event.event.clone()) {
254+
Some(RuntimeEvent::SubtensorModule(Event::<Test>::NetworkAdded(netuid, _))) => netuid,
255+
_ => panic!("Expected NetworkAdded event"),
256+
};
257+
258+
// Check min burn is set to default
259+
assert_eq!(MinBurn::<Test>::get(netuid), InitialMinBurn::get());
260+
});
261+
}

0 commit comments

Comments
 (0)