Skip to content
This repository was archived by the owner on Feb 14, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Growing collection of varios WASM compilable contract examples, both source and
### Rust

- Rust with `wasm32-unknown-unknown` target
- wasm-build util, run `cargo install pwasm-utils` to install
- bash to run `./build.sh`
- wasm-build util, run `cargo install pwasm-utils-cli` to install
- bash to run `./build-all.sh`

### C/C++
- to be updated
Expand Down
4 changes: 2 additions & 2 deletions build-all.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

./build-rust-test.sh alloc
./build-rust-test.sh call
./build-rust-test.sh call_code
./build-rust-test.sh call_static
./build-rust-test.sh creator
./build-rust-test.sh creator kip4
./build-rust-test.sh dispersion
./build-rust-test.sh empty
./build-rust-test.sh externs
Expand Down
9 changes: 5 additions & 4 deletions build-rust-test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

TEST_NAME=$1
ADDITIONAL_FEATURES=$2

cargo run --manifest-path ./gen/Cargo.toml -- $TEST_NAME
cargo run --manifest-path ./gen/Cargo.toml -- $TEST_NAME $ADDITIONAL_FEATURES
RUSTFLAGS="-C link-arg=-z -C link-arg=stack-size=65536" CARGO_TARGET_DIR=./target cargo build --manifest-path=./target/tests/$TEST_NAME/Cargo.toml --release --target=wasm32-unknown-unknown
wasm-build ./target $TEST_NAME --target wasm32-unknown-unknown
cp ./target/$TEST_NAME.wasm ./compiled
wasm-build ./target $TEST_NAME --save-raw ./target/$TEST_NAME.raw.wasm --target wasm32-unknown-unknown
cp ./target/$TEST_NAME.raw.wasm ./compiled/$TEST_NAME.wasm
2 changes: 1 addition & 1 deletion build-wat.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

set -e

Expand Down
Binary file modified compiled/alloc.wasm
Binary file not shown.
Binary file modified compiled/creator.wasm
Binary file not shown.
Binary file modified compiled/events.wasm
Binary file not shown.
Binary file modified compiled/externs.wasm
Binary file not shown.
Binary file modified compiled/keccak.wasm
Binary file not shown.
Binary file modified compiled/math.wasm
Binary file not shown.
27 changes: 15 additions & 12 deletions gen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ use std::path::PathBuf;

