Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Weight v1.5: Opaque Struct #12138

Merged
merged 60 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
78f8104
initial idea
shawntabrizi Aug 28, 2022
7f01ecd
update frame_support
shawntabrizi Aug 28, 2022
3125a97
update a bunch more
shawntabrizi Aug 28, 2022
c6f9e89
add ord
shawntabrizi Aug 28, 2022
1ff21f3
adjust RuntimeDbWeight
shawntabrizi Aug 28, 2022
1b750b6
frame_system builds
shawntabrizi Aug 28, 2022
927cbfc
re-export
shawntabrizi Aug 28, 2022
abef86e
frame_support tests pass
shawntabrizi Aug 28, 2022
505291e
frame_executive compile
shawntabrizi Aug 28, 2022
58ca521
frame_executive builds
shawntabrizi Aug 29, 2022
155247e
frame_system tests passing
shawntabrizi Aug 29, 2022
57a9608
pallet-utility tests pass
shawntabrizi Aug 29, 2022
43a9822
fix a bunch of pallets
shawntabrizi Aug 29, 2022
18d13aa
more
shawntabrizi Aug 29, 2022
9c7e3c8
phragmen
shawntabrizi Aug 29, 2022
690b2d8
state-trie-migration
shawntabrizi Aug 29, 2022
694d8fc
scheduler and referenda
shawntabrizi Aug 29, 2022
2cf7413
pallet-election-provider-multi-phase
shawntabrizi Aug 29, 2022
32537ca
aura
shawntabrizi Aug 29, 2022
11ee8ae
staking
shawntabrizi Aug 29, 2022
58c95ee
more
shawntabrizi Aug 29, 2022
1568fe5
babe
shawntabrizi Aug 29, 2022
431085f
balances
shawntabrizi Aug 29, 2022
ee8d842
Merge branch 'master' into shawntabrizi-weight-v1.5
shawntabrizi Aug 29, 2022
afca016
bunch more
shawntabrizi Aug 29, 2022
6137298
sudo
shawntabrizi Aug 29, 2022
0ccec6d
transaction-payment
shawntabrizi Aug 29, 2022
9fb9378
asset-tx-payment
shawntabrizi Aug 29, 2022
4d6df92
last pallets
shawntabrizi Aug 29, 2022
06979b6
fix alliance merge
shawntabrizi Aug 29, 2022
06b0b50
fix node template runtime
shawntabrizi Aug 29, 2022
062e866
fix pallet-contracts cc @athei
shawntabrizi Aug 29, 2022
9c7f09a
fix node runtime
shawntabrizi Aug 29, 2022
7261a05
fix compile on runtime-benchmarks feature
shawntabrizi Aug 29, 2022
5a35f8d
comment
shawntabrizi Aug 29, 2022
6bc74aa
fix frame-support-test
shawntabrizi Aug 29, 2022
8a0a0c7
fix more tests
shawntabrizi Aug 29, 2022
ce87ef8
weight regex
shawntabrizi Aug 29, 2022
aed139b
frame system works
shawntabrizi Aug 29, 2022
d63e922
fix a bunch
shawntabrizi Aug 29, 2022
a9a6c66
more
shawntabrizi Aug 29, 2022
8adb1a4
more
shawntabrizi Aug 29, 2022
2d9d5a2
more
shawntabrizi Aug 30, 2022
2df2d77
more
shawntabrizi Aug 30, 2022
f5b5a77
more
shawntabrizi Aug 30, 2022
00a4d13
more fixes
shawntabrizi Aug 30, 2022
3694224
update templates
shawntabrizi Aug 30, 2022
5053f80
fix contracts benchmarks
shawntabrizi Aug 30, 2022
8df72dc
Update lib.rs
shawntabrizi Aug 30, 2022
3f00bff
Update lib.rs
shawntabrizi Aug 30, 2022
7c853e4
fix ui
shawntabrizi Aug 30, 2022
dfdbc3b
make scalar saturating mul const
shawntabrizi Aug 30, 2022
04fd22c
more const functions
shawntabrizi Aug 30, 2022
b0c262b
scalar div
shawntabrizi Aug 30, 2022
5c37fb8
refactor using constant functions
shawntabrizi Aug 31, 2022
8e973c0
move impl
shawntabrizi Aug 31, 2022
44246b5
fix overhead template
shawntabrizi Aug 31, 2022
e698cfe
Merge branch 'master' into shawntabrizi-weight-v1.5
shawntabrizi Aug 31, 2022
b6eb595
use compactas
shawntabrizi Aug 31, 2022
6b3a7d2
Update lib.rs
shawntabrizi Aug 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .maintain/frame-weight-template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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}}.
Expand Down Expand Up @@ -64,22 +64,22 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
({{underscore benchmark.base_weight}} as Weight)
Weight::from_ref_time({{underscore benchmark.base_weight}} as RefTimeWeight)
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
.saturating_add(Weight::from_ref_time({{underscore cw.slope}} as RefTimeWeight).scalar_saturating_mul({{cw.name}} as RefTimeWeight))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight))
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as RefTimeWeight))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait a minute, this type casting never made any sense. base_reads isn't a unit of weight, but simply denotes the number of reads. There really shouldn't be a type cast to RefTimeWeight here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with the other similar type casts in this file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base reads is u32, it needs to be u64 or whatever type RefTimeWeight is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will remove ref_time and this will be just u64 in a follow up pr

