Skip to content
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

feat: min take 9% #451

Merged
merged 9 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ parameter_types! {
pub const InitialStakePruningMin: u16 = 0;
pub const InitialFoundationDistribution: u64 = 0;
pub const InitialDefaultTake: u16 = 11_796; // 18% honest number.
pub const InitialMinTake: u16 = 0;
pub const InitialMinTake: u16 = 5_898; // 9%;
pub const InitialWeightsVersionKey: u16 = 0;
pub const InitialServingRateLimit: u64 = 0; // No limit.
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing
Expand Down
4 changes: 2 additions & 2 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ mod sudo_set_nominator_min_required_stake {
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(cold1),
hot1,
0
u16::MAX / 10
));
assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot1), cold1);

Expand All @@ -954,7 +954,7 @@ mod sudo_set_nominator_min_required_stake {
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(cold2),
hot2,
0
u16::MAX / 10
));
assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot2), cold2);

Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ parameter_types! {
pub const InitialStakePruningMin: u16 = 0;
pub const InitialFoundationDistribution: u64 = 0;
pub const InitialDefaultTake: u16 = 11_796; // 18%, same as in production
pub const InitialMinTake: u16 = 0;
pub const InitialMinTake: u16 =5_898; // 9%;
pub const InitialWeightsVersionKey: u16 = 0;
pub const InitialServingRateLimit: u64 = 0; // No limit.
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing
Expand Down
135 changes: 70 additions & 65 deletions pallets/subtensor/tests/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1423,12 +1423,12 @@ fn test_full_with_delegating() {
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
10
u16::MAX / 10
));
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey1),
hotkey1,
10
u16::MAX / 10
distributedstatemachine marked this conversation as resolved.
Show resolved Hide resolved
));
assert!(SubtensorModule::hotkey_is_delegate(&hotkey0));
assert!(SubtensorModule::hotkey_is_delegate(&hotkey1));
Expand All @@ -1438,15 +1438,15 @@ fn test_full_with_delegating() {
SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
1000
u16::MAX / 10
),
Err(Error::<Test>::AlreadyDelegate.into())
);
assert_eq!(
SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey1),
hotkey1,
1000
u16::MAX / 10
),
Err(Error::<Test>::AlreadyDelegate.into())
);
Expand Down Expand Up @@ -1505,21 +1505,21 @@ fn test_full_with_delegating() {
SubtensorModule::emit_inflation_through_hotkey_account(&hotkey1, 0, 1000);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey0, &hotkey0),
601
); // 200 + 1000 x ( 200 / 500 ) = 200 + 400 = 600 ~= 601
660
); // 200 + 1000 x ( 200 / 500 )+ 60 = 200 + 400 ~= 660
distributedstatemachine marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey0, &hotkey1),
700
); // 200 + 1000 x ( 200 / 400 ) = 200 + 500 = 700
650
); // 200 + 1000 x ( 200 / 400 ) - 50 = 200 + 500 = 700
distributedstatemachine marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey1, &hotkey0),
899
); // 300 + 1000 x ( 300 / 500 ) = 300 + 600 = 900 ~= 899
840
); // 300 + 1000 x ( 300 / 500 ) - 60= 300 + 600 = 840
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey1, &hotkey1),
700
); // 200 + 1000 x ( 200 / 400 ) = 300 + 600 = 700
assert_eq!(SubtensorModule::get_total_stake(), 2900); // 600 + 700 + 900 + 700 = 2900
750
); // 200 + 1000 x ( 200 / 400 ) + 50 = 300 + 600 + 50 = 750
assert_eq!(SubtensorModule::get_total_stake(), 2900); // 600 + 700 + 900 + 750 = 2900

// // Try unstaking too much.
assert_eq!(
Expand Down Expand Up @@ -1580,19 +1580,19 @@ fn test_full_with_delegating() {
// All the amounts have been decreased.
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey0, &hotkey0),
501
560
);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey0, &hotkey1),
600
550
);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey1, &hotkey0),
799
740
);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey1, &hotkey1),
600
650
);

