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

Commit

Permalink
Add benchmark-block
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
ggwpez committed Mar 3, 2022
1 parent f4fb93f commit 9ce7f32
Show file tree
Hide file tree
Showing 17 changed files with 610 additions and 12 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ sc-client-api = { version = "4.0.0-dev", path = "../../../client/api" }
sp-runtime = { version = "6.0.0", path = "../../../primitives/runtime" }
sp-timestamp = { version = "4.0.0-dev", path = "../../../primitives/timestamp" }

# OTY added
sp-consensus-slots = { version = "0.10.0-dev", path = "../../../primitives/consensus/slots" }
sp-consensus-babe = { version = "0.10.0-dev", path = "../../../primitives/consensus/babe" }
sp-inherents = { version = "4.0.0-dev", path = "../../../primitives/inherents" }
log = "0.4.8"

# These dependencies are used for the node template's RPCs
jsonrpc-core = "18.0.0"
sc-rpc = { version = "4.0.0-dev", path = "../../../client/rpc" }
Expand All @@ -57,6 +63,10 @@ node-template-runtime = { version = "4.0.0-dev", path = "../runtime" }

# CLI-specific dependencies
try-runtime-cli = { version = "0.10.0-dev", optional = true, path = "../../../utils/frame/try-runtime/cli" }
# OTY added
sp-keyring = { version = "6.0.0", path = "../../../primitives/keyring" }
node-runtime = { version = "3.0.0-dev", path = "../../node/runtime" }
node-cli = { version = "3.0.0-dev", path = "../../node/cli" }

