Integration tests (#805)

* Started substrate tests

* Sync test

* Test updates

* Improved tests

* Use on-chain block delay

* Parallel test execution

* Otimized tests

* Logging

* Fixed racing test

* Fixed compilation

* Fixed timestamp test

* Removed rlp dependency

* Minor fixes

* Fixed tests

* Removed best_block_id and resolved fdlimit issue

* Whitespace

* Use keyring

* Style

* Added API execution setting

* Removed stale import
This commit is contained in:
Arkadiy Paronyan
2018-09-28 11:37:55 +02:00
committed by Gav Wood
parent 955a5393d8
commit 9a660f82ed
30 changed files with 590 additions and 140 deletions
+20 -1
View File
@@ -30,6 +30,16 @@ enum GenesisSource<G> {
Factory(fn() -> G),
}
impl<G: RuntimeGenesis> Clone for GenesisSource<G> {
fn clone(&self) -> Self {
match *self {
GenesisSource::File(ref path) => GenesisSource::File(path.clone()),
GenesisSource::Embedded(d) => GenesisSource::Embedded(d),
GenesisSource::Factory(f) => GenesisSource::Factory(f),
}
}
}
impl<G: RuntimeGenesis> GenesisSource<G> {
fn resolve(&self) -> Result<Genesis<G>, String> {
#[derive(Serialize, Deserialize)]
@@ -69,7 +79,7 @@ enum Genesis<G> {
Raw(HashMap<StorageKey, StorageData>),
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
struct ChainSpecFile {
pub name: String,
@@ -85,6 +95,15 @@ pub struct ChainSpec<G: RuntimeGenesis> {
genesis: GenesisSource<G>,
}
impl<G: RuntimeGenesis> Clone for ChainSpec<G> {
fn clone(&self) -> Self {
ChainSpec {
spec: self.spec.clone(),
genesis: self.genesis.clone(),
}
}
}
impl<G: RuntimeGenesis> ChainSpec<G> {
pub fn boot_nodes(&self) -> &[String] {
&self.spec.boot_nodes
+31 -6
View File
@@ -19,11 +19,13 @@
use std::fmt;
use std::sync::Arc;
use std::marker::PhantomData;
use std::ops::Deref;
use serde::{Serialize, de::DeserializeOwned};
use tokio::runtime::TaskExecutor;
use chain_spec::ChainSpec;
use client_db;
use client::{self, Client};
use error;
use {error, Service};
use network::{self, OnDemand};
use substrate_executor::{NativeExecutor, NativeExecutionDispatch};
use transaction_pool::{self, Options as TransactionPoolOptions, Pool as TransactionPool};
@@ -83,6 +85,9 @@ pub type FactoryGenesis<F> = <F as ServiceFactory>::Genesis;
/// `Block` type for a factory.
pub type FactoryBlock<F> = <F as ServiceFactory>::Block;
/// `Extrinsic` type for a factory.
pub type FactoryExtrinsic<F> = <<F as ServiceFactory>::Block as BlockT>::Extrinsic;
/// `Number` type for a factory.
pub type FactoryBlockNumber<F> = <<FactoryBlock<F> as BlockT>::Header as HeaderT>::Number;
@@ -113,7 +118,7 @@ pub trait RuntimeGenesis: Serialize + DeserializeOwned + BuildStorage {}
impl<T: Serialize + DeserializeOwned + BuildStorage> RuntimeGenesis for T {}
/// A collection of types and methods to build a service on top of the substrate service.
pub trait ServiceFactory: 'static {
pub trait ServiceFactory: 'static + Sized {
/// Block type.
type Block: BlockT;
/// Extrinsic hash type.
@@ -123,13 +128,17 @@ pub trait ServiceFactory: 'static {
/// Chain runtime.
type RuntimeDispatch: NativeExecutionDispatch + Send + Sync + 'static;
/// Extrinsic pool backend type for the full client.
type FullTransactionPoolApi: transaction_pool::ChainApi<Hash=Self::ExtrinsicHash, Block=Self::Block> + Send + 'static;
type FullTransactionPoolApi: transaction_pool::ChainApi<Hash = Self::ExtrinsicHash, Block = Self::Block> + Send + 'static;
/// Extrinsic pool backend type for the light client.
type LightTransactionPoolApi: transaction_pool::ChainApi<Hash=Self::ExtrinsicHash, Block=Self::Block> + 'static;
type LightTransactionPoolApi: transaction_pool::ChainApi<Hash = Self::ExtrinsicHash, Block = Self::Block> + 'static;
/// Genesis configuration for the runtime.
type Genesis: RuntimeGenesis;
/// Other configuration for service members.
type Configuration: Default;
/// Extended full service type.
type FullService: Deref<Target = Service<FullComponents<Self>>> + Send + Sync + 'static;
/// Extended light service type.
type LightService: Deref<Target = Service<LightComponents<Self>>> + Send + Sync + 'static;
//TODO: replace these with a constructor trait. that TransactionPool implements.
/// Extrinsic pool constructor for the full client.
@@ -142,6 +151,13 @@ pub trait ServiceFactory: 'static {
/// Build network protocol.
fn build_network_protocol(config: &FactoryFullConfiguration<Self>)
-> Result<Self::NetworkProtocol, error::Error>;
/// Build full service.
fn new_full(config: FactoryFullConfiguration<Self>, executor: TaskExecutor)
-> Result<Self::FullService, error::Error>;
/// Build light service.
fn new_light(config: FactoryFullConfiguration<Self>, executor: TaskExecutor)
-> Result<Self::LightService, error::Error>;
}
/// A collection of types and function to generalise over full / light client type.
@@ -153,7 +169,10 @@ pub trait Components: 'static {
/// Client executor.
type Executor: 'static + client::CallExecutor<FactoryBlock<Self::Factory>, Blake2Hasher> + Send + Sync;
/// Extrinsic pool type.
type TransactionPoolApi: 'static + transaction_pool::ChainApi<Hash=<Self::Factory as ServiceFactory>::ExtrinsicHash, Block=FactoryBlock<Self::Factory>>;
type TransactionPoolApi: 'static + transaction_pool::ChainApi<
Hash = <Self::Factory as ServiceFactory>::ExtrinsicHash,
Block = FactoryBlock<Self::Factory>
>;
/// Create client.
fn build_client(
@@ -195,7 +214,13 @@ impl<Factory: ServiceFactory> Components for FullComponents<Factory> {
path: config.database_path.as_str().into(),
pruning: config.pruning.clone(),
};
Ok((Arc::new(client_db::new_client(db_settings, executor, &config.chain_spec, config.execution_strategy)?), None))
Ok((Arc::new(client_db::new_client(
db_settings,
executor,
&config.chain_spec,
config.block_execution_strategy,
config.api_execution_strategy,
)?), None))
}
fn build_transaction_pool(config: TransactionPoolOptions, client: Arc<ComponentClient<Self>>)
+7 -6
View File
@@ -28,6 +28,7 @@ use serde::{Serialize, de::DeserializeOwned};
use target_info::Target;
/// Service configuration.
#[derive(Clone)]
pub struct Configuration<C, G: Serialize + DeserializeOwned + BuildStorage> {
/// Implementation name
pub impl_name: &'static str,
@@ -53,12 +54,12 @@ pub struct Configuration<C, G: Serialize + DeserializeOwned + BuildStorage> {
pub chain_spec: ChainSpec<G>,
/// Custom configuration.
pub custom: C,
/// Telemetry server URL, optional - only `Some` if telemetry reporting is enabled
pub telemetry: Option<String>,
/// Node name.
pub name: String,
/// Execution strategy.
pub execution_strategy: ExecutionStrategy,
/// Block execution strategy.
pub block_execution_strategy: ExecutionStrategy,
/// Runtime API execution strategy.
pub api_execution_strategy: ExecutionStrategy,
/// RPC over HTTP binding address. `None` if disabled.
pub rpc_http: Option<SocketAddr>,
/// RPC over Websockets binding address. `None` if disabled.
@@ -83,9 +84,9 @@ impl<C: Default, G: Serialize + DeserializeOwned + BuildStorage> Configuration<C
database_path: Default::default(),
keys: Default::default(),
custom: Default::default(),
telemetry: Default::default(),
pruning: PruningMode::default(),
execution_strategy: ExecutionStrategy::Both,
block_execution_strategy: ExecutionStrategy::Both,
api_execution_strategy: ExecutionStrategy::Both,
rpc_http: None,
rpc_ws: None,
telemetry_url: None,
+13 -7
View File
@@ -25,6 +25,7 @@ extern crate futures;
extern crate exit_future;
extern crate serde;
extern crate serde_json;
extern crate parking_lot;
extern crate substrate_keystore as keystore;
extern crate substrate_primitives as primitives;
extern crate sr_primitives as runtime_primitives;
@@ -61,6 +62,7 @@ use std::net::SocketAddr;
use std::sync::Arc;
use std::collections::HashMap;
use futures::prelude::*;
use parking_lot::Mutex;
use keystore::Store as Keystore;
use client::BlockchainEvents;
use runtime_primitives::traits::{Header, As};
@@ -81,7 +83,7 @@ pub use components::{ServiceFactory, FullBackend, FullExecutor, LightBackend,
ComponentBlock, FullClient, LightClient, FullComponents, LightComponents,
CodeExecutor, NetworkService, FactoryChainSpec, FactoryBlock,
FactoryFullConfiguration, RuntimeGenesis, FactoryGenesis,
ComponentExHash, ComponentExtrinsic,
ComponentExHash, ComponentExtrinsic, FactoryExtrinsic,
};
const DEFAULT_PROTOCOL_ID: &'static str = "sup";
@@ -95,7 +97,7 @@ pub struct Service<Components: components::Components> {
exit: ::exit_future::Exit,
signal: Option<Signal>,
_rpc_http: Option<rpc::HttpServer>,
_rpc_ws: Option<rpc::WsServer>,
_rpc_ws: Option<Mutex<rpc::WsServer>>, // WsServer is not `Sync`, but the service needs to be.
_telemetry: Option<tel::Telemetry>,
}
@@ -186,12 +188,14 @@ impl<Components> Service<Components>
{
// block notifications
let network = network.clone();
let network = Arc::downgrade(&network);
let txpool = transaction_pool.clone();
let events = client.import_notification_stream()
.for_each(move |notification| {
network.on_block_imported(notification.hash, &notification.header);
if let Some(network) = network.upgrade() {
network.on_block_imported(notification.hash, &notification.header);
}
txpool.cull(&BlockId::hash(notification.hash))
.map_err(|e| warn!("Error removing extrinsics: {:?}", e))?;
Ok(())
@@ -203,11 +207,13 @@ impl<Components> Service<Components>
{
// extrinsic notifications
let network = network.clone();
let network = Arc::downgrade(&network);
let events = transaction_pool.import_notification_stream()
// TODO [ToDr] Consider throttling?
.for_each(move |_| {
network.trigger_repropagate();
if let Some(network) = network.upgrade() {
network.trigger_repropagate();
}
Ok(())
})
.select(exit.clone())
@@ -277,7 +283,7 @@ impl<Components> Service<Components>
keystore: keystore,
exit,
_rpc_http: rpc_http,
_rpc_ws: rpc_ws,
_rpc_ws: rpc_ws.map(Mutex::new),
_telemetry: telemetry,
})
}
+17
View File
@@ -0,0 +1,17 @@
[package]
name = "substrate-service-test"
version = "0.3.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
tempdir = "0.3"
tokio = "0.1.7"
futures = "0.1"
log = "0.3"
env_logger = "0.4"
fdlimit = "0.1"
substrate-service = { path = "../../../core/service" }
substrate-network = { path = "../../../core/network" }
substrate-primitives = { path = "../../../core/primitives" }
substrate-client = { path = "../../../core/client" }
sr-primitives = { path = "../../../core/sr-primitives" }
+247
View File
@@ -0,0 +1,247 @@
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Service integration test utils.
#[macro_use]
extern crate log;
extern crate tempdir;
extern crate tokio;
extern crate futures;
extern crate env_logger;
extern crate fdlimit;
extern crate substrate_service as service;
extern crate substrate_network as network;
extern crate substrate_primitives as primitives;
extern crate substrate_client as client;
extern crate sr_primitives;
use std::iter;
use std::sync::Arc;
use std::net::Ipv4Addr;
use std::time::Duration;
use futures::Stream;
use tempdir::TempDir;
use tokio::runtime::Runtime;
use tokio::timer::Interval;
use primitives::blake2_256;
use service::{
ServiceFactory,
ExecutionStrategy,
Configuration,
FactoryFullConfiguration,
FactoryChainSpec,
Roles,
FactoryExtrinsic,
};
use network::{NetworkConfiguration, NonReservedPeerMode, Protocol, SyncProvider, ManageNetwork};
use client::{BlockOrigin, JustifiedHeader};
use sr_primitives::traits::As;
struct TestNet<F: ServiceFactory> {
runtime: Runtime,
authority_nodes: Arc<Vec<(u32, F::FullService)>>,
full_nodes: Arc<Vec<(u32, F::FullService)>>,
_light_nodes: Arc<Vec<(u32, F::LightService)>>,
}
impl<F: ServiceFactory> TestNet<F> {
pub fn run_until_all_full<P: Send + Sync + Fn(u32, &F::FullService) -> bool + 'static>(&mut self, predicate: P) {
let full_nodes = self.full_nodes.clone();
let interval = Interval::new_interval(Duration::from_millis(100)).map_err(|_| ()).for_each(move |_| {
if full_nodes.iter().all(|&(ref id, ref service)| predicate(*id, service)) {
Err(())
} else {
Ok(())
}
});
self.runtime.block_on(interval).ok();
}
}
fn node_private_key_string(index: u32) -> String {
format!("N{}", index)
}
fn node_config<F: ServiceFactory> (
index: u32,
spec: &FactoryChainSpec<F>,
role: Roles,
key_seed: Option<String>,
base_port: u16,
root: &TempDir,
) -> FactoryFullConfiguration<F>
{
let root = root.path().join(format!("node-{}", index));
let mut keys = Vec::new();
if let Some(seed) = key_seed {
keys.push(seed);
}
let network_config = NetworkConfiguration {
config_path: Some(root.join("network").to_str().unwrap().into()),
net_config_path: Some(root.join("network").to_str().unwrap().into()),
listen_addresses: vec! [
iter::once(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1)))
.chain(iter::once(Protocol::Tcp(base_port + index as u16)))
.collect()
],
public_addresses: vec![],
boot_nodes: vec![],
use_secret: Some(blake2_256(node_private_key_string(index).as_bytes())),
min_peers: 25,
max_peers: 500,
reserved_nodes: vec![],
non_reserved_mode: NonReservedPeerMode::Accept,
client_version: "network/test/0.1".to_owned(),
};
Configuration {
impl_name: "network-test-impl",
impl_version: "0.1",
impl_commit: "",
roles: role,
transaction_pool: Default::default(),
network: network_config,
keystore_path: root.join("key").to_str().unwrap().into(),
database_path: root.join("db").to_str().unwrap().into(),
pruning: Default::default(),
keys: keys,
chain_spec: (*spec).clone(),
custom: Default::default(),
name: format!("Node {}", index),
block_execution_strategy: ExecutionStrategy::NativeWhenPossible,
api_execution_strategy: ExecutionStrategy::NativeWhenPossible,
rpc_http: None,
rpc_ws: None,
telemetry_url: None,
}
}
impl<F: ServiceFactory> TestNet<F> {
fn new(temp: &TempDir, spec: FactoryChainSpec<F>, full: u32, light: u32, authorities: Vec<String>, base_port: u16) -> TestNet<F> {
::env_logger::init().ok();
::fdlimit::raise_fd_limit();
let runtime = Runtime::new().expect("Error creating tokio runtime");
let authority_nodes = authorities.iter().enumerate().map(|(index, key)| (index as u32,
F::new_full(node_config::<F>(index as u32, &spec, Roles::AUTHORITY, Some(key.clone()), base_port, &temp), runtime.executor())
.expect("Error creating test node service"))
).collect();
let authorities = authorities.len() as u32;
let full_nodes = (authorities..full + authorities).map(|index| (index,
F::new_full(node_config::<F>(index, &spec, Roles::FULL, None, base_port, &temp), runtime.executor())
.expect("Error creating test node service"))
).collect();
let light_nodes = (full + authorities..full + authorities + light).map(|index| (index,
F::new_light(node_config::<F>(index, &spec, Roles::LIGHT, None, base_port, &temp), runtime.executor())
.expect("Error creating test node service"))
).collect();
TestNet {
runtime,
authority_nodes: Arc::new(authority_nodes),
full_nodes: Arc::new(full_nodes),
_light_nodes: Arc::new(light_nodes),
}
}
}
pub fn connectivity<F: ServiceFactory>(spec: FactoryChainSpec<F>) {
const NUM_NODES: u32 = 10;
{
let temp = TempDir::new("substrate-connectivity-test").expect("Error creating test dir");
{
let mut network = TestNet::<F>::new(&temp, spec.clone(), NUM_NODES, 0, vec![], 30400);
info!("Checking star topology");
let first_address = network.full_nodes[0].1.network().node_id().unwrap();
for (_, service) in network.full_nodes.iter().skip(1) {
service.network().add_reserved_peer(first_address.clone()).expect("Error adding reserved peer");
}
network.run_until_all_full(|_index, service|
service.network().status().num_peers == NUM_NODES as usize - 1
);
}
temp.close().expect("Error removing temp dir");
}
{
let temp = TempDir::new("substrate-connectivity-test").expect("Error creating test dir");
{
let mut network = TestNet::<F>::new(&temp, spec, NUM_NODES as u32, 0, vec![], 30400);
info!("Checking linked topology");
let mut address = network.full_nodes[0].1.network().node_id().unwrap();
for (_, service) in network.full_nodes.iter().skip(1) {
service.network().add_reserved_peer(address.clone()).expect("Error adding reserved peer");
address = service.network().node_id().unwrap();
}
network.run_until_all_full(|_index, service| {
service.network().status().num_peers == NUM_NODES as usize - 1
});
}
temp.close().expect("Error removing temp dir");
}
}
pub fn sync<F, B>(spec: FactoryChainSpec<F>, block_factory: B)
where
F: ServiceFactory,
B: Fn(&F::FullService) -> (JustifiedHeader<F::Block>, Option<Vec<FactoryExtrinsic<F>>>),
{
const NUM_NODES: u32 = 10;
const NUM_BLOCKS: usize = 512;
let temp = TempDir::new("substrate-sync-test").expect("Error creating test dir");
let mut network = TestNet::<F>::new(&temp, spec.clone(), NUM_NODES, 0, vec![], 30500);
info!("Checking block sync");
let first_address = {
let first_service = &network.full_nodes[0].1;
for i in 0 .. NUM_BLOCKS {
if i % 128 == 0 {
info!("Generating #{}", i);
}
let (header, body) = block_factory(&first_service);
first_service.client().import_block(BlockOrigin::File, header, body, true).expect("Error importing test block");
}
first_service.network().node_id().unwrap()
};
info!("Running sync");
for (_, service) in network.full_nodes.iter().skip(1) {
service.network().add_reserved_peer(first_address.clone()).expect("Error adding reserved peer");
}
network.run_until_all_full(|_index, service| {
service.client().info().unwrap().chain.best_number == As::sa(NUM_BLOCKS as u64)
});
}
pub fn consensus<F>(spec: FactoryChainSpec<F>, authorities: Vec<String>)
where
F: ServiceFactory,
{
const NUM_NODES: u32 = 10;
const NUM_BLOCKS: u64 = 200;
info!("Checking consensus");
let temp = TempDir::new("substrate-conensus-test").expect("Error creating test dir");
let mut network = TestNet::<F>::new(&temp, spec.clone(), NUM_NODES, 0, authorities, 30600);
let first_address = network.authority_nodes[0].1.network().node_id().unwrap();
for (_, service) in network.full_nodes.iter() {
service.network().add_reserved_peer(first_address.clone()).expect("Error adding reserved peer");
}
for (_, service) in network.authority_nodes.iter().skip(1) {
service.network().add_reserved_peer(first_address.clone()).expect("Error adding reserved peer");
}
network.run_until_all_full(|_index, service| {
service.client().info().unwrap().chain.finalized_number >= As::sa(NUM_BLOCKS)
});
}