fn main() {
let args = env::args().collect::<Vec<_>>();
if args.len() != 2 {
println!("Usage: {} gen <test.rs>", args[0]);
return;
}

let file_name = &args[1];

let toml = r#"
let (file_name, pwasm_ethereum_version, pwasm_std_version) = match args.len() {
2 => (&args[1], r#""0.5.0""#.to_string(), r#""0.9.0""#),
3 => (&args[1], format!(r#"{{ git = "https://github.com/paritytech/pwasm-ethereum", branch = "kip4", features = [{}] }}"#, args[2].split(",").map(|s| format!(r#""{}""#, s)).collect::<Vec<_>>().join(", ")), r#""0.10.0""#),
_ => {
println!("Usage: {} gen <test.rs>", args[0]);
return;
},
};

let toml = format!(r#"
[package]
name = "wasm-tests"
version = "0.1.0"
authors = ["NikVolf <[email protected]>"]

[dependencies]
pwasm-std = "0.9.0"
pwasm-ethereum = "0.5.0"
bigint = { version = "4", default-features = false }
pwasm-std = {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

could just be updated to 0.10 for all?

pwasm-ethereum = {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

could be just 0.6 ?

bigint = {{ version = "4", default-features = false }}
parity-hash = {{ version = "1", default-features = false }}

[lib]
name = "$file_name"
Expand All @@ -30,7 +33,7 @@ crate-type = ["cdylib"]
panic = "abort"
lto = true
opt-level = "z"
"#;
"#, pwasm_std_version, pwasm_ethereum_version);

let target_toml = toml.replace("$file_name", file_name);

Expand Down
5 changes: 4 additions & 1 deletion src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ pub fn call() {
data.extend_from_slice(&[5u8; 400*1024][..]);
data
});
}
}

#[no_mangle]
pub fn deploy() { }
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why these are needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wasm-build would complain if no deploy symbol is found: https://github.com/paritytech/wasm-utils/blob/master/src/build.rs#L110
In the binary, we always set constructor to true and build the deploy wrapped version.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@sorpaas

there is a bug in wasm-build 0.3.0 with this constructor=true always

you might try pwasm-utils-cli 0.2.0 (cargo install pwasm-utils-cli --version 0.2.0) or wait until @fckt fix it

Copy link
Contributor

Choose a reason for hiding this comment

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

3 changes: 3 additions & 0 deletions src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ pub fn call() {

ret(&[]);
}

#[no_mangle]
pub fn deploy() { }
3 changes: 3 additions & 0 deletions src/call_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ pub fn call() {

ret(&res[..]);
}

#[no_mangle]
pub fn deploy() { }
3 changes: 3 additions & 0 deletions src/call_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ pub fn call() {
logger::debug("Exiting...");
ret(&res[..]);
}

#[no_mangle]
pub fn deploy() { }
25 changes: 21 additions & 4 deletions src/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@

extern crate pwasm_std;
extern crate pwasm_ethereum;
extern crate bigint;
extern crate parity_hash as hash;

use pwasm_std::logger;
use pwasm_ethereum::{input, ret, create, value};
use pwasm_ethereum::{input, ret, create, create2, value};
use bigint::U256;
use hash::H256;

#[no_mangle]
pub fn call() {
if let Ok(addr) = create(value(), &input()) {
logger::debug("Created contractwith code");
ret(&addr[..]);
let mut r = [0u8; 40];
if let Ok(addr) = create(value() / U256::from(2), &input()) {
logger::debug("Created contract with code");
(&mut r[0..20]).copy_from_slice(&addr[..]);
} else {
logger::debug("Error creating contract");
}

if let Ok(addr) = create2(value() / U256::from(2), H256::default(), &input()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe let's use some non-zero salt to actually test the argument reaches the runtime after?

logger::debug("Created contract with code and salt");
(&mut r[0..20]).copy_from_slice(&addr[..]);
} else {
logger::debug("Error creating contract");
}

ret(&r[..]);
}

#[no_mangle]
pub fn deploy() { }
5 changes: 4 additions & 1 deletion src/dispersion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ pub fn call() {
}

ret(&dispersed[..])
}
}

#[no_mangle]
pub fn deploy() { }
Copy link
Collaborator

@NikVolf NikVolf Aug 6, 2018

Choose a reason for hiding this comment

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

there should not be deploy in any of those tests

with deploy, they will get packed as constructors, while they need to get packed as raw wasm module

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NikVolf With no deploy wasm-build would refuse to pack those modules: https://github.com/paritytech/wasm-utils/blob/master/src/build.rs#L110

Copy link
Collaborator

Choose a reason for hiding this comment

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

should be fixed now

Copy link
Collaborator

Choose a reason for hiding this comment

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

(reinstall pwasm-utils-cli)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed all deploy symbols!

5 changes: 4 additions & 1 deletion src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ use pwasm_std::logger;
#[no_mangle]
pub fn call() {
logger::debug("Empty contract");
}
}

#[no_mangle]
pub fn deploy() { }
5 changes: 4 additions & 1 deletion src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ pub fn call() {
ext::log(&[hash, H256::from(&reverse_hash[..])], &reverse_input);

ext::ret(&reverse_input);
}
}

#[no_mangle]
pub fn deploy() { }
3 changes: 3 additions & 0 deletions src/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ pub fn call() {

ext::ret(&output);
}

#[no_mangle]
pub fn deploy() { }
5 changes: 4 additions & 1 deletion src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ extern crate pwasm_ethereum as ext;
#[no_mangle]
pub fn call() {
ext::ret(&ext::sender()[..]);
}
}

#[no_mangle]
pub fn deploy() { }
3 changes: 3 additions & 0 deletions src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ use pwasm_ethereum::{ret, input};
pub fn call() {
ret(&keccak(&input()));
}

#[no_mangle]
pub fn deploy() { }
3 changes: 3 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ pub fn call() {
set_key_from_addr(3, &ext::origin());
set_key_from_u256(4, ext::value());
}

#[no_mangle]
pub fn deploy() { }
5 changes: 4 additions & 1 deletion src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ pub fn call() {

result_bytes
})
}
}

#[no_mangle]
pub fn deploy() { }
5 changes: 4 additions & 1 deletion src/realloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ pub fn call() {
}
data
});
}
}

#[no_mangle]
pub fn deploy() { }
5 changes: 4 additions & 1 deletion src/rterr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ use pwasm_std::logger;
pub fn call() {
logger::debug("Exception will occur here:");
unreachable!();
}
}

#[no_mangle]
pub fn deploy() { }
5 changes: 4 additions & 1 deletion src/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ pub fn call() {
val.copy_from_slice(&ext::input()[32+i*64..64+i*64]);
ext::write(&key, &val);
}
}
}

#[no_mangle]
pub fn deploy() { }
3 changes: 3 additions & 0 deletions src/storage_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pub fn call() {
let val: [u8; 32] = get_value_from_key(1);
ret(&val[..])
}

#[no_mangle]
pub fn deploy() { }
5 changes: 4 additions & 1 deletion src/suicidal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ pub fn call() {
} else {
ret(&input);
}
}
}

#[no_mangle]
pub fn deploy() { }
2 changes: 1 addition & 1 deletion wasm-install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

# this script is intended to be used from .travis.yml.
# Takes an environment variable WATERFALL_BUILD to download a
Expand Down