Skip to content

Commit

Permalink
fix: fast-blocks
Browse files Browse the repository at this point in the history
Changes:
- Fix InvalidWorkBlock in fast-blocks mode
- Fix HotKeyAlreadyRegisteredInSubNet error of registration key on root
- Fix fast-blocks config params
- Fix runtime-version of 100
- Fix SubnetLimit to 1024
- Fix FirstReservedNetuids to 1000
- Add DefaultStakeInterval & DefaultWeightsSetRateLimit into runtime config
- Update local spec
  • Loading branch information
MohsenNz committed Dec 26, 2024
1 parent 3d67dee commit 1596fa9
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 39 deletions.
8 changes: 8 additions & 0 deletions node/src/chain_spec/localnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::*;
#[rustfmt::skip]
pub fn localnet_config(single_authority: bool) -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
let account = |x| Ss58Codec::from_ss58check(x).unwrap();

Ok(ChainSpec::builder(
wasm_binary,
Expand Down Expand Up @@ -48,6 +49,13 @@ pub fn localnet_config(single_authority: bool) -> Result<ChainSpec, String> {
(sr25519_account("Dave") , 2000_000_000_000u128),
(sr25519_account("Ferdie") , 2000_000_000_000u128),
(sr25519_account("Eve") , 2000_000_000_000u128),

// Mohsen's local owner coldkey
(account("5CwP1MPnA3vHmqmvzPampZmAg1m7FCaSJ3PnPpCy4wmruAFq"), 5000_000_000_000u128),
// Mohsen's local miner coldkey
(account("5GbcFmvaUTmfpL6MFfh9Xz7X6bj5cKBXqubgNHhKFiNmwFhy"), 5000_000_000_000u128),
// Mohsen's local validator coldkey
(account("5HTho5p3HQyuvni13e6qhc5Q8UpQZo6RNfgoxAmo3gcfRrJ8"), 5000_000_000_000u128),
],
// trimvirates
vec![
Expand Down
26 changes: 15 additions & 11 deletions pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,9 @@ impl<T: Config> Pallet<T> {
Error::<T>::HotKeyNotRegisteredInSubNet
);

// --- 2. Verify the hotkey is NOT already a member of the Senate.
// --- 2. Verify the hotkey is NOT already a member of the delegate senate.
ensure!(
!T::SenateMembers::is_member(hotkey),
!T::SenateMembers::is_delegate_member(hotkey),
Error::<T>::HotKeyAlreadyRegisteredInSubNet
);

Expand Down Expand Up @@ -978,29 +978,28 @@ impl<T: Config> Pallet<T> {
let first_resv_neuids = FirstReservedNetuids::<T>::get();

let (netuid_from, p) = if is_reserved {
let total_reserved_subnets = ReservedSubnetCount::<T>::get();
let resv_subnet_count = ReservedSubnetCount::<T>::get();

log::debug!(
"reserved subnet count: {:?}\nmax reserved subnets: {:?}",
total_reserved_subnets,
resv_subnet_count,
first_resv_neuids
);

(1, total_reserved_subnets < first_resv_neuids)
(1, resv_subnet_count < first_resv_neuids)
} else {
let max_user_subnets = Self::get_max_subnets();
let total_user_subnets = UserSubnetCount::<T>::get();
let user_subnet_count = UserSubnetCount::<T>::get();

log::debug!(
"user subnet count: {:?}\nmax user subnets: {:?}",
total_user_subnets,
user_subnet_count,
max_user_subnets
);

(first_resv_neuids + 1, total_user_subnets < max_user_subnets)
(first_resv_neuids + 1, user_subnet_count < max_user_subnets)
};

// if Self::get_num_subnets().saturating_sub(1) < Self::get_max_subnets() {
if p {
let mut next_available_netuid = netuid_from;
loop {
Expand Down Expand Up @@ -1034,8 +1033,13 @@ impl<T: Config> Pallet<T> {
Self::set_network_last_lock(actual_lock_amount);

// --- 6. Set initial and custom parameters for the network.
Self::init_new_network(netuid_to_register, 360);
log::debug!("init_new_network: {:?}", netuid_to_register);
let tempo = T::InitialTempo::get();
Self::init_new_network(netuid_to_register, tempo);
log::debug!(
"init_new_network: netuid: {:?}, tempo: {:?}",
netuid_to_register,
tempo
);

// --- 7. Add the identity if it exists
if let Some(identity_value) = identity {
Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl<T: Config> Pallet<T> {
for netuid in subnets.clone().iter() {
// --- 4.1 Check to see if the subnet should run its epoch.
if Self::should_run_epoch(*netuid, current_block) {
log::debug!("Starting Tempo of subnet: {:?}", *netuid);
// --- 4.2 Drain the subnet emission.
let mut subnet_emission: u64 = PendingEmission::<T>::get(*netuid);
PendingEmission::<T>::insert(*netuid, 0);
Expand Down Expand Up @@ -407,6 +408,7 @@ impl<T: Config> Pallet<T> {
/// Special case: tempo = 0, the network never runs.
///
pub fn blocks_until_next_epoch(netuid: u16, tempo: u16, block_number: u64) -> u64 {
log::debug!("blocks_until_next_epoch: netuid: {netuid}, tempo: {tempo}, block_number: {block_number}");
if tempo == 0 {
return u64::MAX;
}
Expand Down
6 changes: 6 additions & 0 deletions pallets/subtensor/src/epoch/run_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ impl<T: Config> Pallet<T> {
///
#[allow(clippy::indexing_slicing)]
pub fn epoch(netuid: u16, rao_emission: u64) -> Vec<(T::AccountId, u64, u64)> {
log::debug!("epoch started for netuid: {netuid}");

// Get subnetwork size.
let n: u16 = Self::get_subnetwork_n(netuid);
log::trace!("Number of Neurons in Network: {:?}", n);
Expand Down Expand Up @@ -783,6 +785,10 @@ impl<T: Config> Pallet<T> {
}
});

log::debug!("server_emission: {:?}", server_emission);
log::debug!("validator_emission: {:?}", validator_emission);
log::debug!("epoch ending");

// Emission tuples ( hotkeys, server_emission, validator_emission )
hotkeys
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub mod pallet {
#[pallet::type_value]
/// Default stake interval.
pub fn DefaultStakeInterval<T: Config>() -> u64 {
360
T::DefaultStakeInterval::get()
}
#[pallet::type_value]
/// Default account linkage
Expand Down Expand Up @@ -490,7 +490,7 @@ pub mod pallet {
#[pallet::type_value]
/// Default value for weights set rate limit.
pub fn DefaultWeightsSetRateLimit<T: Config>() -> u64 {
100
T::DefaultWeightsSetRateLimit::get()
}
#[pallet::type_value]
/// Default block number at registration.
Expand Down
6 changes: 6 additions & 0 deletions pallets/subtensor/src/macros/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,11 @@ mod config {
/// Root subnet Tempo
#[pallet::constant]
type RootTempo: Get<u16>;
/// Default Stake Interval
#[pallet::constant]
type DefaultStakeInterval: Get<u64>;
/// Default Weights Set Rate Limit
#[pallet::constant]
type DefaultWeightsSetRateLimit: Get<u64>;
}
}
20 changes: 12 additions & 8 deletions pallets/subtensor/src/subnets/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,12 @@ impl<T: Config> Pallet<T> {
block_number <= current_block_number,
Error::<T>::InvalidWorkBlock
);
ensure!(
current_block_number.saturating_sub(block_number) < 3,
Error::<T>::InvalidWorkBlock
);
if !cfg!(feature = "fast-blocks") {
ensure!(
current_block_number.saturating_sub(block_number) < 3,
Error::<T>::InvalidWorkBlock
);
}

// --- 8. Ensure the supplied work passes the difficulty.
let difficulty: U256 = Self::get_difficulty(netuid);
Expand Down Expand Up @@ -375,10 +377,12 @@ impl<T: Config> Pallet<T> {
block_number <= current_block_number,
Error::<T>::InvalidWorkBlock
);
ensure!(
current_block_number.saturating_sub(block_number) < 3,
Error::<T>::InvalidWorkBlock
);
if !cfg!(feature = "fast-blocks") {
ensure!(
current_block_number.saturating_sub(block_number) < 3,
Error::<T>::InvalidWorkBlock
);
}

// --- 3. Ensure the supplied work passes the difficulty.
let difficulty: U256 = if !cfg!(feature = "fast-blocks") {
Expand Down
1 change: 0 additions & 1 deletion pallets/subtensor/tests/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2564,7 +2564,6 @@ fn test_compute_ema_bonds_with_liquid_alpha_sparse_empty() {
fn test_get_set_alpha() {
new_test_ext(1).execute_with(|| {
let netuid: u16 = unid(1);
log::debug!("netuid: {:?}", netuid); // debug
let alpha_low: u16 = 12_u16;
let alpha_high: u16 = u16::MAX - 10;

Expand Down
55 changes: 38 additions & 17 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 212,
spec_version: 100,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -197,16 +197,16 @@ pub const MILLISECS_PER_BLOCK: u64 = 12000;

/// Fast blocks for development
#[cfg(feature = "fast-blocks")]
pub const MILLISECS_PER_BLOCK: u64 = 1000;
pub const MILLISECS_PER_BLOCK: u64 = 5000;

// NOTE: Currently it is not possible to change the slot duration after the chain has started.
// Attempting to do so will brick block production.
pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;

// Time is measured by number of blocks.
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;
pub const HOURS: BlockNumber = 60 * MINUTES;
pub const DAYS: BlockNumber = 24 * HOURS;

pub const MAXIMUM_BLOCK_WEIGHT: Weight =
Weight::from_parts(4u64 * WEIGHT_REF_TIME_PER_SECOND, u64::MAX);
Expand Down Expand Up @@ -873,8 +873,13 @@ parameter_types! {
pub const MaxCommitFields : u32 = 1;
pub const CommitmentInitialDeposit : Balance = 0; // Free
pub const CommitmentFieldDeposit : Balance = 0; // Free
pub const CommitmentRateLimit : BlockNumber = 100; // Allow commitment every 100 blocks
pub const CommitmentRateLimit : BlockNumber = COMMITMENT_RATE_LIMIT; // Allow commitment every N blocks
}
#[cfg(not(feature = "fast-blocks"))]
pub const COMMITMENT_RATE_LIMIT: BlockNumber = 100;
#[cfg(feature = "fast-blocks")]
pub const COMMITMENT_RATE_LIMIT: BlockNumber = 5;

pub struct AllowCommitments;
impl CanCommit<AccountId> for AllowCommitments {
#[cfg(not(feature = "runtime-benchmarks"))]
Expand Down Expand Up @@ -931,7 +936,7 @@ impl pallet_subtensor::Config for Runtime {
type InitialMaxBurn = SubtensorInitialMaxBurn;
type InitialMinBurn = SubtensorInitialMinBurn;
// --
type InitialTxRateLimit = SubtensorInitialTxRateLimit;
type InitialTxRateLimit = ConstU64<SUBTENSOR_INITIAL_TX_RATE_LIMIT>;
type InitialTxDelegateTakeRateLimit = SubtensorInitialTxDelegateTakeRateLimit;
type InitialTxChildKeyTakeRateLimit = SubtensorInitialTxChildKeyTakeRateLimit;
type InitialMaxChildKeyTake = SubtensorInitialMaxChildKeyTake;
Expand All @@ -943,9 +948,9 @@ impl pallet_subtensor::Config for Runtime {
type InitialNetworkLockReductionInterval = SubtensorInitialNetworkLockReductionInterval;
type InitialSubnetOwnerCut = SubtensorInitialSubnetOwnerCut;
type InitialSubnetLimit = SubtensorInitialSubnetLimit;
type InitialFirstReservedNetuids = ConstU16<100>;
type InitialFirstReservedNetuids = ConstU16<FIRST_RESERVED_NETUIDS>;
type InitialNetworkRateLimit = SubtensorInitialNetworkRateLimit;
type InitialTargetStakesPerInterval = SubtensorInitialTargetStakesPerInterval;
type InitialTargetStakesPerInterval = ConstU64<1>;
type KeySwapCost = SubtensorInitialKeySwapCost;
type AlphaHigh = InitialAlphaHigh;
type AlphaLow = InitialAlphaLow;
Expand All @@ -955,7 +960,9 @@ impl pallet_subtensor::Config for Runtime {
type Preimages = Preimage;
type InitialColdkeySwapScheduleDuration = InitialColdkeySwapScheduleDuration;
type InitialDissolveNetworkScheduleDuration = InitialDissolveNetworkScheduleDuration;
type RootTempo = ConstU16<100>;
type RootTempo = ConstU16<ROOT_TEMPO>;
type DefaultStakeInterval = ConstU64<DEFAULT_STAKE_INTERVAL>;
type DefaultWeightsSetRateLimit = ConstU64<DEFAULT_WEIGHTS_SET_RATE_LIMIT>;
}
parameter_types! {
pub const SubtensorInitialRho : u16 = 10;
Expand All @@ -968,7 +975,7 @@ parameter_types! {
pub const SubtensorInitialValidatorPruneLen : u64 = 1;
pub const SubtensorInitialScalingLawPower : u16 = 50; // 0.5
pub const SubtensorInitialMaxAllowedValidators : u16 = 128;
pub const SubtensorInitialTempo : u16 = INITIAL_SUBNET_TEMPO;
pub const SubtensorInitialTempo : u16 = DEFAULT_SUBNET_TEMPO;
pub const SubtensorInitialDifficulty : u64 = 10_000_000;
pub const SubtensorInitialAdjustmentInterval : u16 = 100;
pub const SubtensorInitialAdjustmentAlpha : u64 = 0; // no weight to previous value.
Expand All @@ -990,7 +997,6 @@ parameter_types! {
pub const SubtensorInitialBurn : u64 = 1_000_000_000; // 1 tao
pub const SubtensorInitialMinBurn : u64 = 1_000_000_000; // 1 tao
pub const SubtensorInitialMaxBurn : u64 = 100_000_000_000; // 100 tao
pub const SubtensorInitialTxRateLimit : u64 = 1000;
pub const SubtensorInitialTxDelegateTakeRateLimit : u64 = 216000; // 30 days at 12 seconds per block
pub const SubtensorInitialTxChildKeyTakeRateLimit : u64 = INITIAL_CHILDKEY_TAKE_RATELIMIT;
pub const SubtensorInitialRAORecycledForRegistration : u64 = 0; // 0 rao
Expand All @@ -999,10 +1005,9 @@ parameter_types! {
pub const SubtensorInitialMinAllowedUids : u16 = 128;
pub const SubtensorInitialMinLockCost : u64 = 1_000_000_000_000; // 1000 TAO
pub const SubtensorInitialSubnetOwnerCut : u16 = 11_796; // 18 percent
pub const SubtensorInitialSubnetLimit : u16 = 20; // bittensor_value was 12;
pub const SubtensorInitialSubnetLimit : u16 = 1024;
pub const SubtensorInitialNetworkLockReductionInterval : u64 = 14 * 7200;
pub const SubtensorInitialNetworkRateLimit : u64 = 7200;
pub const SubtensorInitialTargetStakesPerInterval : u16 = 1;
pub const SubtensorInitialKeySwapCost : u64 = 1_000_000_000;
pub const InitialAlphaHigh : u16 = 58982; // Represents 0.9 as per the production default
pub const InitialAlphaLow : u16 = 45875; // Represents 0.7 as per the production default
Expand All @@ -1013,17 +1018,33 @@ parameter_types! {
pub const InitialDissolveNetworkScheduleDuration : BlockNumber = 5 * DAYS;
}
#[cfg(not(feature = "fast-blocks"))]
pub const INITIAL_SUBNET_TEMPO: u16 = 99;

pub const ROOT_TEMPO: u16 = 20 * MINUTES as u16; //100;
#[cfg(feature = "fast-blocks")]
pub const INITIAL_SUBNET_TEMPO: u16 = 10;
pub const ROOT_TEMPO: u16 = 1 * MINUTES as u16;

pub const DEFAULT_SUBNET_TEMPO: u16 = ROOT_TEMPO - 1;
pub const DEFAULT_STAKE_INTERVAL: u64 = DEFAULT_SUBNET_TEMPO as u64;

#[cfg(not(feature = "fast-blocks"))]
pub const INITIAL_CHILDKEY_TAKE_RATELIMIT: u64 = 216000; // 30 days at 12 seconds per block

#[cfg(feature = "fast-blocks")]
pub const INITIAL_CHILDKEY_TAKE_RATELIMIT: u64 = 5;

#[cfg(not(feature = "fast-blocks"))]
pub const SUBTENSOR_INITIAL_TX_RATE_LIMIT: u64 = 1000;
#[cfg(feature = "fast-blocks")]
pub const SUBTENSOR_INITIAL_TX_RATE_LIMIT: u64 = 0;

#[cfg(not(feature = "fast-blocks"))]
pub const DEFAULT_WEIGHTS_SET_RATE_LIMIT: u64 = 1 * MINUTES as u64;
#[cfg(feature = "fast-blocks")]
pub const DEFAULT_WEIGHTS_SET_RATE_LIMIT: u64 = 0;

#[cfg(not(feature = "fast-blocks"))]
pub const FIRST_RESERVED_NETUIDS: u16 = 1000;
#[cfg(feature = "fast-blocks")]
pub const FIRST_RESERVED_NETUIDS: u16 = 100;

type EnsureMajoritySenate =
pallet_collective::EnsureProportionMoreThan<AccountId, TriumvirateCollective, 1, 2>;

Expand Down

0 comments on commit 1596fa9

Please sign in to comment.