Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Unify primitives from polkadot-primitives and polkadot-runtime #58

Merged
merged 24 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4cfc108
extract out runtime-codec to a separate crate
rphmeier Jan 30, 2018
cf15e49
more idiomatic std features
rphmeier Jan 30, 2018
d85fec6
clean up workspaces a little
rphmeier Jan 30, 2018
0035e51
fully refactor runtime-std to use conditional compilation
rphmeier Jan 30, 2018
2678266
Merge branch 'master' into author-relay-block
rphmeier Jan 31, 2018
6f6be64
allow polkadot-primitives to compile on nightly
rphmeier Jan 31, 2018
7a1ae23
fix compiler warning
rphmeier Jan 31, 2018
83d5fb4
extract out all primitives
rphmeier Feb 1, 2018
dc81048
refactor codec
rphmeier Feb 1, 2018
8d41279
reintroduce slicable to primitives
rphmeier Feb 5, 2018
ff79e43
integrate new primitives with native-runtime
rphmeier Feb 5, 2018
d6718d9
fix most issues with compiling on WASM
rphmeier Feb 6, 2018
4e9683a
fix compilation for native
rphmeier Feb 6, 2018
014fc55
update native executor tests to use new tx format
rphmeier Feb 6, 2018
6fddf84
get compiling on wasm
rphmeier Feb 6, 2018
d16c9ed
fix tests
rphmeier Feb 6, 2018
e75e8a8
initial merge
rphmeier Feb 6, 2018
6d36db7
fix tests after merge
rphmeier Feb 6, 2018
fe2175b
unclobber function changes
rphmeier Feb 6, 2018
9a76246
fix tests and review grumbles
rphmeier Feb 6, 2018
1676404
move function and proposal types to transaction module
rphmeier Feb 6, 2018
bbbe6ae
remove debug impl in runtime-std
rphmeier Feb 6, 2018
15147c7
merge duty roster changes
rphmeier Feb 6, 2018
4b6006f
combine relay chain primitives into one module
rphmeier Feb 6, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
892 changes: 500 additions & 392 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ members = [
"collator",
"environmental",
"executor",
"native-runtime",
"network",
"primitives",
"rpc-servers",
"rpc",
"rpc_servers",
"native-runtime",
"runtime-codec",
"runtime-std",
"serializer",
"state_machine",
"state-machine",
"validator",
"network",
]
exclude = [
"wasm-runtime"
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ log = "0.3"
polkadot-client = { path = "../client", version = "0.1" }
polkadot-executor = { path = "../executor", version = "0.1" }
polkadot-primitives = { path = "../primitives", version = "0.1" }
polkadot-rpc-servers = { path = "../rpc_servers", version = "0.1" }
polkadot-rpc-servers = { path = "../rpc-servers", version = "0.1" }
4 changes: 3 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ error-chain = "0.11"
log = "0.3"
parking_lot = "0.4"
polkadot-primitives = { path = "../primitives", version = "0.1" }
polkadot-state-machine = { path = "../state_machine", version = "0.1" }
polkadot-state-machine = { path = "../state-machine", version = "0.1" }
polkadot-serializer = { path = "../serializer" }
polkadot-executor = { path = "../executor" }
polkadot-runtime-codec = { path = "../runtime-codec", version = "0.1" }
native-runtime = { path = "../native-runtime" }
triehash = "0.1"
hex-literal = "0.1"
ed25519 = { path = "../ed25519", version = "0.1" }
44 changes: 24 additions & 20 deletions client/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
//! Tool for creating the genesis block.

use std::collections::HashMap;
use native_runtime::primitives::{Block, Header};
use primitives::block::{Block, Header};
use primitives::H256;
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;
let state_root = H256(trie_root(storage.clone().into_iter()).0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use .into() instead of H256(...)?

let header = Header {
parent_hash: Default::default(),
number: 0,
state_root,
transaction_root: trie_root(vec![].into_iter()).0,
transaction_root: H256(trie_root(vec![].into_iter()).0),
digest: Default::default(),
};
Block {
Expand All @@ -39,19 +40,21 @@ pub fn construct_genesis_block(storage: &HashMap<Vec<u8>, Vec<u8>>) -> Block {
#[cfg(test)]
mod tests {
use super::*;
use native_runtime::codec::{Slicable, Joiner};
use codec::{Slicable, Joiner};
use native_runtime::support::{one, two, Hashable};
use native_runtime::runtime::genesismap::{GenesisConfig, additional_storage_with_genesis};
use native_runtime::primitives::{AccountID, Hash, BlockNumber, Transaction,
UncheckedTransaction, Digest, Function};
use state_machine::execute;
use state_machine::OverlayedChanges;
use state_machine::backend::InMemory;
use polkadot_executor::executor;
use primitives::{AccountId, Hash, H256};
use primitives::block::{Number as BlockNumber, Header, Digest};
use primitives::runtime_function::Function;
use primitives::transaction::{UncheckedTransaction, Transaction};
use primitives::contract::CallData;
use primitives::ed25519::Pair;
use ed25519::Pair;

fn secret_for(who: &AccountID) -> Option<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()),
Expand All @@ -64,12 +67,12 @@ mod tests {

let transactions = txs.into_iter().map(|transaction| {
let signature = secret_for(&transaction.signed).unwrap()
.sign(&transaction.to_vec())
.inner();
.sign(&transaction.to_vec());

UncheckedTransaction { transaction, signature }
}).collect::<Vec<_>>();

let transaction_root = ordered_trie_root(transactions.iter().map(Slicable::to_vec)).0;
let transaction_root = H256(ordered_trie_root(transactions.iter().map(Slicable::to_vec)).0);

let mut header = Header {
parent_hash,
Expand All @@ -83,37 +86,38 @@ mod tests {
let mut overlay = OverlayedChanges::default();

for tx in transactions.iter() {
header = Header::from_slice(&execute(
let ret_data = execute(
backend,
&mut overlay,
&executor(),
"execute_transaction",
&CallData(vec![].join(&header).join(tx))
).unwrap()).unwrap();
).unwrap();
header = Header::from_slice(&mut &ret_data[..]).unwrap();
}

header = Header::from_slice(&execute(
let ret_data = execute(
backend,
&mut overlay,
&executor(),
"finalise_block",
&CallData(vec![].join(&header))
).unwrap()).unwrap();
).unwrap();
header = Header::from_slice(&mut &ret_data[..]).unwrap();

(vec![].join(&Block { header, transactions }), hash)
(vec![].join(&Block { header, transactions }), H256(hash))
}

fn block1(genesis_hash: Hash, backend: &InMemory) -> (Vec<u8>, Hash) {
construct_block(
backend,
1,
genesis_hash,
hex!("25e5b37074063ab75c889326246640729b40d0c86932edc527bc80db0e04fe5c"),
H256(hex!("25e5b37074063ab75c889326246640729b40d0c86932edc527bc80db0e04fe5c")),
vec![Transaction {
signed: one(),
nonce: 0,
function: Function::StakingTransfer,
input_data: vec![].join(&two()).join(&69u64),
function: Function::StakingTransfer(two(), 69),
}]
)
}
Expand All @@ -124,7 +128,7 @@ mod tests {
vec![one(), two()], 1000
).genesis_map();
let block = construct_genesis_block(&storage);
let genesis_hash = block.header.blake2_256();
let genesis_hash = H256(block.header.blake2_256());
storage.extend(additional_storage_with_genesis(&block).into_iter());

let mut overlay = OverlayedChanges::default();
Expand Down
2 changes: 1 addition & 1 deletion client/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use primitives::block::{self, HeaderHash};
use blockchain::{self, BlockId, BlockStatus};

fn header_hash(header: &primitives::block::Header) -> primitives::block::HeaderHash {
primitives::hash(&ser::to_vec(header))
primitives::hashing::blake2_256(&ser::to_vec(header)).into()
}

struct PendingBlock {
Expand Down
11 changes: 8 additions & 3 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
extern crate polkadot_primitives as primitives;
extern crate polkadot_state_machine as state_machine;
extern crate polkadot_serializer as ser;
extern crate polkadot_runtime_codec as codec;
extern crate polkadot_executor;
extern crate native_runtime;
extern crate ed25519;

extern crate triehash;
extern crate parking_lot;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate log;
#[macro_use] extern crate hex_literal;

#[cfg(test)]
#[macro_use]
extern crate hex_literal;

pub mod error;
pub mod blockchain;
Expand Down Expand Up @@ -123,8 +128,8 @@ impl<B, E> Client<B, E> where
parent_hash: Default::default(),
number: 0,
state_root: Default::default(),
parachain_activity: Default::default(),
logs: Default::default(),
transaction_root: Default::default(),
digest: Default::default(),
};

let mut tx = backend.begin_transaction(BlockId::Hash(block::HeaderHash::default()))?;
Expand Down
10 changes: 10 additions & 0 deletions ed25519/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "ed25519"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
ring = "0.12"
untrusted = "0.5"
polkadot-primitives = { version = "0.1", path = "../primitives" }
rustc-hex = "1.0"
102 changes: 29 additions & 73 deletions primitives/src/ed25519.rs → ed25519/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

//! Simple Ed25519 API.

use untrusted;
extern crate ring;
extern crate polkadot_primitives as primitives;
extern crate untrusted;
extern crate rustc_hex;

use ring::{rand, signature};
use primitives::Signature;
use rustc_hex::FromHex;

/// Verify a message without type checking the parameters' types for the right size.
Expand All @@ -39,41 +44,6 @@ pub struct Public ([u8; 32]);
/// A key pair.
pub struct Pair(signature::Ed25519KeyPair);

/// A signature.
#[derive(Clone)]
pub struct Signature ([u8; 64]);

impl Signature {
/// A new signature from the given 64-byte `data`.
pub fn from(data: [u8; 64]) -> Self {
Signature(data)
}

/// A new signature from the given slice that should be 64 bytes long.
pub fn from_slice(data: &[u8]) -> Self {
let mut r = [0u8; 64];
r.copy_from_slice(data);
Signature(r)
}

/// Get the inner part.
pub fn inner(self) -> [u8; 64] {
self.0
}
}

impl AsRef<[u8; 64]> for Signature {
fn as_ref(&self) -> &[u8; 64] {
&self.0
}
}

impl AsRef<[u8]> for Signature {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}

impl Public {
/// A new instance from the given 32-byte `data`.
pub fn from(data: [u8; 32]) -> Self {
Expand Down Expand Up @@ -113,14 +83,14 @@ impl Pair {
}
/// Make a new key pair from the raw secret.
pub fn from_secret(secret: &[u8; 32]) -> Pair {
let mut pkcs8_bytes = FromHex::from_hex("302e020100300506032b657004220420").unwrap();
let mut pkcs8_bytes: Vec<_> = FromHex::from_hex("302e020100300506032b657004220420").unwrap();
pkcs8_bytes.extend_from_slice(&secret[..]);
Pair(signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(untrusted::Input::from(&pkcs8_bytes)).unwrap())
}
/// Make a new key pair from the raw secret and public key (it will check to make sure
/// they correspond to each other).
pub fn from_both(secret_public: &[u8; 64]) -> Option<Pair> {
let mut pkcs8_bytes = FromHex::from_hex("3053020101300506032b657004220420").unwrap();
let mut pkcs8_bytes: Vec<_> = FromHex::from_hex("3053020101300506032b657004220420").unwrap();
pkcs8_bytes.extend_from_slice(&secret_public[0..32]);
pkcs8_bytes.extend_from_slice(&[0xa1u8, 0x23, 0x03, 0x21, 0x00]);
pkcs8_bytes.extend_from_slice(&secret_public[32..64]);
Expand All @@ -130,7 +100,7 @@ impl Pair {
pub fn sign(&self, message: &[u8]) -> Signature {
let mut r = [0u8; 64];
r.copy_from_slice(self.0.sign(message).as_ref());
Signature(r)
Signature::from(r)
}
/// Get the public key.
pub fn public(&self) -> Public {
Expand All @@ -140,29 +110,30 @@ impl Pair {
Public(r)
}
}
impl Signature {
/// Verify a message.
pub fn verify(&self, message: &[u8], public: &Public) -> bool {
let peer_public_key = untrusted::Input::from(&public.0[..]);
let msg = untrusted::Input::from(message);
let sig = untrusted::Input::from(&self.0[..]);

match signature::verify(&signature::ED25519, peer_public_key, msg, sig) {
Ok(_) => true,
_ => false,
}

/// Verify a signature on a message.
pub fn verify_strong(sig: &Signature, message: &[u8], pubkey: &Public) -> bool {
let public_key = untrusted::Input::from(&pubkey.0[..]);
let msg = untrusted::Input::from(message);
let sig = untrusted::Input::from(&sig.0[..]);

match signature::verify(&signature::ED25519, public_key, msg, sig) {
Ok(_) => true,
_ => false,
}
}

impl From<&'static str> for Public {
fn from(hex: &'static str) -> Self {
let mut r = [0u8; 32];
r.copy_from_slice(&FromHex::from_hex(hex).unwrap()[0..32]);
let v: Vec<_> = FromHex::from_hex(hex).unwrap();
r.copy_from_slice(&v[0..32]);
Public(r)
}
}
impl From<&'static str> for Pair {
fn from(hex: &'static str) -> Self {
let data = FromHex::from_hex(hex).expect("Key pair given is static so hex should be good.");
let data: Vec<_> = FromHex::from_hex(hex).expect("Key pair given is static so hex should be good.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would have been nice to replace this code with hex![...] macro

Copy link
Contributor Author

@rphmeier rphmeier Feb 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think that's possible; there is a difference between a &'static str and a string literal.

match data.len() {
32 => {
let mut r = [0u8; 32];
Expand All @@ -180,19 +151,6 @@ impl From<&'static str> for Pair {
}
}
}
impl From<&'static str> for Signature {
fn from(hex: &'static str) -> Self {
let mut r = [0u8; 64];
r.copy_from_slice(&FromHex::from_hex(hex).unwrap()[0..64]);
Signature(r)
}
}

impl PartialEq for Signature {
fn eq(&self, other: &Signature) -> bool {
self.0.iter().eq(other.0.iter())
}
}

#[cfg(test)]
mod test {
Expand All @@ -206,7 +164,7 @@ mod test {
let message = b"";
let signature: Signature = "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b".into();
assert!(&pair.sign(&message[..]) == &signature);
assert!(signature.verify(&message[..], &public));
assert!(verify_strong(&signature, &message[..], &public));
}

#[test]
Expand All @@ -215,21 +173,19 @@ mod test {
let public = pair.public();
let message = b"Something important";
let signature = pair.sign(&message[..]);
assert!(signature.verify(&message[..], &public));
assert!(verify_strong(&signature, &message[..], &public));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why _strong prefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function verify already existed, this one is just with stronger types

}

#[test]
fn seeded_pair_should_work() {
use primitives::hexdisplay::HexDisplay;

let pair = Pair::from_seed(b"12345678901234567890123456789012");
let public = pair.public();
assert_eq!(public, "2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee".into());
let message = b"Something important";
let public = pair.public();
assert_eq!(public, "2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee".into());
let message = FromHex::from_hex("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000002228000000d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000").unwrap();
let message: Vec<_> = FromHex::from_hex("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000002228000000d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000").unwrap();
let signature = pair.sign(&message[..]);
use hexdisplay::HexDisplay;
println!("Correct signature: {}", HexDisplay::from(&signature.0));
assert!(signature.verify(&message[..], &public));
assert!(verify_strong(&signature, &message[..], &public));
}
}
Loading