Make everything compile and fix tests

This commit is contained in:
Bastian Köcher
2020-01-14 22:22:21 +01:00
parent 1418b842aa
commit 828447d49c
26 changed files with 5214 additions and 3536 deletions
+40 -33
View File
@@ -14,14 +14,14 @@
// 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,
AccountId, BalancesConfig, GenesisConfig, IndicesConfig, SudoConfig, SystemConfig, WASM_BINARY,
};
use substrate_service;
use sc_service;
use sp_core::{Pair, Public};
/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
pub type ChainSpec = substrate_service::ChainSpec<GenesisConfig>;
pub type ChainSpec = sc_service::ChainSpec<GenesisConfig>;
/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
@@ -32,7 +32,10 @@ pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Pu
/// Helper function to generate stash, controller and session key from seed
pub fn get_authority_keys_from_seed(seed: &str) -> (AccountId, AccountId) {
(get_from_seed::<AccountId>(&format!("{}//stash", seed)), get_from_seed::<AccountId>(seed))
(
get_from_seed::<AccountId>(&format!("{}//stash", seed)),
get_from_seed::<AccountId>(seed),
)
}
/// Returns the chain spec.
@@ -40,28 +43,30 @@ pub fn get_chain_spec() -> ChainSpec {
ChainSpec::from_genesis(
"Local Testnet",
"parachain_local_testnet",
|| testnet_genesis(
vec![
get_authority_keys_from_seed("Alice"),
get_authority_keys_from_seed("Bob"),
],
get_from_seed::<AccountId>("Alice"),
vec![
|| {
testnet_genesis(
vec![
get_authority_keys_from_seed("Alice"),
get_authority_keys_from_seed("Bob"),
],
get_from_seed::<AccountId>("Alice"),
get_from_seed::<AccountId>("Bob"),
get_from_seed::<AccountId>("Charlie"),
get_from_seed::<AccountId>("Dave"),
get_from_seed::<AccountId>("Eve"),
get_from_seed::<AccountId>("Ferdie"),
get_from_seed::<AccountId>("Alice//stash"),
get_from_seed::<AccountId>("Bob//stash"),
get_from_seed::<AccountId>("Charlie//stash"),
get_from_seed::<AccountId>("Dave//stash"),
get_from_seed::<AccountId>("Eve//stash"),
get_from_seed::<AccountId>("Ferdie//stash"),
],
true,
),
vec![
get_from_seed::<AccountId>("Alice"),
get_from_seed::<AccountId>("Bob"),
get_from_seed::<AccountId>("Charlie"),
get_from_seed::<AccountId>("Dave"),
get_from_seed::<AccountId>("Eve"),
get_from_seed::<AccountId>("Ferdie"),
get_from_seed::<AccountId>("Alice//stash"),
get_from_seed::<AccountId>("Bob//stash"),
get_from_seed::<AccountId>("Charlie//stash"),
get_from_seed::<AccountId>("Dave//stash"),
get_from_seed::<AccountId>("Eve//stash"),
get_from_seed::<AccountId>("Ferdie//stash"),
],
true,
)
},
vec![],
None,
None,
@@ -77,19 +82,21 @@ fn testnet_genesis(
_enable_println: bool,
) -> GenesisConfig {
GenesisConfig {
system: Some(SystemConfig {
frame_system: Some(SystemConfig {
code: WASM_BINARY.to_vec(),
changes_trie_config: Default::default(),
}),
indices: Some(IndicesConfig {
pallet_indices: Some(IndicesConfig {
ids: endowed_accounts.clone(),
}),
balances: Some(BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(),
pallet_balances: Some(BalancesConfig {
balances: endowed_accounts
.iter()
.cloned()
.map(|k| (k, 1 << 60))
.collect(),
vesting: vec![],
}),
sudo: Some(SudoConfig {
key: root_key,
}),
pallet_sudo: Some(SudoConfig { key: root_key }),
}
}
+93 -50
View File
@@ -18,23 +18,29 @@ use crate::chain_spec;
use parachain_runtime::Block;
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 substrate_primitives::hexdisplay::HexDisplay;
pub use sc_cli::{
error::{self, Result},
IntoExit, VersionInfo,
};
use sc_cli::{parse_and_prepare, NoCustom, ParseAndPrepare};
use sc_client::genesis;
use sc_service::{Configuration, Roles as ServiceRoles};
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::{
traits::{Block as BlockT, Hash as HashT, Header as HeaderT},
BuildStorage,
};
use futures::{channel::oneshot, future::Map, FutureExt};
use codec::Encode;
use log::info;
use std::{path::PathBuf, cell::RefCell, sync::Arc};
use std::{cell::RefCell, path::PathBuf, sync::Arc};
use structopt::StructOpt;
use futures::{sync::oneshot, future, Future};
/// Sub-commands supported by the collator.
#[derive(Debug, StructOpt, Clone)]
enum SubCommands {
@@ -43,8 +49,10 @@ enum SubCommands {
ExportGenesisState(ExportGenesisStateCommand),
}
impl substrate_cli::GetLogFilter for SubCommands {
fn get_log_filter(&self) -> Option<String> { None }
impl sc_cli::GetSharedParams for SubCommands {
fn shared_params(&self) -> Option<&sc_cli::SharedParams> {
None
}
}
/// Command for exporting the genesis state of the parachain
@@ -57,10 +65,10 @@ struct ExportGenesisStateCommand {
/// Parse command line arguments into service configuration.
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 + Send + 'static,
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
E: IntoExit + Send + 'static,
{
type Config<T> = Configuration<(), T>;
match parse_and_prepare::<SubCommands, NoCustom, _>(
@@ -68,37 +76,68 @@ pub fn run<I, T, E>(args: I, exit: E, version: VersionInfo) -> error::Result<()>
"cumulus-test-parachain-collator",
args,
) {
ParseAndPrepare::Run(cmd) => cmd.run(load_spec, exit,
|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);
info!("Parachain id: {:?}", crate::PARA_ID);
ParseAndPrepare::Run(cmd) => cmd.run(
load_spec,
exit,
|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);
info!("Parachain id: {:?}", crate::PARA_ID);
// TODO
let key = Arc::new(substrate_primitives::Pair::from_seed(&[10; 32]));
// TODO
let key = Arc::new(sp_core::Pair::from_seed(&[10; 32]));
// TODO
config.network.listen_addresses = Vec::new();
config.network.boot_nodes = vec![];
config.chain_spec = chain_spec::get_chain_spec();
// TODO
config.network.listen_addresses = Vec::new();
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<_>|
Ok(new_full_start!(config).0), load_spec, exit),
ParseAndPrepare::ImportBlocks(cmd) => cmd.run_with_builder(|config: Config<_>|
Ok(new_full_start!(config).0), load_spec, exit),
// TODO
let mut polkadot_config =
polkadot_collator::Configuration::default_with_spec_and_base_path(
polkadot_service::ChainSpec::from_json_bytes(
&include_bytes!("../res/polkadot_chainspec.json")[..],
)?,
config.in_chain_config_dir("polkadot"),
);
polkadot_config.network.boot_nodes = config.network.boot_nodes.clone();
if let Some(ref config_dir) = polkadot_config.config_dir {
polkadot_config.database = sc_service::config::DatabaseConfig::Path {
cache_size: Default::default(),
path: config_dir.join("db"),
};
}
match config.roles {
ServiceRoles::LIGHT => unimplemented!("Light client not supported!"),
_ => crate::service::run_collator(config, exit, key, polkadot_config),
}
.map_err(|e| format!("{:?}", e))
},
),
ParseAndPrepare::BuildSpec(cmd) => cmd.run::<NoCustom, _, _, _>(load_spec),
ParseAndPrepare::ExportBlocks(cmd) => cmd.run_with_builder(
|config: Config<_>| Ok(new_full_start!(config).0),
load_spec,
exit,
),
ParseAndPrepare::ImportBlocks(cmd) => cmd.run_with_builder(
|config: Config<_>| Ok(new_full_start!(config).0),
load_spec,
exit,
),
ParseAndPrepare::CheckBlock(cmd) => cmd.run_with_builder(
|config: Config<_>| Ok(new_full_start!(config).0),
load_spec,
exit,
),
ParseAndPrepare::PurgeChain(cmd) => cmd.run(load_spec),
ParseAndPrepare::RevertChain(cmd) => cmd.run_with_builder(|config: Config<_>|
Ok(new_full_start!(config).0), load_spec),
ParseAndPrepare::RevertChain(cmd) => {
cmd.run_with_builder(|config: Config<_>| Ok(new_full_start!(config).0), load_spec)
}
ParseAndPrepare::CustomCommand(SubCommands::ExportGenesisState(cmd)) => {
export_genesis_state(cmd.output)
}
@@ -113,16 +152,16 @@ fn load_spec(_: &str) -> std::result::Result<Option<chain_spec::ChainSpec>, Stri
/// Export the genesis state of the parachain.
fn export_genesis_state(output: Option<PathBuf>) -> error::Result<()> {
let storage = chain_spec::get_chain_spec().build_storage()?;
let storage = (&chain_spec::get_chain_spec()).build_storage()?;
let child_roots = storage.1.iter().map(|(sk, child_map)| {
let child_roots = storage.children.iter().map(|(sk, child_content)| {
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_map.clone().into_iter().collect()
child_content.data.clone().into_iter().collect(),
);
(sk.clone(), state_root.encode())
});
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
storage.0.clone().into_iter().chain(child_roots).collect()
storage.top.clone().into_iter().chain(child_roots).collect(),
);
let block: Block = genesis::construct_genesis_block(state_root);
@@ -140,19 +179,23 @@ fn export_genesis_state(output: Option<PathBuf>) -> error::Result<()> {
// handles ctrl-c
pub struct Exit;
impl IntoExit for Exit {
type Exit = future::MapErr<oneshot::Receiver<()>, fn(oneshot::Canceled) -> ()>;
type Exit = Map<oneshot::Receiver<()>, fn(std::result::Result<(), oneshot::Canceled>) -> ()>;
fn into_exit(self) -> Self::Exit {
// can't use signal directly here because CtrlC takes only `Fn`.
let (exit_send, exit) = oneshot::channel();
let exit_send_cell = RefCell::new(Some(exit_send));
ctrlc::set_handler(move || {
let exit_send = exit_send_cell.try_borrow_mut().expect("signal handler not reentrant; qed").take();
let exit_send = exit_send_cell
.try_borrow_mut()
.expect("signal handler not reentrant; qed")
.take();
if let Some(exit_send) = exit_send {
exit_send.send(()).expect("Error sending exit notification");
}
}).expect("Error setting Ctrl-C handler");
})
.expect("Error setting Ctrl-C handler");
exit.map_err(drop)
exit.map(drop)
}
}
+1 -1
View File
@@ -26,7 +26,7 @@ mod chain_spec;
mod service;
mod cli;
pub use substrate_cli::{VersionInfo, IntoExit, error};
pub use sc_cli::{error, IntoExit, VersionInfo};
/// The parachain id of this parachain.
pub const PARA_ID: ParaId = ParaId::new(100);
+97 -59
View File
@@ -16,20 +16,19 @@
use std::sync::Arc;
use parachain_runtime::{self, GenesisConfig, opaque::Block};
use parachain_runtime::{self, opaque::Block, GenesisConfig};
use inherents::InherentDataProviders;
use substrate_service::{AbstractService, Configuration};
use network::construct_simple_protocol;
use substrate_executor::native_executor_instance;
use sc_executor::native_executor_instance;
use sc_network::construct_simple_protocol;
use sc_service::{AbstractService, Configuration};
use sp_consensus::{BlockImport, Environment, Proposer};
use sp_inherents::InherentDataProviders;
use futures::prelude::*;
use futures03::FutureExt;
use futures::{compat::Future01CompatExt, future, task::Spawn, FutureExt, TryFutureExt};
use log::error;
pub use substrate_executor::NativeExecutor;
pub use sc_executor::NativeExecutor;
// Our native executor instance.
native_executor_instance!(
@@ -49,29 +48,35 @@ construct_simple_protocol! {
/// be able to perform chain operations.
macro_rules! new_full_start {
($config:expr) => {{
let inherent_data_providers = inherents::InherentDataProviders::new();
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
let builder = substrate_service::ServiceBuilder::new_full::<
parachain_runtime::opaque::Block, parachain_runtime::RuntimeApi, crate::service::Executor,
let builder = sc_service::ServiceBuilder::new_full::<
parachain_runtime::opaque::Block,
parachain_runtime::RuntimeApi,
crate::service::Executor,
>($config)?
.with_select_chain(|_config, backend| {
Ok(substrate_client::LongestChain::new(backend.clone()))
})?
.with_transaction_pool(|config, client|
Ok(transaction_pool::txpool::Pool::new(config, transaction_pool::FullChainApi::new(client)))
)?
.with_import_queue(|_config, client, _, _| {
let import_queue = cumulus_consensus::import_queue::import_queue(
client.clone(),
client,
inherent_data_providers.clone(),
)?;
.with_select_chain(|_config, backend| Ok(sc_client::LongestChain::new(backend.clone())))?
.with_transaction_pool(|config, client, _| {
let pool_api = sc_transaction_pool::FullChainApi::new(client.clone());
let pool = sc_transaction_pool::BasicPool::new(config, pool_api);
let maintainer =
sc_transaction_pool::FullBasicPoolMaintainer::new(pool.pool().clone(), client);
let maintainable_pool =
sp_transaction_pool::MaintainableTransactionPool::new(pool, maintainer);
Ok(maintainable_pool)
})?
.with_import_queue(|_config, client, _, _| {
let import_queue = cumulus_consensus::import_queue::import_queue(
client.clone(),
client,
inherent_data_providers.clone(),
)?;
Ok(import_queue)
})?;
Ok(import_queue)
})?;
(builder, inherent_data_providers)
}}
}};
}
/// Run the collator with the given `config`.
@@ -79,13 +84,17 @@ pub fn run_collator<C: Send + Default + 'static, E: crate::cli::IntoExit + Send
config: Configuration<C, GenesisConfig>,
exit: E,
key: Arc<polkadot_primitives::parachain::CollatorPair>,
version: crate::cli::VersionInfo,
polkadot_config: polkadot_collator::Configuration,
) -> crate::cli::Result<()> {
let (builder, inherent_data_providers) = new_full_start!(config);
inherent_data_providers.register_provider(srml_timestamp::InherentDataProvider).unwrap();
inherent_data_providers
.register_provider(sp_timestamp::InherentDataProvider)
.unwrap();
let service = builder.with_network_protocol(|_| Ok(NodeProtocol::new()))?.build()?;
let proposer_factory = basic_authorship::ProposerFactory {
let service = builder
.with_network_protocol(|_| Ok(NodeProtocol::new()))?
.build()?;
let proposer_factory = sc_basic_authority::ProposerFactory {
client: service.client(),
transaction_pool: service.transaction_pool(),
};
@@ -101,7 +110,13 @@ pub fn run_collator<C: Send + Default + 'static, E: crate::cli::IntoExit + Send
block_import,
};
cumulus_collator::run_collator(setup_parachain, crate::PARA_ID, on_exit, key, version)
cumulus_collator::run_collator(
setup_parachain,
crate::PARA_ID,
on_exit,
key,
polkadot_config,
)
}
struct SetupParachain<S, PF, E, BI> {
@@ -112,43 +127,66 @@ struct SetupParachain<S, PF, E, BI> {
block_import: BI,
}
type TransactionFor<E, Block> =
<<E as Environment<Block>>::Proposer as Proposer<Block>>::Transaction;
impl<S, PF, E, BI> cumulus_collator::SetupParachain<Block> for SetupParachain<S, PF, E, BI>
where
S: AbstractService,
E: Send + crate::cli::IntoExit,
PF: consensus_common::Environment<Block> + Send + 'static,
BI: consensus_common::BlockImport<Block, Error=consensus_common::Error> + Send + Sync + 'static,
where
S: AbstractService,
E: Send + crate::cli::IntoExit,
PF: Environment<Block> + Send + 'static,
BI: BlockImport<Block, Error = sp_consensus::Error, Transaction = TransactionFor<PF, Block>>
+ Send
+ Sync
+ 'static,
{
type ProposerFactory = PF;
type BlockImport = BI;
fn setup_parachain<P: cumulus_consensus::PolkadotClient>(
fn setup_parachain<P: cumulus_consensus::PolkadotClient, Spawner>(
self,
polkadot_client: P,
task_executor: polkadot_collator::TaskExecutor,
) -> Result<(Self::ProposerFactory, Self::BlockImport, InherentDataProviders), String> {
spawner: Spawner,
) -> Result<
(
Self::ProposerFactory,
Self::BlockImport,
InherentDataProviders,
),
String,
>
where
Spawner: Spawn + Clone + Send + Sync + 'static,
{
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));
}
};
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!")?;
spawner
.spawn_obj(
Box::new(
future::select(
self.service
.compat()
.map_err(|e| error!("Parachain service error: {:?}", e)),
future::select(follow, self.exit.into_exit()),
)
.map(|_| ()),
)
.into(),
)
.map_err(|_| "Could not spawn parachain server!")?;
Ok((self.proposer_factory, self.block_import, self.inherent_data_providers))
Ok((
self.proposer_factory,
self.block_import,
self.inherent_data_providers,
))
}
}