Skip to content

Commit

Permalink
Merge pull request #699 from opentensor/feat/childkey_take
Browse files Browse the repository at this point in the history
Feat/childkey take
  • Loading branch information
unconst authored Aug 19, 2024
2 parents 37e6169 + dc67f8f commit 1f92aa1
Show file tree
Hide file tree
Showing 23 changed files with 2,593 additions and 184 deletions.
16 changes: 11 additions & 5 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ parameter_types! {
pub const InitialBondsMovingAverage: u64 = 900_000;
pub const InitialStakePruningMin: u16 = 0;
pub const InitialFoundationDistribution: u64 = 0;
pub const InitialDefaultTake: u16 = 11_796; // 18% honest number.
pub const InitialMinTake: u16 = 5_898; // 9%;
pub const InitialDefaultDelegateTake: u16 = 11_796; // 18% honest number.
pub const InitialMinDelegateTake: u16 = 5_898; // 9%;
pub const InitialDefaultChildKeyTake: u16 = 0; // Allow 0 %
pub const InitialMinChildKeyTake: u16 = 0; // Allow 0 %
pub const InitialWeightsVersionKey: u16 = 0;
pub const InitialServingRateLimit: u64 = 0; // No limit.
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing
pub const InitialTxDelegateTakeRateLimit: u64 = 0; // Disable rate limit for testing
pub const InitialTxChildKeyTakeRateLimit: u64 = 0; // Disable rate limit for testing
pub const InitialBurn: u64 = 0;
pub const InitialMinBurn: u64 = 0;
pub const InitialMaxBurn: u64 = 1_000_000_000;
Expand Down Expand Up @@ -115,7 +118,7 @@ parameter_types! {
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
pub const InitialHotkeyEmissionTempo: u64 = 1;
pub const InitialNetworkMaxStake: u64 = 500_000_000_000_000; // 500_000 TAO
pub const InitialNetworkMaxStake: u64 = u64::MAX; // Maximum possible value for u64, this make the make stake infinity
}

impl pallet_subtensor::Config for Test {
Expand Down Expand Up @@ -146,14 +149,17 @@ impl pallet_subtensor::Config for Test {
type InitialPruningScore = InitialPruningScore;
type InitialBondsMovingAverage = InitialBondsMovingAverage;
type InitialMaxAllowedValidators = InitialMaxAllowedValidators;
type InitialDefaultTake = InitialDefaultTake;
type InitialMinTake = InitialMinTake;
type InitialDefaultDelegateTake = InitialDefaultDelegateTake;
type InitialMinDelegateTake = InitialMinDelegateTake;
type InitialDefaultChildKeyTake = InitialDefaultChildKeyTake;
type InitialMinChildKeyTake = InitialMinChildKeyTake;
type InitialWeightsVersionKey = InitialWeightsVersionKey;
type InitialMaxDifficulty = InitialMaxDifficulty;
type InitialMinDifficulty = InitialMinDifficulty;
type InitialServingRateLimit = InitialServingRateLimit;
type InitialTxRateLimit = InitialTxRateLimit;
type InitialTxDelegateTakeRateLimit = InitialTxDelegateTakeRateLimit;
type InitialTxChildKeyTakeRateLimit = InitialTxChildKeyTakeRateLimit;
type InitialBurn = InitialBurn;
type InitialMaxBurn = InitialMaxBurn;
type InitialMinBurn = InitialMinBurn;
Expand Down
6 changes: 3 additions & 3 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ use mock::*;
fn test_sudo_set_default_take() {
new_test_ext().execute_with(|| {
let to_be_set: u16 = 10;
let init_value: u16 = SubtensorModule::get_default_take();
let init_value: u16 = SubtensorModule::get_default_delegate_take();
assert_eq!(
AdminUtils::sudo_set_default_take(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
to_be_set
),
Err(DispatchError::BadOrigin)
);
assert_eq!(SubtensorModule::get_default_take(), init_value);
assert_eq!(SubtensorModule::get_default_delegate_take(), init_value);
assert_ok!(AdminUtils::sudo_set_default_take(
<<Test as Config>::RuntimeOrigin>::root(),
to_be_set
));
assert_eq!(SubtensorModule::get_default_take(), to_be_set);
assert_eq!(SubtensorModule::get_default_delegate_take(), to_be_set);
});
}

Expand Down
26 changes: 25 additions & 1 deletion pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ benchmarks! {
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), wallet_bal);

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

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

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

benchmark_sudo_set_tx_childkey_take_rate_limit {
// We don't need to set up any initial state for this benchmark
// as it's a simple setter function that only requires root origin
let new_rate_limit: u64 = 100;
}: sudo_set_tx_childkey_take_rate_limit(RawOrigin::Root, new_rate_limit)

benchmark_set_childkey_take {
// Setup
let netuid: u16 = 1;
let tempo: u16 = 1;
let seed: u32 = 1;
let coldkey: T::AccountId = account("Cold", 0, seed);
let hotkey: T::AccountId = account("Hot", 0, seed);
let take: u16 = 1000; // 10% in basis points

// Initialize the network
Subtensor::<T>::init_new_network(netuid, tempo);

// Register the hotkey
Subtensor::<T>::set_burn(netuid, 1);
let amount_to_be_staked = 1_000_000u32.into();
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey, amount_to_be_staked);
assert_ok!(Subtensor::<T>::do_burned_registration(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey.clone()));
}: set_childkey_take(RawOrigin::Signed(coldkey), hotkey, netuid, take)
}
7 changes: 6 additions & 1 deletion pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ impl<T: Config> Pallet<T> {
log::debug!("Accumulated emissions on hotkey {:?} for netuid {:?}: mining {:?}, validator {:?}", hotkey, *netuid, mining_emission, validator_emission);
}
} else {
// No epoch, increase blocks since last step and continue
Self::set_blocks_since_last_step(
*netuid,
Self::get_blocks_since_last_step(*netuid).saturating_add(1),
);
log::debug!("Tempo not reached for subnet: {:?}", *netuid);
}
}
Expand Down Expand Up @@ -186,7 +191,7 @@ impl<T: Config> Pallet<T> {
mining_emission: u64,
) {
// --- 1. First, calculate the hotkey's share of the emission.
let take_proportion: I64F64 = I64F64::from_num(Delegates::<T>::get(hotkey))
let take_proportion: I64F64 = I64F64::from_num(Self::get_childkey_take(hotkey, netuid))
.saturating_div(I64F64::from_num(u16::MAX));
let hotkey_take: u64 = take_proportion
.saturating_mul(I64F64::from_num(validating_emission))
Expand Down
4 changes: 1 addition & 3 deletions pallets/subtensor/src/epoch/run_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ impl<T: Config> Pallet<T> {
/// This function does not explicitly panic, but underlying arithmetic operations
/// use saturating arithmetic to prevent overflows.
///
/// TODO: check for self loops.
/// TODO: (@distributedstatemachine): check if we should return error , otherwise self loop
/// detection is impossible to test.
pub fn get_stake_for_hotkey_on_subnet(hotkey: &T::AccountId, netuid: u16) -> u64 {
// Retrieve the initial total stake for the hotkey without any child/parent adjustments.
let initial_stake: u64 = Self::get_total_stake_for_hotkey(hotkey);
log::debug!("Initial stake: {:?}", initial_stake);
let mut stake_to_children: u64 = 0;
let mut stake_from_parents: u64 = 0;

Expand Down
70 changes: 55 additions & 15 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,27 @@ pub mod pallet {
21_000_000_000_000_000
}
#[pallet::type_value]
/// Default total stake.
pub fn DefaultDefaultTake<T: Config>() -> u16 {
T::InitialDefaultTake::get()
/// Default Delegate Take.
pub fn DefaultDelegateTake<T: Config>() -> u16 {
T::InitialDefaultDelegateTake::get()
}

#[pallet::type_value]
/// Default childkey take.
pub fn DefaultChildKeyTake<T: Config>() -> u16 {
T::InitialDefaultChildKeyTake::get()
}
#[pallet::type_value]
/// Default minimum take.
pub fn DefaultMinTake<T: Config>() -> u16 {
T::InitialMinTake::get()
/// Default minimum delegate take.
pub fn DefaultMinDelegateTake<T: Config>() -> u16 {
T::InitialMinDelegateTake::get()
}
#[pallet::type_value]
/// Default minimum childkey take.
pub fn DefaultMinChildKeyTake<T: Config>() -> u16 {
T::InitialMinChildKeyTake::get()
}

#[pallet::type_value]
/// Default account take.
pub fn DefaultAccountTake<T: Config>() -> u64 {
Expand Down Expand Up @@ -364,8 +376,6 @@ pub mod pallet {
}
T::InitialNetworkRateLimit::get()
}
// #[pallet::type_value] /// Default value for network max stake.
// pub fn DefaultNetworkMaxStake<T: Config>() -> u64 { T::InitialNetworkMaxStake::get() }
#[pallet::type_value]
/// Default value for emission values.
pub fn DefaultEmissionValues<T: Config>() -> u64 {
Expand Down Expand Up @@ -539,6 +549,11 @@ pub mod pallet {
T::InitialTxDelegateTakeRateLimit::get()
}
#[pallet::type_value]
/// Default value for chidlkey take rate limiting
pub fn DefaultTxChildKeyTakeRateLimit<T: Config>() -> u64 {
T::InitialTxChildKeyTakeRateLimit::get()
}
#[pallet::type_value]
/// Default value for last extrinsic block.
pub fn DefaultLastTxBlock<T: Config>() -> u64 {
0
Expand Down Expand Up @@ -599,10 +614,15 @@ pub mod pallet {
pub type TotalIssuance<T> = StorageValue<_, u64, ValueQuery, DefaultTotalIssuance<T>>;
#[pallet::storage] // --- ITEM ( total_stake )
pub type TotalStake<T> = StorageValue<_, u64, ValueQuery>;
#[pallet::storage] // --- ITEM ( default_take )
pub type MaxTake<T> = StorageValue<_, u16, ValueQuery, DefaultDefaultTake<T>>;
#[pallet::storage] // --- ITEM ( min_take )
pub type MinTake<T> = StorageValue<_, u16, ValueQuery, DefaultMinTake<T>>;
#[pallet::storage] // --- ITEM ( default_delegate_take )
pub type MaxDelegateTake<T> = StorageValue<_, u16, ValueQuery, DefaultDelegateTake<T>>;
#[pallet::storage] // --- ITEM ( min_delegate_take )
pub type MinDelegateTake<T> = StorageValue<_, u16, ValueQuery, DefaultMinDelegateTake<T>>;
#[pallet::storage] // --- ITEM ( default_childkey_take )
pub type MaxChildkeyTake<T> = StorageValue<_, u16, ValueQuery, DefaultChildKeyTake<T>>;
#[pallet::storage] // --- ITEM ( min_childkey_take )
pub type MinChildkeyTake<T> = StorageValue<_, u16, ValueQuery, DefaultMinChildKeyTake<T>>;

#[pallet::storage] // --- ITEM ( global_block_emission )
pub type BlockEmission<T> = StorageValue<_, u64, ValueQuery, DefaultBlockEmission<T>>;
#[pallet::storage] // --- ITEM (target_stakes_per_interval)
Expand Down Expand Up @@ -635,7 +655,19 @@ pub mod pallet {
#[pallet::storage]
/// MAP ( hot ) --> take | Returns the hotkey delegation take. And signals that this key is open for delegation.
pub type Delegates<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, u16, ValueQuery, DefaultDefaultTake<T>>;
StorageMap<_, Blake2_128Concat, T::AccountId, u16, ValueQuery, DefaultDelegateTake<T>>;
#[pallet::storage]
/// DMAP ( hot, netuid ) --> take | Returns the hotkey childkey take for a specific subnet
pub type ChildkeyTake<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId, // First key: hotkey
Identity,
u16, // Second key: netuid
u16, // Value: take
ValueQuery,
>;

#[pallet::storage]
/// DMAP ( hot, cold ) --> stake | Returns the stake under a coldkey prefixed by hotkey.
pub type Stake<T: Config> = StorageDoubleMap<
Expand Down Expand Up @@ -953,10 +985,14 @@ pub mod pallet {
/// --- ITEM ( tx_rate_limit )
pub type TxRateLimit<T> = StorageValue<_, u64, ValueQuery, DefaultTxRateLimit<T>>;
#[pallet::storage]
/// --- ITEM ( tx_rate_limit )
/// --- ITEM ( tx_delegate_take_rate_limit )
pub type TxDelegateTakeRateLimit<T> =
StorageValue<_, u64, ValueQuery, DefaultTxDelegateTakeRateLimit<T>>;
#[pallet::storage]
/// --- ITEM ( tx_childkey_take_rate_limit )
pub type TxChildkeyTakeRateLimit<T> =
StorageValue<_, u64, ValueQuery, DefaultTxChildKeyTakeRateLimit<T>>;
#[pallet::storage]
/// --- MAP ( netuid ) --> Whether or not Liquid Alpha is enabled
pub type LiquidAlphaOn<T> =
StorageMap<_, Blake2_128Concat, u16, bool, ValueQuery, DefaultLiquidAlpha<T>>;
Expand Down Expand Up @@ -1103,7 +1139,11 @@ pub mod pallet {
pub type LastTxBlock<T: Config> =
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
#[pallet::storage]
/// --- MAP ( key ) --> last_block
/// --- MAP ( key ) --> last_tx_block_childkey_take
pub type LastTxBlockChildKeyTake<T: Config> =
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
#[pallet::storage]
/// --- MAP ( key ) --> last_tx_block_delegate_take
pub type LastTxBlockDelegateTake<T: Config> =
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
#[pallet::storage]
Expand Down
13 changes: 11 additions & 2 deletions pallets/subtensor/src/macros/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,16 @@ mod config {
type InitialMaxAllowedValidators: Get<u16>;
/// Initial default delegation take.
#[pallet::constant]
type InitialDefaultTake: Get<u16>;
type InitialDefaultDelegateTake: Get<u16>;
/// Initial minimum delegation take.
#[pallet::constant]
type InitialMinTake: Get<u16>;
type InitialMinDelegateTake: Get<u16>;
/// Initial default childkey take.
#[pallet::constant]
type InitialDefaultChildKeyTake: Get<u16>;
/// Initial minimum childkey take.
#[pallet::constant]
type InitialMinChildKeyTake: Get<u16>;
/// Initial weights version key.
#[pallet::constant]
type InitialWeightsVersionKey: Get<u64>;
Expand All @@ -128,6 +134,9 @@ mod config {
/// Initial delegate take transaction rate limit.
#[pallet::constant]
type InitialTxDelegateTakeRateLimit: Get<u64>;
/// Initial childkey take transaction rate limit.
#[pallet::constant]
type InitialTxChildKeyTakeRateLimit: Get<u64>;
/// Initial percentage of total stake required to join senate.
#[pallet::constant]
type InitialSenateRequiredStakePercentage: Get<u64>;
Expand Down
Loading

0 comments on commit 1f92aa1

Please sign in to comment.