Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pallets/commitments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub mod pallet {
CommitmentSetRateLimitExceeded,
/// Space Limit Exceeded for the current interval
SpaceLimitExceeded,
/// Indicates that unreserve returned a leftover, which is unexpected.
UnexpectedUnreserveLeftover,
}

#[pallet::type_value]
Expand Down Expand Up @@ -266,7 +268,9 @@ pub mod pallet {
if old_deposit > id.deposit {
let err_amount =
T::Currency::unreserve(&who, old_deposit.saturating_sub(id.deposit));
debug_assert!(err_amount.is_zero());
if !err_amount.is_zero() {
return Err(Error::<T>::UnexpectedUnreserveLeftover.into());
}
}

<CommitmentOf<T>>::insert(netuid, &who, id);
Expand Down
48 changes: 45 additions & 3 deletions pallets/commitments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ use sp_std::prelude::*;
#[cfg(test)]
use crate::{
CommitmentInfo, CommitmentOf, Config, Data, Error, Event, MaxSpace, Pallet, RateLimit,
RevealedCommitments, TimelockedIndex,
Registration, RevealedCommitments, TimelockedIndex,
mock::{
DRAND_QUICKNET_SIG_HEX, RuntimeEvent, RuntimeOrigin, Test, insert_drand_pulse,
Balances, DRAND_QUICKNET_SIG_HEX, RuntimeEvent, RuntimeOrigin, Test, insert_drand_pulse,
new_test_ext, produce_ciphertext,
},
};
use frame_support::pallet_prelude::Hooks;
use frame_support::{BoundedVec, assert_noop, assert_ok, traits::Get};
use frame_support::{
BoundedVec, assert_noop, assert_ok,
traits::{Currency, Get, ReservableCurrency},
};
use frame_system::Pallet as System;

#[allow(clippy::indexing_slicing)]
Expand Down Expand Up @@ -1220,3 +1223,42 @@ fn on_initialize_reveals_matured_timelocks() {
}
});
}

#[test]
fn set_commitment_unreserve_leftover_fails() {
new_test_ext().execute_with(|| {
use frame_system::RawOrigin;

let netuid = 999;
let who = 99;

Balances::make_free_balance_be(&who, 10_000);

let fake_deposit = 100;
let dummy_info = CommitmentInfo {
fields: BoundedVec::try_from(vec![]).expect("empty fields is fine"),
};
let registration = Registration {
deposit: fake_deposit,
info: dummy_info,
block: 0u64.into(),
};

CommitmentOf::<Test>::insert(netuid, who, registration);

assert_ok!(Balances::reserve(&who, fake_deposit));
assert_eq!(Balances::reserved_balance(who), 100);

Balances::unreserve(&who, 10_000);
assert_eq!(Balances::reserved_balance(who), 0);

let commit_small = Box::new(CommitmentInfo {
fields: BoundedVec::try_from(vec![]).expect("no fields is fine"),
});

assert_noop!(
Pallet::<Test>::set_commitment(RawOrigin::Signed(who).into(), netuid, commit_small),
Error::<Test>::UnexpectedUnreserveLeftover
);
});
}
4 changes: 2 additions & 2 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ mod events {
/// a network is removed.
NetworkRemoved(u16),
/// stake has been transferred from the a coldkey account onto the hotkey staking account.
StakeAdded(T::AccountId, T::AccountId, u64, u64, u16),
StakeAdded(T::AccountId, T::AccountId, u64, u64, u16, u64),
/// stake has been removed from the hotkey staking account onto the coldkey account.
StakeRemoved(T::AccountId, T::AccountId, u64, u64, u16),
StakeRemoved(T::AccountId, T::AccountId, u64, u64, u16, u64),
/// stake has been moved from origin (hotkey, subnet ID) to destination (hotkey, subnet ID) of this amount (in TAO).
StakeMoved(T::AccountId, T::AccountId, u16, T::AccountId, u16, u64),
/// a caller successfully sets their weights on a subnetwork.
Expand Down
12 changes: 8 additions & 4 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,14 +794,16 @@ impl<T: Config> Pallet<T> {
tao_unstaked,
actual_alpha_decrease,
netuid,
actual_fee,
));
log::debug!(
"StakeRemoved( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?} )",
"StakeRemoved( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?}, fee: {:?} )",
coldkey.clone(),
hotkey.clone(),
tao_unstaked,
actual_alpha_decrease,
netuid
netuid,
actual_fee
);

// Step 6: Return the amount of TAO unstaked.
Expand Down Expand Up @@ -857,14 +859,16 @@ impl<T: Config> Pallet<T> {
tao_staked,
actual_alpha,
netuid,
actual_fee,
));
log::debug!(
"StakeAdded( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?} )",
"StakeAdded( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?}, fee: {:?} )",
coldkey.clone(),
hotkey.clone(),
tao_staked,
actual_alpha,
netuid
netuid,
actual_fee
);

