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
@@ -30,10 +30,7 @@ use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_core::Bytes;
use sp_rpc::number::NumberOrHex;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, MaybeDisplay},
};
use sp_runtime::traits::{Block as BlockT, MaybeDisplay};
pub use pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi as TransactionPaymentRuntimeApi;
@@ -98,7 +95,7 @@ where
at: Option<Block::Hash>,
) -> RpcResult<RuntimeDispatchInfo<Balance, sp_weights::OldWeight>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
let encoded_len = encoded_xt.len() as u32;
@@ -119,7 +116,7 @@ where
}
let api_version = api
.api_version::<dyn TransactionPaymentRuntimeApi<Block, Balance>>(&at)
.api_version::<dyn TransactionPaymentRuntimeApi<Block, Balance>>(at_hash)
.map_err(|e| map_err(e, "Failed to get transaction payment runtime api version"))?
.ok_or_else(|| {
CallError::Custom(ErrorObject::owned(
@@ -131,11 +128,11 @@ where
if api_version < 2 {
#[allow(deprecated)]
api.query_info_before_version_2(&at, uxt, encoded_len)
api.query_info_before_version_2(at_hash, uxt, encoded_len)
.map_err(|e| map_err(e, "Unable to query dispatch info.").into())
} else {
let res = api
.query_info(&at, uxt, encoded_len)
.query_info(at_hash, uxt, encoded_len)
.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(RuntimeDispatchInfo {
@@ -152,7 +149,7 @@ where
at: Option<Block::Hash>,
) -> RpcResult<FeeDetails<NumberOrHex>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));
let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);
let encoded_len = encoded_xt.len() as u32;
@@ -163,7 +160,7 @@ where
Some(format!("{:?}", e)),
))
})?;
let fee_details = api.query_fee_details(&at, uxt, encoded_len).map_err(|e| {
let fee_details = api.query_fee_details(at_hash, uxt, encoded_len).map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to query fee details.",