Skip to content

Commit

Permalink
Setup CI (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
fakedev9999 authored Dec 9, 2024
1 parent c422396 commit 85f4e17
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 23 deletions.
151 changes: 151 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
name: CI

on:
push:
branches: [main]
pull_request:

env:
CARGO_TERM_COLOR: always

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
# TODO(fakedev9999): Add miri tests

test:
name: test ${{ matrix.os }} ${{ matrix.rust }} ${{ matrix.flags }}
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
# TODO(fakedev9999): Add Windows tests
# See https://github.com/alloy-rs/core/blob/dc0de77b3f8da2983a0a14f4b695e740c1ffaf8f/.github/workflows/ci.yml#L23
os: ["ubuntu-latest"]
rust: [
"stable",
"nightly",
# TODO(fakedev9999): Add MSRV tests when the MSRV is fixed
# See https://github.com/alloy-rs/core/blob/f65c915c41877576e38c99df8edd2de71ae46aa5/.github/workflows/ci.yml#L27
]
flags: [
# No features
"--no-default-features",
# Default features
"",
]
include:
# TODO(fakedev9999): Add MSRV tests when the MSRV is fixed
# See https://github.com/alloy-rs/core/blob/f65c915c41877576e38c99df8edd2de71ae46aa5/.github/workflows/ci.yml#L36-L42
# All features
- os: "ubuntu-latest"
rust: "nightly"
flags: "--all-features"
# TODO(fakedev9999): Add Windows tests
# See https://github.com/alloy-rs/core/blob/dc0de77b3f8da2983a0a14f4b695e740c1ffaf8f/.github/workflows/ci.yml#L47-L49
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- name: Install Dependencies
if: ${{ matrix.os == 'ubuntu-latest' }}
run: |
rustup install nightly-2024-02-01-x86_64-unknown-linux-gnu
rustup component add rust-src --toolchain nightly-2024-02-01-x86_64-unknown-linux-gnu
sudo apt-get update
sudo apt-get install -y gcc-riscv64-unknown-elf make
# Only run tests on latest stable and above
- name: build
if: ${{ matrix.rust == '1.81' }} # MSRV
run: cargo build --workspace ${{ matrix.flags }}
- name: test
if: ${{ matrix.rust != '1.81' }} # MSRV
run: cargo test --workspace ${{ matrix.flags }}

# TODO(fakedev9999): Add wasm tests if needed

feature-checks:
name: features ${{ matrix.rust }} ${{ matrix.flags }}
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
rust: ["stable", "nightly"]
flags: ["", "--all-targets"]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- uses: taiki-e/install-action@cargo-hack
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- name: cargo hack
run: |
args=(${{ matrix.flags }})
[ ${{ matrix.rust }} == "stable" ] && args+=(--skip nightly)
./scripts/check_features.sh "${args[@]}"
clippy:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
with:
components: clippy
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- run: cargo clippy --workspace --all-targets --all-features -- -D warnings
env:
RUSTFLAGS: -D warnings

docs:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- run: cargo doc --workspace --all-features --no-deps --document-private-items
env:
RUSTDOCFLAGS: "--cfg docsrs -D warnings"

fmt:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt
- run: cargo fmt --all --check

ci-success:
name: ci success
runs-on: ubuntu-latest
if: always()
needs:
- test
- feature-checks
- clippy
- docs
- fmt
steps:
- name: Decide whether the needed jobs succeeded or failed
uses: re-actors/alls-green@release/v1
with:
jobs: ${{ toJSON(needs) }}
18 changes: 18 additions & 0 deletions .github/workflows/delete_cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Delete Cache

on:
pull_request:
branches: ["closed"]

jobs:
if_merged:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- run: |
# get the ref key
ref_key="refs/pull/${{ github.event.pull_request.number }}/merge"
# delete cache
gh cache list --ref $ref_key --limit 100 | cut -f 1 | xargs -n1 gh cache delete
continue-on-error: true
2 changes: 1 addition & 1 deletion eth-riscv-interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod tests {
let runtime_path = "../asm-runtime-example/runtime";

// Check if the runtime ELF exists; if not, run `make` to generate it
if !fs::metadata(runtime_path).is_ok() {
if fs::metadata(runtime_path).is_err() {
let status = Command::new("make")
.current_dir("../asm-runtime-example")
.status()?;
Expand Down
2 changes: 1 addition & 1 deletion r55/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum Error<DB: Database = InMemoryDB>
where
DB::Error: std::error::Error + 'static,
{
/// The exception kind on RISC-V [`emulator`](`Emulator`)
/// The exception kind on RISC-V emulator
#[error("Got RISC-V emulator exception: {0:?}")]
RvEmuException(Exception),
/// EVM error
Expand Down
30 changes: 16 additions & 14 deletions r55/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use revm::{
CallInputs, CallScheme, CallValue, Host, InstructionResult, Interpreter, InterpreterAction,
InterpreterResult, SharedMemory,
},
primitives::{address, Address, Bytes, ExecutionResult, Output, TransactTo, U256, B256, Log},
primitives::{address, Address, Bytes, ExecutionResult, Log, Output, TransactTo, B256, U256},
Database, Evm, Frame, FrameOrResult, InMemoryDB,
};
use rvemu::{emulator::Emulator, exception::Exception};
Expand Down Expand Up @@ -61,7 +61,7 @@ pub fn run_tx(db: &mut InMemoryDB, addr: &Address, calldata: Vec<u8>) -> Result<

match result {
ExecutionResult::Success {
reason,
reason: _,
gas_used,
gas_refunded: _,
logs,
Expand Down Expand Up @@ -412,26 +412,28 @@ fn execute_riscv(
let data_size: u64 = emu.cpu.xregs.read(11);
let topics_ptr: u64 = emu.cpu.xregs.read(12);
let topics_size: u64 = emu.cpu.xregs.read(13);

// Read data
let data_slice = match emu.cpu.bus.get_dram_slice(data_ptr..(data_ptr + data_size)) {
Ok(slice) => slice,
Err(_) => &mut[],
};
let data_slice = emu
.cpu
.bus
.get_dram_slice(data_ptr..(data_ptr + data_size))
.unwrap_or(&mut []);
let data = data_slice.to_vec();

// Read topics
let topics_start = topics_ptr;
let topics_end = topics_ptr + topics_size * 32;
let topics_slice = match emu.cpu.bus.get_dram_slice(topics_start..topics_end) {
Ok(slice) => slice,
Err(_) => &mut[],
};
let topics_slice = emu
.cpu
.bus
.get_dram_slice(topics_start..topics_end)
.unwrap_or(&mut []);
let topics = topics_slice
.chunks(32)
.map(|chunk| B256::from_slice(chunk))
.map(B256::from_slice)
.collect::<Vec<B256>>();

host.log(Log::new_unchecked(
interpreter.contract.target_address,
topics,
Expand Down
14 changes: 7 additions & 7 deletions r55/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mod tests {

// Mint 42 tokens to Alice
let mint_result = run_tx(&mut db, &CONTRACT_ADDR, complete_calldata_mint.clone()).unwrap();
assert_eq!(mint_result.status, true, "Mint transaction failed");
assert!(mint_result.status, "Mint transaction failed");

// Check Alice's balance
let alice_balance = run_tx(
Expand All @@ -181,7 +181,7 @@ mod tests {
// Transfer 21 tokens from Alice to Bob
let transfer_result =
run_tx(&mut db, &CONTRACT_ADDR, complete_calldata_transfer.clone()).unwrap();
assert_eq!(transfer_result.status, true, "Transfer transaction failed");
assert!(transfer_result.status, "Transfer transaction failed");

// Check Alice's and Bob's balance
let alice_balance = run_tx(
Expand All @@ -198,7 +198,7 @@ mod tests {
// Approve Carol to spend 21 token from Alice
let approve_result =
run_tx(&mut db, &CONTRACT_ADDR, complete_calldata_approve.clone()).unwrap();
assert_eq!(approve_result.status, true, "Approve transaction failed");
assert!(approve_result.status, "Approve transaction failed");

// Check Carol's allowance
let allowance_res =
Expand Down Expand Up @@ -231,7 +231,7 @@ mod tests {
complete_mint_calldata.append(&mut calldata_mint);

let mint_result = run_tx(&mut db, &CONTRACT_ADDR, complete_mint_calldata).unwrap();
assert_eq!(mint_result.status, true, "Mint transaction failed");
assert!(mint_result.status, "Mint transaction failed");

// Transfer tokens from Alice to Bob
let selector_transfer = get_selector_from_sig("transfer");
Expand All @@ -243,7 +243,7 @@ mod tests {

// Assert the transfer log
assert!(
transfer_result.logs.len() > 0,
!transfer_result.logs.is_empty(),
"No logs found in transfer transaction"
);
let log = &transfer_result.logs[0];
Expand All @@ -252,8 +252,8 @@ mod tests {
// Expected event hash for Transfer event
let expected_event_hash = keccak256("Transfer(address,address,uint64)");
assert_eq!(
hex::encode(&topics[0]),
hex::encode(&expected_event_hash),
hex::encode(topics[0]),
hex::encode(expected_event_hash),
"Incorrect event hash"
);

Expand Down
5 changes: 5 additions & 0 deletions scripts/check_features.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -eo pipefail

cargo hack check --feature-powerset --depth 1 \
"${@}"

0 comments on commit 85f4e17

Please sign in to comment.