Weight v1.5: Opaque Struct (#12138)

* initial idea

* update frame_support

* update a bunch more

* add ord

* adjust RuntimeDbWeight

* frame_system builds

* re-export

* frame_support tests pass

* frame_executive compile

* frame_executive builds

* frame_system tests passing

* pallet-utility tests pass

* fix a bunch of pallets

* more

* phragmen

* state-trie-migration

* scheduler and referenda

* pallet-election-provider-multi-phase

* aura

* staking

* more

* babe

* balances

* bunch more

* sudo

* transaction-payment

* asset-tx-payment

* last pallets

* fix alliance merge

* fix node template runtime

* fix pallet-contracts cc @athei

* fix node runtime

* fix compile on runtime-benchmarks feature

* comment

* fix frame-support-test

* fix more tests

* weight regex

* frame system works

* fix a bunch

* more

* more

* more

* more

* more

* more fixes

* update templates

* fix contracts benchmarks

* Update lib.rs

* Update lib.rs

* fix ui

* make scalar saturating mul const

* more const functions

* scalar div

* refactor using constant functions

* move impl

* fix overhead template

* use compactas

* Update lib.rs
This commit is contained in:
Shawn Tabrizi
2022-08-31 12:26:13 +01:00
committed by GitHub
parent 299f4ba541
commit 30951822ba
187 changed files with 5932 additions and 4930 deletions
@@ -101,7 +101,7 @@ impl<T: Config + Send + Sync> SignedExtension for CheckMortality<T> {
mod tests {
use super::*;
use crate::mock::{new_test_ext, System, Test, CALL};
use frame_support::weights::{DispatchClass, DispatchInfo, Pays};
use frame_support::weights::{DispatchClass, DispatchInfo, Pays, Weight};
use sp_core::H256;
#[test]
@@ -126,8 +126,11 @@ mod tests {
#[test]
fn signed_ext_check_era_should_change_longevity() {
new_test_ext().execute_with(|| {
let normal =
DispatchInfo { weight: 100, class: DispatchClass::Normal, pays_fee: Pays::Yes };
let normal = DispatchInfo {
weight: Weight::from_ref_time(100),
class: DispatchClass::Normal,
pays_fee: Pays::Yes,
};
let len = 0_usize;
let ext = (
crate::CheckWeight::<Test>::new(),
@@ -19,7 +19,7 @@ use crate::{limits::BlockWeights, Config, Pallet};
use codec::{Decode, Encode};
use frame_support::{
traits::Get,
weights::{DispatchClass, DispatchInfo, PostDispatchInfo},
weights::{DispatchClass, DispatchInfo, PostDispatchInfo, Weight},
};
use scale_info::TypeInfo;
use sp_runtime::{
@@ -238,7 +238,7 @@ where
}
let unspent = post_info.calc_unspent(info);
if unspent > 0 {
if unspent > Weight::zero() {
crate::BlockWeight::<T>::mutate(|current_weight| {
current_weight.sub(unspent, info.class);
})
@@ -297,7 +297,7 @@ mod tests {
fn check(call: impl FnOnce(&DispatchInfo, usize)) {
new_test_ext().execute_with(|| {
let max = DispatchInfo {
weight: Weight::max_value(),
weight: Weight::MAX,
class: DispatchClass::Mandatory,
..Default::default()
};
@@ -309,7 +309,7 @@ mod tests {
check(|max, len| {
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(max, len));
assert_eq!(System::block_weight().total(), Weight::max_value());
assert_eq!(System::block_weight().total(), Weight::MAX);
assert!(System::block_weight().total() > block_weight_limit());
});
check(|max, len| {
@@ -321,7 +321,8 @@ mod tests {
fn normal_extrinsic_limited_by_maximum_extrinsic_weight() {
new_test_ext().execute_with(|| {
let max = DispatchInfo {
weight: block_weights().get(DispatchClass::Normal).max_extrinsic.unwrap() + 1,
weight: block_weights().get(DispatchClass::Normal).max_extrinsic.unwrap() +
Weight::one(),
class: DispatchClass::Normal,
..Default::default()
};
@@ -347,7 +348,7 @@ mod tests {
let okay =
DispatchInfo { weight, class: DispatchClass::Operational, ..Default::default() };
let max = DispatchInfo {
weight: weight + 1,
weight: weight + Weight::one(),
class: DispatchClass::Operational,
..Default::default()
};
@@ -364,8 +365,8 @@ mod tests {
#[test]
fn register_extra_weight_unchecked_doesnt_care_about_limits() {
new_test_ext().execute_with(|| {
System::register_extra_weight_unchecked(Weight::max_value(), DispatchClass::Normal);
assert_eq!(System::block_weight().total(), Weight::max_value());
System::register_extra_weight_unchecked(Weight::MAX, DispatchClass::Normal);
assert_eq!(System::block_weight().total(), Weight::MAX);
assert!(System::block_weight().total() > block_weight_limit());
});
}
@@ -378,9 +379,10 @@ mod tests {
// 10 is taken for block execution weight
// So normal extrinsic can be 758 weight (-5 for base extrinsic weight)
// And Operational can be 256 to produce a full block (-5 for base)
let max_normal = DispatchInfo { weight: 753, ..Default::default() };
let max_normal =
DispatchInfo { weight: Weight::from_ref_time(753), ..Default::default() };
let rest_operational = DispatchInfo {
weight: 251,
weight: Weight::from_ref_time(251),
class: DispatchClass::Operational,
..Default::default()
};
@@ -388,9 +390,9 @@ mod tests {
let len = 0_usize;
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&max_normal, len));
assert_eq!(System::block_weight().total(), 768);
assert_eq!(System::block_weight().total(), Weight::from_ref_time(768));
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&rest_operational, len));
assert_eq!(block_weight_limit(), 1024);
assert_eq!(block_weight_limit(), Weight::from_ref_time(1024));
assert_eq!(System::block_weight().total(), block_weight_limit());
// Checking single extrinsic should not take current block weight into account.
assert_eq!(CheckWeight::<Test>::check_extrinsic_weight(&rest_operational), Ok(()));
@@ -401,9 +403,10 @@ mod tests {
fn dispatch_order_does_not_effect_weight_logic() {
new_test_ext().execute_with(|| {
// We switch the order of `full_block_with_normal_and_operational`
let max_normal = DispatchInfo { weight: 753, ..Default::default() };
let max_normal =
DispatchInfo { weight: Weight::from_ref_time(753), ..Default::default() };
let rest_operational = DispatchInfo {
weight: 251,
weight: Weight::from_ref_time(251),
class: DispatchClass::Operational,
..Default::default()
};
@@ -412,9 +415,9 @@ mod tests {
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&rest_operational, len));
// Extra 15 here from block execution + base extrinsic weight
assert_eq!(System::block_weight().total(), 266);
assert_eq!(System::block_weight().total(), Weight::from_ref_time(266));
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&max_normal, len));
assert_eq!(block_weight_limit(), 1024);
assert_eq!(block_weight_limit(), Weight::from_ref_time(1024));
assert_eq!(System::block_weight().total(), block_weight_limit());
});
}
@@ -423,11 +426,14 @@ mod tests {
fn operational_works_on_full_block() {
new_test_ext().execute_with(|| {
// An on_initialize takes up the whole block! (Every time!)
System::register_extra_weight_unchecked(Weight::max_value(), DispatchClass::Mandatory);
let dispatch_normal =
DispatchInfo { weight: 251, class: DispatchClass::Normal, ..Default::default() };
System::register_extra_weight_unchecked(Weight::MAX, DispatchClass::Mandatory);
let dispatch_normal = DispatchInfo {
weight: Weight::from_ref_time(251),
class: DispatchClass::Normal,
..Default::default()
};
let dispatch_operational = DispatchInfo {
weight: 251,
weight: Weight::from_ref_time(251),
class: DispatchClass::Operational,
..Default::default()
};
@@ -453,9 +459,9 @@ mod tests {
#[test]
fn signed_ext_check_weight_works_operational_tx() {
new_test_ext().execute_with(|| {
let normal = DispatchInfo { weight: 100, ..Default::default() };
let normal = DispatchInfo { weight: Weight::from_ref_time(100), ..Default::default() };
let op = DispatchInfo {
weight: 100,
weight: Weight::from_ref_time(100),
class: DispatchClass::Operational,
pays_fee: Pays::Yes,
};
@@ -489,7 +495,7 @@ mod tests {
fn signed_ext_check_weight_block_size_works() {
new_test_ext().execute_with(|| {
let normal = DispatchInfo::default();
let normal_limit = normal_weight_limit() as usize;
let normal_limit = normal_weight_limit().ref_time() as usize;
let reset_check_weight = |tx, s, f| {
AllExtrinsicsLen::<Test>::put(0);
let r = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, tx, s);
@@ -505,8 +511,11 @@ mod tests {
reset_check_weight(&normal, normal_limit + 1, true);
// Operational ones don't have this limit.
let op =
DispatchInfo { weight: 0, class: DispatchClass::Operational, pays_fee: Pays::Yes };
let op = DispatchInfo {
weight: Weight::zero(),
class: DispatchClass::Operational,
pays_fee: Pays::Yes,
};
reset_check_weight(&op, normal_limit, false);
reset_check_weight(&op, normal_limit + 100, false);
reset_check_weight(&op, 1024, false);
@@ -518,12 +527,14 @@ mod tests {
fn signed_ext_check_weight_works_normal_tx() {
new_test_ext().execute_with(|| {
let normal_limit = normal_weight_limit();
let small = DispatchInfo { weight: 100, ..Default::default() };
let small = DispatchInfo { weight: Weight::from_ref_time(100), ..Default::default() };
let base_extrinsic = block_weights().get(DispatchClass::Normal).base_extrinsic;
let medium =
DispatchInfo { weight: normal_limit - base_extrinsic, ..Default::default() };
let big =
DispatchInfo { weight: normal_limit - base_extrinsic + 1, ..Default::default() };
let big = DispatchInfo {
weight: normal_limit - base_extrinsic + Weight::one(),
..Default::default()
};
let len = 0_usize;
let reset_check_weight = |i, f, s| {
@@ -538,9 +549,9 @@ mod tests {
}
};
reset_check_weight(&small, false, 0);
reset_check_weight(&medium, false, 0);
reset_check_weight(&big, true, 1);
reset_check_weight(&small, false, Weight::zero());
reset_check_weight(&medium, false, Weight::zero());
reset_check_weight(&big, true, Weight::one());
})
}
@@ -548,49 +559,25 @@ mod tests {
fn signed_ext_check_weight_refund_works() {
new_test_ext().execute_with(|| {
// This is half of the max block weight
let info = DispatchInfo { weight: 512, ..Default::default() };
let post_info =
PostDispatchInfo { actual_weight: Some(128), pays_fee: Default::default() };
let info = DispatchInfo { weight: Weight::from_ref_time(512), ..Default::default() };
let post_info = PostDispatchInfo {
actual_weight: Some(Weight::from_ref_time(128)),
pays_fee: Default::default(),
};
let len = 0_usize;
let base_extrinsic = block_weights().get(DispatchClass::Normal).base_extrinsic;
// We allow 75% for normal transaction, so we put 25% - extrinsic base weight
BlockWeight::<Test>::mutate(|current_weight| {
current_weight.set(0, DispatchClass::Mandatory);
current_weight.set(256 - base_extrinsic, DispatchClass::Normal);
});
let pre = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, &info, len).unwrap();
assert_eq!(BlockWeight::<Test>::get().total(), info.weight + 256);
assert_ok!(CheckWeight::<Test>::post_dispatch(
Some(pre),
&info,
&post_info,
len,
&Ok(())
));
assert_eq!(BlockWeight::<Test>::get().total(), post_info.actual_weight.unwrap() + 256);
})
}
#[test]
fn signed_ext_check_weight_actual_weight_higher_than_max_is_capped() {
new_test_ext().execute_with(|| {
let info = DispatchInfo { weight: 512, ..Default::default() };
let post_info =
PostDispatchInfo { actual_weight: Some(700), pays_fee: Default::default() };
let len = 0_usize;
BlockWeight::<Test>::mutate(|current_weight| {
current_weight.set(0, DispatchClass::Mandatory);
current_weight.set(128, DispatchClass::Normal);
current_weight.set(Weight::zero(), DispatchClass::Mandatory);
current_weight
.set(Weight::from_ref_time(256) - base_extrinsic, DispatchClass::Normal);
});
let pre = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, &info, len).unwrap();
assert_eq!(
BlockWeight::<Test>::get().total(),
info.weight + 128 + block_weights().get(DispatchClass::Normal).base_extrinsic,
info.weight + Weight::from_ref_time(256)
);
assert_ok!(CheckWeight::<Test>::post_dispatch(
@@ -602,7 +589,46 @@ mod tests {
));
assert_eq!(
BlockWeight::<Test>::get().total(),
info.weight + 128 + block_weights().get(DispatchClass::Normal).base_extrinsic,
post_info.actual_weight.unwrap() + Weight::from_ref_time(256)
);
})
}
#[test]
fn signed_ext_check_weight_actual_weight_higher_than_max_is_capped() {
new_test_ext().execute_with(|| {
let info = DispatchInfo { weight: Weight::from_ref_time(512), ..Default::default() };
let post_info = PostDispatchInfo {
actual_weight: Some(Weight::from_ref_time(700)),
pays_fee: Default::default(),
};
let len = 0_usize;
BlockWeight::<Test>::mutate(|current_weight| {
current_weight.set(Weight::zero(), DispatchClass::Mandatory);
current_weight.set(Weight::from_ref_time(128), DispatchClass::Normal);
});
let pre = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, &info, len).unwrap();
assert_eq!(
BlockWeight::<Test>::get().total(),
info.weight +
Weight::from_ref_time(128) +
block_weights().get(DispatchClass::Normal).base_extrinsic,
);
assert_ok!(CheckWeight::<Test>::post_dispatch(
Some(pre),
&info,
&post_info,
len,
&Ok(())
));
assert_eq!(
BlockWeight::<Test>::get().total(),
info.weight +
Weight::from_ref_time(128) +
block_weights().get(DispatchClass::Normal).base_extrinsic,
);
})
}
@@ -611,7 +637,7 @@ mod tests {
fn zero_weight_extrinsic_still_has_base_weight() {
new_test_ext().execute_with(|| {
let weights = block_weights();
let free = DispatchInfo { weight: 0, ..Default::default() };
let free = DispatchInfo { weight: Weight::zero(), ..Default::default() };
let len = 0_usize;
// Initial weight from `weights.base_block`
@@ -630,9 +656,10 @@ mod tests {
// Max block is 1024
// Max normal is 768 (75%)
// Max mandatory is unlimited
let max_normal = DispatchInfo { weight: 753, ..Default::default() };
let max_normal =
DispatchInfo { weight: Weight::from_ref_time(753), ..Default::default() };
let mandatory = DispatchInfo {
weight: 1019,
weight: Weight::from_ref_time(1019),
class: DispatchClass::Mandatory,
..Default::default()
};
@@ -640,10 +667,10 @@ mod tests {
let len = 0_usize;
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&max_normal, len));
assert_eq!(System::block_weight().total(), 768);
assert_eq!(System::block_weight().total(), Weight::from_ref_time(768));
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&mandatory, len));
assert_eq!(block_weight_limit(), 1024);
assert_eq!(System::block_weight().total(), 1024 + 768);
assert_eq!(block_weight_limit(), Weight::from_ref_time(1024));
assert_eq!(System::block_weight().total(), Weight::from_ref_time(1024 + 768));
assert_eq!(CheckWeight::<Test>::check_extrinsic_weight(&mandatory), Ok(()));
});
}
@@ -652,30 +679,36 @@ mod tests {
fn no_max_total_should_still_be_limited_by_max_block() {
// given
let maximum_weight = BlockWeights::builder()
.base_block(0)
.base_block(Weight::zero())
.for_class(DispatchClass::non_mandatory(), |w| {
w.base_extrinsic = 0;
w.max_total = Some(20);
w.base_extrinsic = Weight::zero();
w.max_total = Some(Weight::from_ref_time(20));
})
.for_class(DispatchClass::Mandatory, |w| {
w.base_extrinsic = 0;
w.reserved = Some(5);
w.base_extrinsic = Weight::zero();
w.reserved = Some(Weight::from_ref_time(5));
w.max_total = None;
})
.build_or_panic();
let all_weight = crate::ConsumedWeight::new(|class| match class {
DispatchClass::Normal => 10,
DispatchClass::Operational => 10,
DispatchClass::Mandatory => 0,
DispatchClass::Normal => Weight::from_ref_time(10),
DispatchClass::Operational => Weight::from_ref_time(10),
DispatchClass::Mandatory => Weight::zero(),
});
assert_eq!(maximum_weight.max_block, all_weight.total());
// fits into reserved
let mandatory1 =
DispatchInfo { weight: 5, class: DispatchClass::Mandatory, ..Default::default() };
let mandatory1 = DispatchInfo {
weight: Weight::from_ref_time(5),
class: DispatchClass::Mandatory,
..Default::default()
};
// does not fit into reserved and the block is full.
let mandatory2 =
DispatchInfo { weight: 6, class: DispatchClass::Mandatory, ..Default::default() };
let mandatory2 = DispatchInfo {
weight: Weight::from_ref_time(6),
class: DispatchClass::Mandatory,
..Default::default()
};
// when
assert_ok!(calculate_consumed_weight::<<Test as Config>::Call>(