Skip to content

Commit

Permalink
Merge branch 'main' into feature/erc721-enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
bidzyyys committed May 29, 2024
2 parents ab042c1 + 5f3915e commit 3afa579
Show file tree
Hide file tree
Showing 31 changed files with 848 additions and 397 deletions.
32 changes: 32 additions & 0 deletions .github/scripts/check-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
set -e

mydir=$(dirname "$0")
cd "$mydir" || exit
cd ../..

# Check contract wasm binary by crate name
check_wasm () {
local CONTRACT_CRATE_NAME=$1
local CONTRACT_BIN_NAME="${CONTRACT_CRATE_NAME//-/_}.wasm"

echo
echo "Checking contract $CONTRACT_CRATE_NAME"
cargo stylus check --wasm-file-path ./target/wasm32-unknown-unknown/release/"$CONTRACT_BIN_NAME"
}

# Retrieve all alphanumeric contract's crate names in `./examples` directory.
get_example_crate_names () {
# shellcheck disable=SC2038
# NOTE: optimistically relying on the 'name = ' string at Cargo.toml file
find ./examples -type f -name "Cargo.toml" | xargs grep 'name = ' | grep -oE '".*"' | tr -d "'\""
}

NIGHTLY_TOOLCHAIN=${NIGHTLY_TOOLCHAIN:-nightly}

cargo +"$NIGHTLY_TOOLCHAIN" build --release --target wasm32-unknown-unknown -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort

for CRATE_NAME in $(get_example_crate_names)
do
check_wasm "$CRATE_NAME"
done
56 changes: 56 additions & 0 deletions .github/workflows/check-wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: check-wasm
# This workflow checks that the compiled wasm binary of every example contract
# can be deployed to Arbitrum Stylus.
#
# NOTE: This is using version `0.2.1` of `cargo-stylus`, which we will need
# to update once `nitro-testnode` gets updated.
permissions:
contents: read
on:
push:
branches: [ main ]
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
env:
CARGO_TERM_COLOR: always
jobs:
check-wasm:
name: check WASM binary
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }}

- name: cache cargo-stylus
uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/.crates.toml
key: ${{ runner.os }}[email protected]
save-always: true

- name: set up rust
uses: dtolnay/rust-toolchain@master
id: toolchain
with:
target: wasm32-unknown-unknown
components: rust-src
toolchain: nightly-2024-01-01

- name: install cargo-stylus
run: RUSTFLAGS="-C link-args=-rdynamic" cargo install [email protected]

- name: run wasm check
run: |
export NIGHTLY_TOOLCHAIN=${{steps.toolchain.outputs.name}}
./.github/scripts/check-wasm.sh
20 changes: 3 additions & 17 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ name: e2e-tests
#
# It roughly follows these steps:
# - A local `nitro-testnode` gets spun up.
# - Contracts get deployed to the local node.
# - A few addresses get funded.
# - The test suite runs.
# - The test suite runs
# Test contract deployments and test user funding happen per test.
permissions:
contents: read
on:
Expand Down Expand Up @@ -53,20 +52,7 @@ jobs:
run: RUSTFLAGS="-C link-args=-rdynamic" cargo install [email protected]

- name: setup nitro node
run: |
# clone nitro test node repo
git clone -b stylus --recurse-submodules https://github.com/OffchainLabs/nitro-testnode.git && cd nitro-testnode
git checkout 1886f4b89f5c20fd5b0c2cf3d08a009ee73e45ca
# setup nitro test node
./test-node.bash --no-run --init --no-tokenbridge
./test-node.bash --detach
# TODO: remove hard coded wallets when user creation will be per test case
# fund Alice's wallet
./test-node.bash script send-l2 --to address_0x01fA6bf4Ee48B6C95900BCcf9BEA172EF5DBd478 --ethamount 10000
# fund Bob's wallet
./test-node.bash script send-l2 --to address_0xF4EaCDAbEf3c8f1EdE91b6f2A6840bc2E4DD3526 --ethamount 10000
run: ./e2e-tests/nitro-testnode.sh -d -i

- name: run integration tests
run: |
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
docs/build/

**/.DS_Store

