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

Introduce first groundwork for Wasm executor #27

Merged
merged 51 commits into from
Jan 8, 2018
Merged
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
4f5c5b6
Introduce first groundwork for Wasm executor.
gavofyork Dec 5, 2017
6237848
Remove old Rust-runtime code.
gavofyork Dec 6, 2017
293cf5d
Avoid commiting compled files.
gavofyork Dec 6, 2017
66b636e
Add runtime precompile.
gavofyork Dec 6, 2017
c7e7456
Rename so module makes more sense.
gavofyork Dec 6, 2017
0cfd67e
Further renaming.
gavofyork Dec 6, 2017
3011edf
Ensure tests work.
gavofyork Dec 6, 2017
a74b4fa
Allow bringing in of externalities.
gavofyork Dec 7, 2017
6dac275
Nice macros for imports.
gavofyork Dec 9, 2017
08f7b26
Allow passing in of data through allocators.
gavofyork Dec 11, 2017
1ace6b2
Can now pass in bytes to WasmExecutor.
gavofyork Dec 11, 2017
27e5fed
Additional cleanup.
gavofyork Dec 11, 2017
13dcd89
Switch usages of `OutData` to `u64`
gavofyork Dec 11, 2017
84b9f84
convert to safe but extremely verbose type conversion.
gavofyork Dec 11, 2017
7f31899
Remove StaticExternalities distinction.
gavofyork Dec 11, 2017
edf061e
Remove another unused use.
gavofyork Dec 11, 2017
edb6bea
Refactor wasm utils out
gavofyork Dec 11, 2017
9f59f48
Remove extraneous copies that weren't really testing anything.
gavofyork Dec 11, 2017
287b29d
Try to use wasm 0.15
gavofyork Dec 31, 2017
1bd55fe
Make it work!
gavofyork Dec 31, 2017
36e254a
Call-time externalities working.
gavofyork Jan 1, 2018
cd651a3
Add basic externalities.
gavofyork Jan 1, 2018
a8f9cca
Merge branch 'with-wasm-0.15' into with-wasm
gavofyork Jan 1, 2018
4404846
Fix grumbles and note unwraps to be sorted.
gavofyork Jan 1, 2018
b1d963a
Test storage externality.
gavofyork Jan 3, 2018
319d9c0
Fix nits.
gavofyork Jan 3, 2018
7ec9221
Merge branch 'master' into with-wasm
gavofyork Jan 3, 2018
2934d94
Compile collation logic.
gavofyork Jan 3, 2018
5998aa1
Move back to refs. Yey.
gavofyork Jan 3, 2018
3f4085a
Remove "object" id for storage access.
gavofyork Jan 4, 2018
4be0537
Fix test.
gavofyork Jan 4, 2018
01d7019
Fix up rest of tests.
gavofyork Jan 4, 2018
db1adee
remove unwrap.
gavofyork Jan 4, 2018
87c54f7
Expose set/get code in externalities
gavofyork Jan 5, 2018
471ea1e
Add validator set.
gavofyork Jan 5, 2018
fa35993
Introduce validator set into externalities and test.
gavofyork Jan 5, 2018
a0f64df
Add another external function.
gavofyork Jan 6, 2018
e736d46
Remove code and validators; use storage for everything.
gavofyork Jan 6, 2018
234297c
Introduce validators function.
gavofyork Jan 6, 2018
3f8a96d
Tests (and a fix) for the validators getter.
gavofyork Jan 6, 2018
6636520
Allow calls into runtime to return data.
gavofyork Jan 7, 2018
964659e
Remove unneeded trace.
gavofyork Jan 7, 2018
8ca1b7b
Make runtime printing a bit nicer.
gavofyork Jan 7, 2018
74156a2
Create separate runtimes for testing and polkadot.
gavofyork Jan 8, 2018
611a7ac
Remove commented code.
gavofyork Jan 8, 2018
c3afecc
Use new path.
gavofyork Jan 8, 2018
ea4d6c5
Refactor into shared support module.
gavofyork Jan 8, 2018
709693d
Fix warning.
gavofyork Jan 8, 2018
ec1e6b6
Remove unwraps.
gavofyork Jan 8, 2018
5c0ec3d
Make macro a little less unhygenic.
gavofyork Jan 8, 2018
79ab46f
Add wasm files.
gavofyork Jan 8, 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
Next Next commit
Introduce first groundwork for Wasm executor.
gavofyork committed Dec 5, 2017
commit 4f5c5b6c176606b99640b7f64da7d23175bf31ed
32 changes: 22 additions & 10 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -18,3 +18,6 @@ members = [
"state_machine",
"validator",
]
exclude = [
"runtime"
]
9 changes: 4 additions & 5 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ extern crate error_chain;
pub mod error;