{{/if}}
{{#each benchmark.component_reads as |cr|}}
.saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
.saturating_add(T::DbWeight::get().reads(({{cr.slope}} as RefTimeWeight).saturating_mul({{cr.name}} as RefTimeWeight)))
{{/each}}
{{#if (ne benchmark.base_writes "0")}}
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight))
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as RefTimeWeight))
{{/if}}
{{#each benchmark.component_writes as |cw|}}
.saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
.saturating_add(T::DbWeight::get().writes(({{cw.slope}} as RefTimeWeight).saturating_mul({{cw.name}} as RefTimeWeight)))
{{/each}}
}
{{/each}}
Expand All @@ -99,22 +99,22 @@ impl WeightInfo for () {
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
({{underscore benchmark.base_weight}} as Weight)
Weight::from_ref_time({{underscore benchmark.base_weight}} as RefTimeWeight)
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
.saturating_add(Weight::from_ref_time({{underscore cw.slope}} as RefTimeWeight).scalar_saturating_mul({{cw.name}} as RefTimeWeight))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight))
.saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as RefTimeWeight))
{{/if}}
{{#each benchmark.component_reads as |cr|}}
.saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
.saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as RefTimeWeight).saturating_mul({{cr.name}} as RefTimeWeight)))
{{/each}}
{{#if (ne benchmark.base_writes "0")}}
.saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as Weight))
.saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as RefTimeWeight))
{{/if}}
{{#each benchmark.component_writes as |cw|}}
.saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
.saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as RefTimeWeight).saturating_mul({{cw.name}} as RefTimeWeight)))
{{/each}}
}
{{/each}}
Expand Down
4 changes: 2 additions & 2 deletions bin/node-template/pallets/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
/// An example dispatchable that takes a singles value as a parameter, writes the value to
/// storage and emits an event. This function must be dispatched by a signed extrinsic.
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
// Check that the extrinsic was signed and get the signer.
// This function will return an error if the extrinsic is not signed.
Expand All @@ -81,7 +81,7 @@ pub mod pallet {
}

/// An example dispatchable that may throw a custom error.
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
let _who = ensure_signed(origin)?;

