Allow for customisation of chain selection systems (#2240)

* move SelectChain trait out of client

* Extend SelectChain, move longest chain implementation into it

* Bring SelectChain into service

* implement LongestChain SelectChain

* implement longest chain for node

* update Cargo.lock's

* in between erroring tests

* deprecate ::backend and ::import_lock

* Remove unneded space

Co-Authored-By: gnunicorn <ben.kampmann@googlemail.com>

* Remove unneded space

Co-Authored-By: gnunicorn <ben.kampmann@googlemail.com>

* Fixes test compilation

* remove todo

* re-enable client test

* add doc

* fixing tests

* Clarify SelectChain Interface, intended implementation and usage

* minor components cleanups

* minor cleanups

* Update lock files

* Implement cleaner interface for SelectChain

* addressing comments

* Updating tests

* bump node runtime impl version

* address grumbles
This commit is contained in:
Benjamin Kampmann
2019-05-10 14:08:12 +02:00
committed by Gavin Wood
parent 32fdeed21c
commit 18ca0170c3
22 changed files with 717 additions and 339 deletions
+27 -3
View File
@@ -42,6 +42,7 @@ use primitives::Pair;
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{Header, As};
use substrate_executor::NativeExecutor;
use consensus_common::SelectChain;
use tel::{telemetry, SUBSTRATE_INFO};
pub use self::error::{ErrorKind, Error};
@@ -73,6 +74,7 @@ const DEFAULT_PROTOCOL_ID: &str = "sup";
/// Substrate service.
pub struct Service<Components: components::Components> {
client: Arc<ComponentClient<Components>>,
select_chain: <Components as components::Components>::SelectChain,
network: Option<Arc<components::NetworkService<Components::Factory>>>,
transaction_pool: Arc<TransactionPool<Components::TransactionPoolApi>>,
inherents_pool: Arc<InherentsPool<ComponentExtrinsic<Components>>>,
@@ -150,8 +152,13 @@ impl<Components: components::Components> Service<Components> {
};
let (client, on_demand) = Components::build_client(&config, executor)?;
let import_queue = Box::new(Components::build_import_queue(&mut config, client.clone())?);
let best_header = client.best_block_header()?;
let select_chain = Components::build_select_chain(&mut config, client.clone())?;
let import_queue = Box::new(Components::build_import_queue(
&mut config,
client.clone(),
select_chain.clone()
)?);
let best_header = select_chain.best_chain()?;
let version = config.full_version();
info!("Best block: #{}", best_header.number());
@@ -357,6 +364,7 @@ impl<Components: components::Components> Service<Components> {
Ok(Service {
client,
network: Some(network),
select_chain,
transaction_pool,
inherents_pool,
signal: Some(signal),
@@ -395,6 +403,11 @@ impl<Components> Service<Components> where Components: components::Components {
self.client.clone()
}
/// Get clone of select chain.
pub fn select_chain(&self) -> <Components as components::Components>::SelectChain {
self.select_chain.clone()
}
/// Get shared network instance.
pub fn network(&self) -> Arc<components::NetworkService<Components::Factory>> {
self.network.as_ref().expect("self.network always Some").clone()
@@ -578,6 +591,8 @@ macro_rules! construct_service_factory {
{ $( $full_import_queue_init:tt )* },
LightImportQueue = $light_import_queue:ty
{ $( $light_import_queue_init:tt )* },
SelectChain = $select_chain:ty
{ $( $select_chain_init:tt )* },
}
) => {
$( #[$attr] )*
@@ -597,6 +612,7 @@ macro_rules! construct_service_factory {
type LightService = $light_service;
type FullImportQueue = $full_import_queue;
type LightImportQueue = $light_import_queue;
type SelectChain = $select_chain;
fn build_full_transaction_pool(
config: $crate::TransactionPoolOptions,
@@ -620,11 +636,19 @@ macro_rules! construct_service_factory {
( $( $protocol_init )* ) (config)
}
fn build_select_chain(
config: &mut $crate::FactoryFullConfiguration<Self>,
client: Arc<$crate::FullClient<Self>>
) -> $crate::Result<Self::SelectChain, $crate::Error> {
( $( $select_chain_init )* ) (config, client)
}
fn build_full_import_queue(
config: &mut $crate::FactoryFullConfiguration<Self>,
client: $crate::Arc<$crate::FullClient<Self>>,
select_chain: Self::SelectChain
) -> $crate::Result<Self::FullImportQueue, $crate::Error> {
( $( $full_import_queue_init )* ) (config, client)
( $( $full_import_queue_init )* ) (config, client, select_chain)
}
fn build_light_import_queue(