// Step 7: Return the amount of alpha staked
Expand Down
3 changes: 1 addition & 2 deletions pallets/subtensor/src/subnets/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<T: Config> Pallet<T> {
netuid_to_register,
mechid
);
Self::deposit_event(Event::NetworkAdded(netuid_to_register, 0));
Self::deposit_event(Event::NetworkAdded(netuid_to_register, mechid));

// --- 17. Return success.
Ok(())
Expand Down Expand Up @@ -272,7 +272,6 @@ impl<T: Config> Pallet<T> {
Self::set_target_registrations_per_interval(netuid, 1);
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
Self::set_immunity_period(netuid, 5000);
Self::set_min_burn(netuid, 1);
Self::set_min_difficulty(netuid, u64::MAX);
Self::set_max_difficulty(netuid, u64::MAX);

Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ parameter_types! {
pub const InitialTxDelegateTakeRateLimit: u64 = 1; // 1 block take rate limit for testing
pub const InitialTxChildKeyTakeRateLimit: u64 = 1; // 1 block take rate limit for testing
pub const InitialBurn: u64 = 0;
pub const InitialMinBurn: u64 = 0;
pub const InitialMinBurn: u64 = 500_000;
pub const InitialMaxBurn: u64 = 1_000_000_000;
pub const InitialValidatorPruneLen: u64 = 0;
pub const InitialScalingLawPower: u16 = 50;
Expand Down
18 changes: 12 additions & 6 deletions pallets/subtensor/src/tests/registration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::unwrap_used)]

use approx::assert_abs_diff_eq;
use frame_support::traits::Currency;

use super::mock::*;
Expand Down Expand Up @@ -535,11 +536,11 @@ fn test_burn_adjustment() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = 1;
let tempo: u16 = 13;
let burn_cost: u64 = 1000;
let init_burn_cost: u64 = InitialMinBurn::get() + 10_000;
let adjustment_interval = 1;
let target_registrations_per_interval = 1;
add_network(netuid, tempo, 0);
SubtensorModule::set_burn(netuid, burn_cost);
SubtensorModule::set_burn(netuid, init_burn_cost);
SubtensorModule::set_adjustment_interval(netuid, adjustment_interval);
SubtensorModule::set_adjustment_alpha(netuid, 58000); // Set to old value.
SubtensorModule::set_target_registrations_per_interval(
Expand All @@ -550,7 +551,7 @@ fn test_burn_adjustment() {
// Register key 1.
let hotkey_account_id_1 = U256::from(1);
let coldkey_account_id_1 = U256::from(1);
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id_1, 10000);
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id_1, init_burn_cost);
assert_ok!(SubtensorModule::burned_register(
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id_1),
netuid,
Expand All @@ -560,7 +561,7 @@ fn test_burn_adjustment() {
// Register key 2.
let hotkey_account_id_2 = U256::from(2);
let coldkey_account_id_2 = U256::from(2);
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id_2, 10000);
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id_2, init_burn_cost);
assert_ok!(SubtensorModule::burned_register(
<<Test as Config>::RuntimeOrigin>::signed(hotkey_account_id_2),
netuid,
Expand All @@ -571,8 +572,13 @@ fn test_burn_adjustment() {
// Step the block and trigger the adjustment.
step_block(1);

// Check the adjusted burn.
assert_eq!(SubtensorModule::get_burn_as_u64(netuid), 1500);
// Check the adjusted burn is above the initial min burn.
assert!(SubtensorModule::get_burn_as_u64(netuid) > init_burn_cost);
assert_abs_diff_eq!(
SubtensorModule::get_burn_as_u64(netuid),
init_burn_cost.saturating_mul(3).saturating_div(2), // 1.5x
epsilon = 1000
);
});
}

Expand Down
37 changes: 37 additions & 0 deletions pallets/subtensor/src/tests/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,40 @@ fn test_do_start_call_ok_with_same_block_number_after_coinbase() {
}
});
}

#[test]
fn test_register_network_min_burn_at_default() {
new_test_ext(1).execute_with(|| {
let sn_owner_coldkey = U256::from(0);
let sn_owner_hotkey = U256::from(1);
let cost = SubtensorModule::get_network_lock_cost();

// Give coldkey enough for lock
SubtensorModule::add_balance_to_coldkey_account(&sn_owner_coldkey, cost + 10_000_000_000);

// Register network
assert_ok!(SubtensorModule::register_network(
<<Test as Config>::RuntimeOrigin>::signed(sn_owner_coldkey),
sn_owner_hotkey
));
// Get last events
let events = System::events();
let min_burn_event = events
.iter()
.filter(|event| {
matches!(
event.event,
RuntimeEvent::SubtensorModule(Event::<Test>::NetworkAdded(..))
)
})
.last();

let netuid = match min_burn_event.map(|event| event.event.clone()) {
Some(RuntimeEvent::SubtensorModule(Event::<Test>::NetworkAdded(netuid, _))) => netuid,
_ => panic!("Expected NetworkAdded event"),
};

// Check min burn is set to default
assert_eq!(MinBurn::<Test>::get(netuid), InitialMinBurn::get());
});
}
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 255,
spec_version: 256,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading