Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
36c171c
Introduce basic skeleton for Polkador runtime.
gavofyork Jan 8, 2018
f79b7bb
Clean up the runtime skeleton.
gavofyork Jan 8, 2018
b48e053
Make initial runtime skeleton compile.
gavofyork Jan 8, 2018
4e25b24
Compile polkadot-runtime both for Wasm ad native, allowing for testin…
gavofyork Jan 9, 2018
b28402c
More fleshing out on runtime.
gavofyork Jan 9, 2018
bac50a4
Update native support.
gavofyork Jan 9, 2018
9a4360f
Fix warning.
gavofyork Jan 9, 2018
0890e72
Update gitignore
gavofyork Jan 9, 2018
4cf5fd1
Update path.
gavofyork Jan 9, 2018
12b15fd
Fix path.
gavofyork Jan 9, 2018
47260c2
Remove accidentally committed files.
gavofyork Jan 9, 2018
800eb20
Add wasm binaries.
gavofyork Jan 9, 2018
10eaefe
Fix test.
gavofyork Jan 9, 2018
24ba809
Native storage support API.
gavofyork Jan 9, 2018
36a49ff
Add environmental module
gavofyork Jan 10, 2018
50e7222
Add native environment to make native source-code compatible with wasm.
gavofyork Jan 10, 2018
3e17caa
Finish up & polish environment stuff.
gavofyork Jan 10, 2018
e807c9a
Avoid using reentrancy issues.
gavofyork Jan 10, 2018
402ae29
Add some docs and a test.
gavofyork Jan 10, 2018
762826b
Remove unneeded function.
gavofyork Jan 10, 2018
7e969c8
Documentation
gavofyork Jan 11, 2018
64b0670
Tweak docs
gavofyork Jan 11, 2018
d34f62a
Remove TODOs.
gavofyork Jan 11, 2018
89f35e3
Balance transfers + util methods.
gavofyork Jan 14, 2018
5e42240
Rejig tests and ensure authorities are addressed consistently.
gavofyork Jan 14, 2018
d97e2d6
Add marshaller for xfer function
gavofyork Jan 14, 2018
26ccc14
Transaction dispatch test.
gavofyork Jan 15, 2018
7258e1f
Minor fix.
gavofyork Jan 15, 2018
c02e335
Add test for ser/de transaction.
gavofyork Jan 15, 2018
28fa761
Add ser/de for header.
gavofyork Jan 15, 2018
cd93a37
Add tests for header ser/de
gavofyork Jan 16, 2018
b55095a
Introduce basic block decoding/execution framework.
gavofyork Jan 16, 2018
288f653
Introduce block decoding/execution framework (p2)
gavofyork Jan 16, 2018
c64976b
Big refactor.
gavofyork Jan 16, 2018
40e28fc
Split out joiner.
gavofyork Jan 16, 2018
ab37f64
Hide away support modules.
gavofyork Jan 16, 2018
4844086
Fix up wasm runtime.
gavofyork Jan 16, 2018
28d775d
use externalities for chain_id
gavofyork Jan 16, 2018
75cedd9
Clean up (Test)Externalities.
gavofyork Jan 16, 2018
0feadc6
Repot and introduce keccak-256 external.
gavofyork Jan 17, 2018
15ae775
Signing with crypto.
gavofyork Jan 17, 2018
5650997
fix unsafety hole in environmental using function
rphmeier Jan 17, 2018
7820149
Introduce Ed25519 crypto.
gavofyork Jan 17, 2018
4ef4239
Repotting.
gavofyork Jan 17, 2018
8fa858c
Merge branch 'signing-ring' into polkadot-runtime-skeleton
gavofyork Jan 17, 2018
78a22d4
Add ed25519_verify external.
gavofyork Jan 17, 2018
93f7aed
Introduce Ed25519 verify as an external.
gavofyork Jan 17, 2018
cdc1387
fix unsafety hole around unwinding
rphmeier Jan 17, 2018
0477b7e
Compile fixes.
gavofyork Jan 17, 2018
66c9384
use new environmental API
rphmeier Jan 17, 2018
121aa0b
Merge branch 'polkadot-runtime-skeleton' into environmental-api
rphmeier Jan 17, 2018
eebc071
Tests for ed25519 verify.
gavofyork Jan 18, 2018
5685173
Polish
gavofyork Jan 18, 2018
f3d415c
Introduce UncheckedTransaction & test.
gavofyork Jan 18, 2018
2c9cb7b
Implement basic block and tx processing
gavofyork Jan 18, 2018
eda7d71
Introduce static hex and valid signature for block test.
gavofyork Jan 18, 2018
fd9d8dc
Merge pull request #35 from paritytech/environmental-api
gavofyork Jan 18, 2018
e8cd918
Repot session.
gavofyork Jan 18, 2018
3d1ff94
comments.
gavofyork Jan 18, 2018
92a12f0
Refactor and timestamp test
gavofyork Jan 19, 2018
5523277
Remove fluff
gavofyork Jan 19, 2018
39128b7
Merge branch 'polkadot-runtime-skeleton' of github.com:paritytech/pol…
gavofyork Jan 19, 2018
9a8f61d
Remove fluff.
gavofyork Jan 19, 2018
90f965a
Staking eras and tests.
gavofyork Jan 19, 2018
58670fc
Implement sessions.
gavofyork Jan 19, 2018
999025f
Polish
gavofyork Jan 19, 2018
4fde6f0
Test sessions.
gavofyork Jan 19, 2018
9c8cafd
Introduce better hashing.
gavofyork Jan 19, 2018
ae5a9c9
Fix tests.
gavofyork Jan 19, 2018
639554c
Introduce staking.
gavofyork Jan 19, 2018
0e88dad
Tests for simple staking system.
gavofyork Jan 19, 2018
80aa38c
Build fix for wasm.
gavofyork Jan 19, 2018
ef5d78a
Fix tests.
gavofyork Jan 19, 2018
e068f44
Repotting and docs.
gavofyork Jan 19, 2018
2499910
Docs and licence.
gavofyork Jan 19, 2018
0c53f71
Documentation.
gavofyork Jan 19, 2018
850f7ee
Remove superfluous code.
gavofyork Jan 19, 2018
dde319c
Remove dummy key.
gavofyork Jan 19, 2018
17ef2d0
Remove other superfluous file.
gavofyork Jan 19, 2018
8adb7c6
Optimise with swap_remove
gavofyork Jan 20, 2018
cbd65cb
Merge remote-tracking branch 'origin/master' into polkadot-runtime-sk…
gavofyork Jan 23, 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
Prev Previous commit
Next Next commit
Repot and introduce keccak-256 external.
  • Loading branch information
