-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce rate limits in decrease take #626
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
852fa4c
Set take setting block in decrease take so that take cannot be increa…
gztensor 095b618
Enfornce rate limits on decrease_take
gztensor 907b40a
Correct the rate limit comment
gztensor f2b0f47
Update rate limit block after map insert
gztensor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2824,6 +2824,9 @@ fn test_can_set_min_take_ok() { | |
u16::MAX / 10 | ||
)); | ||
|
||
// Wait so that we can decrease take | ||
step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16); | ||
|
||
// Coldkey / hotkey 0 decreases take to min | ||
assert_ok!(SubtensorModule::do_decrease_take( | ||
<<Test as Config>::RuntimeOrigin>::signed(coldkey0), | ||
|
@@ -3136,6 +3139,132 @@ fn test_rate_limits_enforced_on_increase_take() { | |
}); | ||
} | ||
|
||
// Test rate-limiting on decrease_take + increase_take | ||
#[test] | ||
fn test_rate_limits_enforced_on_increase_take_after_decrease() { | ||
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, 0, 0); | ||
register_ok_neuron(netuid, hotkey0, coldkey0, 124124); | ||
|
||
// Coldkey / hotkey 0 become delegates with max take | ||
assert_ok!(SubtensorModule::do_become_delegate( | ||
<<Test as Config>::RuntimeOrigin>::signed(coldkey0), | ||
hotkey0, | ||
pallet_subtensor::MaxTake::<Test>::get() | ||
)); | ||
|
||
// Wait so that we can decrease take | ||
step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16); | ||
|
||
// Coldkey / hotkey 0 decreases take | ||
assert_ok!(SubtensorModule::do_decrease_take( | ||
<<Test as Config>::RuntimeOrigin>::signed(coldkey0), | ||
hotkey0, | ||
SubtensorModule::get_min_take() | ||
)); | ||
|
||
// Attempt to immediately increase fails due to rate limit | ||
assert_eq!( | ||
SubtensorModule::do_increase_take( | ||
<<Test as Config>::RuntimeOrigin>::signed(coldkey0), | ||
hotkey0, | ||
pallet_subtensor::MaxTake::<Test>::get() | ||
), | ||
Err(Error::<Test>::DelegateTxRateLimitExceeded.into()) | ||
); | ||
assert_eq!( | ||
SubtensorModule::get_hotkey_take(&hotkey0), | ||
SubtensorModule::get_min_take() | ||
); | ||
|
||
// After number of blocks, take can be increased | ||
step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16); | ||
|
||
// Can increase after waiting | ||
assert_ok!(SubtensorModule::do_increase_take( | ||
<<Test as Config>::RuntimeOrigin>::signed(coldkey0), | ||
hotkey0, | ||
pallet_subtensor::MaxTake::<Test>::get() | ||
)); | ||
assert_eq!( | ||
SubtensorModule::get_hotkey_take(&hotkey0), | ||
pallet_subtensor::MaxTake::<Test>::get() | ||
); | ||
}); | ||
} | ||
|
||
// Test rate-limiting on decrease_take | ||
#[test] | ||
fn test_rate_limits_enforced_on_decrease_take() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the expected behaviour? imo, one should be able to decrease without rate limit, but not increase until after the rate limit since the last change (increase or decrease) |
||
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, 0, 0); | ||
register_ok_neuron(netuid, hotkey0, coldkey0, 124124); | ||
|
||
// Coldkey / hotkey 0 become delegates with max take | ||
assert_ok!(SubtensorModule::do_become_delegate( | ||
<<Test as Config>::RuntimeOrigin>::signed(coldkey0), | ||
hotkey0, | ||
pallet_subtensor::MaxTake::<Test>::get() | ||
)); | ||
|
||
// Wait so that we can decrease take first time | ||
step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16); | ||
|
||
// Coldkey / hotkey 0 decreases take | ||
assert_ok!(SubtensorModule::do_decrease_take( | ||
<<Test as Config>::RuntimeOrigin>::signed(coldkey0), | ||
hotkey0, | ||
SubtensorModule::get_min_take() + 1 | ||
)); | ||
|
||
// Attempt to immediately decrease again fails due to rate limit | ||
assert_eq!( | ||
SubtensorModule::do_decrease_take( | ||
<<Test as Config>::RuntimeOrigin>::signed(coldkey0), | ||
hotkey0, | ||
SubtensorModule::get_min_take() | ||
), | ||
Err(Error::<Test>::DelegateTxRateLimitExceeded.into()) | ||
); | ||
assert_eq!( | ||
SubtensorModule::get_hotkey_take(&hotkey0), | ||
SubtensorModule::get_min_take() + 1 | ||
); | ||
|
||
// After number of blocks, take can be decreased | ||
step_block(1 + InitialTxDelegateTakeRateLimit::get() as u16); | ||
|
||
// Can increase after waiting | ||
assert_ok!(SubtensorModule::do_decrease_take( | ||
<<Test as Config>::RuntimeOrigin>::signed(coldkey0), | ||
hotkey0, | ||
SubtensorModule::get_min_take() | ||
)); | ||
assert_eq!( | ||
SubtensorModule::get_hotkey_take(&hotkey0), | ||
SubtensorModule::get_min_take() | ||
); | ||
}); | ||
} | ||
|
||
// Helper function to set up a test environment | ||
fn setup_test_environment() -> (AccountId, AccountId, AccountId) { | ||
let current_coldkey = U256::from(1); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.