diff --git a/bridges/bin/node/node/Cargo.toml b/bridges/bin/node/node/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..4bd5972c3465bc708918d30528d8a93c0dca03d7 --- /dev/null +++ b/bridges/bin/node/node/Cargo.toml @@ -0,0 +1,108 @@ +[package] +name = "bridge-node" +version = "0.1.0" +authors = ["Parity Technologies <admin@parity.io>"] +edition = "2018" +build = "build.rs" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/parity-bridges-common/" + +[[bin]] +name = "bridge-node" + +[dependencies] +futures = "0.3.1" +log = "0.4.8" +structopt = "0.3.8" +bridge-node-runtime = { version = "0.1.0", path = "../runtime" } + +[dependencies.sc-cli] +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sp-core] +version = "2.0.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sc-executor] +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sc-service] +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sp-inherents] +version = "2.0.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sc-transaction-pool] +version = "2.0.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sp-transaction-pool] +version = "2.0.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sc-network] +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sc-consensus-aura] +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sp-consensus-aura] +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sp-consensus] +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.grandpa] +package = "sc-finality-grandpa" +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.grandpa-primitives] +package = "sp-finality-grandpa" +version = "2.0.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sc-client] +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sp-runtime] +version = "2.0.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[dependencies.sc-basic-authorship] +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" + +[build-dependencies] +vergen = "3.0.4" + +[build-dependencies.build-script-utils] +package = "substrate-build-script-utils" +version = "2.0.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate.git" diff --git a/bridges/bin/node/node/build.rs b/bridges/bin/node/node/build.rs new file mode 100644 index 0000000000000000000000000000000000000000..222cbb409285b40e7204cd609d444854dd4082aa --- /dev/null +++ b/bridges/bin/node/node/build.rs @@ -0,0 +1,9 @@ +use vergen::{ConstantsFlags, generate_cargo_keys}; + +const ERROR_MSG: &str = "Failed to generate metadata files"; + +fn main() { + generate_cargo_keys(ConstantsFlags::SHA_SHORT).expect(ERROR_MSG); + + build_script_utils::rerun_if_git_head_changed(); +} diff --git a/bridges/bin/node/node/src/chain_spec.rs b/bridges/bin/node/node/src/chain_spec.rs new file mode 100644 index 0000000000000000000000000000000000000000..5693dc21d8a7f947aaf1d2791718444a32426e5a --- /dev/null +++ b/bridges/bin/node/node/src/chain_spec.rs @@ -0,0 +1,113 @@ +use sp_core::{Pair, Public, sr25519}; +use bridge_node_runtime::{ + AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, + SudoConfig, SystemConfig, WASM_BINARY, Signature +}; +use sp_consensus_aura::sr25519::{AuthorityId as AuraId}; +use grandpa_primitives::{AuthorityId as GrandpaId}; +use sc_service; +use sp_runtime::traits::{Verify, IdentifyAccount}; + +/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type. +pub type ChainSpec = sc_service::ChainSpec<GenesisConfig>; + +/// The chain specification option. This is expected to come in from the CLI and +/// is little more than one of a number of alternatives which can easily be converted +/// from a string (`--chain=...`) into a `ChainSpec`. +#[derive(Clone, Debug)] +pub enum Alternative { + /// Whatever the current runtime is, with just Alice as an auth. + Development, +} + +/// Helper function to generate a crypto pair from seed +pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public { + TPublic::Pair::from_string(&format!("//{}", seed), None) + .expect("static values are valid; qed") + .public() +} + +type AccountPublic = <Signature as Verify>::Signer; + +/// Helper function to generate an account ID from seed +pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId where + AccountPublic: From<<TPublic::Pair as Pair>::Public> +{ + AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account() +} + +/// Helper function to generate an authority key for Aura +pub fn get_authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) { + ( + get_from_seed::<AuraId>(s), + get_from_seed::<GrandpaId>(s), + ) +} + +impl Alternative { + /// Get an actual chain config from one of the alternatives. + pub(crate) fn load(self) -> Result<ChainSpec, String> { + Ok(match self { + Alternative::Development => ChainSpec::from_genesis( + "Development", + "dev", + || testnet_genesis( + vec![ + get_authority_keys_from_seed("Alice"), + ], + get_account_id_from_seed::<sr25519::Public>("Alice"), + vec![ + get_account_id_from_seed::<sr25519::Public>("Alice"), + get_account_id_from_seed::<sr25519::Public>("Bob"), + get_account_id_from_seed::<sr25519::Public>("Alice//stash"), + get_account_id_from_seed::<sr25519::Public>("Bob//stash"), + ], + true, + ), + vec![], + None, + None, + None, + None + ), + }) + } + + pub(crate) fn from(s: &str) -> Option<Self> { + match s { + "" | "dev" => Some(Alternative::Development), + _ => None, + } + } +} + +fn testnet_genesis(initial_authorities: Vec<(AuraId, GrandpaId)>, + root_key: AccountId, + endowed_accounts: Vec<AccountId>, + _enable_println: bool) -> GenesisConfig { + GenesisConfig { + frame_system: Some(SystemConfig { + code: WASM_BINARY.to_vec(), + changes_trie_config: Default::default(), + }), + pallet_balances: Some(BalancesConfig { + balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(), + }), + pallet_aura: Some(AuraConfig { + authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(), + }), + pallet_grandpa: Some(GrandpaConfig { + authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(), + }), + pallet_sudo: Some(SudoConfig { + key: root_key, + }), + } +} + +pub fn load_spec(id: &str) -> Result<Option<ChainSpec>, String> { + Ok(match Alternative::from(id) { + Some(spec) => Some(spec.load()?), + None => None, + }) +} diff --git a/bridges/bin/node/node/src/cli.rs b/bridges/bin/node/node/src/cli.rs new file mode 100644 index 0000000000000000000000000000000000000000..0091ef7d75912f736c1cbffde84d29e17186038c --- /dev/null +++ b/bridges/bin/node/node/src/cli.rs @@ -0,0 +1,11 @@ +use sc_cli::{RunCmd, Subcommand}; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +pub struct Cli { + #[structopt(subcommand)] + pub subcommand: Option<Subcommand>, + + #[structopt(flatten)] + pub run: RunCmd, +} diff --git a/bridges/bin/node/node/src/command.rs b/bridges/bin/node/node/src/command.rs new file mode 100644 index 0000000000000000000000000000000000000000..0f4c301dbff5b7defe0565bf71e85b21a6418867 --- /dev/null +++ b/bridges/bin/node/node/src/command.rs @@ -0,0 +1,49 @@ +// Copyright 2017-2020 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/>. + +use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair}; +use sc_cli::VersionInfo; +use crate::service; +use crate::chain_spec; +use crate::cli::Cli; + +/// Parse and run command line arguments +pub fn run(version: VersionInfo) -> sc_cli::Result<()> { + let opt = sc_cli::from_args::<Cli>(&version); + + let mut config = sc_service::Configuration::from_version(&version); + + match opt.subcommand { + Some(subcommand) => { + subcommand.init(&version)?; + subcommand.update_config(&mut config, chain_spec::load_spec, &version)?; + subcommand.run( + config, + |config: _| Ok(new_full_start!(config).0), + ) + }, + None => { + opt.run.init(&version)?; + opt.run.update_config(&mut config, chain_spec::load_spec, &version)?; + opt.run.run( + config, + service::new_light, + service::new_full, + &version, + ) + }, + } +} diff --git a/bridges/bin/node/node/src/main.rs b/bridges/bin/node/node/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..7e497cfdfd8fddb6b0b3d6c1342f36eae3237a2f --- /dev/null +++ b/bridges/bin/node/node/src/main.rs @@ -0,0 +1,23 @@ +//! Substrate Node Template CLI library. +#![warn(missing_docs)] + +mod chain_spec; +#[macro_use] +mod service; +mod cli; +mod command; + +fn main() -> sc_cli::Result<()> { + let version = sc_cli::VersionInfo { + name: "Bridge Node", + commit: env!("VERGEN_SHA_SHORT"), + version: env!("CARGO_PKG_VERSION"), + executable_name: "bridge-node", + author: "Parity Technologies", + description: "Bridge Node", + support_url: "https://github.com/paritytech/parity-bridges-common/", + copyright_start_year: 2017, + }; + + command::run(version) +} diff --git a/bridges/bin/node/node/src/service.rs b/bridges/bin/node/node/src/service.rs new file mode 100644 index 0000000000000000000000000000000000000000..9c860e67ca6313d1904221a514dc8ed81ed0d6d7 --- /dev/null +++ b/bridges/bin/node/node/src/service.rs @@ -0,0 +1,225 @@ +//! Service and ServiceFactory implementation. Specialized wrapper over substrate service. + +use std::sync::Arc; +use std::time::Duration; +use sc_client::LongestChain; +use bridge_node_runtime::{self, GenesisConfig, opaque::Block, RuntimeApi}; +use sc_service::{error::{Error as ServiceError}, AbstractService, Configuration, ServiceBuilder}; +use sp_inherents::InherentDataProviders; +use sc_executor::native_executor_instance; +pub use sc_executor::NativeExecutor; +use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair}; +use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider}; + +// Our native executor instance. +native_executor_instance!( + pub Executor, + bridge_node_runtime::api::dispatch, + bridge_node_runtime::native_version, +); + +/// Starts a `ServiceBuilder` for a full service. +/// +/// 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 mut import_setup = None; + let inherent_data_providers = sp_inherents::InherentDataProviders::new(); + + let builder = sc_service::ServiceBuilder::new_full::< + bridge_node_runtime::opaque::Block, bridge_node_runtime::RuntimeApi, crate::service::Executor + >($config)? + .with_select_chain(|_config, backend| { + Ok(sc_client::LongestChain::new(backend.clone())) + })? + .with_transaction_pool(|config, client, _fetcher| { + let pool_api = sc_transaction_pool::FullChainApi::new(client.clone()); + Ok(sc_transaction_pool::BasicPool::new(config, std::sync::Arc::new(pool_api))) + })? + .with_import_queue(|_config, client, mut select_chain, _transaction_pool| { + let select_chain = select_chain.take() + .ok_or_else(|| sc_service::Error::SelectChainRequired)?; + + let (grandpa_block_import, grandpa_link) = + grandpa::block_import(client.clone(), &*client, select_chain)?; + + let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new( + grandpa_block_import.clone(), client.clone(), + ); + + let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair>( + sc_consensus_aura::slot_duration(&*client)?, + aura_block_import, + Some(Box::new(grandpa_block_import.clone())), + None, + client, + inherent_data_providers.clone(), + )?; + + import_setup = Some((grandpa_block_import, grandpa_link)); + + Ok(import_queue) + })?; + + (builder, import_setup, inherent_data_providers) + }} +} + +/// Builds a new service for a full client. +pub fn new_full(config: Configuration<GenesisConfig>) + -> Result<impl AbstractService, ServiceError> +{ + let is_authority = config.roles.is_authority(); + let force_authoring = config.force_authoring; + let name = config.name.clone(); + let disable_grandpa = config.disable_grandpa; + + // sentry nodes announce themselves as authorities to the network + // and should run the same protocols authorities do, but it should + // never actively participate in any consensus process. + let participates_in_consensus = is_authority && !config.sentry_mode; + + let (builder, mut import_setup, inherent_data_providers) = new_full_start!(config); + + let (block_import, grandpa_link) = + import_setup.take() + .expect("Link Half and Block Import are present for Full Services or setup failed before. qed"); + + let service = builder + .with_finality_proof_provider(|client, backend| + Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _) + )? + .build()?; + + if participates_in_consensus { + let proposer = sc_basic_authorship::ProposerFactory { + client: service.client(), + transaction_pool: service.transaction_pool(), + }; + + let client = service.client(); + let select_chain = service.select_chain() + .ok_or(ServiceError::SelectChainRequired)?; + + let can_author_with = + sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()); + + let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>( + sc_consensus_aura::slot_duration(&*client)?, + client, + select_chain, + block_import, + proposer, + service.network(), + inherent_data_providers.clone(), + force_authoring, + service.keystore(), + can_author_with, + )?; + + // the AURA authoring task is considered essential, i.e. if it + // fails we take down the service with it. + service.spawn_essential_task("aura", aura); + } + + // if the node isn't actively participating in consensus then it doesn't + // need a keystore, regardless of which protocol we use below. + let keystore = if participates_in_consensus { + Some(service.keystore()) + } else { + None + }; + + let grandpa_config = grandpa::Config { + // FIXME #1578 make this available through chainspec + gossip_duration: Duration::from_millis(333), + justification_period: 512, + name: Some(name), + observer_enabled: false, + keystore, + is_authority, + }; + + let enable_grandpa = !disable_grandpa; + if enable_grandpa { + // start the full GRANDPA voter + // NOTE: non-authorities could run the GRANDPA observer protocol, but at + // this point the full voter should provide better guarantees of block + // and vote data availability than the observer. The observer has not + // been tested extensively yet and having most nodes in a network run it + // could lead to finality stalls. + let grandpa_config = grandpa::GrandpaParams { + config: grandpa_config, + link: grandpa_link, + network: service.network(), + inherent_data_providers: inherent_data_providers.clone(), + on_exit: service.on_exit(), + telemetry_on_connect: Some(service.telemetry_on_connect_stream()), + voting_rule: grandpa::VotingRulesBuilder::default().build(), + }; + + // the GRANDPA voter task is considered infallible, i.e. + // if it fails we take down the service with it. + service.spawn_essential_task( + "grandpa-voter", + grandpa::run_grandpa_voter(grandpa_config)? + ); + } else { + grandpa::setup_disabled_grandpa( + service.client(), + &inherent_data_providers, + service.network(), + )?; + } + + Ok(service) +} + +/// Builds a new service for a light client. +pub fn new_light(config: Configuration<GenesisConfig>) + -> Result<impl AbstractService, ServiceError> +{ + let inherent_data_providers = InherentDataProviders::new(); + + ServiceBuilder::new_light::<Block, RuntimeApi, Executor>(config)? + .with_select_chain(|_config, backend| { + Ok(LongestChain::new(backend.clone())) + })? + .with_transaction_pool(|config, client, fetcher| { + let fetcher = fetcher + .ok_or_else(|| "Trying to start light transaction pool without active fetcher")?; + + let pool_api = sc_transaction_pool::LightChainApi::new(client.clone(), fetcher.clone()); + let pool = sc_transaction_pool::BasicPool::with_revalidation_type( + config, Arc::new(pool_api), sc_transaction_pool::RevalidationType::Light, + ); + Ok(pool) + })? + .with_import_queue_and_fprb(|_config, client, backend, fetcher, _select_chain, _tx_pool| { + let fetch_checker = fetcher + .map(|fetcher| fetcher.checker().clone()) + .ok_or_else(|| "Trying to start light import queue without active fetch checker")?; + let grandpa_block_import = grandpa::light_block_import( + client.clone(), backend, &*client.clone(), Arc::new(fetch_checker), + )?; + let finality_proof_import = grandpa_block_import.clone(); + let finality_proof_request_builder = + finality_proof_import.create_finality_proof_request_builder(); + + let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair>( + sc_consensus_aura::slot_duration(&*client)?, + grandpa_block_import, + None, + Some(Box::new(finality_proof_import)), + client, + inherent_data_providers.clone(), + )?; + + Ok((import_queue, finality_proof_request_builder)) + })? + .with_finality_proof_provider(|client, backend| + Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _) + )? + .build() +} diff --git a/bridges/bin/node/runtime/Cargo.toml b/bridges/bin/node/runtime/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..4b59c813b56533fa072ece5b56796d19fd72796d --- /dev/null +++ b/bridges/bin/node/runtime/Cargo.toml @@ -0,0 +1,187 @@ +[package] +name = "bridge-node-runtime" +version = "0.1.0" +authors = ["Parity Technologies <admin@parity.io>"] +edition = "2018" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/parity-bridges-common/" + +[dependencies.codec] +package = "parity-scale-codec" +version = "1.0.0" +default-features = false +features = ["derive"] + +[dependencies.serde] +version = "1.0.101" +optional = true +features = ["derive"] + +# Substrate Dependencies +[dependencies.pallet-aura] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.pallet-balances] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.frame-support] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.pallet-grandpa] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.pallet-randomness-collective-flip] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.pallet-sudo] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.frame-system] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.pallet-timestamp] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.pallet-transaction-payment] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.frame-executive] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +# Substrate Primitives +[dependencies.sp-api] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-block-builder] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-consensus-aura] +version = "0.8.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-core] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-inherents] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-io] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-offchain] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-runtime] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-session] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-std] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-transaction-pool] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[dependencies.sp-version] +version = "2.0.0-alpha.2" +default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[build-dependencies.wasm-builder-runner] +version = "1.0.5" +package = "substrate-wasm-builder-runner" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" + +[features] +default = ["std"] +std = [ + "pallet-aura/std", + "pallet-balances/std", + "codec/std", + "frame-executive/std", + "frame-support/std", + "frame-system/std", + "pallet-grandpa/std", + "pallet-randomness-collective-flip/std", + "serde", + "sp-api/std", + "sp-block-builder/std", + "sp-consensus-aura/std", + "sp-core/std", + "sp-inherents/std", + "sp-io/std", + "sp-offchain/std", + "sp-runtime/std", + "sp-session/std", + "sp-std/std", + "sp-transaction-pool/std", + "sp-version/std", + "pallet-sudo/std", + "pallet-timestamp/std", + "pallet-transaction-payment/std", +] diff --git a/bridges/bin/node/runtime/build.rs b/bridges/bin/node/runtime/build.rs new file mode 100644 index 0000000000000000000000000000000000000000..39f7f56feb0b1730d7f2c9a7daa898fe54bfd052 --- /dev/null +++ b/bridges/bin/node/runtime/build.rs @@ -0,0 +1,10 @@ +use wasm_builder_runner::WasmBuilder; + +fn main() { + WasmBuilder::new() + .with_current_project() + .with_wasm_builder_from_crates("1.0.9") + .export_heap_base() + .import_memory() + .build() +} diff --git a/bridges/bin/node/runtime/src/lib.rs b/bridges/bin/node/runtime/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..6036697b47ec57ba4630da6aadf26ac734777075 --- /dev/null +++ b/bridges/bin/node/runtime/src/lib.rs @@ -0,0 +1,353 @@ +//! The Substrate Node Template runtime. This can be compiled with `#[no_std]`, ready for Wasm. + +#![cfg_attr(not(feature = "std"), no_std)] +// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256. +#![recursion_limit="256"] + +// Make the WASM binary available. +#[cfg(feature = "std")] +include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); + +use sp_std::prelude::*; +use sp_core::OpaqueMetadata; +use sp_runtime::{ + ApplyExtrinsicResult, transaction_validity::TransactionValidity, generic, create_runtime_str, + impl_opaque_keys, MultiSignature, +}; +use sp_runtime::traits::{ + BlakeTwo256, Block as BlockT, IdentityLookup, Verify, ConvertInto, IdentifyAccount +}; +use sp_api::impl_runtime_apis; +use sp_consensus_aura::sr25519::AuthorityId as AuraId; +use pallet_grandpa::AuthorityList as GrandpaAuthorityList; +use pallet_grandpa::fg_primitives; +use sp_version::RuntimeVersion; +#[cfg(feature = "std")] +use sp_version::NativeVersion; + +// A few exports that help ease life for downstream crates. +#[cfg(any(feature = "std", test))] +pub use sp_runtime::BuildStorage; +pub use pallet_timestamp::Call as TimestampCall; +pub use pallet_balances::Call as BalancesCall; +pub use sp_runtime::{Permill, Perbill}; +pub use frame_support::{ + StorageValue, construct_runtime, parameter_types, + traits::Randomness, + weights::Weight, +}; + +/// An index to a block. +pub type BlockNumber = u32; + +/// Alias to 512-bit hash when used in the context of a transaction signature on the chain. +pub type Signature = MultiSignature; + +/// Some way of identifying an account on the chain. We intentionally make it equivalent +/// to the public key of our transaction signing scheme. +pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId; + +/// The type for looking up accounts. We don't expect more than 4 billion of them, but you +/// never know... +pub type AccountIndex = u32; + +/// Balance of an account. +pub type Balance = u128; + +/// Index of a transaction in the chain. +pub type Index = u32; + +/// A hash of some data used by the chain. +pub type Hash = sp_core::H256; + +/// Digest item type. +pub type DigestItem = generic::DigestItem<Hash>; + +/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know +/// the specifics of the runtime. They can then be made to be agnostic over specific formats +/// of data like extrinsics, allowing for them to continue syncing the network through upgrades +/// to even the core data structures. +pub mod opaque { + use super::*; + + pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic; + + /// Opaque block header type. + pub type Header = generic::Header<BlockNumber, BlakeTwo256>; + /// Opaque block type. + pub type Block = generic::Block<Header, UncheckedExtrinsic>; + /// Opaque block identifier type. + pub type BlockId = generic::BlockId<Block>; + + impl_opaque_keys! { + pub struct SessionKeys { + pub aura: Aura, + pub grandpa: Grandpa, + } + } +} + +/// This runtime version. +pub const VERSION: RuntimeVersion = RuntimeVersion { + spec_name: create_runtime_str!("bridge-node"), + impl_name: create_runtime_str!("bridge-node"), + authoring_version: 1, + spec_version: 1, + impl_version: 1, + apis: RUNTIME_API_VERSIONS, +}; + +pub const MILLISECS_PER_BLOCK: u64 = 6000; + +pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK; + +// These time units are defined in number of blocks. +pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber); +pub const HOURS: BlockNumber = MINUTES * 60; +pub const DAYS: BlockNumber = HOURS * 24; + +/// The version information used to identify this runtime when compiled natively. +#[cfg(feature = "std")] +pub fn native_version() -> NativeVersion { + NativeVersion { + runtime_version: VERSION, + can_author_with: Default::default(), + } +} + +parameter_types! { + pub const BlockHashCount: BlockNumber = 250; + pub const MaximumBlockWeight: Weight = 1_000_000; + pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75); + pub const MaximumBlockLength: u32 = 5 * 1024 * 1024; + pub const Version: RuntimeVersion = VERSION; +} + +impl frame_system::Trait for Runtime { + /// The identifier used to distinguish between accounts. + type AccountId = AccountId; + /// The aggregated dispatch type that is available for extrinsics. + type Call = Call; + /// The lookup mechanism to get account ID from whatever is passed in dispatchers. + type Lookup = IdentityLookup<AccountId>; + /// The index type for storing how many extrinsics an account has signed. + type Index = Index; + /// The index type for blocks. + type BlockNumber = BlockNumber; + /// The type for hashing blocks and tries. + type Hash = Hash; + /// The hashing algorithm used. + type Hashing = BlakeTwo256; + /// The header type. + type Header = generic::Header<BlockNumber, BlakeTwo256>; + /// The ubiquitous event type. + type Event = Event; + /// The ubiquitous origin type. + type Origin = Origin; + /// Maximum number of block number to block hash mappings to keep (oldest pruned first). + type BlockHashCount = BlockHashCount; + /// Maximum weight of each block. + type MaximumBlockWeight = MaximumBlockWeight; + /// Maximum size of all encoded transactions (in bytes) that are allowed in one block. + type MaximumBlockLength = MaximumBlockLength; + /// Portion of the block weight that is available to all normal transactions. + type AvailableBlockRatio = AvailableBlockRatio; + /// Version of the runtime. + type Version = Version; + /// Converts a module to the index of the module in `construct_runtime!`. + /// + /// This type is being generated by `construct_runtime!`. + type ModuleToIndex = ModuleToIndex; + /// What to do if a new account is created. + type OnNewAccount = (); + /// What to do if an account is fully reaped from the system. + type OnKilledAccount = (); + /// The data to be stored in an account. + type AccountData = pallet_balances::AccountData<Balance>; +} + +impl pallet_aura::Trait for Runtime { + type AuthorityId = AuraId; +} + +impl pallet_grandpa::Trait for Runtime { + type Event = Event; +} + +parameter_types! { + pub const MinimumPeriod: u64 = SLOT_DURATION / 2; +} + +impl pallet_timestamp::Trait for Runtime { + /// A timestamp: milliseconds since the unix epoch. + type Moment = u64; + type OnTimestampSet = Aura; + type MinimumPeriod = MinimumPeriod; +} + +parameter_types! { + pub const ExistentialDeposit: u128 = 500; +} + +impl pallet_balances::Trait for Runtime { + /// The type for recording an account's balance. + type Balance = Balance; + /// The ubiquitous event type. + type Event = Event; + type DustRemoval = (); + type ExistentialDeposit = ExistentialDeposit; + type AccountStore = System; +} + +parameter_types! { + pub const TransactionBaseFee: Balance = 0; + pub const TransactionByteFee: Balance = 1; +} + +impl pallet_transaction_payment::Trait for Runtime { + type Currency = pallet_balances::Module<Runtime>; + type OnTransactionPayment = (); + type TransactionBaseFee = TransactionBaseFee; + type TransactionByteFee = TransactionByteFee; + type WeightToFee = ConvertInto; + type FeeMultiplierUpdate = (); +} + +impl pallet_sudo::Trait for Runtime { + type Event = Event; + type Call = Call; +} + +construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = opaque::Block, + UncheckedExtrinsic = UncheckedExtrinsic + { + System: frame_system::{Module, Call, Config, Storage, Event<T>}, + RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage}, + Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, + Aura: pallet_aura::{Module, Config<T>, Inherent(Timestamp)}, + Grandpa: pallet_grandpa::{Module, Call, Storage, Config, Event}, + Balances: pallet_balances::{Module, Call, Storage, Config<T>, Event<T>}, + TransactionPayment: pallet_transaction_payment::{Module, Storage}, + Sudo: pallet_sudo::{Module, Call, Config<T>, Storage, Event<T>}, + } +); + +/// The address format for describing accounts. +pub type Address = AccountId; +/// Block header type as expected by this runtime. +pub type Header = generic::Header<BlockNumber, BlakeTwo256>; +/// Block type as expected by this runtime. +pub type Block = generic::Block<Header, UncheckedExtrinsic>; +/// A Block signed with a Justification +pub type SignedBlock = generic::SignedBlock<Block>; +/// BlockId type as expected by this runtime. +pub type BlockId = generic::BlockId<Block>; +/// The SignedExtension to the basic transaction logic. +pub type SignedExtra = ( + frame_system::CheckVersion<Runtime>, + frame_system::CheckGenesis<Runtime>, + frame_system::CheckEra<Runtime>, + frame_system::CheckNonce<Runtime>, + frame_system::CheckWeight<Runtime>, + pallet_transaction_payment::ChargeTransactionPayment<Runtime> +); +/// Unchecked extrinsic type as expected by this runtime. +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>; +/// Extrinsic type that has already been checked. +pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExtra>; +/// Executive: handles dispatch to the various modules. +pub type Executive = frame_executive::Executive<Runtime, Block, frame_system::ChainContext<Runtime>, Runtime, AllModules>; + +impl_runtime_apis! { + impl sp_api::Core<Block> for Runtime { + fn version() -> RuntimeVersion { + VERSION + } + + fn execute_block(block: Block) { + Executive::execute_block(block) + } + + fn initialize_block(header: &<Block as BlockT>::Header) { + Executive::initialize_block(header) + } + } + + impl sp_api::Metadata<Block> for Runtime { + fn metadata() -> OpaqueMetadata { + Runtime::metadata().into() + } + } + + impl sp_block_builder::BlockBuilder<Block> for Runtime { + fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult { + Executive::apply_extrinsic(extrinsic) + } + + fn apply_trusted_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult { + Executive::apply_trusted_extrinsic(extrinsic) + } + + fn finalize_block() -> <Block as BlockT>::Header { + Executive::finalize_block() + } + + fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> { + data.create_extrinsics() + } + + fn check_inherents( + block: Block, + data: sp_inherents::InherentData, + ) -> sp_inherents::CheckInherentsResult { + data.check_extrinsics(&block) + } + + fn random_seed() -> <Block as BlockT>::Hash { + RandomnessCollectiveFlip::random_seed() + } + } + + impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime { + fn validate_transaction(tx: <Block as BlockT>::Extrinsic) -> TransactionValidity { + Executive::validate_transaction(tx) + } + } + + impl sp_offchain::OffchainWorkerApi<Block> for Runtime { + fn offchain_worker(header: &<Block as BlockT>::Header) { + Executive::offchain_worker(header) + } + } + + impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime { + fn slot_duration() -> u64 { + Aura::slot_duration() + } + + fn authorities() -> Vec<AuraId> { + Aura::authorities() + } + } + + impl sp_session::SessionKeys<Block> for Runtime { + fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> { + opaque::SessionKeys::generate(seed) + } + + fn decode_session_keys( + encoded: Vec<u8>, + ) -> Option<Vec<(Vec<u8>, sp_core::crypto::KeyTypeId)>> { + opaque::SessionKeys::decode_into_raw_public_keys(&encoded) + } + } + + impl fg_primitives::GrandpaApi<Block> for Runtime { + fn grandpa_authorities() -> GrandpaAuthorityList { + Grandpa::grandpa_authorities() + } + } +} diff --git a/bridges/bin/node/scripts/init.sh b/bridges/bin/node/scripts/init.sh new file mode 100755 index 0000000000000000000000000000000000000000..1405a41ef333e6af863080d83f854d3edb5fb4fa --- /dev/null +++ b/bridges/bin/node/scripts/init.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -e + +echo "*** Initializing WASM build environment" + +if [ -z $CI_PROJECT_NAME ] ; then + rustup update nightly + rustup update stable +fi + +rustup target add wasm32-unknown-unknown --toolchain nightly diff --git a/bridges/modules/ethereum/Cargo.toml b/bridges/modules/ethereum/Cargo.toml index ee4673e5894811b55beeed71432a8770c293c31b..2a750ffbf5f6a1724d21341bb64fad90b1d51f09 100644 --- a/bridges/modules/ethereum/Cargo.toml +++ b/bridges/modules/ethereum/Cargo.toml @@ -12,29 +12,34 @@ primitives = { package = "sp-bridge-eth-poa", path = "../../primitives/ethereum- # Substrate Based Dependencies [dependencies.frame-support] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dependencies.frame-system] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dependencies.sp-std] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dependencies.sp-io] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dependencies.sp-runtime] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" # Dev Dependencies [dev-dependencies] diff --git a/bridges/modules/substrate/Cargo.toml b/bridges/modules/substrate/Cargo.toml index cd8787a76683e388b256044259e0c6bb7e298212..069fed67832094901e96b36b227b9e7a5fca5151 100644 --- a/bridges/modules/substrate/Cargo.toml +++ b/bridges/modules/substrate/Cargo.toml @@ -9,56 +9,63 @@ edition = "2018" [dependencies] codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false } serde = { version = "1.0", optional = true } +hash-db = { version = "0.15.2", default-features = false } # Substrate Based Dependencies [dependencies.frame-support] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dependencies.frame-system] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" -[dependencies.hash-db] -version = "0.15.2" -default-features = false [dependencies.pallet-session] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dependencies.sp-core] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dependencies.sp-finality-grandpa] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dependencies.sp-runtime] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dependencies.sp-trie] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" # Dev Dependencies [dev-dependencies.sp-io] -version = "2.0.0" -git = "https://github.com/paritytech/substrate.git" +version = "2.0.0-alpha.2" default-features = false +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [dev-dependencies.sp-state-machine] -version = "0.8.0" -git = "https://github.com/paritytech/substrate.git" +version = "0.8.0-alpha.2" +rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" +git = "https://github.com/paritytech/substrate/" [features] default = ["std"] diff --git a/bridges/modules/substrate/src/lib.rs b/bridges/modules/substrate/src/lib.rs index ad27627f62cae3c842841217bd0752eef55de8f7..bcb7f7081bceb58babb9ddf97e710a34f6672e4f 100644 --- a/bridges/modules/substrate/src/lib.rs +++ b/bridges/modules/substrate/src/lib.rs @@ -249,7 +249,7 @@ mod tests { type ModuleToIndex = (); type AccountData = (); type OnNewAccount = (); - type OnReapAccount = (); + type OnKilledAccount = (); } impl Trait for Test {} diff --git a/bridges/primitives/ethereum-poa/Cargo.toml b/bridges/primitives/ethereum-poa/Cargo.toml index 6b88c8901bfeac99e4e66440cb3d1e542f51f177..02d144ccb38efbc7b2d1bacd58cdbc7d81f99271 100644 --- a/bridges/primitives/ethereum-poa/Cargo.toml +++ b/bridges/primitives/ethereum-poa/Cargo.toml @@ -16,10 +16,12 @@ impl-rlp = { version = "0.2", default-features = false } impl-serde = { version = "0.2.3", optional = true } codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false } rlp = { version = "0.4", default-features = false } -sp-std = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", default-features = false } -sp-runtime = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", default-features = false } -sp-api = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", default-features = false } -sp-io = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", default-features = false } + +sp-std = { version = "2.0.0-alpha.2", git = "https://github.com/paritytech/substrate.git", default-features = false, rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" } +sp-runtime = { version = "2.0.0-alpha.2", git = "https://github.com/paritytech/substrate.git", default-features = false, rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" } +sp-api = { version = "2.0.0-alpha.2", git = "https://github.com/paritytech/substrate.git", default-features = false, rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" } +sp-io = { version = "2.0.0-alpha.2", git = "https://github.com/paritytech/substrate.git", default-features = false, rev = "2afecf81ee19b8a6edb364b419190ea47c4a4a31" } + hash-db = { version = "0.15.2", default-features = false } triehash = { version = "0.8.2", default-features = false } plain_hasher = { version = "0.2.2", default-features = false }