Add a Node (#22)

* Copy node-template over from Substrate repo

Got the template at rev=6e6d06c33911

* Use dependencies from crates.io + stop renaming on import

* Remove template pallet

* Stop using crates.io dependencies

Instead they're going to be pinned at v2.0.0-alpha.2
at commit `2afecf81ee19b8a6edb364b419190ea47c4a4a31`
until something stable comes along.

* Remove LICENSE

* Change references of `node-template` to `bridge-node`

* Remove README

* Fix some missed node-template references

* Add WASM toolchain to CI

* Be more specific about nightly version to use

* Maybe don't tie to a specific nightly

* Use composite accounts

* Update to use lazy reaping

* Only use Development chain config
This commit is contained in:
Hernando Castano
2020-03-04 04:44:21 -05:00
committed by Bastian Köcher
parent ebbc4724d0
commit 40b4f78bd8
15 changed files with 1150 additions and 36 deletions
+108
View File
@@ -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"
+9
View File
@@ -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();
}
+113
View File
@@ -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,
})
}
+11
View File
@@ -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,
}
+49
View File
@@ -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,
)
},
}
}
+23
View File
@@ -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)
}
+225
View File
@@ -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()
}