From 1596fa97d672a2302ea699b53ad376f0857e83f4 Mon Sep 17 00:00:00 2001 From: Mohsen Date: Wed, 25 Dec 2024 14:01:13 +0330 Subject: [PATCH] fix: fast-blocks 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 --- node/src/chain_spec/localnet.rs | 8 +++ pallets/subtensor/src/coinbase/root.rs | 26 +++++---- .../subtensor/src/coinbase/run_coinbase.rs | 2 + pallets/subtensor/src/epoch/run_epoch.rs | 6 ++ pallets/subtensor/src/lib.rs | 4 +- pallets/subtensor/src/macros/config.rs | 6 ++ pallets/subtensor/src/subnets/registration.rs | 20 ++++--- pallets/subtensor/tests/epoch.rs | 1 - runtime/src/lib.rs | 55 +++++++++++++------ 9 files changed, 89 insertions(+), 39 deletions(-) diff --git a/node/src/chain_spec/localnet.rs b/node/src/chain_spec/localnet.rs index 17f3d984..d3148ba2 100644 --- a/node/src/chain_spec/localnet.rs +++ b/node/src/chain_spec/localnet.rs @@ -6,6 +6,7 @@ use super::*; #[rustfmt::skip] pub fn localnet_config(single_authority: bool) -> Result { 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, @@ -48,6 +49,13 @@ pub fn localnet_config(single_authority: bool) -> Result { (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![ diff --git a/pallets/subtensor/src/coinbase/root.rs b/pallets/subtensor/src/coinbase/root.rs index 7cf70c47..fecdbcad 100644 --- a/pallets/subtensor/src/coinbase/root.rs +++ b/pallets/subtensor/src/coinbase/root.rs @@ -727,9 +727,9 @@ impl Pallet { Error::::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::::HotKeyAlreadyRegisteredInSubNet ); @@ -978,29 +978,28 @@ impl Pallet { let first_resv_neuids = FirstReservedNetuids::::get(); let (netuid_from, p) = if is_reserved { - let total_reserved_subnets = ReservedSubnetCount::::get(); + let resv_subnet_count = ReservedSubnetCount::::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::::get(); + let user_subnet_count = UserSubnetCount::::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 { @@ -1034,8 +1033,13 @@ impl Pallet { 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 { diff --git a/pallets/subtensor/src/coinbase/run_coinbase.rs b/pallets/subtensor/src/coinbase/run_coinbase.rs index 96e015f7..07268d45 100644 --- a/pallets/subtensor/src/coinbase/run_coinbase.rs +++ b/pallets/subtensor/src/coinbase/run_coinbase.rs @@ -77,6 +77,7 @@ impl Pallet { 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::::get(*netuid); PendingEmission::::insert(*netuid, 0); @@ -407,6 +408,7 @@ impl Pallet { /// 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; } diff --git a/pallets/subtensor/src/epoch/run_epoch.rs b/pallets/subtensor/src/epoch/run_epoch.rs index d919c6db..3b5d2826 100644 --- a/pallets/subtensor/src/epoch/run_epoch.rs +++ b/pallets/subtensor/src/epoch/run_epoch.rs @@ -436,6 +436,8 @@ impl Pallet { /// #[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); @@ -783,6 +785,10 @@ impl Pallet { } }); + 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() diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 9d184e21..2cc819eb 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -296,7 +296,7 @@ pub mod pallet { #[pallet::type_value] /// Default stake interval. pub fn DefaultStakeInterval() -> u64 { - 360 + T::DefaultStakeInterval::get() } #[pallet::type_value] /// Default account linkage @@ -490,7 +490,7 @@ pub mod pallet { #[pallet::type_value] /// Default value for weights set rate limit. pub fn DefaultWeightsSetRateLimit() -> u64 { - 100 + T::DefaultWeightsSetRateLimit::get() } #[pallet::type_value] /// Default block number at registration. diff --git a/pallets/subtensor/src/macros/config.rs b/pallets/subtensor/src/macros/config.rs index fae7717a..8600a02a 100644 --- a/pallets/subtensor/src/macros/config.rs +++ b/pallets/subtensor/src/macros/config.rs @@ -219,5 +219,11 @@ mod config { /// Root subnet Tempo #[pallet::constant] type RootTempo: Get; + /// Default Stake Interval + #[pallet::constant] + type DefaultStakeInterval: Get; + /// Default Weights Set Rate Limit + #[pallet::constant] + type DefaultWeightsSetRateLimit: Get; } } diff --git a/pallets/subtensor/src/subnets/registration.rs b/pallets/subtensor/src/subnets/registration.rs index 516fd8cf..280c4ac5 100644 --- a/pallets/subtensor/src/subnets/registration.rs +++ b/pallets/subtensor/src/subnets/registration.rs @@ -276,10 +276,12 @@ impl Pallet { block_number <= current_block_number, Error::::InvalidWorkBlock ); - ensure!( - current_block_number.saturating_sub(block_number) < 3, - Error::::InvalidWorkBlock - ); + if !cfg!(feature = "fast-blocks") { + ensure!( + current_block_number.saturating_sub(block_number) < 3, + Error::::InvalidWorkBlock + ); + } // --- 8. Ensure the supplied work passes the difficulty. let difficulty: U256 = Self::get_difficulty(netuid); @@ -375,10 +377,12 @@ impl Pallet { block_number <= current_block_number, Error::::InvalidWorkBlock ); - ensure!( - current_block_number.saturating_sub(block_number) < 3, - Error::::InvalidWorkBlock - ); + if !cfg!(feature = "fast-blocks") { + ensure!( + current_block_number.saturating_sub(block_number) < 3, + Error::::InvalidWorkBlock + ); + } // --- 3. Ensure the supplied work passes the difficulty. let difficulty: U256 = if !cfg!(feature = "fast-blocks") { diff --git a/pallets/subtensor/tests/epoch.rs b/pallets/subtensor/tests/epoch.rs index 8762843f..d40ff47b 100644 --- a/pallets/subtensor/tests/epoch.rs +++ b/pallets/subtensor/tests/epoch.rs @@ -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; diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 4971fdd4..9eea0bbd 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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, @@ -197,7 +197,7 @@ 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. @@ -205,8 +205,8 @@ 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); @@ -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 for AllowCommitments { #[cfg(not(feature = "runtime-benchmarks"))] @@ -931,7 +936,7 @@ impl pallet_subtensor::Config for Runtime { type InitialMaxBurn = SubtensorInitialMaxBurn; type InitialMinBurn = SubtensorInitialMinBurn; // -- - type InitialTxRateLimit = SubtensorInitialTxRateLimit; + type InitialTxRateLimit = ConstU64; type InitialTxDelegateTakeRateLimit = SubtensorInitialTxDelegateTakeRateLimit; type InitialTxChildKeyTakeRateLimit = SubtensorInitialTxChildKeyTakeRateLimit; type InitialMaxChildKeyTake = SubtensorInitialMaxChildKeyTake; @@ -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; type InitialNetworkRateLimit = SubtensorInitialNetworkRateLimit; - type InitialTargetStakesPerInterval = SubtensorInitialTargetStakesPerInterval; + type InitialTargetStakesPerInterval = ConstU64<1>; type KeySwapCost = SubtensorInitialKeySwapCost; type AlphaHigh = InitialAlphaHigh; type AlphaLow = InitialAlphaLow; @@ -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; + type DefaultStakeInterval = ConstU64; + type DefaultWeightsSetRateLimit = ConstU64; } parameter_types! { pub const SubtensorInitialRho : u16 = 10; @@ -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. @@ -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 @@ -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 @@ -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;