Skip to content

Commit

Permalink
Update pallet weights (#929)
Browse files Browse the repository at this point in the history
* wip : fix e2e test

* update weights

* fmt

* cleanup

* more cleanup

* more cleanup

* lst working

* clippy

* wip

* wip

* wip

* clippy

* clippy

* add scripts

* clippy

* fix subxt

* run weights

* add benchmarks to CI

* clippy

* clippy

* clippy

* fix bench build
  • Loading branch information
1xstj authored Feb 22, 2025
1 parent bbdc48d commit 1dd8be2
Show file tree
Hide file tree
Showing 45 changed files with 1,647 additions and 461 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@ jobs:
- name: Run test suite
run: cargo nextest run --workspace --exclude tangle --profile ci

benchmarks:
env:
CARGO_TERM_COLOR: always

concurrency:
group: benchmark-${{ github.ref }}
cancel-in-progress: true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: ./.github/actions/free-disk-space
- name: Install toolchain
id: toolchain
uses: actions-rs/toolchain@master
with:
profile: minimal
toolchain: stable
target: wasm32-unknown-unknown

- name: Rust Cache
uses: Swatinem/[email protected]

- name: Install Protobuf
run: sudo apt-get install protobuf-compiler

- name: Run test suite
run: cargo b -rp tangle --features runtime-benchmarks

e2e:
env:
CARGO_TERM_COLOR: always
Expand Down
74 changes: 11 additions & 63 deletions .maintain/frame-weights-template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: `{{cmd.repeat}}`, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}`
//! WORST CASE MAP SIZE: `{{cmd.worst_case_map_values}}`
//! HOSTNAME: `{{hostname}}`, CPU: `{{cpuname}}`
//! WASM-EXECUTION: `{{cmd.wasm_execution}}`, CHAIN: `{{cmd.chain}}`, DB CACHE: `{{cmd.db_cache}}`
//! WASM-EXECUTION: `{{cmd.wasm_execution}}`, CHAIN: `{{cmd.chain}}`, DB CACHE: {{cmd.db_cache}}

// Executed Command:
{{#each args as |arg|}}
Expand All @@ -17,26 +17,15 @@
#![allow(unused_imports)]
#![allow(missing_docs)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use frame_support::{traits::Get, weights::Weight};
use core::marker::PhantomData;

/// Weight functions needed for `{{pallet}}`.
pub trait WeightInfo {
{{#each benchmarks as |benchmark|}}
fn {{benchmark.name~}}
(
{{~#each benchmark.components as |c| ~}}
{{c.name}}: u32, {{/each~}}
) -> Weight;
{{/each}}
}

/// Weights for `{{pallet}}` using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
{{#if (eq pallet "frame_system")}}
impl<T: crate::Config> WeightInfo for SubstrateWeight<T> {
/// Weight functions for `{{pallet}}`.
pub struct WeightInfo<T>(PhantomData<T>);
{{#if (eq pallet "frame_system_extensions")}}
impl<T: frame_system::Config> frame_system::ExtensionsWeightInfo for WeightInfo<T> {
{{else}}
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
impl<T: frame_system::Config> {{pallet}}::WeightInfo for WeightInfo<T> {
{{/if}}
{{#each benchmarks as |benchmark|}}
{{#each benchmark.comments as |comment|}}
Expand All @@ -54,19 +43,20 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Measured: `{{benchmark.base_recorded_proof_size}}{{#each benchmark.component_recorded_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Estimated: `{{benchmark.base_calculated_proof_size}}{{#each benchmark.component_calculated_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Minimum execution time: {{underscore benchmark.min_execution_time}}_000 picoseconds.
Weight::from_parts({{underscore benchmark.base_weight}}, {{benchmark.base_calculated_proof_size}})
Weight::from_parts({{underscore benchmark.base_weight}}, 0)
.saturating_add(Weight::from_parts(0, {{benchmark.base_calculated_proof_size}}))
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(Weight::from_parts({{underscore cw.slope}}, 0).saturating_mul({{cw.name}}.into()))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}}_u64))
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}}))
{{/if}}
{{#each benchmark.component_reads as |cr|}}
.saturating_add(T::DbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into())))
{{/each}}
{{#if (ne benchmark.base_writes "0")}}
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}}_u64))
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}}))
{{/if}}
{{#each benchmark.component_writes as |cw|}}
.saturating_add(T::DbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into())))
Expand All @@ -77,45 +67,3 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
}
{{/each}}
}