use primitives::{block, Address, H256};
use primitives::contract::{CallData, OutData, StorageData};
use primitives::contract::{CallData, OutData, StorageKey, StorageData};
use state_machine::backend::Backend;

use self::error::ResultExt;
@@ -72,23 +72,22 @@ impl<B, E> Client<B, E> where
}

/// Return single storage entry of contract under given address in state in a block of given hash.
pub fn storage(&self, hash: &block::HeaderHash, address: &Address, key: &H256) -> error::Result<StorageData> {
pub fn storage(&self, hash: &block::HeaderHash, object: u64, key: &StorageKey) -> error::Result<StorageData> {
self.state_at(hash)?
.storage(address, key)
.storage(object, &key.0)
.map(|x| StorageData(x.to_vec()))
.chain_err(|| error::ErrorKind::Backend)
}

/// Execute a call to a contract on top of state in a block of given hash.
pub fn call(&self, hash: &block::HeaderHash, address: &Address, method: &str, call_data: &CallData) -> error::Result<OutData> {
pub fn call(&self, hash: &block::HeaderHash, method: &str, call_data: &CallData) -> error::Result<OutData> {
let state = self.state_at(hash)?;
let mut changes = state_machine::OverlayedChanges::default();

Ok(state_machine::execute(
&state,
&mut changes,
&self.executor,
address,
method,
call_data,
)?)
1 change: 1 addition & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ polkadot-serializer = { path = "../serializer", version = "0.1" }
polkadot-state-machine = { path = "../state_machine" , version = "0.1" }
serde = "1.0"
serde_derive = "1.0"
parity-wasm = "0.17.0"

[dev-dependencies]
assert_matches = "1.1"
2 changes: 1 addition & 1 deletion contracts/src/auth.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ use primitives::Address;
use state_machine::StaticExternalities;

use error::Result;
use executor::RustExecutor;
use rust_executor::RustExecutor;

/// Data and some sort of Authentication Data
type DataAndAuth = (Vec<u8>, Vec<u8>);
2 changes: 1 addition & 1 deletion contracts/src/balances.rs
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ use primitives::uint::U256;
use state_machine::{Externalities, StaticExternalities};

use error::Result;
use executor::RustExecutor;
use rust_executor::RustExecutor;

#[derive(Debug, Serialize, Deserialize)]
pub struct Transfer {
6 changes: 6 additions & 0 deletions contracts/src/error.rs
Original file line number Diff line number Diff line change
@@ -42,5 +42,11 @@ error_chain! {
description("externalities failure"),
display("Externalities error: {}", e),
}

/// Runtime failed.
Runtime {
description("runtime failure"),
display("Runtime error"),
}
}
}
22 changes: 16 additions & 6 deletions contracts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -17,31 +17,41 @@
//! Temporary crate for contracts implementations.
//!
//! This will be replaced with WASM contracts stored on-chain.
//! ** NOTE ***
//! This is entirely deprecated with the idea of a single-module Wasm module for state transition.
//! The dispatch table should be replaced with the specific functions needed:
//! - execute_block(bytes)
//! - init_block(PrevBlock?) -> InProgressBlock
//! - add_transaction(InProgressBlock) -> InProgressBlock
//! I leave it as is for now as it might be removed before this is ever done.

#![warn(missing_docs)]

extern crate polkadot_primitives as primitives;
extern crate polkadot_serializer as serializer;
extern crate polkadot_state_machine as state_machine;
extern crate serde;
extern crate parity_wasm;

#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate serde_derive;
//#[macro_use]
//extern crate serde_derive;

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

/*
mod auth;
mod balances;
mod validator_set;
pub mod rust_executor;
*/
mod wasm_executor;

pub mod error;
pub mod executor;

/// Creates new RustExecutor for contracts.
pub fn executor() -> executor::RustExecutor {
executor::RustExecutor::default()
pub fn executor() -> wasm_executor::WasmExecutor {
wasm_executor::WasmExecutor::default()
}
70 changes: 9 additions & 61 deletions contracts/src/executor.rs → contracts/src/rust_executor.rs
Original file line number Diff line number Diff line change
@@ -36,43 +36,9 @@ pub struct RustExecutor {
validator_set: validator_set::Contract,
}

impl RustExecutor {
const AUTH: u8 = 1;
const BALANCES: u8 = 2;
const VALIDATOR_SET: u8 = 3;
}

impl CodeExecutor for RustExecutor {
type Error = Error;

fn call_static<E: StaticExternalities<Self>>(
&self,
ext: &E,
code: &[u8],
method: &str,
data: &CallData,
) -> Result<OutData> {
ensure!(code.len() == 1, ErrorKind::InvalidCode(code.to_vec()));

Ok(OutData(match code[0] {
Self::AUTH => match method {
"check_auth" => ser(&self.auth.check_auth(ext, de(&data.0)?)?),
m => bail!(ErrorKind::MethodNotFound(m.to_owned())),
},
Self::BALANCES => match method {
"balance_of" => ser(&self.balance.balance_of(ext, de(&data.0)?)?),
"next_nonce" => ser(&self.balance.next_nonce(ext, de(&data.0)?)?),
"transfer_preconditions" => ser(&self.balance.transfer_preconditions(ext, de(&data.0)?)?),
m => bail!(ErrorKind::MethodNotFound(m.to_owned())),
},
Self::VALIDATOR_SET => match method {
"validator_set" => ser(&self.validator_set.validator_set(ext, de(&data.0)?)?),
m => bail!(ErrorKind::MethodNotFound(m.to_owned())),
},
c => bail!(ErrorKind::InvalidCode(vec![c])),
}))
}

fn call<E: Externalities<Self>>(
&self,
ext: &mut E,
@@ -82,12 +48,14 @@ impl CodeExecutor for RustExecutor {
) -> Result<OutData> {
ensure!(code.len() == 1, ErrorKind::InvalidCode(code.to_vec()));

Ok(OutData(match code[0] {
Self::BALANCES=> match method {
"transfer" => ser(&self.balance.transfer(ext, de(&data.0)?)?),
m => bail!(ErrorKind::MethodNotFound(m.to_owned())),
},
c => bail!(ErrorKind::InvalidCode(vec![c])),
Ok(OutData(match method {
"check_auth" => ser(&self.auth.check_auth(ext, de(&data.0)?)?),
"balance_of" => ser(&self.balance.balance_of(ext, de(&data.0)?)?),
"next_nonce" => ser(&self.balance.next_nonce(ext, de(&data.0)?)?),
"transfer_preconditions" => ser(&self.balance.transfer_preconditions(ext, de(&data.0)?)?),
"validator_set" => ser(&self.validator_set.validator_set(ext, de(&data.0)?)?),
"transfer" => ser(&self.balance.transfer(ext, de(&data.0)?)?),
m => bail!(ErrorKind::MethodNotFound(m.to_owned())),
}))
}
}
@@ -122,34 +90,14 @@ mod tests {
}
}

#[test]
fn should_fail_for_empty_or_unknown_code() {
// given
let mut ext = TestExternalities::default();
let executor = RustExecutor::default();

assert_matches!(
*executor.call(&mut ext, &[], "any", &CallData(vec![])).unwrap_err().kind(),
ErrorKind::InvalidCode(ref code) if code.is_empty()
);
assert_matches!(
*executor.call(&mut ext, &[1, 2], "any", &CallData(vec![])).unwrap_err().kind(),
ErrorKind::InvalidCode(ref code) if code.len() == 2
);
assert_matches!(
*executor.call(&mut ext, &[255,], "any", &CallData(vec![])).unwrap_err().kind(),
ErrorKind::InvalidCode(_)
);
}

#[test]
fn should_fail_on_invalid_method() {
// given
let mut ext = TestExternalities::default();
let executor = RustExecutor::default();

assert_matches!(
*executor.call(&mut ext, &[2], "any", &CallData(vec![])).unwrap_err().kind(),
*executor.call(&mut ext, "any", &CallData(vec![])).unwrap_err().kind(),
ErrorKind::MethodNotFound(ref method) if &*method == "any"
);
}
2 changes: 1 addition & 1 deletion contracts/src/validator_set.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ use primitives::Address;
use state_machine::StaticExternalities;

use error::Result;
use executor::RustExecutor;
use rust_executor::RustExecutor;

/// Harcoded validator set contract.
#[derive(Debug, Default)]
Loading
Oops, something went wrong.