Skip to content
Draft
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
275 changes: 265 additions & 10 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pallet-subtensor-swap-runtime-api = { path = "pallets/swap/runtime-api", default
pallet-subtensor-swap-rpc = { path = "pallets/swap/rpc", default-features = false }
procedural-fork = { path = "support/procedural-fork", default-features = false }
safe-math = { path = "primitives/safe-math", default-features = false }
safe-bigmath = { rev = "013c499", package = "safe-bigmath", default-features = false, git = "https://github.com/sam0x17/safe-bigmath" }
share-pool = { path = "primitives/share-pool", default-features = false }
subtensor-macros = { path = "support/macros", default-features = false }
subtensor-custom-rpc = { default-features = false, path = "pallets/subtensor/rpc" }
Expand All @@ -82,6 +83,7 @@ hex = { version = "0.4", default-features = false }
hex-literal = "0.4.1"
jsonrpsee = { version = "0.24.9", default-features = false }
libsecp256k1 = { version = "0.7.2", default-features = false }
lencode = "0.1.6"
log = { version = "0.4.21", default-features = false }
memmap2 = "0.9.8"
ndarray = { version = "0.16.1", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub trait BalanceOps<AccountId> {
hotkey: &AccountId,
netuid: NetUid,
alpha: AlphaCurrency,
) -> Result<AlphaCurrency, DispatchError>;
) -> Result<(), DispatchError>;
}

pub mod time {
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,15 @@ impl<T: Config> Pallet<T> {
log::debug!(
"owner_hotkey: {owner_hotkey:?} owner_coldkey: {owner_coldkey:?}, owner_cut: {owner_cut:?}"
);
let real_owner_cut = Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
&owner_hotkey,
&owner_coldkey,
netuid,
owner_cut,
);
// If the subnet is leased, notify the lease logic that owner cut has been distributed.
if let Some(lease_id) = SubnetUidToLeaseId::<T>::get(netuid) {
Self::distribute_leased_network_dividends(lease_id, real_owner_cut);
Self::distribute_leased_network_dividends(lease_id, owner_cut);
}
}

Expand Down
33 changes: 32 additions & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub mod pallet {
use frame_system::pallet_prelude::*;
use pallet_drand::types::RoundNumber;
use runtime_common::prod_or_fast;
use share_pool::SafeFloatSerializable;
use sp_core::{ConstU32, H160, H256};
use sp_runtime::traits::{Dispatchable, TrailingZeroInput};
use sp_std::collections::btree_map::BTreeMap;
Expand Down Expand Up @@ -1437,6 +1438,31 @@ pub mod pallet {
ValueQuery,
>;

/// DMAP ( hot, netuid ) --> total_alpha_shares | Returns the number of alpha shares for a hotkey on a subnet, stores bigmath vector.
#[pallet::storage]
pub type TotalHotkeySharesV2<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId, // hot
Identity,
NetUid, // subnet
SafeFloatSerializable, // Hotkey shares in unlimited precision
ValueQuery,
>;

/// --- NMAP ( hot, cold, netuid ) --> alpha | Returns the alpha shares for a hotkey, coldkey, netuid triplet, stores bigmath vector.
#[pallet::storage]
pub type AlphaV2<T: Config> = StorageNMap<
_,
(
NMapKey<Blake2_128Concat, T::AccountId>, // hot
NMapKey<Blake2_128Concat, T::AccountId>, // cold
NMapKey<Identity, NetUid>, // subnet
),
SafeFloatSerializable, // Shares in unlimited precision
ValueQuery,
>;

/// Contains last Alpha storage map key to iterate (check first)
#[pallet::storage]
pub type AlphaMapLastKey<T: Config> =
Expand Down Expand Up @@ -2635,12 +2661,17 @@ impl<T: Config + pallet_balances::Config<Balance = u64>>
hotkey: &T::AccountId,
netuid: NetUid,
alpha: AlphaCurrency,
) -> Result<AlphaCurrency, DispatchError> {
) -> Result<(), DispatchError> {
ensure!(
Self::hotkey_account_exists(hotkey),
Error::<T>::HotKeyAccountNotExists
);

ensure!(
Self::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid) >= alpha,
Error::<T>::InsufficientBalance
);

// Decrese alpha out counter
SubnetAlphaOut::<T>::mutate(netuid, |total| {
*total = total.saturating_sub(alpha);
Expand Down
9 changes: 7 additions & 2 deletions pallets/subtensor/src/rpc_info/delegate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use substrate_fixed::types::U64F64;
extern crate alloc;
use alloc::collections::BTreeMap;
use codec::Compact;
use share_pool::SafeFloat;
use subtensor_runtime_common::{AlphaCurrency, NetUid};

#[freeze_struct("1fafc4fcf28cba7a")]
Expand Down Expand Up @@ -65,8 +66,12 @@ impl<T: Config> Pallet<T> {
alpha_share_pools.push(alpha_share_pool);
}

for ((nominator, netuid), alpha_stake) in Alpha::<T>::iter_prefix((delegate.clone(),)) {
if alpha_stake == 0 {
for ((nominator, netuid), alpha_stake_float_serializable) in
AlphaV2::<T>::iter_prefix((delegate.clone(),))
{
let alpha_stake = SafeFloat::from(&alpha_stake_float_serializable);

if alpha_stake.is_zero() {
continue;
}

Expand Down
30 changes: 6 additions & 24 deletions pallets/subtensor/src/staking/recycle_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,12 @@ impl<T: Config> Pallet<T> {
);

// Deduct from the coldkey's stake.
let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey, &coldkey, netuid, amount,
);

ensure!(actual_alpha_decrease <= amount, Error::<T>::PrecisionLoss);
Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid, amount);

// Recycle means we should decrease the alpha issuance tracker.
Self::recycle_subnet_alpha(netuid, actual_alpha_decrease);
Self::recycle_subnet_alpha(netuid, amount);

Self::deposit_event(Event::AlphaRecycled(
coldkey,
hotkey,
actual_alpha_decrease,
netuid,
));
Self::deposit_event(Event::AlphaRecycled(coldkey, hotkey, amount, netuid));

Ok(())
}
Expand Down Expand Up @@ -118,21 +109,12 @@ impl<T: Config> Pallet<T> {
);

// Deduct from the coldkey's stake.
let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey, &coldkey, netuid, amount,
);

ensure!(actual_alpha_decrease <= amount, Error::<T>::PrecisionLoss);
Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid, amount);

Self::burn_subnet_alpha(netuid, actual_alpha_decrease);
Self::burn_subnet_alpha(netuid, amount);

// Deposit event
Self::deposit_event(Event::AlphaBurned(
coldkey,
hotkey,
actual_alpha_decrease,
netuid,
));
Self::deposit_event(Event::AlphaBurned(coldkey, hotkey, amount, netuid));

Ok(())
}
Expand Down
Loading
Loading