/e2e-tests/nitro-testnode
24 changes: 23 additions & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ members = [
"examples/ownable",
"examples/access-control",
"e2e-tests",
"lib/e2e",
"lib/e2e-proc",
]
default-members = [
"contracts",
Expand All @@ -21,6 +23,7 @@ default-members = [
"examples/merkle-proofs",
"examples/ownable",
"examples/access-control",
"lib/e2e-proc",
]

# Explicitly set the resolver to version 2, which is the default for packages
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository.workspace = true
tokio = { version = "1.12.0", features = ["full"] }
ethers = "2.0"
eyre = "0.6.8"
async-trait = "0.1.80"
e2e = { path = "../lib/e2e" }

[lib]
doctest = false
42 changes: 12 additions & 30 deletions e2e-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,31 @@

## Run tests

Deploy every contract from `./examples` directory and running integration tests.
### Setup local nitro node

### Against local nitro node

Set up first a local nitro node according to
this [guide](https://github.com/OffchainLabs/nitro-testnode/blob/release/README.md)
Run in detached mode:

```terminal
# setup nitro test node in detached mode
# docker images should be shutdown manually later
./test-node.bash --no-run --init --no-tokenbridge
./test-node.bash --detach
# fund Alice's wallet
./test-node.bash script send-l2 --to address_0x01fA6bf4Ee48B6C95900BCcf9BEA172EF5DBd478 --ethamount 10000
# fund Bob's wallet
./test-node.bash script send-l2 --to address_0xF4EaCDAbEf3c8f1EdE91b6f2A6840bc2E4DD3526 --ethamount 10000
./nitro-testnode -d
```

Run integration testing command:
Clean local nitro node to free system resources:

```terminal
./e2e-tests/test.sh
./nitro-testnode -down
```

### Against stylus dev net
### Run end-to-end tests

`ALICE_PRIV_KEY` and `BOB_PRIV_KEY` should be valid funded wallets.
`RPC_URL` should contain url of the stylus testnet.
Run this command:
Builds every contract with entrypoint and run tests against locally deployed nitro node:

```terminal
ALICE_PRIV_KEY=0x... \
BOB_PRIV_KEY=0x... \
RPC_URL=https://stylus-testnet.arbitrum.io/rpc \
./e2e-tests//test.sh
./test.sh
```

## Add test for the new contract

Assuming that contract associated crate exists at `./examples` directory
with the crate name `erc20-example`.
Add ethereum contracts to `./e2e-tests/src/context` directory like:
Add solidity abi:

```rust
ethers::contract::abigen!(
Expand All @@ -58,15 +40,15 @@ ethers::contract::abigen!(
);
```

Then add wrapper type for the contract:
Then add wrapper type for the contract and link to an example crate name:

```rust
pub type Erc20 = Erc20Token<HttpMiddleware>;
link_to_crate!(Erc20, "erc20-example");
```

Tests should create new infrastructure instance like this:
Tests should instantiate new contract like this:

```rust
let context = E2EContext::<Erc20>::new().await?;
let erc20 = &alice.deploys::<Erc20>().await?;
```
30 changes: 30 additions & 0 deletions e2e-tests/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::path::{Path, PathBuf};

fn main() {
set_env("TARGET_DIR", &get_target_dir());
set_env("RPC_URL", "http://localhost:8547");
set_env(
"TEST_NITRO_NODE_PATH",
Path::new(&load_env_var("CARGO_MANIFEST_DIR"))
.join("nitro-testnode")
.to_str()
.expect("set env var TEST_NITRO_NODE_PATH"),
);
}

fn set_env(var_name: &str, value: &str) {
println!("cargo:rustc-env={}={}", var_name, value);
}

fn load_env_var(var_name: &str) -> String {
std::env::var(var_name)
.unwrap_or_else(|_| panic!("failed to load {} env var", var_name))
}

fn get_target_dir() -> String {
// should be smth like
// ./rust-contracts-stylus/target/debug/build/e2e-tests-b008947425bb8267/out
let out_dir = load_env_var("OUT_DIR");
let target_dir = Path::new(&out_dir).join("../../../../");
target_dir.to_str().expect("target dir").to_string()
}
58 changes: 58 additions & 0 deletions e2e-tests/nitro-testnode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

MYDIR=$(realpath "$(dirname "$0")")
cd "$MYDIR" || exit

HAS_INIT=false
HAS_DETACH=false

while [[ $# -gt 0 ]]
do
case "$1" in
-i|--init)
HAS_INIT=true
shift
;;
-d|--detach)
HAS_DETACH=true
shift
;;
-down|--shutdown)
docker container stop "$(docker container ls -q --filter name=nitro-testnode)"
exit 0
;;
*)
echo "OPTIONS:"
echo "-i|--init: clone repo and init nitro test node"
echo "-d|--detach: setup nitro test node in detached mode"
echo "-down|--shutdown: shutdown nitro test node docker containers"
exit 0
;;
esac
done

TEST_NODE_DIR="$MYDIR/nitro-testnode"
if [ ! -d "$TEST_NODE_DIR" ]; then
HAS_INIT=true
fi

if $HAS_INIT
then
cd "$MYDIR" || exit
# clone nitro test node repo
git clone -b stylus --recurse-submodules https://github.com/OffchainLabs/nitro-testnode.git
cd ./nitro-testnode || exit
git checkout 1886f4b89f5c20fd5b0c2cf3d08a009ee73e45ca || exit

# setup nitro test node
./test-node.bash --no-run --init --no-tokenbridge || exit
fi


cd "$TEST_NODE_DIR" || exit
if $HAS_DETACH
then
./test-node.bash --detach
else
./test-node.bash
fi
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use ethers::prelude::*;

use crate::context::*;
use e2e::prelude::*;

abigen!(
Erc20Token,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use ethers::prelude::*;

use crate::context::*;
use e2e::prelude::*;

abigen!(
Erc721Token,
Expand Down
2 changes: 2 additions & 0 deletions e2e-tests/src/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod erc20;
pub mod erc721;
Loading

0 comments on commit 3afa579

Please sign in to comment.