Skip to content

Commit

Permalink
Merge branch 'devnet-ready' into spiigot/fix-commit-onexact-epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnReedV authored Dec 30, 2024
2 parents 129bf3e + 80150de commit 3988e11
Show file tree
Hide file tree
Showing 20 changed files with 422 additions and 435 deletions.
233 changes: 72 additions & 161 deletions Cargo.lock

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,32 +166,32 @@ sc-network-sync = { git = "https://github.com/paritytech/polkadot-sdk.git", tag
substrate-prometheus-endpoint = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }

# Frontier
fp-evm = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fp-rpc = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fp-self-contained = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false, features = [
fp-evm = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fp-rpc = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fp-self-contained = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false, features = [
"serde",
] }
fp-account = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fc-storage = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fc-db = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fc-consensus = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fp-consensus = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fp-dynamic-fee = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fc-api = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fc-rpc = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fc-rpc-core = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fc-mapping-sync = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
fp-account = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-storage = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-db = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-consensus = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fp-consensus = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fp-dynamic-fee = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-api = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-rpc = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-rpc-core = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
fc-mapping-sync = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }

# Frontier FRAME
pallet-base-fee = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
pallet-dynamic-fee = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
pallet-ethereum = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
pallet-evm = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
pallet-evm-chain-id = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
pallet-evm-precompile-modexp = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
pallet-evm-precompile-sha3fips = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
pallet-evm-precompile-simple = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
pallet-hotfix-sufficients = { git = "https://github.com/gztensor/frontier", rev = "b8e3025", default-features = false }
pallet-base-fee = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-dynamic-fee = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-ethereum = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm-chain-id = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm-precompile-modexp = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm-precompile-sha3fips = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-evm-precompile-simple = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }
pallet-hotfix-sufficients = { git = "https://github.com/gztensor/frontier", rev = "b9c606f9", default-features = false }

#DRAND
pallet-drand = { path = "pallets/drand", default-features = false }
Expand Down
4 changes: 3 additions & 1 deletion chainspecs/plain_spec_finney.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion chainspecs/raw_spec_finney.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion nakamoto_gen.json

This file was deleted.

1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ clap = { workspace = true, features = ["derive"] }
futures = { workspace = true, features = ["thread-pool"] }
scale-codec = { workspace = true }
serde = { workspace = true, features = ["derive"] }
hex = { workspace = true }

# Storage import
memmap2 = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions node/src/chain_spec/code_substitute_2585476.txt

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions node/src/chain_spec/finney.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(clippy::unwrap_used)]

use super::*;
use hex::FromHex;

