Adds integration test based on adder collator (#1928)

* Adds integration test based on adder collator

This adds an integration test for parachains that uses the adder
collator. The test will start two relay chain nodes and one collator and
waits until 4 blocks are build and enacted by the parachain.

* Make sure the integration test is run in CI

* Fix wasm compilation

* Update parachain/test-parachains/adder/collator/src/lib.rs

Co-authored-by: Sergei Shulepov <sergei@parity.io>

* Update cli/src/command.rs

Co-authored-by: Sergei Shulepov <sergei@parity.io>
This commit is contained in:
Bastian Köcher
2020-11-09 11:26:29 +01:00
committed by GitHub
parent 80a2be8c4b
commit 83661eb8dc
24 changed files with 546 additions and 333 deletions
+16 -30
View File
@@ -37,38 +37,18 @@ const MAX_RUNTIME_MEM: usize = 1024 * 1024 * 1024; // 1 GiB
const MAX_CODE_MEM: usize = 16 * 1024 * 1024; // 16 MiB
const MAX_VALIDATION_RESULT_HEADER_MEM: usize = MAX_CODE_MEM + 1024; // 16.001 MiB
/// A stub validation-pool defined when compiling for Android or WASM.
#[cfg(any(target_os = "android", target_os = "unknown"))]
#[derive(Clone)]
pub struct ValidationPool {
_inner: (), // private field means not publicly-instantiable
}
#[cfg(any(target_os = "android", target_os = "unknown"))]
impl ValidationPool {
/// Create a new `ValidationPool`.
pub fn new() -> Self {
ValidationPool { _inner: () }
}
}
/// A stub function defined when compiling for Android or WASM.
#[cfg(any(target_os = "android", target_os = "unknown"))]
pub fn run_worker(_: &str) -> Result<(), String> {
Err("Cannot run validation worker on this platform".to_string())
}
/// The execution mode for the `ValidationPool`.
#[derive(Clone)]
#[cfg_attr(not(any(target_os = "android", target_os = "unknown")), derive(Debug))]
#[derive(Clone, Debug)]
pub enum ExecutionMode {
/// The validation worker is ran in a thread inside the same process.
InProcess,
/// The validation worker is ran using the process' executable and the subcommand `validation-worker` is passed
/// following by the address of the shared memory.
#[cfg(not(any(target_os = "android", target_os = "unknown")))]
ExternalProcessSelfHost(ValidationPool),
/// The validation worker is ran using the command provided and the argument provided. The address of the shared
/// memory is added at the end of the arguments.
#[cfg(not(any(target_os = "android", target_os = "unknown")))]
ExternalProcessCustomHost {
/// Validation pool.
pool: ValidationPool,
@@ -80,6 +60,19 @@ pub enum ExecutionMode {
},
}
impl Default for ExecutionMode {
fn default() -> Self {
#[cfg(not(any(target_os = "android", target_os = "unknown")))]
{
Self::ExternalProcessSelfHost(ValidationPool::new())
}
#[cfg(any(target_os = "android", target_os = "unknown"))]
{
Self::InProcess
}
}
}
#[derive(Debug, thiserror::Error)]
/// Candidate validation error.
@@ -159,13 +152,6 @@ pub fn validate_candidate(
let args: Vec<&str> = args.iter().map(|x| x.as_str()).collect();
pool.validate_candidate_custom(validation_code, params, binary, &args)
},
#[cfg(any(target_os = "android", target_os = "unknown"))]
ExecutionMode::ExternalProcessSelfHost(_) | ExecutionMode::ExternalProcessCustomHost { .. } =>
Err(ValidationError::Internal(InternalError::System(
Box::<dyn std::error::Error + Send + Sync>::from(
"Remote validator not available".to_string()
) as Box<_>
))),
}
}
@@ -12,6 +12,7 @@ path = "src/main.rs"
[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] }
futures = "0.3.4"
futures-timer = "3.0.2"
log = "0.4.8"
structopt = "0.3.8"
@@ -28,6 +29,13 @@ sc-authority-discovery = { git = "https://github.com/paritytech/substrate", bran
[dev-dependencies]
polkadot-parachain = { path = "../../.." }
polkadot-test-service = { path = "../../../../node/test/service" }
substrate-test-utils = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" }
tokio = { version = "0.2", features = ["macros"] }
[features]
real-overseer = [ "polkadot-service/real-overseer" ]
@@ -16,12 +16,14 @@
//! Collator for the adder test parachain.
use std::{pin::Pin, sync::{Arc, Mutex}, collections::HashMap};
use std::{pin::Pin, sync::{Arc, Mutex}, collections::HashMap, time::Duration};
use test_parachain_adder::{hash_state, BlockData, HeadData, execute};
use futures::{Future, FutureExt};
use polkadot_primitives::v1::{ValidationData, PoV, Hash};
use futures_timer::Delay;
use polkadot_primitives::v1::{ValidationData, PoV, Hash, CollatorId, CollatorPair};
use polkadot_node_primitives::Collation;
use codec::{Encode, Decode};
use sp_core::Pair;
/// The amount we add when producing a new block.
///
@@ -32,6 +34,8 @@ const ADD: u64 = 2;
struct State {
head_to_state: HashMap<Arc<HeadData>, u64>,
number_to_head: HashMap<u64, Arc<HeadData>>,
/// Block number of the best block.
best_block: u64,
}
impl State {
@@ -46,6 +50,7 @@ impl State {
Self {
head_to_state: vec![(genesis_state.clone(), 0)].into_iter().collect(),
number_to_head: vec![(0, genesis_state)].into_iter().collect(),
best_block: 0,
}
}
@@ -53,6 +58,8 @@ impl State {
///
/// Returns the new [`BlockData`] and the new [`HeadData`].
fn advance(&mut self, parent_head: HeadData) -> (BlockData, HeadData) {
self.best_block = parent_head.number;
let block = BlockData {
state: *self.head_to_state.get(&parent_head).expect("Getting state using parent head"),
add: ADD,
@@ -72,6 +79,7 @@ impl State {
/// The collator of the adder parachain.
pub struct Collator {
state: Arc<Mutex<State>>,
key: CollatorPair,
}
impl Collator {
@@ -79,6 +87,7 @@ impl Collator {
pub fn new() -> Self {
Self {
state: Arc::new(Mutex::new(State::genesis())),
key: CollatorPair::generate().0,
}
}
@@ -92,6 +101,16 @@ impl Collator {
test_parachain_adder::wasm_binary_unwrap()
}
/// Get the collator key.
pub fn collator_key(&self) -> CollatorPair {
self.key.clone()
}
/// Get the collator id.
pub fn collator_id(&self) -> CollatorId {
self.key.public()
}
/// Create the collation function.
///
/// This collation function can be plugged into the overseer to generate collations for the adder parachain.
@@ -125,6 +144,20 @@ impl Collator {
async move { Some(collation) }.boxed()
})
}
/// Wait until `blocks` are built and enacted.
pub async fn wait_for_blocks(&self, blocks: u64) {
let start_block = self.state.lock().unwrap().best_block;
loop {
Delay::new(Duration::from_secs(1)).await;
let current_block = self.state.lock().unwrap().best_block;
if start_block + blocks <= current_block {
return
}
}
}
}
#[cfg(test)]
@@ -20,9 +20,9 @@ use sc_cli::{Result, Role, SubstrateCli};
use polkadot_cli::Cli;
use polkadot_node_subsystem::messages::{CollatorProtocolMessage, CollationGenerationMessage};
use polkadot_node_primitives::CollationGenerationConfig;
use polkadot_primitives::v1::{CollatorPair, Id as ParaId};
use polkadot_primitives::v1::Id as ParaId;
use test_parachain_adder_collator::Collator;
use sp_core::{Pair, hexdisplay::HexDisplay};
use sp_core::hexdisplay::HexDisplay;
use std::time::Duration;
const PARA_ID: ParaId = ParaId::new(100);
@@ -42,11 +42,11 @@ fn main() -> Result<()> {
match role {
Role::Light => Err("Light client not supported".into()),
_ => {
let collator_key = CollatorPair::generate().0;
let collator = Collator::new();
let full_node = polkadot_service::build_full(
config,
polkadot_service::IsCollator::Yes(collator_key.public()),
polkadot_service::IsCollator::Yes(collator.collator_id()),
None,
Some(sc_authority_discovery::WorkerConfig {
query_interval: Duration::from_secs(1),
@@ -57,7 +57,6 @@ fn main() -> Result<()> {
let mut overseer_handler = full_node.overseer_handler
.expect("Overseer handler should be initialized for collators");
let collator = Collator::new();
let genesis_head_hex = format!("0x{:?}", HexDisplay::from(&collator.genesis_head()));
let validation_code_hex = format!("0x{:?}", HexDisplay::from(&collator.validation_code()));
@@ -66,7 +65,7 @@ fn main() -> Result<()> {
log::info!("Validation code: {}", validation_code_hex);
let config = CollationGenerationConfig {
key: collator_key,
key: collator.collator_key(),
collator: collator.create_collation_function(),
para_id: PARA_ID,
};
@@ -0,0 +1,73 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Integration test that ensures that we can build and include parachain
//! blocks of the adder parachain.
// If this test is failing, make sure to run all tests with the `real-overseer` feature being enabled.
#[substrate_test_utils::test]
async fn collating_using_adder_collator(task_executor: sc_service::TaskExecutor) {
use sp_keyring::AccountKeyring::*;
use futures::join;
use polkadot_primitives::v1::Id as ParaId;
sc_cli::init_logger("", Default::default(), None).expect("Sets up logger");
let para_id = ParaId::from(100);
// start alice
let alice = polkadot_test_service::run_validator_node(task_executor.clone(), Alice, || {}, vec![]);
// start bob
let bob = polkadot_test_service::run_validator_node(
task_executor.clone(),
Bob,
|| {},
vec![alice.addr.clone()],
);
let collator = test_parachain_adder_collator::Collator::new();
// register parachain
alice
.register_parachain(
para_id,
collator.validation_code().to_vec(),
collator.genesis_head(),
)
.await
.unwrap();
// run the collator node
let mut charlie = polkadot_test_service::run_collator_node(
task_executor.clone(),
Charlie,
|| {},
vec![alice.addr.clone(), bob.addr.clone()],
collator.collator_id(),
);
charlie.register_collator(collator.collator_key(), para_id, collator.create_collation_function()).await;
// Wait until the parachain has 4 blocks produced.
collator.wait_for_blocks(4).await;
join!(
alice.task_manager.clean_shutdown(),
bob.task_manager.clean_shutdown(),
charlie.task_manager.clean_shutdown(),
);
}