mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 15:11:03 +00:00
make block builder and construct_runtime! generic over inherent-data (#1191)
* make block builder generic over inherent-data * construct_runtime has you specify inherent data type * get all tests to compile
This commit is contained in:
committed by
GitHub
parent
69a288e586
commit
63980e3770
@@ -16,19 +16,19 @@
|
||||
|
||||
//! The runtime api for building blocks.
|
||||
|
||||
use runtime_primitives::{traits::Block as BlockT, ApplyResult, InherentData, CheckInherentError};
|
||||
use runtime_primitives::{traits::Block as BlockT, ApplyResult, CheckInherentError};
|
||||
use rstd::vec::Vec;
|
||||
|
||||
decl_runtime_apis! {
|
||||
/// The `BlockBuilder` api trait that provides required functions for building a block for a runtime.
|
||||
pub trait BlockBuilder {
|
||||
pub trait BlockBuilder<InherentData> {
|
||||
/// Apply the given extrinsics.
|
||||
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult;
|
||||
/// Finish the current block.
|
||||
fn finalise_block() -> <Block as BlockT>::Header;
|
||||
/// Generate inherent extrinsics.
|
||||
/// Generate inherent extrinsics. The inherent data will vary from chain to chain.
|
||||
fn inherent_extrinsics(inherent: InherentData) -> Vec<<Block as BlockT>::Extrinsic>;
|
||||
/// Check that the inherents are valid.
|
||||
/// Check that the inherents are valid. The inherent data will vary from chain to chain.
|
||||
fn check_inherents(block: Block, data: InherentData) -> Result<(), CheckInherentError>;
|
||||
/// Generate a random seed.
|
||||
fn random_seed() -> <Block as BlockT>::Hash;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
use super::api::BlockBuilder as BlockBuilderApi;
|
||||
use std::vec::Vec;
|
||||
use std::marker::PhantomData;
|
||||
use codec::Encode;
|
||||
use blockchain::HeaderBackend;
|
||||
use runtime_primitives::traits::{
|
||||
@@ -28,18 +29,19 @@ use error;
|
||||
use runtime_primitives::ApplyOutcome;
|
||||
|
||||
/// Utility for building new (valid) blocks from a stream of extrinsics.
|
||||
pub struct BlockBuilder<'a, Block, A: ProvideRuntimeApi> where Block: BlockT {
|
||||
pub struct BlockBuilder<'a, Block, InherentData, A: ProvideRuntimeApi> where Block: BlockT {
|
||||
header: <Block as BlockT>::Header,
|
||||
extrinsics: Vec<<Block as BlockT>::Extrinsic>,
|
||||
api: ApiRef<'a, A::Api>,
|
||||
block_id: BlockId<Block>,
|
||||
_marker: PhantomData<InherentData>,
|
||||
}
|
||||
|
||||
impl<'a, Block, A> BlockBuilder<'a, Block, A>
|
||||
impl<'a, Block, A, InherentData> BlockBuilder<'a, Block, InherentData, A>
|
||||
where
|
||||
Block: BlockT<Hash=H256>,
|
||||
A: ProvideRuntimeApi + HeaderBackend<Block> + 'a,
|
||||
A::Api: BlockBuilderApi<Block>,
|
||||
A::Api: BlockBuilderApi<Block, InherentData>,
|
||||
{
|
||||
/// Create a new instance of builder from the given client, building on the latest block.
|
||||
pub fn new(api: &'a A) -> error::Result<Self> {
|
||||
@@ -72,6 +74,7 @@ where
|
||||
extrinsics: Vec::new(),
|
||||
api,
|
||||
block_id: *block_id,
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -79,12 +82,12 @@ where
|
||||
/// can be validly executed (by executing it); if it is invalid, it'll be returned along with
|
||||
/// the error. Otherwise, it will return a mutable reference to self (in order to chain).
|
||||
pub fn push(&mut self, xt: <Block as BlockT>::Extrinsic) -> error::Result<()> {
|
||||
fn impl_push<'a, T, Block: BlockT>(
|
||||
fn impl_push<'a, T, Block: BlockT, InherentData>(
|
||||
api: &mut ApiRef<'a, T>,
|
||||
block_id: &BlockId<Block>,
|
||||
xt: Block::Extrinsic,
|
||||
extrinsics: &mut Vec<Block::Extrinsic>
|
||||
) -> error::Result<()> where T: BlockBuilderApi<Block> {
|
||||
) -> error::Result<()> where T: BlockBuilderApi<Block, InherentData> {
|
||||
api.map_api_result(|api| {
|
||||
match api.apply_extrinsic(block_id, &xt)? {
|
||||
Ok(ApplyOutcome::Success) | Ok(ApplyOutcome::Fail) => {
|
||||
|
||||
@@ -538,21 +538,21 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
}
|
||||
|
||||
/// Create a new block, built on the head of the chain.
|
||||
pub fn new_block(
|
||||
pub fn new_block<InherentData>(
|
||||
&self
|
||||
) -> error::Result<block_builder::BlockBuilder<Block, Self>> where
|
||||
) -> error::Result<block_builder::BlockBuilder<Block, InherentData, Self>> where
|
||||
E: Clone + Send + Sync,
|
||||
RA: BlockBuilderAPI<Block>
|
||||
RA: BlockBuilderAPI<Block, InherentData>
|
||||
{
|
||||
block_builder::BlockBuilder::new(self)
|
||||
}
|
||||
|
||||
/// Create a new block, built on top of `parent`.
|
||||
pub fn new_block_at(
|
||||
pub fn new_block_at<InherentData>(
|
||||
&self, parent: &BlockId<Block>
|
||||
) -> error::Result<block_builder::BlockBuilder<Block, Self>> where
|
||||
) -> error::Result<block_builder::BlockBuilder<Block, InherentData, Self>> where
|
||||
E: Clone + Send + Sync,
|
||||
RA: BlockBuilderAPI<Block>
|
||||
RA: BlockBuilderAPI<Block, InherentData>
|
||||
{
|
||||
block_builder::BlockBuilder::at_block(parent, &self)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user