Skip to content

Commit fd1dbe4

Browse files
committed
Add increase_take and decrease_take extrinsics, rate limiting, and tests
1 parent fe5c89f commit fd1dbe4

File tree

9 files changed

+679
-20
lines changed

9 files changed

+679
-20
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,21 @@ pub mod pallet {
785785
}
786786
Ok(())
787787
}
788+
789+
#[pallet::call_index(45)]
790+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
791+
pub fn sudo_set_tx_delegate_take_rate_limit(
792+
origin: OriginFor<T>,
793+
tx_rate_limit: u64,
794+
) -> DispatchResult {
795+
ensure_root(origin)?;
796+
T::Subtensor::set_tx_delegate_take_rate_limit(tx_rate_limit);
797+
log::info!(
798+
"TxRateLimitDelegateTakeSet( tx_delegate_take_rate_limit: {:?} ) ",
799+
tx_rate_limit
800+
);
801+
Ok(())
802+
}
788803
}
789804
}
790805

@@ -808,6 +823,7 @@ impl<A, M> AuraInterface<A, M> for () {
808823
pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
809824
fn set_default_take(default_take: u16);
810825
fn set_tx_rate_limit(rate_limit: u64);
826+
fn set_tx_delegate_take_rate_limit(rate_limit: u64);
811827

812828
fn set_serving_rate_limit(netuid: u16, rate_limit: u64);
813829

pallets/admin-utils/tests/mock.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ parameter_types! {
7979
pub const InitialWeightsVersionKey: u16 = 0;
8080
pub const InitialServingRateLimit: u64 = 0; // No limit.
8181
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing
82+
pub const InitialTxDelegateTakeRateLimit: u64 = 0; // Disable rate limit for testing
8283
pub const InitialBurn: u64 = 0;
8384
pub const InitialMinBurn: u64 = 0;
8485
pub const InitialMaxBurn: u64 = 1_000_000_000;
@@ -143,6 +144,7 @@ impl pallet_subtensor::Config for Test {
143144
type InitialMinDifficulty = InitialMinDifficulty;
144145
type InitialServingRateLimit = InitialServingRateLimit;
145146
type InitialTxRateLimit = InitialTxRateLimit;
147+
type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit;
146148
type InitialBurn = InitialBurn;
147149
type InitialMaxBurn = InitialMaxBurn;
148150
type InitialMinBurn = InitialMinBurn;
@@ -211,6 +213,10 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
211213
SubtensorModule::set_tx_rate_limit(rate_limit);
212214
}
213215

216+
fn set_tx_delegate_take_rate_limit(rate_limit: u64) {
217+
SubtensorModule::set_tx_delegate_take_rate_limit(rate_limit);
218+
}
219+
214220
fn set_serving_rate_limit(netuid: u16, rate_limit: u64) {
215221
SubtensorModule::set_serving_rate_limit(netuid, rate_limit);
216222
}

pallets/admin-utils/tests/tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,3 +1060,24 @@ mod sudo_set_nominator_min_required_stake {
10601060
});
10611061
}
10621062
}
1063+
1064+
#[test]
1065+
fn test_sudo_set_tx_delegate_take_rate_limit() {
1066+
new_test_ext().execute_with(|| {
1067+
let to_be_set: u64 = 10;
1068+
let init_value: u64 = SubtensorModule::get_tx_delegate_take_rate_limit();
1069+
assert_eq!(
1070+
AdminUtils::sudo_set_tx_delegate_take_rate_limit(
1071+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1072+
to_be_set
1073+
),
1074+
Err(DispatchError::BadOrigin.into())
1075+
);
1076+
assert_eq!(SubtensorModule::get_tx_delegate_take_rate_limit(), init_value);
1077+
assert_ok!(AdminUtils::sudo_set_tx_delegate_take_rate_limit(
1078+
<<Test as Config>::RuntimeOrigin>::root(),
1079+
to_be_set
1080+
));
1081+
assert_eq!(SubtensorModule::get_tx_delegate_take_rate_limit(), to_be_set);
1082+
});
1083+
}

