From 5d1a0cfbe806b6d2a077df064025634c6bb3f091 Mon Sep 17 00:00:00 2001 From: vedhavyas Date: Thu, 30 May 2024 20:08:16 +0530 Subject: [PATCH] update proof sizes for domain block weights to be compatible for Consensus limits --- crates/subspace-runtime-primitives/Cargo.toml | 4 +- crates/subspace-runtime-primitives/src/lib.rs | 9 +++ crates/subspace-runtime/src/lib.rs | 11 ++-- domains/primitives/runtime/src/lib.rs | 61 ++++++++----------- domains/runtime/auto-id/src/lib.rs | 2 +- domains/runtime/evm/src/lib.rs | 6 +- domains/test/runtime/evm/src/lib.rs | 6 +- 7 files changed, 49 insertions(+), 50 deletions(-) diff --git a/crates/subspace-runtime-primitives/Cargo.toml b/crates/subspace-runtime-primitives/Cargo.toml index e4ec1c7c1b..6af925ceb5 100644 --- a/crates/subspace-runtime-primitives/Cargo.toml +++ b/crates/subspace-runtime-primitives/Cargo.toml @@ -18,7 +18,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] } frame-support = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "6da3c45e1d5b3c1f09b5e54152b8848149f9d5e6" } -frame-system = { default-features = false, optional = true, git = "https://github.com/subspace/polkadot-sdk", rev = "6da3c45e1d5b3c1f09b5e54152b8848149f9d5e6" } +frame-system = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "6da3c45e1d5b3c1f09b5e54152b8848149f9d5e6" } pallet-transaction-payment = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "6da3c45e1d5b3c1f09b5e54152b8848149f9d5e6" } scale-info = { version = "2.11.1", default-features = false, features = ["derive"] } sp-core = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "6da3c45e1d5b3c1f09b5e54152b8848149f9d5e6" } @@ -31,6 +31,7 @@ default = ["std"] std = [ "codec/std", "frame-support/std", + "frame-system/std", "pallet-transaction-payment/std", "scale-info/std", "sp-core/std", @@ -38,6 +39,5 @@ std = [ "subspace-core-primitives/std", ] testing = [ - "frame-system", "sp-io" ] diff --git a/crates/subspace-runtime-primitives/src/lib.rs b/crates/subspace-runtime-primitives/src/lib.rs index a62fc99f6f..07c3bb3f72 100644 --- a/crates/subspace-runtime-primitives/src/lib.rs +++ b/crates/subspace-runtime-primitives/src/lib.rs @@ -27,6 +27,7 @@ use codec::{Codec, Decode, Encode}; use frame_support::pallet_prelude::Weight; use frame_support::traits::tokens; use frame_support::weights::constants::WEIGHT_REF_TIME_PER_SECOND; +use frame_system::limits::BlockLength; use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment}; use scale_info::TypeInfo; use sp_core::parameter_types; @@ -53,6 +54,14 @@ pub const SLOT_PROBABILITY: (u64, u64) = (1, 6); pub const BLOCK_WEIGHT_FOR_2_SEC: Weight = Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), u64::MAX); +/// Maximum block length for non-`Normal` extrinsic is 5 MiB. +pub const MAX_BLOCK_LENGTH: u32 = 5 * 1024 * 1024; + +/// We allow for 3.75 MiB for `Normal` extrinsic with 5 MiB maximum block length. +pub fn maximum_normal_block_length() -> BlockLength { + BlockLength::max_with_normal_ratio(MAX_BLOCK_LENGTH, NORMAL_DISPATCH_RATIO) +} + /// Alias to 512-bit hash when used in the context of a transaction signature on the chain. pub type Signature = MultiSignature; diff --git a/crates/subspace-runtime/src/lib.rs b/crates/subspace-runtime/src/lib.rs index ef56343b2c..4de14c1731 100644 --- a/crates/subspace-runtime/src/lib.rs +++ b/crates/subspace-runtime/src/lib.rs @@ -98,9 +98,9 @@ use subspace_core_primitives::{ SegmentCommitment, SegmentHeader, SegmentIndex, SlotNumber, SolutionRange, U256, }; use subspace_runtime_primitives::{ - AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash, Moment, Nonce, Signature, - SlowAdjustingFeeUpdate, BLOCK_WEIGHT_FOR_2_SEC, MIN_REPLICATION_FACTOR, NORMAL_DISPATCH_RATIO, - SHANNON, SLOT_PROBABILITY, SSC, + maximum_normal_block_length, AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash, + Moment, Nonce, Signature, SlowAdjustingFeeUpdate, BLOCK_WEIGHT_FOR_2_SEC, MAX_BLOCK_LENGTH, + MIN_REPLICATION_FACTOR, NORMAL_DISPATCH_RATIO, SHANNON, SLOT_PROBABILITY, SSC, }; sp_runtime::impl_opaque_keys! { @@ -196,16 +196,13 @@ const RECENT_HISTORY_FRACTION: (HistorySize, HistorySize) = ( const MIN_SECTOR_LIFETIME: HistorySize = HistorySize::new(NonZeroU64::new(4).expect("Not zero; qed")); -/// Maximum block length for non-`Normal` extrinsic is 5 MiB. -const MAX_BLOCK_LENGTH: u32 = 5 * 1024 * 1024; - parameter_types! { pub const Version: RuntimeVersion = VERSION; pub const BlockHashCount: BlockNumber = 250; /// We allow for 2 seconds of compute with a 6 second average block time. pub SubspaceBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults(BLOCK_WEIGHT_FOR_2_SEC, NORMAL_DISPATCH_RATIO); /// We allow for 3.75 MiB for `Normal` extrinsic with 5 MiB maximum block length. - pub SubspaceBlockLength: BlockLength = BlockLength::max_with_normal_ratio(MAX_BLOCK_LENGTH, NORMAL_DISPATCH_RATIO); + pub SubspaceBlockLength: BlockLength = maximum_normal_block_length(); } pub type SS58Prefix = ConstU16<2254>; diff --git a/domains/primitives/runtime/src/lib.rs b/domains/primitives/runtime/src/lib.rs index a3ba5f7eea..02cb53e935 100644 --- a/domains/primitives/runtime/src/lib.rs +++ b/domains/primitives/runtime/src/lib.rs @@ -25,7 +25,7 @@ use alloc::string::String; #[cfg(not(feature = "std"))] use alloc::vec::Vec; pub use fp_account::AccountId20; -use frame_support::dispatch::{DispatchClass, PerDispatchClass}; +use frame_support::dispatch::DispatchClass; use frame_support::weights::constants::{BlockExecutionWeight, ExtrinsicBaseWeight}; use frame_system::limits::{BlockLength, BlockWeights}; use parity_scale_codec::{Decode, Encode}; @@ -35,10 +35,9 @@ use sp_runtime::generic::UncheckedExtrinsic; use sp_runtime::traits::{Convert, IdentifyAccount, Verify}; use sp_runtime::transaction_validity::TransactionValidityError; use sp_runtime::{MultiAddress, MultiSignature, Perbill}; +use sp_weights::constants::WEIGHT_REF_TIME_PER_SECOND; use sp_weights::Weight; -use subspace_runtime_primitives::{ - BLOCK_WEIGHT_FOR_2_SEC, NORMAL_DISPATCH_RATIO, SHANNON, SLOT_PROBABILITY, -}; +use subspace_runtime_primitives::{MAX_BLOCK_LENGTH, SHANNON, SLOT_PROBABILITY}; /// Alias to 512-bit hash when used in the context of a transaction signature on the chain. pub type Signature = MultiSignature; @@ -68,32 +67,29 @@ pub const SLOT_DURATION: u64 = 1000; /// The EVM chain Id type pub type EVMChainId = u64; -/// The maximum domain block weight. +/// Dispatch ratio for domains +pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(65); + +/// The maximum domain block weight with 3.25 MiB as proof size +/// Consensus allows 3.75 MiB but Fraud proof can carry extra size along with proof size +/// So we set the proof size to 3.25 MiB pub fn maximum_domain_block_weight() -> Weight { - NORMAL_DISPATCH_RATIO * BLOCK_WEIGHT_FOR_2_SEC + let consensus_maximum_normal_block_length = + *maximum_block_length().max.get(DispatchClass::Normal) as u64; + let weight = + NORMAL_DISPATCH_RATIO * Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND.saturating_mul(2), 0); + weight.set_proof_size(consensus_maximum_normal_block_length) } -/// Maximum block length for mandatory dispatch. -pub const MAXIMUM_MANDATORY_BLOCK_LENGTH: u32 = 5 * 1024 * 1024; - -/// Maximum block length for operational and normal dispatches. -pub const MAXIMUM_OPERATIONAL_AND_NORMAL_BLOCK_LENGTH: u32 = u32::MAX; - /// Custom error when nonce overflow occurs. pub const ERR_NONCE_OVERFLOW: u8 = 100; /// Custom error when balance overflow occurs. pub const ERR_BALANCE_OVERFLOW: u8 = 200; /// Maximum block length for all dispatches. +/// This is set to 3.75 MiB since consensus chain supports on 3.75 MiB for normal pub fn maximum_block_length() -> BlockLength { - BlockLength { - max: PerDispatchClass::new(|class| match class { - DispatchClass::Normal | DispatchClass::Operational => { - MAXIMUM_OPERATIONAL_AND_NORMAL_BLOCK_LENGTH - } - DispatchClass::Mandatory => MAXIMUM_MANDATORY_BLOCK_LENGTH, - }), - } + BlockLength::max_with_normal_ratio(MAX_BLOCK_LENGTH, NORMAL_DISPATCH_RATIO) } /// The existential deposit. Same with the one on primary chain. @@ -103,9 +99,6 @@ pub const EXISTENTIAL_DEPOSIT: Balance = 500 * SHANNON; /// used to limit the maximal weight of a single extrinsic. const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5); -/// Maximum total block weight. -pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(u64::MAX, u64::MAX); - /// Calculates the max bundle weight // See https://forum.subspace.network/t/on-bundle-weight-limits-sum/2277 for more details // about the formula @@ -125,13 +118,20 @@ pub fn calculate_max_bundle_weight( .checked_mul(consensus_slot_probability.0)?, )?; + // set the proof size for bundle to be proof size of max domain weight + // so that each domain extrinsic can use the full proof size if required + let max_proof_size = max_domain_block_weight.proof_size(); let max_bundle_weight = max_domain_block_weight.checked_div(expected_bundles_per_block)?; - Some((expected_bundles_per_block, max_bundle_weight)) + Some(( + expected_bundles_per_block, + max_bundle_weight.set_proof_size(max_proof_size), + )) } /// Calculates the maximum extrinsic weight for domains. /// We take bundle slot probability to be always at the maximum i.e 1 such that /// operator can produce bundle in each slot +/// we also set the maximum extrinsic POV to be 3.75 MiB which is what Consensus allows fn maximum_domain_extrinsic_weight() -> Option { let (_, max_bundle_weight) = calculate_max_bundle_weight(maximum_domain_block_weight(), SLOT_PROBABILITY, (1, 1))?; @@ -139,26 +139,19 @@ fn maximum_domain_extrinsic_weight() -> Option { } pub fn block_weights() -> BlockWeights { + // allow u64::MAX for ref_time and proof size for total domain weight + let maximum_block_weight = Weight::from_parts(u64::MAX, u64::MAX); let max_extrinsic_weight = maximum_domain_extrinsic_weight().expect("Maximum extrinsic weight must always be valid"); BlockWeights::builder() .base_block(BlockExecutionWeight::get()) .for_class(DispatchClass::all(), |weights| { weights.base_extrinsic = ExtrinsicBaseWeight::get(); - }) - .for_class(DispatchClass::Normal, |weights| { // maximum weight of each transaction would be the maximum weight of // single bundle weights.max_extrinsic = Some(max_extrinsic_weight); // explicitly set max_total weight for normal dispatches to maximum - weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT); - }) - .for_class(DispatchClass::Operational, |weights| { - // maximum weight of each transaction would be the maximum weight of - // single bundle - weights.max_extrinsic = Some(max_extrinsic_weight); - // explicitly set max_total weight for operational dispatches to maximum - weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT); + weights.max_total = Some(maximum_block_weight); }) .avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO) .build_or_panic() diff --git a/domains/runtime/auto-id/src/lib.rs b/domains/runtime/auto-id/src/lib.rs index 21f30ac013..e9c4454817 100644 --- a/domains/runtime/auto-id/src/lib.rs +++ b/domains/runtime/auto-id/src/lib.rs @@ -16,7 +16,7 @@ use codec::{Decode, Encode, MaxEncodedLen}; use domain_runtime_primitives::opaque::Header; pub use domain_runtime_primitives::{ block_weights, maximum_block_length, opaque, Balance, BlockNumber, Hash, Nonce, - EXISTENTIAL_DEPOSIT, MAXIMUM_BLOCK_WEIGHT, + EXISTENTIAL_DEPOSIT, }; use domain_runtime_primitives::{ AccountId, Address, CheckExtrinsicsValidityError, DecodeExtrinsicError, Signature, diff --git a/domains/runtime/evm/src/lib.rs b/domains/runtime/evm/src/lib.rs index a5c83be475..663a0504d8 100644 --- a/domains/runtime/evm/src/lib.rs +++ b/domains/runtime/evm/src/lib.rs @@ -17,8 +17,8 @@ use alloc::format; use codec::{Decode, Encode, MaxEncodedLen}; use domain_runtime_primitives::opaque::Header; pub use domain_runtime_primitives::{ - block_weights, maximum_block_length, opaque, Balance, BlockNumber, Hash, Nonce, - EXISTENTIAL_DEPOSIT, MAXIMUM_BLOCK_WEIGHT, + block_weights, maximum_block_length, maximum_domain_block_weight, opaque, Balance, BlockNumber, + Hash, Nonce, EXISTENTIAL_DEPOSIT, }; use domain_runtime_primitives::{ CheckExtrinsicsValidityError, DecodeExtrinsicError, ERR_BALANCE_OVERFLOW, ERR_NONCE_OVERFLOW, @@ -589,7 +589,7 @@ pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND.saturating_div(GAS_PE parameter_types! { /// EVM block gas limit is set to maximum to allow all the transaction stored on Consensus chain. pub BlockGasLimit: U256 = U256::from( - MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS + maximum_domain_block_weight().ref_time() / WEIGHT_PER_GAS ); pub PrecompilesValue: Precompiles = Precompiles::default(); pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0); diff --git a/domains/test/runtime/evm/src/lib.rs b/domains/test/runtime/evm/src/lib.rs index 3ad492455a..58bb39df82 100644 --- a/domains/test/runtime/evm/src/lib.rs +++ b/domains/test/runtime/evm/src/lib.rs @@ -17,8 +17,8 @@ use alloc::format; use codec::{Decode, Encode, MaxEncodedLen}; pub use domain_runtime_primitives::opaque::Header; use domain_runtime_primitives::{ - block_weights, maximum_block_length, ERR_BALANCE_OVERFLOW, ERR_NONCE_OVERFLOW, - EXISTENTIAL_DEPOSIT, MAXIMUM_BLOCK_WEIGHT, SLOT_DURATION, + block_weights, maximum_block_length, maximum_domain_block_weight, ERR_BALANCE_OVERFLOW, + ERR_NONCE_OVERFLOW, EXISTENTIAL_DEPOSIT, SLOT_DURATION, }; pub use domain_runtime_primitives::{ opaque, Balance, BlockNumber, CheckExtrinsicsValidityError, DecodeExtrinsicError, Hash, Nonce, @@ -571,7 +571,7 @@ pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND.saturating_div(GAS_PE parameter_types! { /// EVM block gas limit is set to maximum to allow all the transaction stored on Consensus chain. pub BlockGasLimit: U256 = U256::from( - MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS + maximum_domain_block_weight().ref_time() / WEIGHT_PER_GAS ); pub PrecompilesValue: Precompiles = Precompiles::default(); pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0);