Skip to content

Commit

Permalink
Merge pull request #1250 from scrtlabs/wasm3-cw1
Browse files Browse the repository at this point in the history
Wasm3 CW1
  • Loading branch information
Cashmaney authored Nov 8, 2022
2 parents 943b3cd + 570417e commit 621d389
Show file tree
Hide file tree
Showing 58 changed files with 3,389 additions and 554 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,4 @@ jobs:
secretcli-macOS
secretcli-Windows
secretcli-Linux
secretcli-MacOS-arm64
secretcli-MacOS-arm64
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "third_party/incubator-teaclave-sgx-sdk"]
path = third_party/incubator-teaclave-sgx-sdk
url = https://github.com/scrtlabs/incubator-teaclave-sgx-sdk
branch = secret-1.1.5
branch = master
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ callback-sanity-test:
cp ./$(EXECUTE_ENCLAVE_PATH)/librust_cosmwasm_enclave.signed.so .
SGX_MODE=SW ./cosmwasm/testing/callback-test.sh

build-bench-contract:
$(MAKE) -C $(TEST_CONTRACT_V1_PATH)/bench-contract
cp $(TEST_CONTRACT_V1_PATH)/bench-contract/*.wasm $(TEST_COMPUTE_MODULE_PATH)/

build-test-contract:
# echo "" | sudo add-apt-repository ppa:hnakamur/binaryen
# sudo apt update
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: src/contract.rs src src src Cargo.toml Cargo.lock
rustup target add wasm32-unknown-unknown
RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown
cp ./target/wasm32-unknown-unknown/release/v1_sanity_contract.wasm ./v1-contract.wasm
cp ./target/wasm32-unknown-unknown/release/bench_contract.wasm ./bench_contract.wasm

clean:
cargo clean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::{StdError, StdResult};

pub fn do_allocate_large_memory() -> StdResult<()> {
// We create memory pages explicitely since Rust's default allocator seems to be clever enough
// We create memory pages explicitly since Rust's default allocator seems to be clever enough
// to not grow memory for unused capacity like `Vec::<u8>::with_capacity(100 * 1024 * 1024)`.
// Even with std::alloc::alloc the memory did now grow beyond 1.5 MiB.

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use cosmwasm_std::{Deps, DepsMut, StdResult};

// as long as keys is > 10 the single write shouldn't produce high enough variance
// if anyone wants to they can set up test.key in the go side of things to make it more accurate
pub fn bench_read_storage_same_key(deps: DepsMut, keys: u64) -> StdResult<()> {
deps.storage.set(b"test.key", b"test.value");

Expand All @@ -10,12 +12,21 @@ pub fn bench_read_storage_same_key(deps: DepsMut, keys: u64) -> StdResult<()> {
Ok(())
}

#[allow(dead_code)]
/// call this test only after the bench of write storage, so the keys are populated
pub fn bench_read_storage_different_key(deps: Deps, keys: u64) -> StdResult<()> {
/// call this test only after setting up the test with write storage, so the keys are populated
pub fn bench_read_storage_different_key(deps: DepsMut, keys: u64) -> StdResult<()> {
for i in 1..keys {
deps.storage.get(&i.to_be_bytes()).unwrap();
}

Ok(())
}

/// call this test only after setting up the test with write storage, so the keys are populated
pub fn bench_read_large_key_from_storage(deps: DepsMut, keys: u64) -> StdResult<()> {
// deps.storage.set(b"test.key", crate::benches::LARGE_VALUE);
for _ in 1..keys {
deps.storage.get(b"test.key");
}

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ pub fn bench_write_storage_different_key(deps: DepsMut, keys: u64) -> StdResult<

Ok(())
}

pub fn bench_write_large_storage_key(deps: DepsMut, keys: u64) -> StdResult<()> {
for i in 1..keys {
deps.storage
.set(&i.to_be_bytes(), crate::benches::LARGE_VALUE);
}

Ok(())
}
17 changes: 13 additions & 4 deletions cosmwasm/contracts/v1/compute-tests/bench-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ use benches::cpu::do_cpu_loop;

use crate::benches;
use crate::benches::allocate::do_allocate_large_memory;
use crate::benches::read_storage::bench_read_storage_same_key;
use crate::benches::write_storage::bench_write_storage_different_key;
use crate::benches::read_storage::{
bench_read_large_key_from_storage, bench_read_storage_different_key,
bench_read_storage_same_key,
};
use crate::benches::write_storage::{
bench_write_large_storage_key, bench_write_storage_different_key,
};
use cosmwasm_std::{
entry_point, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdResult,
};
Expand Down Expand Up @@ -32,9 +37,13 @@ pub fn execute(
ExecuteMsg::BenchCPU {} => do_cpu_loop(5000),
ExecuteMsg::BenchReadStorage {} => bench_read_storage_same_key(deps, 100),
ExecuteMsg::BenchWriteStorage {} => bench_write_storage_different_key(deps, 100),
ExecuteMsg::BenchReadStorageMultipleKeys {} => bench_read_storage_different_key(deps, 100),
ExecuteMsg::BenchAllocate {} => do_allocate_large_memory(),
ExecuteMsg::BenchReadLargeItemFromStorage { .. } => Ok(()),
ExecuteMsg::BenchWriteLargeItemToStorage { .. } => Ok(()),
// start with running large item bench once, otherwise cache will skew performance numbers
ExecuteMsg::BenchReadLargeItemFromStorage { .. } => {
bench_read_large_key_from_storage(deps, 2)
}
ExecuteMsg::BenchWriteLargeItemToStorage { .. } => bench_write_large_storage_key(deps, 2),
};

Ok(Response::default())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum ExecuteMsg {
BenchCPU {},
BenchReadStorage {},
BenchWriteStorage {},
BenchReadStorageMultipleKeys {},
BenchAllocate {},
BenchReadLargeItemFromStorage {},
BenchWriteLargeItemToStorage {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2494,65 +2494,58 @@ fn pass_null_pointer_to_imports_should_throw(deps: DepsMut, pass_type: String) -
fn test_canonicalize_address_errors(deps: DepsMut) -> StdResult<Response> {
match deps.api.addr_canonicalize("") {
Err(StdError::GenericErr { msg }) => {
if msg != String::from("addr_canonicalize errored: Input is empty") {
return Err(StdError::generic_err(
"empty address should have failed with 'addr_canonicalize errored: Input is empty'",
));
if !msg.to_lowercase().contains("input is empty") {
return Err(StdError::generic_err(format!(
"empty address should have failed with 'addr_canonicalize errored: Input is empty; got {:?}'",
msg)));
}
// all is good, continue
}
_ => {
other => {
return Err(StdError::generic_err(
"empty address should have failed with 'addr_canonicalize errored: Input is empty'",
format!("empty address should have failed with 'addr_canonicalize errored: Input is empty', instead was {:?}", other),
))
}
}

match deps.api.addr_canonicalize(" ") {
Err(StdError::GenericErr { msg }) => {
if msg != String::from("addr_canonicalize errored: invalid length") {
return Err(StdError::generic_err(
"empty trimmed address should have failed with 'addr_canonicalize errored: invalid length'",
));
if !msg.to_lowercase().contains("invalid length") {
return Err(StdError::generic_err(format!(
"empty trimmed address should have failed with 'addr_canonicalize errored: invalid length; got {:?}'",
msg)));
}
// all is good, continue
}
_ => {
other => {
return Err(StdError::generic_err(
"empty trimmed address should have failed with 'addr_canonicalize errored: invalid length'",
format!("empty trimmed address should have failed with 'addr_canonicalize errored: invalid length', instead was: {:?}", other),
))
}
}

match deps.api.addr_canonicalize("cosmos1h99hrcc54ms9lxxxx") {
Err(StdError::GenericErr { msg }) => {
if msg != String::from("addr_canonicalize errored: invalid checksum") {
return Err(StdError::generic_err(
"bad bech32 should have failed with 'addr_canonicalize errored: invalid checksum'",
));
}
Err(StdError::GenericErr { msg })
if msg == String::from("addr_canonicalize errored: invalid checksum") =>
{
// all is good, continue
}
_ => {
other => {
return Err(StdError::generic_err(
"bad bech32 should have failed with 'addr_canonicalize errored: invalid checksum'",
format!("bad bech32 should have failed with 'addr_canonicalize errored: invalid checksum', instead was {:?}", other),
))
}
}

match deps.api.addr_canonicalize("cosmos1h99hrcc54ms9luwpex9kw0rwdt7etvfdyxh6gu") {
Err(StdError::GenericErr { msg }) => {
if msg != String::from("addr_canonicalize errored: wrong address prefix: \"cosmos\"")
{
return Err(StdError::generic_err(
"bad prefix should have failed with 'addr_canonicalize errored: wrong address prefix: \"cosmos\"'",
));
}
Err(StdError::GenericErr { msg })
if msg == String::from("addr_canonicalize errored: wrong address prefix: \"cosmos\"") =>
{
// all is good, continue
}
_ => {
other => {
return Err(StdError::generic_err(
"bad prefix should have failed with 'addr_canonicalize errored: wrong address prefix: \"cosmos\"'",
format!("bad prefix should have failed with 'addr_canonicalize errored: wrong address prefix: \"cosmos\"', instead was {:?}", other),
))
}
}
Expand Down
1 change: 1 addition & 0 deletions cosmwasm/enclaves/Xargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ std = { git = "https://github.com/apache/teaclave-sgx-sdk.git", rev = "c70a82f70
"net",
"backtrace",
"untrusted_fs",
"untrusted_time"
] }
sgx_no_tstd = { path = "../../third_party/incubator-teaclave-sgx-sdk/sgx_no_tstd", stage = 5 }
sgx_rand = { path = "../../third_party/incubator-teaclave-sgx-sdk/sgx_rand", stage = 6 }
Expand Down
3 changes: 2 additions & 1 deletion cosmwasm/enclaves/execute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ test = ["enclave_contract_engine/test", "enclave_crypto/test", "enclave_cosmos_t
# when compiling to the "sgx" target, we pull this from the target root with an "extern crate" directive
[target.'cfg(not(target_env = "sgx"))'.dependencies]
sgx_tstd = { rev = "d2d339cbb005f676bb700059bd51dc689c025f6b", git = "https://github.com/apache/teaclave-sgx-sdk.git", features = [
"backtrace"
"backtrace",
"untrusted_time"
] }
sgx_types = { rev = "d2d339cbb005f676bb700059bd51dc689c025f6b", git = "https://github.com/apache/teaclave-sgx-sdk.git" }

Expand Down
8 changes: 8 additions & 0 deletions cosmwasm/enclaves/execute/Enclave.edl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ enclave {
uintptr_t key_len
);

OcallReturn ocall_multiple_write_db(
Ctx context,
[out] UntrustedVmError* vm_error,
[out] uint64_t* gas_used,
[in, count=keys_len] const uint8_t* keys,
uintptr_t keys_len
);

OcallReturn ocall_write_db(
Ctx context,
[out] UntrustedVmError* vm_error,
Expand Down
1 change: 0 additions & 1 deletion cosmwasm/enclaves/execute/src/registration/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::io::BufReader;
use std::str;
use std::time::{SystemTime, UNIX_EPOCH};
use std::untrusted::time::SystemTimeEx;

use sgx_tcrypto::SgxEccHandle;
use sgx_types::{
Expand Down
3 changes: 3 additions & 0 deletions cosmwasm/enclaves/ffi-types/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ pub enum EnclaveError {
/// The contract has run out of space on the stack.
#[display(fmt = "the contract has run out of space on the stack")]
ContractPanicStackOverflow,
/// The contract performed integer overflow.
#[display(fmt = "the contract has run out of space on the stack")]
ContractPanicIntegerOverflow,
/// The contract tried to call a function but expected an incorrect function signature.
#[display(
fmt = "the contract tried to call a function but expected an incorrect function signature"
Expand Down
15 changes: 12 additions & 3 deletions cosmwasm/enclaves/shared/contract-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ authors = ["Cashmaney <[email protected]>"]
edition = "2018"

[features]
default = ["wasm3"]
query-only = []
debug-print = []
test = []
wasm3 = []
wasmi-engine = ["wasmi", "parity-wasm", "pwasm-utils"]

# This annotation is here to trick the IDE into showing us type information about this crate.
# We always compile to the "sgx" target, so this will always be false.
Expand All @@ -27,6 +30,7 @@ cw_types_v010 = { path = "../cosmwasm-types/v0.10" }
cw_types_v1 = { path = "../cosmwasm-types/v1.0" }
cw_types_generic = { path = "../cosmwasm-types/generic" }
enclave_utils = { path = "../utils" }

serde = { git = "https://github.com/mesalock-linux/serde-sgx", features = [
"derive"
] }
Expand All @@ -38,11 +42,15 @@ log = "0.4.14"
derive_more = "0.99"
sha2 = "0.8.1"
bech32 = "0.7.2"
pwasm-utils = { version = "0.12.0", default-features = false }
parity-wasm = { version = "0.41.0", default-features = false }

pwasm-utils = { version = "0.12.0", default-features = false, optional = true }
parity-wasm = { version = "0.41.0", default-features = false, optional = true }
wasm3 = { git = "https://github.com/scrtlabs/wasm3-rs", rev = "ad1c868" }
walrus = { version = "0.19.0", git = "https://github.com/scrtlabs/walrus", rev = "c5777d4" }

lru = { version = "0.6", default-features = false }
hex = "0.4.2"
secp256k1 = { version = "0.21.3", features = ["recovery"] }
secp256k1 = { version = "0.24.0", features = ["recovery", "alloc"] }
ed25519-zebra = { version = "=2.2.0", default-features = false }
rand_core = "0.5.0"
rand_chacha = { version = "0.2.1", default-features = false }
Expand All @@ -51,4 +59,5 @@ rand_chacha = { version = "0.2.1", default-features = false }
git = "https://github.com/paritytech/wasmi"
rev = "84d2764594d80425373bf4949a58fa3df3d624c3"
default-features = false
optional = true
features = ["core"]
Loading

0 comments on commit 621d389

Please sign in to comment.