Many renames.

- Everything polkadot becomes polkadot-.
- Wasm (substrate) executor tests split from Wasm (Polkadot) runtime and
built independently.
This commit is contained in:
Gav
2018-02-07 23:36:34 +01:00
parent 3d0a44c8a9
commit 5c842f77bc
453 changed files with 3230 additions and 90 deletions
+24
View File
@@ -0,0 +1,24 @@
[package]
name = "polkadot-cli"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Polkadot node implementation in Rust."
[dependencies]
clap = { version = "2.27", features = ["yaml"] }
env_logger = "0.4"
error-chain = "0.11"
log = "0.3"
hex-literal = "0.1"
ed25519 = { path = "../ed25519" }
triehash = { version = "0.1" }
substrate-client = { path = "../client" }
substrate-codec = { path = "../codec" }
substrate-runtime-io = { path = "../runtime-io" }
substrate-state-machine = { path = "../state-machine" }
substrate-executor = { path = "../executor" }
substrate-primitives = { path = "../primitives" }
substrate-rpc-servers = { path = "../rpc-servers" }
polkadot-primitives = { path = "../polkadot-primitives" }
polkadot-executor = { path = "../polkadot-executor" }
polkadot-runtime = { path = "../polkadot-runtime" }
+14
View File
@@ -0,0 +1,14 @@
name: polkadot
author: "Parity Team <admin@polkadot.io>"
about: Polkadot Node Rust Implementation
args:
- log:
short: l
value_name: LOG_PATTERN
help: Sets a custom logging
takes_value: true
subcommands:
- collator:
about: Run collator node
- validator:
about: Run validator node
+29
View File
@@ -0,0 +1,29 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Initialization errors.
use client;
error_chain! {
foreign_links {
Io(::std::io::Error) #[doc="IO error"];
Cli(::clap::Error) #[doc="CLI error"];
}
links {
Client(client::error::Error, client::error::ErrorKind) #[doc="Client error"];
}
}
+142
View File
@@ -0,0 +1,142 @@
// Copyright 2017 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/>.
//! Tool for creating the genesis block.
use std::collections::HashMap;
use polkadot_primitives::{Block, Header};
use triehash::trie_root;
/// Create a genesis block, given the initial storage.
pub fn construct_genesis_block(storage: &HashMap<Vec<u8>, Vec<u8>>) -> Block {
let state_root = trie_root(storage.clone().into_iter()).0.into();
let header = Header {
parent_hash: Default::default(),
number: 0,
state_root,
transaction_root: trie_root(vec![].into_iter()).0.into(),
digest: Default::default(),
};
Block {
header,
transactions: vec![],
}
}
#[cfg(test)]
mod tests {
use super::*;
use codec::{Slicable, Joiner};
use polkadot_runtime::support::{one, two, Hashable};
use polkadot_runtime::genesismap::{GenesisConfig, additional_storage_with_genesis};
use state_machine::execute;
use state_machine::OverlayedChanges;
use state_machine::backend::InMemory;
use polkadot_executor::executor;
use polkadot_primitives::{AccountId, Hash, BlockNumber, Header, Digest, UncheckedTransaction,
Transaction, Function};
use ed25519::Pair;
fn secret_for(who: &AccountId) -> Option<Pair> {
match who {
x if *x == one() => Some(Pair::from_seed(b"12345678901234567890123456789012")),
x if *x == two() => Some("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60".into()),
_ => None,
}
}
fn construct_block(backend: &InMemory, number: BlockNumber, parent_hash: Hash, state_root: Hash, txs: Vec<Transaction>) -> (Vec<u8>, Hash) {
use triehash::ordered_trie_root;
let transactions = txs.into_iter().map(|transaction| {
let signature = secret_for(&transaction.signed).unwrap()
.sign(&transaction.to_vec());
UncheckedTransaction { transaction, signature }
}).collect::<Vec<_>>();
let transaction_root = ordered_trie_root(transactions.iter().map(Slicable::to_vec)).0.into();
let mut header = Header {
parent_hash,
number,
state_root,
transaction_root,
digest: Digest { logs: vec![], },
};
let hash = header.blake2_256();
let mut overlay = OverlayedChanges::default();
for tx in transactions.iter() {
let ret_data = execute(
backend,
&mut overlay,
&executor(),
"execute_transaction",
&vec![].join(&header).join(tx)
).unwrap();
header = Header::from_slice(&mut &ret_data[..]).unwrap();
}
let ret_data = execute(
backend,
&mut overlay,
&executor(),
"finalise_block",
&vec![].join(&header)
).unwrap();
header = Header::from_slice(&mut &ret_data[..]).unwrap();
(vec![].join(&Block { header, transactions }), hash.into())
}
fn block1(genesis_hash: Hash, backend: &InMemory) -> (Vec<u8>, Hash) {
construct_block(
backend,
1,
genesis_hash,
hex!("25e5b37074063ab75c889326246640729b40d0c86932edc527bc80db0e04fe5c").into(),
vec![Transaction {
signed: one(),
nonce: 0,
function: Function::StakingTransfer(two(), 69),
}]
)
}
#[test]
fn construct_genesis_should_work() {
let mut storage = GenesisConfig::new_simple(
vec![one(), two()], 1000
).genesis_map();
let block = construct_genesis_block(&storage);
let genesis_hash = block.header.blake2_256().into();
storage.extend(additional_storage_with_genesis(&block).into_iter());
let mut overlay = OverlayedChanges::default();
let backend = InMemory::from(storage);
let (b1data, _b1hash) = block1(genesis_hash, &backend);
let _ = execute(
&backend,
&mut overlay,
&executor(),
"execute_block",
&b1data
).unwrap();
}
}
+127
View File
@@ -0,0 +1,127 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Polkadot CLI library.
#![warn(missing_docs)]
extern crate env_logger;
extern crate ed25519;
extern crate triehash;
extern crate substrate_codec as codec;
extern crate substrate_state_machine as state_machine;
extern crate substrate_client as client;
extern crate substrate_primitives as primitives;
extern crate substrate_runtime_io as runtime_io;
extern crate substrate_rpc_servers as rpc;
extern crate polkadot_primitives;
extern crate polkadot_executor;
extern crate polkadot_runtime;
#[macro_use]
extern crate hex_literal;
#[macro_use]
extern crate clap;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate log;
mod genesis;
pub mod error;
/// Parse command line arguments and start the node.
///
/// IANA unassigned port ranges that we could use:
/// 6717-6766 Unassigned
/// 8504-8553 Unassigned
/// 9556-9591 Unassigned
/// 9803-9874 Unassigned
/// 9926-9949 Unassigned
pub fn run<I, T>(args: I) -> error::Result<()> where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
{
let yaml = load_yaml!("./cli.yml");
let matches = clap::App::from_yaml(yaml).version(crate_version!()).get_matches_from_safe(args)?;
// TODO [ToDr] Split parameters parsing from actual execution.
let log_pattern = matches.value_of("log").unwrap_or("");
init_logger(log_pattern);
// Create client
let executor = polkadot_executor::executor();
let mut storage = Default::default();
let god_key = hex!["3d866ec8a9190c8343c2fc593d21d8a6d0c5c4763aaab2349de3a6111d64d124"];
use polkadot_runtime::genesismap::{additional_storage_with_genesis, GenesisConfig};
let genesis_config = GenesisConfig {
validators: vec![god_key.clone()],
authorities: vec![god_key.clone()],
balances: vec![(god_key.clone(), 1u64 << 63)].into_iter().collect(),
block_time: 5, // 5 second block time.
session_length: 720, // that's 1 hour per session.
sessions_per_era: 24, // 24 hours per era.
bonding_duration: 90, // 90 days per bond.
approval_ratio: 667, // 66.7% approvals required for legislation.
};
let prepare_genesis = || {
storage = genesis_config.genesis_map();
let block = genesis::construct_genesis_block(&storage);
storage.extend(additional_storage_with_genesis(&block).into_iter());
use codec::Slicable;
(primitives::block::Header::from_slice(&mut block.header.to_vec().as_ref()).expect("to_vec() always gives a valid serialisation; qed"), storage.into_iter().collect())
};
let client = client::new_in_mem(executor, prepare_genesis)?;
let address = "127.0.0.1:9933".parse().unwrap();
let handler = rpc::rpc_handler(client);
let server = rpc::start_http(&address, handler)?;
if let Some(_) = matches.subcommand_matches("collator") {
info!("Starting collator.");
server.wait();
return Ok(());
}
if let Some(_) = matches.subcommand_matches("validator") {
info!("Starting validator.");
server.wait();
return Ok(());
}
println!("No command given.\n");
let _ = clap::App::from_yaml(yaml).print_long_help();
Ok(())
}
fn init_logger(pattern: &str) {
let mut builder = env_logger::LogBuilder::new();
// Disable info logging by default for some modules:
builder.filter(Some("hyper"), log::LogLevelFilter::Warn);
// Enable info for others.
builder.filter(None, log::LogLevelFilter::Info);
if let Ok(lvl) = std::env::var("RUST_LOG") {
builder.parse(&lvl);
}
builder.parse(pattern);
builder.init().expect("Logger initialized only once.");
}