mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 11:07:56 +00:00
ca5f10567a
The `BlockBuilderProvider` was a trait that was defined in `sc-block-builder`. The trait was implemented for `Client`. This basically meant that you needed to import `sc-block-builder` any way to have access to the block builder. So, this trait was not providing any real value. This pull request is removing the said trait. Instead of the trait it introduces a builder for creating a `BlockBuilder`. The builder currently has the quite fabulous name `BlockBuilderBuilder` (I'm open to any better name 😅). The rest of the pull request is about replacing the old trait with the new builder. # Downstream code changes If you used `new_block` or `new_block_at` before you now need to switch it over to the new `BlockBuilderBuilder` pattern: ```rust // `new` requires a type that implements `CallApiAt`. let mut block_builder = BlockBuilderBuilder::new(client) // Then you need to specify the hash of the parent block the block will be build on top of .on_parent_block(at) // The block builder also needs the block number of the parent block. // Here it is fetched from the given `client` using the `HeaderBackend` // However, there also exists `with_parent_block_number` for directly passing the number .fetch_parent_block_number(client) .unwrap() // Enable proof recording if required. This call is optional. .enable_proof_recording() // Pass the digests. This call is optional. .with_inherent_digests(digests) .build() .expect("Creates new block builder"); ``` --------- Co-authored-by: Sebastian Kunert <skunert49@gmail.com> Co-authored-by: command-bot <>
117 lines
3.7 KiB
Rust
117 lines
3.7 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) 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.
|
|
|
|
//! Contains the [`BlockCmd`] as entry point for the CLI to execute
|
|
//! the *block* benchmark.
|
|
|
|
use sc_block_builder::BlockBuilderApi;
|
|
use sc_cli::{CliConfiguration, ImportParams, Result, SharedParams};
|
|
use sc_client_api::{Backend as ClientBackend, BlockBackend, StorageProvider, UsageProvider};
|
|
use sp_api::{ApiExt, ProvideRuntimeApi};
|
|
use sp_blockchain::HeaderBackend;
|
|
use sp_runtime::{traits::Block as BlockT, OpaqueExtrinsic};
|
|
|
|
use clap::Parser;
|
|
use std::{fmt::Debug, sync::Arc};
|
|
|
|
use super::bench::{Benchmark, BenchmarkParams};
|
|
|
|
/// Benchmark the execution time of historic blocks.
|
|
///
|
|
/// This can be used to verify that blocks do not use more weight than they consumed
|
|
/// in their `WeightInfo`. Example:
|
|
///
|
|
/// Let's say you are on a Substrate chain and want to verify that the first 3 blocks
|
|
/// did not use more weight than declared which would otherwise be an issue.
|
|
/// To test this with a dev node, first create one with a temp directory:
|
|
///
|
|
/// $ substrate --dev -d /tmp/my-dev --wasm-execution compiled
|
|
///
|
|
/// And wait some time to let it produce 3 blocks. Then benchmark them with:
|
|
///
|
|
/// $ substrate benchmark-block --from 1 --to 3 --dev -d /tmp/my-dev
|
|
/// --wasm-execution compiled --pruning archive
|
|
///
|
|
/// The output will be similar to this:
|
|
///
|
|
/// Block 1 with 1 tx used 77.34% of its weight ( 5,308,964 of 6,864,645 ns)
|
|
/// Block 2 with 1 tx used 77.99% of its weight ( 5,353,992 of 6,864,645 ns)
|
|
/// Block 3 with 1 tx used 75.91% of its weight ( 5,305,938 of 6,989,645 ns)
|
|
///
|
|
/// The percent number is important and indicates how much weight
|
|
/// was used as compared to the consumed weight.
|
|
/// This number should be below 100% for reference hardware.
|
|
#[derive(Debug, Parser)]
|
|
pub struct BlockCmd {
|
|
#[allow(missing_docs)]
|
|
#[clap(flatten)]
|
|
pub shared_params: SharedParams,
|
|
|
|
#[allow(missing_docs)]
|
|
#[clap(flatten)]
|
|
pub import_params: ImportParams,
|
|
|
|
#[allow(missing_docs)]
|
|
#[clap(flatten)]
|
|
pub params: BenchmarkParams,
|
|
|
|
/// Enable the Trie cache.
|
|
///
|
|
/// This should only be used for performance analysis and not for final results.
|
|
#[arg(long)]
|
|
pub enable_trie_cache: bool,
|
|
}
|
|
|
|
impl BlockCmd {
|
|
/// Benchmark the execution time of historic blocks and compare it to their consumed weight.
|
|
///
|
|
/// Output will be printed to console.
|
|
pub fn run<Block, BA, C>(&self, client: Arc<C>) -> Result<()>
|
|
where
|
|
Block: BlockT<Extrinsic = OpaqueExtrinsic>,
|
|
BA: ClientBackend<Block>,
|
|
C: BlockBackend<Block>
|
|
+ ProvideRuntimeApi<Block>
|
|
+ StorageProvider<Block, BA>
|
|
+ UsageProvider<Block>
|
|
+ HeaderBackend<Block>,
|
|
C::Api: ApiExt<Block> + BlockBuilderApi<Block>,
|
|
{
|
|
// Put everything in the benchmark type to have the generic types handy.
|
|
Benchmark::new(client, self.params.clone()).run()
|
|
}
|
|
}
|
|
|
|
// Boilerplate
|
|
impl CliConfiguration for BlockCmd {
|
|
fn shared_params(&self) -> &SharedParams {
|
|
&self.shared_params
|
|
}
|
|
|
|
fn import_params(&self) -> Option<&ImportParams> {
|
|
Some(&self.import_params)
|
|
}
|
|
|
|
fn trie_cache_maximum_size(&self) -> Result<Option<usize>> {
|
|
if self.enable_trie_cache {
|
|
Ok(self.import_params().map(|x| x.trie_cache_maximum_size()).unwrap_or_default())
|
|
} else {
|
|
Ok(None)
|
|
}
|
|
}
|
|
}
|