pallets/subtensor/src/lib.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use codec::{Decode, Encode};
1919
use frame_support::sp_runtime::transaction_validity::InvalidTransaction;
2020
use frame_support::sp_runtime::transaction_validity::ValidTransaction;
2121
use scale_info::TypeInfo;
22+
use sp_core::Get;
2223
use sp_runtime::{
2324
traits::{DispatchInfoOf, Dispatchable, PostDispatchInfoOf, SignedExtension},
2425
transaction_validity::{TransactionValidity, TransactionValidityError},
@@ -165,6 +166,8 @@ pub mod pallet {
165166
type InitialServingRateLimit: Get<u64>;
166167
#[pallet::constant] // Initial transaction rate limit.
167168
type InitialTxRateLimit: Get<u64>;
169+
#[pallet::constant] // Initial delegate take transaction rate limit.
170+
type InitialTxDelegateTakeRateLimit: Get<u64>;
168171
#[pallet::constant] // Initial percentage of total stake required to join senate.
169172
type InitialSenateRequiredStakePercentage: Get<u64>;
170173
#[pallet::constant] // Initial adjustment alpha on burn and pow.
@@ -582,15 +585,24 @@ pub mod pallet {
582585
T::InitialTxRateLimit::get()
583586
}
584587
#[pallet::type_value]
588+
pub fn DefaultTxDelegateTakeRateLimit<T: Config>() -> u64 {
589+
T::InitialTxDelegateTakeRateLimit::get()
590+
}
591+
#[pallet::type_value]
585592
pub fn DefaultLastTxBlock<T: Config>() -> u64 {
586593
0
587594
}
588595

589596
#[pallet::storage] // --- ITEM ( tx_rate_limit )
590597
pub(super) type TxRateLimit<T> = StorageValue<_, u64, ValueQuery, DefaultTxRateLimit<T>>;
598+
#[pallet::storage] // --- ITEM ( tx_rate_limit )
599+
pub(super) type TxDelegateTakeRateLimit<T> = StorageValue<_, u64, ValueQuery, DefaultTxDelegateTakeRateLimit<T>>;
591600
#[pallet::storage] // --- MAP ( key ) --> last_block
592601
pub(super) type LastTxBlock<T: Config> =
593602
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
603+
#[pallet::storage] // --- MAP ( key ) --> last_block
604+
pub(super) type LastTxBlockDelegateTake<T: Config> =
605+
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
594606

595607
#[pallet::type_value]
596608
pub fn DefaultServingRateLimit<T: Config>() -> u64 {
@@ -899,6 +911,7 @@ pub mod pallet {
899911
MaxBurnSet(u16, u64), // --- Event created when setting max burn on a network.
900912
MinBurnSet(u16, u64), // --- Event created when setting min burn on a network.
901913
TxRateLimitSet(u64), // --- Event created when setting the transaction rate limit.
914+
TxDelegateTakeRateLimitSet(u64), // --- Event created when setting the delegate take transaction rate limit.
902915
Sudid(DispatchResult), // --- Event created when a sudo call is done.
903916
RegistrationAllowed(u16, bool), // --- Event created when registration is allowed/disallowed for a subnet.
904917
PowRegistrationAllowed(u16, bool), // --- Event created when POW registration is allowed/disallowed for a subnet.
@@ -914,6 +927,8 @@ pub mod pallet {
914927
NetworkMinLockCostSet(u64), // Event created when the network minimum locking cost is set.
915928
SubnetLimitSet(u16), // Event created when the maximum number of subnets is set
916929
NetworkLockCostReductionIntervalSet(u64), // Event created when the lock cost reduction is set
930+
TakeDecreased( T::AccountId, T::AccountId, u16 ), // Event created when the take for a delegate is decreased.
931+
TakeIncreased( T::AccountId, T::AccountId, u16 ), // Event created when the take for a delegate is increased.
917932
HotkeySwapped {
918933
coldkey: T::AccountId,
919934
old_hotkey: T::AccountId,
@@ -986,6 +1001,7 @@ pub mod pallet {
9861001
NoNeuronIdAvailable, // -- Thrown when no neuron id is available
9871002
/// Thrown a stake would be below the minimum threshold for nominator validations
9881003
NomStakeBelowMinimumThreshold,
1004+
InvalidTake, // --- Thrown when delegate take is being set out of bounds
9891005
}
9901006

9911007
// ==================
@@ -1325,6 +1341,89 @@ pub mod pallet {
13251341
Self::do_become_delegate(origin, hotkey, Self::get_default_take())
13261342
}
13271343

1344+
// --- Allows delegates to decrease its take value.
1345+
//
1346+
// # Args:
1347+
// * 'origin': (<T as frame_system::Config>::Origin):
1348+
// - The signature of the caller's coldkey.
1349+
//
1350+
// * 'hotkey' (T::AccountId):
1351+
// - The hotkey we are delegating (must be owned by the coldkey.)
1352+
//
1353+
// * 'netuid' (u16):
1354+
// - Subnet ID to decrease take for
1355+
//
1356+
// * 'take' (u16):
1357+
// - The new stake proportion that this hotkey takes from delegations.
1358+
// The new value can be between 0 and 11_796 and should be strictly
1359+
// lower than the previous value. It T is the new value (rational number),
1360+
// the the parameter is calculated as [65535 * T]. For example, 1% would be
1361+
// [0.01 * 65535] = [655.35] = 655
1362+
//
1363+
// # Event:
1364+
// * TakeDecreased;
1365+
// - On successfully setting a decreased take for this hotkey.
1366+
//
1367+
// # Raises:
1368+
// * 'NotRegistered':
1369+
// - The hotkey we are delegating is not registered on the network.
1370+
//
1371+
// * 'NonAssociatedColdKey':
1372+
// - The hotkey we are delegating is not owned by the calling coldkey.
1373+
//
1374+
// * 'InvalidTransaction':
1375+
// - The delegate is setting a take which is not lower than the previous.
1376+
//
1377+
#[pallet::call_index(65)]
1378+
#[pallet::weight((0, DispatchClass::Normal, Pays::No))]
1379+
pub fn decrease_take(
1380+
origin: OriginFor<T>,
1381+
hotkey: T::AccountId,
1382+
take: u16,
1383+
) -> DispatchResult {
1384+
Self::do_decrease_take(origin, hotkey, take)
1385+
}
1386+
1387+
// --- Allows delegates to increase its take value. This call is rate-limited.
1388+
//
1389+
// # Args:
1390+
// * 'origin': (<T as frame_system::Config>::Origin):
1391+
// - The signature of the caller's coldkey.
1392+
//
1393+
// * 'hotkey' (T::AccountId):
1394+
// - The hotkey we are delegating (must be owned by the coldkey.)
1395+
//
1396+
// * 'take' (u16):
1397+
// - The new stake proportion that this hotkey takes from delegations.
1398+
// The new value can be between 0 and 11_796 and should be strictly
1399+
// greater than the previous value. It T is the new value (rational number),
1400+
// the the parameter is calculated as [65535 * T]. For example, 1% would be
1401+
// [0.01 * 65535] = [655.35] = 655
1402+
//
1403+
// # Event:
1404+
// * TakeDecreased;
1405+
// - On successfully setting a decreased take for this hotkey.
1406+
//
1407+
// # Raises:
1408+
// * 'NotRegistered':
1409+
// - The hotkey we are delegating is not registered on the network.
1410+
//
1411+
// * 'NonAssociatedColdKey':
1412+
// - The hotkey we are delegating is not owned by the calling coldkey.
1413+
//
1414+
// * 'InvalidTransaction':
1415+
// - The delegate is setting a take which is not lower than the previous.
1416+
//
1417+
#[pallet::call_index(66)]
1418+
#[pallet::weight((0, DispatchClass::Normal, Pays::No))]
1419+
pub fn increase_take(
1420+
origin: OriginFor<T>,
1421+
hotkey: T::AccountId,
1422+
take: u16,
1423+
) -> DispatchResult {
1424+
Self::do_increase_take(origin, hotkey, take)
1425+
}
1426+
13281427
// --- Adds stake to a hotkey. The call is made from the
13291428
// coldkey account linked in the hotkey.
13301429
// Only the associated coldkey is allowed to make staking and

0 commit comments

Comments
 (0)