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
+10 -10
View File
@@ -185,7 +185,7 @@ pub mod pallet {
let dispatch_infos = calls.iter().map(|call| call.get_dispatch_info()).collect::<Vec<_>>();
let dispatch_weight = dispatch_infos.iter()
.map(|di| di.weight)
.fold(0, |total: Weight, weight: Weight| total.saturating_add(weight))
.fold(Weight::new(), |total: Weight, weight: Weight| total.saturating_add(weight))
.saturating_add(T::WeightInfo::batch(calls.len() as u32));
let dispatch_class = {
let all_operational = dispatch_infos.iter()
@@ -208,7 +208,7 @@ pub mod pallet {
ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::<T>::TooManyCalls);
// Track the actual weight of each of the batch calls.
let mut weight: Weight = 0;
let mut weight = Weight::new();
for (index, call) in calls.into_iter().enumerate() {
let info = call.get_dispatch_info();
// If origin is root, don't apply any dispatch filters; root can call anything.
@@ -253,9 +253,9 @@ pub mod pallet {
let dispatch_info = call.get_dispatch_info();
(
T::WeightInfo::as_derivative()
.saturating_add(dispatch_info.weight)
// AccountData for inner call origin accountdata.
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
.saturating_add(T::DbWeight::get().reads_writes(1, 1))
.saturating_add(dispatch_info.weight),
dispatch_info.class,
)
})]
@@ -301,7 +301,7 @@ pub mod pallet {
let dispatch_infos = calls.iter().map(|call| call.get_dispatch_info()).collect::<Vec<_>>();
let dispatch_weight = dispatch_infos.iter()
.map(|di| di.weight)
.fold(0, |total: Weight, weight: Weight| total.saturating_add(weight))
.fold(Weight::new(), |total: Weight, weight: Weight| total.saturating_add(weight))
.saturating_add(T::WeightInfo::batch_all(calls.len() as u32));
let dispatch_class = {
let all_operational = dispatch_infos.iter()
@@ -324,7 +324,7 @@ pub mod pallet {
ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::<T>::TooManyCalls);
// Track the actual weight of each of the batch calls.
let mut weight: Weight = 0;
let mut weight = Weight::new();
for (index, call) in calls.into_iter().enumerate() {
let info = call.get_dispatch_info();
// If origin is root, bypass any dispatch filter; root can call anything.
@@ -352,7 +352,7 @@ pub mod pallet {
}
Self::deposit_event(Event::BatchCompleted);
let base_weight = T::WeightInfo::batch_all(calls_len as u32);
Ok(Some(base_weight + weight).into())
Ok(Some(base_weight.saturating_add(weight)).into())
}
/// Dispatches a function call with a provided origin.
@@ -406,7 +406,7 @@ pub mod pallet {
let dispatch_infos = calls.iter().map(|call| call.get_dispatch_info()).collect::<Vec<_>>();
let dispatch_weight = dispatch_infos.iter()
.map(|di| di.weight)
.fold(0, |total: Weight, weight: Weight| total.saturating_add(weight))
.fold(Weight::zero(), |total: Weight, weight: Weight| total.saturating_add(weight))
.saturating_add(T::WeightInfo::force_batch(calls.len() as u32));
let dispatch_class = {
let all_operational = dispatch_infos.iter()
@@ -429,7 +429,7 @@ pub mod pallet {
ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::<T>::TooManyCalls);
// Track the actual weight of each of the batch calls.
let mut weight: Weight = 0;
let mut weight = Weight::new();
// Track failed dispatch occur.
let mut has_error: bool = false;
for call in calls.into_iter() {
@@ -455,7 +455,7 @@ pub mod pallet {
Self::deposit_event(Event::BatchCompleted);
}
let base_weight = T::WeightInfo::batch(calls_len as u32);
Ok(Some(base_weight + weight).into())
Ok(Some(base_weight.saturating_add(weight)).into())
}
}
}
+15 -15
View File
@@ -99,7 +99,7 @@ frame_support::construct_runtime!(
parameter_types! {
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(Weight::max_value());
frame_system::limits::BlockWeights::simple_max(Weight::MAX);
}
impl frame_system::Config for Test {
type BaseCallFilter = TestBaseCallFilter;
@@ -191,7 +191,7 @@ fn call_transfer(dest: u64, value: u64) -> Call {
Call::Balances(BalancesCall::transfer { dest, value })
}
fn call_foobar(err: bool, start_weight: u64, end_weight: Option<u64>) -> Call {
fn call_foobar(err: bool, start_weight: Weight, end_weight: Option<Weight>) -> Call {
Call::Example(ExampleCall::foobar { err, start_weight, end_weight })
}
@@ -213,8 +213,8 @@ fn as_derivative_works() {
#[test]
fn as_derivative_handles_weight_refund() {
new_test_ext().execute_with(|| {
let start_weight = 100;
let end_weight = 75;
let start_weight = Weight::from_ref_time(100);
let end_weight = Weight::from_ref_time(75);
let diff = start_weight - end_weight;
// Full weight when ok
@@ -364,24 +364,24 @@ fn batch_weight_calculation_doesnt_overflow() {
use sp_runtime::Perbill;
new_test_ext().execute_with(|| {
let big_call = Call::System(SystemCall::fill_block { ratio: Perbill::from_percent(50) });
assert_eq!(big_call.get_dispatch_info().weight, Weight::max_value() / 2);
assert_eq!(big_call.get_dispatch_info().weight, Weight::MAX / 2);
// 3 * 50% saturates to 100%
let batch_call = Call::Utility(crate::Call::batch {
calls: vec![big_call.clone(), big_call.clone(), big_call.clone()],
});
assert_eq!(batch_call.get_dispatch_info().weight, Weight::max_value());
assert_eq!(batch_call.get_dispatch_info().weight, Weight::MAX);
});
}
#[test]
fn batch_handles_weight_refund() {
new_test_ext().execute_with(|| {
let start_weight = 100;
let end_weight = 75;
let start_weight = Weight::from_ref_time(100);
let end_weight = Weight::from_ref_time(75);
let diff = start_weight - end_weight;
let batch_len: Weight = 4;
let batch_len = 4;
// Full weight when ok
let inner_call = call_foobar(false, start_weight, None);
@@ -420,7 +420,7 @@ fn batch_handles_weight_refund() {
let good_call = call_foobar(false, start_weight, Some(end_weight));
let bad_call = call_foobar(true, start_weight, Some(end_weight));
let batch_calls = vec![good_call, bad_call];
let batch_len = batch_calls.len() as Weight;
let batch_len = Weight::from_ref_time(batch_calls.len() as u64);
let call = Call::Utility(UtilityCall::batch { calls: batch_calls });
let info = call.get_dispatch_info();
let result = call.dispatch(Origin::signed(1));
@@ -494,10 +494,10 @@ fn batch_all_revert() {
#[test]
fn batch_all_handles_weight_refund() {
new_test_ext().execute_with(|| {
let start_weight = 100;
let end_weight = 75;
let start_weight = Weight::from_ref_time(100);
let end_weight = Weight::from_ref_time(75);
let diff = start_weight - end_weight;
let batch_len: Weight = 4;
let batch_len = 4;
// Full weight when ok
let inner_call = call_foobar(false, start_weight, None);
@@ -533,7 +533,7 @@ fn batch_all_handles_weight_refund() {
let good_call = call_foobar(false, start_weight, Some(end_weight));
let bad_call = call_foobar(true, start_weight, Some(end_weight));
let batch_calls = vec![good_call, bad_call];
let batch_len = batch_calls.len() as Weight;
let batch_len = Weight::from_ref_time(batch_calls.len() as u64);
let call = Call::Utility(UtilityCall::batch_all { calls: batch_calls });
let info = call.get_dispatch_info();
let result = call.dispatch(Origin::signed(1));
@@ -616,7 +616,7 @@ fn force_batch_works() {
Origin::signed(1),
vec![
call_transfer(2, 5),
call_foobar(true, 75, None),
call_foobar(true, Weight::from_ref_time(75), None),
call_transfer(2, 10),
call_transfer(2, 5),
]
+17 -17
View File
@@ -41,7 +41,7 @@
#![allow(unused_parens)]
#![allow(unused_imports)]
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use frame_support::{traits::Get, weights::{RefTimeWeight, Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;
/// Weight functions needed for pallet_utility.
@@ -58,27 +58,27 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// The range of component `c` is `[0, 1000]`.
fn batch(c: u32, ) -> Weight {
(23_113_000 as Weight)
Weight::from_ref_time(23_113_000 as RefTimeWeight)
// Standard Error: 2_000
.saturating_add((2_701_000 as Weight).saturating_mul(c as Weight))
.saturating_add(Weight::from_ref_time(2_701_000 as RefTimeWeight).scalar_saturating_mul(c as RefTimeWeight))
}
fn as_derivative() -> Weight {
(4_182_000 as Weight)
Weight::from_ref_time(4_182_000 as RefTimeWeight)
}
/// The range of component `c` is `[0, 1000]`.
fn batch_all(c: u32, ) -> Weight {
(18_682_000 as Weight)
Weight::from_ref_time(18_682_000 as RefTimeWeight)
// Standard Error: 1_000
.saturating_add((2_794_000 as Weight).saturating_mul(c as Weight))
.saturating_add(Weight::from_ref_time(2_794_000 as RefTimeWeight).scalar_saturating_mul(c as RefTimeWeight))
}
fn dispatch_as() -> Weight {
(12_049_000 as Weight)
Weight::from_ref_time(12_049_000 as RefTimeWeight)
}
/// The range of component `c` is `[0, 1000]`.
fn force_batch(c: u32, ) -> Weight {
(19_136_000 as Weight)
Weight::from_ref_time(19_136_000 as RefTimeWeight)
// Standard Error: 2_000
.saturating_add((2_697_000 as Weight).saturating_mul(c as Weight))
.saturating_add(Weight::from_ref_time(2_697_000 as RefTimeWeight).scalar_saturating_mul(c as RefTimeWeight))
}
}
@@ -86,26 +86,26 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
impl WeightInfo for () {
/// The range of component `c` is `[0, 1000]`.
fn batch(c: u32, ) -> Weight {
(23_113_000 as Weight)
Weight::from_ref_time(23_113_000 as RefTimeWeight)
// Standard Error: 2_000
.saturating_add((2_701_000 as Weight).saturating_mul(c as Weight))
.saturating_add(Weight::from_ref_time(2_701_000 as RefTimeWeight).scalar_saturating_mul(c as RefTimeWeight))
}
fn as_derivative() -> Weight {
(4_182_000 as Weight)
Weight::from_ref_time(4_182_000 as RefTimeWeight)
}
/// The range of component `c` is `[0, 1000]`.
fn batch_all(c: u32, ) -> Weight {
(18_682_000 as Weight)
Weight::from_ref_time(18_682_000 as RefTimeWeight)
// Standard Error: 1_000
.saturating_add((2_794_000 as Weight).saturating_mul(c as Weight))
.saturating_add(Weight::from_ref_time(2_794_000 as RefTimeWeight).scalar_saturating_mul(c as RefTimeWeight))
}
fn dispatch_as() -> Weight {
(12_049_000 as Weight)
Weight::from_ref_time(12_049_000 as RefTimeWeight)
}
/// The range of component `c` is `[0, 1000]`.
fn force_batch(c: u32, ) -> Weight {
(19_136_000 as Weight)
Weight::from_ref_time(19_136_000 as RefTimeWeight)
// Standard Error: 2_000
.saturating_add((2_697_000 as Weight).saturating_mul(c as Weight))
.saturating_add(Weight::from_ref_time(2_697_000 as RefTimeWeight).scalar_saturating_mul(c as RefTimeWeight))
}
}