BlockId removal: runtime-api refactor (#13255)

* BlockId removal: refactor of runtime API

It changes the arguments of:
- `ApiExt` methods:  `has_api`, `has_api_with`, `api_version`
- `CallApiAt` method: `runtime_version_at`
from: `BlockId<Block>` to: `Block::Hash`

It also changes the first argument of all generated runtime API calls from: `BlockId<Block>` to: `Block::Hash`

This PR is part of BlockId::Number refactoring analysis (paritytech/substrate#11292)

* BlockId removal: refactor of runtime API - tests

- tests adjusted to new runtime API,
- some tests migrated from block number to block hash

* benchmarking-cli: BlockId(0) migrated to info().genesis_hash

`runtime_api.call()` now requires the block hash instead of BlockId::Number.
To access the genesis hash widely used in benchmarking engine the Client
was constrained to satisfy `sp_blockchain::HeaderBackend<Block>` trait
which provides `info().genesis_hash`.

* trivial: api.call(BlockId) -> api.call(Hash)

- Migrated all `runtime_api.calls` to use Hash
- Noteworthy (?):
-- `validate_transaction_blocking` in transaction pool,

* CallApiAtParams::at changed to Block::Hash

* missed doc updated

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

* ".git/.scripts/commands/fmt/fmt.sh"

* BlockId removal: Benchmark::consumed_weight

Little refactor around `Benchmark::consumed_weight`: `BlockId` removed.

* at_hash renamed

* wrong merge fixed

* beefy worker: merged with master

* beefy: tests: missing block problem fixed

* Apply review suggestion

* fix

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: command-bot <>
This commit is contained in:
Michal Kucharczyk
2023-02-20 23:47:21 +01:00
committed by GitHub
parent ac13aaeb2f
commit 7a10154188
52 changed files with 321 additions and 391 deletions
@@ -92,13 +92,12 @@ where
for i in self.params.from..=self.params.to {
let block_num = BlockId::Number(i.into());
let parent_num = BlockId::Number(((i - 1) as u32).into());
let consumed = self.consumed_weight(&block_num)?;
let hash = self.client.expect_block_hash_from_id(&block_num)?;
let consumed = self.consumed_weight(hash)?;
let block = self.client.block(hash)?.ok_or(format!("Block {} not found", block_num))?;
let block = self.unsealed(block.block);
let took = self.measure_block(&block, &parent_num)?;
let took = self.measure_block(&block, *block.header().parent_hash())?;
self.log_weight(i, block.extrinsics().len(), consumed, took);
}
@@ -107,7 +106,7 @@ where
}
/// Return the average *execution* aka. *import* time of the block.
fn measure_block(&self, block: &Block, parent_num: &BlockId<Block>) -> Result<NanoSeconds> {
fn measure_block(&self, block: &Block, parent_hash: Block::Hash) -> Result<NanoSeconds> {
let mut record = Vec::<NanoSeconds>::default();
// Interesting part here:
// Execute the block multiple times and collect stats about its execution time.
@@ -117,7 +116,7 @@ where
let start = Instant::now();
runtime_api
.execute_block(&parent_num, block)
.execute_block(parent_hash, block)
.map_err(|e| Error::Client(RuntimeApiError(e)))?;
record.push(start.elapsed().as_nanos() as NanoSeconds);
@@ -131,7 +130,7 @@ where
///
/// This is the post-dispatch corrected weight and is only available
/// after executing the block.
fn consumed_weight(&self, block: &BlockId<Block>) -> Result<NanoSeconds> {
fn consumed_weight(&self, block_hash: Block::Hash) -> Result<NanoSeconds> {
// Hard-coded key for System::BlockWeight. It could also be passed in as argument
// for the benchmark, but I think this should work as well.
let hash = array_bytes::hex2bytes(
@@ -139,11 +138,10 @@ where
)?;
let key = StorageKey(hash);
let block_hash = self.client.expect_block_hash_from_id(block)?;
let mut raw_weight = &self
.client
.storage(block_hash, &key)?
.ok_or(format!("Could not find System::BlockWeight for block: {}", block))?
.ok_or(format!("Could not find System::BlockWeight for block: {}", block_hash))?
.0[..];
let weight = ConsumedWeight::decode_all(&mut raw_weight)?;
@@ -20,13 +20,13 @@
use sc_block_builder::{BlockBuilderApi, BlockBuilderProvider};
use sc_cli::{Error, Result};
use sc_client_api::Backend as ClientBackend;
use sp_api::{ApiExt, BlockId, Core, ProvideRuntimeApi};
use sp_api::{ApiExt, Core, ProvideRuntimeApi};
use sp_blockchain::{
ApplyExtrinsicFailed::Validity,
Error::{ApplyExtrinsicFailed, RuntimeApiError},
};
use sp_runtime::{
traits::{Block as BlockT, Zero},
traits::Block as BlockT,
transaction_validity::{InvalidTransaction, TransactionValidityError},
Digest, DigestItem, OpaqueExtrinsic,
};
@@ -73,7 +73,9 @@ impl<Block, BA, C> Benchmark<Block, BA, C>
where
Block: BlockT<Extrinsic = OpaqueExtrinsic>,
BA: ClientBackend<Block>,
C: BlockBuilderProvider<BA, Block, C> + ProvideRuntimeApi<Block>,
C: BlockBuilderProvider<BA, Block, C>
+ ProvideRuntimeApi<Block>
+ sp_blockchain::HeaderBackend<Block>,
C::Api: ApiExt<Block, StateBackend = BA::State> + BlockBuilderApi<Block>,
{
/// Create a new [`Self`] from the arguments.
@@ -167,13 +169,13 @@ where
/// Measures the time that it take to execute a block or an extrinsic.
fn measure_block(&self, block: &Block) -> Result<BenchRecord> {
let mut record = BenchRecord::new();
let genesis = BlockId::Number(Zero::zero());
let genesis = self.client.info().genesis_hash;
info!("Running {} warmups...", self.params.warmup);
for _ in 0..self.params.warmup {
self.client
.runtime_api()
.execute_block(&genesis, block.clone())
.execute_block(genesis, block.clone())
.map_err(|e| Error::Client(RuntimeApiError(e)))?;
}
@@ -186,7 +188,7 @@ where
let start = Instant::now();
runtime_api
.execute_block(&genesis, block)
.execute_block(genesis, block)
.map_err(|e| Error::Client(RuntimeApiError(e)))?;
let elapsed = start.elapsed().as_nanos();
@@ -94,7 +94,9 @@ impl ExtrinsicCmd {
where
Block: BlockT<Extrinsic = OpaqueExtrinsic>,
BA: ClientBackend<Block>,
C: BlockBuilderProvider<BA, Block, C> + ProvideRuntimeApi<Block>,
C: BlockBuilderProvider<BA, Block, C>
+ ProvideRuntimeApi<Block>
+ sp_blockchain::HeaderBackend<Block>,
C::Api: ApiExt<Block, StateBackend = BA::State> + BlockBuilderApi<Block>,
{
// Short circuit if --list was specified.
@@ -108,7 +108,9 @@ impl OverheadCmd {
where
Block: BlockT<Extrinsic = OpaqueExtrinsic>,
BA: ClientBackend<Block>,
C: BlockBuilderProvider<BA, Block, C> + ProvideRuntimeApi<Block>,
C: BlockBuilderProvider<BA, Block, C>
+ ProvideRuntimeApi<Block>
+ sp_blockchain::HeaderBackend<Block>,
C::Api: ApiExt<Block, StateBackend = BA::State> + BlockBuilderApi<Block>,
{
if ext_builder.pallet() != "system" || ext_builder.extrinsic() != "remark" {
+9 -9
View File
@@ -32,7 +32,7 @@ use sp_api::ApiExt;
use sp_block_builder::BlockBuilder;
use sp_blockchain::HeaderBackend;
use sp_core::{hexdisplay::HexDisplay, Bytes};
use sp_runtime::{generic::BlockId, legacy, traits};
use sp_runtime::{legacy, traits};
pub use frame_system_rpc_runtime_api::AccountNonceApi;
@@ -101,9 +101,8 @@ where
async fn nonce(&self, account: AccountId) -> RpcResult<Index> {
let api = self.client.runtime_api();
let best = self.client.info().best_hash;
let at = BlockId::hash(best);
let nonce = api.account_nonce(&at, account.clone()).map_err(|e| {
let nonce = api.account_nonce(best, account.clone()).map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to query nonce.",
@@ -120,9 +119,9 @@ where
) -> RpcResult<Bytes> {
self.deny_unsafe.check_if_safe()?;
let api = self.client.runtime_api();
let at = BlockId::<Block>::hash(at.unwrap_or_else(||
let best_hash = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));
self.client.info().best_hash);
let uxt: <Block as traits::Block>::Extrinsic =
Decode::decode(&mut &*extrinsic).map_err(|e| {
@@ -134,7 +133,7 @@ where
})?;
let api_version = api
.api_version::<dyn BlockBuilder<Block>>(&at)
.api_version::<dyn BlockBuilder<Block>>(best_hash)
.map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
@@ -146,13 +145,13 @@ where
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to dry run extrinsic.",
Some(format!("Could not find `BlockBuilder` api for block `{:?}`.", at)),
Some(format!("Could not find `BlockBuilder` api for block `{:?}`.", best_hash)),
))
})?;
let result = if api_version < 6 {
#[allow(deprecated)]
api.apply_extrinsic_before_version_6(&at, uxt)
api.apply_extrinsic_before_version_6(best_hash, uxt)
.map(legacy::byte_sized_error::convert_to_latest)
.map_err(|e| {
CallError::Custom(ErrorObject::owned(
@@ -162,7 +161,7 @@ where
))
})?
} else {
api.apply_extrinsic(&at, uxt).map_err(|e| {
api.apply_extrinsic(best_hash, uxt).map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to dry run extrinsic.",
@@ -220,6 +219,7 @@ mod tests {
use jsonrpsee::{core::Error as JsonRpseeError, types::error::CallError};
use sc_transaction_pool::BasicPool;
use sp_runtime::{
generic::BlockId,
transaction_validity::{InvalidTransaction, TransactionValidityError},
ApplyExtrinsicResult,
};