// For backwards compatibility and tests.
impl WeightInfo for () {
{{#each benchmarks as |benchmark|}}
{{#each benchmark.comments as |comment|}}
/// {{comment}}
{{/each}}
{{#each benchmark.component_ranges as |range|}}
/// The range of component `{{range.name}}` is `[{{range.min}}, {{range.max}}]`.
{{/each}}
fn {{benchmark.name~}}
(
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
// Proof Size summary in bytes:
// Measured: `{{benchmark.base_recorded_proof_size}}{{#each benchmark.component_recorded_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Estimated: `{{benchmark.base_calculated_proof_size}}{{#each benchmark.component_calculated_proof_size as |cp|}} + {{cp.name}} * ({{cp.slope}} ±{{underscore cp.error}}){{/each}}`
// Minimum execution time: {{underscore benchmark.min_execution_time}}_000 picoseconds.
Weight::from_parts({{underscore benchmark.base_weight}}, {{benchmark.base_calculated_proof_size}})
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(Weight::from_parts({{underscore cw.slope}}, 0).saturating_mul({{cw.name}}.into()))
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}}_u64))
{{/if}}
{{#each benchmark.component_reads as |cr|}}
.saturating_add(RocksDbWeight::get().reads(({{cr.slope}}_u64).saturating_mul({{cr.name}}.into())))
{{/each}}
{{#if (ne benchmark.base_writes "0")}}
.saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}}_u64))
{{/if}}
{{#each benchmark.component_writes as |cw|}}
.saturating_add(RocksDbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into())))
{{/each}}
{{#each benchmark.component_calculated_proof_size as |cp|}}
.saturating_add(Weight::from_parts(0, {{cp.slope}}).saturating_mul({{cp.name}}.into()))
{{/each}}
}
{{/each}}
}
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn create_benchmark_extrinsic(
let period = runtime::BlockHashCount::get()
.checked_next_power_of_two()
.map(|c| c / 2)
.unwrap_or(2) as u64;
.unwrap_or(2);
let extra: runtime::SignedExtra = (
frame_system::CheckNonZeroSender::<runtime::Runtime>::new(),
frame_system::CheckSpecVersion::<runtime::Runtime>::new(),
Expand Down
10 changes: 5 additions & 5 deletions node/tests/evm_restaking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod common;

use common::*;
use tangle_subxt::tangle_testnet_runtime::api::runtime_types::pallet_multi_asset_delegation::types::operator::DelegatorBond;
use tangle_subxt::tangle_testnet_runtime::api::runtime_types::tangle_primitives::services::Asset;
use tangle_subxt::tangle_testnet_runtime::api::runtime_types::tangle_primitives::services::types::Asset;

sol! {
#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -333,7 +333,7 @@ where
.sudo(api::runtime_types::tangle_testnet_runtime::RuntimeCall::Rewards(
api::runtime_types::pallet_rewards::pallet::Call::manage_asset_reward_vault {
vault_id,
asset_id: x,
asset: x,
action: api::runtime_types::pallet_rewards::types::AssetAction::Add,
},
))
Expand Down Expand Up @@ -506,7 +506,7 @@ fn operator_join_delegator_delegate_erc20() {
Some(DelegatorBond {
delegator: bob.address().to_account_id(),
amount: delegate_amount.to::<u128>(),
asset_id: Asset::Erc20((<[u8; 20]>::from(*usdc.address())).into()),
asset: Asset::Erc20((<[u8; 20]>::from(*usdc.address())).into()),
__ignore: std::marker::PhantomData
})
);
Expand Down Expand Up @@ -601,7 +601,7 @@ fn operator_join_delegator_delegate_asset_id() {
Some(DelegatorBond {
delegator: bob.address().to_account_id(),
amount: delegate_amount,
asset_id: Asset::Custom(t.usdc_asset_id),
asset: Asset::Custom(t.usdc_asset_id),
__ignore: std::marker::PhantomData
})
);
Expand Down Expand Up @@ -841,7 +841,7 @@ fn lrt_deposit_withdraw_erc20() {
Some(DelegatorBond {
delegator: lrt_address.to_account_id(),
amount: deposit_amount.to::<u128>(),
asset_id: Asset::Erc20((<[u8; 20]>::from(t.weth)).into()),
asset: Asset::Erc20((<[u8; 20]>::from(t.weth)).into()),
__ignore: std::marker::PhantomData
})
);
Expand Down
Loading

0 comments on commit 1dd8be2

Please sign in to comment.