Skip to content

Commit

Permalink
Add a Node (#22)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
HCastano authored and bkchr committed Apr 10, 2024
1 parent ebbc472 commit 40b4f78
Show file tree
Hide file tree
Showing 15 changed files with 1,150 additions and 36 deletions.
108 changes: 108 additions & 0 deletions bridges/bin/node/node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions bridges/bin/node/node/build.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 113 additions & 0 deletions bridges/bin/node/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions bridges/bin/node/node/src/cli.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 49 additions & 0 deletions bridges/bin/node/node/src/command.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions bridges/bin/node/node/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 40b4f78

Please sign in to comment.