mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 04:01:10 +00:00
Introduce BlockExecutionWeight and ExtrinsicBaseWeight (#5722)
* Introduce `BlockExectionWeight` and `ExtrinsicBaseWeight` * Add new traits everywhere * Missed one update * fix tests * Update `check_weight` logic * introduce `max_extrinsic_weight` function * fix + add tests * format nits * remove println * make test a bit more clear * Remove minimum weight * newlines left over from find/replace * Fix test, improve clarity * Fix executor tests * Extrinsic base weight same as old `MINIMUM_WEIGHT` * fix example test * Expose constants * Add test for full block with operational and normal * Initiate test environment with `BlockExecutionWeight` weight * format nit * Update frame/system/src/lib.rs Co-Authored-By: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Replace `TransactionBaseFee` with `ExtrinsicBaseWeight` (#5761) * Replace `TransactionBaseFee` with `ExtrinsicBaseFee` * Fix stuff * Fix and make tests better * Forgot to update this test * Fix priority number in test * Remove minimum weight from merge * Fix weight in contracts * remove `TransactionBaseFee` from contract tests * Let `register_extra_weight_unchecked` go past `MaximumBlockWeight` * address feedback Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
@@ -192,8 +192,9 @@ where
|
||||
frame_system::InitKind::Full,
|
||||
);
|
||||
<frame_system::Module<System> as OnInitialize<System::BlockNumber>>::on_initialize(*block_number);
|
||||
let weight = <AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number);
|
||||
<frame_system::Module<System>>::register_extra_weight_unchecked(weight);
|
||||
let weight = <AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number)
|
||||
.saturating_add(<System::BlockExecutionWeight as frame_support::traits::Get<_>>::get());
|
||||
<frame_system::Module::<System>>::register_extra_weight_unchecked(weight);
|
||||
|
||||
frame_system::Module::<System>::note_finished_initialize();
|
||||
}
|
||||
@@ -398,12 +399,12 @@ mod tests {
|
||||
use sp_core::H256;
|
||||
use sp_runtime::{
|
||||
generic::Era, Perbill, DispatchError, testing::{Digest, Header, Block},
|
||||
traits::{Header as HeaderT, BlakeTwo256, IdentityLookup, ConvertInto},
|
||||
traits::{Header as HeaderT, BlakeTwo256, IdentityLookup, Convert, ConvertInto},
|
||||
transaction_validity::{InvalidTransaction, UnknownTransaction, TransactionValidityError},
|
||||
};
|
||||
use frame_support::{
|
||||
impl_outer_event, impl_outer_origin, parameter_types, impl_outer_dispatch,
|
||||
weights::Weight,
|
||||
weights::{Weight, RuntimeDbWeight},
|
||||
traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons, WithdrawReason},
|
||||
};
|
||||
use frame_system::{self as system, Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo};
|
||||
@@ -475,6 +476,12 @@ mod tests {
|
||||
pub const MaximumBlockWeight: Weight = 1024;
|
||||
pub const MaximumBlockLength: u32 = 2 * 1024;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::one();
|
||||
pub const BlockExecutionWeight: Weight = 10;
|
||||
pub const ExtrinsicBaseWeight: Weight = 5;
|
||||
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
|
||||
read: 10,
|
||||
write: 100,
|
||||
};
|
||||
}
|
||||
impl frame_system::Trait for Runtime {
|
||||
type Origin = Origin;
|
||||
@@ -489,20 +496,24 @@ mod tests {
|
||||
type Event = MetaEvent;
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type MaximumBlockWeight = MaximumBlockWeight;
|
||||
type DbWeight = ();
|
||||
type DbWeight = DbWeight;
|
||||
type BlockExecutionWeight = BlockExecutionWeight;
|
||||
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = RuntimeVersion;
|
||||
type ModuleToIndex = ();
|
||||
type AccountData = pallet_balances::AccountData<u64>;
|
||||
type AccountData = pallet_balances::AccountData<Balance>;
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
}
|
||||
|
||||
type Balance = u64;
|
||||
parameter_types! {
|
||||
pub const ExistentialDeposit: u64 = 1;
|
||||
pub const ExistentialDeposit: Balance = 1;
|
||||
}
|
||||
impl pallet_balances::Trait for Runtime {
|
||||
type Balance = u64;
|
||||
type Balance = Balance;
|
||||
type Event = MetaEvent;
|
||||
type DustRemoval = ();
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
@@ -510,13 +521,11 @@ mod tests {
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const TransactionBaseFee: u64 = 10;
|
||||
pub const TransactionByteFee: u64 = 0;
|
||||
pub const TransactionByteFee: Balance = 0;
|
||||
}
|
||||
impl pallet_transaction_payment::Trait for Runtime {
|
||||
type Currency = Balances;
|
||||
type OnTransactionPayment = ();
|
||||
type TransactionBaseFee = TransactionBaseFee;
|
||||
type TransactionByteFee = TransactionByteFee;
|
||||
type WeightToFee = ConvertInto;
|
||||
type FeeMultiplierUpdate = ();
|
||||
@@ -563,7 +572,7 @@ mod tests {
|
||||
type TestXt = sp_runtime::testing::TestXt<Call, SignedExtra>;
|
||||
type Executive = super::Executive<Runtime, Block<TestXt>, ChainContext<Runtime>, Runtime, AllModules>;
|
||||
|
||||
fn extra(nonce: u64, fee: u64) -> SignedExtra {
|
||||
fn extra(nonce: u64, fee: Balance) -> SignedExtra {
|
||||
(
|
||||
frame_system::CheckEra::from(Era::Immortal),
|
||||
frame_system::CheckNonce::from(nonce),
|
||||
@@ -572,7 +581,7 @@ mod tests {
|
||||
)
|
||||
}
|
||||
|
||||
fn sign_extra(who: u64, nonce: u64, fee: u64) -> Option<(u64, SignedExtra)> {
|
||||
fn sign_extra(who: u64, nonce: u64, fee: Balance) -> Option<(u64, SignedExtra)> {
|
||||
Some((who, extra(nonce, fee)))
|
||||
}
|
||||
|
||||
@@ -583,7 +592,8 @@ mod tests {
|
||||
balances: vec![(1, 211)],
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
let xt = TestXt::new(Call::Balances(BalancesCall::transfer(2, 69)), sign_extra(1, 0, 0));
|
||||
let weight = xt.get_dispatch_info().weight as u64;
|
||||
let weight = xt.get_dispatch_info().weight + <Runtime as frame_system::Trait>::ExtrinsicBaseWeight::get();
|
||||
let fee: Balance = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::convert(weight);
|
||||
let mut t = sp_io::TestExternalities::new(t);
|
||||
t.execute_with(|| {
|
||||
Executive::initialize_block(&Header::new(
|
||||
@@ -595,12 +605,12 @@ mod tests {
|
||||
));
|
||||
let r = Executive::apply_extrinsic(xt);
|
||||
assert!(r.is_ok());
|
||||
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 142 - 10 - weight);
|
||||
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 142 - fee);
|
||||
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&2), 69);
|
||||
});
|
||||
}
|
||||
|
||||
fn new_test_ext(balance_factor: u64) -> sp_io::TestExternalities {
|
||||
fn new_test_ext(balance_factor: Balance) -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
|
||||
pallet_balances::GenesisConfig::<Runtime> {
|
||||
balances: vec![(1, 111 * balance_factor)],
|
||||
@@ -683,8 +693,10 @@ mod tests {
|
||||
let xt = TestXt::new(Call::Balances(BalancesCall::transfer(33, 0)), sign_extra(1, 0, 0));
|
||||
let encoded = xt.encode();
|
||||
let encoded_len = encoded.len() as Weight;
|
||||
let limit = AvailableBlockRatio::get() * MaximumBlockWeight::get() - 175;
|
||||
let num_to_exhaust_block = limit / encoded_len;
|
||||
// Block execution weight + on_initialize weight
|
||||
let base_block_weight = 175 + <Runtime as frame_system::Trait>::BlockExecutionWeight::get();
|
||||
let limit = AvailableBlockRatio::get() * MaximumBlockWeight::get() - base_block_weight;
|
||||
let num_to_exhaust_block = limit / (encoded_len + 5);
|
||||
t.execute_with(|| {
|
||||
Executive::initialize_block(&Header::new(
|
||||
1,
|
||||
@@ -693,8 +705,8 @@ mod tests {
|
||||
[69u8; 32].into(),
|
||||
Digest::default(),
|
||||
));
|
||||
// Initial block weight form the custom module.
|
||||
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), 175);
|
||||
// Base block execution weight + `on_initialize` weight from the custom module.
|
||||
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), base_block_weight);
|
||||
|
||||
for nonce in 0..=num_to_exhaust_block {
|
||||
let xt = TestXt::new(
|
||||
@@ -705,7 +717,8 @@ mod tests {
|
||||
assert!(res.is_ok());
|
||||
assert_eq!(
|
||||
<frame_system::Module<Runtime>>::all_extrinsics_weight(),
|
||||
encoded_len * (nonce + 1) + 175,
|
||||
//--------------------- on_initialize + block_execution + extrinsic_base weight
|
||||
(encoded_len + 5) * (nonce + 1) + base_block_weight,
|
||||
);
|
||||
assert_eq!(<frame_system::Module<Runtime>>::extrinsic_index(), Some(nonce as u32 + 1));
|
||||
} else {
|
||||
@@ -731,7 +744,10 @@ mod tests {
|
||||
assert!(Executive::apply_extrinsic(x2.clone()).unwrap().is_ok());
|
||||
|
||||
// default weight for `TestXt` == encoded length.
|
||||
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), (3 * len) as Weight);
|
||||
assert_eq!(
|
||||
<frame_system::Module<Runtime>>::all_extrinsics_weight(),
|
||||
3 * (len as Weight + <Runtime as frame_system::Trait>::ExtrinsicBaseWeight::get()),
|
||||
);
|
||||
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 3 * len);
|
||||
|
||||
let _ = <frame_system::Module<Runtime>>::finalize();
|
||||
@@ -761,7 +777,7 @@ mod tests {
|
||||
let execute_with_lock = |lock: WithdrawReasons| {
|
||||
let mut t = new_test_ext(1);
|
||||
t.execute_with(|| {
|
||||
<pallet_balances::Module<Runtime> as LockableCurrency<u64>>::set_lock(
|
||||
<pallet_balances::Module<Runtime> as LockableCurrency<Balance>>::set_lock(
|
||||
id,
|
||||
&1,
|
||||
110,
|
||||
@@ -771,7 +787,9 @@ mod tests {
|
||||
Call::System(SystemCall::remark(vec![1u8])),
|
||||
sign_extra(1, 0, 0),
|
||||
);
|
||||
let weight = xt.get_dispatch_info().weight as u64;
|
||||
let weight = xt.get_dispatch_info().weight
|
||||
+ <Runtime as frame_system::Trait>::ExtrinsicBaseWeight::get();
|
||||
let fee: Balance = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::convert(weight);
|
||||
Executive::initialize_block(&Header::new(
|
||||
1,
|
||||
H256::default(),
|
||||
@@ -783,7 +801,7 @@ mod tests {
|
||||
if lock == WithdrawReasons::except(WithdrawReason::TransactionPayment) {
|
||||
assert!(Executive::apply_extrinsic(xt).unwrap().is_ok());
|
||||
// tx fee has been deducted.
|
||||
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 111 - 10 - weight);
|
||||
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 111 - fee);
|
||||
} else {
|
||||
assert_eq!(
|
||||
Executive::apply_extrinsic(xt),
|
||||
@@ -803,9 +821,10 @@ mod tests {
|
||||
new_test_ext(1).execute_with(|| {
|
||||
|
||||
Executive::initialize_block(&Header::new_from_number(1));
|
||||
// NOTE: might need updates over time if system and balance introduce new weights. For
|
||||
// now only accounts for the custom module.
|
||||
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), 150 + 25);
|
||||
// NOTE: might need updates over time if new weights are introduced.
|
||||
// For now it only accounts for the base block execution weight and
|
||||
// the `on_initialize` weight defined in the custom test module.
|
||||
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight(), 175 + 10);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user