Skip to content

Commit

Permalink
Switch to manual parsing for validation (#157)
Browse files Browse the repository at this point in the history
* wasm-parser: remove parity-wasm dependency

* wasm-parser: proof of basic idea

* wasm-parser: import instruction mappings from parity-wasm

* wasm-parser: build and link with kernel

* wasm-parser: fix parsing of import entries

* wasm-parser: add missing file

* kernel: add test script

* wasm-parser: minor cleanup

* circleci: don't force rebuild of parity-ethereum

* kernel: update tests to be more accurate

* kernel: update nightly version for alloc crate

* circleci: fix error with parity installation

* circleci: fix error in previous commit

* wasm-parser: reinclude alloc

* wasm-parser: merge Cursor and CodeCursor

* circleci: add example contract 2

* circleci: set-up environment for test

* circleci: fix example_contract_2 build

* wasm-parser: properly validate instructions in syscall

* wasm-parser: fix import cursor progression

* circleci: remove config step from parity

* circleci: clear cache
  • Loading branch information
JakeOShannessy committed Jun 6, 2019
1 parent f912c3d commit 2f83b1c
Show file tree
Hide file tree
Showing 23 changed files with 3,915 additions and 189 deletions.
19 changes: 12 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ jobs:
steps:
- restore_cache:
keys:
- deps6-{{ .Branch }}-{{ .Revision }}
# - deps6-{{ .Branch }}-cargo-{{ checksum "kernel-ewasm/Cargo.lock" }}
- deps6-{{ .Branch }}-
- deps6-
- deps7-{{ .Branch }}-{{ .Revision }}
# - deps7-{{ .Branch }}-cargo-{{ checksum "kernel-ewasm/Cargo.lock" }}
- deps7-{{ .Branch }}-
- deps7-
- run:
name: Install native build prequisites
command: |
Expand Down Expand Up @@ -96,9 +96,13 @@ jobs:
# cargo build --verbose --release --features final
# strip target/debug/parity
# file target/debug/parity
cargo install --bin parity -j 1 --path . --bin parity parity-ethereum --force
if parity --version; then
echo "Parity node installed"
else
cargo install --bin parity -j 1 --path . --bin parity parity-ethereum
fi
- save_cache:
key: deps6-{{ .Branch }}-cargo #-{{ checksum "kernel-ewasm/Cargo.lock" }}
key: deps7-{{ .Branch }}-cargo #-{{ checksum "kernel-ewasm/Cargo.lock" }}
paths:
- "~/.cargo"
- "~/.rustup"
Expand All @@ -118,7 +122,7 @@ jobs:
cd kernel-ewasm
# we need to run parity once to set up the accounts and keys
# this only needs to be active for a few seconds (hence timeout)
timeout 5 parity --config dev || true
# timeout 5 parity --config dev || true
# We then run parity properly, now unlocking the previously setup
# account
parity --config dev --chain ./wasm-dev-chain.json --jsonrpc-apis=all --ws-apis=all --reseal-min-period 0 --gasprice 0
Expand All @@ -135,6 +139,7 @@ jobs:
- run:
name: Test Rust Component
command: |
. ~/.profile
cd cap9
cd kernel-ewasm && npm install
npm run test
1 change: 1 addition & 0 deletions kernel-ewasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ crate-type = ["cdylib"]
[features]
default = ["std"]
std = ["pwasm-std/std", "pwasm-ethereum/std", "pwasm-test/std"]
panic_with_msg = ["pwasm-std/panic_with_msg"]

[profile.release]
panic = "abort"
Expand Down
2 changes: 1 addition & 1 deletion kernel-ewasm/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cargo install --path ..\cap9-build --bin cap9-build --force

set contract_name=kernel_ewasm

cargo build --release --target wasm32-unknown-unknown --no-default-features
cargo build --release --target wasm32-unknown-unknown --no-default-features --features "panic_with_msg"
cap9-build set-mem --pages 3 .\target\wasm32-unknown-unknown\release\%contract_name%.wasm .\target\wasm32-unknown-unknown\release\%contract_name%.wasm
wasm-build --target=wasm32-unknown-unknown .\target kernel-ewasm
REM ..\..\wasm-utils\target\debug\wasm-build.exe --target=wasm32-unknown-unknown .\target kernel-ewasm
Expand Down
2 changes: 1 addition & 1 deletion kernel-ewasm/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ rustup target add wasm32-unknown-unknown
cargo install pwasm-utils-cli --bin wasm-build --version 0.6.0
cargo install --path ../cap9-build --bin cap9-build --force

cargo build --release --target wasm32-unknown-unknown --no-default-features
cargo build --release --target wasm32-unknown-unknown --no-default-features --features "panic_with_msg"
cap9-build set-mem --pages 3 ./target/wasm32-unknown-unknown/release/kernel_ewasm.wasm ./target/wasm32-unknown-unknown/release/kernel_ewasm.wasm
wasm-build --target=wasm32-unknown-unknown ./target kernel-ewasm

Expand Down
22 changes: 22 additions & 0 deletions kernel-ewasm/example_contract_2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "example_contract_2"
version = "0.1.0"

[dependencies]
pwasm-std = "0.13"
pwasm-ethereum = { version = "0.8", features = ["kip6"] }
pwasm-abi = "0.2"
pwasm-abi-derive = "0.2"
tiny-keccak = "1.4.2"

[dev-dependencies]
pwasm-test = { git = "https://github.com/paritytech/pwasm-test" }
cap9-build = { path = "../../cap9-build" }
pwasm-utils-cli = "0.7.0"

[features]
default = []
std = ["pwasm-std/std", "pwasm-ethereum/std"]

[lib]
crate-type = ["cdylib"]
15 changes: 15 additions & 0 deletions kernel-ewasm/example_contract_2/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo OFF

mkdir .\build

rustup target add wasm32-unknown-unknown
REM cargo install pwasm-utils-cli --bin wasm-build --force

set contract_name=example_contract_2

cargo build --release --target wasm32-unknown-unknown
REM We don't need to use cap9 build here as it contains no syscalls
wasm-build --target=wasm32-unknown-unknown .\target %contract_name%

copy .\target\*.wasm .\build
copy .\target\json\* .\build
16 changes: 16 additions & 0 deletions kernel-ewasm/example_contract_2/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@echo OFF

mkdir ./build

rustup target add wasm32-unknown-unknown
REM cargo install pwasm-utils-cli --bin wasm-build --force
cargo install --path ../../cap9-build --bin cap9-build --force

export contract_name=example_contract_2

cargo build --release --target wasm32-unknown-unknown
# We don't need to use cap9 build here as it contains no syscalls
wasm-build --target=wasm32-unknown-unknown ./target $contract_name

cp ./target/*.wasm ./build
cp ./target/json/* ./build
Binary file not shown.
52 changes: 52 additions & 0 deletions kernel-ewasm/example_contract_2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![cfg_attr(not(feature="std"), no_std)]

#![allow(non_snake_case)]

extern crate tiny_keccak;
extern crate pwasm_std;
extern crate pwasm_ethereum;
extern crate pwasm_abi;
extern crate pwasm_abi_derive;

use tiny_keccak::Keccak;
use pwasm_ethereum as eth;
use pwasm_abi::types::*;
use pwasm_abi_derive::eth_abi;
use pwasm_ethereum::Error;

/// The call function is the main function of the *deployed* contract
#[no_mangle]
pub fn call() {
let mut endpoint = contract::ExampleContract2Endpoint::new(contract::ExampleContract2{});
pwasm_ethereum::ret(&endpoint.dispatch(&pwasm_ethereum::input()));
}

// Declares the dispatch and dispatch_ctor methods
use pwasm_abi::eth::EndpointInterface;

#[no_mangle]
pub fn deploy() {
let mut endpoint = contract::ExampleContract2Endpoint::new(contract::ExampleContract2{});
endpoint.dispatch_ctor(&pwasm_ethereum::input());
}


pub mod contract {
use super::*;
use pwasm_abi_derive::eth_abi;

#[eth_abi(ExampleContract2Endpoint, ExampleContract2Client)]
pub trait ExampleContract2Interface {
/// Check if Procedure Contract is Valid
fn check_contract(&mut self, _to: Address) -> bool;
}

pub struct ExampleContract2;

impl ExampleContract2Interface for ExampleContract2 {
fn check_contract(&mut self, _target: Address) -> bool {
// unimplemented!()
false
}
}
}
2 changes: 1 addition & 1 deletion kernel-ewasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"deploy": "node script/deploy.js",
"test": "test.sh"
"test": "sh ./test.sh"
},
"author": "",
"license": "ISC",
Expand Down
2 changes: 1 addition & 1 deletion kernel-ewasm/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2019-04-01
nightly-2019-06-06
20 changes: 2 additions & 18 deletions kernel-ewasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub mod token {
use pwasm_ethereum;
use pwasm_abi::types::*;
// use parity_wasm::elements::{Module};
use validator::{Validity, Module, deserialize_buffer};
use validator::{Validity};


// eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html
Expand Down Expand Up @@ -130,23 +130,7 @@ pub mod token {
// Next we get the code of the contract, using EXTCODECOPY under
// the hood.
let code: pwasm_std::Vec<u8> = self.code_copy(target);
// code_slice is magic number and version number only
let code_slice = &[0, 97, 115, 109, 1, 0, 0, 0];
// big_code_slice is magic number, version number and a simple
// data section.
let big_code_slice = &[0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x0B, 0x07, 0x01, 0x00, 0x41, 0x01, 0x0B, 0x01, 0x54, 0x00, 0x08, 0x04, 0x6E, 0x61, 0x6D, 0x65, 0x02, 0x01, 0x00];
// Next we deserialise the code from Vec<u8> into a Module.
let module: Module = match deserialize_buffer(code.as_slice()) {
// let module: Module = match deserialize_buffer(code_slice) {
Ok(module) => module,
// If we are unable to decode the contract, we assume it is
// not valid, but for now we will panic for testing
// purposes.
Err(_) => panic!("invalid wasm module"),
};
// // Then we perform a boolen is_valid() check.
module.is_valid();
false
code.as_slice().is_valid()
}
}

Expand Down
4 changes: 4 additions & 0 deletions kernel-ewasm/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pushd example_contract_2
sh ./build.sh
popd
mocha tests/**/**.js
8 changes: 4 additions & 4 deletions kernel-ewasm/tests/integration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ describe('Kernel', function() {
assert.strictEqual(code_size, code.length, "The code length should be as given by EXTCODESIZE");
})

it('should return false when trying to validate the kernel itself', async function() {
it('should return a boolean when trying to validate the kernel itself', async function() {
const kernelAddress = kernel.options.address;
assert(web3.utils.isAddress(kernelAddress), "The kernel address should be a valid address")
let rec_validation = await kernel.methods.check_contract(kernelAddress).call();
assert.strictEqual(rec_validation, false)
assert.strictEqual(typeof rec_validation, "boolean");
})

it('should copy the code of an example contract', async function() {
Expand All @@ -193,15 +193,15 @@ describe('Kernel', function() {
// assert.strictEqual(code_size, code.length, "The code length should be as given by EXTCODESIZE");
})

it('should correctly validate an example contract', async function() {
it('should return a boolean when validating an example contract', async function() {
const contract = await deployContract("example_contract_2/build/ExampleContract2Interface.json", "example_contract_2/build/example_contract_2.wasm");
assert(web3.utils.isAddress(contract.address), "The contract address should be a valid address")
const code_size = await kernel.methods.get_code_size(contract.address).call();
const code_hex = await kernel.methods.code_copy(contract.address).call();
const code = web3.utils.hexToBytes(code_hex);
assert.strictEqual(code_size, code.length, "The code length should be as given by EXTCODESIZE");
let rec_validation = await kernel.methods.check_contract(contract.address).call();
assert.strictEqual(rec_validation, true);
assert.strictEqual(typeof rec_validation, "boolean");
})
})
})
7 changes: 1 addition & 6 deletions kernel-ewasm/validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ authors = ["Daohub Inc <info@daohub.io>"]
edition = "2018"

[dependencies]
parity-wasm = { version = "0.35", default-features = false }
pwasm-std = {version = "0.13", default-features = false}
pwasm-ethereum = {version = "0.8", default-features = false}

[dev-dependencies]
wabt = "0.7.1"

[dev-dependencies.pwasm-test]
git = "https://github.com/paritytech/pwasm-test"
default-features = false

[features]
default = ["std"]
std = ["parity-wasm/std", "pwasm-std/std", "pwasm-ethereum/std", "pwasm-test/std"]
std = ["pwasm-std/std", "pwasm-ethereum/std"]

[lib]
name = "validator"
Expand Down

0 comments on commit 2f83b1c

Please sign in to comment.