Collate only if --validator is given (#161)

This commit is contained in:
Cecile Tonglet
2020-07-29 20:49:31 +02:00
committed by GitHub
parent c66f9755cb
commit d1d40765c7
16 changed files with 1425 additions and 1363 deletions
+4 -2
View File
@@ -31,15 +31,18 @@ sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "cumu
sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-cli = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-executor = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-network = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-basic-authorship = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", version = "0.8.0-alpha.5" }
sc-basic-authorship = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", version = "0.8.0-rc5" }
sp-timestamp = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-informant = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-chain-spec = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
# Cumulus dependencies
cumulus-consensus = { path = "../../consensus" }
@@ -72,6 +75,5 @@ polkadot-test-service = { git = "https://github.com/paritytech/polkadot", branch
# Substrate dependencies
pallet-sudo = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
substrate-test-runtime-client = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
+7 -5
View File
@@ -152,8 +152,10 @@ pub fn run() -> Result<()> {
Some(Subcommand::Base(subcommand)) => {
let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| {
Ok(new_full_start!(config).0.to_chain_ops_parts())
runner.run_subcommand(subcommand, |mut config| {
let params = crate::service::new_partial(&mut config)?;
Ok((params.client, params.backend, params.import_queue, params.task_manager))
})
}
Some(Subcommand::ExportGenesisState(params)) => {
@@ -185,7 +187,7 @@ pub fn run() -> Result<()> {
let id = ParaId::from(cli.run.parachain_id);
let parachain_account =
AccountIdConversion::<polkadot_primitives::AccountId>::into_account(&id);
AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&id);
let block = generate_genesis_state(id)?;
let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));
@@ -207,8 +209,8 @@ pub fn run() -> Result<()> {
info!("Parachain Account: {}", parachain_account);
info!("Parachain genesis state: {}", genesis_state);
crate::service::run_collator(config, key, polkadot_config, id)
.map(|x| x.task_manager)
crate::service::run_collator(config, key, polkadot_config, id, cli.run.base.validator)
.map(|(x, _)| x)
})
}
}
@@ -19,7 +19,7 @@ use futures::{
future::{self, FutureExt},
pin_mut, select,
};
use polkadot_primitives::parachain::{Id as ParaId, Info, Scheduling};
use polkadot_primitives::v0::{Id as ParaId, Info, Scheduling};
use polkadot_runtime_common::registrar;
use polkadot_test_runtime_client::Sr25519Keyring;
use sc_client_api::execution_extensions::ExecutionStrategies;
@@ -43,9 +43,8 @@ static INTEGRATION_TEST_ALLOWED_TIME: Option<&str> = option_env!("INTEGRATION_TE
#[ignore]
async fn integration_test() {
let task_executor: TaskExecutor = (|fut, _| {
spawn(fut);
})
.into();
spawn(fut).map(|_| ())
}).into();
// start alice
let mut alice =
@@ -104,10 +103,10 @@ async fn integration_test() {
polkadot_config.rpc_methods = sc_service::config::RpcMethods::Unsafe;
let parachain_config =
parachain_config(task_executor.clone(), Charlie, vec![], para_id).unwrap();
let service =
crate::service::run_collator(parachain_config, key, polkadot_config, para_id).unwrap();
let (_service, charlie_client) =
crate::service::run_collator(parachain_config, key, polkadot_config, para_id, true).unwrap();
sleep(Duration::from_secs(3)).await;
service.client.wait_for_blocks(4).await;
charlie_client.wait_for_blocks(4).await;
alice.task_manager.terminate();
bob.task_manager.terminate();
+158 -104
View File
@@ -18,15 +18,15 @@ use ansi_term::Color;
use cumulus_collator::{prepare_collator_config, CollatorBuilder};
use cumulus_network::DelayedBlockAnnounceValidator;
use futures::{future::ready, FutureExt};
use polkadot_primitives::parachain::CollatorPair;
use polkadot_primitives::v0::CollatorPair;
use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sc_finality_grandpa::{
FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider,
};
use sc_informant::OutputFormat;
use sc_service::{Configuration, ServiceComponents, TFullBackend, TFullClient};
use sc_service::{Configuration, PartialComponents, TaskManager, TFullBackend, TFullClient, Role};
use std::sync::Arc;
use sp_core::crypto::Pair;
use sp_trie::PrefixedMemoryDB;
use sp_runtime::traits::BlakeTwo256;
// Our native executor instance.
native_executor_instance!(
@@ -39,45 +39,60 @@ native_executor_instance!(
///
/// Use this macro if you don't actually need the full service, but just the builder in order to
/// be able to perform chain operations.
macro_rules! new_full_start {
($config:expr) => {{
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
pub fn new_partial(config: &mut Configuration) -> Result<
PartialComponents<
TFullClient<parachain_runtime::opaque::Block, parachain_runtime::RuntimeApi, crate::service::Executor>,
TFullBackend<parachain_runtime::opaque::Block>,
(),
sp_consensus::import_queue::BasicQueue<parachain_runtime::opaque::Block, PrefixedMemoryDB<BlakeTwo256>>,
sc_transaction_pool::FullPool<parachain_runtime::opaque::Block, TFullClient<parachain_runtime::opaque::Block, parachain_runtime::RuntimeApi, crate::service::Executor>>,
(),
>,
sc_service::Error,
>
{
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
let builder = sc_service::ServiceBuilder::new_full::<
let (client, backend, keystore, task_manager) =
sc_service::new_full_parts::<
parachain_runtime::opaque::Block,
parachain_runtime::RuntimeApi,
crate::service::Executor,
>($config)?
.with_select_chain(|_config, backend| Ok(sc_consensus::LongestChain::new(backend.clone())))?
.with_transaction_pool(|builder| {
let client = builder.client();
let pool_api = Arc::new(sc_transaction_pool::FullChainApi::new(
client.clone(),
builder.prometheus_registry(),
));
let pool = sc_transaction_pool::BasicPool::new_full(
builder.config().transaction_pool.clone(),
pool_api,
builder.prometheus_registry(),
builder.spawn_handle(),
client.clone(),
);
Ok(pool)
})?
.with_import_queue(|_config, client, _, _, spawner, registry| {
let import_queue = cumulus_consensus::import_queue::import_queue(
client.clone(),
client,
inherent_data_providers.clone(),
spawner,
registry,
)?;
>(&config)?;
let client = Arc::new(client);
//let select_chain = sc_consensus::LongestChain::new(backend.clone());
Ok(import_queue)
})?;
let registry = config.prometheus_registry();
(builder, inherent_data_providers)
}};
let transaction_pool = sc_transaction_pool::BasicPool::new_full(
config.transaction_pool.clone(),
//std::sync::Arc::new(pool_api),
config.prometheus_registry(),
task_manager.spawn_handle(),
client.clone(),
);
let import_queue = cumulus_consensus::import_queue::import_queue(
client.clone(),
client.clone(),
inherent_data_providers.clone(),
&task_manager.spawn_handle(),
registry.clone(),
)?;
let params = PartialComponents {
backend,
client,
import_queue,
keystore,
task_manager,
transaction_pool,
inherent_data_providers,
select_chain: (),
other: (),
};
Ok(params)
}
/// Run a collator node with the given parachain `Configuration` and relaychain `Configuration`
@@ -87,84 +102,123 @@ pub fn run_collator(
parachain_config: Configuration,
key: Arc<CollatorPair>,
mut polkadot_config: polkadot_collator::Configuration,
id: polkadot_primitives::parachain::Id,
) -> sc_service::error::Result<ServiceComponents<
parachain_runtime::opaque::Block,
TFullBackend<parachain_runtime::opaque::Block>,
sc_consensus::LongestChain<TFullBackend<parachain_runtime::opaque::Block>, parachain_runtime::opaque::Block>,
sc_transaction_pool::BasicPool<
sc_transaction_pool::FullChainApi<
TFullClient<parachain_runtime::opaque::Block, parachain_runtime::RuntimeApi, crate::service::Executor>,
parachain_runtime::opaque::Block,
>,
parachain_runtime::opaque::Block,
>,
TFullClient<parachain_runtime::opaque::Block, parachain_runtime::RuntimeApi, crate::service::Executor>,
>> {
id: polkadot_primitives::v0::Id,
validator: bool,
) -> sc_service::error::Result<(
TaskManager,
Arc<TFullClient<parachain_runtime::opaque::Block, parachain_runtime::RuntimeApi, crate::service::Executor>>,
)> {
let mut parachain_config = prepare_collator_config(parachain_config);
parachain_config.informant_output_format = OutputFormat {
enable_color: true,
prefix: format!("[{}] ", Color::Yellow.bold().paint("Parachain")),
};
let (builder, inherent_data_providers) = new_full_start!(parachain_config);
inherent_data_providers
.register_provider(sp_timestamp::InherentDataProvider)
.unwrap();
let block_announce_validator = DelayedBlockAnnounceValidator::new();
let block_announce_validator_copy = block_announce_validator.clone();
let service = builder
.with_finality_proof_provider(|client, backend| {
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
})?
.with_block_announce_validator(|_client| Box::new(block_announce_validator_copy))?
.build_full()?;
let registry = service.prometheus_registry.clone();
let proposer_factory = sc_basic_authorship::ProposerFactory::new(
service.client.clone(),
service.transaction_pool.clone(),
registry.as_ref(),
);
let block_import = service.client.clone();
let client = service.client.clone();
let network = service.network.clone();
let announce_block = Arc::new(move |hash, data| network.announce_block(hash, data));
let builder = CollatorBuilder::new(
proposer_factory,
inherent_data_providers,
block_import,
client.clone(),
id,
client,
announce_block,
block_announce_validator,
);
polkadot_config.informant_output_format = OutputFormat {
enable_color: true,
prefix: format!("[{}] ", Color::Blue.bold().paint("Relaychain")),
};
let (polkadot_future, task_manager) =
polkadot_collator::start_collator(builder, id, key, polkadot_config)?;
let params = new_partial(&mut parachain_config)?;
params.inherent_data_providers
.register_provider(sp_timestamp::InherentDataProvider)
.unwrap();
// Make sure the polkadot task manager survives as long as the service.
let polkadot_future = polkadot_future.then(move |_| {
let _ = task_manager;
ready(())
});
let client = params.client.clone();
let backend = params.backend.clone();
let block_announce_validator = DelayedBlockAnnounceValidator::new();
let block_announce_validator_builder = {
let block_announce_validator = block_announce_validator.clone();
move |_| Box::new(block_announce_validator) as Box<_>
};
service
.task_manager
.spawn_essential_handle()
.spawn("polkadot", polkadot_future);
let prometheus_registry = parachain_config.prometheus_registry().cloned();
let transaction_pool = params.transaction_pool.clone();
let mut task_manager = params.task_manager;
let import_queue = params.import_queue;
let (network, network_status_sinks, system_rpc_tx) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: &parachain_config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
spawn_handle: task_manager.spawn_handle(),
import_queue,
on_demand: None,
block_announce_validator_builder: Some(Box::new(block_announce_validator_builder)),
finality_proof_request_builder: None,
finality_proof_provider: None,
})?;
Ok(service)
let _rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams {
on_demand: None,
remote_blockchain: None,
rpc_extensions_builder: Box::new(|_| ()),
client: client.clone(),
transaction_pool: transaction_pool.clone(),
task_manager: &mut task_manager,
telemetry_connection_sinks: Default::default(),
config: parachain_config,
keystore: params.keystore,
backend,
network: network.clone(),
network_status_sinks,
system_rpc_tx,
})?;
if validator {
let proposer_factory = sc_basic_authorship::ProposerFactory::new(
client.clone(),
transaction_pool,
prometheus_registry.as_ref(),
);
let block_import = client.clone();
let announce_block = Arc::new(move |hash, data| network.announce_block(hash, data));
let builder = CollatorBuilder::new(
proposer_factory,
params.inherent_data_providers,
block_import,
client.clone(),
id,
client.clone(),
announce_block,
block_announce_validator,
);
let (polkadot_future, polkadpt_task_manager) =
polkadot_collator::start_collator(builder, id, key, polkadot_config)?;
// Make sure the polkadot task manager survives as long as the service.
let polkadot_future = polkadot_future.then(move |_| {
let _ = polkadpt_task_manager;
ready(())
});
task_manager
.spawn_essential_handle()
.spawn("polkadot", polkadot_future);
} else {
let is_light = matches!(polkadot_config.role, Role::Light);
let builder = polkadot_service::NodeBuilder::new(polkadot_config);
let mut polkadot_task_manager = if is_light {
return Err("Light client not supported.".into());
} else {
builder.build_full(
Some((key.public(), id)),
None,
false,
6000,
None,
)
}?;
let polkadot_future = async move {
polkadot_task_manager.future().await.expect("polkadot essential task failed");
};
task_manager
.spawn_essential_handle()
.spawn("polkadot", polkadot_future);
}
Ok((task_manager, client))
}
@@ -28,7 +28,7 @@ fn interrupt_polkadot_mdns_issue_test() {
fn run_command_and_kill(signal: Signal) {
let _ = fs::remove_dir_all("interrupt_polkadot_mdns_issue_test");
let mut cmd = Command::new(cargo_bin("cumulus-test-parachain-collator"))
.args(&["-d", "interrupt_polkadot_mdns_issue_test"])
.args(&["-d", "interrupt_polkadot_mdns_issue_test", "--dev", "--", "--dev"])
.spawn()
.unwrap();
@@ -29,7 +29,7 @@ fn purge_chain_works() {
let _ = fs::remove_dir_all(base_path);
let mut cmd = Command::new(cargo_bin("cumulus-test-parachain-collator"))
.args(&["-d", base_path])
.args(&["-d", base_path, "--dev", "--", "--dev"])
.spawn()
.unwrap();
@@ -28,7 +28,7 @@ fn running_the_node_works_and_can_be_interrupted() {
fn run_command_and_kill(signal: Signal) {
let _ = fs::remove_dir_all("interrupt_test");
let mut cmd = Command::new(cargo_bin("cumulus-test-parachain-collator"))
.args(&["--dev", "-d", "interrupt_test"])
.args(&["--dev", "-d", "interrupt_test", "--", "--dev"])
.spawn()
.unwrap();