Expand Down
6 changes: 3 additions & 3 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use codec::{Decode, Encode, Joiner};
use frame_support::{
traits::Currency,
weights::{DispatchClass, DispatchInfo, GetDispatchInfo},
weights::{DispatchClass, DispatchInfo, GetDispatchInfo, Weight},
};
use frame_system::{self, AccountInfo, EventRecord, Phase};
use sp_core::{storage::well_known_keys, traits::Externalities, NeverNativeValue};
Expand Down Expand Up @@ -733,7 +733,7 @@ fn deploying_wasm_contract_should_work() {
function: Call::Contracts(
pallet_contracts::Call::instantiate_with_code::<Runtime> {
value: 0,
gas_limit: 500_000_000,
gas_limit: Weight::from_ref_time(500_000_000),
storage_deposit_limit: None,
code: transfer_code,
data: Vec::new(),
Expand All @@ -746,7 +746,7 @@ fn deploying_wasm_contract_should_work() {
function: Call::Contracts(pallet_contracts::Call::call::<Runtime> {
dest: sp_runtime::MultiAddress::Id(addr.clone()),
value: 10,
gas_limit: 500_000_000,
gas_limit: Weight::from_ref_time(500_000_000),
storage_deposit_limit: None,
data: vec![0x00, 0x01, 0x02, 0x03],
}),
Expand Down
2 changes: 1 addition & 1 deletion bin/node/executor/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn transaction_fee_is_correct() {
// we know that weight to fee multiplier is effect-less in block 1.
// current weight of transfer = 200_000_000
// Linear weight to fee is 1:1 right now (1 weight = 1 unit of balance)
assert_eq!(weight_fee, weight as Balance);
assert_eq!(weight_fee, weight.ref_time() as Balance);
balance_alice -= base_fee;
balance_alice -= weight_fee;
balance_alice -= tip;
Expand Down
39 changes: 20 additions & 19 deletions bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ mod multiplier_tests {
let previous_float = previous_float.max(min_multiplier().into_inner() as f64 / accuracy);

// maximum tx weight
let m = max_normal() as f64;
let m = max_normal().ref_time() as f64;
// block weight always truncated to max weight
let block_weight = (block_weight as f64).min(m);
let block_weight = (block_weight.ref_time() as f64).min(m);
let v: f64 = AdjustmentVariable::get().to_float();

// Ideal saturation in terms of weight
let ss = target() as f64;
let ss = target().ref_time() as f64;
// Current saturation in terms of weight
let s = block_weight;

Expand Down Expand Up @@ -197,9 +197,9 @@ mod multiplier_tests {
fn truth_value_update_poc_works() {
let fm = Multiplier::saturating_from_rational(1, 2);
let test_set = vec![
(0, fm),
(100, fm),
(1000, fm),
(Weight::zero(), fm),
(Weight::from_ref_time(100), fm),
(Weight::from_ref_time(1000), fm),
(target(), fm),
(max_normal() / 2, fm),
(max_normal(), fm),
Expand Down Expand Up @@ -229,7 +229,7 @@ mod multiplier_tests {
#[test]
fn multiplier_cannot_go_below_limit() {
// will not go any further below even if block is empty.
run_with_system_weight(0, || {
run_with_system_weight(Weight::new(), || {
let next = runtime_multiplier_update(min_multiplier());
assert_eq!(next, min_multiplier());
})
Expand All @@ -247,7 +247,7 @@ mod multiplier_tests {
// 1 < 0.00001 * k * 0.1875
// 10^9 / 1875 < k
// k > 533_333 ~ 18,5 days.
run_with_system_weight(0, || {
run_with_system_weight(Weight::new(), || {
// start from 1, the default.
let mut fm = Multiplier::one();
let mut iterations: u64 = 0;
Expand Down Expand Up @@ -283,7 +283,8 @@ mod multiplier_tests {
// `cargo test congested_chain_simulation -- --nocapture` to get some insight.

// almost full. The entire quota of normal transactions is taken.
let block_weight = BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap() - 100;
let block_weight = BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap() -
Weight::from_ref_time(100);

// Default substrate weight.
let tx_weight = frame_support::weights::constants::ExtrinsicBaseWeight::get();
Expand Down Expand Up @@ -407,27 +408,27 @@ mod multiplier_tests {

#[test]
fn weight_to_fee_should_not_overflow_on_large_weights() {
let kb = 1024 as Weight;
let kb = Weight::from_ref_time(1024);
let mb = kb * kb;
let max_fm = Multiplier::saturating_from_integer(i128::MAX);

// check that for all values it can compute, correctly.
vec![
0,
1,
10,
1000,
Weight::zero(),
Weight::one(),
Weight::from_ref_time(10),
Weight::from_ref_time(1000),
kb,
10 * kb,
100 * kb,
mb,
10 * mb,
2147483647,
4294967295,
Weight::from_ref_time(2147483647),
Weight::from_ref_time(4294967295),
BlockWeights::get().max_block / 2,
BlockWeights::get().max_block,
Weight::max_value() / 2,
Weight::max_value(),
Weight::MAX / 2,
Weight::MAX,
]
.into_iter()
.for_each(|i| {
Expand All @@ -440,7 +441,7 @@ mod multiplier_tests {

// Some values that are all above the target and will cause an increase.
let t = target();
vec![t + 100, t * 2, t * 4].into_iter().for_each(|i| {
vec![t + Weight::from_ref_time(100), t * 2, t * 4].into_iter().for_each(|i| {
run_with_system_weight(i, || {
let fm = runtime_multiplier_update(max_fm);
// won't grow. The convert saturates everything.
Expand Down
6 changes: 3 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
/// by Operational extrinsics.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// We allow for 2 seconds of compute with a 6 second average block time.
const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;
const MAXIMUM_BLOCK_WEIGHT: Weight = WEIGHT_PER_SECOND.scalar_saturating_mul(2);

parameter_types! {
pub const BlockHashCount: BlockNumber = 2400;
Expand Down Expand Up @@ -1936,7 +1936,7 @@ impl_runtime_apis! {
storage_deposit_limit: Option<Balance>,
input_data: Vec<u8>,
) -> pallet_contracts_primitives::ContractExecResult<Balance> {
Contracts::bare_call(origin, dest, value, gas_limit, storage_deposit_limit, input_data, true)
Contracts::bare_call(origin, dest, value, Weight::from_ref_time(gas_limit), storage_deposit_limit, input_data, true)
}

fn instantiate(
Expand All @@ -1949,7 +1949,7 @@ impl_runtime_apis! {
salt: Vec<u8>,
) -> pallet_contracts_primitives::ContractInstantiateResult<AccountId, Balance>
{
Contracts::bare_instantiate(origin, value, gas_limit, storage_deposit_limit, code, data, salt, true)
Contracts::bare_instantiate(origin, value, Weight::from_ref_time(gas_limit), storage_deposit_limit, code, data, salt, true)
}

fn upload_code(
Expand Down
8 changes: 4 additions & 4 deletions frame/alliance/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ benchmarks_instance_pallet! {
// Whitelist voter account from further DB operations.
let voter_key = frame_system::Account::<T>::hashed_key_for(&voter);
frame_benchmarking::benchmarking::add_to_whitelist(voter_key.into());
}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::max_value(), bytes_in_storage)
}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::MAX, bytes_in_storage)
verify {
// The last proposal is removed.
assert_eq!(T::ProposalProvider::proposal_of(last_hash), None);
Expand Down Expand Up @@ -417,7 +417,7 @@ benchmarks_instance_pallet! {
index,
true,
)?;
}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::max_value(), bytes_in_storage)
}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::MAX, bytes_in_storage)
verify {
// The last proposal is removed.
assert_eq!(T::ProposalProvider::proposal_of(last_hash), None);
Expand Down Expand Up @@ -489,7 +489,7 @@ benchmarks_instance_pallet! {

System::<T>::set_block_number(T::BlockNumber::max_value());

}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::max_value(), bytes_in_storage)
}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::MAX, bytes_in_storage)
verify {
// The last proposal is removed.
assert_eq!(T::ProposalProvider::proposal_of(last_hash), None);
Expand Down Expand Up @@ -562,7 +562,7 @@ benchmarks_instance_pallet! {
// caller is prime, prime already votes aye by creating the proposal
System::<T>::set_block_number(T::BlockNumber::max_value());

}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::max_value(), bytes_in_storage)
}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::MAX, bytes_in_storage)
verify {
// The last proposal is removed.
assert_eq!(T::ProposalProvider::proposal_of(last_hash), None);
Expand Down
2 changes: 1 addition & 1 deletion frame/alliance/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
/// Wrapper for all migrations of this pallet.
pub fn migrate<T: Config<I>, I: 'static>() -> Weight {
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
let mut weight: Weight = 0;
let mut weight: Weight = Weight::new();

if onchain_version < 1 {
weight = weight.saturating_add(v0_to_v1::migrate::<T, I>());
Expand Down
Loading