Skip to content

Commit

Permalink
impl all externals funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyTimes committed Jan 15, 2022
1 parent 576f340 commit 78737e4
Show file tree
Hide file tree
Showing 32 changed files with 3,013 additions and 49 deletions.
1,074 changes: 1,072 additions & 2 deletions enclave/app/Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions enclave/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ authors = ["SkyeKiwi <[email protected]>"]
build = "build.rs"

[dependencies]
skw_ipfs = { path = "../crates/skw-ipfs" }
sgx_types = { path = "../../teaclave-sgx-sdk/sgx_types" }
sgx_urts = { path = "../../teaclave-sgx-sdk/sgx_urts" }
5 changes: 5 additions & 0 deletions enclave/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ fn main() {
},
};

const CONTENT: &str = "some random string ...";
let result = skw_ipfs::IpfsClient::add(CONTENT.as_bytes().to_vec()).unwrap();
let recovered = skw_ipfs::IpfsClient::cat(result.cid).unwrap();
println!("{:?}", String::from_utf8(recovered));

let input_string = String::from("This is a normal world string passed into Enclave!\n");
let mut retval = sgx_status_t::SGX_SUCCESS;

Expand Down
2 changes: 1 addition & 1 deletion enclave/crates/skw-ipfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl IpfsClient {

#[test]
fn ipfs_works() {
const CONTENT: &'static str = "some random string ...";
const CONTENT: &str = "some random string ...";

let result = IpfsClient::add(CONTENT.as_bytes().to_vec()).unwrap();
let recovered = IpfsClient::cat(result.cid).unwrap();
Expand Down
13 changes: 13 additions & 0 deletions mock-enclave/src/near-test-contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "near-test-contracts"
version = "0.0.0"
publish = false
authors = ["Near Inc <[email protected]>"]
# Please update rust-toolchain.toml as well when changing version here:
rust-version = "1.56.0"
edition = "2021"
license = "Apache-2.0"

[dependencies]
once_cell = "1"
wat = "1.0.40"
21 changes: 21 additions & 0 deletions mock-enclave/src/near-test-contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
A collection of smart-contract used in nearcore tests.

Rust contracts are built via `build.rs`, the Assembly Script contract
is build manually and committed to the git repository.
`res/near_evm.wasm` and `res/ZombieOwnership.bin` are taken from
<https://github.com/near/near-evm/tree/a651e9be680b59cca9aad86c1f9e9e9b54ad9c06>
and it's for reproduce a performance issue encountered in EVM
contracts.

If you want to use a contract from rust core, add

```toml
[dev-dependencies]
near-test-contracts = { path = "../near-test-contracts" }
```

to the Cargo.toml and use `near_test_contract::rs_contract()`.

If you want to use a contract from an integration test, you can read
the wasm file directly from the `./res` directory. To populate
`./res`, you need to make sure that this crate was compiled.
79 changes: 79 additions & 0 deletions mock-enclave/src/near-test-contracts/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::{env, fs, io, process};

fn main() {
if let Err(err) = try_main() {
eprintln!("{}", err);
process::exit(1);
}
}

fn try_main() -> io::Result<()> {
build_contract("./test-contract-rs", &[], "test_contract_rs")?;
build_contract(
"./test-contract-rs",
&["--features", "nightly_protocol_features"],
"nightly_test_contract_rs",
)?;
build_contract("./contract-for-fuzzing-rs", &[], "contract_for_fuzzing_rs")?;
build_contract(
"./test-contract-rs",
&["--features", "base_protocol"],
"test_contract_rs_base_protocol",
)?;
Ok(())
}

fn build_contract(dir: &str, args: &[&str], output: &str) -> io::Result<()> {
let mut cmd = cargo_build_cmd();
cmd.args(args);
cmd.current_dir(dir);
check_status(cmd)?;

let target_dir = shared_target_dir().unwrap_or_else(|| format!("./{}/target", dir).into());
fs::copy(
target_dir.join(format!("wasm32-unknown-unknown/release/{}.wasm", dir.replace('-', "_"))),
format!("./res/{}.wasm", output),
)?;
println!("cargo:rerun-if-changed=./{}/src/lib.rs", dir);
println!("cargo:rerun-if-changed=./{}/Cargo.toml", dir);
Ok(())
}

fn cargo_build_cmd() -> Command {
let mut res = Command::new("cargo");
res.env("RUSTFLAGS", "-C link-arg=-s");
res.env_remove("CARGO_ENCODED_RUSTFLAGS");
if let Some(target_dir) = shared_target_dir() {
res.env("CARGO_TARGET_DIR", target_dir);
}

res.args(&["build", "--target=wasm32-unknown-unknown", "--release"]);
res
}

fn check_status(mut cmd: Command) -> io::Result<()> {
let status = cmd.status().map_err(|err| {
io::Error::new(io::ErrorKind::Other, format!("command `{:?}` failed to run: {}", cmd, err))
})?;
if !status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("command `{:?}` exited with non-zero status: {:?}", cmd, status),
));
}
Ok(())
}

fn shared_target_dir() -> Option<PathBuf> {
let target_dir = env::var("CARGO_TARGET_DIR").ok()?;
// Avoid sharing the same target directory with the patent Cargo
// invocation, to avoid deadlock on the target dir.
//
// That is, this logic is needed for the case like the following:
//
// CARGO_TARGET_DIR=/tmp cargo build -p near-test-contracts --release
Some(Path::new(&target_dir).join("near-test-contracts"))
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "contract-for-fuzzing-rs"
version = "0.1.0"
authors = ["Near Inc <[email protected]>"]
publish = false
# Please update rust-toolchain.toml as well when changing version here:
rust-version = "1.56.0"
edition = "2018"

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

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

[workspace]
members = []
Loading

0 comments on commit 78737e4

Please sign in to comment.