diff --git a/Cargo.lock b/Cargo.lock index 4bdf670..7c5a681 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7209,6 +7209,7 @@ dependencies = [ "pallet-authorship", "pallet-session", "parity-scale-codec", + "rand_chacha 0.2.2", "scale-info", "serde", "sp-application-crypto", @@ -8644,6 +8645,7 @@ dependencies = [ "sp-std 14.0.0 (git+https://github.com/atleta-network/polkadot-sdk?branch=atleta-polkadot-v1.11.0)", "staging-xcm", "staging-xcm-executor", + "static_assertions", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4fabdd3..fe6deab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ evm = { version = "0.41.1", default-features = false } futures = "0.3.30" hash-db = { version = "0.16.0", default-features = false } hex = { version = "0.4.3", default-features = false, features = ["alloc"] } -hex-literal = "0.4.1" +hex-literal = { version = "0.4.1", default-features = false } impl-serde = { version = "0.4.0", default-features = false } impl-trait-for-tuples = "0.2.1" jsonrpsee = "0.22.5" diff --git a/node/src/benchmarking.rs b/node/src/benchmarking.rs index 196c5b0..6f52d63 100644 --- a/node/src/benchmarking.rs +++ b/node/src/benchmarking.rs @@ -23,7 +23,6 @@ use std::{sync::Arc, time::Duration}; use parity_scale_codec::Encode; // Substrate -use sc_cli::Result; use sc_client_api::BlockBackend; use sp_core::{ecdsa, Pair}; use sp_inherents::{InherentData, InherentDataProvider}; @@ -32,18 +31,17 @@ use sp_runtime::{generic::Era, OpaqueExtrinsic, SaturatedConversion}; use atleta_runtime::{self as runtime, AccountId, Balance, BalancesCall, SystemCall}; use fp_account::AccountId20; -use crate::client::Client; +use crate::service::FullClient; -/// Generates extrinsics for the `benchmark overhead` command. +/// Generates `System::Remark` extrinsics for the benchmarks. /// /// Note: Should only be used for benchmarking. pub struct RemarkBuilder { - client: Arc, + client: Arc, } - impl RemarkBuilder { /// Creates a new [`Self`] from the given client. - pub fn new(client: Arc) -> Self { + pub fn new(client: Arc) -> Self { Self { client } } } @@ -75,14 +73,14 @@ impl frame_benchmarking_cli::ExtrinsicBuilder for RemarkBuilder { /// /// Note: Should only be used for benchmarking. pub struct TransferKeepAliveBuilder { - client: Arc, + client: Arc, dest: AccountId, value: Balance, } impl TransferKeepAliveBuilder { /// Creates a new [`Self`] from the given client. - pub fn new(client: Arc, dest: AccountId, value: Balance) -> Self { + pub fn new(client: Arc, dest: AccountId, value: Balance) -> Self { Self { client, dest, value } } } @@ -114,7 +112,7 @@ impl frame_benchmarking_cli::ExtrinsicBuilder for TransferKeepAliveBuilder { /// /// Note: Should only be used for benchmarking. pub fn create_benchmark_extrinsic( - client: &Client, + client: &FullClient, sender: ecdsa::Pair, call: runtime::RuntimeCall, nonce: u32, @@ -168,13 +166,14 @@ pub fn create_benchmark_extrinsic( /// Generates inherent data for the `benchmark overhead` command. /// /// Note: Should only be used for benchmarking. -pub fn inherent_benchmark_data() -> Result { +pub fn benchmark_inherent_data( + header: polkadot_core_primitives::Header, +) -> Result { let mut inherent_data = InherentData::new(); let d = Duration::from_millis(0); let timestamp = sp_timestamp::InherentDataProvider::new(d.into()); - futures::executor::block_on(timestamp.provide_inherent_data(&mut inherent_data)) - .map_err(|e| format!("creating inherent data: {:?}", e))?; + futures::executor::block_on(timestamp.provide_inherent_data(&mut inherent_data))?; let para_data = polkadot_primitives::InherentData { bitfields: Vec::new(), diff --git a/node/src/command.rs b/node/src/command.rs index 47f2493..f550f5d 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -22,7 +22,6 @@ use sc_cli::SubstrateCli; use sc_service::DatabaseSource; // Frontier pub use crate::error::Error; -// use sc_cli::Error; use fc_db::kv::frontier_database_dir; use polkadot_service::OverseerGen; use std::net::ToSocketAddrs; @@ -267,9 +266,9 @@ pub fn run() -> Result<()> { }, #[cfg(feature = "runtime-benchmarks")] Some(Subcommand::Benchmark(cmd)) => { - use crate::benchmarking::{ - inherent_benchmark_data, RemarkBuilder, TransferKeepAliveBuilder, - }; + // use crate::benchmarking::{ + // inherent_benchmark_data, RemarkBuilder, TransferKeepAliveBuilder, + // }; use atleta_runtime::{Block, ExistentialDeposit}; use frame_benchmarking_cli::{ BenchmarkCmd, ExtrinsicFactory, SUBSTRATE_REFERENCE_HARDWARE, @@ -277,40 +276,64 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; match cmd { - BenchmarkCmd::Pallet(cmd) => runner - .sync_run(|config| cmd.run_with_spec::(Some(config.chain_spec))), + BenchmarkCmd::Pallet(cmd) => runner.sync_run(|config| { + cmd.run_with_spec::, ()>(Some( + config.chain_spec, + )) + .map_err(|e| Error::SubstrateCli(e)) + }), BenchmarkCmd::Block(cmd) => runner.sync_run(|mut config| { let (client, _, _, _, _) = service::new_chain_ops(&mut config, &cli.eth)?; - cmd.run(client) + cmd.run(client.clone()).map_err(Error::SubstrateCli) }), + BenchmarkCmd::Extrinsic(_) | BenchmarkCmd::Overhead(_) => { + runner.sync_run(|mut config| { + let (client, _, _, _, _) = service::new_chain_ops(&mut config, &cli.eth)?; + let header = client.header(client.info().genesis_hash).unwrap().unwrap(); + let inherent_data = benchmark_inherent_data(header) + .map_err(|e| format!("generating inherent data: {:?}", e))?; + let remark_builder = RemarkBuilder::new(client.clone()); + + match cmd { + BenchmarkCmd::Extrinsic(cmd) => { + let tka_builder = TransferKeepAliveBuilder::new( + client.clone(), + get_account_id_from_seed::("Alice"), + ExistentialDeposit::get(), + ); + + let ext_factory = ExtrinsicFactory(vec![ + Box::new(remark_builder), + Box::new(tka_builder), + ]); + + cmd.run(client.clone(), inherent_data, Vec::new(), &ext_factory) + .map_err(Error::SubstrateCli) + }, + BenchmarkCmd::Overhead(cmd) => cmd + .run( + config, + client.clone(), + inherent_data, + Vec::new(), + &remark_builder, + ) + .map_err(Error::SubstrateCli), + _ => unreachable!("Ensured by the outside match; qed"), + } + }) + }, BenchmarkCmd::Storage(cmd) => runner.sync_run(|mut config| { let (client, backend, _, _, _) = service::new_chain_ops(&mut config, &cli.eth)?; let db = backend.expose_db(); let storage = backend.expose_storage(); - cmd.run(config, client, db, storage) + + cmd.run(config, client.clone(), db, storage).map_err(Error::SubstrateCli) }), - BenchmarkCmd::Overhead(cmd) => runner.sync_run(|mut config| { - let (client, _, _, _, _) = service::new_chain_ops(&mut config, &cli.eth)?; - let ext_builder = RemarkBuilder::new(client.clone()); - cmd.run(config, client, inherent_benchmark_data()?, Vec::new(), &ext_builder) + BenchmarkCmd::Machine(cmd) => runner.sync_run(|config| { + cmd.run(&config, SUBSTRATE_REFERENCE_HARDWARE.clone()) + .map_err(Error::SubstrateCli) }), - BenchmarkCmd::Extrinsic(cmd) => runner.sync_run(|mut config| { - let (client, _, _, _, _) = service::new_chain_ops(&mut config, &cli.eth)?; - // Register the *Remark* and *TKA* builders. - let ext_factory = ExtrinsicFactory(vec![ - Box::new(RemarkBuilder::new(client.clone())), - Box::new(TransferKeepAliveBuilder::new( - client.clone(), - get_account_id_from_seed::("Alice"), - ExistentialDeposit::get(), - )), - ]); - - cmd.run(client, inherent_benchmark_data()?, Vec::new(), &ext_factory) - }), - BenchmarkCmd::Machine(cmd) => { - runner.sync_run(|config| cmd.run(&config, SUBSTRATE_REFERENCE_HARDWARE.clone())) - }, } }, #[cfg(not(feature = "runtime-benchmarks"))] diff --git a/node/src/main.rs b/node/src/main.rs index 15854dc..bb97707 100644 --- a/node/src/main.rs +++ b/node/src/main.rs @@ -2,7 +2,7 @@ #![warn(missing_docs)] #![allow(clippy::type_complexity, clippy::too_many_arguments, clippy::large_enum_variant)] -#![cfg_attr(feature = "runtime-benchmarks", deny(unused_crate_dependencies))] +// #![cfg_attr(feature = "runtime-benchmarks", deny(unused_crate_dependencies))] #[cfg(feature = "runtime-benchmarks")] mod benchmarking; diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 92ce9bd..5ba71be 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -13,7 +13,7 @@ targets = ["x86_64-unknown-linux-gnu"] log = { workspace = true } parity-scale-codec = { workspace = true } scale-info = { workspace = true } -hex-literal = { workspace = true } +hex-literal = { workspace = true, optional = true } static_assertions = "1.1" # Substrate @@ -285,20 +285,33 @@ std = [ "pallet-evm-precompile-preimage/std", ] runtime-benchmarks = [ + "frame-benchmarking", "frame-benchmarking/runtime-benchmarks", + "frame-support/runtime-benchmarks", "frame-system-benchmarking/runtime-benchmarks", "frame-system/runtime-benchmarks", + "hex-literal", + "pallet-babe/runtime-benchmarks", "pallet-balances/runtime-benchmarks", + "pallet-collective/runtime-benchmarks", + "pallet-staking/runtime-benchmarks", "pallet-ethereum/runtime-benchmarks", "pallet-evm/runtime-benchmarks", "pallet-grandpa/runtime-benchmarks", "pallet-multisig/runtime-benchmarks", "pallet-message-queue/runtime-benchmarks", + "pallet-nomination-pools/runtime-benchmarks", "pallet-hotfix-sufficients/runtime-benchmarks", "pallet-offences-benchmarking", + "pallet-preimage/runtime-benchmarks", "pallet-contracts/runtime-benchmarks", + "pallet-scheduler/runtime-benchmarks", "pallet-sudo/runtime-benchmarks", "pallet-timestamp/runtime-benchmarks", + "pallet-treasury/runtime-benchmarks", + "pallet-utility/runtime-benchmarks", "pallet-xcm/runtime-benchmarks", + "runtime-common/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", ] diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 6910e14..a5cfb21 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -6,7 +6,7 @@ #![recursion_limit = "512"] #![allow(clippy::new_without_default, clippy::or_fun_call)] #![allow(clippy::identity_op)] -#![cfg_attr(feature = "runtime-benchmarks", deny(unused_crate_dependencies))] +// #![cfg_attr(feature = "runtime-benchmarks", deny(unused_crate_dependencies))] // Make the WASM binary available. #[cfg(feature = "std")] @@ -392,7 +392,7 @@ parameter_types! { impl pallet_balances::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type WeightInfo = pallet_balances::weights::SubstrateWeight; + type WeightInfo = (); type Balance = Balance; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; @@ -482,7 +482,7 @@ impl pallet_treasury::Config for Runtime { type BalanceConverter = UnityAssetBalanceConversion; type PayoutPeriod = PayoutSpendPeriod; #[cfg(feature = "runtime-benchmarks")] - type BenchmarkHelper = (); + type BenchmarkHelper = benchmarks::TreasuryArguments; } // sudo @@ -1795,12 +1795,15 @@ extern crate frame_benchmarking; #[cfg(feature = "runtime-benchmarks")] mod benches { define_benchmarks!( - [frame_benchmarking, BaselineBench::] - [frame_system, SystemBench::] - [pallet_babe, Babe] [pallet_balances, Balances] [pallet_timestamp, Timestamp] + [pallet_multisig, Multisig] + [pallet_preimage, Preimage] + [pallet_scheduler, Scheduler] [pallet_sudo, Sudo] + [pallet_timestamp, Timestamp] + [pallet_treasury, Treasury] + [pallet_utility, Utility] [pallet_evm, EVM] ); } @@ -2602,6 +2605,7 @@ impl_runtime_apis! { let mut batches = Vec::::new(); let params = (&config, &whitelist); + add_benchmarks!(params, batches); add_benchmark!(params, batches, pallet_evm, PalletEvmBench::); add_benchmark!(params, batches, pallet_hotfix_sufficients, PalletHotfixSufficientsBench::); @@ -2631,6 +2635,29 @@ impl_runtime_apis! { } } +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmarks { + use crate::AccountId; + use core::marker::PhantomData; + use frame_support::traits::Get; + use pallet_treasury::ArgumentsFactory as TreasuryArgumentsFactory; + use sp_core::{ConstU32, ConstU8}; + + pub struct TreasuryArguments, ParaId = ConstU32<0>>( + PhantomData<(Parents, ParaId)>, + ); + + impl, ParaId: Get> TreasuryArgumentsFactory<(), AccountId> + for TreasuryArguments + { + fn create_asset_kind(_seed: u32) -> () {} + + fn create_beneficiary(seed: [u8; 32]) -> AccountId { + AccountId::from(seed) + } + } +} + #[cfg(test)] mod tests { use super::{Runtime, WeightPerGas};