Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/subtensor/src/staking/decrease_take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ impl<T: Config> Pallet<T> {
// --- 4. Set the new take value.
Delegates::<T>::insert(hotkey.clone(), take);

// --- 5. Emit the take value.
// --- 5. Set last block for rate limiting
let block: u64 = Self::get_current_block_as_u64();
Self::set_last_tx_block_delegate_take(&hotkey, block);

// --- 6. Emit the take value.
log::debug!(
"TakeDecreased( coldkey:{:?}, hotkey:{:?}, take:{:?} )",
coldkey,
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/staking/increase_take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ impl<T: Config> Pallet<T> {
let block: u64 = Self::get_current_block_as_u64();
ensure!(
!Self::exceeds_tx_delegate_take_rate_limit(
Self::get_last_tx_block_delegate_take(&coldkey),
Self::get_last_tx_block_delegate_take(&hotkey),
block
),
Error::<T>::DelegateTxRateLimitExceeded
);

// Set last block for rate limiting
Self::set_last_tx_block_delegate_take(&coldkey, block);
Self::set_last_tx_block_delegate_take(&hotkey, block);

// --- 6. Set the new take value.
Delegates::<T>::insert(hotkey.clone(), take);
Expand Down
64 changes: 64 additions & 0 deletions pallets/subtensor/src/tests/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,70 @@ fn test_rate_limits_enforced_on_increase_take() {
});
}

// Test rate-limiting on an increase take just after a decrease take
// Prevents a Validator from decreasing take and then increasing it immediately after.
#[test]
fn test_rate_limits_enforced_on_decrease_before_increase_take() {
new_test_ext(1).execute_with(|| {
// Make account
let hotkey0 = U256::from(1);
let coldkey0 = U256::from(3);

// Add balance
SubtensorModule::add_balance_to_coldkey_account(&coldkey0, 100000);

// Register the neuron to a new network
let netuid = 1;
add_network(netuid, 1, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

// Coldkey / hotkey 0 become delegates with 9% take
Delegates::<Test>::insert(hotkey0, SubtensorModule::get_min_delegate_take() + 1);
assert_eq!(
SubtensorModule::get_hotkey_take(&hotkey0),
SubtensorModule::get_min_delegate_take() + 1
);

// Decrease take
assert_ok!(SubtensorModule::do_decrease_take(
RuntimeOrigin::signed(coldkey0),
hotkey0,
SubtensorModule::get_min_delegate_take()
)); // Verify decrease
assert_eq!(
SubtensorModule::get_hotkey_take(&hotkey0),
SubtensorModule::get_min_delegate_take()
);

// Increase take immediately after
assert_eq!(
SubtensorModule::do_increase_take(
RuntimeOrigin::signed(coldkey0),
hotkey0,
SubtensorModule::get_min_delegate_take() + 1
),
Err(Error::<Test>::DelegateTxRateLimitExceeded.into())
); // Verify no change
assert_eq!(
SubtensorModule::get_hotkey_take(&hotkey0),
SubtensorModule::get_min_delegate_take()
);

step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16);

// Can increase after waiting
assert_ok!(SubtensorModule::do_increase_take(
RuntimeOrigin::signed(coldkey0),
hotkey0,
SubtensorModule::get_min_delegate_take() + 1
)); // Verify increase
assert_eq!(
SubtensorModule::get_hotkey_take(&hotkey0),
SubtensorModule::get_min_delegate_take() + 1
);
});
}

#[test]
fn test_get_total_delegated_stake_after_unstaking() {
new_test_ext(1).execute_with(|| {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,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: 243,
spec_version: 244,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading