Skip to content

Commit

Permalink
Revert changes pulled from main
Browse files Browse the repository at this point in the history
  • Loading branch information
gztensor committed Sep 21, 2024
1 parent 4cbbbf4 commit bf5e375
Show file tree
Hide file tree
Showing 21 changed files with 74 additions and 2,526 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/check-finney.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

- name: Check that spec_version has been bumped
run: |
spec_version=$(PATH=$PATH:$HOME/.cargo/.bin substrate-spec-version ${{ vars.NUCLEUS_ARCHIVE_NODE }} | tr -d '\n')
spec_version=$(PATH=$PATH:$HOME/.cargo/.bin substrate-spec-version wss://entrypoint-finney.opentensor.ai:443 | tr -d '\n')
echo "network spec_version: $spec_version"
: ${spec_version:?bad spec version}
local_spec_version=$(cargo run -p node-subtensor-runtime --bin spec_version | tr -d '\n')
Expand All @@ -50,6 +50,6 @@ jobs:
uses: "paritytech/[email protected]"
with:
runtime-package: "node-subtensor-runtime"
node-uri: ${{ vars.NUCLEUS_ARCHIVE_NODE }}
node-uri: "wss://entrypoint-finney.opentensor.ai:443"
checks: "pre-and-post"
extra-args: "--disable-spec-version-check --no-weight-warnings"
extra-args: "--disable-spec-version-check --no-weight-warnings"
72 changes: 28 additions & 44 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ impl<T: Config> Pallet<T> {
// --- 3. Drain the subnet block emission and accumulate it as subnet emission, which increases until the tempo is reached in #4.
// subnet_blockwise_emission -> subnet_pending_emission
for netuid in subnets.clone().iter() {
if *netuid == 0 {
continue;
}
// --- 3.1 Get the network's block-wise emission amount.
// This value is newly minted TAO which has not reached staking accounts yet.
let subnet_blockwise_emission: u64 = EmissionValues::<T>::get(*netuid);
Expand Down Expand Up @@ -249,20 +246,6 @@ impl<T: Config> Pallet<T> {
});
}

/// Calculates the nonviable stake for a nominator.
/// The nonviable stake is the stake that was added by the nominator since the last emission drain.
/// This stake will not receive emission until the next emission drain.
/// Note: if the stake delta is below zero, we return zero. We don't allow more stake than the nominator has.
pub fn get_nonviable_stake(hotkey: &T::AccountId, nominator: &T::AccountId) -> u64 {
let stake_delta = StakeDeltaSinceLastEmissionDrain::<T>::get(hotkey, nominator);
if stake_delta.is_negative() {
0
} else {
// Should never fail the into, but we handle it anyway.
stake_delta.try_into().unwrap_or(u64::MAX)
}
}

//. --- 4. Drains the accumulated hotkey emission through to the nominators. The hotkey takes a proportion of the emission.
/// The remainder is drained through to the nominators keeping track of the last stake increase event to ensure that the hotkey does not
/// gain more emission than it's stake since the last drain.
Expand All @@ -282,67 +265,68 @@ impl<T: Config> Pallet<T> {
// --- 1.0 Drain the hotkey emission.
PendingdHotkeyEmission::<T>::insert(hotkey, 0);

// --- 2 Update the block value to the current block number.
// --- 2 Retrieve the last time this hotkey's emissions were drained.
let last_emission_drain: u64 = LastHotkeyEmissionDrain::<T>::get(hotkey);

// --- 3 Update the block value to the current block number.
LastHotkeyEmissionDrain::<T>::insert(hotkey, block_number);

// --- 3 Retrieve the total stake for the hotkey from all nominations.
// --- 4 Retrieve the total stake for the hotkey from all nominations.
let total_hotkey_stake: u64 = Self::get_total_stake_for_hotkey(hotkey);

// --- 4 Calculate the emission take for the hotkey.
// --- 5 Calculate the emission take for the hotkey.
let take_proportion: I64F64 = I64F64::from_num(Delegates::<T>::get(hotkey))
.saturating_div(I64F64::from_num(u16::MAX));
let hotkey_take: u64 =
(take_proportion.saturating_mul(I64F64::from_num(emission))).to_num::<u64>();

// --- 5 Compute the remaining emission after deducting the hotkey's take.
// --- 6 Compute the remaining emission after deducting the hotkey's take.
let emission_minus_take: u64 = emission.saturating_sub(hotkey_take);

// --- 6 Calculate the remaining emission after the hotkey's take.
// --- 7 Calculate the remaining emission after the hotkey's take.
let mut remainder: u64 = emission_minus_take;

// --- 7 Iterate over each nominator and get all viable stake.
// --- 8 Iterate over each nominator and get all viable stake.
let mut total_viable_nominator_stake: u64 = total_hotkey_stake;
for (nominator, _) in Stake::<T>::iter_prefix(hotkey) {
let nonviable_nomintaor_stake = Self::get_nonviable_stake(hotkey, &nominator);

total_viable_nominator_stake =
total_viable_nominator_stake.saturating_sub(nonviable_nomintaor_stake);
for (nominator, nominator_stake) in Stake::<T>::iter_prefix(hotkey) {
if LastAddStakeIncrease::<T>::get(hotkey, nominator) > last_emission_drain {
total_viable_nominator_stake =
total_viable_nominator_stake.saturating_sub(nominator_stake);
}
}

// --- 8 Iterate over each nominator.
// --- 9 Iterate over each nominator.
if total_viable_nominator_stake != 0 {
for (nominator, nominator_stake) in Stake::<T>::iter_prefix(hotkey) {
// --- 9 Skip emission for any stake the was added by the nominator since the last emission drain.
// This means the nominator will get emission on existing stake, but not on new stake, until the next emission drain.
let viable_nominator_stake =
nominator_stake.saturating_sub(Self::get_nonviable_stake(hotkey, &nominator));
// --- 10 Check if the stake was manually increased by the user since the last emission drain for this hotkey.
// If it was, skip this nominator as they will not receive their proportion of the emission.
if LastAddStakeIncrease::<T>::get(hotkey, nominator.clone()) > last_emission_drain {
continue;
}

// --- 10 Calculate this nominator's share of the emission.
let nominator_emission: I64F64 = I64F64::from_num(viable_nominator_stake)
// --- 11 Calculate this nominator's share of the emission.
let nominator_emission: I64F64 = I64F64::from_num(emission_minus_take)
.saturating_mul(I64F64::from_num(nominator_stake))
.checked_div(I64F64::from_num(total_viable_nominator_stake))
.unwrap_or(I64F64::from_num(0))
.saturating_mul(I64F64::from_num(emission_minus_take));
.unwrap_or(I64F64::from_num(0));

// --- 11 Increase the stake for the nominator.
// --- 12 Increase the stake for the nominator.
Self::increase_stake_on_coldkey_hotkey_account(
&nominator,
hotkey,
nominator_emission.to_num::<u64>(),
);

// --- 12* Record event and Subtract the nominator's emission from the remainder.
// --- 13* Record event and Subtract the nominator's emission from the remainder.
total_new_tao = total_new_tao.saturating_add(nominator_emission.to_num::<u64>());
remainder = remainder.saturating_sub(nominator_emission.to_num::<u64>());
}
}

// --- 13 Finally, add the stake to the hotkey itself, including its take and the remaining emission.
// --- 14 Finally, add the stake to the hotkey itself, including its take and the remaining emission.
let hotkey_new_tao: u64 = hotkey_take.saturating_add(remainder);
Self::increase_stake_on_hotkey_account(hotkey, hotkey_new_tao);

// --- 14 Reset the stake delta for the hotkey.
let _ = StakeDeltaSinceLastEmissionDrain::<T>::clear_prefix(hotkey, u32::MAX, None);

// --- 15 Record new tao creation event and return the amount created.
total_new_tao = total_new_tao.saturating_add(hotkey_new_tao);
total_new_tao
Expand Down Expand Up @@ -398,4 +382,4 @@ impl<T: Config> Pallet<T> {
let remainder = block_plus_netuid.rem_euclid(tempo_plus_one);
(tempo as u64).saturating_sub(remainder)
}
}
}
37 changes: 10 additions & 27 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,6 @@ pub mod pallet {
0
}
#[pallet::type_value]
/// Default stake delta.
pub fn DefaultStakeDelta<T: Config>() -> i128 {
0
}
#[pallet::type_value]
/// Default stakes per interval.
pub fn DefaultStakesPerInterval<T: Config>() -> (u64, u64) {
(0, 0)
Expand Down Expand Up @@ -775,18 +770,6 @@ pub mod pallet {
DefaultAccountTake<T>,
>;
#[pallet::storage]
/// Map ( hot, cold ) --> stake: i128 | Stake added/removed since last emission drain.
pub type StakeDeltaSinceLastEmissionDrain<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Identity,
T::AccountId,
i128,
ValueQuery,
DefaultStakeDelta<T>,
>;
#[pallet::storage]
/// DMAP ( parent, netuid ) --> Vec<(proportion,child)>
pub type ChildKeys<T: Config> = StorageDoubleMap<
_,
Expand Down Expand Up @@ -1267,7 +1250,7 @@ pub mod pallet {
/// Returns the transaction priority for setting weights.
pub fn get_priority_set_weights(hotkey: &T::AccountId, netuid: u16) -> u64 {
if let Ok(uid) = Self::get_uid_for_net_and_hotkey(netuid, hotkey) {
let _stake = Self::get_stake_for_hotkey_on_subnet(hotkey, netuid);
let _stake = Self::get_total_stake_for_hotkey(hotkey);
let current_block_number: u64 = Self::get_current_block_as_u64();
let default_priority: u64 =
current_block_number.saturating_sub(Self::get_last_update_for_uid(netuid, uid));
Expand All @@ -1277,9 +1260,9 @@ pub mod pallet {
}

/// Is the caller allowed to set weights
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
pub fn check_weights_min_stake(hotkey: &T::AccountId) -> bool {
// Blacklist weights transactions for low stake peers.
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_weights_min_stake()
Self::get_total_stake_for_hotkey(hotkey) >= Self::get_weights_min_stake()
}

/// Helper function to check if register is allowed
Expand Down Expand Up @@ -1372,8 +1355,8 @@ where
Pallet::<T>::get_priority_set_weights(who, netuid)
}

pub fn check_weights_min_stake(who: &T::AccountId, netuid: u16) -> bool {
Pallet::<T>::check_weights_min_stake(who, netuid)
pub fn check_weights_min_stake(who: &T::AccountId) -> bool {
Pallet::<T>::check_weights_min_stake(who)
}
}

Expand Down Expand Up @@ -1411,7 +1394,7 @@ where
) -> TransactionValidity {
match call.is_sub_type() {
Some(Call::commit_weights { netuid, .. }) => {
if Self::check_weights_min_stake(who, *netuid) {
if Self::check_weights_min_stake(who) {
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
Ok(ValidTransaction {
priority,
Expand All @@ -1423,7 +1406,7 @@ where
}
}
Some(Call::reveal_weights { netuid, .. }) => {
if Self::check_weights_min_stake(who, *netuid) {
if Self::check_weights_min_stake(who) {
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
Ok(ValidTransaction {
priority,
Expand All @@ -1435,7 +1418,7 @@ where
}
}
Some(Call::set_weights { netuid, .. }) => {
if Self::check_weights_min_stake(who, *netuid) {
if Self::check_weights_min_stake(who) {
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
Ok(ValidTransaction {
priority,
Expand All @@ -1447,7 +1430,7 @@ where
}
}
Some(Call::set_root_weights { netuid, hotkey, .. }) => {
if Self::check_weights_min_stake(hotkey, *netuid) {
if Self::check_weights_min_stake(hotkey) {
let priority: u64 = Self::get_priority_set_weights(hotkey, *netuid);
Ok(ValidTransaction {
priority,
Expand Down Expand Up @@ -1677,4 +1660,4 @@ impl<T, H, P> CollectiveInterface<T, H, P> for () {
fn add_vote(_: &T, _: H, _: P, _: bool) -> Result<bool, DispatchError> {
Ok(true)
}
}
}
Loading

0 comments on commit bf5e375

Please sign in to comment.