test-utils: add chain-spec-builder cli (#1061)

* test-utils: add chain-spec-builder cli

* style changes, mostly indentation

* fix padding

* add issue to todo

* more style fixes

* share seed padding with keystore

* fix master rebase error
This commit is contained in:
azban
2018-11-12 11:21:03 -08:00
committed by Gav Wood
parent 168de867f5
commit 367c99b2bb
11 changed files with 4116 additions and 56 deletions
+1
View File
@@ -1729,6 +1729,7 @@ dependencies = [
"substrate-cli 0.3.0",
"substrate-client 0.1.0",
"substrate-consensus-aura 0.1.0",
"substrate-keystore 0.1.0",
"substrate-network 0.1.0",
"substrate-primitives 0.1.0",
"substrate-service 0.3.0",
+1
View File
@@ -76,6 +76,7 @@ exclude = [
"core/executor/wasm",
"pwasm-alloc",
"core/test-runtime/wasm",
"test-utils/chain-spec-builder"
]
[badges]
+22 -17
View File
@@ -131,6 +131,24 @@ pub struct Store {
additional: HashMap<Public, Seed>,
}
pub fn pad_seed(seed: &str) -> Seed {
let mut s: [u8; 32] = [' ' as u8; 32];
let was_hex = if seed.len() == 66 && &seed[0..2] == "0x" {
if let Ok(d) = hex::decode(&seed[2..]) {
s.copy_from_slice(&d);
true
} else { false }
} else { false };
if !was_hex {
let len = ::std::cmp::min(32, seed.len());
&mut s[..len].copy_from_slice(&seed.as_bytes()[..len]);
}
s
}
impl Store {
/// Create a new store at the given path.
pub fn open(path: PathBuf) -> Result<Self> {
@@ -153,24 +171,11 @@ impl Store {
/// Create a new key from seed. Do not place it into the store.
/// Only the first 32 bytes of the sead are used. This is meant to be used for testing only.
// TODO: Remove this
// FIXME: remove this - https://github.com/paritytech/substrate/issues/1063
pub fn generate_from_seed(&mut self, seed: &str) -> Result<Pair> {
let mut s: [u8; 32] = [' ' as u8; 32];
let was_hex = if seed.len() == 66 && &seed[0..2] == "0x" {
if let Ok(d) = hex::decode(&seed[2..]) {
s.copy_from_slice(&d);
true
} else { false }
} else { false };
if !was_hex {
let len = ::std::cmp::min(32, seed.len());
&mut s[..len].copy_from_slice(&seed.as_bytes()[..len]);
}
let pair = Pair::from_seed(&s);
self.additional.insert(pair.public(), s);
let padded_seed = pad_seed(seed);
let pair = Pair::from_seed(&padded_seed);
self.additional.insert(pair.public(), padded_seed);
Ok(pair)
}
+3 -1
View File
@@ -139,10 +139,12 @@ impl<Components> Service<Components>
let executor = NativeExecutor::new();
let mut keystore = Keystore::open(config.keystore_path.as_str().into())?;
// This is meant to be for testing only
// FIXME: remove this - https://github.com/paritytech/substrate/issues/1063
for seed in &config.keys {
keystore.generate_from_seed(seed)?;
}
// Keep the public key for telemetry
let public_key = match keystore.contents()?.get(0) {
Some(public_key) => public_key.clone(),
+1
View File
@@ -24,6 +24,7 @@ substrate-network = { path = "../../core/network" }
substrate-consensus-aura = { path = "../../core/consensus/aura" }
sr-primitives = { path = "../../core/sr-primitives" }
node-executor = { path = "../executor" }
substrate-keystore = { path = "../../core/keystore" }
[dev-dependencies]
substrate-service-test = { path = "../../core/service/test" }
+58 -37
View File
@@ -18,16 +18,20 @@
use primitives::{AuthorityId, ed25519};
use node_primitives::AccountId;
use node_runtime::{GenesisConfig, ConsensusConfig, CouncilSeatsConfig, CouncilVotingConfig, DemocracyConfig,
SessionConfig, StakingConfig, TimestampConfig, BalancesConfig, TreasuryConfig, UpgradeKeyConfig,
ContractConfig, Permill, Perbill};
use node_runtime::{ConsensusConfig, CouncilSeatsConfig, CouncilVotingConfig, DemocracyConfig,
SessionConfig, StakingConfig, TimestampConfig, BalancesConfig, TreasuryConfig,
UpgradeKeyConfig, ContractConfig, Permill, Perbill};
pub use node_runtime::GenesisConfig;
use substrate_service;
use substrate_keystore::pad_seed;
const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";
/// Specialised `ChainSpec`.
pub type ChainSpec = substrate_service::ChainSpec<GenesisConfig>;
/// BBQ birch testnet generator
pub fn bbq_birch_config() -> Result<ChainSpec, String> {
ChainSpec::from_embedded(include_bytes!("../res/bbq-birch.json"))
}
@@ -43,7 +47,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
hex!["f295940fa750df68a686fcf4abd4111c8a9c5a5a5a83c4c8639c451a94a7adfd"].into(),
];
const MILLICENTS: u128 = 1_000_000_000;
const CENTS: u128 = 1_000 * MILLICENTS; // assume this is worth about a cent.
const CENTS: u128 = 1_000 * MILLICENTS; // assume this is worth about a cent.
const DOLLARS: u128 = 100 * CENTS;
const SECS_PER_BLOCK: u64 = 4;
@@ -53,12 +57,12 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
GenesisConfig {
consensus: Some(ConsensusConfig {
code: include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm").to_vec(), // TODO change
code: include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm").to_vec(), // TODO change
authorities: initial_authorities.clone(),
}),
system: None,
balances: Some(BalancesConfig {
balances: endowed_accounts.iter().map(|&k|(k, 10_000_000 * DOLLARS)).collect(),
balances: endowed_accounts.iter().map(|&k| (k, 10_000_000 * DOLLARS)).collect(),
transaction_base_fee: 1 * CENTS,
transaction_byte_fee: 10 * MILLICENTS,
existential_deposit: 1 * DOLLARS,
@@ -68,7 +72,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
}),
session: Some(SessionConfig {
validators: initial_authorities.iter().cloned().map(Into::into).collect(),
session_length: 5 * MINUTES
session_length: 5 * MINUTES,
}),
staking: Some(StakingConfig {
current_era: 0,
@@ -84,9 +88,9 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
minimum_validator_count: 4,
}),
democracy: Some(DemocracyConfig {
launch_period: 5 * MINUTES, // 1 day per public referendum
voting_period: 5 * MINUTES, // 3 days to discuss & vote on an active referendum
minimum_deposit: 50 * DOLLARS, // 12000 as the minimum deposit for a referendum
launch_period: 5 * MINUTES, // 1 day per public referendum
voting_period: 5 * MINUTES, // 3 days to discuss & vote on an active referendum
minimum_deposit: 50 * DOLLARS, // 12000 as the minimum deposit for a referendum
}),
council_seats: Some(CouncilSeatsConfig {
active_council: vec![],
@@ -98,8 +102,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
approval_voting_period: 2 * DAYS,
term_duration: 28 * DAYS,
desired_seats: 0,
inactive_grace_period: 1, // one additional vote should go by before an inactive voter can be reaped.
inactive_grace_period: 1, // one additional vote should go by before an inactive voter can be reaped.
}),
council_voting: Some(CouncilVotingConfig {
cooloff_period: 4 * DAYS,
@@ -130,8 +133,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
/// Staging testnet config.
pub fn staging_testnet_config() -> ChainSpec {
let boot_nodes = vec![
];
let boot_nodes = vec![];
ChainSpec::from_genesis(
"Staging Testnet",
"staging_testnet",
@@ -144,15 +146,30 @@ pub fn staging_testnet_config() -> ChainSpec {
)
}
fn testnet_genesis(initial_authorities: Vec<AuthorityId>, upgrade_key: AccountId) -> GenesisConfig {
let endowed_accounts = vec![
ed25519::Pair::from_seed(b"Alice ").public().0.into(),
ed25519::Pair::from_seed(b"Bob ").public().0.into(),
ed25519::Pair::from_seed(b"Charlie ").public().0.into(),
ed25519::Pair::from_seed(b"Dave ").public().0.into(),
ed25519::Pair::from_seed(b"Eve ").public().0.into(),
ed25519::Pair::from_seed(b"Ferdie ").public().0.into(),
];
/// Helper function to generate AuthorityID from seed
pub fn get_authority_id_from_seed(seed: &str) -> AuthorityId {
let padded_seed = pad_seed(seed);
// NOTE from ed25519 impl:
// prefer pkcs#8 unless security doesn't matter -- this is used primarily for tests.
ed25519::Pair::from_seed(&padded_seed).public().0.into()
}
/// Helper function to create GenesisConfig for testing
pub fn testnet_genesis(
initial_authorities: Vec<AuthorityId>,
upgrade_key: AccountId,
endowed_accounts: Option<Vec<AuthorityId>>,
) -> GenesisConfig {
let endowed_accounts = endowed_accounts.unwrap_or_else(|| {
vec![
get_authority_id_from_seed("Alice"),
get_authority_id_from_seed("Bob"),
get_authority_id_from_seed("Charlie"),
get_authority_id_from_seed("Dave"),
get_authority_id_from_seed("Eve"),
get_authority_id_from_seed("Ferdie"),
]
});
GenesisConfig {
consensus: Some(ConsensusConfig {
code: include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm").to_vec(),
@@ -166,7 +183,7 @@ fn testnet_genesis(initial_authorities: Vec<AuthorityId>, upgrade_key: AccountId
transfer_fee: 0,
creation_fee: 0,
reclaim_rebate: 0,
balances: endowed_accounts.iter().map(|&k|(k, (1 << 60))).collect(),
balances: endowed_accounts.iter().map(|&k| (k.into(), (1 << 60))).collect(),
}),
session: Some(SessionConfig {
validators: initial_authorities.iter().cloned().map(Into::into).collect(),
@@ -192,8 +209,8 @@ fn testnet_genesis(initial_authorities: Vec<AuthorityId>, upgrade_key: AccountId
}),
council_seats: Some(CouncilSeatsConfig {
active_council: endowed_accounts.iter()
.filter(|a| initial_authorities.iter().find(|&b| a.as_bytes() == b.0).is_none())
.map(|a| (a.clone(), 1000000)).collect(),
.filter(|a| initial_authorities.iter().find(|&b| a.0 == b.0).is_none())
.map(|a| (a.clone().into(), 1000000)).collect(),
candidacy_bond: 10,
voter_bond: 2,
present_slash_per_voter: 1,
@@ -209,7 +226,7 @@ fn testnet_genesis(initial_authorities: Vec<AuthorityId>, upgrade_key: AccountId
voting_period: 20,
}),
timestamp: Some(TimestampConfig {
period: 5, // 5 second block time.
period: 5, // 5 second block time.
}),
treasury: Some(TreasuryConfig {
proposal_bond: Permill::from_percent(5),
@@ -232,10 +249,12 @@ fn testnet_genesis(initial_authorities: Vec<AuthorityId>, upgrade_key: AccountId
}
fn development_config_genesis() -> GenesisConfig {
testnet_genesis(vec![
ed25519::Pair::from_seed(b"Alice ").public().into(),
],
ed25519::Pair::from_seed(b"Alice ").public().0.into()
testnet_genesis(
vec![
get_authority_id_from_seed("Alice"),
],
get_authority_id_from_seed("Alice").into(),
None,
)
}
@@ -245,11 +264,13 @@ pub fn development_config() -> ChainSpec {
}
fn local_testnet_genesis() -> GenesisConfig {
testnet_genesis(vec![
ed25519::Pair::from_seed(b"Alice ").public().into(),
ed25519::Pair::from_seed(b"Bob ").public().into(),
],
ed25519::Pair::from_seed(b"Alice ").public().0.into()
testnet_genesis(
vec![
get_authority_id_from_seed("Alice"),
get_authority_id_from_seed("Bob"),
],
get_authority_id_from_seed("Alice").into(),
None,
)
}
@@ -276,7 +297,7 @@ mod tests {
}
#[test]
fn test_connectivity() {
fn test_connectiviy() {
service_test::connectivity::<Factory>(integration_test_config());
}
}
+2 -1
View File
@@ -37,12 +37,13 @@ extern crate node_primitives;
#[macro_use]
extern crate substrate_service;
extern crate node_executor;
extern crate substrate_keystore;
#[macro_use]
extern crate log;
pub use cli::error;
mod chain_spec;
pub mod chain_spec;
mod service;
use tokio::runtime::Runtime;
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,10 @@
[package]
name = "chain-spec-builder"
version = "0.1.0"
authors = ["haydn dufrene <haydn.dufrene@gmail.com>"]
[dependencies]
clap = { version = "~2.32", features = ["yaml"] }
node-cli = { path = "../../node/cli" }
substrate-primitives = { path = "../../core/primitives" }
substrate-service = { path = "../../core/service" }
@@ -0,0 +1,24 @@
name: chain-spec-builder
author: "azban <me@azban.net>"
about: Utility for creating chain specs primarily for testing
args:
- initial_authority_seed:
short: a
value_name: INITIAL_AUTHORITY_SEED
help: Initial authority seed
takes_value: true
multiple: true
required: true
- endowed_account_seed:
short: e
value_name: ENDOWED_ACCOUNT_SEED
help: Endowed account seed
takes_value: true
multiple: true
required: true
- upgrade_key_seed:
short: u
value_name: UPGRADE_KEY_SEED
help: Upgrade key seed
takes_value: true
required: true
@@ -0,0 +1,51 @@
#[macro_use]
extern crate clap;
use clap::App;
extern crate node_cli;
extern crate substrate_service;
extern crate substrate_primitives;
use node_cli::chain_spec;
use substrate_service::chain_ops::build_spec;
fn genesis_constructor() -> chain_spec::GenesisConfig {
let yaml = load_yaml!("./cli.yml");
let matches = App::from_yaml(yaml).get_matches();
let authorities = matches.values_of("initial_authority_seed")
.unwrap()
.map(chain_spec::get_authority_id_from_seed)
.collect();
let endowed_accounts = matches.values_of("endowed_account_seed")
.unwrap()
.map(chain_spec::get_authority_id_from_seed)
.collect();
let upgrade_key_seed = matches.value_of("upgrade_key_seed").unwrap();
let upgrade_key = chain_spec::get_authority_id_from_seed(upgrade_key_seed);
chain_spec::testnet_genesis(
authorities,
upgrade_key.into(),
Some(endowed_accounts),
)
}
fn generate_chain_spec() -> String {
let chain_spec = chain_spec::ChainSpec::from_genesis(
"Custom",
"custom",
genesis_constructor,
vec![],
None,
None,
None,
);
build_spec(chain_spec, false).unwrap()
}
fn main() {
let json = generate_chain_spec();
println!("{}", json);
}