Make basic collation working

This commit is contained in:
Bastian Köcher
2019-10-18 14:16:32 +02:00
parent d9c6bc408c
commit b5bc9a21a3
12 changed files with 506 additions and 292 deletions
+19 -3
View File
@@ -1,4 +1,20 @@
use primitives::{Pair, Public};
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Cumulus 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.
// Cumulus 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
use substrate_primitives::{Pair, Public};
use parachain_runtime::{
AccountId, BalancesConfig, GenesisConfig, SudoConfig, IndicesConfig, SystemConfig, WASM_BINARY,
};
@@ -23,7 +39,7 @@ pub fn get_authority_keys_from_seed(seed: &str) -> (AccountId, AccountId) {
pub fn get_chain_spec() -> ChainSpec {
ChainSpec::from_genesis(
"Local Testnet",
"local_testnet",
"parachain_local_testnet",
|| testnet_genesis(
vec![
get_authority_keys_from_seed("Alice"),
@@ -55,7 +71,7 @@ pub fn get_chain_spec() -> ChainSpec {
}
fn testnet_genesis(
initial_authorities: Vec<(AccountId, AccountId)>,
_initial_authorities: Vec<(AccountId, AccountId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
+38 -14
View File
@@ -1,19 +1,35 @@
use crate::{chain_spec, service};
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Cumulus 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.
// Cumulus 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
use crate::chain_spec;
use parachain_runtime::Block;
pub use substrate_cli::{VersionInfo, IntoExit, error};
use substrate_cli::{informant, parse_and_prepare, ParseAndPrepare, NoCustom};
use substrate_service::{AbstractService, Roles as ServiceRoles, Configuration};
pub use substrate_cli::{VersionInfo, IntoExit, error::{self, Result}};
use substrate_cli::{parse_and_prepare, ParseAndPrepare, NoCustom};
use substrate_service::{Roles as ServiceRoles, Configuration};
use sr_primitives::{traits::{Block as BlockT, Header as HeaderT, Hash as HashT}, BuildStorage};
use substrate_client::genesis;
use primitives::hexdisplay::HexDisplay;
use substrate_primitives::hexdisplay::HexDisplay;
use codec::Encode;
use log::info;
use std::{path::PathBuf, cell::RefCell};
use std::{path::PathBuf, cell::RefCell, sync::Arc};
use structopt::StructOpt;
@@ -44,7 +60,7 @@ pub fn run<I, T, E>(args: I, exit: E, version: VersionInfo) -> error::Result<()>
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
E: IntoExit,
E: IntoExit + Send + 'static,
{
type Config<T> = Configuration<(), T>;
match parse_and_prepare::<SubCommands, NoCustom, _>(
@@ -53,18 +69,26 @@ pub fn run<I, T, E>(args: I, exit: E, version: VersionInfo) -> error::Result<()>
args,
) {
ParseAndPrepare::Run(cmd) => cmd.run(load_spec, exit,
|exit, _cli_args, _custom_args, config: Config<_>| {
|exit, _cli_args, _custom_args, mut config: Config<_>| {
info!("{}", version.name);
info!(" version {}", config.full_version());
info!(" by {}, 2019", version.author);
info!("Chain specification: {}", config.chain_spec.name());
info!("Node name: {}", config.name);
info!("Roles: {:?}", config.roles);
panic!();
// match config.roles {
// ServiceRoles::LIGHT => unimplemented!("Light client not supported!"),
// _ => unimplemented!(),
// }.map_err(|e| format!("{:?}", e))
info!("Parachain id: {:?}", crate::PARA_ID);
// TODO
let key = Arc::new(substrate_primitives::Pair::from_seed(&[10; 32]));
// TODO
config.network.listen_addresses = Vec::new();
config.chain_spec = chain_spec::get_chain_spec();
match config.roles {
ServiceRoles::LIGHT => unimplemented!("Light client not supported!"),
_ => crate::service::run_collator(config, exit, key, version.clone()),
}.map_err(|e| format!("{:?}", e))
}),
ParseAndPrepare::BuildSpec(cmd) => cmd.run(load_spec),
ParseAndPrepare::ExportBlocks(cmd) => cmd.run_with_builder(|config: Config<_>|
@@ -82,7 +106,7 @@ pub fn run<I, T, E>(args: I, exit: E, version: VersionInfo) -> error::Result<()>
Ok(())
}
fn load_spec(id: &str) -> Result<Option<chain_spec::ChainSpec>, String> {
fn load_spec(_: &str) -> std::result::Result<Option<chain_spec::ChainSpec>, String> {
Ok(Some(chain_spec::get_chain_spec()))
}
+22 -1
View File
@@ -1,8 +1,26 @@
//! Substrate Node Template CLI library.
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Cumulus 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.
// Cumulus 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
//! Cumulus test parachain collator
#![warn(missing_docs)]
#![warn(unused_extern_crates)]
use polkadot_primitives::parachain::Id as ParaId;
mod chain_spec;
#[macro_use]
mod service;
@@ -10,6 +28,9 @@ mod cli;
pub use substrate_cli::{VersionInfo, IntoExit, error};
/// The parachain id of this parachain.
pub const PARA_ID: ParaId = ParaId::new(100);
fn main() -> Result<(), cli::error::Error> {
let version = VersionInfo {
name: "Cumulus Test Parachain Collator",
+90 -17
View File
@@ -1,15 +1,34 @@
//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Cumulus 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.
// Cumulus 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 Cumulus. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc;
use std::time::Duration;
use substrate_client::LongestChain;
use futures::prelude::*;
use parachain_runtime::{self, GenesisConfig, opaque::Block, RuntimeApi};
use substrate_service::{error::{Error as ServiceError}, AbstractService, Configuration, ServiceBuilder};
use transaction_pool::{self, txpool::{Pool as TransactionPool}};
use parachain_runtime::{self, GenesisConfig, opaque::Block};
use inherents::InherentDataProviders;
use substrate_service::{AbstractService, Configuration};
use network::construct_simple_protocol;
use substrate_executor::native_executor_instance;
use futures::prelude::*;
use futures03::FutureExt;
use log::error;
pub use substrate_executor::NativeExecutor;
// Our native executor instance.
@@ -55,19 +74,73 @@ macro_rules! new_full_start {
}}
}
/// Builds a new service for a full client.
pub fn new_full<C: Send + Default + 'static>(config: Configuration<C, GenesisConfig>)
-> Result<impl AbstractService, ServiceError>
{
let is_authority = config.roles.is_authority();
let name = config.name.clone();
let disable_grandpa = config.disable_grandpa;
let force_authoring = config.force_authoring;
/// Run the collator with the given `config`.
pub fn run_collator<C: Send + Default + 'static, E: crate::cli::IntoExit + Send + 'static>(
config: Configuration<C, GenesisConfig>,
exit: E,
key: Arc<polkadot_primitives::parachain::CollatorPair>,
version: crate::cli::VersionInfo,
) -> crate::cli::Result<()> {
let (builder, inherent_data_providers) = new_full_start!(config);
inherent_data_providers.register_provider(srml_timestamp::InherentDataProvider).unwrap();
let service = builder.with_network_protocol(|_| Ok(NodeProtocol::new()))?.build()?;
let proposer_factory = basic_authorship::ProposerFactory {
client: service.client(),
transaction_pool: service.transaction_pool(),
};
Ok(service)
let on_exit = service.on_exit();
let setup_parachain = SetupParachain { service, inherent_data_providers, proposer_factory, exit };
cumulus_collator::run_collator(setup_parachain, crate::PARA_ID, on_exit, key, version)
}
struct SetupParachain<S, PF, E> {
service: S,
proposer_factory: PF,
exit: E,
inherent_data_providers: InherentDataProviders,
}
impl<S, PF, E> cumulus_collator::SetupParachain<Block> for SetupParachain<S, PF, E>
where
S: AbstractService,
E: Send + crate::cli::IntoExit,
PF: consensus_common::Environment<Block> + Send + 'static,
<PF::Proposer as consensus_common::Proposer<Block>>::Create: Send + Unpin,
PF::Error: std::fmt::Debug,
{
type ProposerFactory = PF;
fn setup_parachain<P: cumulus_consensus::PolkadotClient>(
self,
polkadot_client: P,
task_executor: polkadot_collator::TaskExecutor,
) -> Result<(Self::ProposerFactory, InherentDataProviders), String> {
let client = self.service.client();
let follow = match cumulus_consensus::follow_polkadot(crate::PARA_ID, client, polkadot_client) {
Ok(follow) => follow,
Err(e) => {
return Err(format!("Could not start following polkadot: {:?}", e));
}
};
task_executor.execute(
Box::new(
self.service
.map_err(|e| error!("Parachain service error: {:?}", e))
.select(futures03::compat::Compat::new(follow.map(|_| Ok::<(), ()>(()))))
.map(|_| ())
.map_err(|_| ())
.select(self.exit.into_exit())
.map(|_| ())
.map_err(|_| ())
),
).map_err(|_| "Could not spawn parachain server!")?;
Ok((self.proposer_factory, self.inherent_data_providers))
}
}