mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-22 11:25:42 +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)
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ pub trait BlockBuilder<Block: BlockT> {
|
||||
pub trait AuthoringApi:
|
||||
Send
|
||||
+ Sync
|
||||
+ BlockBuilderAPI<<Self as AuthoringApi>::Block, Error=<Self as AuthoringApi>::Error>
|
||||
+ BlockBuilderAPI<<Self as AuthoringApi>::Block, InherentData, Error=<Self as AuthoringApi>::Error>
|
||||
+ Core<<Self as AuthoringApi>::Block, AuthorityId, Error=<Self as AuthoringApi>::Error>
|
||||
+ OldTxQueue<<Self as AuthoringApi>::Block, Error=<Self as AuthoringApi>::Error>
|
||||
{
|
||||
@@ -1174,7 +1174,7 @@ impl<C, A> BaseProposer<<C as AuthoringApi>::Block> for Proposer<C, A> where
|
||||
let proposed_timestamp = match self.client.check_inherents(
|
||||
&self.parent_id,
|
||||
&unchecked_proposal,
|
||||
&inherent
|
||||
&inherent,
|
||||
) {
|
||||
Ok(Ok(())) => None,
|
||||
Ok(Err(BlockBuilderError::TimestampInFuture(timestamp))) => Some(timestamp),
|
||||
|
||||
@@ -234,7 +234,7 @@ impl<V: 'static + Verifier<Block>, D> Peer<V, D> {
|
||||
|
||||
/// Add blocks to the peer -- edit the block before adding
|
||||
pub fn generate_blocks<F>(&self, count: usize, origin: BlockOrigin, mut edit_block: F)
|
||||
where F: FnMut(BlockBuilder<Block, PeersClient>) -> Block
|
||||
where F: FnMut(BlockBuilder<Block, (), PeersClient>) -> Block
|
||||
{
|
||||
use blocks::BlockData;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! provide consensus service to substrate.
|
||||
//! A consensus proposer for "basic" chains which use the primitive inherent-data.
|
||||
|
||||
// FIXME: move this into substrate-consensus-common - https://github.com/paritytech/substrate/issues/1021
|
||||
|
||||
@@ -29,7 +29,7 @@ use consensus_common::{self, evaluation, offline_tracker::OfflineTracker};
|
||||
use primitives::{H256, AuthorityId, ed25519, Blake2Hasher};
|
||||
use runtime_primitives::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, ProvideRuntimeApi};
|
||||
use runtime_primitives::generic::BlockId;
|
||||
use runtime_primitives::InherentData;
|
||||
use runtime_primitives::BasicInherentData;
|
||||
use transaction_pool::txpool::{self, Pool as TransactionPool};
|
||||
|
||||
use parking_lot::RwLock;
|
||||
@@ -60,16 +60,18 @@ pub trait AuthoringApi: Send + Sync + ProvideRuntimeApi where
|
||||
fn build_block<F: FnMut(&mut BlockBuilder<Self::Block>) -> ()>(
|
||||
&self,
|
||||
at: &BlockId<Self::Block>,
|
||||
inherent_data: InherentData,
|
||||
inherent_data: BasicInherentData,
|
||||
build_ctx: F,
|
||||
) -> Result<Self::Block, error::Error>;
|
||||
}
|
||||
|
||||
impl<'a, B, E, Block, RA> BlockBuilder<Block> for client::block_builder::BlockBuilder<'a, Block, SubstrateClient<B, E, Block, RA>> where
|
||||
impl<'a, B, E, Block, RA> BlockBuilder<Block>
|
||||
for client::block_builder::BlockBuilder<'a, Block, BasicInherentData, SubstrateClient<B, E, Block, RA>>
|
||||
where
|
||||
B: client::backend::Backend<Block, Blake2Hasher> + 'static,
|
||||
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + Clone + 'static,
|
||||
Block: BlockT<Hash=H256>,
|
||||
RA: BlockBuilderApi<Block>,
|
||||
RA: BlockBuilderApi<Block, BasicInherentData>,
|
||||
{
|
||||
fn push_extrinsic(&mut self, extrinsic: <Block as BlockT>::Extrinsic) -> Result<(), error::Error> {
|
||||
client::block_builder::BlockBuilder::push(self, extrinsic).map_err(Into::into)
|
||||
@@ -80,7 +82,7 @@ impl<B, E, Block, RA> AuthoringApi for SubstrateClient<B, E, Block, RA> where
|
||||
B: client::backend::Backend<Block, Blake2Hasher> + Send + Sync + 'static,
|
||||
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + Clone + 'static,
|
||||
Block: BlockT<Hash=H256>,
|
||||
RA: BlockBuilderApi<Block>,
|
||||
RA: BlockBuilderApi<Block, BasicInherentData>,
|
||||
{
|
||||
type Block = Block;
|
||||
type Error = client::error::Error;
|
||||
@@ -88,7 +90,7 @@ impl<B, E, Block, RA> AuthoringApi for SubstrateClient<B, E, Block, RA> where
|
||||
fn build_block<F: FnMut(&mut BlockBuilder<Self::Block>) -> ()>(
|
||||
&self,
|
||||
at: &BlockId<Self::Block>,
|
||||
inherent_data: InherentData,
|
||||
inherent_data: BasicInherentData,
|
||||
mut build_ctx: F,
|
||||
) -> Result<Self::Block, error::Error> {
|
||||
let runtime_version = self.runtime_version_at(at)?;
|
||||
@@ -119,7 +121,7 @@ pub struct ProposerFactory<C, A> where A: txpool::ChainApi {
|
||||
|
||||
impl<C, A> consensus_common::Environment<<C as AuthoringApi>::Block> for ProposerFactory<C, A> where
|
||||
C: AuthoringApi,
|
||||
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<<C as AuthoringApi>::Block>,
|
||||
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<<C as AuthoringApi>::Block, BasicInherentData>,
|
||||
A: txpool::ChainApi<Block=<C as AuthoringApi>::Block>,
|
||||
client::error::Error: From<<C as AuthoringApi>::Error>
|
||||
{
|
||||
@@ -174,7 +176,7 @@ pub struct Proposer<Block: BlockT, C, A: txpool::ChainApi> {
|
||||
impl<Block, C, A> consensus_common::Proposer<<C as AuthoringApi>::Block> for Proposer<Block, C, A> where
|
||||
Block: BlockT,
|
||||
C: AuthoringApi<Block=Block>,
|
||||
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<Block>,
|
||||
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<Block, BasicInherentData>,
|
||||
A: txpool::ChainApi<Block=Block>,
|
||||
client::error::Error: From<<C as AuthoringApi>::Error>
|
||||
{
|
||||
@@ -201,7 +203,7 @@ impl<Block, C, A> consensus_common::Proposer<<C as AuthoringApi>::Block> for Pro
|
||||
)
|
||||
}
|
||||
|
||||
let inherent_data = InherentData::new(timestamp, offline_indices);
|
||||
let inherent_data = BasicInherentData::new(timestamp, offline_indices);
|
||||
|
||||
let block = self.client.build_block(
|
||||
&self.parent_id,
|
||||
|
||||
@@ -180,9 +180,9 @@ impl<F: ServiceFactory> TestNet<F> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn connectivity<F: ServiceFactory>(spec: FactoryChainSpec<F>) where
|
||||
pub fn connectivity<F: ServiceFactory, Inherent>(spec: FactoryChainSpec<F>) where
|
||||
<F as ServiceFactory>::RuntimeApi:
|
||||
client::block_builder::api::BlockBuilder<<F as service::ServiceFactory>::Block>
|
||||
client::block_builder::api::BlockBuilder<<F as service::ServiceFactory>::Block, Inherent>
|
||||
{
|
||||
const NUM_NODES: u32 = 10;
|
||||
{
|
||||
@@ -224,7 +224,7 @@ where
|
||||
B: Fn(&F::FullService) -> ImportBlock<F::Block>,
|
||||
E: Fn(&F::FullService) -> FactoryExtrinsic<F>,
|
||||
<F as ServiceFactory>::RuntimeApi:
|
||||
client::block_builder::api::BlockBuilder<<F as service::ServiceFactory>::Block> +
|
||||
client::block_builder::api::BlockBuilder<<F as service::ServiceFactory>::Block, ()> +
|
||||
client::runtime_api::TaggedTransactionQueue<<F as service::ServiceFactory>::Block>
|
||||
{
|
||||
const NUM_NODES: u32 = 10;
|
||||
@@ -263,7 +263,7 @@ pub fn consensus<F>(spec: FactoryChainSpec<F>, authorities: Vec<String>)
|
||||
where
|
||||
F: ServiceFactory,
|
||||
<F as ServiceFactory>::RuntimeApi:
|
||||
client::block_builder::api::BlockBuilder<<F as service::ServiceFactory>::Block>
|
||||
client::block_builder::api::BlockBuilder<<F as service::ServiceFactory>::Block, ()>
|
||||
{
|
||||
const NUM_NODES: u32 = 20;
|
||||
const NUM_BLOCKS: u64 = 200;
|
||||
|
||||
@@ -463,17 +463,17 @@ macro_rules! impl_outer_log {
|
||||
}
|
||||
|
||||
//TODO: https://github.com/paritytech/substrate/issues/1022
|
||||
/// Inherent data to include in a block.
|
||||
/// Basic Inherent data to include in a block; used by simple runtimes.
|
||||
#[derive(Encode, Decode)]
|
||||
pub struct InherentData {
|
||||
pub struct BasicInherentData {
|
||||
/// Current timestamp.
|
||||
pub timestamp: u64,
|
||||
/// Indices of offline validators.
|
||||
pub consensus: Vec<u32>,
|
||||
}
|
||||
|
||||
impl InherentData {
|
||||
/// Create a new `InherentData` instance.
|
||||
impl BasicInherentData {
|
||||
/// Create a new `BasicInherentData` instance.
|
||||
pub fn new(timestamp: u64, consensus: Vec<u32>) -> Self {
|
||||
Self {
|
||||
timestamp,
|
||||
|
||||
@@ -29,9 +29,9 @@ pub trait BlockBuilderExt {
|
||||
fn push_transfer(&mut self, transfer: runtime::Transfer) -> Result<(), client::error::Error>;
|
||||
}
|
||||
|
||||
impl<'a, A> BlockBuilderExt for client::block_builder::BlockBuilder<'a, runtime::Block, A> where
|
||||
impl<'a, A> BlockBuilderExt for client::block_builder::BlockBuilder<'a, runtime::Block, (), A> where
|
||||
A: ProvideRuntimeApi + client::blockchain::HeaderBackend<runtime::Block> + 'a,
|
||||
A::Api: BlockBuilder<runtime::Block>
|
||||
A::Api: BlockBuilder<runtime::Block, ()>
|
||||
{
|
||||
fn push_transfer(&mut self, transfer: runtime::Transfer) -> Result<(), client::error::Error> {
|
||||
self.push(sign_tx(transfer))
|
||||
|
||||
@@ -56,7 +56,7 @@ use runtime_primitives::{
|
||||
traits::{
|
||||
BlindCheckable, BlakeTwo256, Block as BlockT, Extrinsic as ExtrinsicT,
|
||||
GetNodeBlockType, GetRuntimeBlockType
|
||||
}, InherentData, CheckInherentError
|
||||
}, CheckInherentError
|
||||
};
|
||||
use runtime_version::RuntimeVersion;
|
||||
pub use primitives::hash::H256;
|
||||
@@ -220,7 +220,7 @@ impl_runtime_apis! {
|
||||
}
|
||||
}
|
||||
|
||||
impl block_builder_api::BlockBuilder<Block> for Runtime {
|
||||
impl block_builder_api::BlockBuilder<Block, ()> for Runtime {
|
||||
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult {
|
||||
system::execute_transaction(extrinsic)
|
||||
}
|
||||
@@ -229,11 +229,11 @@ impl_runtime_apis! {
|
||||
system::finalise_block()
|
||||
}
|
||||
|
||||
fn inherent_extrinsics(_data: InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
|
||||
fn inherent_extrinsics(_data: ()) -> Vec<<Block as BlockT>::Extrinsic> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn check_inherents(_block: Block, _data: InherentData) -> Result<(), CheckInherentError> {
|
||||
fn check_inherents(_block: Block, _data: ()) -> Result<(), CheckInherentError> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user