Skip to content

Commit c00ff02

Browse files
authored
Merge pull request #380 from opentensor/feat/change-delegate-takes-dev
Add increase_take and decrease_take extrinsics, rate limiting, and tests
2 parents a322703 + 4e8d9ca commit c00ff02

File tree

11 files changed

+841
-62
lines changed

11 files changed

+841
-62
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub mod pallet {
8888
#[pallet::weight(T::WeightInfo::sudo_set_default_take())]
8989
pub fn sudo_set_default_take(origin: OriginFor<T>, default_take: u16) -> DispatchResult {
9090
ensure_root(origin)?;
91-
T::Subtensor::set_default_take(default_take);
91+
T::Subtensor::set_max_delegate_take(default_take);
9292
log::info!("DefaultTakeSet( default_take: {:?} ) ", default_take);
9393
Ok(())
9494
}
@@ -785,6 +785,30 @@ 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+
}
803+
804+
#[pallet::call_index(46)]
805+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
806+
pub fn sudo_set_min_delegate_take(origin: OriginFor<T>, take: u16) -> DispatchResult {
807+
ensure_root(origin)?;
808+
T::Subtensor::set_min_delegate_take(take);
809+
log::info!("TxMinDelegateTakeSet( tx_min_delegate_take: {:?} ) ", take);
810+
Ok(())
811+
}
788812
}
789813
}
790814

