Skip to content
Open
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
2 changes: 1 addition & 1 deletion pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl<T: Config> Pallet<T> {
Error::<T>::SubnetNotExists
);

Self::finalize_all_subnet_root_dividends(netuid);
Self::clear_root_claim_root_data(netuid);

// --- Perform the cleanup before removing the network.
T::SwapInterface::dissolve_all_liquidity_providers(netuid)?;
Expand Down
31 changes: 31 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,20 @@ pub mod pallet {
/// ==== Staking + Accounts ====
/// ============================

#[crate::freeze_struct("e453805be107a384")]
#[derive(
Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug, DecodeWithMemTracking,
)]
/// Accounting root claim data per subnet per block.
pub struct PendingRootClaimedData {
/// Total alpha kept this block
pub alpha_kept: AlphaCurrency,
/// Total alpha swapped this block
pub alpha_swapped: AlphaCurrency,
/// Total TAO swapped this block
pub tao_swapped: TaoCurrency,
}

#[derive(
Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug, DecodeWithMemTracking,
)]
Expand Down Expand Up @@ -357,6 +371,12 @@ pub mod pallet {
RootClaimTypeEnum::default()
}

/// Default root claim accounting data.
#[pallet::type_value]
pub fn DefaultRootClaimedData<T: Config>() -> PendingRootClaimedData {
PendingRootClaimedData::default()
}

/// Default number of root claims per claim call.
/// Ideally this is calculated using the number of staking coldkey
/// and the block time.
Expand Down Expand Up @@ -2265,6 +2285,17 @@ pub mod pallet {
#[pallet::storage] // --- Value --> num_root_claim | Number of coldkeys to claim each auto-claim.
pub type NumRootClaim<T: Config> = StorageValue<_, u64, ValueQuery, DefaultNumRootClaim<T>>;

/// --- MAP ( subnet ) --> pending accounting root claimed data. In-block storage only - to be cleaned each block.
#[pallet::storage]
pub type PendingSubnetRootClaimData<T: Config> = StorageMap<
_,
Blake2_128Concat,
NetUid,
PendingRootClaimedData,
ValueQuery,
DefaultRootClaimedData<T>,
>;

/// =============================
/// ==== EVM related storage ====
/// =============================
Expand Down
6 changes: 6 additions & 0 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,5 +479,11 @@ mod events {
/// The amount of alpha distributed
alpha: AlphaCurrency,
},

/// Root claimed data for this block.
RootClaimedData {
/// Root claim data for this block
data: BTreeMap<NetUid, PendingRootClaimedData>,
},
}
}
2 changes: 2 additions & 0 deletions pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ mod hooks {
for _ in StakingOperationRateLimiter::<T>::drain() {
// Clear all entries each block
}

Self::deposit_root_claim_accounting_event();
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
Expand Down
36 changes: 34 additions & 2 deletions pallets/subtensor/src/staking/claim_root.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;
use frame_support::weights::Weight;
use sp_core::Get;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::collections::btree_set::BTreeSet;
use substrate_fixed::types::I96F32;
use subtensor_swap_interface::SwapHandler;
Expand Down Expand Up @@ -191,6 +192,12 @@ impl<T: Config> Pallet<T> {
coldkey,
owed_tao.amount_paid_out.into(),
);

Self::register_root_claim_swapped_alpha(
netuid,
owed_u64.into(),
owed_tao.amount_paid_out,
);
} else
/* Keep */
{
Expand All @@ -201,6 +208,8 @@ impl<T: Config> Pallet<T> {
netuid,
owed_u64.into(),
);

Self::register_root_claim_kept_alpha(netuid, owed_u64.into());
}

// Increase root claimed by owed amount.
Expand All @@ -209,6 +218,18 @@ impl<T: Config> Pallet<T> {
});
}

fn register_root_claim_swapped_alpha(netuid: NetUid, alpha: AlphaCurrency, tao: TaoCurrency) {
PendingSubnetRootClaimData::<T>::mutate(netuid, |data| {
data.alpha_swapped = data.alpha_swapped.saturating_add(alpha);
data.tao_swapped = data.tao_swapped.saturating_add(tao);
});
}
fn register_root_claim_kept_alpha(netuid: NetUid, alpha: AlphaCurrency) {
PendingSubnetRootClaimData::<T>::mutate(netuid, |data| {
data.alpha_kept = data.alpha_kept.saturating_add(alpha);
});
}

fn root_claim_on_subnet_weight(_root_claim_type: RootClaimTypeEnum) -> Weight {
Weight::from_parts(60_000_000, 6987)
.saturating_add(T::DbWeight::get().reads(7_u64))
Expand Down Expand Up @@ -347,6 +368,17 @@ impl<T: Config> Pallet<T> {
weight
}

pub fn deposit_root_claim_accounting_event() {
let root_claim_data = PendingSubnetRootClaimData::<T>::iter().collect::<BTreeMap<_, _>>();

Self::deposit_event(Event::RootClaimedData {
data: root_claim_data,
});

// Subnet Number = 100+
let _ = PendingSubnetRootClaimData::<T>::clear(u32::MAX, None);
}

pub fn change_root_claim_type(coldkey: &T::AccountId, new_type: RootClaimTypeEnum) {
RootClaimType::<T>::insert(coldkey.clone(), new_type.clone());

Expand Down Expand Up @@ -388,8 +420,8 @@ impl<T: Config> Pallet<T> {
RootClaimable::<T>::insert(new_hotkey, dst_root_claimable);
}

/// Claim all root dividends for subnet and remove all associated data.
pub fn finalize_all_subnet_root_dividends(netuid: NetUid) {
/// Remove all root claim data for subnet.
pub fn clear_root_claim_root_data(netuid: NetUid) {
let hotkeys = RootClaimable::<T>::iter_keys().collect::<Vec<_>>();

for hotkey in hotkeys.iter() {
Expand Down
Loading
Loading