diff --git a/Cargo.lock b/Cargo.lock index 4f65c50a0..19e0ed840 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -969,9 +969,11 @@ dependencies = [ "dvm-rpc-runtime-api", "ethereum-primitives", "evm", + "frame-benchmarking", "frame-executive", "frame-support", "frame-system", + "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", "log", @@ -1373,6 +1375,7 @@ name = "darwinia-cli" version = "0.11.2" dependencies = [ "darwinia-service", + "frame-benchmarking-cli", "log", "sc-cli", "sc-client-api", @@ -1718,9 +1721,11 @@ dependencies = [ "darwinia-tron-backing", "darwinia-vesting", "ethereum-primitives", + "frame-benchmarking", "frame-executive", "frame-support", "frame-system", + "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", "log", @@ -1806,6 +1811,7 @@ dependencies = [ "dp-rpc", "dvm-dynamic-fee", "dvm-rpc-runtime-api", + "frame-benchmarking", "frame-system-rpc-runtime-api", "futures 0.3.16", "log", @@ -2132,7 +2138,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4d33be9473d06f75f58220f71f7a9317aca647dc061dbd3c361b0bef505fbea" dependencies = [ "byteorder", - "quick-error", + "quick-error 1.2.3", ] [[package]] @@ -2727,6 +2733,29 @@ dependencies = [ "sp-storage", ] +[[package]] +name = "frame-benchmarking-cli" +version = "3.0.0" +source = "git+https://github.com/darwinia-network/substrate.git?tag=darwinia-v0.11.2#1d7f6e12c651d776fc0dc1adefd007bb60f60b63" +dependencies = [ + "Inflector", + "chrono", + "frame-benchmarking", + "handlebars", + "parity-scale-codec", + "sc-cli", + "sc-client-db", + "sc-executor", + "sc-service", + "serde", + "sp-core", + "sp-externalities", + "sp-keystore", + "sp-runtime", + "sp-state-machine", + "structopt", +] + [[package]] name = "frame-election-provider-support" version = "3.0.0" @@ -2843,6 +2872,20 @@ dependencies = [ "sp-version", ] +[[package]] +name = "frame-system-benchmarking" +version = "3.0.0" +source = "git+https://github.com/darwinia-network/substrate.git?tag=darwinia-v0.11.2#1d7f6e12c651d776fc0dc1adefd007bb60f60b63" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "parity-scale-codec", + "sp-core", + "sp-runtime", + "sp-std", +] + [[package]] name = "frame-system-rpc-runtime-api" version = "3.0.0" @@ -3230,6 +3273,20 @@ dependencies = [ "tracing-futures", ] +[[package]] +name = "handlebars" +version = "3.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4498fc115fa7d34de968184e473529abb40eeb6be8bc5f7faba3d08c316cb3e3" +dependencies = [ + "log", + "pest", + "pest_derive", + "quick-error 2.0.1", + "serde", + "serde_json", +] + [[package]] name = "hash-db" version = "0.15.2" @@ -3414,7 +3471,7 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" dependencies = [ - "quick-error", + "quick-error 1.2.3", ] [[package]] @@ -4633,6 +4690,12 @@ dependencies = [ "libc", ] +[[package]] +name = "maplit" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" + [[package]] name = "match_cfg" version = "0.1.0" @@ -5187,6 +5250,7 @@ name = "pallet-collective" version = "3.0.0" source = "git+https://github.com/darwinia-network/substrate.git?tag=darwinia-v0.11.2#1d7f6e12c651d776fc0dc1adefd007bb60f60b63" dependencies = [ + "frame-benchmarking", "frame-support", "frame-system", "log", @@ -5792,6 +5856,40 @@ dependencies = [ "ucd-trie", ] +[[package]] +name = "pest_derive" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "833d1ae558dc601e9a60366421196a8d94bc0ac980476d0b67e1d0988d72b2d0" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99b8db626e31e5b81787b9783425769681b347011cc59471e33ea46d2ea0cf55" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54be6e404f5317079812fc8f9f5279de376d8856929e21c184ecf6bbd692a11d" +dependencies = [ + "maplit", + "pest", + "sha-1 0.8.2", +] + [[package]] name = "petgraph" version = "0.5.1" @@ -6080,6 +6178,12 @@ version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" +[[package]] +name = "quick-error" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" + [[package]] name = "quicksink" version = "0.1.2" @@ -6418,7 +6522,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52e44394d2086d010551b14b53b1f24e31647570cd1deb0379e2c21b329aba00" dependencies = [ "hostname", - "quick-error", + "quick-error 1.2.3", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 3ec441b65..dc24dbc90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,8 @@ dev = [ try-runtime = ["darwinia-cli/try-runtime"] +runtime-benchmarks = ["darwinia-cli/runtime-benchmarks"] + [workspace] members = [ "cli", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 6aee3e8e3..1781d3891 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -33,6 +33,8 @@ sc-network = { optional = true, git = "https://github.com/darwinia-network/ sc-service = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } sc-tracing = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } try-runtime-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } +# benchmark +frame-benchmarking-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } # substrate primitives sp-core = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } # this crate is used only to enable `trie-memory-tracker` feature @@ -71,3 +73,8 @@ try-runtime = [ "darwinia-service/try-runtime", "try-runtime-cli", ] + +runtime-benchmarks = [ + "frame-benchmarking-cli", + "darwinia-service/runtime-benchmarks", +] diff --git a/cli/src/cli.rs b/cli/src/cli.rs index 628cbb563..a8eb3df5f 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -112,6 +112,11 @@ pub enum Subcommand { /// testing. #[cfg(feature = "try-runtime")] TryRuntime(try_runtime_cli::TryRuntimeCmd), + + /// The custom benchmark subcommmand benchmarking runtime pallets. + #[cfg(feature = "runtime-benchmarks")] + #[structopt(name = "benchmark", about = "Benchmark runtime pallets.")] + Benchmark(frame_benchmarking_cli::BenchmarkCmd), } #[derive(Debug, StructOpt)] diff --git a/cli/src/command.rs b/cli/src/command.rs index a134d9d94..65f03e178 100644 --- a/cli/src/command.rs +++ b/cli/src/command.rs @@ -379,5 +379,21 @@ pub fn run() -> sc_cli::Result<()> { }) } } + #[cfg(feature = "runtime-benchmarks")] + Some(Subcommand::Benchmark(cmd)) => { + let runner = cli.create_runner(cmd)?; + let chain_spec = &runner.config().chain_spec; + + if chain_spec.is_crab() { + runner.sync_run(|config| cmd.run::(config)) + } else if chain_spec.is_darwinia() { + runner + .sync_run(|config| cmd.run::(config)) + } else { + Err("Benchmarking wasn't enabled when building the node. \ + You can enable it with `--features runtime-benchmarks`." + .into()) + } + } } } diff --git a/file_header.txt b/file_header.txt new file mode 100644 index 000000000..0751e299c --- /dev/null +++ b/file_header.txt @@ -0,0 +1,17 @@ +// This file is part of Darwinia. +// +// Copyright (C) 2018-2021 Darwinia Network +// SPDX-License-Identifier: GPL-3.0 +// +// Darwinia is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Darwinia is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Darwinia. If not, see . diff --git a/node/service/Cargo.toml b/node/service/Cargo.toml index edb14fc51..a07ac5ce8 100644 --- a/node/service/Cargo.toml +++ b/node/service/Cargo.toml @@ -51,6 +51,7 @@ sc-service = { git = "https://github.com/darwinia-network/substrate. sc-telemetry = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } sc-transaction-pool = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } # substrate frame +frame-benchmarking = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } frame-system-rpc-runtime-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } pallet-im-online = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } @@ -92,3 +93,8 @@ try-runtime = [ "crab-runtime/try-runtime", "darwinia-runtime/try-runtime", ] + +runtime-benchmarks = [ + "crab-runtime/runtime-benchmarks", + "darwinia-runtime/runtime-benchmarks", +] diff --git a/node/service/src/service/crab.rs b/node/service/src/service/crab.rs index f7601fa18..60ce26110 100644 --- a/node/service/src/service/crab.rs +++ b/node/service/src/service/crab.rs @@ -72,6 +72,7 @@ native_executor_instance!( pub CrabExecutor, crab_runtime::api::dispatch, crab_runtime::native_version, + frame_benchmarking::benchmarking::HostFunctions, ); impl_runtime_apis!(dvm_rpc_runtime_api::EthereumRuntimeRPCApi); diff --git a/node/service/src/service/darwinia.rs b/node/service/src/service/darwinia.rs index c6b0d4e5d..fac7e5f76 100644 --- a/node/service/src/service/darwinia.rs +++ b/node/service/src/service/darwinia.rs @@ -63,6 +63,7 @@ native_executor_instance!( pub DarwiniaExecutor, darwinia_runtime::api::dispatch, darwinia_runtime::native_version, + frame_benchmarking::benchmarking::HostFunctions, ); impl_runtime_apis!(); diff --git a/runtime/crab/Cargo.toml b/runtime/crab/Cargo.toml index 20a631f8a..65efc4d5b 100644 --- a/runtime/crab/Cargo.toml +++ b/runtime/crab/Cargo.toml @@ -51,6 +51,8 @@ ethereum-primitives = { default-features = false, git = "https://github.com/darw # darwinia runtime darwinia-runtime-common = { default-features = false, path = "../common" } # substrate frame +frame-benchmarking = { optional=true, default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } +frame-system-benchmarking = { optional=true, default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } frame-executive = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } @@ -193,3 +195,12 @@ try-runtime = [ on-chain-release-build = [ "sp-api/disable-logging", ] + +runtime-benchmarks = [ + "frame-benchmarking", + "frame-system-benchmarking", + "frame-system/runtime-benchmarks", + "frame-support/runtime-benchmarks", + "pallet-collective/runtime-benchmarks", + "pallet-society/runtime-benchmarks", +] diff --git a/runtime/crab/src/lib.rs b/runtime/crab/src/lib.rs index 8bdd1125f..4eb56e0ad 100644 --- a/runtime/crab/src/lib.rs +++ b/runtime/crab/src/lib.rs @@ -644,6 +644,28 @@ sp_api::impl_runtime_apis! { Ok((weight, RuntimeBlockWeights::get().max_block)) } } + + #[cfg(feature = "runtime-benchmarks")] + impl frame_benchmarking::Benchmark for Runtime { + fn dispatch_benchmark( + config: frame_benchmarking::BenchmarkConfig + ) -> Result, sp_runtime::RuntimeString> { + use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey}; + use frame_system_benchmarking::Pallet as SystemBench; + impl frame_system_benchmarking::Config for Runtime {} + + let whitelist: Vec = vec![]; + + let mut batches = Vec::::new(); + let params = (&config, &whitelist); + + + add_benchmark!(params, batches, frame_system, SystemBench::); + + if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } + Ok(batches) + } + } } pub struct TransactionConverter; diff --git a/runtime/darwinia/Cargo.toml b/runtime/darwinia/Cargo.toml index f83d27532..5c7a7599c 100644 --- a/runtime/darwinia/Cargo.toml +++ b/runtime/darwinia/Cargo.toml @@ -42,6 +42,8 @@ ethereum-primitives = { default-features = false, git = "https://github.com/darw # darwinia runtime darwinia-runtime-common = { default-features = false, path = "../common" } # substrate frame +frame-benchmarking = { optional=true, default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } +frame-system-benchmarking = { optional=true, default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } frame-executive = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "darwinia-v0.11.2" } @@ -173,3 +175,12 @@ try-runtime = [ on-chain-release-build = [ "sp-api/disable-logging", ] + +runtime-benchmarks = [ + "frame-benchmarking", + "frame-system-benchmarking", + "frame-support/runtime-benchmarks", + "frame-system/runtime-benchmarks", + "pallet-collective/runtime-benchmarks", + "pallet-society/runtime-benchmarks", +] diff --git a/runtime/darwinia/src/lib.rs b/runtime/darwinia/src/lib.rs index bc708b08e..febe19e9a 100644 --- a/runtime/darwinia/src/lib.rs +++ b/runtime/darwinia/src/lib.rs @@ -564,6 +564,27 @@ impl_runtime_apis! { Ok((weight, RuntimeBlockWeights::get().max_block)) } } + + #[cfg(feature = "runtime-benchmarks")] + impl frame_benchmarking::Benchmark for Runtime { + fn dispatch_benchmark( + config: frame_benchmarking::BenchmarkConfig + ) -> Result, sp_runtime::RuntimeString> { + use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey}; + use frame_system_benchmarking::Pallet as SystemBench; + impl frame_system_benchmarking::Config for Runtime {} + + let whitelist: Vec = vec![]; + + let mut batches = Vec::::new(); + let params = (&config, &whitelist); + + add_benchmark!(params, batches, frame_system, SystemBench::); + + if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } + Ok(batches) + } + } } pub struct CustomOnRuntimeUpgrade;