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:
Robert Habermeier
2018-12-03 11:49:30 +01:00
committed by GitHub
parent 69a288e586
commit 63980e3770
17 changed files with 96 additions and 109 deletions
@@ -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) => {