Skip to content

Commit

Permalink
test: replace ganache with anvil (gakonst#1286)
Browse files Browse the repository at this point in the history
* ci: install anvil

* test: use anvil instead of ganache

* ci: fix anvil ver

* ci: re-enable example tests

* test: remove unnecessary assertions

* test: enable anvil launch test

* docs: typo

* test: fix anvil chain id

* ci: install ganache

Ganache is needed for the Ganache tests

* chore: remove legacy feature from some examples

* ci: correctly build examples

* test: use correct account balance for anvil

* chore: remove sub_id == 1 check

this was only possible in ganache because it gives serial
sub ids, but in every other reasonable client the ids are generated
randomly, so we cannot test for its value

* test: ensure txs are different

There is a bug in Ganache's mempool which accepts duplicate transactions (here with the same nonce), whereas here we pre-set all the nonces so that they end up having a different transaction hash.

* test: ignore ganache tests

* fix: terzor api changes

* ci(examples): install Anvil, remove geth/ganache

* test(provider): Anvil instead of Geth

some tests start to fail now

* fix: revert usage of Anvil in ipc tests

Anvil does not support IPC yet

* fix: update examples script

* ci: use anvil for wasm example

* replace last ganache usage

Co-authored-by: Oliver Nordbjerg <[email protected]>
Co-authored-by: Georgios Konstantopoulos <[email protected]>
  • Loading branch information
3 people authored Jun 1, 2022
1 parent 1a699ad commit 89bc642
Show file tree
Hide file tree
Showing 33 changed files with 306 additions and 296 deletions.
149 changes: 75 additions & 74 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ jobs:
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Set up node
uses: actions/setup-node@v1
- name: Install Anvil
uses: foundry-rs/foundry-toolchain@v1
with:
node-version: 10
- name: Install ganache
run: npm install -g ganache
version: nightly
- name: Install Solc
run: |
mkdir -p "$HOME/bin"
Expand Down Expand Up @@ -66,13 +64,11 @@ jobs:
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Set up node
uses: actions/setup-node@v1
with:
node-version: 10
# TODO: can we combine these shared steps in github actions?
- name: Install ganache
run: npm install -g ganache
- name: Install Anvil
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- name: Install Solc
run: |
mkdir -p "$HOME/bin"
Expand Down Expand Up @@ -133,12 +129,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install node
uses: actions/setup-node@v1
with:
node-version: 10
- name: Checkout sources
uses: actions/checkout@v2
- name: Install rust
uses: actions-rs/toolchain@v1
with:
Expand All @@ -147,6 +139,11 @@ jobs:
profile: minimal
override: true

- name: Install Anvil
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- uses: Swatinem/rust-cache@v1
with:
cache-on-failure: true
Expand All @@ -157,11 +154,8 @@ jobs:
command: check
args: --target wasm32-unknown-unknown

- name: Launch Ganache
run: |
cd examples/ethers-wasm
npm install
npm run ganache &
- name: Launch Anvil
run: anvil --block-time 2 -m "stuff inherit faith park genre spread huge knee ecology private marble supreme" &

- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
Expand All @@ -176,59 +170,66 @@ jobs:
cd examples/ethers-wasm
wasm-pack test --headless --chrome
# TODO: Re-enable once we figure out why examples are running forever.
# examples:
# name: Examples
# runs-on: ubuntu-latest
# steps:
# - name: Checkout sources
# uses: actions/checkout@v2
# - name: Set up node
# uses: actions/setup-node@v1
# with:
# node-version: 10
# - name: Install ganache
# run: npm install -g ganache
# - name: Install Solc
# run: |
# mkdir -p "$HOME/bin"
# wget -q https://github.com/ethereum/solidity/releases/download/v0.8.10/solc-static-linux -O $HOME/bin/solc
# chmod u+x "$HOME/bin/solc"
# export PATH=$HOME/bin:$PATH
# solc --version
examples:
name: Examples
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install Anvil
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Install Solc
run: |
mkdir -p "$HOME/bin"
wget -q https://github.com/ethereum/solidity/releases/download/v0.8.10/solc-static-linux -O $HOME/bin/solc
chmod u+x "$HOME/bin/solc"
export PATH=$HOME/bin:$PATH
solc --version
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v1
with:
cache-on-failure: true
- name: Build all examples
run: |
export PATH=$HOME/bin:$PATH
examples=$(cargo metadata --format-version 1 | \
jq -c '.packages[]
| select(.name == "ethers")
| .targets[]
| select(.kind[] | contains("example"))
| with_entries(select([.key]
| inside(["name", "required-features"])))'
)
for example in $examples; do
name="$(echo "$example" | jq -r '.name')"
args=(
-p ethers
--example "$name"
)
features="$(echo "$example" | jq -r 'try(."required-features" | join(","))')"
if [[ ! -z "$features" ]]; then
args+=(--features "$features")
fi
# - name: Install geth
# run: |
# mkdir -p "$HOME/bin"
# wget -q https://gethstore.blob.core.windows.net/builds/geth-linux-amd64-1.9.23-8c2f2715.tar.gz
# tar -xvf geth-linux-amd64-1.9.23-8c2f2715.tar.gz
# mv geth-linux-amd64-1.9.23-8c2f2715/geth $HOME/bin/geth
# chmod u+x "$HOME/bin/geth"
# export PATH=$HOME/bin:$PATH
# geth version
# - name: Install stable toolchain
# uses: actions-rs/toolchain@v1
# with:
# profile: minimal
# toolchain: stable
# override: true
# components: rustfmt, clippy
# - uses: Swatinem/rust-cache@v1
# with:
# cache-on-failure: true
# - name: Build all examples
# run: |
# export PATH=$HOME/bin:$PATH
# for file in examples/*.rs; do
# name="$(echo "$file" | cut -f 1 -d '.')"
# echo "building $name"
# cargo build -p ethers --example "$(basename "$name")"
# done
# - name: Run all examples
# run: |
# export PATH=$HOME/bin:$PATH
# chmod +x ./scripts/examples.sh
# ./scripts/examples.sh
echo "building $name"
cargo build "${args[@]}"
done
- name: Run all examples
run: |
export PATH=$HOME/bin:$PATH
chmod +x ./scripts/examples.sh
./scripts/examples.sh
windows-build:
runs-on: windows-latest
Expand Down
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,14 @@ opt-level = "s"
[[example]]
name = "contract_human_readable"
path = "examples/contract_human_readable.rs"
required-features = ["legacy"]

[[example]]
name = "contract_with_abi"
path = "examples/contract_with_abi.rs"
required-features = ["legacy"]

[[example]]
name = "contract_with_abi_and_bytecode"
path = "examples/contract_with_abi_and_bytecode.rs"
required-features = ["legacy"]

[[example]]
name = "ipc"
Expand All @@ -151,3 +148,8 @@ required-features = ["trezor"]
name = "yubi"
path = "examples/yubi.rs"
required-features = ["yubi"]

[[example]]
name = "paginated_logs"
path = "examples/paginated_logs.rs"
required-features = ["rustls"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ethers = { git = "https://github.com/gakonst/ethers-rs" }
Tests require the following installed:

1. [`solc`](https://solidity.readthedocs.io/en/latest/installing-solidity.html) (>=0.8.10). We also recommend using [solc-select](https://github.com/crytic/solc-select) for more flexibility.
2. [`ganache-cli`](https://github.com/trufflesuite/ganache-cli#installation)
2. [`anvil`](https://github.com/foundry-rs/foundry/blob/master/anvil/README.md)
3. [`geth`](https://github.com/ethereum/go-ethereum)

In addition, it is recommended that you set the `ETHERSCAN_API_KEY` environment variable
Expand Down
24 changes: 12 additions & 12 deletions ethers-contract/tests/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ethers_contract::{abigen, EthCall, EthEvent};
use ethers_core::{
abi::{AbiDecode, AbiEncode, Address, Tokenizable},
types::{transaction::eip2718::TypedTransaction, Eip1559TransactionRequest, U256},
utils::Ganache,
utils::Anvil,
};
use ethers_middleware::SignerMiddleware;
use ethers_providers::{MockProvider, Provider};
Expand Down Expand Up @@ -305,10 +305,10 @@ async fn can_handle_underscore_functions() {
"ethers-contract/tests/solidity-contracts/simplestorage_abi.json",
);

// launcht the network & connect to it
let ganache = ethers_core::utils::Ganache::new().spawn();
let from = ganache.addresses()[0];
let provider = Provider::try_from(ganache.endpoint())
// launch the network & connect to it
let anvil = Anvil::new().spawn();
let from = anvil.addresses()[0];
let provider = Provider::try_from(anvil.endpoint())
.unwrap()
.with_sender(from)
.interval(std::time::Duration::from_millis(10));
Expand Down Expand Up @@ -484,9 +484,9 @@ fn can_handle_case_sensitive_calls() {
#[tokio::test]
async fn can_deploy_greeter() {
abigen!(Greeter, "ethers-contract/tests/solidity-contracts/greeter.json",);
let ganache = ethers_core::utils::Ganache::new().spawn();
let from = ganache.addresses()[0];
let provider = Provider::try_from(ganache.endpoint())
let anvil = Anvil::new().spawn();
let from = anvil.addresses()[0];
let provider = Provider::try_from(anvil.endpoint())
.unwrap()
.with_sender(from)
.interval(std::time::Duration::from_millis(10));
Expand All @@ -502,9 +502,9 @@ async fn can_deploy_greeter() {
#[tokio::test]
async fn can_abiencoderv2_output() {
abigen!(AbiEncoderv2Test, "ethers-contract/tests/solidity-contracts/abiencoderv2test_abi.json",);
let ganache = ethers_core::utils::Ganache::new().spawn();
let from = ganache.addresses()[0];
let provider = Provider::try_from(ganache.endpoint())
let anvil = Anvil::new().spawn();
let from = anvil.addresses()[0];
let provider = Provider::try_from(anvil.endpoint())
.unwrap()
.with_sender(from)
.interval(std::time::Duration::from_millis(10));
Expand Down Expand Up @@ -575,7 +575,7 @@ fn can_handle_overloaded_events() {
async fn can_send_struct_param() {
abigen!(StructContract, "./tests/solidity-contracts/StructContract.json");

let server = Ganache::default().spawn();
let server = Anvil::new().spawn();
let wallet: LocalWallet = server.keys()[0].clone().into();
let provider = Provider::try_from(server.endpoint()).unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet.with_chain_id(1337u64)));
Expand Down
10 changes: 5 additions & 5 deletions ethers-contract/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ethers_contract::EthEvent;
mod derive;

use ethers_contract::{Contract, ContractFactory};
use ethers_core::{abi::Abi, types::Bytes, utils::GanacheInstance};
use ethers_core::{abi::Abi, types::Bytes, utils::AnvilInstance};
use ethers_providers::{Http, Middleware, Provider};
use ethers_solc::Solc;
use std::{convert::TryFrom, sync::Arc, time::Duration};
Expand Down Expand Up @@ -38,16 +38,16 @@ pub fn compile_contract(name: &str, filename: &str) -> (Abi, Bytes) {
}

/// connects the private key to http://localhost:8545
pub fn connect(ganache: &GanacheInstance, idx: usize) -> Arc<Provider<Http>> {
let sender = ganache.addresses()[idx];
let provider = Provider::<Http>::try_from(ganache.endpoint())
pub fn connect(anvil: &AnvilInstance, idx: usize) -> Arc<Provider<Http>> {
let sender = anvil.addresses()[idx];
let provider = Provider::<Http>::try_from(anvil.endpoint())
.unwrap()
.interval(Duration::from_millis(10u64))
.with_sender(sender);
Arc::new(provider)
}

/// Launches a ganache instance and deploys the SimpleStorage contract
/// Launches a Anvil instance and deploys the SimpleStorage contract
pub async fn deploy<M: Middleware>(client: Arc<M>, abi: Abi, bytecode: Bytes) -> Contract<M> {
let factory = ContractFactory::new(abi, bytecode, client);
let deployer = factory.deploy("initial value".to_string()).unwrap();
Expand Down
Loading

0 comments on commit 89bc642

Please sign in to comment.