gavofyork committed Jan 17, 2018
commit 0feadc6b84c7880c5927431e94c24165bd96b600
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ serde = "1.0"
serde_derive = "1.0"
parity-wasm = "0.15.0"
byteorder = "1.1"
tiny-keccak = "1.3"
rustc-hex = "1.0.0"

[dev-dependencies]
assert_matches = "1.1"
2 changes: 2 additions & 0 deletions executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ extern crate polkadot_state_machine as state_machine;
extern crate serde;
extern crate parity_wasm;
extern crate byteorder;
extern crate tiny_keccak;
extern crate rustc_hex;

#[macro_use]
extern crate error_chain;
Expand Down
69 changes: 32 additions & 37 deletions executor/src/wasm_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use state_machine::{Externalities, CodeExecutor};
use error::{Error, ErrorKind, Result};
use wasm_utils::{MemoryInstance, UserDefinedElements,
AddModuleWithoutFullDependentInstance};
use tiny_keccak;

struct Heap {
end: u32,
Expand Down Expand Up @@ -138,6 +139,15 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
},
ext_chain_id() -> u64 => {
this.ext.chain_id()
},
ext_keccak256(data: *const u8, len: u32, out: *mut u8) => {
let result =
if let Ok(value) = this.memory.get(data, len as usize) {
tiny_keccak::keccak256(&value)
} else {
[0; 32]
};
let _ = this.memory.set(out, &result);
}
=> <'e, E: Externalities + 'e>
);
Expand Down Expand Up @@ -193,6 +203,7 @@ impl CodeExecutor for WasmExecutor {
mod tests {

use super::*;
use rustc_hex::FromHex;

#[derive(Debug, Default)]
struct TestExternalities {
Expand All @@ -213,50 +224,34 @@ mod tests {
}

#[test]
fn should_pass_externalities_at_call() {
fn storage_should_work() {
let mut ext = TestExternalities::default();
ext.set_storage(b"\0code".to_vec(), b"The code".to_vec());

let program = ProgramInstance::new().unwrap();

let test_module = include_bytes!("../../wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
let module = deserialize_buffer(test_module.to_vec()).expect("Failed to load module");
let module = program.add_module_by_sigs("test", module, map!["env" => FunctionExecutor::<TestExternalities>::SIGNATURES]).expect("Failed to initialize module");

let output = {
let memory = module.memory(ItemIndex::Internal(0)).unwrap();
let mut fec = FunctionExecutor::new(&memory, &mut ext);

let data = b"Hello world";
let size = data.len() as u32;
let offset = fec.heap.allocate(size);
memory.set(offset, data).unwrap();

let returned = program
.params_with_external("env", &mut fec)
.map(|p| p
.add_argument(I32(offset as i32))
.add_argument(I32(size as i32)))
.and_then(|p| module.execute_export("test_data_in", p))
.map_err(|_| -> Error { ErrorKind::Runtime.into() }).expect("function should be callable");

if let Some(I64(r)) = returned {
println!("returned {:?} ({:?}, {:?})", r, r as u32, (r >> 32) as u32 as usize);
memory.get(r as u32, (r >> 32) as u32 as usize).expect("memory address should be reasonable.")
} else {
panic!("bad return value, not u64");
}
};
ext.set_storage(b"foo".to_vec(), b"bar".to_vec());
let test_code = include_bytes!("../../wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");

let output = WasmExecutor.call(&mut ext, &test_code[..], "test_data_in", &CallData(b"Hello world".to_vec())).unwrap();

assert_eq!(output, b"all ok!".to_vec());

let expected: HashMap<_, _> = map![
b"\0code".to_vec() => b"Hello world".to_vec(),
b"input".to_vec() => b"Hello world".to_vec(),
b"code".to_vec() => b"The code".to_vec(),
b"\0authority_count".to_vec() => vec![1],
b"\0authority".to_vec() => b"Hello world".to_vec()
b"foo".to_vec() => b"bar".to_vec(),
b"baz".to_vec() => b"bar".to_vec()
];
assert_eq!(expected, ext.storage);
}

#[test]
fn keccak256_should_work() {
let mut ext = TestExternalities::default();
let test_code = include_bytes!("../../wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
assert_eq!(
WasmExecutor.call(&mut ext, &test_code[..], "test_keccak256", &CallData(b"".to_vec())).unwrap(),
FromHex::from_hex("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap()
);
assert_eq!(
WasmExecutor.call(&mut ext, &test_code[..], "test_keccak256", &CallData(b"Hello world!".to_vec())).unwrap(),
FromHex::from_hex("ecd0e108a98e192af1d2c25055f4e3bed784b5c877204e73219a5203251feaab").unwrap()
);
}
}
1 change: 1 addition & 0 deletions native-runtime/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ lazy_static = "1.0.0"
parking_lot = "0.5"
polkadot-state-machine = { path = "../../state_machine" , version = "0.1" }
environmental = { path = "../../environmental", version = "0.1.0" }
tiny-keccak = "1.3"
4 changes: 4 additions & 0 deletions native-runtime/support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[macro_use]
extern crate environmental;
extern crate polkadot_state_machine;
extern crate tiny_keccak;

pub use std::vec::Vec;
pub use std::rc::Rc;
Expand Down Expand Up @@ -75,6 +76,9 @@ pub fn chain_id() -> u64 {
).unwrap_or(0)
}

/// Conduct a Keccak-256 hash of the given data.
pub use tiny_keccak::keccak256;

/// Execute the given closure with global function available whose functionality routes into the
/// externalities `ext`. Forwards the value that the closure returns.
pub fn with_externalities<R, F: FnOnce() -> R>(ext: &mut Externalities<Error=NoError>, f: F) -> R {
Expand Down
5 changes: 5 additions & 0 deletions wasm-runtime/polkadot/src/codec/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod endiansensitive;
pub mod streamreader;
pub mod joiner;
pub mod slicable;
pub mod keyedvec;
20 changes: 7 additions & 13 deletions wasm-runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@
#[macro_use]
extern crate runtime_support;

mod codec;
mod support;
pub use support::{endiansensitive, streamreader, joiner, slicable, primitives, keyedvec, function,
environment, storage};
mod runtime;
pub use codec::{endiansensitive, streamreader, joiner, slicable, keyedvec};
pub use support::{primitives, function, environment, storage};
#[cfg(test)]
pub use support::testing;

#[allow(unused)]
mod system;
#[allow(unused)]
mod consensus;
#[allow(unused)]
mod staking;
#[allow(unused)]
mod timestamp;
#[allow(unused_imports)] // TODO: remove in due course

use runtime_support::Vec;
use slicable::Slicable;
Expand All @@ -29,11 +23,11 @@ use primitives::{Block, Transaction};
// - ECDSA-recover (or some better sig scheme)

pub fn execute_block(input: Vec<u8>) -> Vec<u8> {
system::execute_block(Block::from_slice(&input).unwrap())
runtime::system::execute_block(Block::from_slice(&input).unwrap())
}

pub fn execute_transaction(input: Vec<u8>) -> Vec<u8> {
system::execute_transaction(&Transaction::from_slice(&input).unwrap())
runtime::system::execute_transaction(&Transaction::from_slice(&input).unwrap())
}

impl_stubs!(execute_block, execute_transaction);
8 changes: 8 additions & 0 deletions wasm-runtime/polkadot/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[allow(unused)]
pub mod system;
#[allow(unused)]
pub mod consensus;
#[allow(unused)]
pub mod staking;
#[allow(unused)]
pub mod timestamp;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use keyedvec::KeyedVec;
use storage::{Storage, storage_into};
use primitives::{BlockNumber, Balance, AccountID};
use consensus;
use runtime::consensus;

/// The length of a staking era in blocks.
pub fn era_length() -> BlockNumber {
Expand Down Expand Up @@ -68,7 +68,7 @@ mod tests {
use runtime_support::with_externalities;
use testing::TestExternalities;
use primitives::{AccountID};
use staking;
use runtime::staking;

macro_rules! map {
($( $name:expr => $value:expr ),*) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use primitives::{Block, BlockNumber, Hash, Transaction};
use runtime_support::{Vec, swap};
use environment::with_env;
use staking;
use runtime_support;
use runtime::staking;

/// The current block number being processed. Set by `execute_block`.
pub fn block_number() -> BlockNumber {
Expand Down Expand Up @@ -79,8 +79,7 @@ mod tests {
use std::collections::HashMap;
use runtime_support::{NoError, with_externalities, Externalities};
use primitives::{AccountID, Transaction};
use system;
use staking;
use runtime::{system, staking};

#[derive(Debug, Default)]
struct TestExternalities {
Expand Down
3 changes: 1 addition & 2 deletions wasm-runtime/polkadot/src/support/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use staking;
use consensus;
use runtime::{staking, consensus};
use primitives::AccountID;
use streamreader::StreamReader;

Expand Down
5 changes: 0 additions & 5 deletions wasm-runtime/polkadot/src/support/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
pub mod endiansensitive;
pub mod streamreader;
pub mod joiner;
pub mod slicable;
pub mod primitives;
pub mod keyedvec;
pub mod function;
pub mod environment;
pub mod storage;
Expand Down
11 changes: 11 additions & 0 deletions wasm-runtime/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C" {
fn ext_get_allocated_storage(key_data: *const u8, key_len: u32, written_out: *mut u32) -> *mut u8;
fn ext_get_storage_into(key_data: *const u8, key_len: u32, value_data: *mut u8, value_len: u32) -> u32;
fn ext_chain_id() -> u64;
fn ext_keccak256(data: *const u8, len: u32, out: *mut u8);
}

pub fn storage(key: &[u8]) -> Vec<u8> {
Expand Down Expand Up @@ -78,6 +79,16 @@ pub fn chain_id() -> u64 {
}
}

/// Conduct a keccak256 hash.
pub fn keccak256(data: &[u8]) -> [u8; 32] {
unsafe {
let mut result: [u8; 32] = uninitialized();
// guaranteed to write into result.
ext_keccak256(&data[0], data.len() as u32, &mut result[0]);
result
}
}

pub trait Printable {
fn print(self);
}
Expand Down
Binary file not shown.
Binary file not shown.
65 changes: 9 additions & 56 deletions wasm-runtime/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,25 @@ use alloc::vec::Vec;

#[macro_use]
extern crate runtime_support;
use runtime_support::{set_storage, storage, print};
use runtime_support::{set_storage, storage, print, keccak256};

pub fn code() -> Vec<u8> {
storage(b"\0code")
fn test_keccak256(input: Vec<u8>) -> Vec<u8> {
keccak256(&input).to_vec()
}

pub fn set_code(new: &[u8]) {
set_storage(b"\0code", new)
}

fn value_vec(mut value: usize, initial: Vec<u8>) -> Vec<u8> {
let mut acc = initial;
while value > 0 {
acc.push(value as u8);
value /= 256;
}
acc
}

pub fn set_authority(index: usize, authority: &[u8]) {
set_storage(&value_vec(index, b"\0authority".to_vec()), authority);
}

pub fn authority(index: usize) -> Vec<u8> {
storage(&value_vec(index, b"\0authority".to_vec()))
}

pub fn set_authority_count(count: usize) {
(count..authority_count()).for_each(|i| set_authority(i, &[]));
set_storage(b"\0authority_count", &value_vec(count, Vec::new()));
}

pub fn authority_count() -> usize {
storage(b"\0authority_count").into_iter().rev().fold(0, |acc, i| (acc << 8) + (i as usize))
}

pub fn authorities() -> Vec<Vec<u8>> {
(0..authority_count()).into_iter().map(authority).collect()
}

pub fn set_authorities(authorities: &[&[u8]]) {
set_authority_count(authorities.len());
authorities.iter().enumerate().for_each(|(v, i)| set_authority(v, i));
}

impl_stubs!(test_data_in);
fn test_data_in(input: Vec<u8>) -> Vec<u8> {
print(b"set_storage" as &[u8]);
set_storage(b"input", &input);

print(b"code" as &[u8]);
set_storage(b"code", &code());

print(b"set_code" as &[u8]);
set_code(&input);

print(b"storage" as &[u8]);
let copy = storage(b"input");

print(b"authorities" as &[u8]);
let mut v = authorities();
v.push(copy);
let foo = storage(b"foo");

print(b"set_authorities" as &[u8]);
set_authorities(&v.iter().map(Vec::as_slice).collect::<Vec<_>>());
print(b"set_storage" as &[u8]);
set_storage(b"baz", &foo);

print(b"finished!" as &[u8]);
b"all ok!".to_vec()
}


impl_stubs!(test_data_in, test_keccak256);