mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 19:11:04 +00:00
Maximum extrinsic weight limit (#6067)
* Only check single extrinsics weight limit in validate_transaction. * Add missing parameter to all pallets. * Add tests, fix default configuration. * Bump spec version. * Use AvailableBlockRation to calculate MaxExtrinsicWeight
This commit is contained in:
@@ -39,6 +39,7 @@ impl system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -16,6 +16,7 @@ use sp_runtime::{
|
||||
};
|
||||
use sp_runtime::traits::{
|
||||
BlakeTwo256, Block as BlockT, IdentityLookup, Verify, ConvertInto, IdentifyAccount, NumberFor,
|
||||
Saturating,
|
||||
};
|
||||
use sp_api::impl_runtime_apis;
|
||||
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
|
||||
@@ -127,6 +128,9 @@ parameter_types! {
|
||||
/// We allow for 2 seconds of compute with a 6 second average block time.
|
||||
pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
|
||||
/// Assume 10% of weight for average on_initialize calls.
|
||||
pub const MaximumExtrinsicWeight: Weight = AvailableBlockRatio::get()
|
||||
.saturating_sub(Perbill::from_percent(10)) * MaximumBlockWeight::get();
|
||||
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
|
||||
pub const Version: RuntimeVersion = VERSION;
|
||||
}
|
||||
@@ -164,6 +168,10 @@ impl system::Trait for Runtime {
|
||||
/// The base weight of any extrinsic processed by the runtime, independent of the
|
||||
/// logic of that extrinsic. (Signature verification, nonce increment, fee, etc...)
|
||||
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
|
||||
/// The maximum weight that a single extrinsic of `Normal` dispatch class can have,
|
||||
/// idependent of the logic of that extrinsics. (Roughly max block weight - average on
|
||||
/// initialize cost).
|
||||
type MaximumExtrinsicWeight = MaximumExtrinsicWeight;
|
||||
/// Maximum size of all encoded transactions (in bytes) that are allowed in one block.
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
/// Portion of the block weight that is available to all normal transactions.
|
||||
|
||||
@@ -47,7 +47,7 @@ use sp_runtime::curve::PiecewiseLinear;
|
||||
use sp_runtime::transaction_validity::{TransactionValidity, TransactionSource, TransactionPriority};
|
||||
use sp_runtime::traits::{
|
||||
self, BlakeTwo256, Block as BlockT, StaticLookup, SaturatedConversion,
|
||||
ConvertInto, OpaqueKeys, NumberFor,
|
||||
ConvertInto, OpaqueKeys, NumberFor, Saturating,
|
||||
};
|
||||
use sp_version::RuntimeVersion;
|
||||
#[cfg(any(feature = "std", test))]
|
||||
@@ -94,7 +94,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
// implementation changes and behavior does not, then leave spec_version as
|
||||
// is and increment impl_version.
|
||||
spec_version: 249,
|
||||
impl_version: 1,
|
||||
impl_version: 0,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
transaction_version: 1,
|
||||
};
|
||||
@@ -130,9 +130,12 @@ parameter_types! {
|
||||
pub const BlockHashCount: BlockNumber = 2400;
|
||||
/// We allow for 2 seconds of compute with a 6 second average block time.
|
||||
pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
|
||||
/// Assume 10% of weight for average on_initialize calls.
|
||||
pub const MaximumExtrinsicWeight: Weight = AvailableBlockRatio::get()
|
||||
.saturating_sub(Perbill::from_percent(10)) * MaximumBlockWeight::get();
|
||||
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
|
||||
pub const Version: RuntimeVersion = VERSION;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
|
||||
}
|
||||
|
||||
impl frame_system::Trait for Runtime {
|
||||
@@ -151,6 +154,7 @@ impl frame_system::Trait for Runtime {
|
||||
type DbWeight = RocksDbWeight;
|
||||
type BlockExecutionWeight = BlockExecutionWeight;
|
||||
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
|
||||
type MaximumExtrinsicWeight = MaximumExtrinsicWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = Version;
|
||||
|
||||
@@ -317,6 +317,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -61,6 +61,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -158,6 +158,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -433,6 +433,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -73,6 +73,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type ModuleToIndex = ();
|
||||
|
||||
@@ -863,6 +863,7 @@ impl<T: Subtrait<I>, I: Instance> frame_system::Trait for ElevatedTrait<T, I> {
|
||||
type DbWeight = T::DbWeight;
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = T::MaximumBlockWeight;
|
||||
type MaximumBlockLength = T::MaximumBlockLength;
|
||||
type AvailableBlockRatio = T::AvailableBlockRatio;
|
||||
type Version = T::Version;
|
||||
|
||||
@@ -71,6 +71,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -71,6 +71,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -82,6 +82,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = ();
|
||||
type MaximumBlockLength = ();
|
||||
type AvailableBlockRatio = ();
|
||||
type Version = ();
|
||||
|
||||
@@ -984,6 +984,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -103,6 +103,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -100,6 +100,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -1087,6 +1087,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -54,6 +54,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -69,6 +69,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -754,6 +754,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -560,6 +560,7 @@ mod tests {
|
||||
type DbWeight = DbWeight;
|
||||
type BlockExecutionWeight = BlockExecutionWeight;
|
||||
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = RuntimeVersion;
|
||||
|
||||
@@ -267,6 +267,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -1127,6 +1127,7 @@ impl<T: Subtrait> frame_system::Trait for ElevatedTrait<T> {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = T::MaximumBlockWeight;
|
||||
type MaximumBlockLength = T::MaximumBlockLength;
|
||||
type AvailableBlockRatio = T::AvailableBlockRatio;
|
||||
type Version = T::Version;
|
||||
|
||||
@@ -60,6 +60,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type BlockHashCount = BlockHashCount;
|
||||
|
||||
@@ -109,6 +109,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -1189,6 +1189,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -119,6 +119,7 @@ impl frame_system::Trait for Runtime {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -65,6 +65,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -318,6 +318,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -285,6 +285,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -57,6 +57,7 @@ impl frame_system::Trait for Test {
|
||||
type OnKilledAccount = (Balances,);
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = ();
|
||||
}
|
||||
parameter_types! {
|
||||
pub const ExistentialDeposit: Balance = 10;
|
||||
|
||||
@@ -104,6 +104,7 @@ impl frame_system::Trait for Runtime {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -173,6 +173,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -79,6 +79,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -502,6 +502,7 @@ mod tests {
|
||||
type DbWeight = RocksDbWeight;
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -70,6 +70,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -73,6 +73,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = ();
|
||||
type AvailableBlockRatio = ();
|
||||
type MaximumBlockLength = ();
|
||||
type Version = ();
|
||||
|
||||
@@ -188,6 +188,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -80,6 +80,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -61,6 +61,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = ();
|
||||
type Index = AccountIndex;
|
||||
type BlockNumber = BlockNumber;
|
||||
type Call = Call;
|
||||
|
||||
@@ -215,6 +215,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = RocksDbWeight;
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -136,6 +136,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -76,6 +76,7 @@ impl system::Trait for Runtime {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -66,6 +66,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = ();
|
||||
type AvailableBlockRatio = ();
|
||||
type MaximumBlockLength = ();
|
||||
type Version = ();
|
||||
|
||||
@@ -212,6 +212,11 @@ pub trait Trait: 'static + Eq + Clone {
|
||||
/// The base weight of an Extrinsic in the block, independent of the of extrinsic being executed.
|
||||
type ExtrinsicBaseWeight: Get<Weight>;
|
||||
|
||||
/// The maximal weight of a single Extrinsic. This should be set to at most
|
||||
/// `MaximumBlockWeight - AverageOnInitializeWeight`. The limit only applies to extrinsics
|
||||
/// containing `Normal` dispatch class calls.
|
||||
type MaximumExtrinsicWeight: Get<Weight>;
|
||||
|
||||
/// The maximum length of a block (in bytes).
|
||||
type MaximumBlockLength: Get<u32>;
|
||||
|
||||
@@ -1352,10 +1357,29 @@ impl<T: Trait + Send + Sync> CheckWeight<T> where
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the current extrinsic does not exceed `MaximumExtrinsicWeight` limit.
|
||||
fn check_extrinsic_weight(
|
||||
info: &DispatchInfoOf<T::Call>,
|
||||
) -> Result<(), TransactionValidityError> {
|
||||
match info.class {
|
||||
// Mandatory and Operational transactions does not
|
||||
DispatchClass::Mandatory | DispatchClass::Operational => Ok(()),
|
||||
DispatchClass::Normal => {
|
||||
let maximum_weight = T::MaximumExtrinsicWeight::get();
|
||||
let extrinsic_weight = info.weight.saturating_add(T::ExtrinsicBaseWeight::get());
|
||||
if extrinsic_weight > maximum_weight {
|
||||
Err(InvalidTransaction::ExhaustsResources.into())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the current extrinsic can fit into the block with respect to block weight limits.
|
||||
///
|
||||
/// Upon successes, it returns the new block weight as a `Result`.
|
||||
fn check_weight(
|
||||
fn check_block_weight(
|
||||
info: &DispatchInfoOf<T::Call>,
|
||||
) -> Result<ExtrinsicsWeight, TransactionValidityError> {
|
||||
let maximum_weight = T::MaximumBlockWeight::get();
|
||||
@@ -1446,7 +1470,9 @@ impl<T: Trait + Send + Sync> CheckWeight<T> where
|
||||
len: usize,
|
||||
) -> Result<(), TransactionValidityError> {
|
||||
let next_len = Self::check_block_length(info, len)?;
|
||||
let next_weight = Self::check_weight(info)?;
|
||||
let next_weight = Self::check_block_weight(info)?;
|
||||
Self::check_extrinsic_weight(info)?;
|
||||
|
||||
AllExtrinsicsLen::put(next_len);
|
||||
AllExtrinsicsWeight::put(next_weight);
|
||||
Ok(())
|
||||
@@ -1459,9 +1485,12 @@ impl<T: Trait + Send + Sync> CheckWeight<T> where
|
||||
info: &DispatchInfoOf<T::Call>,
|
||||
len: usize,
|
||||
) -> TransactionValidity {
|
||||
// ignore the next weight and length. If they return `Ok`, then it is below the limit.
|
||||
// ignore the next length. If they return `Ok`, then it is below the limit.
|
||||
let _ = Self::check_block_length(info, len)?;
|
||||
let _ = Self::check_weight(info)?;
|
||||
// during validation we skip block limit check. Since the `validate_transaction`
|
||||
// call runs on an empty block anyway, by this we prevent `on_initialize` weight
|
||||
// consumption from causing false negatives.
|
||||
Self::check_extrinsic_weight(info)?;
|
||||
|
||||
Ok(ValidTransaction { priority: Self::get_priority(info), ..Default::default() })
|
||||
}
|
||||
@@ -1847,6 +1876,7 @@ pub(crate) mod tests {
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u64 = 10;
|
||||
pub const MaximumBlockWeight: Weight = 1024;
|
||||
pub const MaximumExtrinsicWeight: Weight = 768;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
|
||||
pub const MaximumBlockLength: u32 = 1024;
|
||||
pub const Version: RuntimeVersion = RuntimeVersion {
|
||||
@@ -1905,6 +1935,7 @@ pub(crate) mod tests {
|
||||
type DbWeight = DbWeight;
|
||||
type BlockExecutionWeight = BlockExecutionWeight;
|
||||
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
|
||||
type MaximumExtrinsicWeight = MaximumExtrinsicWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = Version;
|
||||
@@ -2344,17 +2375,43 @@ pub(crate) mod tests {
|
||||
|
||||
#[test]
|
||||
fn mandatory_extrinsic_doesnt_care_about_limits() {
|
||||
fn check(call: impl FnOnce(&DispatchInfo, usize)) {
|
||||
new_test_ext().execute_with(|| {
|
||||
let max = DispatchInfo {
|
||||
weight: Weight::max_value(),
|
||||
class: DispatchClass::Mandatory,
|
||||
..Default::default()
|
||||
};
|
||||
let len = 0_usize;
|
||||
|
||||
call(&max, len);
|
||||
});
|
||||
}
|
||||
|
||||
check(|max, len| {
|
||||
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(max, len));
|
||||
assert_eq!(System::all_extrinsics_weight().total(), Weight::max_value());
|
||||
assert!(System::all_extrinsics_weight().total() > <Test as Trait>::MaximumBlockWeight::get());
|
||||
});
|
||||
check(|max, len| {
|
||||
assert_ok!(CheckWeight::<Test>::do_validate(max, len));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normal_extrinsic_limited_by_maximum_extrinsic_weight() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let max = DispatchInfo {
|
||||
weight: Weight::max_value(),
|
||||
class: DispatchClass::Mandatory,
|
||||
weight: MaximumExtrinsicWeight::get() + 1,
|
||||
class: DispatchClass::Normal,
|
||||
..Default::default()
|
||||
};
|
||||
let len = 0_usize;
|
||||
|
||||
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&max, len));
|
||||
assert_eq!(System::all_extrinsics_weight().total(), Weight::max_value());
|
||||
assert!(System::all_extrinsics_weight().total() > <Test as Trait>::MaximumBlockWeight::get());
|
||||
assert_noop!(
|
||||
CheckWeight::<Test>::do_validate(&max, len),
|
||||
InvalidTransaction::ExhaustsResources
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2449,7 +2506,7 @@ pub(crate) mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_ext_check_weight_priority_works() {
|
||||
fn signed_ext() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let normal = DispatchInfo { weight: 100, class: DispatchClass::Normal, pays_fee: Pays::Yes };
|
||||
let op = DispatchInfo { weight: 100, class: DispatchClass::Operational, pays_fee: Pays::Yes };
|
||||
|
||||
@@ -329,6 +329,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -380,6 +380,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -75,6 +75,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
|
||||
@@ -75,6 +75,7 @@ impl frame_system::Trait for Test {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -396,6 +396,7 @@ mod tests {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
@@ -414,6 +414,7 @@ impl frame_system::Trait for Runtime {
|
||||
type DbWeight = ();
|
||||
type BlockExecutionWeight = ();
|
||||
type ExtrinsicBaseWeight = ();
|
||||
type MaximumExtrinsicWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
|
||||
Reference in New Issue
Block a user