pub fn finney_mainnet_config() -> Result<ChainSpec, String> {
let path: PathBuf = std::path::PathBuf::from("./snapshot.json");
Expand Down Expand Up @@ -69,7 +70,7 @@ pub fn finney_mainnet_config() -> Result<ChainSpec, String> {
properties.insert("tokenDecimals".into(), 9.into());
properties.insert("ss58Format".into(), 42.into());

Ok(ChainSpec::builder(
let chain_spec = ChainSpec::builder(
wasm_binary,
Extensions {
bad_blocks: Some(HashSet::new()),
Expand Down Expand Up @@ -181,7 +182,25 @@ pub fn finney_mainnet_config() -> Result<ChainSpec, String> {
balances_issuance,
))
.with_properties(properties)
.build())
.build();

// Load and set the code substitute to avoid archive node sync panic
// See <https://github.com/opentensor/subtensor/pull/1051>
//
// Need to do it in this hacky way because the ChainSpec builder doesn't support setting it
let code_substitute_2585476_hex = include_bytes!("code_substitute_2585476.txt");
let chain_spec_json = chain_spec.as_json(false).unwrap();
let mut chain_spec_json = serde_json::from_str(&chain_spec_json).unwrap();
sc_chain_spec::set_code_substitute_in_json_chain_spec(
&mut chain_spec_json,
Vec::from_hex(code_substitute_2585476_hex)
.unwrap()
.as_slice(),
2585476,
);
let chain_spec_bytes = chain_spec_json.to_string().into_bytes();
let chain_spec = ChainSpec::from_json_bytes(chain_spec_bytes).unwrap();
Ok(chain_spec)
}

// Configure initial storage state for FRAME modules.
Expand Down
6 changes: 5 additions & 1 deletion pallets/admin-utils/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Benchmarking setup
#![cfg(feature = "runtime-benchmarks")]
#![allow(clippy::arithmetic_side_effects)]
use super::*;

extern crate alloc;
use alloc::vec::Vec;

#[allow(unused)]
use crate::Pallet as AdminUtils;
Expand All @@ -10,6 +12,8 @@ use frame_benchmarking::v2::*;
use frame_support::BoundedVec;
use frame_system::RawOrigin;

use super::*;

#[benchmarks]
mod benchmarks {
use super::*;
Expand Down
28 changes: 14 additions & 14 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ 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 || !Self::is_registration_allowed(*netuid) {
if *netuid == 0 || !Self::get_network_registration_allowed(*netuid) {
continue;
}
// --- 3.1 Get the network's block-wise emission amount.
Expand Down Expand Up @@ -105,7 +105,10 @@ impl<T: Config> Pallet<T> {
};
}

// --- 4.3 Drain the subnet emission.
// 4.3 Apply pending childkeys of this subnet for the next epoch
Self::do_set_pending_children(*netuid);

// --- 4.4 Drain the subnet emission.
let mut subnet_emission: u64 = PendingEmission::<T>::get(*netuid);
PendingEmission::<T>::insert(*netuid, 0);
log::debug!(
Expand All @@ -114,39 +117,39 @@ impl<T: Config> Pallet<T> {
subnet_emission
);

// --- 4.4 Set last step counter.
// --- 4.5 Set last step counter.
Self::set_blocks_since_last_step(*netuid, 0);
Self::set_last_mechanism_step_block(*netuid, current_block);

if *netuid == 0 || !Self::is_registration_allowed(*netuid) {
if *netuid == 0 || !Self::get_network_registration_allowed(*netuid) {
// Skip netuid 0 payouts
continue;
}

// --- 4.5 Distribute owner take.
// --- 4.6 Distribute owner take.
if SubnetOwner::<T>::contains_key(netuid) {
// Does the subnet have an owner?

// --- 4.5.1 Compute the subnet owner cut.
// --- 4.6.1 Compute the subnet owner cut.
let owner_cut: I96F32 = I96F32::from_num(subnet_emission).saturating_mul(
I96F32::from_num(Self::get_subnet_owner_cut())
.saturating_div(I96F32::from_num(u16::MAX)),
);

// --- 4.5.2 Remove the cut from the subnet emission
// --- 4.6.2 Remove the cut from the subnet emission
subnet_emission = subnet_emission.saturating_sub(owner_cut.to_num::<u64>());

// --- 4.5.3 Add the cut to the balance of the owner
// --- 4.6.3 Add the cut to the balance of the owner
Self::add_balance_to_coldkey_account(
&Self::get_subnet_owner(*netuid),
owner_cut.to_num::<u64>(),
);

// --- 4.5.4 Increase total issuance on the chain.
// --- 4.6.4 Increase total issuance on the chain.
Self::coinbase(owner_cut.to_num::<u64>());
}

// 4.6 Pass emission through epoch() --> hotkey emission.
// 4.7 Pass emission through epoch() --> hotkey emission.
let hotkey_emission: Vec<(T::AccountId, u64, u64)> =
Self::epoch(*netuid, subnet_emission);
log::debug!(
Expand All @@ -155,7 +158,7 @@ impl<T: Config> Pallet<T> {
hotkey_emission
);

// 4.7 Accumulate the tuples on hotkeys:
// 4.8 Accumulate the tuples on hotkeys:
for (hotkey, mining_emission, validator_emission) in hotkey_emission {
// 4.8 Accumulate the emission on the hotkey and parent hotkeys.
Self::accumulate_hotkey_emission(
Expand All @@ -166,9 +169,6 @@ impl<T: Config> Pallet<T> {
);
log::debug!("Accumulated emissions on hotkey {:?} for netuid {:?}: mining {:?}, validator {:?}", hotkey, *netuid, mining_emission, validator_emission);
}

// 4.5 Apply pending childkeys of this subnet for the next epoch
Self::do_set_pending_children(*netuid);
} else {
// No epoch, increase blocks since last step and continue
Self::set_blocks_since_last_step(
Expand Down
4 changes: 1 addition & 3 deletions pallets/subtensor/src/macros/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ mod errors {
/// A transactor exceeded the rate limit for setting or swapping hotkey.
HotKeySetTxRateLimitExceeded,
/// A transactor exceeded the rate limit for staking.
StakeRateLimitExceeded,
/// A transactor exceeded the rate limit for unstaking.
UnstakeRateLimitExceeded,
StakingRateLimitExceeded,
/// Registration is disabled.
SubNetRegistrationDisabled,
/// The number of registration attempts exceeded the allowed number in the interval.
Expand Down
29 changes: 8 additions & 21 deletions pallets/subtensor/src/staking/add_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@ impl<T: Config> Pallet<T> {
Error::<T>::HotKeyNotDelegateAndSignerNotOwnHotKey
);

// Ensure we don't exceed stake rate limit
let stakes_this_interval =
Self::get_stakes_this_interval_for_coldkey_hotkey(&coldkey, &hotkey);
ensure!(
stakes_this_interval < Self::get_target_stakes_per_interval(),
Error::<T>::StakeRateLimitExceeded
);

// Track this addition in the stake delta.
StakeDeltaSinceLastEmissionDrain::<T>::mutate(&hotkey, &coldkey, |stake_delta| {
*stake_delta = stake_delta.saturating_add_unsigned(stake_to_be_added as u128);
});

// If coldkey is not owner of the hotkey, it's a nomination stake.
if !Self::coldkey_owns_hotkey(&coldkey, &hotkey) {
let total_stake_after_add =
Expand All @@ -86,24 +73,24 @@ impl<T: Config> Pallet<T> {
);
}

Self::try_increase_staking_counter(&coldkey, &hotkey)?;

// Ensure the remove operation from the coldkey is a success.
let actual_amount_to_stake =
Self::remove_balance_from_coldkey_account(&coldkey, stake_to_be_added)?;

// If we reach here, add the balance to the hotkey.
Self::increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, actual_amount_to_stake);

// Track this addition in the stake delta.
StakeDeltaSinceLastEmissionDrain::<T>::mutate(&hotkey, &coldkey, |stake_delta| {
*stake_delta = stake_delta.saturating_add_unsigned(stake_to_be_added as u128);
});

// Set last block for rate limiting
let block: u64 = Self::get_current_block_as_u64();
let block = Self::get_current_block_as_u64();
Self::set_last_tx_block(&coldkey, block);

// Emit the staking event.
Self::set_stakes_this_interval_for_coldkey_hotkey(
&coldkey,
&hotkey,
stakes_this_interval.saturating_add(1),
block,
);
log::debug!(
"StakeAdded( hotkey:{:?}, stake_to_be_added:{:?} )",
hotkey,
Expand Down
36 changes: 0 additions & 36 deletions pallets/subtensor/src/staking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,42 +59,6 @@ impl<T: Config> Pallet<T> {
Stake::<T>::get(hotkey, coldkey)
}

// Retrieves the total stakes for a given hotkey (account ID) for the current staking interval.
pub fn get_stakes_this_interval_for_coldkey_hotkey(
coldkey: &T::AccountId,
hotkey: &T::AccountId,
) -> u64 {
// Retrieve the configured stake interval duration from storage.
let stake_interval = StakeInterval::<T>::get();

// Obtain the current block number as an unsigned 64-bit integer.
let current_block = Self::get_current_block_as_u64();

// Fetch the total stakes and the last block number when stakes were made for the hotkey.
let (stakes, block_last_staked_at) =
TotalHotkeyColdkeyStakesThisInterval::<T>::get(coldkey, hotkey);

// Calculate the block number after which the stakes for the hotkey should be reset.
let block_to_reset_after = block_last_staked_at.saturating_add(stake_interval);

// If the current block number is beyond the reset point,
// it indicates the end of the staking interval for the hotkey.
if block_to_reset_after <= current_block {
// Reset the stakes for this hotkey for the current interval.
Self::set_stakes_this_interval_for_coldkey_hotkey(
coldkey,
hotkey,
0,
block_last_staked_at,
);
// Return 0 as the stake amount since we've just reset the stakes.
return 0;
}

// If the staking interval has not yet ended, return the current stake amount.
stakes
}

pub fn get_target_stakes_per_interval() -> u64 {
TargetStakesPerInterval::<T>::get()
}
Expand Down
16 changes: 2 additions & 14 deletions pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ impl<T: Config> Pallet<T> {
Error::<T>::NotEnoughStakeToWithdraw
);

// Ensure we don't exceed stake rate limit
let unstakes_this_interval =
Self::get_stakes_this_interval_for_coldkey_hotkey(&coldkey, &hotkey);
ensure!(
unstakes_this_interval < Self::get_target_stakes_per_interval(),
Error::<T>::UnstakeRateLimitExceeded
);
Self::try_increase_staking_counter(&coldkey, &hotkey)?;

// We remove the balance from the hotkey.
Self::decrease_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_to_be_removed);
Expand All @@ -98,16 +92,10 @@ impl<T: Config> Pallet<T> {
}

// Set last block for rate limiting
let block: u64 = Self::get_current_block_as_u64();
let block = Self::get_current_block_as_u64();
Self::set_last_tx_block(&coldkey, block);

// Emit the unstaking event.
Self::set_stakes_this_interval_for_coldkey_hotkey(
&coldkey,
&hotkey,
unstakes_this_interval.saturating_add(1),
block,
);
log::debug!(
"StakeRemoved( hotkey:{:?}, stake_to_be_removed:{:?} )",
hotkey,
Expand Down
4 changes: 3 additions & 1 deletion pallets/subtensor/src/staking/set_children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ impl<T: Config> Pallet<T> {
}

// Check that the parent key has at least the minimum own stake
// if children vector is not empty
// (checking with check_weights_min_stake wouldn't work because it considers
// grandparent stake in this case)
ensure!(
Self::get_total_stake_for_hotkey(&hotkey) >= StakeThreshold::<T>::get(),
children.is_empty()
|| Self::get_total_stake_for_hotkey(&hotkey) >= StakeThreshold::<T>::get(),
Error::<T>::NotEnoughStakeToSetChildkeys
);

Expand Down
Loading

0 comments on commit 3988e11

Please sign in to comment.