mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-29 10:17:57 +00:00
Rewrite impl_runtime_apis! and decl_runtime_apis! as proc-macro (#1174)
* Rewrites `impl_runtime_apis!` macro as `proc-macro` * Adds some documentation * Require the `impl_runtime_apis` to use a path for accessing the trait * Make the runtime implement `GetNodeBlockType` * Moves first chunk of runtime api code into the `impl_runtime_apis` macro This also renames `ClientWithApi` into `RuntimeApi`. * Make `impl_runtime_apis` use `runtime` api version automatically * `decl_runtime_apis` automatically adds `Block: BlockT` as generic parameter * Remove function generic arguments in block builder api * Remove some unnused stuff from the `decl_runtime_apis` macro * Make `InherentData` working again * Make `impl_runtime_apis!` implement the `RuntimeApi` side as well * Make it compile again after rebasing with master * Split `sr-api-macros` into multiple files * Reimplement `decl_runtime_apis!` as proc_macro * Use `decl_runtime_apis!` for `Core` as well and improve error reporting * Adds documentation for `decl_runtime_apis!` and `impl_runtime_apis!` * Move some code * Adds compile fail tests * Adds a test and fixes some bugs * Make `impl_runtime_apis!` support `_` as parameter name * Fixes build errors with wasm * Wasm rebuild after master rebase * Apply suggestions from code review Co-Authored-By: bkchr <bkchr@users.noreply.github.com> * Addresses some grumbles * Adds test to ensure that method signatures need to match * New wasm files
This commit is contained in:
@@ -54,27 +54,21 @@ extern crate srml_upgrade_key as upgrade_key;
|
||||
extern crate sr_version as version;
|
||||
extern crate node_primitives;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use codec::{Encode, Decode};
|
||||
use rstd::prelude::*;
|
||||
use substrate_primitives::u32_trait::{_2, _4};
|
||||
use node_primitives::{
|
||||
AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature
|
||||
};
|
||||
use grandpa::fg_primitives::{runtime::GrandpaApi, ScheduledChange, id::*};
|
||||
#[cfg(feature = "std")]
|
||||
use node_primitives::Block as GBlock;
|
||||
use client::{block_builder::api::runtime::*, runtime_api::{runtime::*, id::*}};
|
||||
#[cfg(feature = "std")]
|
||||
use client::runtime_api::ApiExt;
|
||||
use runtime_primitives::ApplyResult;
|
||||
use grandpa::fg_primitives::{self, ScheduledChange, id::*};
|
||||
use client::{
|
||||
block_builder::api as block_builder_api, runtime_api::{self as client_api, id::*}
|
||||
};
|
||||
use runtime_primitives::{ApplyResult, CheckInherentError};
|
||||
use runtime_primitives::transaction_validity::TransactionValidity;
|
||||
use runtime_primitives::generic;
|
||||
use runtime_primitives::traits::{Convert, BlakeTwo256, Block as BlockT, DigestFor, NumberFor};
|
||||
#[cfg(feature = "std")]
|
||||
use runtime_primitives::traits::ApiRef;
|
||||
#[cfg(feature = "std")]
|
||||
use substrate_primitives::AuthorityId;
|
||||
use runtime_primitives::traits::{
|
||||
Convert, BlakeTwo256, Block as BlockT, DigestFor, NumberFor, ProvideInherent
|
||||
};
|
||||
use version::RuntimeVersion;
|
||||
use council::{motions as council_motions, voting as council_voting};
|
||||
#[cfg(feature = "std")]
|
||||
@@ -215,7 +209,7 @@ impl grandpa::Trait for Runtime {
|
||||
construct_runtime!(
|
||||
pub enum Runtime with Log(InternalLog: DigestItem<Hash, SessionKey>) where
|
||||
Block = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
NodeBlock = node_primitives::Block
|
||||
{
|
||||
System: system::{default, Log(ChangesTrieRoot)},
|
||||
Timestamp: timestamp::{Module, Call, Storage, Config<T>, Inherent},
|
||||
@@ -237,6 +231,7 @@ construct_runtime!(
|
||||
|
||||
/// The address format for describing accounts.
|
||||
pub use balances::address::Address as RawAddress;
|
||||
|
||||
/// The address format for describing accounts.
|
||||
pub type Address = balances::Address<Runtime>;
|
||||
/// Block header type as expected by this runtime.
|
||||
@@ -254,168 +249,8 @@ pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Index, Call>;
|
||||
/// Executive: handles dispatch to the various modules.
|
||||
pub type Executive = executive::Executive<Runtime, Block, balances::ChainContext<Runtime>, Balances, AllModules>;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub struct ClientWithApi {
|
||||
call: ::std::ptr::NonNull<client::runtime_api::CallApiAt<GBlock>>,
|
||||
commit_on_success: ::std::cell::RefCell<bool>,
|
||||
initialised_block: ::std::cell::RefCell<Option<GBlockId>>,
|
||||
changes: ::std::cell::RefCell<client::runtime_api::OverlayedChanges>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
unsafe impl Send for ClientWithApi {}
|
||||
#[cfg(feature = "std")]
|
||||
unsafe impl Sync for ClientWithApi {}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl ApiExt for ClientWithApi {
|
||||
fn map_api_result<F: FnOnce(&Self) -> Result<R, E>, R, E>(&self, map_call: F) -> Result<R, E> {
|
||||
*self.commit_on_success.borrow_mut() = false;
|
||||
let res = map_call(self);
|
||||
*self.commit_on_success.borrow_mut() = true;
|
||||
|
||||
self.commit_on_ok(&res);
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::runtime_api::ConstructRuntimeApi<GBlock> for ClientWithApi {
|
||||
fn construct_runtime_api<'a, T: client::runtime_api::CallApiAt<GBlock>>(call: &'a T) -> ApiRef<'a, Self> {
|
||||
ClientWithApi {
|
||||
call: unsafe {
|
||||
::std::ptr::NonNull::new_unchecked(
|
||||
::std::mem::transmute(
|
||||
call as &client::runtime_api::CallApiAt<GBlock>
|
||||
)
|
||||
)
|
||||
},
|
||||
commit_on_success: true.into(),
|
||||
initialised_block: None.into(),
|
||||
changes: Default::default(),
|
||||
}.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl ClientWithApi {
|
||||
fn call_api_at<A: Encode, R: Decode>(
|
||||
&self,
|
||||
at: &GBlockId,
|
||||
function: &'static str,
|
||||
args: &A
|
||||
) -> client::error::Result<R> {
|
||||
let res = unsafe {
|
||||
self.call.as_ref().call_api_at(
|
||||
at,
|
||||
function,
|
||||
args.encode(),
|
||||
&mut *self.changes.borrow_mut(),
|
||||
&mut *self.initialised_block.borrow_mut()
|
||||
).and_then(|r|
|
||||
R::decode(&mut &r[..])
|
||||
.ok_or_else(||
|
||||
client::error::ErrorKind::CallResultDecode(function).into()
|
||||
)
|
||||
)
|
||||
};
|
||||
|
||||
self.commit_on_ok(&res);
|
||||
res
|
||||
}
|
||||
|
||||
fn commit_on_ok<R, E>(&self, res: &Result<R, E>) {
|
||||
if *self.commit_on_success.borrow() {
|
||||
if res.is_err() {
|
||||
self.changes.borrow_mut().discard_prospective();
|
||||
} else {
|
||||
self.changes.borrow_mut().commit_prospective();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
type GBlockId = generic::BlockId<GBlock>;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::runtime_api::Core<GBlock> for ClientWithApi {
|
||||
fn version(&self, at: &GBlockId) -> Result<RuntimeVersion, client::error::Error> {
|
||||
self.call_api_at(at, "version", &())
|
||||
}
|
||||
|
||||
fn authorities(&self, at: &GBlockId) -> Result<Vec<AuthorityId>, client::error::Error> {
|
||||
self.call_api_at(at, "authorities", &())
|
||||
}
|
||||
|
||||
fn execute_block(&self, at: &GBlockId, block: &GBlock) -> Result<(), client::error::Error> {
|
||||
self.call_api_at(at, "execute_block", block)
|
||||
}
|
||||
|
||||
fn initialise_block(&self, at: &GBlockId, header: &<GBlock as BlockT>::Header) -> Result<(), client::error::Error> {
|
||||
self.call_api_at(at, "initialise_block", header)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::block_builder::api::BlockBuilder<GBlock> for ClientWithApi {
|
||||
fn apply_extrinsic(&self, at: &GBlockId, extrinsic: &<GBlock as BlockT>::Extrinsic) -> Result<ApplyResult, client::error::Error> {
|
||||
self.call_api_at(at, "apply_extrinsic", extrinsic)
|
||||
}
|
||||
|
||||
fn finalise_block(&self, at: &GBlockId) -> Result<<GBlock as BlockT>::Header, client::error::Error> {
|
||||
self.call_api_at(at, "finalise_block", &())
|
||||
}
|
||||
|
||||
fn inherent_extrinsics<Inherent: Decode + Encode, Unchecked: Decode + Encode>(
|
||||
&self, at: &GBlockId, inherent: &Inherent
|
||||
) -> Result<Vec<Unchecked>, client::error::Error> {
|
||||
self.call_api_at(at, "inherent_extrinsics", inherent)
|
||||
}
|
||||
|
||||
fn check_inherents<Inherent: Decode + Encode, Error: Decode + Encode>(&self, at: &GBlockId, block: &GBlock, inherent: &Inherent) -> Result<Result<(), Error>, client::error::Error> {
|
||||
self.call_api_at(at, "check_inherents", &(block, inherent))
|
||||
}
|
||||
|
||||
fn random_seed(&self, at: &GBlockId) -> Result<<GBlock as BlockT>::Hash, client::error::Error> {
|
||||
self.call_api_at(at, "random_seed", &())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::runtime_api::TaggedTransactionQueue<GBlock> for ClientWithApi {
|
||||
fn validate_transaction(
|
||||
&self,
|
||||
at: &GBlockId,
|
||||
utx: &<GBlock as BlockT>::Extrinsic
|
||||
) -> Result<TransactionValidity, client::error::Error> {
|
||||
self.call_api_at(at, "validate_transaction", utx)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl client::runtime_api::Metadata<GBlock> for ClientWithApi {
|
||||
fn metadata(&self, at: &GBlockId) -> Result<OpaqueMetadata, client::error::Error> {
|
||||
self.call_api_at(at, "metadata", &())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl substrate_finality_grandpa_primitives::GrandpaApi<GBlock> for ClientWithApi {
|
||||
fn grandpa_pending_change(&self, at: &GBlockId, digest: &DigestFor<GBlock>)
|
||||
-> Result<Option<ScheduledChange<NumberFor<GBlock>>>, client::error::Error> {
|
||||
self.call_api_at(at, "grandpa_pending_change", digest)
|
||||
}
|
||||
|
||||
fn grandpa_authorities(&self, at: &GBlockId)
|
||||
-> Result<Vec<(AuthorityId, u64)>, client::error::Error> {
|
||||
self.call_api_at(at, "grandpa_authorities", &())
|
||||
}
|
||||
}
|
||||
|
||||
impl_runtime_apis! {
|
||||
impl Core<Block> for Runtime {
|
||||
impl client_api::Core<Block> for Runtime {
|
||||
fn version() -> RuntimeVersion {
|
||||
VERSION
|
||||
}
|
||||
@@ -433,13 +268,13 @@ impl_runtime_apis! {
|
||||
}
|
||||
}
|
||||
|
||||
impl Metadata for Runtime {
|
||||
impl client_api::Metadata<Block> for Runtime {
|
||||
fn metadata() -> OpaqueMetadata {
|
||||
Runtime::metadata().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockBuilder<Block, InherentData, UncheckedExtrinsic, InherentData, InherentError> for Runtime {
|
||||
impl block_builder_api::BlockBuilder<Block> for Runtime {
|
||||
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult {
|
||||
Executive::apply_extrinsic(extrinsic)
|
||||
}
|
||||
@@ -448,12 +283,27 @@ impl_runtime_apis! {
|
||||
Executive::finalise_block()
|
||||
}
|
||||
|
||||
fn inherent_extrinsics(data: InherentData) -> Vec<UncheckedExtrinsic> {
|
||||
data.create_inherent_extrinsics()
|
||||
fn inherent_extrinsics(data: runtime_primitives::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
|
||||
let mut inherent = Vec::new();
|
||||
|
||||
inherent.extend(
|
||||
Timestamp::create_inherent_extrinsics(data.timestamp)
|
||||
.into_iter()
|
||||
.map(|v| (v.0, UncheckedExtrinsic::new_unsigned(Call::Timestamp(v.1))))
|
||||
);
|
||||
|
||||
inherent.extend(
|
||||
Consensus::create_inherent_extrinsics(data.consensus)
|
||||
.into_iter()
|
||||
.map(|v| (v.0, UncheckedExtrinsic::new_unsigned(Call::Consensus(v.1))))
|
||||
);
|
||||
|
||||
inherent.as_mut_slice().sort_unstable_by_key(|v| v.0);
|
||||
inherent.into_iter().map(|v| v.1).collect()
|
||||
}
|
||||
|
||||
fn check_inherents(block: Block, data: InherentData) -> Result<(), InherentError> {
|
||||
data.check_inherents(block)
|
||||
fn check_inherents(block: Block, data: runtime_primitives::InherentData) -> Result<(), CheckInherentError> {
|
||||
InherentData::check_inherents(data, block)
|
||||
}
|
||||
|
||||
fn random_seed() -> <Block as BlockT>::Hash {
|
||||
@@ -461,13 +311,13 @@ impl_runtime_apis! {
|
||||
}
|
||||
}
|
||||
|
||||
impl TaggedTransactionQueue<Block> for Runtime {
|
||||
impl client_api::TaggedTransactionQueue<Block> for Runtime {
|
||||
fn validate_transaction(tx: <Block as BlockT>::Extrinsic) -> TransactionValidity {
|
||||
Executive::validate_transaction(tx)
|
||||
}
|
||||
}
|
||||
|
||||
impl GrandpaApi<Block> for Runtime {
|
||||
impl fg_primitives::GrandpaApi<Block> for Runtime {
|
||||
fn grandpa_pending_change(digest: DigestFor<Block>)
|
||||
-> Option<ScheduledChange<NumberFor<Block>>>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user