Skip to content

Commit

Permalink
Runtime asynchrony tests (#1283)
Browse files Browse the repository at this point in the history
* Async runtime testing

* Get rid of shard_id

* Add first working tests for the runtime

* Fix after merge

* Separate tools from tests

* Add multi-cross-contract test

* Undo leaked code

* Fix storage usage assertions

* Fix evil test

* Update contract size
  • Loading branch information
MaksymZavershynskyi committed Sep 11, 2019
1 parent 3c89de3 commit 041adcc
Show file tree
Hide file tree
Showing 11 changed files with 863 additions and 36 deletions.
Binary file modified runtime/near-vm-runner/tests/res/test_contract_rs.wasm
Binary file not shown.
2 changes: 0 additions & 2 deletions runtime/near-vm-runner/tests/test-contract-rs/.cargo/config

This file was deleted.

58 changes: 36 additions & 22 deletions runtime/near-vm-runner/tests/test-contract-rs/Cargo.lock

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

19 changes: 12 additions & 7 deletions runtime/near-vm-runner/tests/test-contract-rs/Cargo.toml
@@ -1,18 +1,23 @@
[package]
name = "test-contract-rs"
version = "0.0.1"
version = "0.1.0"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
wee_alloc = { version = "0.4.4", default-features = false }

[workspace]
members = []
serde_json = "1.0"
wee_alloc = { version = "0.4.5", default-features = false }

[profile.release]
panic = "abort"
lto = true
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = true
debug = false
panic = "abort"

[workspace]
members = []
2 changes: 1 addition & 1 deletion runtime/near-vm-runner/tests/test-contract-rs/build.sh
@@ -1,5 +1,5 @@
#!/bin/bash

cargo +nightly build --target wasm32-unknown-unknown --release
RUSTFLAGS='-C link-arg=-s' cargo +nightly build --target wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/test_contract_rs.wasm ../res/
rm -rf target
71 changes: 71 additions & 0 deletions runtime/near-vm-runner/tests/test-contract-rs/src/lib.rs
Expand Up @@ -363,3 +363,74 @@ fn internal_recurse(n: u64) -> u64 {
internal_recurse(n - 1) + 1
}
}

// Can be used for debugging
#[no_mangle]
fn log_u64(msg: u64) {
unsafe {
log_utf8(8, &msg as *const u64 as u64);
}
}

#[no_mangle]
fn call_promise() {
unsafe {
input(0);
let data = vec![0u8; register_len(0) as usize];
read_register(0, data.as_ptr() as u64);
let input_args: serde_json::Value = serde_json::from_slice(&data).unwrap();
for arg in input_args.as_array().unwrap() {
let actual_id = if let Some(create) = arg.get("create") {
let account_id = create["account_id"].as_str().unwrap().as_bytes();
let method_name = create["method_name"].as_str().unwrap().as_bytes();
let arguments = serde_json::to_vec(&create["arguments"]).unwrap();
let amount = create["amount"].as_i64().unwrap() as u128;
let gas = create["gas"].as_i64().unwrap() as u64;
promise_create(
account_id.len() as u64,
account_id.as_ptr() as u64,
method_name.len() as u64,
method_name.as_ptr() as u64,
arguments.len() as u64,
arguments.as_ptr() as u64,
&amount as *const u128 as *const u64 as u64,
gas,
)
} else if let Some(then) = arg.get("then") {
let promise_index = then["promise_index"].as_i64().unwrap() as u64;
let account_id = then["account_id"].as_str().unwrap().as_bytes();
let method_name = then["method_name"].as_str().unwrap().as_bytes();
let arguments = serde_json::to_vec(&then["arguments"]).unwrap();
let amount = then["amount"].as_i64().unwrap() as u128;
let gas = then["gas"].as_i64().unwrap() as u64;
promise_then(
promise_index,
account_id.len() as u64,
account_id.as_ptr() as u64,
method_name.len() as u64,
method_name.as_ptr() as u64,
arguments.len() as u64,
arguments.as_ptr() as u64,
&amount as *const u128 as *const u64 as u64,
gas,
)
} else if let Some(and) = arg.get("and") {
let and = and.as_array().unwrap();
let mut curr = and[0].as_i64().unwrap() as u64;
for other in &and[1..] {
curr = promise_and(curr, other.as_i64().unwrap() as u64);
}
curr
} else {
unimplemented!()
};
let expected_id = arg["id"].as_i64().unwrap() as u64;
assert_eq!(actual_id, expected_id);
if let Some(ret) = arg.get("return") {
if ret.as_bool().unwrap() == true {
promise_return(actual_id);
}
}
}
}
}
1 change: 1 addition & 0 deletions runtime/runtime/Cargo.toml
Expand Up @@ -35,5 +35,6 @@ ethereum-rlp = "0.2"
ethereum-block = "0.3"
ethereum-hexutil = "0.2"
tempdir = "0.3"
serde_json = "1.0.40"

testlib = { path = "../../test-utils/testlib" }
4 changes: 2 additions & 2 deletions runtime/runtime/src/lib.rs
Expand Up @@ -317,7 +317,7 @@ impl Runtime {
})
}

pub fn process_transaction(
fn process_transaction(
&self,
state_update: &mut TrieUpdate,
apply_state: &ApplyState,
Expand Down Expand Up @@ -648,7 +648,7 @@ impl Runtime {
}
}

pub fn process_receipt(
fn process_receipt(
&self,
state_update: &mut TrieUpdate,
apply_state: &ApplyState,
Expand Down

0 comments on commit 041adcc

Please sign in to comment.