Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pallets/admin-utils/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl pallet_subtensor::Config for Test {
type InitialColdkeySwapScheduleDuration = InitialColdkeySwapScheduleDuration;
type InitialDissolveNetworkScheduleDuration = InitialDissolveNetworkScheduleDuration;
type InitialTaoWeight = InitialTaoWeight;
type WeightInfo = pallet_subtensor::weights::SubstrateWeight<Test>;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
Expand Down
7 changes: 7 additions & 0 deletions pallets/subtensor/src/coinbase/block_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
pub fn block_step() -> Result<(), &'static str> {
let block_number: u64 = Self::get_current_block_as_u64();
log::debug!("block_step for block: {:?} ", block_number);

let last_block_hash: T::Hash = Self::get_last_block_hash();
log::debug!("last_block_hash: {:?}", last_block_hash);

// --- 1. Adjust difficulties.
Self::adjust_registration_terms_for_networks();
// --- 2. Get the current coinbase emission.
Expand All @@ -20,6 +24,9 @@ impl<T: Config + pallet_drand::Config> Pallet<T> {
// --- 4. Set pending children on the epoch; but only after the coinbase has been run.
Self::try_set_pending_children(block_number);

// --- 5. Run auto-claim root divs.
Self::run_auto_claim_root_divs(last_block_hash);

// Return ok.
Ok(())
}
Expand Down
487 changes: 73 additions & 414 deletions pallets/subtensor/src/coinbase/run_coinbase.rs

Large diffs are not rendered by default.

110 changes: 107 additions & 3 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ use sp_std::marker::PhantomData;
// ============================
mod benchmarks;

pub mod weights;
pub use weights::WeightInfo;

// =========================
// ==== Pallet Imports =====
// =========================
Expand Down Expand Up @@ -223,9 +226,29 @@ pub mod pallet {
/// The subnet's contact
pub subnet_contact: Vec<u8>,
}
/// ============================
/// ==== Staking + Accounts ====
/// ============================
// ============================
// ==== Staking + Accounts ====
// ============================

/// Enum for the per-coldkey root claim setting.
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub enum RootClaimTypeEnum {
/// Swap any alpha emission for TAO.
#[default]
Swap,
/// Keep all alpha emission.
Keep,
}

/// Enum for the per-coldkey root claim frequency setting.
#[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)]
pub enum RootClaimFrequencyEnum {
/// Claim automatically.
#[default]
Auto,
/// Only claim manually; Never automatically.
Manual,
}

#[pallet::type_value]
/// Default value for zero.
Expand Down Expand Up @@ -720,6 +743,40 @@ pub mod pallet {
500_000
}

#[pallet::type_value]
/// Default minimum root claim amount.
/// This is the minimum amount of root claim that can be made.
/// Any amount less than this will not be claimed.
pub fn DefaultMinRootClaimAmount<T: Config>() -> u64 {
500_000
}

#[pallet::type_value]
/// Default root claim type.
/// This is the type of root claim that will be made.
/// This is set by the user. Either swap to TAO or keep as alpha.
pub fn DefaultRootClaimType<T: Config>() -> RootClaimTypeEnum {
RootClaimTypeEnum::default()
}

#[pallet::type_value]
/// Default root claim frequency.
/// This is the frequency of root claims for a coldkey.
/// This is set by the user. Either auto or manual.
pub fn DefaultRootClaimFrequency<T: Config>() -> RootClaimFrequencyEnum {
RootClaimFrequencyEnum::default()
}

#[pallet::type_value]
/// Default number of root claims per claim call.
/// Ideally this is calculated using the number of staking coldkey
/// and the block time.
pub fn DefaultNumRootClaim<T: Config>() -> u64 {
// TODO: replace with size of staking coldkeys / 7200
// i.e. once per day
15
}

#[pallet::type_value]
/// Default unicode vector for tau symbol.
pub fn DefaultUnicodeVecU8<T: Config>() -> Vec<u8> {
Expand Down Expand Up @@ -1000,6 +1057,53 @@ pub mod pallet {
#[pallet::storage] // --- MAP ( netuid ) --> subnet_name | Returns the name of the subnet.
pub type SubnetName<T: Config> =
StorageMap<_, Identity, u16, Vec<u8>, ValueQuery, DefaultUnicodeVecU8<T>>;
#[pallet::storage] // --- DMAP ( hot, netuid ) --> claimable_dividends | Root claimable dividends.
pub type RootClaimable<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Identity,
u16,
u64,
ValueQuery,
DefaultZeroU64<T>,
>;
#[pallet::storage] // --- NMAP ( hot, cold, netuid ) --> claimable_debt | Returns a keys debt for claimable divs.
pub type RootDebt<T: Config> = StorageNMap<
_,
(
NMapKey<Blake2_128Concat, T::AccountId>, // hot
NMapKey<Blake2_128Concat, T::AccountId>, // cold
NMapKey<Identity, u16>, // subnet
),
I96F32, // Shares
ValueQuery,
>;
#[pallet::storage] // -- MAP ( cold ) --> root_claim_type enum
pub type RootClaimType<T: Config> = StorageMap<
_,
Blake2_128Concat,
T::AccountId,
RootClaimTypeEnum,
ValueQuery,
DefaultRootClaimType<T>,
>;
#[pallet::storage] // -- MAP ( cold ) --> root_claim_frequency enum
pub type RootClaimFrequency<T: Config> = StorageMap<
_,
Blake2_128Concat,
T::AccountId,
RootClaimFrequencyEnum,
ValueQuery,
DefaultRootClaimFrequency<T>,
>;

#[pallet::storage] // --- MAP ( u64 ) --> coldkey | Maps coldkeys that have stake to an index
pub type ColdkeysIndex<T: Config> = StorageMap<_, Identity, u64, T::AccountId, OptionQuery>;
#[pallet::storage] // --- Value --> num_staking_coldkeys
pub type NumColdkeys<T: Config> = StorageValue<_, u64, ValueQuery, DefaultZeroU64<T>>;
#[pallet::storage] // --- Value --> num_root_claim | Number of coldkeys to claim each auto-claim.
pub type NumRootClaim<T: Config> = StorageValue<_, u64, ValueQuery, DefaultNumRootClaim<T>>;

/// ============================
/// ==== Global Parameters =====
Expand Down
3 changes: 3 additions & 0 deletions pallets/subtensor/src/macros/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,8 @@ mod config {
/// Initial TAO weight.
#[pallet::constant]
type InitialTaoWeight: Get<u64>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: crate::WeightInfo;
}
}
43 changes: 42 additions & 1 deletion pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,47 @@ mod dispatches {
)
}

/// --- Claims the root emissions for a coldkey.
/// # Args:
/// * 'origin': (<T as frame_system::Config>Origin):
/// - The signature of the caller's coldkey.
///
/// # Event:
/// * RootClaimed;
/// - On the successfully claiming the root emissions for a coldkey.
///
/// # Raises:
///
#[pallet::call_index(90)]
#[pallet::weight((Weight::from_parts(200_000, 0).saturating_add(T::DbWeight::get().reads_writes(1, 2)), DispatchClass::Normal, Pays::Yes))]
pub fn claim_root(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let coldkey: T::AccountId = ensure_signed(origin)?;

let weight = Self::do_root_claim(coldkey);
Ok((Some(weight), Pays::Yes).into())
}

/// --- Sets the root claim type for the coldkey.
/// # Args:
/// * 'origin': (<T as frame_system::Config>Origin):
/// - The signature of the caller's coldkey.
///
/// # Event:
/// * RootClaimTypeSet;
/// - On the successfully setting the root claim type for the coldkey.
///
#[pallet::call_index(91)]
#[pallet::weight((Weight::from_parts(45_000_000, 0).saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Normal, Pays::Yes))]
pub fn set_root_claim_type(
origin: OriginFor<T>,
new_root_claim_type: RootClaimTypeEnum,
) -> DispatchResult {
let coldkey: T::AccountId = ensure_signed(origin)?;

Self::change_root_claim_type(&coldkey, new_root_claim_type);
Ok(())
}

/// Swaps a specified amount of stake from one subnet to another, while keeping the same coldkey and hotkey.
///
/// # Arguments
Expand All @@ -1833,7 +1874,7 @@ mod dispatches {
///
/// # Events
/// May emit a `StakeSwapped` event on success.
#[pallet::call_index(90)]
#[pallet::call_index(92)]
#[pallet::weight((
Weight::from_parts(3_000_000, 0).saturating_add(T::DbWeight::get().writes(1)),
DispatchClass::Operational,
Expand Down
15 changes: 15 additions & 0 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,20 @@ mod events {
/// Parameters:
/// (coldkey, hotkey, origin_netuid, destination_netuid, amount)
StakeSwapped(T::AccountId, T::AccountId, u16, u16, u64),

/// Root emissions have been claimed for a coldkey on all subnets and hotkeys.
/// Parameters:
/// (coldkey)
RootClaimed(T::AccountId),

/// Root claim type for a coldkey has been set.
/// Parameters:
/// (coldkey, u8)
RootClaimTypeSet(T::AccountId, RootClaimTypeEnum),

/// Root claim frequency for a coldkey has been set.
/// Parameters:
/// (coldkey, u8)
RootClaimFrequencySet(T::AccountId, RootClaimTypeEnum),
}
}
18 changes: 9 additions & 9 deletions pallets/subtensor/src/migrations/migrate_rao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ pub fn migrate_rao<T: Config>() -> Weight {
continue;
}
let owner = SubnetOwner::<T>::get(netuid);
let lock = SubnetLocked::<T>::get(netuid);
let mut lock = SubnetLocked::<T>::get(netuid);

// Subnet 1 doesn't have any lock, so we'll mint 1 TAO for them
if lock == 0 {
lock = 1_000_000_000;
// TODO: Update TotalIssuance properly
}

// Put initial TAO from lock into subnet TAO and produce numerically equal amount of Alpha
// The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO.
Expand All @@ -81,14 +87,8 @@ pub fn migrate_rao<T: Config>() -> Weight {
let remaining_lock = lock.saturating_sub(pool_initial_tao);
// Refund the owner for the remaining lock.
Pallet::<T>::add_balance_to_coldkey_account(&owner, remaining_lock);
SubnetTAO::<T>::insert(netuid, pool_initial_tao); // Set TAO to the lock.

SubnetAlphaIn::<T>::insert(
netuid,
pool_initial_tao.saturating_mul(netuids.len() as u64),
); // Set AlphaIn to the initial alpha distribution.

SubnetAlphaOut::<T>::insert(netuid, 0); // Set zero subnet alpha out.
SubnetTAO::<T>::insert(netuid, pool_initial_tao);
SubnetAlphaIn::<T>::insert(netuid, pool_initial_tao);
SubnetMechanism::<T>::insert(netuid, 1); // Convert to dynamic immediately with initialization.
Tempo::<T>::insert(netuid, DefaultTempo::<T>::get());
// Set the token symbol for this subnet using Self instead of Pallet::<T>
Expand Down
Loading
Loading