[build-dependencies]
substrate-build-script-utils = { version = "3.0.0", path = "../../../utils/build-script-utils" }
Expand Down
4 changes: 4 additions & 0 deletions bin/node-template/node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub enum Subcommand {
#[clap(name = "benchmark", about = "Benchmark runtime pallets.")]
Benchmark(frame_benchmarking_cli::BenchmarkCmd),

/// The custom benchmark subcommand benchmarking runtime pallets.
#[clap(name = "benchmark-block", about = "Benchmark runtime pallets.")]
BenchmarkBlock(frame_benchmarking_cli::BlockCmd),

/// Try some command against runtime state.
#[cfg(feature = "try-runtime")]
TryRuntime(try_runtime_cli::TryRuntimeCmd),
Expand Down
75 changes: 74 additions & 1 deletion bin/node-template/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ use crate::{
cli::{Cli, Subcommand},
service,
};
use node_template_runtime::Block;
use node_cli::service::create_extrinsic;
use node_runtime::SystemCall;
use sc_cli::{ChainSpec, RuntimeVersion, SubstrateCli};
use sc_service::PartialComponents;

use std::sync::Arc;

impl SubstrateCli for Cli {
fn impl_name() -> String {
"Substrate Node".into()
Expand Down Expand Up @@ -46,6 +49,47 @@ impl SubstrateCli for Cli {
}
}

use super::service::FullClient;
struct ExtrinsicGen {
client: Arc<FullClient>,
}
use node_runtime::UncheckedExtrinsic;
use node_template_runtime::Block;
use sp_keyring::Sr25519Keyring;
use sp_runtime::{traits::Block as BlockT, OpaqueExtrinsic};
/*
impl frame_benchmarking_cli::block::cmd::ExtrinsicGenerator for ExtrinsicGen {
fn noop(&self, nonce: u32) -> Option<OpaqueExtrinsic> {
let src = Sr25519Keyring::Alice.pair();
let extrinsic: OpaqueExtrinsic = create_extrinsic(
self.client.as_ref(),
src.clone(),
SystemCall::remark { remark: vec![] },
Some(nonce),
)
.into();
None
}
}*/

#[derive(Default)]
struct InherentProv {}

impl frame_benchmarking_cli::block::cmd::BlockInherentDataProvider for InherentProv {
fn providers(
&self,
block: u64,
) -> std::result::Result<Vec<Arc<dyn sp_inherents::InherentDataProvider>>, sp_inherents::Error>
{
log::info!("Creating inherents for block #{}", block);
let d = std::time::Duration::from_millis(0);
let timestamp = sp_timestamp::InherentDataProvider::new(d.into());

Ok(vec![Arc::new(timestamp)])
}
}

/// Parse and run command line arguments
pub fn run() -> sc_cli::Result<()> {
let cli = Cli::from_args();
Expand Down Expand Up @@ -98,6 +142,35 @@ pub fn run() -> sc_cli::Result<()> {
Ok((cmd.run(client, backend), task_manager))
})
},
Some(Subcommand::BenchmarkBlock(cmd)) => {
unimplemented!();
},
/*let runner = cli.create_runner(cmd)?;
runner.async_run(|mut config| {
config.role = sc_service::Role::Full;
let PartialComponents { client, task_manager, backend, import_queue, .. } =
service::new_partial(&config)?;
let db = backend.expose_db();
let storage = backend.expose_storage();
let ext_gen = ExtrinsicGen { client: client.clone() };
let provs: InherentProv = Default::default();
Ok((
cmd.run(
config,
client.clone(),
import_queue,
db,
storage,
client.clone(),
Arc::new(provs),
Arc::new(ext_gen),
),
task_manager,
))
})
},*/
Some(Subcommand::Benchmark(cmd)) =>
if cfg!(feature = "runtime-benchmarks") {
let runner = cli.create_runner(cmd)?;
Expand Down
2 changes: 1 addition & 1 deletion bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl sc_executor::NativeExecutionDispatch for ExecutorDispatch {
}
}

type FullClient =
pub(crate) type FullClient =
sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
Expand Down
6 changes: 6 additions & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ frame-benchmarking-cli = { version = "4.0.0-dev", optional = true, path = "../..
node-inspect = { version = "0.9.0-dev", optional = true, path = "../inspect" }
try-runtime-cli = { version = "0.10.0-dev", optional = true, path = "../../../utils/frame/try-runtime/cli" }

# OTY added
sp-consensus-slots = { version = "0.10.0-dev", path = "../../../primitives/consensus/slots" }
# OTY
sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" }
chrono = "0.4.19"

[target.'cfg(any(target_arch="x86_64", target_arch="aarch64"))'.dependencies]
node-executor = { version = "3.0.0-dev", path = "../executor", features = ["wasmtime"] }
sc-cli = { version = "0.10.0-dev", optional = true, path = "../../../client/cli", features = ["wasmtime"] }
Expand Down
4 changes: 4 additions & 0 deletions bin/node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub enum Subcommand {
#[clap(name = "benchmark", about = "Benchmark runtime pallets.")]
Benchmark(frame_benchmarking_cli::BenchmarkCmd),

/// Sub command for benchmarking the storage speed.
#[clap(name = "benchmark-block", about = "Benchmark TODO.")]
BenchmarkBlock(frame_benchmarking_cli::BlockCmd),

/// Sub command for benchmarking the storage speed.
#[clap(name = "benchmark-storage", about = "Benchmark storage speed.")]
BenchmarkStorage(frame_benchmarking_cli::StorageCmd),
Expand Down
70 changes: 69 additions & 1 deletion bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

use crate::{chain_spec, service, service::new_partial, Cli, Subcommand};
use node_executor::ExecutorDispatch;
use node_runtime::{Block, RuntimeApi};
use node_runtime::RuntimeApi;
use sc_cli::{ChainSpec, Result, RuntimeVersion, SubstrateCli};
use sc_service::PartialComponents;

use std::sync::Arc;

impl SubstrateCli for Cli {
fn impl_name() -> String {
"Substrate Node".into()
Expand Down Expand Up @@ -68,6 +70,46 @@ impl SubstrateCli for Cli {
&node_runtime::VERSION
}
}
struct ExtrinsicGen {
client: Arc<FullClient>,
}
use crate::service::{create_extrinsic, FullClient};
use node_primitives::Block;
use node_runtime::{BalancesCall, SystemCall};
use sp_keyring::Sr25519Keyring;
use sp_runtime::AccountId32;
use sp_runtime::{traits::Block as BlockT, MultiAddress, OpaqueExtrinsic};
impl frame_benchmarking_cli::block::cmd::ExtrinsicGenerator for ExtrinsicGen {
fn noop(&self, nonce: u32) -> Option<OpaqueExtrinsic> {
let src = Sr25519Keyring::Alice.pair();

let extrinsic: OpaqueExtrinsic = create_extrinsic(
self.client.as_ref(),
src.clone(),
SystemCall::remark { remark: vec![] },
Some(nonce),
)
.into();
Some(extrinsic)
}
}

#[derive(Default)]
struct InherentProv {}

impl frame_benchmarking_cli::block::cmd::BlockInherentDataProvider for InherentProv {
fn providers(
&self,
block: u64,
) -> std::result::Result<Vec<Arc<dyn sp_inherents::InherentDataProvider>>, sp_inherents::Error>
{
log::info!("Creating inherents for block #{}", block);
let d = std::time::Duration::from_millis(0);
let timestamp = sp_timestamp::InherentDataProvider::new(d.into());

Ok(vec![Arc::new(timestamp)])
}
}

/// Parse command line arguments into service configuration.
pub fn run() -> Result<()> {
Expand Down Expand Up @@ -95,6 +137,32 @@ pub fn run() -> Result<()> {
You can enable it with `--features runtime-benchmarks`."
.into())
},
Some(Subcommand::BenchmarkBlock(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.async_run(|mut config| {
config.role = sc_service::Role::Full;

let PartialComponents { client, task_manager, backend, .. } = new_partial(&config)?;
let db = backend.expose_db();
let storage = backend.expose_storage();
let ext_gen = ExtrinsicGen { client: client.clone() };

let provs: InherentProv = Default::default();
Ok((
cmd.run(
config,
client.clone(),
client.clone(),
db,
storage,
client.clone(),
Arc::new(provs),
Arc::new(ext_gen),
),
task_manager,
))
})
},
Some(Subcommand::BenchmarkStorage(cmd)) => {
if !cfg!(feature = "runtime-benchmarks") {
return Err("Benchmarking wasn't enabled when building the node. \
Expand Down
5 changes: 1 addition & 4 deletions frame/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,6 @@ impl<T: Config> OnTimestampSet<T::Moment> for Pallet<T> {
let timestamp_slot = moment / slot_duration;
let timestamp_slot = Slot::from(timestamp_slot.saturated_into::<u64>());

assert!(
CurrentSlot::<T>::get() == timestamp_slot,
"Timestamp slot must match `CurrentSlot`"
);
assert!(CurrentSlot::<T>::get() == timestamp_slot, "sdaf`");
}
}
11 changes: 7 additions & 4 deletions frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,10 +842,13 @@ impl<T: Config> OnTimestampSet<T::Moment> for Pallet<T> {
let timestamp_slot = moment / slot_duration;
let timestamp_slot = Slot::from(timestamp_slot.saturated_into::<u64>());

assert!(
CurrentSlot::<T>::get() == timestamp_slot,
"Timestamp slot must match `CurrentSlot`"
);
let c = CurrentSlot::<T>::get();
if c != timestamp_slot {
panic!(
"d: {:?}, moment: {:?}, want: {:?}, got: {:?}",
slot_duration, moment, c, timestamp_slot
);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions utils/frame/benchmarking-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ hex = "0.4.3"
memory-db = "0.29.0"
rand = { version = "0.8.4", features = ["small_rng"] }

sc-block-builder = { version = "0.10.0-dev", path = "../../../client/block-builder" }
sp-inherents = { version = "4.0.0-dev", path = "../../../primitives/inherents" }
futures = "0.3.19"
sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" }
sc-consensus = { version = "0.10.0-dev", path = "../../../client/consensus/common" }

[features]
default = ["db", "sc-client-db/runtime-benchmarks"]
db = ["sc-client-db/with-kvdb-rocksdb", "sc-client-db/with-parity-db"]
Loading

0 comments on commit 9ce7f32

Please sign in to comment.