@@ -806,8 +830,10 @@ impl<A, M> AuraInterface<A, M> for () {
806830
///////////////////////////////////////////
807831

808832
pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
809-
fn set_default_take(default_take: u16);
833+
fn set_min_delegate_take(take: u16);
834+
fn set_max_delegate_take(take: u16);
810835
fn set_tx_rate_limit(rate_limit: u64);
836+
fn set_tx_delegate_take_rate_limit(rate_limit: u64);
811837

812838
fn set_serving_rate_limit(netuid: u16, rate_limit: u64);
813839

pallets/admin-utils/src/weights.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ use core::marker::PhantomData;
3434
/// Weight functions needed for `pallet_admin_utils`.
3535
pub trait WeightInfo {
3636
fn swap_authorities(a: u32, ) -> Weight;
37-
fn sudo_set_default_take() -> Weight;
37+
fn sudo_set_min_delegate_take() -> Weight;
38+
fn sudo_set_default_take() -> Weight;
3839
fn sudo_set_serving_rate_limit() -> Weight;
3940
fn sudo_set_max_difficulty() -> Weight;
4041
fn sudo_set_min_difficulty() -> Weight;
@@ -90,6 +91,16 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
9091
Weight::from_parts(27_199_000, 655)
9192
.saturating_add(T::DbWeight::get().writes(1_u64))
9293
}
94+
/// Storage: SubtensorModule DefaultTake (r:0 w:1)
95+
/// Proof Skipped: SubtensorModule DefaultTake (max_values: Some(1), max_size: None, mode: Measured)
96+
fn sudo_set_min_delegate_take() -> Weight {
97+
// Proof Size summary in bytes:
98+
// Measured: `655`
99+
// Estimated: `655`
100+
// Minimum execution time: 26_770_000 picoseconds.
101+
Weight::from_parts(27_199_000, 655)
102+
.saturating_add(T::DbWeight::get().writes(1_u64))
103+
}
93104
/// Storage: SubtensorModule ServingRateLimit (r:0 w:1)
94105
/// Proof Skipped: SubtensorModule ServingRateLimit (max_values: None, max_size: None, mode: Measured)
95106
fn sudo_set_serving_rate_limit() -> Weight {
@@ -430,6 +441,16 @@ impl WeightInfo for () {
430441
Weight::from_parts(27_199_000, 655)
431442
.saturating_add(RocksDbWeight::get().writes(1_u64))
432443
}
444+
/// Storage: SubtensorModule DefaultTake (r:0 w:1)
445+
/// Proof Skipped: SubtensorModule DefaultTake (max_values: Some(1), max_size: None, mode: Measured)
446+
fn sudo_set_min_delegate_take() -> Weight {
447+
// Proof Size summary in bytes:
448+
// Measured: `655`
449+
// Estimated: `655`
450+
// Minimum execution time: 26_770_000 picoseconds.
451+
Weight::from_parts(27_199_000, 655)
452+
.saturating_add(RocksDbWeight::get().writes(1_u64))
453+
}
433454
/// Storage: SubtensorModule ServingRateLimit (r:0 w:1)
434455
/// Proof Skipped: SubtensorModule ServingRateLimit (max_values: None, max_size: None, mode: Measured)
435456
fn sudo_set_serving_rate_limit() -> Weight {

pallets/admin-utils/tests/mock.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ parameter_types! {
7676
pub const InitialStakePruningMin: u16 = 0;
7777
pub const InitialFoundationDistribution: u64 = 0;
7878
pub const InitialDefaultTake: u16 = 11_796; // 18% honest number.
79+
pub const InitialMinTake: u16 = 0;
7980
pub const InitialWeightsVersionKey: u16 = 0;
8081
pub const InitialServingRateLimit: u64 = 0; // No limit.
8182
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing
83+
pub const InitialTxDelegateTakeRateLimit: u64 = 0; // Disable rate limit for testing
8284
pub const InitialBurn: u64 = 0;
8385
pub const InitialMinBurn: u64 = 0;
8486
pub const InitialMaxBurn: u64 = 1_000_000_000;
@@ -138,11 +140,13 @@ impl pallet_subtensor::Config for Test {
138140
type InitialBondsMovingAverage = InitialBondsMovingAverage;
139141
type InitialMaxAllowedValidators = InitialMaxAllowedValidators;
140142
type InitialDefaultTake = InitialDefaultTake;
143+
type InitialMinTake = InitialMinTake;
141144
type InitialWeightsVersionKey = InitialWeightsVersionKey;
142145
type InitialMaxDifficulty = InitialMaxDifficulty;
143146
type InitialMinDifficulty = InitialMinDifficulty;
144147
type InitialServingRateLimit = InitialServingRateLimit;
145148
type InitialTxRateLimit = InitialTxRateLimit;
149+
type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit;
146150
type InitialBurn = InitialBurn;
147151
type InitialMaxBurn = InitialMaxBurn;
148152
type InitialMinBurn = InitialMinBurn;
@@ -203,14 +207,22 @@ impl pallet_balances::Config for Test {
203207
pub struct SubtensorIntrf;
204208

205209
impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> for SubtensorIntrf {
206-
fn set_default_take(default_take: u16) {
207-
SubtensorModule::set_default_take(default_take);
210+
fn set_max_delegate_take(default_take: u16) {
211+
SubtensorModule::set_max_delegate_take(default_take);
212+
}
213+
214+
fn set_min_delegate_take(default_take: u16) {
215+
SubtensorModule::set_min_delegate_take(default_take);
208216
}
209217

210218
fn set_tx_rate_limit(rate_limit: u64) {
211219
SubtensorModule::set_tx_rate_limit(rate_limit);
212220
}
213221

222+
fn set_tx_delegate_take_rate_limit(rate_limit: u64) {
223+
SubtensorModule::set_tx_delegate_take_rate_limit(rate_limit);
224+
}
225+
214226
fn set_serving_rate_limit(netuid: u16, rate_limit: u64) {
215227
SubtensorModule::set_serving_rate_limit(netuid, rate_limit);
216228
}

pallets/admin-utils/tests/tests.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,3 +1060,51 @@ 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!(
1077+
SubtensorModule::get_tx_delegate_take_rate_limit(),
1078+
init_value
1079+
);
1080+
assert_ok!(AdminUtils::sudo_set_tx_delegate_take_rate_limit(
1081+
<<Test as Config>::RuntimeOrigin>::root(),
1082+
to_be_set
1083+
));
1084+
assert_eq!(
1085+
SubtensorModule::get_tx_delegate_take_rate_limit(),
1086+
to_be_set
1087+
);
1088+
});
1089+
}
1090+
1091+
#[test]
1092+
fn test_sudo_set_min_delegate_take() {
1093+
new_test_ext().execute_with(|| {
1094+
let to_be_set = u16::MAX / 100;
1095+
let init_value = SubtensorModule::get_min_delegate_take();
1096+
assert_eq!(
1097+
AdminUtils::sudo_set_min_delegate_take(
1098+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1099+
to_be_set
1100+
),
1101+
Err(DispatchError::BadOrigin.into())
1102+
);
1103+
assert_eq!(SubtensorModule::get_min_delegate_take(), init_value);
1104+
assert_ok!(AdminUtils::sudo_set_min_delegate_take(
1105+
<<Test as Config>::RuntimeOrigin>::root(),
1106+
to_be_set
1107+
));
1108+
assert_eq!(SubtensorModule::get_min_delegate_take(), to_be_set);
1109+
});
1110+
}

pallets/subtensor/src/lib.rs

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,16 @@ pub mod pallet {
159159
type InitialMaxAllowedValidators: Get<u16>;
160160
#[pallet::constant] // Initial default delegation take.
161161
type InitialDefaultTake: Get<u16>;
162+
#[pallet::constant] // Initial minimum delegation take.
163+
type InitialMinTake: Get<u16>;
162164
#[pallet::constant] // Initial weights version key.
163165
type InitialWeightsVersionKey: Get<u64>;
164166
#[pallet::constant] // Initial serving rate limit.
165167
type InitialServingRateLimit: Get<u64>;
166168
#[pallet::constant] // Initial transaction rate limit.
167169
type InitialTxRateLimit: Get<u64>;
170+
#[pallet::constant] // Initial delegate take transaction rate limit.
171+
type InitialTxDelegateTakeRateLimit: Get<u64>;
168172
#[pallet::constant] // Initial percentage of total stake required to join senate.
169173
type InitialSenateRequiredStakePercentage: Get<u64>;
170174
#[pallet::constant] // Initial adjustment alpha on burn and pow.
@@ -211,6 +215,10 @@ pub mod pallet {
211215
T::InitialDefaultTake::get()
212216
}
213217
#[pallet::type_value]
218+
pub fn DefaultMinTake<T: Config>() -> u16 {
219+
T::InitialMinTake::get()
220+
}
221+
#[pallet::type_value]
214222
pub fn DefaultAccountTake<T: Config>() -> u64 {
215223
0
216224
}
@@ -247,7 +255,9 @@ pub mod pallet {
247255
#[pallet::storage] // --- ITEM ( total_stake )
248256
pub type TotalStake<T> = StorageValue<_, u64, ValueQuery>;
249257
#[pallet::storage] // --- ITEM ( default_take )
250-
pub type DefaultTake<T> = StorageValue<_, u16, ValueQuery, DefaultDefaultTake<T>>;
258+
pub type MaxTake<T> = StorageValue<_, u16, ValueQuery, DefaultDefaultTake<T>>;
259+
#[pallet::storage] // --- ITEM ( min_take )
260+
pub type MinTake<T> = StorageValue<_, u16, ValueQuery, DefaultMinTake<T>>;
251261
#[pallet::storage] // --- ITEM ( global_block_emission )
252262
pub type BlockEmission<T> = StorageValue<_, u64, ValueQuery, DefaultBlockEmission<T>>;
253263
#[pallet::storage] // --- ITEM ( total_issuance )
@@ -584,15 +594,25 @@ pub mod pallet {
584594
T::InitialTxRateLimit::get()
585595
}
586596
#[pallet::type_value]
597+
pub fn DefaultTxDelegateTakeRateLimit<T: Config>() -> u64 {
598+
T::InitialTxDelegateTakeRateLimit::get()
599+
}
600+
#[pallet::type_value]
587601
pub fn DefaultLastTxBlock<T: Config>() -> u64 {
588602
0
589603
}
590604

591605
#[pallet::storage] // --- ITEM ( tx_rate_limit )
592606
pub(super) type TxRateLimit<T> = StorageValue<_, u64, ValueQuery, DefaultTxRateLimit<T>>;
607+
#[pallet::storage] // --- ITEM ( tx_rate_limit )
608+
pub(super) type TxDelegateTakeRateLimit<T> =
609+
StorageValue<_, u64, ValueQuery, DefaultTxDelegateTakeRateLimit<T>>;
593610
#[pallet::storage] // --- MAP ( key ) --> last_block
594611
pub(super) type LastTxBlock<T: Config> =
595612
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
613+
#[pallet::storage] // --- MAP ( key ) --> last_block
614+
pub(super) type LastTxBlockDelegateTake<T: Config> =
615+
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
596616

597617
#[pallet::type_value]
598618
pub fn DefaultServingRateLimit<T: Config>() -> u64 {
@@ -902,7 +922,8 @@ pub mod pallet {
902922
MaxBurnSet(u16, u64), // --- Event created when setting max burn on a network.
903923
MinBurnSet(u16, u64), // --- Event created when setting min burn on a network.
904924
TxRateLimitSet(u64), // --- Event created when setting the transaction rate limit.
905-
Sudid(DispatchResult), // --- Event created when a sudo call is done.
925+
TxDelegateTakeRateLimitSet(u64), // --- Event created when setting the delegate take transaction rate limit.
926+
Sudid(DispatchResult), // --- Event created when a sudo call is done.
906927
RegistrationAllowed(u16, bool), // --- Event created when registration is allowed/disallowed for a subnet.
907928
PowRegistrationAllowed(u16, bool), // --- Event created when POW registration is allowed/disallowed for a subnet.
908929
TempoSet(u16, u16), // --- Event created when setting tempo on a network
@@ -917,11 +938,15 @@ pub mod pallet {
917938
NetworkMinLockCostSet(u64), // Event created when the network minimum locking cost is set.
918939
SubnetLimitSet(u16), // Event created when the maximum number of subnets is set
919940
NetworkLockCostReductionIntervalSet(u64), // Event created when the lock cost reduction is set
941+
TakeDecreased(T::AccountId, T::AccountId, u16), // Event created when the take for a delegate is decreased.
942+
TakeIncreased(T::AccountId, T::AccountId, u16), // Event created when the take for a delegate is increased.
920943
HotkeySwapped {
921944
coldkey: T::AccountId,
922945
old_hotkey: T::AccountId,
923946
new_hotkey: T::AccountId,
924947
}, // Event created when a hotkey is swapped
948+
MaxDelegateTakeSet(u16), // Event emitted when maximum delegate take is set by sudo/admin transaction
949+
MinDelegateTakeSet(u16), // Event emitted when minimum delegate take is set by sudo/admin transaction
925950
}
926951

927952
// Errors inform users that something went wrong.
@@ -989,6 +1014,7 @@ pub mod pallet {
9891014
NoNeuronIdAvailable, // -- Thrown when no neuron id is available
9901015
/// Thrown a stake would be below the minimum threshold for nominator validations
9911016
NomStakeBelowMinimumThreshold,
1017+
InvalidTake, // --- Thrown when delegate take is being set out of bounds
9921018
}
9931019

9941020
// ==================
@@ -1335,6 +1361,89 @@ pub mod pallet {
13351361
Self::do_become_delegate(origin, hotkey, Self::get_default_take())
13361362
}
13371363

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

0 commit comments

Comments
 (0)