Remove the service, replacing it with a struct of individual chain components (#6352)

* WIP

* Making progress

* Almost ready

* Get service tests compiling

* Fix node screenshot

* Line widths

* Fix node cli tests

* Fix node cli warning

* ChainComponents -> ServiceComponents, fix tests

* make spawn_handle public

* Remove spawnnamed impl for taskmanager

* Move the keep alive stuff to the task manager

* Move the telemetry, base path, rpc keep_alive to the service builder

* Make the task manager keep alive an internal detail

* Rewrite the browser start_client future

* Remove run_node etc

* Revert my personal changes to browser-demo/build.sh

* use |config|

* Add a runtime_version function to SubstrateCli

* Reexport role and runtime version from sc cli

* Update Cargo.lock

* runtime_version -> native_runtime_version

* Pass chain spec to native_runtime_version for polkadot

* Fix line widths

* Traitify ServiceComponents Client
This commit is contained in:
Ashley
2020-06-30 12:00:42 +02:00
committed by GitHub
parent 493d5d8591
commit b832e35c5e
19 changed files with 646 additions and 782 deletions
@@ -40,7 +40,13 @@ use sp_api::{InitializeBlock, StorageTransactionCache, ProofRecorder, OffchainOv
use sp_consensus::{BlockOrigin};
use sc_executor::{NativeExecutor, WasmExecutionMethod, RuntimeVersion, NativeVersion};
use sp_core::{H256, tasks::executor as tasks_executor, NativeOrEncoded};
use sc_client_api::{blockchain::Info, backend::NewBlockState, Backend as ClientBackend, ProofProvider, in_mem::{Backend as InMemBackend, Blockchain as InMemoryBlockchain}, AuxStore, Storage, CallExecutor, cht, ExecutionStrategy, StorageProof, BlockImportOperation, RemoteCallRequest, StorageProvider, ChangesProof, RemoteBodyRequest, RemoteReadRequest, RemoteChangesRequest, FetchChecker, RemoteReadChildRequest, RemoteHeaderRequest};
use sc_client_api::{
blockchain::Info, backend::NewBlockState, Backend as ClientBackend, ProofProvider,
in_mem::{Backend as InMemBackend, Blockchain as InMemoryBlockchain},
AuxStore, Storage, CallExecutor, cht, ExecutionStrategy, StorageProof, BlockImportOperation,
RemoteCallRequest, StorageProvider, ChangesProof, RemoteBodyRequest, RemoteReadRequest,
RemoteChangesRequest, FetchChecker, RemoteReadChildRequest, RemoteHeaderRequest, BlockBackend,
};
use sp_externalities::Extensions;
use sc_block_builder::BlockBuilderProvider;
use sp_blockchain::{
+143 -70
View File
@@ -19,18 +19,18 @@
//! Service integration test utils.
use std::iter;
use std::sync::{Arc, Mutex, MutexGuard};
use std::sync::Arc;
use std::net::Ipv4Addr;
use std::pin::Pin;
use std::time::Duration;
use log::info;
use log::{info, debug};
use futures01::{Future, Stream, Poll};
use futures::{FutureExt as _, TryFutureExt as _};
use tempfile::TempDir;
use tokio::{runtime::Runtime, prelude::FutureExt};
use tokio::timer::Interval;
use sc_service::{
AbstractService,
TaskManager,
GenericChainSpec,
ChainSpecExtension,
Configuration,
@@ -39,12 +39,15 @@ use sc_service::{
Role,
Error,
TaskExecutor,
client::Client,
};
use sp_blockchain::HeaderBackend;
use sc_network::{multiaddr, Multiaddr};
use sc_network::config::{NetworkConfiguration, TransportConfig};
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use sp_transaction_pool::TransactionPool;
use sc_client_api::{Backend, CallExecutor};
use parking_lot::Mutex;
#[cfg(test)]
mod client;
@@ -54,47 +57,100 @@ const MAX_WAIT_TIME: Duration = Duration::from_secs(60 * 3);
struct TestNet<G, E, F, L, U> {
runtime: Runtime,
authority_nodes: Vec<(usize, SyncService<F>, U, Multiaddr)>,
full_nodes: Vec<(usize, SyncService<F>, U, Multiaddr)>,
light_nodes: Vec<(usize, SyncService<L>, Multiaddr)>,
authority_nodes: Vec<(usize, F, U, Multiaddr)>,
full_nodes: Vec<(usize, F, U, Multiaddr)>,
light_nodes: Vec<(usize, L, Multiaddr)>,
chain_spec: GenericChainSpec<G, E>,
base_port: u16,
nodes: usize,
}
/// Wraps around an `Arc<Service>` and implements `Future`.
pub struct SyncService<T>(Arc<Mutex<T>>);
pub trait TestNetNode: Clone + Future<Item = (), Error = sc_service::Error> + Send + 'static {
type Block: BlockT;
type Backend: Backend<Self::Block>;
type Executor: CallExecutor<Self::Block> + Send + Sync;
type RuntimeApi: Send + Sync;
type TransactionPool: TransactionPool<Block = Self::Block>;
impl<T> SyncService<T> {
pub fn get(&self) -> MutexGuard<T> {
self.0.lock().unwrap()
fn client(&self) -> Arc<Client<Self::Backend, Self::Executor, Self::Block, Self::RuntimeApi>>;
fn transaction_pool(&self) -> Arc<Self::TransactionPool>;
fn network(&self) -> Arc<sc_network::NetworkService<Self::Block, <Self::Block as BlockT>::Hash>>;
}
pub struct TestNetComponents<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> {
task_manager: Arc<Mutex<TaskManager>>,
client: Arc<Client<TBackend, TExec, TBl, TRtApi>>,
transaction_pool: Arc<TExPool>,
network: Arc<sc_network::NetworkService<TBl, <TBl as BlockT>::Hash>>,
}
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool>
TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool> {
pub fn new(
task_manager: TaskManager,
client: Arc<Client<TBackend, TExec, TBl, TRtApi>>,
network: Arc<sc_network::NetworkService<TBl, <TBl as BlockT>::Hash>>,
transaction_pool: Arc<TExPool>,
) -> Self {
Self {
client, transaction_pool, network,
task_manager: Arc::new(Mutex::new(task_manager)),
}
}
}
impl<T> Clone for SyncService<T> {
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Clone for
TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool> {
fn clone(&self) -> Self {
Self(self.0.clone())
Self {
task_manager: self.task_manager.clone(),
client: self.client.clone(),
transaction_pool: self.transaction_pool.clone(),
network: self.network.clone(),
}
}
}
impl<T> From<T> for SyncService<T> {
fn from(service: T) -> Self {
SyncService(Arc::new(Mutex::new(service)))
}
}
impl<T: futures::Future<Output=Result<(), sc_service::Error>> + Unpin> Future for SyncService<T> {
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Future for
TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
{
type Item = ();
type Error = sc_service::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let mut f = self.0.lock().unwrap();
futures::compat::Compat::new(&mut *f).poll()
futures::compat::Compat::new(&mut self.task_manager.lock().future()).poll()
}
}
impl<TBl, TBackend, TExec, TRtApi, TExPool> TestNetNode for
TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
where
TBl: BlockT,
TBackend: sc_client_api::Backend<TBl> + Send + Sync + 'static,
TExec: CallExecutor<TBl> + Send + Sync + 'static,
TRtApi: Send + Sync + 'static,
TExPool: TransactionPool<Block = TBl> + Send + Sync + 'static,
{
type Block = TBl;
type Backend = TBackend;
type Executor = TExec;
type RuntimeApi = TRtApi;
type TransactionPool = TExPool;
fn client(&self) -> Arc<Client<Self::Backend, Self::Executor, Self::Block, Self::RuntimeApi>> {
self.client.clone()
}
fn transaction_pool(&self) -> Arc<Self::TransactionPool> {
self.transaction_pool.clone()
}
fn network(&self) -> Arc<sc_network::NetworkService<Self::Block, <Self::Block as BlockT>::Hash>> {
self.network.clone()
}
}
impl<G, E, F, L, U> TestNet<G, E, F, L, U>
where F: Send + 'static, L: Send +'static, U: Clone + Send + 'static
where F: Clone + Send + 'static, L: Clone + Send +'static, U: Clone + Send + 'static
{
pub fn run_until_all_full<FP, LP>(
&mut self,
@@ -102,8 +158,8 @@ where F: Send + 'static, L: Send +'static, U: Clone + Send + 'static
light_predicate: LP,
)
where
FP: Send + Fn(usize, &SyncService<F>) -> bool + 'static,
LP: Send + Fn(usize, &SyncService<L>) -> bool + 'static,
FP: Send + Fn(usize, &F) -> bool + 'static,
LP: Send + Fn(usize, &L) -> bool + 'static,
{
let full_nodes = self.full_nodes.clone();
let light_nodes = self.light_nodes.clone();
@@ -217,8 +273,8 @@ fn node_config<G: RuntimeGenesis + 'static, E: ChainSpecExtension + Clone + 'sta
}
impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
F: AbstractService,
L: AbstractService,
F: TestNetNode,
L: TestNetNode,
E: ChainSpecExtension + Clone + 'static + Send,
G: RuntimeGenesis + 'static,
{
@@ -276,10 +332,9 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
);
let addr = node_config.network.listen_addresses.iter().next().unwrap().clone();
let (service, user_data) = authority(node_config).expect("Error creating test node service");
let service = SyncService::from(service);
executor.spawn(service.clone().map_err(|_| ()));
let addr = addr.with(multiaddr::Protocol::P2p(service.get().network().local_peer_id().clone().into()));
let addr = addr.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
self.authority_nodes.push((self.nodes, service, user_data, addr));
self.nodes += 1;
}
@@ -296,10 +351,9 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
);
let addr = node_config.network.listen_addresses.iter().next().unwrap().clone();
let (service, user_data) = full(node_config).expect("Error creating test node service");
let service = SyncService::from(service);
executor.spawn(service.clone().map_err(|_| ()));
let addr = addr.with(multiaddr::Protocol::P2p(service.get().network().local_peer_id().clone().into()));
let addr = addr.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
self.full_nodes.push((self.nodes, service, user_data, addr));
self.nodes += 1;
}
@@ -315,10 +369,10 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
&temp,
);
let addr = node_config.network.listen_addresses.iter().next().unwrap().clone();
let service = SyncService::from(light(node_config).expect("Error creating test node service"));
let service = light(node_config).expect("Error creating test node service");
executor.spawn(service.clone().map_err(|_| ()));
let addr = addr.with(multiaddr::Protocol::P2p(service.get().network().local_peer_id().clone().into()));
let addr = addr.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
self.light_nodes.push((self.nodes, service, addr));
self.nodes += 1;
}
@@ -337,9 +391,9 @@ pub fn connectivity<G, E, Fb, F, Lb, L>(
E: ChainSpecExtension + Clone + 'static + Send,
G: RuntimeGenesis + 'static,
Fb: Fn(Configuration) -> Result<F, Error>,
F: AbstractService,
F: TestNetNode,
Lb: Fn(Configuration) -> Result<L, Error>,
L: AbstractService,
L: TestNetNode,
{
const NUM_FULL_NODES: usize = 5;
const NUM_LIGHT_NODES: usize = 5;
@@ -363,19 +417,25 @@ pub fn connectivity<G, E, Fb, F, Lb, L>(
info!("Checking star topology");
let first_address = network.full_nodes[0].3.clone();
for (_, service, _, _) in network.full_nodes.iter().skip(1) {
service.get().network().add_reserved_peer(first_address.to_string())
service.network().add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _) in network.light_nodes.iter() {
service.get().network().add_reserved_peer(first_address.to_string())
service.network().add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
network.run_until_all_full(
move |_index, service| service.get().network().num_connected()
== expected_full_connections,
move |_index, service| service.get().network().num_connected()
== expected_light_connections,
move |_index, service| {
let connected = service.network().num_connected();
debug!("Got {}/{} full connections...", connected, expected_full_connections);
connected == expected_full_connections
},
move |_index, service| {
let connected = service.network().num_connected();
debug!("Got {}/{} light connections...", connected, expected_light_connections);
connected == expected_light_connections
},
);
network.runtime
@@ -404,24 +464,30 @@ pub fn connectivity<G, E, Fb, F, Lb, L>(
for i in 0..max_nodes {
if i != 0 {
if let Some((_, service, _, node_id)) = network.full_nodes.get(i) {
service.get().network().add_reserved_peer(address.to_string())
service.network().add_reserved_peer(address.to_string())
.expect("Error adding reserved peer");
address = node_id.clone();
}
}
if let Some((_, service, node_id)) = network.light_nodes.get(i) {
service.get().network().add_reserved_peer(address.to_string())
service.network().add_reserved_peer(address.to_string())
.expect("Error adding reserved peer");
address = node_id.clone();
}
}
network.run_until_all_full(
move |_index, service| service.get().network().num_connected()
== expected_full_connections,
move |_index, service| service.get().network().num_connected()
== expected_light_connections,
move |_index, service| {
let connected = service.network().num_connected();
debug!("Got {}/{} full connections...", connected, expected_full_connections);
connected == expected_full_connections
},
move |_index, service| {
let connected = service.network().num_connected();
debug!("Got {}/{} light connections...", connected, expected_light_connections);
connected == expected_light_connections
},
);
}
temp.close().expect("Error removing temp dir");
@@ -436,9 +502,9 @@ pub fn sync<G, E, Fb, F, Lb, L, B, ExF, U>(
mut extrinsic_factory: ExF
) where
Fb: Fn(Configuration) -> Result<(F, U), Error>,
F: AbstractService,
F: TestNetNode,
Lb: Fn(Configuration) -> Result<L, Error>,
L: AbstractService,
L: TestNetNode,
B: FnMut(&F, &mut U),
ExF: FnMut(&F, &U) -> <F::Block as BlockT>::Extrinsic,
U: Clone + Send + 'static,
@@ -468,39 +534,41 @@ pub fn sync<G, E, Fb, F, Lb, L, B, ExF, U>(
info!("Generating #{}", i + 1);
}
make_block_and_import(&first_service.get(), first_user_data);
make_block_and_import(&first_service, first_user_data);
}
(network.full_nodes[0].1).0.lock().unwrap().network().update_chain();
network.full_nodes[0].1.network().update_chain();
network.full_nodes[0].3.clone()
};
info!("Running sync");
for (_, service, _, _) in network.full_nodes.iter().skip(1) {
service.get().network().add_reserved_peer(first_address.to_string()).expect("Error adding reserved peer");
service.network().add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _) in network.light_nodes.iter() {
service.get().network().add_reserved_peer(first_address.to_string()).expect("Error adding reserved peer");
service.network().add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
network.run_until_all_full(
|_index, service|
service.get().client().info().best_number == (NUM_BLOCKS as u32).into(),
service.client().info().best_number == (NUM_BLOCKS as u32).into(),
|_index, service|
service.get().client().info().best_number == (NUM_BLOCKS as u32).into(),
service.client().info().best_number == (NUM_BLOCKS as u32).into(),
);
info!("Checking extrinsic propagation");
let first_service = network.full_nodes[0].1.clone();
let first_user_data = &network.full_nodes[0].2;
let best_block = BlockId::number(first_service.get().client().info().best_number);
let extrinsic = extrinsic_factory(&first_service.get(), first_user_data);
let best_block = BlockId::number(first_service.client().info().best_number);
let extrinsic = extrinsic_factory(&first_service, first_user_data);
let source = sp_transaction_pool::TransactionSource::External;
futures::executor::block_on(
first_service.get().transaction_pool().submit_one(&best_block, source, extrinsic)
first_service.transaction_pool().submit_one(&best_block, source, extrinsic)
).expect("failed to submit extrinsic");
network.run_until_all_full(
|_index, service| service.get().transaction_pool().ready().count() == 1,
|_index, service| service.transaction_pool().ready().count() == 1,
|_index, _service| true,
);
}
@@ -512,9 +580,9 @@ pub fn consensus<G, E, Fb, F, Lb, L>(
authorities: impl IntoIterator<Item = String>
) where
Fb: Fn(Configuration) -> Result<F, Error>,
F: AbstractService,
F: TestNetNode,
Lb: Fn(Configuration) -> Result<L, Error>,
L: AbstractService,
L: TestNetNode,
E: ChainSpecExtension + Clone + 'static + Send,
G: RuntimeGenesis + 'static,
{
@@ -534,19 +602,22 @@ pub fn consensus<G, E, Fb, F, Lb, L>(
info!("Checking consensus");
let first_address = network.authority_nodes[0].3.clone();
for (_, service, _, _) in network.full_nodes.iter() {
service.get().network().add_reserved_peer(first_address.to_string()).expect("Error adding reserved peer");
service.network().add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _) in network.light_nodes.iter() {
service.get().network().add_reserved_peer(first_address.to_string()).expect("Error adding reserved peer");
service.network().add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _, _) in network.authority_nodes.iter().skip(1) {
service.get().network().add_reserved_peer(first_address.to_string()).expect("Error adding reserved peer");
service.network().add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
network.run_until_all_full(
|_index, service|
service.get().client().info().finalized_number >= (NUM_BLOCKS as u32 / 2).into(),
service.client().info().finalized_number >= (NUM_BLOCKS as u32 / 2).into(),
|_index, service|
service.get().client().info().best_number >= (NUM_BLOCKS as u32 / 2).into(),
service.client().info().best_number >= (NUM_BLOCKS as u32 / 2).into(),
);
info!("Adding more peers");
@@ -559,15 +630,17 @@ pub fn consensus<G, E, Fb, F, Lb, L>(
(0..0).map(|_| (String::new(), { |cfg| full_builder(cfg).map(|s| (s, ())) })),
);
for (_, service, _, _) in network.full_nodes.iter() {
service.get().network().add_reserved_peer(first_address.to_string()).expect("Error adding reserved peer");
service.network().add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _) in network.light_nodes.iter() {
service.get().network().add_reserved_peer(first_address.to_string()).expect("Error adding reserved peer");
service.network().add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
network.run_until_all_full(
|_index, service|
service.get().client().info().finalized_number >= (NUM_BLOCKS as u32).into(),
service.client().info().finalized_number >= (NUM_BLOCKS as u32).into(),
|_index, service|
service.get().client().info().best_number >= (NUM_BLOCKS as u32).into(),
service.client().info().best_number >= (NUM_BLOCKS as u32).into(),
);
}