Skip to content

Commit

Permalink
Merge pull request #2369 from subspace/domain_block_limits
Browse files Browse the repository at this point in the history
Domains: Set maximum Domain block weight
  • Loading branch information
vedhavyas authored Dec 25, 2023
2 parents 947eacb + 379c70e commit 7d59453
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 108 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/subspace-runtime-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub mod tests_utils {
// if the min is too small, then this will not change, and we are doomed forever.
// the block ref time is 1/100th bigger than target.
Self::run_with_system_weight(
Self::target().set_ref_time(Self::target().ref_time() * 101 / 100),
Self::target().set_ref_time((Self::target().ref_time() / 100) * 101),
|| {
let next = Self::runtime_multiplier_update(Self::min_multiplier());
assert!(
Expand Down
22 changes: 13 additions & 9 deletions domains/primitives/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ description = "Common primitives of subspace domain runtime"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
parity-scale-codec = { version = "3.6.5", default-features = false, features = ["derive"] }
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
sp-api = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "0831dfc3c54b10ab46e82acf98603b4af1a47bd5" }
Expand All @@ -25,13 +27,15 @@ sp-weights = { version = "20.0.0", default-features = false, git = "https://gith
[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"scale-info/std",
"sp-api/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"sp-weights/std",
"subspace-core-primitives/std",
"subspace-runtime-primitives/std",
"frame-support/std",
"frame-system/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-api/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"sp-weights/std",
"subspace-core-primitives/std",
"subspace-runtime-primitives/std",
]
63 changes: 61 additions & 2 deletions domains/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::dispatch::{DispatchClass, PerDispatchClass};
use frame_support::weights::constants::{BlockExecutionWeight, ExtrinsicBaseWeight};
use frame_system::limits::{BlockLength, BlockWeights};
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::generic::{Era, UncheckedExtrinsic};
use sp_runtime::traits::{Block as BlockT, Convert, IdentifyAccount, NumberFor, Verify};
use sp_runtime::transaction_validity::TransactionValidityError;
use sp_runtime::{Digest, MultiAddress, MultiSignature};
use sp_runtime::{Digest, MultiAddress, MultiSignature, Perbill};
use sp_std::vec::Vec;
use sp_weights::Weight;
use subspace_core_primitives::U256;
use subspace_runtime_primitives::Moment;
use subspace_runtime_primitives::{Moment, SHANNON};

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;
Expand Down Expand Up @@ -56,6 +59,52 @@ pub const SLOT_DURATION: u64 = 1000;
/// The EVM chain Id type
pub type EVMChainId = u64;

/// 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;

/// Maximum block length for all dispatches.
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,
}),
}
}

/// The existential deposit. Same with the one on primary chain.
pub const EXISTENTIAL_DEPOSIT: Balance = 500 * SHANNON;

/// We assume that ~5% of the block weight is consumed by `on_initialize` handlers. This is
/// 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);

pub fn block_weights() -> BlockWeights {
BlockWeights::builder()
.base_block(BlockExecutionWeight::get())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = ExtrinsicBaseWeight::get();
})
.for_class(DispatchClass::Normal, |weights| {
// explicitly set max_total weight for normal dispatches to maximum
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
})
.for_class(DispatchClass::Operational, |weights| {
// explicitly set max_total weight for operational dispatches to maximum
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
})
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
.build_or_panic()
}

/// Extracts the signer from an unchecked extrinsic.
///
/// Used by executor to extract the optional signer when shuffling the extrinsics.
Expand Down Expand Up @@ -203,3 +252,13 @@ sp_api::decl_runtime_apis! {
fn block_weight() -> Weight;
}
}

