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
@@ -316,7 +316,7 @@ fn generate_runtime_decls(decls: &[ItemTrait]) -> Result<TokenStream> {
/// Modify the given runtime api declaration to be usable on the client side.
struct ToClientSideDecl<'a> {
block_id: &'a TokenStream,
block_hash: &'a TokenStream,
crate_: &'a TokenStream,
found_attributes: &'a mut HashMap<&'static str, Attribute>,
/// Any error that we found while converting this declaration.
@@ -329,7 +329,7 @@ impl<'a> ToClientSideDecl<'a> {
fn process(mut self, decl: ItemTrait) -> ItemTrait {
let mut decl = self.fold_item_trait(decl);
let block_id = self.block_id;
let block_hash = self.block_hash;
let crate_ = self.crate_;
// Add the special method that will be implemented by the `impl_runtime_apis!` macro
@@ -339,7 +339,7 @@ impl<'a> ToClientSideDecl<'a> {
#[doc(hidden)]
fn __runtime_api_internal_call_api_at(
&self,
at: &#block_id,
at: #block_hash,
context: #crate_::ExecutionContext,
params: std::vec::Vec<u8>,
fn_name: &dyn Fn(#crate_::RuntimeVersion) -> &'static str,
@@ -420,7 +420,7 @@ impl<'a> ToClientSideDecl<'a> {
};
let ret_type = return_type_extract_type(&method.sig.output);
fold_fn_decl_for_client_side(&mut method.sig, self.block_id, self.crate_);
fold_fn_decl_for_client_side(&mut method.sig, self.block_hash, self.crate_);
let crate_ = self.crate_;
@@ -621,14 +621,14 @@ fn generate_client_side_decls(decls: &[ItemTrait]) -> Result<TokenStream> {
let decl = decl.clone();
let crate_ = generate_crate_access(HIDDEN_INCLUDES_ID);
let block_id = quote!( #crate_::BlockId<Block> );
let block_hash = quote!( <Block as #crate_::BlockT>::Hash );
let mut found_attributes = HashMap::new();
let mut errors = Vec::new();
let trait_ = decl.ident.clone();
let decl = ToClientSideDecl {
crate_: &crate_,
block_id: &block_id,
block_hash: &block_hash,
found_attributes: &mut found_attributes,
errors: &mut errors,
trait_: &trait_,
@@ -233,7 +233,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
fn has_api<A: #crate_::RuntimeApiInfo + ?Sized>(
&self,
at: &#crate_::BlockId<Block>,
at: <Block as #crate_::BlockT>::Hash,
) -> std::result::Result<bool, #crate_::ApiError> where Self: Sized {
#crate_::CallApiAt::<Block>::runtime_version_at(self.call, at)
.map(|v| #crate_::RuntimeVersion::has_api_with(&v, &A::ID, |v| v == A::VERSION))
@@ -241,7 +241,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
fn has_api_with<A: #crate_::RuntimeApiInfo + ?Sized, P: Fn(u32) -> bool>(
&self,
at: &#crate_::BlockId<Block>,
at: <Block as #crate_::BlockT>::Hash,
pred: P,
) -> std::result::Result<bool, #crate_::ApiError> where Self: Sized {
#crate_::CallApiAt::<Block>::runtime_version_at(self.call, at)
@@ -250,7 +250,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
fn api_version<A: #crate_::RuntimeApiInfo + ?Sized>(
&self,
at: &#crate_::BlockId<Block>,
at: <Block as #crate_::BlockT>::Hash,
) -> std::result::Result<Option<u32>, #crate_::ApiError> where Self: Sized {
#crate_::CallApiAt::<Block>::runtime_version_at(self.call, at)
.map(|v| #crate_::RuntimeVersion::api_version(&v, &A::ID))
@@ -281,8 +281,7 @@ fn generate_runtime_api_base_structures() -> Result<TokenStream> {
#crate_::StorageChanges<C::StateBackend, Block>,
String
> where Self: Sized {
let at = #crate_::BlockId::Hash(std::clone::Clone::clone(&parent_hash));
let state_version = #crate_::CallApiAt::<Block>::runtime_version_at(self.call, &at)
let state_version = #crate_::CallApiAt::<Block>::runtime_version_at(self.call, std::clone::Clone::clone(&parent_hash))
.map(|v| #crate_::RuntimeVersion::state_version(&v))
.map_err(|e| format!("Failed to get state version: {}", e))?;
@@ -424,7 +423,7 @@ impl<'a> ApiRuntimeImplToApiRuntimeApiImpl<'a> {
input.items.push(parse_quote! {
fn __runtime_api_internal_call_api_at(
&self,
at: &#crate_::BlockId<__SR_API_BLOCK__>,
at: <__SR_API_BLOCK__ as #crate_::BlockT>::Hash,
context: #crate_::ExecutionContext,
params: std::vec::Vec<u8>,
fn_name: &dyn Fn(#crate_::RuntimeVersion) -> &'static str,
@@ -38,7 +38,7 @@ const HIDDEN_INCLUDES_ID: &str = "MOCK_IMPL_RUNTIME_APIS";
/// The `advanced` attribute.
///
/// If this attribute is given to a function, the function gets access to the `BlockId` as first
/// If this attribute is given to a function, the function gets access to the `Hash` as first
/// parameter and needs to return a `Result` with the appropriate error type.
const ADVANCED_ATTRIBUTE: &str = "advanced";
@@ -80,14 +80,14 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
fn has_api<A: #crate_::RuntimeApiInfo + ?Sized>(
&self,
_: &#crate_::BlockId<#block_type>,
_: <Block as #crate_::BlockT>::Hash,
) -> std::result::Result<bool, #crate_::ApiError> where Self: Sized {
Ok(true)
}
fn has_api_with<A: #crate_::RuntimeApiInfo + ?Sized, P: Fn(u32) -> bool>(
&self,
_: &#crate_::BlockId<#block_type>,
_: <Block as #crate_::BlockT>::Hash,
pred: P,
) -> std::result::Result<bool, #crate_::ApiError> where Self: Sized {
Ok(pred(A::VERSION))
@@ -95,7 +95,7 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
fn api_version<A: #crate_::RuntimeApiInfo + ?Sized>(
&self,
_: &#crate_::BlockId<#block_type>,
_: <Block as #crate_::BlockT>::Hash,
) -> std::result::Result<Option<u32>, #crate_::ApiError> where Self: Sized {
Ok(Some(A::VERSION))
}
@@ -129,7 +129,7 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
impl #crate_::Core<#block_type> for #self_ty {
fn __runtime_api_internal_call_api_at(
&self,
_: &#crate_::BlockId<#block_type>,
_: <#block_type as #crate_::BlockT>::Hash,
_: #crate_::ExecutionContext,
_: std::vec::Vec<u8>,
_: &dyn Fn(#crate_::RuntimeVersion) -> &'static str,
@@ -139,14 +139,14 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
fn version(
&self,
_: &#crate_::BlockId<#block_type>,
_: <#block_type as #crate_::BlockT>::Hash,
) -> std::result::Result<#crate_::RuntimeVersion, #crate_::ApiError> {
unimplemented!("`Core::version` not implemented for runtime api mocks")
}
fn version_with_context(
&self,
_: &#crate_::BlockId<#block_type>,
_: <#block_type as #crate_::BlockT>::Hash,
_: #crate_::ExecutionContext,
) -> std::result::Result<#crate_::RuntimeVersion, #crate_::ApiError> {
unimplemented!("`Core::version` not implemented for runtime api mocks")
@@ -154,7 +154,7 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
fn execute_block(
&self,
_: &#crate_::BlockId<#block_type>,
_: <#block_type as #crate_::BlockT>::Hash,
_: #block_type,
) -> std::result::Result<(), #crate_::ApiError> {
unimplemented!("`Core::execute_block` not implemented for runtime api mocks")
@@ -162,7 +162,7 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
fn execute_block_with_context(
&self,
_: &#crate_::BlockId<#block_type>,
_: <#block_type as #crate_::BlockT>::Hash,
_: #crate_::ExecutionContext,
_: #block_type,
) -> std::result::Result<(), #crate_::ApiError> {
@@ -171,7 +171,7 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
fn initialize_block(
&self,
_: &#crate_::BlockId<#block_type>,
_: <#block_type as #crate_::BlockT>::Hash,
_: &<#block_type as #crate_::BlockT>::Header,
) -> std::result::Result<(), #crate_::ApiError> {
unimplemented!("`Core::initialize_block` not implemented for runtime api mocks")
@@ -179,7 +179,7 @@ fn implement_common_api_traits(block_type: TypePath, self_ty: Type) -> Result<To
fn initialize_block_with_context(
&self,
_: &#crate_::BlockId<#block_type>,
_: <#block_type as #crate_::BlockT>::Hash,
_: #crate_::ExecutionContext,
_: &<#block_type as #crate_::BlockT>::Header,
) -> std::result::Result<(), #crate_::ApiError> {
@@ -214,7 +214,7 @@ fn get_at_param_name(
param_names: &mut Vec<Pat>,
param_types_and_borrows: &mut Vec<(TokenStream, bool)>,
function_span: Span,
default_block_id_type: &TokenStream,
default_hash_type: &TokenStream,
) -> Result<(TokenStream, TokenStream)> {
if is_advanced {
if param_names.is_empty() {
@@ -222,7 +222,7 @@ fn get_at_param_name(
function_span,
format!(
"If using the `{}` attribute, it is required that the function \
takes at least one argument, the `BlockId`.",
takes at least one argument, the `Hash`.",
ADVANCED_ATTRIBUTE,
),
))
@@ -232,17 +232,14 @@ fn get_at_param_name(
// `param_types` can not be empty as well.
let ptype_and_borrows = param_types_and_borrows.remove(0);
let span = ptype_and_borrows.1.span();
if !ptype_and_borrows.1 {
return Err(Error::new(
span,
"`BlockId` needs to be taken by reference and not by value!",
))
if ptype_and_borrows.1 {
return Err(Error::new(span, "`Hash` needs to be taken by value and not by reference!"))
}
let name = param_names.remove(0);
Ok((quote!( #name ), ptype_and_borrows.0))
} else {
Ok((quote!(_), default_block_id_type.clone()))
Ok((quote!(_), default_hash_type.clone()))
}
}
@@ -279,7 +276,7 @@ impl<'a> FoldRuntimeApiImpl<'a> {
impl_item.items.push(parse_quote! {
fn __runtime_api_internal_call_api_at(
&self,
_: &#crate_::BlockId<#block_type>,
_: <#block_type as #crate_::BlockT>::Hash,
_: #crate_::ExecutionContext,
_: std::vec::Vec<u8>,
_: &dyn Fn(#crate_::RuntimeVersion) -> &'static str,
@@ -325,19 +322,19 @@ impl<'a> Fold for FoldRuntimeApiImpl<'a> {
};
let block_type = &self.block_type;
let block_id_type = quote!( &#crate_::BlockId<#block_type> );
let hash_type = quote!( <#block_type as #crate_::BlockT>::Hash );
let (at_param_name, block_id_type) = match get_at_param_name(
let (at_param_name, hash_type) = match get_at_param_name(
is_advanced,
&mut param_names,
&mut param_types_and_borrows,
input.span(),
&block_id_type,
&hash_type,
) {
Ok(res) => res,
Err(e) => {
errors.push(e.to_compile_error());
(quote!(_), block_id_type)
(quote!(_), hash_type)
},
};
@@ -345,7 +342,7 @@ impl<'a> Fold for FoldRuntimeApiImpl<'a> {
// Rewrite the input parameters.
input.sig.inputs = parse_quote! {
&self,
#at_param_name: #block_id_type,
#at_param_name: #hash_type,
#( #param_names: #param_types ),*
};
@@ -94,13 +94,13 @@ pub fn replace_wild_card_parameter_names(input: &mut Signature) {
/// Fold the given `Signature` to make it usable on the client side.
pub fn fold_fn_decl_for_client_side(
input: &mut Signature,
block_id: &TokenStream,
block_hash: &TokenStream,
crate_: &TokenStream,
) {
replace_wild_card_parameter_names(input);
// Add `&self, at:& BlockId` as parameters to each function at the beginning.
input.inputs.insert(0, parse_quote!( __runtime_api_at_param__: &#block_id ));
// Add `&self, at:& Block::Hash` as parameters to each function at the beginning.
input.inputs.insert(0, parse_quote!( __runtime_api_at_param__: #block_hash ));
input.inputs.insert(0, parse_quote!(&self));
// Wrap the output in a `Result`