// Lets register and stake a new key.
Expand Down Expand Up @@ -1876,12 +1876,12 @@ fn test_full_with_delegating_some_servers() {
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
10
u16::MAX / 10
));
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey1),
hotkey1,
10
u16::MAX / 10
));
assert!(SubtensorModule::hotkey_is_delegate(&hotkey0));
assert!(SubtensorModule::hotkey_is_delegate(&hotkey1));
Expand Down Expand Up @@ -1939,22 +1939,22 @@ fn test_full_with_delegating_some_servers() {
SubtensorModule::emit_inflation_through_hotkey_account(&hotkey1, 123, 2_000); // 2_123 total emission.
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey0, &hotkey0),
801
); // 200 + (200 + 1000 x ( 200 / 500 )) = 200 + (200 + 400) = 800 ~= 801
860
); // 200 + (200 + 1000 x ( 200 / 500 )) + 60 = 200 + (200 + 400) + 60 = 860
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey1, &hotkey0),
899
); // 300 + 1000 x ( 300 / 500 ) = 300 + 600 = 900 ~= 899
840
); // 300 + 1000 x ( 300 / 500 ) - 50 = 300 + 600 - 50 = 840
assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&hotkey0), 1_700); // initial + server emission + validator emission = 799 + 899 = 1_698

assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey0, &hotkey1),
1_200
); // 200 + (0 + 2000 x ( 200 / 400 )) = 200 + (1000) = 1_200
1_100
); // 200 + (0 + 2000 x ( 200 / 400 )) - 100 = 200 + (1000) - 100= 1_100
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey1, &hotkey1),
1_323
); // 200 + (123 + 2000 x ( 200 / 400 )) = 200 + (1_200) = 1_323
1_423
); // 200 + (123 + 2000 x ( 200 / 400 )) + 100 = 200 + (1_200)+ 100 = 1_423
assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&hotkey1), 2_523); // 400 + 2_123
assert_eq!(SubtensorModule::get_total_stake(), 4_223); // 1_700 + 2_523 = 4_223

Expand All @@ -1964,20 +1964,20 @@ fn test_full_with_delegating_some_servers() {
SubtensorModule::emit_inflation_through_hotkey_account(&hotkey1, 150, 0);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey0, &hotkey0),
1_151
); // + 350 = 1_151
1_210
); // + 350 + 60 = 1_210
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey0, &hotkey1),
1_200
1_100
); // No change.
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey1, &hotkey0),
899
840
); // No change.
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey1, &hotkey1),
1_473
); // 1_323 + 150 = 1_473
1_573
); // 1_323 + 150 + 100 = 1_573
assert_eq!(SubtensorModule::get_total_stake(), 4_723); // 4_223 + 500 = 4_823

// Lets register and stake a new key.
Expand Down Expand Up @@ -2204,12 +2204,12 @@ fn test_full_block_emission_occurs() {
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
10
u16::MAX / 10
));
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey1),
hotkey1,
10
u16::MAX / 10
));
assert!(SubtensorModule::hotkey_is_delegate(&hotkey0));
assert!(SubtensorModule::hotkey_is_delegate(&hotkey1));
Expand Down Expand Up @@ -2455,7 +2455,7 @@ fn test_clear_small_nominations() {
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(cold1),
hot1,
0
SubtensorModule::get_min_take()
));
assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot1), cold1);

Expand All @@ -2464,7 +2464,7 @@ fn test_clear_small_nominations() {
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(cold2),
hot2,
0
SubtensorModule::get_min_take()
));
assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot2), cold2);

Expand Down Expand Up @@ -2725,13 +2725,15 @@ fn test_delegate_take_can_be_decreased() {
));
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 10);