#[cfg(test)]
mod test {
use super::block_weights;
#[test]
fn test_block_weights() {
// validate and build block weights
let _block_weights = block_weights();
}
}
58 changes: 10 additions & 48 deletions domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use codec::{Decode, Encode};
use domain_runtime_primitives::opaque::Header;
pub use domain_runtime_primitives::{opaque, Balance, BlockNumber, Hash, Nonce};
pub use domain_runtime_primitives::{
block_weights, maximum_block_length, opaque, Balance, BlockNumber, Hash, Nonce,
EXISTENTIAL_DEPOSIT, MAXIMUM_BLOCK_WEIGHT,
};
use domain_runtime_primitives::{
CheckExtrinsicsValidityError, MultiAccountId, TryConvertBack, SLOT_DURATION,
};
Expand All @@ -22,10 +25,7 @@ use frame_support::traits::{
ConstU16, ConstU32, ConstU64, Currency, Everything, FindAuthor, Imbalance, OnFinalize,
OnUnbalanced,
};
use frame_support::weights::constants::{
BlockExecutionWeight, ExtrinsicBaseWeight, ParityDbWeight, WEIGHT_REF_TIME_PER_MILLIS,
WEIGHT_REF_TIME_PER_SECOND,
};
use frame_support::weights::constants::{ParityDbWeight, WEIGHT_REF_TIME_PER_SECOND};
use frame_support::weights::{ConstantMultiplier, IdentityFee, Weight};
use frame_support::{construct_runtime, parameter_types};
use frame_system::limits::{BlockLength, BlockWeights};
Expand Down Expand Up @@ -64,7 +64,7 @@ use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use subspace_runtime_primitives::{Moment, SlowAdjustingFeeUpdate, SHANNON};
use subspace_runtime_primitives::{Moment, SlowAdjustingFeeUpdate};

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = EthereumSignature;
Expand Down Expand Up @@ -193,24 +193,6 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
state_version: 0,
};

/// The existential deposit. Same with the one on primary chain.
pub const EXISTENTIAL_DEPOSIT: Balance = 500 * SHANNON;

/// We assume that ~5% of the block weight is consumed by `on_initialize` handlers. This is
/// used to limit the maximal weight of a single extrinsic.
const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5);

/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used by
/// `Operational` extrinsics.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// We allow for 2000ms of compute with a 6 second average block time.
pub const WEIGHT_MILLISECS_PER_BLOCK: u64 = 2000;
pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(
WEIGHT_MILLISECS_PER_BLOCK * WEIGHT_REF_TIME_PER_MILLIS,
u64::MAX,
);
pub const MAXIMUM_BLOCK_LENGTH: u32 = 5 * 1024 * 1024;

/// The version information used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
Expand Down Expand Up @@ -248,26 +230,8 @@ parameter_types! {
// The `RuntimeBlockLength` and `RuntimeBlockWeights` exist here because the
// `DeletionWeightLimit` and `DeletionQueueDepth` depend on those to parameterize
// the lazy contract deletion.
pub RuntimeBlockLength: BlockLength =
BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
pub RuntimeBlockWeights: BlockWeights = BlockWeights::builder()
.base_block(BlockExecutionWeight::get())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = ExtrinsicBaseWeight::get();
})
.for_class(DispatchClass::Normal, |weights| {
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
})
.for_class(DispatchClass::Operational, |weights| {
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
// Operational transactions have some extra reserved space, so that they
// are included even if block reached `MAXIMUM_BLOCK_WEIGHT`.
weights.reserved = Some(
MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT
);
})
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
.build_or_panic();
pub RuntimeBlockLength: BlockLength = maximum_block_length();
pub RuntimeBlockWeights: BlockWeights = block_weights();
pub const ExtrinsicsRootStateVersion: StateVersion = StateVersion::V1;
}

