Skip to content

Commit 1f92aa1

Browse files
authored
Merge pull request #699 from opentensor/feat/childkey_take
Feat/childkey take
2 parents 37e6169 + dc67f8f commit 1f92aa1

File tree

23 files changed

+2593
-184
lines changed

23 files changed

+2593
-184
lines changed

pallets/admin-utils/tests/mock.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,15 @@ parameter_types! {
7777
pub const InitialBondsMovingAverage: u64 = 900_000;
7878
pub const InitialStakePruningMin: u16 = 0;
7979
pub const InitialFoundationDistribution: u64 = 0;
80-
pub const InitialDefaultTake: u16 = 11_796; // 18% honest number.
81-
pub const InitialMinTake: u16 = 5_898; // 9%;
80+
pub const InitialDefaultDelegateTake: u16 = 11_796; // 18% honest number.
81+
pub const InitialMinDelegateTake: u16 = 5_898; // 9%;
82+
pub const InitialDefaultChildKeyTake: u16 = 0; // Allow 0 %
83+
pub const InitialMinChildKeyTake: u16 = 0; // Allow 0 %
8284
pub const InitialWeightsVersionKey: u16 = 0;
8385
pub const InitialServingRateLimit: u64 = 0; // No limit.
8486
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing
8587
pub const InitialTxDelegateTakeRateLimit: u64 = 0; // Disable rate limit for testing
88+
pub const InitialTxChildKeyTakeRateLimit: u64 = 0; // Disable rate limit for testing
8689
pub const InitialBurn: u64 = 0;
8790
pub const InitialMinBurn: u64 = 0;
8891
pub const InitialMaxBurn: u64 = 1_000_000_000;
@@ -115,7 +118,7 @@ parameter_types! {
115118
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
116119
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
117120
pub const InitialHotkeyEmissionTempo: u64 = 1;
118-
pub const InitialNetworkMaxStake: u64 = 500_000_000_000_000; // 500_000 TAO
121+
pub const InitialNetworkMaxStake: u64 = u64::MAX; // Maximum possible value for u64, this make the make stake infinity
119122
}
120123

121124
impl pallet_subtensor::Config for Test {
@@ -146,14 +149,17 @@ impl pallet_subtensor::Config for Test {
146149
type InitialPruningScore = InitialPruningScore;
147150
type InitialBondsMovingAverage = InitialBondsMovingAverage;
148151
type InitialMaxAllowedValidators = InitialMaxAllowedValidators;
149-
type InitialDefaultTake = InitialDefaultTake;
150-
type InitialMinTake = InitialMinTake;
152+
type InitialDefaultDelegateTake = InitialDefaultDelegateTake;
153+
type InitialMinDelegateTake = InitialMinDelegateTake;
154+
type InitialDefaultChildKeyTake = InitialDefaultChildKeyTake;
155+
type InitialMinChildKeyTake = InitialMinChildKeyTake;
151156
type InitialWeightsVersionKey = InitialWeightsVersionKey;
152157
type InitialMaxDifficulty = InitialMaxDifficulty;
153158
type InitialMinDifficulty = InitialMinDifficulty;
154159
type InitialServingRateLimit = InitialServingRateLimit;
155160
type InitialTxRateLimit = InitialTxRateLimit;
156161
type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit;
162+
type InitialTxChildKeyTakeRateLimit = InitialTxChildKeyTakeRateLimit;
157163
type InitialBurn = InitialBurn;
158164
type InitialMaxBurn = InitialMaxBurn;
159165
type InitialMinBurn = InitialMinBurn;

pallets/admin-utils/tests/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ use mock::*;
1616
fn test_sudo_set_default_take() {
1717
new_test_ext().execute_with(|| {
1818
let to_be_set: u16 = 10;
19-
let init_value: u16 = SubtensorModule::get_default_take();
19+
let init_value: u16 = SubtensorModule::get_default_delegate_take();
2020
assert_eq!(
2121
AdminUtils::sudo_set_default_take(
2222
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
2323
to_be_set
2424
),
2525
Err(DispatchError::BadOrigin)
2626
);
27-
assert_eq!(SubtensorModule::get_default_take(), init_value);
27+
assert_eq!(SubtensorModule::get_default_delegate_take(), init_value);
2828
assert_ok!(AdminUtils::sudo_set_default_take(
2929
<<Test as Config>::RuntimeOrigin>::root(),
3030
to_be_set
3131
));
32-
assert_eq!(SubtensorModule::get_default_take(), to_be_set);
32+
assert_eq!(SubtensorModule::get_default_delegate_take(), to_be_set);
3333
});
3434
}
3535

pallets/subtensor/src/benchmarks.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ benchmarks! {
159159
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), wallet_bal);
160160

161161
assert_ok!(Subtensor::<T>::do_burned_registration(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey.clone()));
162-
assert_ok!(Subtensor::<T>::do_become_delegate(RawOrigin::Signed(coldkey.clone()).into(), hotkey.clone(), Subtensor::<T>::get_default_take()));
162+
assert_ok!(Subtensor::<T>::do_become_delegate(RawOrigin::Signed(coldkey.clone()).into(), hotkey.clone(), Subtensor::<T>::get_default_delegate_take()));
163163

164164
// Stake 10% of our current total staked TAO
165165
let u64_staked_amt = 100_000_000_000;
@@ -429,4 +429,28 @@ reveal_weights {
429429

430430
}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, salt, version_key)
431431

432+
benchmark_sudo_set_tx_childkey_take_rate_limit {
433+
// We don't need to set up any initial state for this benchmark
434+
// as it's a simple setter function that only requires root origin
435+
let new_rate_limit: u64 = 100;
436+
}: sudo_set_tx_childkey_take_rate_limit(RawOrigin::Root, new_rate_limit)
437+
438+
benchmark_set_childkey_take {
439+
// Setup
440+
let netuid: u16 = 1;
441+
let tempo: u16 = 1;
442+
let seed: u32 = 1;
443+
let coldkey: T::AccountId = account("Cold", 0, seed);
444+
let hotkey: T::AccountId = account("Hot", 0, seed);
445+
let take: u16 = 1000; // 10% in basis points
446+
447+
// Initialize the network
448+
Subtensor::<T>::init_new_network(netuid, tempo);
449+
450+
// Register the hotkey
451+
Subtensor::<T>::set_burn(netuid, 1);
452+
let amount_to_be_staked = 1_000_000u32.into();
453+
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey, amount_to_be_staked);
454+
assert_ok!(Subtensor::<T>::do_burned_registration(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey.clone()));
455+
}: set_childkey_take(RawOrigin::Signed(coldkey), hotkey, netuid, take)
432456
}

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ impl<T: Config> Pallet<T> {
131131
log::debug!("Accumulated emissions on hotkey {:?} for netuid {:?}: mining {:?}, validator {:?}", hotkey, *netuid, mining_emission, validator_emission);
132132
}
133133
} else {
134+
// No epoch, increase blocks since last step and continue
135+
Self::set_blocks_since_last_step(
136+
*netuid,
137+
Self::get_blocks_since_last_step(*netuid).saturating_add(1),
138+
);
134139
log::debug!("Tempo not reached for subnet: {:?}", *netuid);
135140
}
136141
}
@@ -186,7 +191,7 @@ impl<T: Config> Pallet<T> {
186191
mining_emission: u64,
187192
) {
188193
// --- 1. First, calculate the hotkey's share of the emission.
189-
let take_proportion: I64F64 = I64F64::from_num(Delegates::<T>::get(hotkey))
194+
let take_proportion: I64F64 = I64F64::from_num(Self::get_childkey_take(hotkey, netuid))
190195
.saturating_div(I64F64::from_num(u16::MAX));
191196
let hotkey_take: u64 = take_proportion
192197
.saturating_mul(I64F64::from_num(validating_emission))

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ impl<T: Config> Pallet<T> {
3131
/// This function does not explicitly panic, but underlying arithmetic operations
3232
/// use saturating arithmetic to prevent overflows.
3333
///
34-
/// TODO: check for self loops.
35-
/// TODO: (@distributedstatemachine): check if we should return error , otherwise self loop
36-
/// detection is impossible to test.
3734
pub fn get_stake_for_hotkey_on_subnet(hotkey: &T::AccountId, netuid: u16) -> u64 {
3835
// Retrieve the initial total stake for the hotkey without any child/parent adjustments.
3936
let initial_stake: u64 = Self::get_total_stake_for_hotkey(hotkey);
37+
log::debug!("Initial stake: {:?}", initial_stake);
4038
let mut stake_to_children: u64 = 0;
4139
let mut stake_from_parents: u64 = 0;
4240

pallets/subtensor/src/lib.rs

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,27 @@ pub mod pallet {
166166
21_000_000_000_000_000
167167
}
168168
#[pallet::type_value]
169-
/// Default total stake.
170-
pub fn DefaultDefaultTake<T: Config>() -> u16 {
171-
T::InitialDefaultTake::get()
169+
/// Default Delegate Take.
170+
pub fn DefaultDelegateTake<T: Config>() -> u16 {
171+
T::InitialDefaultDelegateTake::get()
172+
}
173+
174+
#[pallet::type_value]
175+
/// Default childkey take.
176+
pub fn DefaultChildKeyTake<T: Config>() -> u16 {
177+
T::InitialDefaultChildKeyTake::get()
172178
}
173179
#[pallet::type_value]
174-
/// Default minimum take.
175-
pub fn DefaultMinTake<T: Config>() -> u16 {
176-
T::InitialMinTake::get()
180+
/// Default minimum delegate take.
181+
pub fn DefaultMinDelegateTake<T: Config>() -> u16 {
182+
T::InitialMinDelegateTake::get()
177183
}
184+
#[pallet::type_value]
185+
/// Default minimum childkey take.
186+
pub fn DefaultMinChildKeyTake<T: Config>() -> u16 {
187+
T::InitialMinChildKeyTake::get()
188+
}
189+
178190
#[pallet::type_value]
179191
/// Default account take.
180192
pub fn DefaultAccountTake<T: Config>() -> u64 {
@@ -364,8 +376,6 @@ pub mod pallet {
364376
}
365377
T::InitialNetworkRateLimit::get()
366378
}
367-
// #[pallet::type_value] /// Default value for network max stake.
368-
// pub fn DefaultNetworkMaxStake<T: Config>() -> u64 { T::InitialNetworkMaxStake::get() }
369379
#[pallet::type_value]
370380
/// Default value for emission values.
371381
pub fn DefaultEmissionValues<T: Config>() -> u64 {
@@ -539,6 +549,11 @@ pub mod pallet {
539549
T::InitialTxDelegateTakeRateLimit::get()
540550
}
541551
#[pallet::type_value]
552+
/// Default value for chidlkey take rate limiting
553+
pub fn DefaultTxChildKeyTakeRateLimit<T: Config>() -> u64 {
554+
T::InitialTxChildKeyTakeRateLimit::get()
555+
}
556+
#[pallet::type_value]
542557
/// Default value for last extrinsic block.
543558
pub fn DefaultLastTxBlock<T: Config>() -> u64 {
544559
0
@@ -599,10 +614,15 @@ pub mod pallet {
599614
pub type TotalIssuance<T> = StorageValue<_, u64, ValueQuery, DefaultTotalIssuance<T>>;
600615
#[pallet::storage] // --- ITEM ( total_stake )
601616
pub type TotalStake<T> = StorageValue<_, u64, ValueQuery>;
602-
#[pallet::storage] // --- ITEM ( default_take )
603-
pub type MaxTake<T> = StorageValue<_, u16, ValueQuery, DefaultDefaultTake<T>>;
604-
#[pallet::storage] // --- ITEM ( min_take )
605-
pub type MinTake<T> = StorageValue<_, u16, ValueQuery, DefaultMinTake<T>>;
617+
#[pallet::storage] // --- ITEM ( default_delegate_take )
618+
pub type MaxDelegateTake<T> = StorageValue<_, u16, ValueQuery, DefaultDelegateTake<T>>;
619+
#[pallet::storage] // --- ITEM ( min_delegate_take )
620+
pub type MinDelegateTake<T> = StorageValue<_, u16, ValueQuery, DefaultMinDelegateTake<T>>;
621+
#[pallet::storage] // --- ITEM ( default_childkey_take )
622+
pub type MaxChildkeyTake<T> = StorageValue<_, u16, ValueQuery, DefaultChildKeyTake<T>>;
623+
#[pallet::storage] // --- ITEM ( min_childkey_take )
624+
pub type MinChildkeyTake<T> = StorageValue<_, u16, ValueQuery, DefaultMinChildKeyTake<T>>;
625+
606626
#[pallet::storage] // --- ITEM ( global_block_emission )
607627
pub type BlockEmission<T> = StorageValue<_, u64, ValueQuery, DefaultBlockEmission<T>>;
608628
#[pallet::storage] // --- ITEM (target_stakes_per_interval)
@@ -635,7 +655,19 @@ pub mod pallet {
635655
#[pallet::storage]
636656
/// MAP ( hot ) --> take | Returns the hotkey delegation take. And signals that this key is open for delegation.
637657
pub type Delegates<T: Config> =
638-
StorageMap<_, Blake2_128Concat, T::AccountId, u16, ValueQuery, DefaultDefaultTake<T>>;
658+
StorageMap<_, Blake2_128Concat, T::AccountId, u16, ValueQuery, DefaultDelegateTake<T>>;
659+
#[pallet::storage]
660+
/// DMAP ( hot, netuid ) --> take | Returns the hotkey childkey take for a specific subnet
661+
pub type ChildkeyTake<T: Config> = StorageDoubleMap<
662+
_,
663+
Blake2_128Concat,
664+
T::AccountId, // First key: hotkey
665+
Identity,
666+
u16, // Second key: netuid
667+
u16, // Value: take
668+
ValueQuery,
669+
>;
670+
639671
#[pallet::storage]
640672
/// DMAP ( hot, cold ) --> stake | Returns the stake under a coldkey prefixed by hotkey.
641673
pub type Stake<T: Config> = StorageDoubleMap<
@@ -953,10 +985,14 @@ pub mod pallet {
953985
/// --- ITEM ( tx_rate_limit )
954986
pub type TxRateLimit<T> = StorageValue<_, u64, ValueQuery, DefaultTxRateLimit<T>>;
955987
#[pallet::storage]
956-
/// --- ITEM ( tx_rate_limit )
988+
/// --- ITEM ( tx_delegate_take_rate_limit )
957989
pub type TxDelegateTakeRateLimit<T> =
958990
StorageValue<_, u64, ValueQuery, DefaultTxDelegateTakeRateLimit<T>>;
959991
#[pallet::storage]
992+
/// --- ITEM ( tx_childkey_take_rate_limit )
993+
pub type TxChildkeyTakeRateLimit<T> =
994+
StorageValue<_, u64, ValueQuery, DefaultTxChildKeyTakeRateLimit<T>>;
995+
#[pallet::storage]
960996
/// --- MAP ( netuid ) --> Whether or not Liquid Alpha is enabled
961997
pub type LiquidAlphaOn<T> =
962998
StorageMap<_, Blake2_128Concat, u16, bool, ValueQuery, DefaultLiquidAlpha<T>>;
@@ -1103,7 +1139,11 @@ pub mod pallet {
11031139
pub type LastTxBlock<T: Config> =
11041140
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
11051141
#[pallet::storage]
1106-
/// --- MAP ( key ) --> last_block
1142+
/// --- MAP ( key ) --> last_tx_block_childkey_take
1143+
pub type LastTxBlockChildKeyTake<T: Config> =
1144+
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
1145+
#[pallet::storage]
1146+
/// --- MAP ( key ) --> last_tx_block_delegate_take
11071147
pub type LastTxBlockDelegateTake<T: Config> =
11081148
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
11091149
#[pallet::storage]

pallets/subtensor/src/macros/config.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,16 @@ mod config {
112112
type InitialMaxAllowedValidators: Get<u16>;
113113
/// Initial default delegation take.
114114
#[pallet::constant]
115-
type InitialDefaultTake: Get<u16>;
115+
type InitialDefaultDelegateTake: Get<u16>;
116116
/// Initial minimum delegation take.
117117
#[pallet::constant]
118-
type InitialMinTake: Get<u16>;
118+
type InitialMinDelegateTake: Get<u16>;
119+
/// Initial default childkey take.
120+
#[pallet::constant]
121+
type InitialDefaultChildKeyTake: Get<u16>;
122+
/// Initial minimum childkey take.
123+
#[pallet::constant]
124+
type InitialMinChildKeyTake: Get<u16>;
119125
/// Initial weights version key.
120126
#[pallet::constant]
121127
type InitialWeightsVersionKey: Get<u64>;
@@ -128,6 +134,9 @@ mod config {
128134
/// Initial delegate take transaction rate limit.
129135
#[pallet::constant]
130136
type InitialTxDelegateTakeRateLimit: Get<u64>;
137+
/// Initial childkey take transaction rate limit.
138+
#[pallet::constant]
139+
type InitialTxChildKeyTakeRateLimit: Get<u64>;
131140
/// Initial percentage of total stake required to join senate.
132141
#[pallet::constant]
133142
type InitialSenateRequiredStakePercentage: Get<u64>;

0 commit comments

Comments
 (0)