From 2eb3860fd54488899e43c8234457a89b7a385408 Mon Sep 17 00:00:00 2001 From: Erwan Renaut <73958772+renauter@users.noreply.github.com> Date: Mon, 22 May 2023 08:36:16 -0300 Subject: [PATCH] feat(benchmark): pallet tft price [1/4] (#696) --- .../.maintain/frame-weight-template.hbs | 121 +++++++++--------- substrate-node/node/Cargo.toml | 78 +++++------ substrate-node/pallets/pallet-dao/src/mock.rs | 2 +- .../pallet-smart-contract/src/benchmarking.rs | 38 ++---- .../pallets/pallet-smart-contract/src/mock.rs | 1 + .../pallets/pallet-tft-price/Cargo.toml | 7 + .../pallet-tft-price/src/benchmarking.rs | 57 +++++++++ .../pallets/pallet-tft-price/src/lib.rs | 25 ++-- .../pallets/pallet-tft-price/src/mock.rs | 2 + .../pallets/pallet-tft-price/src/tests.rs | 4 +- .../pallets/pallet-tft-price/src/weights.rs | 106 +++++++++++++++ substrate-node/runtime/Cargo.toml | 15 ++- substrate-node/runtime/src/lib.rs | 2 + 13 files changed, 319 insertions(+), 139 deletions(-) create mode 100644 substrate-node/pallets/pallet-tft-price/src/benchmarking.rs create mode 100644 substrate-node/pallets/pallet-tft-price/src/weights.rs diff --git a/substrate-node/.maintain/frame-weight-template.hbs b/substrate-node/.maintain/frame-weight-template.hbs index 9ce2b4dd1..9c9e29780 100644 --- a/substrate-node/.maintain/frame-weight-template.hbs +++ b/substrate-node/.maintain/frame-weight-template.hbs @@ -1,32 +1,17 @@ -// This file is part of Substrate. - -// Copyright (C) 2022 Threefold Tech -// Copyright (C) 2021 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: Apache-2.0 - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - +{{header}} //! Autogenerated weights for {{pallet}} //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}} -//! DATE: {{date}}, STEPS: {{cmd.steps}}, REPEAT: {{cmd.repeat}}, LOW RANGE: {{cmd.lowest_range_values}}, HIGH RANGE: {{cmd.highest_range_values}} +//! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: {{cmd.repeat}}, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}` +//! HOSTNAME: `{{hostname}}`, CPU: `{{cpuname}}` //! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}} // Executed Command: -{{#each args as |arg|~}} +{{#each args as |arg|}} // {{arg}} {{/each}} +#![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] #![allow(unused_imports)] @@ -35,70 +20,88 @@ use sp_std::marker::PhantomData; /// Weight functions needed for {{pallet}}. pub trait WeightInfo { - {{~#each benchmarks as |benchmark|}} + {{#each benchmarks as |benchmark|}} fn {{benchmark.name~}} ( {{~#each benchmark.components as |c| ~}} {{c.name}}: u32, {{/each~}} ) -> Weight; - {{~/each}} + {{/each}} } /// Weights for {{pallet}} using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); +{{#if (eq pallet "frame_system")}} +impl WeightInfo for SubstrateWeight { +{{else}} impl WeightInfo for SubstrateWeight { - {{~#each benchmarks as |benchmark|}} +{{/if}} + {{#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 { - ({{underscore benchmark.base_weight}} as Weight) - {{~#each benchmark.component_weight as |cw|}} + // Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds. + Weight::from_ref_time({{underscore benchmark.base_weight}}) + {{#each benchmark.component_weight as |cw|}} // Standard Error: {{underscore cw.error}} - .saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)) - {{~/each}} - {{~#if (ne benchmark.base_reads "0")}} - .saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight)) - {{~/if}} - {{~#each benchmark.component_reads as |cr|}} - .saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight))) - {{~/each}} - {{~#if (ne benchmark.base_writes "0")}} - .saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight)) - {{~/if}} - {{~#each benchmark.component_writes as |cw|}} - .saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))) - {{~/each}} + .saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into())) + {{/each}} + {{#if (ne benchmark.base_reads "0")}} + .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}})) + {{/if}} + {{#each benchmark.component_writes as |cw|}} + .saturating_add(T::DbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into()))) + {{/each}} } - {{~/each}} + {{/each}} } // For backwards compatibility and tests impl WeightInfo for () { - {{~#each benchmarks as |benchmark|}} + {{#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 { - ({{underscore benchmark.base_weight}} as Weight) - {{~#each benchmark.component_weight as |cw|}} + // Minimum execution time: {{underscore benchmark.min_execution_time}} nanoseconds. + Weight::from_ref_time({{underscore benchmark.base_weight}}) + {{#each benchmark.component_weight as |cw|}} // Standard Error: {{underscore cw.error}} - .saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)) - {{~/each}} - {{~#if (ne benchmark.base_reads "0")}} - .saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight)) - {{~/if}} - {{~#each benchmark.component_reads as |cr|}} - .saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight))) - {{~/each}} - {{~#if (ne benchmark.base_writes "0")}} - .saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as Weight)) - {{~/if}} - {{~#each benchmark.component_writes as |cw|}} - .saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))) - {{~/each}} + .saturating_add(Weight::from_ref_time({{underscore cw.slope}}).saturating_mul({{cw.name}}.into())) + {{/each}} + {{#if (ne benchmark.base_reads "0")}} + .saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}})) + {{/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}})) + {{/if}} + {{#each benchmark.component_writes as |cw|}} + .saturating_add(RocksDbWeight::get().writes(({{cw.slope}}_u64).saturating_mul({{cw.name}}.into()))) + {{/each}} } - {{~/each}} -} \ No newline at end of file + {{/each}} +} diff --git a/substrate-node/node/Cargo.toml b/substrate-node/node/Cargo.toml index deb712be2..087591d13 100644 --- a/substrate-node/node/Cargo.toml +++ b/substrate-node/node/Cargo.toml @@ -29,41 +29,42 @@ clap = { version = "4.0.9", features = ["derive"] } serde = { version = "1.0.119", features = ["derive"] } serde_json = "1.0.64" -frame-benchmarking = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36" } -frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36" } -frame-benchmarking-cli = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36" } -pallet-transaction-payment-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-basic-authorship = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-cli = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-executor = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-service = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-consensus-aura = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-finality-grandpa-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-keystore = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-network = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-rpc-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sc-transaction-pool-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-block-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-consensus-aura = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } -frame-rpc-system = { package = "substrate-frame-rpc-system", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } +frame-benchmarking = {git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36"} +frame-system = {git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36"} +frame-benchmarking-cli = {git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36"} +pallet-transaction-payment-rpc = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +pallet-transaction-payment = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-basic-authorship = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-cli = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" } +sc-client-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-client-db = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-executor = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-service = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-consensus = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-consensus-aura = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-finality-grandpa = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-finality-grandpa-rpc = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-keystore = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-keyring = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-network = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-rpc = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-rpc-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-telemetry = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-transaction-pool = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sc-transaction-pool-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-api = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-block-builder = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-blockchain = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-consensus = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-consensus-aura = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-core = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-finality-grandpa = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-inherents = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-runtime = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-io = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-timestamp = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +sp-transaction-pool = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} +frame-rpc-system = {package = "substrate-frame-rpc-system", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36"} frame-try-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36", optional = true } try-runtime-cli = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36", optional = true } @@ -71,5 +72,8 @@ tfchain-runtime = { path = '../runtime' } [features] default = [] -runtime-benchmarks = ["tfchain-runtime/runtime-benchmarks"] -try-runtime = ["tfchain-runtime/try-runtime", "try-runtime-cli/try-runtime"] +runtime-benchmarks = [ + "tfchain-runtime/runtime-benchmarks", + "sc-client-db/runtime-benchmarks", +] +try-runtime = [ "tfchain-runtime/try-runtime", "try-runtime-cli/try-runtime"] \ No newline at end of file diff --git a/substrate-node/pallets/pallet-dao/src/mock.rs b/substrate-node/pallets/pallet-dao/src/mock.rs index 1fd72559f..a4f8bdbce 100644 --- a/substrate-node/pallets/pallet-dao/src/mock.rs +++ b/substrate-node/pallets/pallet-dao/src/mock.rs @@ -203,7 +203,7 @@ pub(crate) fn get_relay_input(relay_input: &[u8]) -> RelayInput { } pub(crate) fn get_public_key_input(pk_input: &[u8]) -> PkInput { - Some(BoundedVec::try_from(pk_input.to_vec()).expect("Invalid document hash input.")) + Some(BoundedVec::try_from(pk_input.to_vec()).expect("Invalid public key input.")) } pub(crate) fn get_public_ip_ip_input(ip_input: &[u8]) -> Ip4Input { diff --git a/substrate-node/pallets/pallet-smart-contract/src/benchmarking.rs b/substrate-node/pallets/pallet-smart-contract/src/benchmarking.rs index bf3a630c5..20236a32e 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/benchmarking.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/benchmarking.rs @@ -3,7 +3,7 @@ use super::*; use crate::Pallet as SmartContractModule; -use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller}; +use frame_benchmarking::{account, benchmarks, whitelisted_caller}; use frame_support::assert_ok; use frame_system::RawOrigin; use sp_std::{ @@ -15,7 +15,7 @@ use sp_std::{ use pallet_tfgrid::{ types::{self as pallet_tfgrid_types, LocationInput}, CityNameInput, CountryNameInput, DocumentHashInput, DocumentLinkInput, LatitudeInput, - LongitudeInput, ResourcesInput, TwinIpInput, + LongitudeInput, PkInput, RelayInput, ResourcesInput, }; use tfchain_support::{resources::Resources, types::IP4}; const GIGABYTE: u64 = 1024 * 1024 * 1024; @@ -103,7 +103,7 @@ benchmarks! { let stamp: u64 = 1628082000 * 1000 * 10 * 6000; pallet_timestamp::Pallet::::set_timestamp(stamp.try_into().unwrap()); // run_to_block::(10); - }: _ (RawOrigin::Signed(a1.clone()), 1) + }: _ (RawOrigin::Signed(a1.clone()), 0, 1) // Update here verify { let contract = SmartContractModule::::contracts(1).unwrap(); assert_eq!( @@ -112,23 +112,6 @@ benchmarks! { } } -impl_benchmark_test_suite! {Pallet, crate::tests::new_test_ext(), crate::tests::Test} - -#[cfg(test)] -mod benchmarktests { - use super::*; - use crate::mock::{new_test_ext, TestRuntime}; - use frame_support::assert_ok; - - #[test] - fn test_benchmarks() { - new_test_ext().execute_with(|| { - assert_ok!(test_benchmark_create_node_contract::()); - assert_ok!(test_benchmark_add_nru_reports::()); - }); - } -} - pub fn create_twin(source: T::AccountId) { assert_ok!(pallet_tfgrid::Pallet::::user_accept_tc( RawOrigin::Signed(source.clone()).into(), @@ -136,10 +119,13 @@ pub fn create_twin(source: T::AccountId) { get_document_hash_input(b"some_hash"), )); - let ip = get_twin_ip_input(b"::1"); + let relay = get_relay_input(b"somerelay.io"); + let pk = + get_public_key_input(b"0x6c8fd181adc178cea218e168e8549f0b0ff30627c879db9eac4318927e87c901"); assert_ok!(pallet_tfgrid::Pallet::::create_twin( RawOrigin::Signed(source).into(), - ip + relay, + pk )); } @@ -291,6 +277,10 @@ pub(crate) fn get_document_hash_input(document_hash_input: &[u8]) -> DocumentHas BoundedVec::try_from(document_hash_input.to_vec()).expect("Invalid document hash input.") } -pub(crate) fn get_twin_ip_input(twin_ip_input: &[u8]) -> TwinIpInput { - BoundedVec::try_from(twin_ip_input.to_vec()).expect("Invalid twin ip input.") +pub(crate) fn get_relay_input(relay_input: &[u8]) -> RelayInput { + Some(BoundedVec::try_from(relay_input.to_vec()).expect("Invalid relay input.")) +} + +pub(crate) fn get_public_key_input(pk_input: &[u8]) -> PkInput { + Some(BoundedVec::try_from(pk_input.to_vec()).expect("Invalid public key input.")) } diff --git a/substrate-node/pallets/pallet-smart-contract/src/mock.rs b/substrate-node/pallets/pallet-smart-contract/src/mock.rs index e8f9f49c2..aa281649a 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/mock.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/mock.rs @@ -234,6 +234,7 @@ impl pallet_tft_price::Config for TestRuntime { type AuthorityId = pallet_tft_price::AuthId; type Call = RuntimeCall; type RestrictedOrigin = EnsureRoot; + type WeightInfo = pallet_tft_price::weights::SubstrateWeight; } impl pallet_timestamp::Config for TestRuntime { diff --git a/substrate-node/pallets/pallet-tft-price/Cargo.toml b/substrate-node/pallets/pallet-tft-price/Cargo.toml index c913f394e..3422f8600 100644 --- a/substrate-node/pallets/pallet-tft-price/Cargo.toml +++ b/substrate-node/pallets/pallet-tft-price/Cargo.toml @@ -35,6 +35,9 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v pallet-authorship = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false } pallet-session = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false } +# Benchmarking +frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true } + [dev-dependencies] tfchain-support = { path = "../../support", default-features = false } substrate-validator-set = { path = '../substrate-validator-set' } @@ -45,6 +48,7 @@ std = [ 'codec/std', 'frame-support/std', 'frame-system/std', + 'frame-benchmarking/std', 'lite-json/std', 'sp-io/std', 'sp-runtime/std', @@ -57,6 +61,9 @@ std = [ 'pallet-authorship/std', 'pallet-session/std' ] +runtime-benchmarks = [ + "frame-benchmarking/runtime-benchmarks", +] try-runtime = [ "frame-support/try-runtime", ] \ No newline at end of file diff --git a/substrate-node/pallets/pallet-tft-price/src/benchmarking.rs b/substrate-node/pallets/pallet-tft-price/src/benchmarking.rs new file mode 100644 index 000000000..cbc14ed6e --- /dev/null +++ b/substrate-node/pallets/pallet-tft-price/src/benchmarking.rs @@ -0,0 +1,57 @@ +#![cfg(feature = "runtime-benchmarks")] + +use super::*; +use crate::Pallet as TFTPriceModule; +use frame_benchmarking::benchmarks; +use frame_system::{EventRecord, Pallet as System, RawOrigin}; +use pallet_session::Pallet as Session; + +benchmarks! { + where_clause { + where + T: pallet_session::Config::AccountId>, + } + + // set_prices() + set_prices { + let block = T::BlockNumber::from(1u32); + let price = 500; + + let validators = Session::::validators(); + assert!(validators.len() > 0); + let validator = validators.into_iter().next().unwrap(); + + }: _(RawOrigin::Signed(validator), price, block) + verify { + assert_eq!(TFTPriceModule::::tft_price(), price); + assert_eq!(TFTPriceModule::::average_tft_price(), price); + assert_last_event::(Event::AveragePriceStored(price).into()); + } + + // set_min_tft_price() + set_min_tft_price { + let price = 20; + }: _(RawOrigin::Root, price) + verify { + assert_eq!(TFTPriceModule::::min_tft_price(), price); + } + + // set_max_tft_price() + set_max_tft_price { + let price = 2000; + }: _(RawOrigin::Root, price) + verify { + assert_eq!(TFTPriceModule::::max_tft_price(), price); + } + + // Calling the `impl_benchmark_test_suite` macro inside the `benchmarks` + // block will generate one #[test] function per benchmark + impl_benchmark_test_suite!(TFTPriceModule, crate::mock::ExternalityBuilder::build(), crate::mock::TestRuntime) +} + +fn assert_last_event(generic_event: ::RuntimeEvent) { + let events = System::::events(); + let system_event: ::RuntimeEvent = generic_event.into(); + let EventRecord { event, .. } = &events[events.len() - 1]; + assert_eq!(event, &system_event); +} diff --git a/substrate-node/pallets/pallet-tft-price/src/lib.rs b/substrate-node/pallets/pallet-tft-price/src/lib.rs index cb4e9c309..59dfb3064 100644 --- a/substrate-node/pallets/pallet-tft-price/src/lib.rs +++ b/substrate-node/pallets/pallet-tft-price/src/lib.rs @@ -31,22 +31,26 @@ mod mock; #[cfg(test)] mod tests; +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmarking; + +pub mod weights; + // Re-export pallet items so that they can be accessed from the crate namespace. pub use pallet::*; #[frame_support::pallet] pub mod pallet { - - use frame_support::pallet_prelude::*; - use frame_system::pallet_prelude::*; - - use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, traits::EnsureOrigin}; + use super::weights::WeightInfo; + use crate::KEY_TYPE; + use frame_support::{ + dispatch::DispatchResultWithPostInfo, ensure, pallet_prelude::*, traits::EnsureOrigin, + }; use frame_system::{ ensure_signed, offchain::{AppCrypto, CreateSignedTransaction}, + pallet_prelude::*, }; - - use crate::KEY_TYPE; use sp_core::sr25519::Signature as Sr25519Signature; use sp_runtime::{ app_crypto::{app_crypto, sr25519}, @@ -90,6 +94,7 @@ pub mod pallet { /// Origin for restricted extrinsics /// Can be the root or another origin configured in the runtime type RestrictedOrigin: EnsureOrigin; + type WeightInfo: crate::weights::WeightInfo; } #[pallet::event] @@ -150,7 +155,7 @@ pub mod pallet { #[pallet::call] impl Pallet { #[pallet::call_index(0)] - #[pallet::weight(100_000_000 + T::DbWeight::get().writes(1).ref_time() + T::DbWeight::get().reads(1).ref_time())] + #[pallet::weight(::WeightInfo::set_prices())] pub fn set_prices( origin: OriginFor, price: u32, @@ -167,7 +172,7 @@ pub mod pallet { } #[pallet::call_index(2)] - #[pallet::weight(100_000_000 + T::DbWeight::get().writes(1).ref_time() + T::DbWeight::get().reads(1).ref_time())] + #[pallet::weight(::WeightInfo::set_min_tft_price())] pub fn set_min_tft_price(origin: OriginFor, price: u32) -> DispatchResultWithPostInfo { T::RestrictedOrigin::ensure_origin(origin)?; ensure!( @@ -179,7 +184,7 @@ pub mod pallet { } #[pallet::call_index(3)] - #[pallet::weight(100_000_000 + T::DbWeight::get().writes(1).ref_time() + T::DbWeight::get().reads(1).ref_time())] + #[pallet::weight(::WeightInfo::set_max_tft_price())] pub fn set_max_tft_price(origin: OriginFor, price: u32) -> DispatchResultWithPostInfo { T::RestrictedOrigin::ensure_origin(origin)?; ensure!( diff --git a/substrate-node/pallets/pallet-tft-price/src/mock.rs b/substrate-node/pallets/pallet-tft-price/src/mock.rs index 564bf4759..130f3bf55 100644 --- a/substrate-node/pallets/pallet-tft-price/src/mock.rs +++ b/substrate-node/pallets/pallet-tft-price/src/mock.rs @@ -118,11 +118,13 @@ parameter_types! { pub const UnsignedPriority: u64 = 100; } +use weights; impl Config for TestRuntime { type AuthorityId = pallet_tft_price::AuthId; type Call = RuntimeCall; type RuntimeEvent = RuntimeEvent; type RestrictedOrigin = EnsureRoot; + type WeightInfo = weights::SubstrateWeight; } parameter_types! { diff --git a/substrate-node/pallets/pallet-tft-price/src/tests.rs b/substrate-node/pallets/pallet-tft-price/src/tests.rs index 58121e8c0..c64519f8c 100644 --- a/substrate-node/pallets/pallet-tft-price/src/tests.rs +++ b/substrate-node/pallets/pallet-tft-price/src/tests.rs @@ -30,7 +30,7 @@ fn test_calc_avg_rounding_works() { fn test_set_prices_works() { let mut t = ExternalityBuilder::build(); t.execute_with(|| { - for i in 1..1441 { + for i in 1..11 { let target_block = i * 100; // we set the price every 100 blocks run_to_block(target_block); match TFTPriceModule::set_prices(RuntimeOrigin::signed(alice()), 500, target_block) { @@ -40,7 +40,7 @@ fn test_set_prices_works() { } let queue = TFTPriceModule::queue_transient(); let items = queue.get_all_values(); - assert_eq!(items.len(), 1440); + assert_eq!(items.len(), 10); assert_eq!(TFTPriceModule::tft_price(), 500); assert_eq!(TFTPriceModule::average_tft_price(), 500); diff --git a/substrate-node/pallets/pallet-tft-price/src/weights.rs b/substrate-node/pallets/pallet-tft-price/src/weights.rs new file mode 100644 index 000000000..b02211226 --- /dev/null +++ b/substrate-node/pallets/pallet-tft-price/src/weights.rs @@ -0,0 +1,106 @@ + +//! Autogenerated weights for pallet_tft_price +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-05-15, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `R1-HP-ProBook-630-G8`, CPU: `11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/tfchain +// benchmark +// pallet +// --chain=dev +// --pallet=pallet_tft_price +// --extrinsic=* +// --steps=50 +// --repeat=20 +// --execution=wasm +// --heap-pages=4096 +// --output +// pallets/pallet-tft-price/src/weights.rs +// --template +// ./.maintain/frame-weight-template.hbs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; +use sp_std::marker::PhantomData; + +/// Weight functions needed for pallet_tft_price. +pub trait WeightInfo { + fn set_prices() -> Weight; + fn set_min_tft_price() -> Weight; + fn set_max_tft_price() -> Weight; +} + +/// Weights for pallet_tft_price using the Substrate node and recommended hardware. +pub struct SubstrateWeight(PhantomData); +impl WeightInfo for SubstrateWeight { + // Storage: Session Validators (r:1 w:0) + // Storage: TFTPriceModule BufferRange (r:1 w:1) + // Storage: TFTPriceModule TftPriceHistory (r:2 w:1) + // Storage: TFTPriceModule MinTftPrice (r:1 w:0) + // Storage: TFTPriceModule MaxTftPrice (r:1 w:0) + // Storage: TFTPriceModule TftPrice (r:0 w:1) + // Storage: TFTPriceModule LastBlockSet (r:0 w:1) + // Storage: TFTPriceModule AverageTftPrice (r:0 w:1) + fn set_prices() -> Weight { + // Minimum execution time: 56_471 nanoseconds. + Weight::from_ref_time(57_331_000) + .saturating_add(T::DbWeight::get().reads(6)) + .saturating_add(T::DbWeight::get().writes(5)) + } + // Storage: TFTPriceModule MaxTftPrice (r:1 w:0) + // Storage: TFTPriceModule MinTftPrice (r:0 w:1) + fn set_min_tft_price() -> Weight { + // Minimum execution time: 10_811 nanoseconds. + Weight::from_ref_time(11_099_000) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + // Storage: TFTPriceModule MinTftPrice (r:1 w:0) + // Storage: TFTPriceModule MaxTftPrice (r:0 w:1) + fn set_max_tft_price() -> Weight { + // Minimum execution time: 10_624 nanoseconds. + Weight::from_ref_time(10_932_000) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } +} + +// For backwards compatibility and tests +impl WeightInfo for () { + // Storage: Session Validators (r:1 w:0) + // Storage: TFTPriceModule BufferRange (r:1 w:1) + // Storage: TFTPriceModule TftPriceHistory (r:2 w:1) + // Storage: TFTPriceModule MinTftPrice (r:1 w:0) + // Storage: TFTPriceModule MaxTftPrice (r:1 w:0) + // Storage: TFTPriceModule TftPrice (r:0 w:1) + // Storage: TFTPriceModule LastBlockSet (r:0 w:1) + // Storage: TFTPriceModule AverageTftPrice (r:0 w:1) + fn set_prices() -> Weight { + // Minimum execution time: 56_471 nanoseconds. + Weight::from_ref_time(57_331_000) + .saturating_add(RocksDbWeight::get().reads(6)) + .saturating_add(RocksDbWeight::get().writes(5)) + } + // Storage: TFTPriceModule MaxTftPrice (r:1 w:0) + // Storage: TFTPriceModule MinTftPrice (r:0 w:1) + fn set_min_tft_price() -> Weight { + // Minimum execution time: 10_811 nanoseconds. + Weight::from_ref_time(11_099_000) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } + // Storage: TFTPriceModule MinTftPrice (r:1 w:0) + // Storage: TFTPriceModule MaxTftPrice (r:0 w:1) + fn set_max_tft_price() -> Weight { + // Minimum execution time: 10_624 nanoseconds. + Weight::from_ref_time(10_932_000) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) + } +} diff --git a/substrate-node/runtime/Cargo.toml b/substrate-node/runtime/Cargo.toml index 7e70a5182..bfbe46c10 100644 --- a/substrate-node/runtime/Cargo.toml +++ b/substrate-node/runtime/Cargo.toml @@ -97,21 +97,23 @@ frame-system-rpc-runtime-api = { git = "https://github.com/paritytech/substrate" pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false } pallet-utility = {git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.36"} -frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true } frame-try-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true } try-runtime-cli = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true } -frame-system-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true } hex-literal = { version = "0.3.1", optional = true } +# Used for runtime benchmarking +frame-system-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true } +frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true } + tfchain-support = { path = "../support", default-features = false } [features] default = ['std'] runtime-benchmarks = [ - 'hex-literal', - "frame-benchmarking/runtime-benchmarks", + 'hex-literal', + 'frame-benchmarking/runtime-benchmarks', + 'frame-system-benchmarking/runtime-benchmarks', 'frame-support/runtime-benchmarks', - 'frame-system-benchmarking', 'frame-system/runtime-benchmarks', 'pallet-timestamp/runtime-benchmarks', 'sp-runtime/runtime-benchmarks', @@ -119,6 +121,7 @@ runtime-benchmarks = [ 'pallet-balances/runtime-benchmarks', 'pallet-collective/runtime-benchmarks', 'pallet-smart-contract/runtime-benchmarks', + 'pallet-tft-price/runtime-benchmarks', ] try-runtime = [ "frame-executive/try-runtime", @@ -194,4 +197,4 @@ std = [ 'tfchain-support/std', 'log/std', 'pallet-utility/std', -] +] \ No newline at end of file diff --git a/substrate-node/runtime/src/lib.rs b/substrate-node/runtime/src/lib.rs index 4815bbd4a..a79f81da3 100644 --- a/substrate-node/runtime/src/lib.rs +++ b/substrate-node/runtime/src/lib.rs @@ -432,6 +432,7 @@ impl pallet_tft_price::Config for Runtime { type Call = RuntimeCall; type RuntimeEvent = RuntimeEvent; type RestrictedOrigin = EnsureRootOrCouncilApproval; + type WeightInfo = pallet_tft_price::weights::SubstrateWeight; } impl pallet_validator::Config for Runtime { @@ -777,6 +778,7 @@ extern crate frame_benchmarking; mod benches { define_benchmarks!( [pallet_smart_contract, SmartContractModule] + [pallet_tft_price, TFTPriceModule] // Substrate [frame_benchmarking::baseline, Baseline::] [frame_system, SystemBench::]