Expand Down Expand Up @@ -500,18 +464,16 @@ impl FindAuthor<H160> for FindAuthorTruncated {

/// Current approximation of the gas/s consumption considering
/// EVM execution over compiled WASM (on 4.4Ghz CPU).
/// Given the 500ms Weight, from which 75% only are used for transactions,
/// the total EVM execution gas limit is: GAS_PER_SECOND * 0.500 * 0.75 ~= 15_000_000.
pub const GAS_PER_SECOND: u64 = 40_000_000;

/// Approximate ratio of the amount of Weight per Gas.
/// u64 works for approximations because Weight is a very small unit compared to gas.
pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND.saturating_div(GAS_PER_SECOND);

parameter_types! {
/// EVM gas limit
/// EVM block gas limit is set to maximum to allow all the transaction stored on Consensus chain.
pub BlockGasLimit: U256 = U256::from(
NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS
MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS
);
pub PrecompilesValue: Precompiles = Precompiles::default();
pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0);
Expand Down
58 changes: 10 additions & 48 deletions domains/test/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use codec::{Decode, Encode};
pub use domain_runtime_primitives::opaque::Header;
use domain_runtime_primitives::{
block_weights, maximum_block_length, MultiAccountId, TryConvertBack, EXISTENTIAL_DEPOSIT,
MAXIMUM_BLOCK_WEIGHT, SLOT_DURATION,
};
pub use domain_runtime_primitives::{
opaque, Balance, BlockNumber, CheckExtrinsicsValidityError, Hash, Nonce,
};
use domain_runtime_primitives::{MultiAccountId, TryConvertBack, SLOT_DURATION};
use fp_account::EthereumSignature;
use fp_self_contained::{CheckedSignature, SelfContainedCall};
use frame_support::dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo};
Expand All @@ -22,10 +25,7 @@ use frame_support::traits::{
ConstU16, ConstU32, ConstU64, Currency, Everything, FindAuthor, Imbalance, OnFinalize,
OnUnbalanced,
};
use frame_support::weights::constants::{
BlockExecutionWeight, ExtrinsicBaseWeight, ParityDbWeight, WEIGHT_REF_TIME_PER_MILLIS,
WEIGHT_REF_TIME_PER_SECOND,
};
use frame_support::weights::constants::{ParityDbWeight, WEIGHT_REF_TIME_PER_SECOND};
use frame_support::weights::{ConstantMultiplier, IdentityFee, Weight};
use frame_support::{construct_runtime, parameter_types};
use frame_system::limits::{BlockLength, BlockWeights};
Expand Down Expand Up @@ -64,7 +64,7 @@ use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use subspace_runtime_primitives::{Moment, SHANNON};
use subspace_runtime_primitives::Moment;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = EthereumSignature;
Expand Down Expand Up @@ -193,24 +193,6 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
state_version: 0,
};

/// The existential deposit. Same with the one on primary chain.
pub const EXISTENTIAL_DEPOSIT: Balance = 500 * SHANNON;

/// We assume that ~5% of the block weight is consumed by `on_initialize` handlers. This is
/// used to limit the maximal weight of a single extrinsic.
const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5);

/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used by
/// `Operational` extrinsics.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// We allow for 2000ms of compute with a 6 second average block time.
pub const WEIGHT_MILLISECS_PER_BLOCK: u64 = 2000;
pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(
WEIGHT_MILLISECS_PER_BLOCK * WEIGHT_REF_TIME_PER_MILLIS,
u64::MAX,
);
pub const MAXIMUM_BLOCK_LENGTH: u32 = 5 * 1024 * 1024;

/// The version information used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
Expand All @@ -228,26 +210,8 @@ parameter_types! {
// The `RuntimeBlockLength` and `RuntimeBlockWeights` exist here because the
// `DeletionWeightLimit` and `DeletionQueueDepth` depend on those to parameterize
// the lazy contract deletion.
pub RuntimeBlockLength: BlockLength =
BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
pub RuntimeBlockWeights: BlockWeights = BlockWeights::builder()
.base_block(BlockExecutionWeight::get())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = ExtrinsicBaseWeight::get();
})
.for_class(DispatchClass::Normal, |weights| {
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
})
.for_class(DispatchClass::Operational, |weights| {
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
// Operational transactions have some extra reserved space, so that they
// are included even if block reached `MAXIMUM_BLOCK_WEIGHT`.
weights.reserved = Some(
MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT
);
})
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
.build_or_panic();
pub RuntimeBlockLength: BlockLength = maximum_block_length();
pub RuntimeBlockWeights: BlockWeights = block_weights();
pub const ExtrinsicsRootStateVersion: StateVersion = StateVersion::V1;
}

Expand Down Expand Up @@ -479,18 +443,16 @@ impl FindAuthor<H160> for FindAuthorTruncated {

/// Current approximation of the gas/s consumption considering
/// EVM execution over compiled WASM (on 4.4Ghz CPU).
/// Given the 500ms Weight, from which 75% only are used for transactions,
/// the total EVM execution gas limit is: GAS_PER_SECOND * 0.500 * 0.75 ~= 15_000_000.
pub const GAS_PER_SECOND: u64 = 40_000_000;

/// Approximate ratio of the amount of Weight per Gas.
/// u64 works for approximations because Weight is a very small unit compared to gas.
pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND.saturating_div(GAS_PER_SECOND);

parameter_types! {
/// EVM gas limit
/// EVM block gas limit is set to maximum to allow all the transaction stored on Consensus chain.
pub BlockGasLimit: U256 = U256::from(
NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS
MAXIMUM_BLOCK_WEIGHT.ref_time() / WEIGHT_PER_GAS
);
pub PrecompilesValue: Precompiles = Precompiles::default();
pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0);
Expand Down

0 comments on commit 7d59453

Please sign in to comment.