* Introduce CollatorFn type alias

* Make test-runtime imports consistent with rococo-runtime

* Update node/primitives/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fix warnings

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Sergei Shulepov
2020-11-09 15:21:05 +01:00
committed by GitHub
parent 83661eb8dc
commit 2026228b77
5 changed files with 56 additions and 43 deletions
+13 -6
View File
@@ -269,16 +269,23 @@ pub struct Collation<BlockNumber = polkadot_primitives::v1::BlockNumber> {
pub hrmp_watermark: BlockNumber, pub hrmp_watermark: BlockNumber,
} }
/// Collation function.
///
/// Will be called with the hash of the relay chain block the parachain
/// block should be build on and the [`ValidationData`] that provides
/// information about the state of the parachain on the relay chain.
pub type CollatorFn = Box<
dyn Fn(Hash, &ValidationData) -> Pin<Box<dyn Future<Output = Option<Collation>> + Send>>
+ Send
+ Sync,
>;
/// Configuration for the collation generator /// Configuration for the collation generator
pub struct CollationGenerationConfig { pub struct CollationGenerationConfig {
/// Collator's authentication key, so it can sign things. /// Collator's authentication key, so it can sign things.
pub key: CollatorPair, pub key: CollatorPair,
/// Collation function. /// Collation function. See [`CollatorFn`] for more details.
/// pub collator: CollatorFn,
/// Will be called with the hash of the relay chain block the parachain
/// block should be build on and the [`ValidationData`] that provides
/// information about the state of the parachain on the relay chain.
pub collator: Box<dyn Fn(Hash, &ValidationData) -> Pin<Box<dyn Future<Output = Option<Collation>> + Send>> + Send + Sync>,
/// The parachain that this collator collates for /// The parachain that this collator collates for
pub para_id: ParaId, pub para_id: ParaId,
} }
+9 -8
View File
@@ -24,7 +24,7 @@ pub use chain_spec::*;
use futures::future::Future; use futures::future::Future;
use polkadot_overseer::OverseerHandler; use polkadot_overseer::OverseerHandler;
use polkadot_primitives::v1::{ use polkadot_primitives::v1::{
Id as ParaId, HeadData, ValidationCode, Balance, CollatorPair, CollatorId, ValidationData, Hash, Id as ParaId, HeadData, ValidationCode, Balance, CollatorPair, CollatorId,
}; };
use polkadot_runtime_common::BlockHashCount; use polkadot_runtime_common::BlockHashCount;
use polkadot_service::{ use polkadot_service::{
@@ -34,7 +34,7 @@ use polkadot_node_subsystem::messages::{CollatorProtocolMessage, CollationGenera
use polkadot_test_runtime::{ use polkadot_test_runtime::{
Runtime, SignedExtra, SignedPayload, VERSION, ParasSudoWrapperCall, SudoCall, UncheckedExtrinsic, Runtime, SignedExtra, SignedPayload, VERSION, ParasSudoWrapperCall, SudoCall, UncheckedExtrinsic,
}; };
use polkadot_node_primitives::{Collation, CollationGenerationConfig}; use polkadot_node_primitives::{CollatorFn, CollationGenerationConfig};
use polkadot_runtime_parachains::paras::ParaGenesisArgs; use polkadot_runtime_parachains::paras::ParaGenesisArgs;
use sc_chain_spec::ChainSpec; use sc_chain_spec::ChainSpec;
use sc_client_api::execution_extensions::ExecutionStrategies; use sc_client_api::execution_extensions::ExecutionStrategies;
@@ -54,7 +54,7 @@ use sp_blockchain::HeaderBackend;
use sp_keyring::Sr25519Keyring; use sp_keyring::Sr25519Keyring;
use sp_runtime::{codec::Encode, generic, traits::IdentifyAccount, MultiSigner}; use sp_runtime::{codec::Encode, generic, traits::IdentifyAccount, MultiSigner};
use sp_state_machine::BasicExternalities; use sp_state_machine::BasicExternalities;
use std::{sync::Arc, time::Duration, pin::Pin}; use std::{sync::Arc, time::Duration};
use substrate_test_client::{BlockchainEventsExt, RpcHandlersExt, RpcTransactionOutput, RpcTransactionError}; use substrate_test_client::{BlockchainEventsExt, RpcHandlersExt, RpcTransactionOutput, RpcTransactionError};
native_executor_instance!( native_executor_instance!(
@@ -326,17 +326,18 @@ impl PolkadotTestNode {
&mut self, &mut self,
collator_key: CollatorPair, collator_key: CollatorPair,
para_id: ParaId, para_id: ParaId,
collator: Box<dyn Fn(Hash, &ValidationData) -> Pin<Box<dyn Future<Output = Option<Collation>> + Send>> + Send + Sync>, collator: CollatorFn,
) { ) {
let config = CollationGenerationConfig { let config = CollationGenerationConfig {
key: collator_key, key: collator_key,
collator, collator,
para_id para_id,
}; };
self.overseer_handler.send_msg( self.overseer_handler
CollationGenerationMessage::Initialize(config), .send_msg(CollationGenerationMessage::Initialize(config))
).await.expect("Registers the collator"); .await
.expect("Registers the collator");
self.overseer_handler self.overseer_handler
.send_msg(CollatorProtocolMessage::CollateOn(para_id)) .send_msg(CollatorProtocolMessage::CollateOn(para_id))
@@ -16,12 +16,11 @@
//! Collator for the adder test parachain. //! Collator for the adder test parachain.
use std::{pin::Pin, sync::{Arc, Mutex}, collections::HashMap, time::Duration}; use std::{sync::{Arc, Mutex}, collections::HashMap, time::Duration};
use test_parachain_adder::{hash_state, BlockData, HeadData, execute}; use test_parachain_adder::{hash_state, BlockData, HeadData, execute};
use futures::{Future, FutureExt};
use futures_timer::Delay; use futures_timer::Delay;
use polkadot_primitives::v1::{ValidationData, PoV, Hash, CollatorId, CollatorPair}; use polkadot_primitives::v1::{PoV, CollatorId, CollatorPair};
use polkadot_node_primitives::Collation; use polkadot_node_primitives::{Collation, CollatorFn};
use codec::{Encode, Decode}; use codec::{Encode, Decode};
use sp_core::Pair; use sp_core::Pair;
@@ -116,7 +115,9 @@ impl Collator {
/// This collation function can be plugged into the overseer to generate collations for the adder parachain. /// This collation function can be plugged into the overseer to generate collations for the adder parachain.
pub fn create_collation_function( pub fn create_collation_function(
&self, &self,
) -> Box<dyn Fn(Hash, &ValidationData) -> Pin<Box<dyn Future<Output = Option<Collation>> + Send>> + Send + Sync> { ) -> CollatorFn {
use futures::FutureExt as _;
let state = self.state.clone(); let state = self.state.clone();
Box::new(move |relay_parent, validation_data| { Box::new(move |relay_parent, validation_data| {
@@ -166,7 +167,7 @@ mod tests {
use futures::executor::block_on; use futures::executor::block_on;
use polkadot_parachain::{primitives::ValidationParams, wasm_executor::ExecutionMode}; use polkadot_parachain::{primitives::ValidationParams, wasm_executor::ExecutionMode};
use polkadot_primitives::v1::PersistedValidationData; use polkadot_primitives::v1::{ValidationData, PersistedValidationData};
use codec::Decode; use codec::Decode;
#[test] #[test]
@@ -32,12 +32,16 @@ pub struct Collation {
pub proof_of_validity: PoV, pub proof_of_validity: PoV,
} }
type CollatorFn = Box<
dyn Fn(Hash, &ValidationData) -> Pin<Box<dyn Future<Output = Option<Collation>>>>
>;
struct CollationGenerationConfig { struct CollationGenerationConfig {
key: CollatorPair, key: CollatorPair,
/// Collate will be called with the relay chain hash the parachain should build /// Collate will be called with the relay chain hash the parachain should build
/// a block on and the `ValidationData` that provides information about the state /// a block on and the `ValidationData` that provides information about the state
/// of the parachain on the relay chain. /// of the parachain on the relay chain.
collator: Box<dyn Fn(Hash, &ValidationData) -> Pin<Box<dyn Future<Output = Option<Collation>>>>> collator: CollatorFn,
para_id: ParaId, para_id: ParaId,
} }
``` ```
+21 -21
View File
@@ -24,16 +24,16 @@ use pallet_transaction_payment::CurrencyAdapter;
use sp_std::prelude::*; use sp_std::prelude::*;
use sp_std::collections::btree_map::BTreeMap; use sp_std::collections::btree_map::BTreeMap;
use codec::Encode; use codec::Encode;
use polkadot_runtime_parachains::{
configuration as parachains_configuration, use polkadot_runtime_parachains::configuration as parachains_configuration;
inclusion, use polkadot_runtime_parachains::inclusion as parachains_inclusion;
inclusion_inherent, use polkadot_runtime_parachains::inclusion_inherent as parachains_inclusion_inherent;
initializer, use polkadot_runtime_parachains::initializer as parachains_initializer;
paras, use polkadot_runtime_parachains::paras as parachains_paras;
router, use polkadot_runtime_parachains::router as parachains_router;
runtime_api_impl::v1 as runtime_impl, use polkadot_runtime_parachains::scheduler as parachains_scheduler;
scheduler, use polkadot_runtime_parachains::runtime_api_impl::v1 as runtime_impl;
};
use primitives::v1::{ use primitives::v1::{
AccountId, AccountIndex, Balance, BlockNumber, CandidateEvent, CommittedCandidateReceipt, AccountId, AccountIndex, Balance, BlockNumber, CandidateEvent, CommittedCandidateReceipt,
CoreState, GroupRotationInfo, Hash as HashT, Id as ParaId, Moment, Nonce, OccupiedCoreAssumption, CoreState, GroupRotationInfo, Hash as HashT, Id as ParaId, Moment, Nonce, OccupiedCoreAssumption,
@@ -445,26 +445,26 @@ impl pallet_sudo::Trait for Runtime {
impl parachains_configuration::Trait for Runtime {} impl parachains_configuration::Trait for Runtime {}
impl inclusion::Trait for Runtime { impl parachains_inclusion::Trait for Runtime {
type Event = Event; type Event = Event;
} }
impl inclusion_inherent::Trait for Runtime {} impl parachains_inclusion_inherent::Trait for Runtime {}
impl initializer::Trait for Runtime { impl parachains_initializer::Trait for Runtime {
type Randomness = RandomnessCollectiveFlip; type Randomness = RandomnessCollectiveFlip;
} }
impl paras::Trait for Runtime { impl parachains_paras::Trait for Runtime {
type Origin = Origin; type Origin = Origin;
} }
impl router::Trait for Runtime { impl parachains_router::Trait for Runtime {
type Origin = Origin; type Origin = Origin;
type UmpSink = (); type UmpSink = ();
} }
impl scheduler::Trait for Runtime {} impl parachains_scheduler::Trait for Runtime {}
impl paras_sudo_wrapper::Trait for Runtime {} impl paras_sudo_wrapper::Trait for Runtime {}
@@ -503,11 +503,11 @@ construct_runtime! {
// Parachains runtime modules // Parachains runtime modules
ParachainsConfiguration: parachains_configuration::{Module, Call, Storage, Config<T>}, ParachainsConfiguration: parachains_configuration::{Module, Call, Storage, Config<T>},
Inclusion: inclusion::{Module, Call, Storage, Event<T>}, Inclusion: parachains_inclusion::{Module, Call, Storage, Event<T>},
InclusionInherent: inclusion_inherent::{Module, Call, Storage, Inherent}, InclusionInherent: parachains_inclusion_inherent::{Module, Call, Storage, Inherent},
Initializer: initializer::{Module, Call, Storage}, Initializer: parachains_initializer::{Module, Call, Storage},
Paras: paras::{Module, Call, Storage, Origin}, Paras: parachains_paras::{Module, Call, Storage, Origin},
Scheduler: scheduler::{Module, Call, Storage}, Scheduler: parachains_scheduler::{Module, Call, Storage},
ParasSudoWrapper: paras_sudo_wrapper::{Module, Call}, ParasSudoWrapper: paras_sudo_wrapper::{Module, Call},
Sudo: pallet_sudo::{Module, Call, Storage, Config<T>, Event<T>}, Sudo: pallet_sudo::{Module, Call, Storage, Config<T>, Event<T>},