// Coldkey / hotkey 0 decreases take to 5%
assert_ok!(SubtensorModule::do_decrease_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
u16::MAX / 20
));
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 20);
// Coldkey / hotkey 0 decreases take to 5%. This should fail as the minimum take is 9%
assert_err!(
SubtensorModule::do_decrease_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
u16::MAX / 20
),
Error::<Test>::InvalidTake
);
});
}

Expand Down Expand Up @@ -2764,7 +2766,10 @@ fn test_can_set_min_take_ok() {
hotkey0,
SubtensorModule::get_min_take()
));
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), 0);
assert_eq!(
SubtensorModule::get_hotkey_take(&hotkey0),
SubtensorModule::get_min_take()
);
});
}

Expand All @@ -2784,24 +2789,24 @@ fn test_delegate_take_can_not_be_increased_with_decrease_take() {
add_network(netuid, 0, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

// Coldkey / hotkey 0 become delegates with 5% take
// Coldkey / hotkey 0 become delegates with 10% take
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
u16::MAX / 20
u16::MAX / 10
));
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 20);
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 10);

// Coldkey / hotkey 0 tries to increase take to 10%
// Coldkey / hotkey 0 tries to increase take to 12.5%
assert_eq!(
SubtensorModule::do_decrease_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
u16::MAX / 10
u16::MAX / 8
),
Err(Error::<Test>::InvalidTake.into())
);
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 20);
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 10);
});
}

Expand All @@ -2821,23 +2826,23 @@ fn test_delegate_take_can_be_increased() {
add_network(netuid, 0, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

// Coldkey / hotkey 0 become delegates with 5% take
// Coldkey / hotkey 0 become delegates with 10% take
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
u16::MAX / 20
u16::MAX / 10
));
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 20);
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 10);

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

// Coldkey / hotkey 0 decreases take to 10%
// Coldkey / hotkey 0 decreases take to 12.5%
assert_ok!(SubtensorModule::do_increase_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
u16::MAX / 10
u16::MAX / 8
));
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 10);
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 8);
});
}

Expand Down Expand Up @@ -3006,33 +3011,33 @@ fn test_rate_limits_enforced_on_increase_take() {
add_network(netuid, 0, 0);
register_ok_neuron(netuid, hotkey0, coldkey0, 124124);

// Coldkey / hotkey 0 become delegates with 5% take
// Coldkey / hotkey 0 become delegates with 10% take
assert_ok!(SubtensorModule::do_become_delegate(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
u16::MAX / 20
u16::MAX / 10
));
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 20);
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 10);

// Coldkey / hotkey 0 increases take to 10%
// Coldkey / hotkey 0 increases take to 12.5%
assert_eq!(
SubtensorModule::do_increase_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
u16::MAX / 10
u16::MAX / 8
),
Err(Error::<Test>::TxRateLimitExceeded.into())
);
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 20);
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 10);

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

// Can increase after waiting
assert_ok!(SubtensorModule::do_increase_take(
<<Test as Config>::RuntimeOrigin>::signed(coldkey0),
hotkey0,
u16::MAX / 10
u16::MAX / 8
));
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 10);
assert_eq!(SubtensorModule::get_hotkey_take(&hotkey0), u16::MAX / 8);
});
}
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ parameter_types! {
pub const SubtensorInitialPruningScore : u16 = u16::MAX;
pub const SubtensorInitialBondsMovingAverage: u64 = 900_000;
pub const SubtensorInitialDefaultTake: u16 = 11_796; // 18% honest number.
pub const SubtensorInitialMinTake: u16 = 11_796; // 18%, no change is allowed initially
pub const SubtensorInitialMinTake: u16 = 5_898; // 9%
pub const SubtensorInitialWeightsVersionKey: u64 = 0;
pub const SubtensorInitialMinDifficulty: u64 = 10_000_000;
pub const SubtensorInitialMaxDifficulty: u64 = u64::MAX / 4;
Expand Down
Loading