diff --git a/.github/workflows/bench-comment.yml b/.github/workflows/bench-comment.yml index 571ecd5c8..82f31bcee 100644 --- a/.github/workflows/bench-comment.yml +++ b/.github/workflows/bench-comment.yml @@ -45,6 +45,8 @@ jobs: ".github/workflows/bench.yml", "scripts/jit/bench.py", "scripts/jit/compare.py", + "scripts/jit/utils.py", + "scripts/common.py", ]; let behind = false; try { @@ -66,7 +68,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: version: "0.13.4" diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index 655056ec2..01e3dc867 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -44,11 +44,11 @@ jobs: with: toolchain: stable - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false - - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: version: "0.13.4" @@ -68,7 +68,7 @@ jobs: fi # The CLI reads fixtures from its source tree, so check out the old code in place and - # keep only this ref's benchmark scripts. + # keep only this ref's benchmark scripts and the helpers they share. - name: Check out backfill commit if: inputs.commit != '' env: @@ -78,15 +78,17 @@ jobs: [[ "$COMMIT" =~ ^[0-9a-f]{40}$ ]] || { echo "commit must be a full SHA" >&2; exit 1; } git merge-base --is-ancestor "$COMMIT" HEAD || { echo "$COMMIT is not in this ref's history" >&2; exit 1; } # bench.py builds against the LLVM version the old code requires. - llvm="$(git show "$COMMIT:crates/jit/llvm/Cargo.toml" | sed -nE 's/.*"llvm([0-9]+)-[0-9]+".*/\1/p')" + llvm="$(git show "$COMMIT:crates/jit/llvm/Cargo.toml" | sed -nE -e 's/.*"llvm([0-9]+)-[0-9]+".*/\1/p' -e 's/^llvm-sys = "([0-9]+)[0-9]"$/\1/p')" if ! command -v "llvm-config-$llvm" >/dev/null; then sudo .github/scripts/install_llvm_ubuntu.sh "$llvm" dev fi scripts="$RUNNER_TEMP/jit-scripts" - cp -R scripts/jit "$scripts" + mkdir "$scripts" + cp -R scripts/common.py scripts/jit "$scripts" git checkout --quiet --detach "$COMMIT" rm -rf scripts/jit - cp -R "$scripts" scripts/jit + cp -R "$scripts/jit" scripts/jit + cp "$scripts/common.py" scripts/common.py - name: Resolve and download exact baseline id: baseline @@ -117,23 +119,11 @@ jobs: | select(.display_title | startswith("Benchmark backfill ") | not) | {createdAt: .created_at, databaseId: .id, status}]' } - runs_json="$(list_runs 2>/dev/null || echo '[]')" - latest_run_id="$( - jq -r --argjson current "$CURRENT_RUN_ID" \ - '[.[] | select(.databaseId != $current)] | max_by(.createdAt).databaseId // empty' \ - <<< "$runs_json" - )" - if [[ -n "$latest_run_id" ]]; then - latest_status="$( - jq -r --argjson run_id "$latest_run_id" \ - '.[] | select(.databaseId == $run_id) | .status' \ - <<< "$runs_json" - )" - if [[ "$latest_status" != "completed" ]]; then - echo "Waiting for exact-base benchmark run $latest_run_id" - gh run watch "$latest_run_id" --interval 10 || true - runs_json="$(list_runs)" - fi + # A base run that is still in progress is not waited for: polling it from every + # PR run exhausts the API rate limit, so such a PR benchmarks the base itself. + if ! runs_json="$(list_runs)"; then + echo "::warning::Could not list benchmark runs for $base_sha" + runs_json='[]' fi run_ids="$( jq -r --argjson current "$CURRENT_RUN_ID" \ @@ -176,7 +166,7 @@ jobs: if [[ "$GITHUB_EVENT_NAME" == "pull_request" && "$BASELINE_FOUND" != "true" ]]; then args+=(--diff "$BASE_SHA" --base-output target/jit-bench/baseline) # bench.py builds each side against the LLVM version it requires. - base_llvm="$(git show "$BASE_SHA:crates/jit/llvm/Cargo.toml" | sed -nE 's/.*"llvm([0-9]+)-[0-9]+".*/\1/p')" + base_llvm="$(git show "$BASE_SHA:crates/jit/llvm/Cargo.toml" | sed -nE -e 's/.*"llvm([0-9]+)-[0-9]+".*/\1/p' -e 's/^llvm-sys = "([0-9]+)[0-9]"$/\1/p')" if ! command -v "llvm-config-$base_llvm" >/dev/null; then sudo .github/scripts/install_llvm_ubuntu.sh "$base_llvm" dev fi @@ -254,18 +244,18 @@ jobs: with: toolchain: stable - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false - name: Install cargo-codspeed - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 + uses: taiki-e/install-action@1919c4a95903d2c33e4a221102edca87309fc14b # v2.89.0 with: tool: cargo-codspeed - name: Build the benchmark target run: cargo codspeed build --profile profiling -p evm2-cli --features jit --bench evm - name: Run the benchmarks - uses: CodSpeedHQ/action@821a40cd6c80e3cfe387caf55668287addd808da # v5.5.0 + uses: CodSpeedHQ/action@61efe07e57e55bedeab750c434504de272a56e8c # v5.6.1 with: run: cargo codspeed run -p evm2-cli --bench evm mode: simulation diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9a753bce..c8cd5bc95 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,10 @@ on: env: CARGO_TERM_COLOR: always RUSTC_WRAPPER: "sccache" + # Nightly after 2026-10-31 fails to compile boa_engine (E0391 "needs async drop" cycle): + # https://github.com/rust-lang/rust/issues/165263 + # Remove this pin once a nightly builds boa_engine again. + NIGHTLY_TOOLCHAIN: nightly-2026-10-31 concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} @@ -29,7 +33,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: "3.14" - name: Generate matrices @@ -67,16 +71,16 @@ jobs: - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 if: ${{ !matrix.target }} with: - toolchain: ${{ matrix.rust }} + toolchain: ${{ matrix.rust == 'nightly' && env.NIGHTLY_TOOLCHAIN || matrix.rust }} - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 if: ${{ matrix.target }} with: - toolchain: ${{ matrix.rust }} + toolchain: ${{ matrix.rust == 'nightly' && env.NIGHTLY_TOOLCHAIN || matrix.rust }} targets: ${{ matrix.target }} - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 if: runner.os == 'Linux' - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -101,28 +105,18 @@ jobs: checksum: true fallback: none if: ${{ matrix.command == 'wasm-test' }} - - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: version: "0.13.4" if: ${{ matrix.kind == 'eest' }} - name: Download EEST fixtures if: ${{ matrix.kind == 'eest' }} - shell: bash - env: - INPUT_FIXTURE: ${{ matrix.fixture }} - run: | - if [[ "${INPUT_FIXTURE}" == "devnet" ]]; then - export DEVNET_VERSION="tests-glamsterdam-devnet%40v8.1.4" - export DEVNET_TAR="fixtures_glamsterdam-devnet.tar.gz" - export EVM2_STATETEST_DEVNET_ONLY=1 - fi - ./scripts/setup_test_fixtures.py + run: ./scripts/setup_test_fixtures.py - name: ${{ matrix.kind }} if: ${{ !matrix.command || matrix.command == 'nextest' }} shell: bash env: INPUT_CXX: ${{ matrix.cxx }} - INPUT_FIXTURE: ${{ matrix.fixture }} INPUT_FLAGS: ${{ matrix.flags }} INPUT_KIND: ${{ matrix.kind }} INPUT_OS: ${{ matrix.os }} @@ -144,10 +138,6 @@ jobs: --exclude evm2-jit-* ) fi - if [[ "${INPUT_FIXTURE}" == "devnet" ]]; then - export EVM2_STATETEST_ROOT="test-fixtures/devnet/state_tests" - export EVM2_BLOCKCHAINTEST_ROOT="test-fixtures/devnet/blockchain_tests" - fi cargo nextest --profile ci run "${package_flags[@]}" --no-fail-fast "${matrix_flags[@]}" "${extra_flags[@]}" # Fetch on the protected host; cross containers do not inherit Aegis routing. - name: Fetch cross-test dependencies through Aegis @@ -248,7 +238,7 @@ jobs: - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 if: runner.os == 'Linux' - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -258,7 +248,7 @@ jobs: tool: nextest@0.9.140 checksum: true fallback: none - - uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0 + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: version: "0.13.4" - name: Download EEST fixtures @@ -292,7 +282,7 @@ jobs: toolchain: stable targets: ${{ matrix.target }} - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -329,7 +319,7 @@ jobs: checksum: true fallback: none - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -376,7 +366,7 @@ jobs: with: toolchain: stable - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -413,11 +403,11 @@ jobs: run: .github/scripts/install_llvm.sh ubuntu - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 with: - toolchain: nightly + toolchain: ${{ env.NIGHTLY_TOOLCHAIN }} components: clippy - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false @@ -441,10 +431,10 @@ jobs: run: .github/scripts/install_llvm.sh ubuntu - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 with: - toolchain: nightly + toolchain: ${{ env.NIGHTLY_TOOLCHAIN }} - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false diff --git a/.github/workflows/fuzzer.yml b/.github/workflows/fuzzer.yml index e5e52f51d..c2c2c6097 100644 --- a/.github/workflows/fuzzer.yml +++ b/.github/workflows/fuzzer.yml @@ -19,6 +19,10 @@ concurrency: env: CARGO_TERM_COLOR: always RUSTC_WRAPPER: "sccache" + # Nightly after 2026-10-31 fails to compile boa_engine (E0391 "needs async drop" cycle): + # https://github.com/rust-lang/rust/issues/165263 + # Remove this pin once a nightly builds boa_engine again. + NIGHTLY_TOOLCHAIN: nightly-2026-10-31 jobs: smoke: @@ -31,7 +35,7 @@ jobs: strategy: fail-fast: false matrix: - dispatch_backend: [tco, packed, single_return, unpacked] + dispatch_backend: [tco, packed, unpacked] steps: - name: Secure runner uses: tempoxyz/gh-actions/actions/secure-runner@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 @@ -40,10 +44,10 @@ jobs: persist-credentials: false - uses: tempoxyz/gh-actions/vendor/dtolnay/rust-toolchain@2ab28a31ae569be9f780c7675b684471060afa40 # 2026-10-09T21-43-19Z-2ab28a31 with: - toolchain: nightly + toolchain: ${{ env.NIGHTLY_TOOLCHAIN }} - uses: rui314/setup-mold@339e96b4ff1f3bd865856ba4cbbe8818a7acb038 # v1 - uses: mozilla-actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: cache-on-failure: true cache-bin: false diff --git a/.gitignore b/.gitignore index f7ee5e08a..d3963b304 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,7 @@ -/target +target/ .vscode .idea /tmp -/tests/GeneralStateTests -/tests/BlockchainTests /test-fixtures __pycache__/ diff --git a/AGENTS.md b/AGENTS.md index eb52af7e9..78ee5e773 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -26,8 +26,8 @@ cargo st # include EEST fixtures with interpreter, JIT, and AOT suites ``` Use `EVM2_DISPATCH_BACKEND` to force an interpreter dispatch backend for manual -testing. Accepted values are `auto` (default), `tco`, `packed`, `single_return`, -and `unpacked`, for example: +testing. Accepted values are `auto` (default), `tco`, `packed`, and `unpacked`, +for example: ```bash EVM2_DISPATCH_BACKEND=packed cargo nextest run -p evm2-eest --test eest --ignore-default-filter @@ -38,21 +38,21 @@ EVM2_DISPATCH_BACKEND=packed cargo nextest run -p evm2-eest --test eest --ignore `./scripts/setup_test_fixtures.py` downloads fixtures into `test-fixtures/`. If fixtures are already available in another worktree, symlink `test-fixtures` to that directory instead of re-downloading them. -By default it downloads the glamsterdam (Amsterdam) devnet fixtures plus legacy -Cancun/Constantinople state tests. The devnet release defaults to -`tests-glamsterdam-devnet@v8.1.4` / `fixtures_glamsterdam-devnet.tar.gz` -(from the `ethereum/execution-specs` repo, base overridable via `DEVNET_BASE_URL`); -override `DEVNET_VERSION` and `DEVNET_TAR` to select a different devnet release, or -clear either to disable devnet. The glamsterdam devnet fixtures cover -frontier..amsterdam, so the EEST `main` develop suite is now opt-in via -`EVM2_STATETEST_MAIN=1` (stable with `EVM2_STATETEST_STABLE=1`). Add -`EVM2_STATETEST_DEVNET_ONLY=1` to skip legacy fixtures. Use `EVM2_STATETEST_ROOT` -or `EVM2_BLOCKCHAINTEST_ROOT` for a single explicit root. +By default it downloads the mainline execution-specs fixtures plus legacy +Cancun/Constantinople state tests. The release defaults to +`tests@v21.0.1` / `fixtures.tar.gz` (from the `ethereum/execution-specs` repo, +base overridable via `EEST_BASE_URL`); override `EEST_VERSION` and `EEST_TAR` +to select a different release (for example a devnet), or clear either to disable +it. The release covers frontier..amsterdam, so the EEST `main` develop suite is +opt-in via `EVM2_STATETEST_MAIN=1` (stable with `EVM2_EEST_STABLE=1`, honored +by both runners). Add `EVM2_STATETEST_EEST_ONLY=1` to skip legacy fixtures. Use +`EVM2_STATETEST_ROOT` or `EVM2_BLOCKCHAINTEST_ROOT` for a single explicit root. Compiled EEST runs use a default subset when the full corpus is too expensive: AOT defaults to the `ci-aot` subset, and JIT defaults to the `ci-smoke` subset. Set `EVM2_COMPILED_EEST_SUBSET=all` to force full coverage, or set it to -`ci-aot` or `ci-smoke` to run those subsets explicitly. +`ci-aot` or `ci-smoke` to run those subsets explicitly. Set +`EVM2_EEST_SINGLE_THREAD=1` to run compiled files on one worker thread. To run additional tests from an arbitrary folder (or single file) without a test-name filter, use `./scripts/eest.sh `. Every JSON file found anywhere diff --git a/Cargo.lock b/Cargo.lock index d15558309..8051e8eda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -201,6 +201,7 @@ dependencies = [ "alloy-primitives", "auto_impl", "dyn-clone", + "serde", ] [[package]] @@ -2580,6 +2581,7 @@ dependencies = [ "alloy-consensus", "alloy-eip7928", "alloy-eips", + "alloy-hardforks", "alloy-primitives", "ark-bls12-381", "ark-bn254", @@ -2793,7 +2795,7 @@ dependencies = [ "alloy-primitives", "cc", "evm2-jit-backend", - "inkwell", + "llvm-sys", "object", "tracing", ] @@ -2816,7 +2818,6 @@ dependencies = [ "evm2-macros", "eyre", "libc", - "libloading", "paste", "quanta", "rayon", @@ -3587,28 +3588,6 @@ dependencies = [ "serde_core", ] -[[package]] -name = "inkwell" -version = "0.10.0" -source = "git+https://github.com/TheDan64/inkwell?rev=c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff#c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff" -dependencies = [ - "bitflags 2.13.2", - "inkwell_internals", - "libc", - "llvm-sys", - "thiserror 2.0.21", -] - -[[package]] -name = "inkwell_internals" -version = "0.15.0" -source = "git+https://github.com/TheDan64/inkwell?rev=c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff#c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] - [[package]] name = "intrusive-collections" version = "0.10.3" diff --git a/Cargo.toml b/Cargo.toml index c96f2197e..1a56a47f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -199,6 +199,3 @@ ark-ff-macros = { git = "https://github.com/DaniPopes/algebra", rev = "a71d9795e ark-poly = { git = "https://github.com/DaniPopes/algebra", rev = "a71d9795e2e527cd0c80e6831bd957134de30d22" } ark-serialize = { git = "https://github.com/DaniPopes/algebra", rev = "a71d9795e2e527cd0c80e6831bd957134de30d22" } ark-serialize-derive = { git = "https://github.com/DaniPopes/algebra", rev = "a71d9795e2e527cd0c80e6831bd957134de30d22" } - -# LLVM 23 support is not released yet. -inkwell = { git = "https://github.com/TheDan64/inkwell", rev = "c9d9734ae17dbaf1d9902dab3b8ee9d2041781ff" } diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 04d90e79b..023a57657 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -26,7 +26,7 @@ alloy-consensus = { workspace = true, features = ["k256", "secp256k1", "std"] } alloy-chains.workspace = true alloy-eips = { workspace = true, features = ["serde", "std"] } alloy-hardforks.workspace = true -alloy-primitives = { workspace = true, features = ["keccak-cache-global", "secp256k1", "serde", "std"] } +alloy-primitives = { workspace = true, features = ["keccak-cache-global", "map-indexmap", "secp256k1", "serde", "std"] } alloy-provider = { workspace = true, features = ["debug-api", "reqwest", "reqwest-rustls-tls"] } alloy-rpc-client = { workspace = true, features = ["reqwest"] } alloy-rpc-types-eth = { workspace = true, features = ["serde", "std"] } diff --git a/crates/cli/benches/evm/fixture.rs b/crates/cli/benches/evm/fixture.rs index afd5b88ce..fc8bf7167 100644 --- a/crates/cli/benches/evm/fixture.rs +++ b/crates/cli/benches/evm/fixture.rs @@ -3,7 +3,7 @@ use alloy_consensus::{TxLegacy, transaction::Recovered}; use alloy_primitives::Bytes; use alloy_primitives::{B256, TxKind, U256}; use evm2::{ - SpecId, Version, + SpecId, bytecode::Bytecode, env::{BlockEnv, BlockEnvExt}, ethereum::{RecoveredTxEnvelope, TxEnvelope}, @@ -35,7 +35,7 @@ impl Suites { } } -fn workspace_path(path: &str) -> PathBuf { +pub(crate) fn workspace_path(path: &str) -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..").join(path) } @@ -62,7 +62,7 @@ impl Suite { let post = unit .post .iter() - .filter(|(spec_name, _)| spec_name.to_spec_id() == Some(spec)) + .filter(|(spec_name, _)| spec_name.to_state_spec_id() == Some(spec)) .flat_map(|(_, posts)| posts) .next() .unwrap_or_else(|| panic!("fixture suite does not contain {name} post for {spec:?}")); @@ -108,7 +108,7 @@ impl Case<'_> { pub(crate) fn state(&self) -> InMemoryDB { let mut db = InMemoryDB::default(); for (address, account) in &self.unit.pre { - let bytecode = Bytecode::new_legacy(account.code.clone()); + let bytecode = Bytecode::from_stored(account.code.clone()); // Keep first-use jump analysis outside the timed execution. let _ = bytecode.legacy_jump_table(); let mut info = AccountInfo::default().with_code(bytecode); @@ -123,12 +123,12 @@ impl Case<'_> { db } - pub(crate) fn tx(&self, spec: SpecId) -> RecoveredTxEnvelope { + pub(crate) fn tx(&self) -> RecoveredTxEnvelope { let raw = &self.unit.transaction; let indexes = self.post.indexes; - let gas_limit = - raw.gas_limit.get(indexes.gas).copied().expect("fixture gas index must exist"); - let gas_limit = u64_value(gas_limit).min(Version::base(spec).tx_gas_limit_cap); + let gas_limit = u64_value( + raw.gas_limit.get(indexes.gas).copied().expect("fixture gas index must exist"), + ); let tx = TxLegacy { chain_id: None, nonce: u64_value(raw.nonce), @@ -168,7 +168,7 @@ impl Case<'_> { if account.code.is_empty() { return None; } - let bytecode = Bytecode::new_legacy(account.code.clone()); + let bytecode = Bytecode::from_stored(account.code.clone()); Some(CompiledAccount { code_hash: bytecode.hash_slow(), bytecode: account.code.clone(), @@ -179,7 +179,7 @@ impl Case<'_> { } fn b256_to_u256(value: B256) -> U256 { - U256::from_be_bytes(value.0) + value.into() } #[cfg(feature = "jit")] diff --git a/crates/cli/benches/evm/jit.rs b/crates/cli/benches/evm/jit.rs index 3d2e857be..13e5f6f12 100644 --- a/crates/cli/benches/evm/jit.rs +++ b/crates/cli/benches/evm/jit.rs @@ -1,5 +1,5 @@ use crate::fixture::Suites; -use alloy_primitives::{B256, hex}; +use alloy_primitives::{B256, map::B256Map}; use criterion::{BatchSize, BenchmarkGroup, black_box, measurement::WallTime}; use evm2::{ BaseEvmTypes, Evm, InterpreterRunner, Precompiles, SpecId, @@ -9,7 +9,7 @@ use evm2::{ interpreter::{InstrStop, Interpreter}, }; use evm2_cli::evm_bench::BenchCase; -use evm2_jit_context::EvmCompilerFn; +use evm2_jit_context::{EvmCompilerFn, can_run_compiled}; use evm2_jit_llvm::EvmLlvmBackend; use evm2_jit_runtime::{EvmCompiler, OptimizationLevel}; use std::{borrow::Cow, collections::HashMap, sync::Arc}; @@ -60,7 +60,7 @@ pub(crate) struct PreparedBench { db: InMemoryDB, tx: RecoveredTxEnvelope, entry_bytecode: Option, - functions: Arc>, + functions: Arc>, } impl PreparedBench { @@ -81,10 +81,10 @@ impl PreparedBench { return None; } - let mut functions = HashMap::new(); + let mut functions = B256Map::default(); for account in accounts { let func = *compiler.functions.entry((account.code_hash, spec)).or_insert_with(|| { - let name = format!("contract_{}_{spec:?}", hex::encode(account.code_hash)); + let name = format!("contract_{:x}_{spec:?}", account.code_hash); let func = unsafe { compiler.compiler.jit(&name, account.bytecode.as_ref(), spec).unwrap_or_else( |err| panic!("{} benchmark JIT compilation failed: {err:?}", bench.name), @@ -101,7 +101,7 @@ impl PreparedBench { spec, block: case.block(), db: case.state(), - tx: case.tx(spec), + tx: case.tx(), entry_bytecode: case.entry_bytecode(), functions: Arc::new(functions), }) @@ -114,11 +114,13 @@ impl PreparedBench { let jit = self.run_jit().unwrap_or_else(|err| { panic!("{} JIT benchmark transaction must execute: {err:?}", self.name) }); - assert_eq!( - interpreter.status, jit.status, - "{} interpreter and JIT status differ", - self.name + assert!( + interpreter.is_success(), + "{} benchmark transaction must succeed: {:?}", + self.name, + interpreter.stop ); + assert_eq!(interpreter, jit, "{} interpreter and JIT results differ", self.name); } pub(crate) fn bench(&self, group: &mut BenchmarkGroup<'_, WallTime>) { @@ -199,7 +201,7 @@ impl Runner { #[derive(Clone, Debug)] struct FixedJitRunner { - functions: Arc>, + functions: Arc>, } impl InterpreterRunner for FixedJitRunner { @@ -208,8 +210,7 @@ impl InterpreterRunner for FixedJitRunner { interpreter: &mut Interpreter<'frame, 'host, BaseEvmTypes>, host: &mut Evm<'host, BaseEvmTypes>, ) -> Option { - // Compiled code treats the remaining gas as signed, so it cannot hold more than `i64::MAX`. - if interpreter.gas().remaining() > i64::MAX as u64 { + if !can_run_compiled(interpreter) { return None; } let func = *self.functions.get(&interpreter.original_bytecode_hash())?; @@ -219,7 +220,7 @@ impl InterpreterRunner for FixedJitRunner { } fn new_evm(spec: SpecId, block: BlockEnv, db: InMemoryDB) -> BenchEvm { - Evm::new(spec, block, ethereum_tx_registry(spec), db, Precompiles::base(spec)) + BenchEvm::new(spec, block, ethereum_tx_registry(spec), db, Precompiles::base(spec)) } fn new_compiler(opt_level: OptimizationLevel) -> LlvmCompiler { diff --git a/crates/cli/benches/evm/mainnet.rs b/crates/cli/benches/evm/mainnet.rs index f97d0c2f4..826328a70 100644 --- a/crates/cli/benches/evm/mainnet.rs +++ b/crates/cli/benches/evm/mainnet.rs @@ -1,3 +1,4 @@ +use crate::fixture::workspace_path; use criterion::{BatchSize, BenchmarkGroup, black_box, measurement::WallTime}; use evm2::evm::InMemoryDB; use evm2_cli::evm_bench::BenchCase; @@ -6,11 +7,7 @@ use evm2_eest::{ blockchaintest::BlockchainTest, execute_blockchain_test_case_on, execute_blockchain_tests_suite, }; -use std::{ - borrow::Cow, - path::{Path, PathBuf}, - sync::Arc, -}; +use std::{borrow::Cow, path::PathBuf, sync::Arc}; #[derive(Clone)] pub(crate) struct PreparedBench { @@ -57,8 +54,7 @@ impl PreparedBench { pub(crate) fn bench(&self, group: &mut BenchmarkGroup<'_, WallTime>) { let (case_name, case) = self.suite.0.first_key_value().expect("fixture must contain a case"); - let config = - BlockchainTestExecuteConfig { validate_post_state: false, ..Default::default() }; + let config = BlockchainTestExecuteConfig { validate: false, ..Default::default() }; group.bench_function(format!("{}/replay", self.name), |b| { b.iter_batched_ref( || self.pre_state.clone(), @@ -80,7 +76,3 @@ impl PreparedBench { }); } } - -fn workspace_path(path: &str) -> PathBuf { - Path::new(env!("CARGO_MANIFEST_DIR")).join("../..").join(path) -} diff --git a/crates/cli/benches/evm/mainnet_revm.rs b/crates/cli/benches/evm/mainnet_revm.rs index 1bc1bfb56..6699418a9 100644 --- a/crates/cli/benches/evm/mainnet_revm.rs +++ b/crates/cli/benches/evm/mainnet_revm.rs @@ -1,3 +1,4 @@ +use crate::fixture::workspace_path; use criterion::{BatchSize, BenchmarkGroup, measurement::WallTime}; use evm2_cli::{ evm_bench::BenchCase, @@ -5,11 +6,7 @@ use evm2_cli::{ }; use evm2_eest::BlockchainTestNoopHook; use revm::database::InMemoryDB; -use std::{ - borrow::Cow, - path::{Path, PathBuf}, - sync::Arc, -}; +use std::{borrow::Cow, sync::Arc}; /// revm counterpart of `mainnet::PreparedBench`. #[derive(Clone)] @@ -63,7 +60,3 @@ impl PreparedBench { }); } } - -fn workspace_path(path: &str) -> PathBuf { - Path::new(env!("CARGO_MANIFEST_DIR")).join("../..").join(path) -} diff --git a/crates/cli/benches/evm/revm.rs b/crates/cli/benches/evm/revm.rs index 7df14dd79..da65088a8 100644 --- a/crates/cli/benches/evm/revm.rs +++ b/crates/cli/benches/evm/revm.rs @@ -1,12 +1,12 @@ use crate::fixture::Suites; use criterion::{BatchSize, BenchmarkGroup, black_box, measurement::WallTime}; -use evm2::{SpecId, Version}; -use evm2_cli::evm_bench::BenchCase; +use evm2::SpecId; +use evm2_cli::{evm_bench::BenchCase, reference}; use revm::{ ExecuteEvm, MainBuilder, MainContext, context::{BlockEnv, CfgEnv, Context, TxEnv}, database::{CacheDB, EmptyDB, InMemoryDB}, - primitives::{U256, hardfork::SpecId as RevmSpecId}, + primitives::hardfork::SpecId as RevmSpecId, statetest_types::{Test, TestSuite, TestUnit}, }; use std::{borrow::Cow, sync::Arc}; @@ -29,7 +29,7 @@ impl PreparedBench { let suite = suites.get(bench.fixture_path); let (unit, test) = revm_case(suite.input(), &bench.name, spec); let tx = test.tx_env(&unit).expect("revm benchmark transaction must build"); - let (cfg, block) = envs(&unit, revm_spec_id(spec)); + let (cfg, block) = envs(&unit, reference::spec_id(spec)); let db = Arc::new(database(&unit)); Self { name: bench.name.clone(), cfg, block, db, tx } } @@ -104,15 +104,10 @@ fn revm_case(input: &str, name: &str, spec: SpecId) -> (TestUnit, Test) { let tests = unit .post .iter_mut() - .find(|(spec_name, _)| spec_name.to_spec_id() == revm_spec_id(spec)) + .find(|(spec_name, _)| spec_name.to_spec_id() == reference::spec_id(spec)) .map(|(_, tests)| tests) .expect("fixture suite must contain revm post test"); let test = tests.remove(0); - let gas_limit = &mut unit.transaction.gas_limit[test.indexes.gas]; - let cap = U256::from(Version::base(spec).tx_gas_limit_cap); - if *gas_limit > cap { - *gas_limit = cap; - } (unit, test) } @@ -150,24 +145,3 @@ fn database(unit: &TestUnit) -> InMemoryDB { } db } - -fn revm_spec_id(spec: SpecId) -> RevmSpecId { - match spec { - SpecId::FRONTIER => RevmSpecId::FRONTIER, - SpecId::HOMESTEAD => RevmSpecId::HOMESTEAD, - SpecId::TANGERINE => RevmSpecId::TANGERINE, - SpecId::SPURIOUS_DRAGON => RevmSpecId::SPURIOUS_DRAGON, - SpecId::BYZANTIUM => RevmSpecId::BYZANTIUM, - SpecId::PETERSBURG => RevmSpecId::PETERSBURG, - SpecId::ISTANBUL => RevmSpecId::ISTANBUL, - SpecId::BERLIN => RevmSpecId::BERLIN, - SpecId::LONDON => RevmSpecId::LONDON, - SpecId::MERGE => RevmSpecId::MERGE, - SpecId::SHANGHAI => RevmSpecId::SHANGHAI, - SpecId::CANCUN => RevmSpecId::CANCUN, - SpecId::PRAGUE => RevmSpecId::PRAGUE, - SpecId::OSAKA => RevmSpecId::OSAKA, - SpecId::AMSTERDAM => RevmSpecId::AMSTERDAM, - _ => panic!("unsupported benchmark spec: {spec:?}"), - } -} diff --git a/crates/cli/benches/evm/support.rs b/crates/cli/benches/evm/support.rs index 1ae48a22d..401348d2b 100644 --- a/crates/cli/benches/evm/support.rs +++ b/crates/cli/benches/evm/support.rs @@ -30,15 +30,21 @@ impl PreparedBench { spec, block: case.block(), db: case.state(), - tx: case.tx(spec), + tx: case.tx(), } } pub(crate) fn sanity_check(&self) { let mut runner = Runner::new(self); - let _ = runner.run().unwrap_or_else(|err| { + let result = runner.run().unwrap_or_else(|err| { panic!("{} benchmark transaction must execute: {err:?}", self.name) }); + assert!( + result.is_success(), + "{} benchmark transaction must succeed: {:?}", + self.name, + result.stop + ); } pub(crate) fn bench(&self, group: &mut BenchmarkGroup<'_, WallTime>) { @@ -75,5 +81,5 @@ impl Runner { } fn new_evm(spec: SpecId, block: BlockEnv, db: InMemoryDB) -> BenchEvm { - Evm::new(spec, block, ethereum_tx_registry(spec), db, Precompiles::base(spec)) + BenchEvm::new(spec, block, ethereum_tx_registry(spec), db, Precompiles::base(spec)) } diff --git a/crates/cli/fuzzer/README.md b/crates/cli/fuzzer/README.md index 264937de8..86a40a456 100644 --- a/crates/cli/fuzzer/README.md +++ b/crates/cli/fuzzer/README.md @@ -27,7 +27,7 @@ Force an evm2 dispatch backend with `EVM2_DISPATCH_BACKEND`: EVM2_DISPATCH_BACKEND=packed cargo run -q -p evm2-cli -- fuzzer --duration 1m -j 0 ``` -Accepted backends are `auto`, `tco`, `packed`, `single_return`, and `unpacked`. +Accepted backends are `auto`, `tco`, `packed`, and `unpacked`. ## Corpus and replay @@ -55,19 +55,6 @@ Minimize a reproducing case: cargo run -q -p evm2-cli -- fuzzer minimize crates/cli/fuzzer/corpus/failures/case-....json ``` -Import the `evm-protobuf-fuzzer` corpus as replayable JSON cases: - -```sh -./scripts/import_evm_protobuf_corpus.py \ - /path/to/corp-evm-protobuf-fuzzer.tar.xz \ - --output tmp/evm-protobuf-import --clean -cargo run -q -p evm2-cli -- fuzzer corpus tmp/evm-protobuf-import -``` - -The import is intentionally lossy where the fuzzer case format has no field for -the original data, such as block coinbase, difficulty, and excess blob gas. Such -cases are tagged in `features`. - ## Coverage report Generate an HTML coverage report from fuzzer execution, excluding the fuzzer diff --git a/crates/cli/src/capture/builder.rs b/crates/cli/src/capture/builder.rs index 3d0f4bde7..bf5946aa2 100644 --- a/crates/cli/src/capture/builder.rs +++ b/crates/cli/src/capture/builder.rs @@ -201,7 +201,7 @@ impl CaptureBuilder { withdrawal_balance, }, )?; - if balance.is_zero() && withdrawal_balance != U256::ZERO { + if balance.is_zero() && !withdrawal_balance.is_zero() { return Ok(()); } let state = self.account_state(address); diff --git a/crates/cli/src/capture/error.rs b/crates/cli/src/capture/error.rs index 1e5b622a7..5daede9aa 100644 --- a/crates/cli/src/capture/error.rs +++ b/crates/cli/src/capture/error.rs @@ -47,6 +47,8 @@ pub(crate) enum CaptureError { MultipleSpecs, #[error("capture uses unsupported spec id {0}")] UnsupportedSpec(u32), + #[error("block timestamp {timestamp} has no supported mainnet hardfork")] + UnsupportedHardfork { timestamp: u64 }, #[error("capture has conflicting bytecode for code hash {code_hash}")] CodeHashCollision { code_hash: B256 }, #[error( diff --git a/crates/cli/src/capture/export.rs b/crates/cli/src/capture/export.rs index a7b5dc2b1..4924e105a 100644 --- a/crates/cli/src/capture/export.rs +++ b/crates/cli/src/capture/export.rs @@ -2,26 +2,30 @@ use super::{ error::CaptureError, model::{CapturedBlock, CapturedCase, CapturedInput, State as CapturedState}, }; +use crate::ethereum::mainnet_spec_for_header; use alloy_consensus::{ EthereumTxEnvelope, Header, TxEip4844, transaction::{Transaction as _, to_eip155_value}, }; use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization}; +use alloy_hardforks::EthereumHardfork; use alloy_primitives::{Address, B256, Bytes, FixedBytes, U256}; -use evm2::SpecId; +use evm2::{SpecId, utils::b256_to_word}; use evm2_eest::{ AccessListItem, TestAuthorization, blockchaintest::{ - Account, Block, BlockHash as EestBlockHash, BlockHeader, BlockchainTest, - BlockchainTestCase, ForkSpec, SealEngine, State as EestState, Transaction, Withdrawal, + Account, BlobFork, BlobScheduleEntry, Block, BlockHash as EestBlockHash, BlockHeader, + BlockchainTest, BlockchainTestCase, ChainConfig, ForkSpec, SealEngine, State as EestState, + Transaction, Withdrawal, }, }; use serde_json::json; use std::collections::BTreeMap; pub(super) fn suite(capture: &CapturedCase) -> Result { - let network = network(capture)?; let inputs = captured_blocks(capture)?; + let blob_fork = blob_fork(inputs)?; + let network = network(capture, blob_fork)?; let decoded_blocks = inputs.iter().map(export_block).collect::, CaptureError>>()?; let first = decoded_blocks.first().expect("blocks is not empty"); @@ -45,6 +49,10 @@ pub(super) fn suite(capture: &CapturedCase) -> Result Result<&[CapturedBlock], CaptureEr Ok(blocks) } -fn network(capture: &CapturedCase) -> Result { +/// Returns the mainnet fork that sets the blob parameters of every block, or `None` before Cancun. +fn blob_fork(blocks: &[CapturedBlock]) -> Result, CaptureError> { + let mut forks = blocks.iter().map(|input| mainnet_blob_fork(&input.block.header)); + let first = forks.next().expect("blocks is not empty")?; + for fork in forks { + if fork? != first { + return Err(CaptureError::MultipleSpecs); + } + } + Ok(first) +} + +fn mainnet_blob_fork(header: &Header) -> Result, CaptureError> { + let spec = mainnet_spec_for_header(header)?; + if !spec.enables(SpecId::CANCUN) { + return Ok(None); + } + let hardfork = EthereumHardfork::from_chain_and_timestamp( + alloy_chains::Chain::mainnet(), + header.timestamp, + ); + match hardfork { + Some(EthereumHardfork::Cancun) => Ok(Some(BlobFork::Cancun)), + Some(EthereumHardfork::Prague) => Ok(Some(BlobFork::Prague)), + Some(EthereumHardfork::Osaka) => Ok(Some(BlobFork::Osaka)), + Some(EthereumHardfork::Bpo1) => Ok(Some(BlobFork::Bpo1)), + Some(EthereumHardfork::Bpo2) => Ok(Some(BlobFork::Bpo2)), + Some(EthereumHardfork::Amsterdam) => Ok(Some(BlobFork::Amsterdam)), + _ => Err(CaptureError::UnsupportedSpec(u32::from(spec))), + } +} + +/// Returns the mainnet blob parameters of every fork up to `last`, or none before Cancun. +fn mainnet_blob_schedule(last: Option) -> BTreeMap { + [ + BlobFork::Cancun, + BlobFork::Prague, + BlobFork::Osaka, + BlobFork::Bpo1, + BlobFork::Bpo2, + BlobFork::Amsterdam, + ] + .into_iter() + .filter(|fork| Some(*fork) <= last) + .filter_map(|fork| Some((fork, fork.mainnet_params()?.into()))) + .collect() +} + +fn network(capture: &CapturedCase, blob_fork: Option) -> Result { let first = capture.versions.versions.first().ok_or(CaptureError::EmptyCapturedVersions)?; if capture.versions.versions.iter().any(|version| version.spec_id != first.spec_id) { return Err(CaptureError::MultipleSpecs); } let spec = SpecId::try_from_u32(first.spec_id).ok_or(CaptureError::UnsupportedSpec(first.spec_id))?; - match spec { - SpecId::FRONTIER => Ok(ForkSpec::Frontier), - SpecId::HOMESTEAD => Ok(ForkSpec::Homestead), - SpecId::TANGERINE => Ok(ForkSpec::EIP150), - SpecId::SPURIOUS_DRAGON => Ok(ForkSpec::EIP158), - SpecId::BYZANTIUM => Ok(ForkSpec::Byzantium), - SpecId::PETERSBURG => Ok(ForkSpec::ConstantinopleFix), - SpecId::ISTANBUL => Ok(ForkSpec::Istanbul), - SpecId::BERLIN => Ok(ForkSpec::Berlin), - SpecId::LONDON => Ok(ForkSpec::London), - SpecId::MERGE => Ok(ForkSpec::Paris), - SpecId::SHANGHAI => Ok(ForkSpec::Shanghai), - SpecId::CANCUN => Ok(ForkSpec::Cancun), - SpecId::PRAGUE => Ok(ForkSpec::Prague), - SpecId::OSAKA => Ok(ForkSpec::Osaka), - SpecId::AMSTERDAM => Ok(ForkSpec::Amsterdam), - _ => Err(CaptureError::UnsupportedSpec(first.spec_id)), - } + ForkSpec::from_spec_id(spec, blob_fork).ok_or(CaptureError::UnsupportedSpec(first.spec_id)) } fn export_state(capture: &CapturedCase, state: &CapturedState) -> EestState { @@ -115,7 +154,7 @@ fn export_state(capture: &CapturedCase, state: &CapturedState) -> EestState { let storage = account .storage .iter() - .map(|entry| (U256::from_be_bytes(entry.slot.0), entry.value)) + .map(|entry| (b256_to_word(entry.slot), entry.value)) .collect(); ( account.address, @@ -134,7 +173,7 @@ fn export_state(capture: &CapturedCase, state: &CapturedState) -> EestState { fn parent_header(first: &Block) -> BlockHeader { let mut header = first.block_header.clone().expect("exported blocks include headers"); header.hash = header.parent_hash; - header.number = header.number.saturating_sub(U256::from(1)); + header.number = header.number.saturating_sub(U256::ONE); header.gas_used = U256::ZERO; header.transactions_trie = B256::ZERO; header.receipt_trie = B256::ZERO; @@ -152,8 +191,7 @@ fn export_block(input: &CapturedBlock) -> Result { transactions: Some( block .body - .transactions - .iter() + .transactions() .zip(input.transactions.iter()) .map(|(tx, input_tx)| export_transaction(tx, input_tx.signer)) .collect(), @@ -226,7 +264,7 @@ fn export_transaction(tx: &EthereumTxEnvelope, signer: Address) -> Tr s: signature.s(), v: U256::from(signature_v(tx_type, signature.v(), tx.chain_id())), value: tx.value(), - to: tx.kind().into_to(), + to: tx.to(), chain_id: tx.chain_id().map(U256::from), access_list: tx.access_list().map(export_access_list), max_fee_per_gas: tx.tx_type().is_dynamic_fee().then(|| U256::from(tx.max_fee_per_gas())), @@ -278,3 +316,30 @@ fn export_authorization_list(authorizations: &[SignedAuthorization]) -> Vec>(), + [BlobFork::Cancun, BlobFork::Prague, BlobFork::Osaka, BlobFork::Bpo1] + ); + assert_eq!(schedule[&BlobFork::Bpo1].params(SpecId::OSAKA).update_fraction, 8_346_193); + } +} diff --git a/crates/cli/src/capture/mod.rs b/crates/cli/src/capture/mod.rs index 6eb880faa..d150b6992 100644 --- a/crates/cli/src/capture/mod.rs +++ b/crates/cli/src/capture/mod.rs @@ -109,7 +109,7 @@ async fn capture( for (tx_index, ((pre_trace, diff_trace), tx)) in pre_traces .iter() .zip(diff_traces.iter()) - .zip(consensus_block.body.transactions.iter()) + .zip(consensus_block.body.transactions()) .enumerate() { let pre = parse::trace_result(pre_trace)?; @@ -122,7 +122,7 @@ async fn capture( } overlay.apply_withdrawals(&consensus_block); - let spec = ethereum::mainnet_spec_for_header(&consensus_block.header); + let spec = ethereum::mainnet_spec_for_header(&consensus_block.header)?; let _version_index = builder.version_index(spec)?; let block_transaction_count = transactions.len(); transaction_count += block_transaction_count; @@ -214,8 +214,7 @@ async fn prepare_block(block: FetchedBlock) -> std::result::Result SpecId { - spec_for_hardfork(EthereumHardfork::from_chain_and_timestamp( +pub(crate) fn mainnet_spec_for_header(header: &Header) -> Result { + let hardfork = EthereumHardfork::from_chain_and_timestamp( alloy_chains::Chain::mainnet(), header.timestamp, - )) + ); + hardfork + .and_then(spec_for_hardfork) + .ok_or(CaptureError::UnsupportedHardfork { timestamp: header.timestamp }) } -const fn spec_for_hardfork(hardfork: Option) -> SpecId { - match hardfork { - Some(EthereumHardfork::Frontier) => SpecId::FRONTIER, - Some(EthereumHardfork::Homestead | EthereumHardfork::Dao) => SpecId::HOMESTEAD, - Some(EthereumHardfork::Tangerine) => SpecId::TANGERINE, - Some(EthereumHardfork::SpuriousDragon) => SpecId::SPURIOUS_DRAGON, - Some(EthereumHardfork::Byzantium | EthereumHardfork::Constantinople) => SpecId::BYZANTIUM, - Some(EthereumHardfork::Petersburg) => SpecId::PETERSBURG, - Some(EthereumHardfork::Istanbul | EthereumHardfork::MuirGlacier) => SpecId::ISTANBUL, - Some(EthereumHardfork::Berlin) => SpecId::BERLIN, - Some( - EthereumHardfork::London - | EthereumHardfork::ArrowGlacier - | EthereumHardfork::GrayGlacier, - ) => SpecId::LONDON, - Some(EthereumHardfork::Paris) => SpecId::MERGE, - Some(EthereumHardfork::Shanghai) => SpecId::SHANGHAI, - Some(EthereumHardfork::Cancun) => SpecId::CANCUN, - Some(EthereumHardfork::Prague) => SpecId::PRAGUE, - Some( - EthereumHardfork::Osaka - | EthereumHardfork::Bpo1 - | EthereumHardfork::Bpo2 - | EthereumHardfork::Bpo3 - | EthereumHardfork::Bpo4 - | EthereumHardfork::Bpo5, - ) => SpecId::OSAKA, - Some(EthereumHardfork::Amsterdam) => SpecId::AMSTERDAM, - Some(_) => SpecId::AMSTERDAM, - None => SpecId::FRONTIER, - } +const fn spec_for_hardfork(hardfork: EthereumHardfork) -> Option { + Some(match hardfork { + EthereumHardfork::Frontier => SpecId::FRONTIER, + EthereumHardfork::Homestead | EthereumHardfork::Dao => SpecId::HOMESTEAD, + EthereumHardfork::Tangerine => SpecId::TANGERINE, + EthereumHardfork::SpuriousDragon => SpecId::SPURIOUS_DRAGON, + EthereumHardfork::Byzantium | EthereumHardfork::Constantinople => SpecId::BYZANTIUM, + EthereumHardfork::Petersburg => SpecId::PETERSBURG, + EthereumHardfork::Istanbul | EthereumHardfork::MuirGlacier => SpecId::ISTANBUL, + EthereumHardfork::Berlin => SpecId::BERLIN, + EthereumHardfork::London + | EthereumHardfork::ArrowGlacier + | EthereumHardfork::GrayGlacier => SpecId::LONDON, + EthereumHardfork::Paris => SpecId::MERGE, + EthereumHardfork::Shanghai => SpecId::SHANGHAI, + EthereumHardfork::Cancun => SpecId::CANCUN, + EthereumHardfork::Prague => SpecId::PRAGUE, + EthereumHardfork::Osaka + | EthereumHardfork::Bpo1 + | EthereumHardfork::Bpo2 + | EthereumHardfork::Bpo3 + | EthereumHardfork::Bpo4 + | EthereumHardfork::Bpo5 => SpecId::OSAKA, + EthereumHardfork::Amsterdam => SpecId::AMSTERDAM, + _ => return None, + }) } diff --git a/crates/cli/src/fixture.rs b/crates/cli/src/fixture.rs index c541cc659..ec16c30c6 100644 --- a/crates/cli/src/fixture.rs +++ b/crates/cli/src/fixture.rs @@ -1,24 +1,7 @@ use crate::error::{Error, Result}; -use serde::de::{DeserializeSeed, Deserializer, IgnoredAny, MapAccess, Visitor}; -use serde_json::Value; -use std::{fmt, path::Path}; - -pub(crate) struct FixtureInput { - pub(crate) json: Value, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum FixtureKind { - StateTest, - BlockchainTest, -} - -pub(crate) fn read(path: &Path) -> Result { - let text = read_text(path)?; - let json = serde_json::from_str(&text) - .map_err(|source| Error::DecodeJson { path: path.to_path_buf(), source })?; - Ok(FixtureInput { json }) -} +use evm2_eest::FixtureKind; +use serde::de::IgnoredAny; +use std::{collections::BTreeMap, path::Path}; pub(crate) fn read_blockchain(path: &Path) -> Result { evm2_eest::read_blockchain_fixture(path) @@ -30,135 +13,15 @@ pub(crate) fn read_text(path: &Path) -> Result { .map_err(|source| Error::ReadInput { path: path.to_path_buf(), source }) } -pub(crate) fn is_binary_path(path: &Path) -> bool { - evm2_eest::is_binary_fixture_path(path) -} - +/// Detects the kind of a JSON fixture from its text. pub(crate) fn detect_str(path: &Path, input: &str) -> Result> { - struct FixtureKindVisitor; - - impl<'de> Visitor<'de> for FixtureKindVisitor { - type Value = Option; - - fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str("an EEST fixture object") - } - - fn visit_map(self, mut map: A) -> std::result::Result - where - A: MapAccess<'de>, - { - let mut kind = None; - while map.next_key::()?.is_some() { - if kind.is_none() { - kind = map.next_value_seed(FixtureCaseVisitor)?; - } else { - map.next_value::()?; - } - } - Ok(kind) - } - } - - struct FixtureCaseVisitor; - - impl<'de> DeserializeSeed<'de> for FixtureCaseVisitor { - type Value = Option; - - fn deserialize(self, deserializer: D) -> std::result::Result - where - D: serde::Deserializer<'de>, - { - deserializer.deserialize_any(self) - } - } - - impl<'de> Visitor<'de> for FixtureCaseVisitor { - type Value = Option; - - fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str("an EEST fixture case object") - } - - fn visit_map(self, mut map: A) -> std::result::Result - where - A: MapAccess<'de>, - { - let mut is_blockchain = false; - let mut is_state = false; - while let Some(field) = map.next_key::<&str>()? { - is_blockchain |= - matches!(field, "blocks" | "genesisBlockHeader" | "lastblockhash" | "network"); - is_state |= matches!(field, "env" | "post" | "transaction"); - map.next_value::()?; - } - Ok(if is_blockchain { - Some(FixtureKind::BlockchainTest) - } else if is_state { - Some(FixtureKind::StateTest) - } else { - None - }) - } - } - - serde_json::Deserializer::from_str(input) - .deserialize_any(FixtureKindVisitor) + FixtureKind::from_json(input) .map_err(|source| Error::DecodeJson { path: path.to_path_buf(), source }) } -pub(crate) fn detect(value: &Value) -> Option { - let cases = value.as_object()?; - let first = cases.values().find_map(Value::as_object)?; - - if has_any(first, &["blocks", "genesisBlockHeader", "lastblockhash", "network"]) { - return Some(FixtureKind::BlockchainTest); - } - if has_any(first, &["env", "post", "transaction"]) { - return Some(FixtureKind::StateTest); - } - None -} - -pub(crate) fn test_names(value: &Value) -> Option> { - let mut names = value.as_object()?.keys().map(String::as_str).collect::>(); - names.sort_unstable(); - Some(names) -} - -fn has_any(object: &serde_json::Map, fields: &[&str]) -> bool { - fields.iter().any(|field| object.contains_key(*field)) -} - -#[cfg(test)] -mod tests { - use super::{FixtureKind, detect, detect_str, test_names}; - use serde_json::json; - use std::path::Path; - - #[test] - fn detects_blockchain_fixture() { - let value = json!({"case": {"blocks": [], "pre": {}, "network": "Prague"}}); - assert_eq!(detect(&value), Some(FixtureKind::BlockchainTest)); - assert_eq!( - detect_str(Path::new("fixture.json"), &value.to_string()).unwrap(), - Some(FixtureKind::BlockchainTest) - ); - } - - #[test] - fn detects_state_fixture() { - let value = json!({"case": {"env": {}, "pre": {}, "post": {}, "transaction": {}}}); - assert_eq!(detect(&value), Some(FixtureKind::StateTest)); - assert_eq!( - detect_str(Path::new("fixture.json"), &value.to_string()).unwrap(), - Some(FixtureKind::StateTest) - ); - } - - #[test] - fn lists_top_level_case_names() { - let value = json!({"b": {"env": {}}, "a": {"env": {}}}); - assert_eq!(test_names(&value).unwrap(), ["a", "b"]); - } +/// Returns the sorted top-level test names of a JSON fixture. +pub(crate) fn test_names(path: &Path, input: &str) -> Result> { + let cases = serde_json::from_str::>(input) + .map_err(|source| Error::DecodeJson { path: path.to_path_buf(), source })?; + Ok(cases.into_keys().collect()) } diff --git a/crates/cli/src/fuzzer/backend.rs b/crates/cli/src/fuzzer/backend.rs index 38c73ca88..1f3472f77 100644 --- a/crates/cli/src/fuzzer/backend.rs +++ b/crates/cli/src/fuzzer/backend.rs @@ -5,20 +5,20 @@ use crate::fuzzer::{ apply_account_changes, canonical_accounts, canonical_log, state_from_revm, }, }; -use alloy_primitives::{Address, map::HashMap}; +use alloy_primitives::map::AddressMap; use evm2::{ - BaseEvmConfigSelector, BaseEvmTypes, Evm, EvmConfigSelector, Precompiles, SpecId, + BaseEvmConfigSelector, BaseEvmTypes, Evm, EvmConfigSelector, Precompiles, bytecode::Bytecode, ethereum::ethereum_tx_registry, evm::{AccountInfo as Evm2AccountInfo, InMemoryDB}, interpreter::InstrStop, }; +use evm2_cli::reference; use revm::{ ExecuteCommitEvm, ExecuteEvm, MainBuilder, MainContext, context::{CfgEnv, Context}, context_interface::either::Either, database::{EmptyDB as RevmEmptyDB, InMemoryDB as RevmInMemoryDB, State as RevmState}, - primitives::hardfork::SpecId as RevmSpecId, }; pub(crate) trait EvmBackend { @@ -66,13 +66,13 @@ impl EvmBackend for Evm2Backend { Ok(result) => { let mut state = CanonicalState::default(); let Ok(tx_result) = result.commit_with(&mut state); - let output = if tx_result.status || tx_result.stop == InstrStop::Revert { + let output = if tx_result.is_success() || tx_result.stop == InstrStop::Revert { Some(tx_result.output.to_vec()) } else { None }; receipts.push(TxReceipt { - kind: if tx_result.status { + kind: if tx_result.is_success() { FuzzOutcomeKind::Success } else { FuzzOutcomeKind::RevertOrHalt @@ -99,7 +99,7 @@ type RevmExecutor = revm::MainnetEvm, - accounts: HashMap, + accounts: AddressMap, } impl EvmBackend for RevmBackend { @@ -109,7 +109,7 @@ impl EvmBackend for RevmBackend { fn run(&mut self, case: &EvmCase) -> Outcome { let mut cfg = CfgEnv::new(); - cfg.set_spec_and_mainnet_gas_params(revm_spec(case.spec)); + cfg.set_spec_and_mainnet_gas_params(reference::spec_id(case.spec)); cfg = cfg.disable_tx_chain_id_check(); let context = Context::mainnet() .with_cfg(cfg) @@ -118,7 +118,7 @@ impl EvmBackend for RevmBackend { let evm = if let Some(evm) = &mut self.evm { evm.ctx = context; evm.instruction = revm::handler::instructions::EthInstructions::new_mainnet_with_spec( - revm_spec(case.spec), + reference::spec_id(case.spec), ); evm } else { @@ -202,27 +202,6 @@ fn revm_db(case: &EvmCase) -> RevmInMemoryDB { db } -const fn revm_spec(spec: SpecId) -> RevmSpecId { - match spec { - SpecId::FRONTIER => RevmSpecId::FRONTIER, - SpecId::HOMESTEAD => RevmSpecId::HOMESTEAD, - SpecId::TANGERINE => RevmSpecId::TANGERINE, - SpecId::SPURIOUS_DRAGON => RevmSpecId::SPURIOUS_DRAGON, - SpecId::BYZANTIUM => RevmSpecId::BYZANTIUM, - SpecId::PETERSBURG => RevmSpecId::PETERSBURG, - SpecId::ISTANBUL => RevmSpecId::ISTANBUL, - SpecId::BERLIN => RevmSpecId::BERLIN, - SpecId::LONDON => RevmSpecId::LONDON, - SpecId::MERGE => RevmSpecId::MERGE, - SpecId::SHANGHAI => RevmSpecId::SHANGHAI, - SpecId::CANCUN => RevmSpecId::CANCUN, - SpecId::PRAGUE => RevmSpecId::PRAGUE, - SpecId::OSAKA => RevmSpecId::OSAKA, - SpecId::AMSTERDAM => RevmSpecId::AMSTERDAM, - _ => RevmSpecId::CANCUN, - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/cli/src/fuzzer/case.rs b/crates/cli/src/fuzzer/case.rs index 19e0d7a81..18783caa6 100644 --- a/crates/cli/src/fuzzer/case.rs +++ b/crates/cli/src/fuzzer/case.rs @@ -12,7 +12,10 @@ use alloy_eips::{ eip2930::{AccessList, AccessListItem}, eip7702::{Authorization, SignedAuthorization}, }; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256, map::HashMap}; +use alloy_primitives::{ + Address, B256, Bytes, TxKind, U256, + map::{IndexMap, U256IndexMap}, +}; use evm2::{ SpecId, env::{BlockEnv, BlockEnvExt}, @@ -24,15 +27,14 @@ use revm::{ primitives::TxKind as RevmTxKind, }; use secp256k1::{Message, SecretKey, ecdsa::RecoverableSignature}; -use serde::{Deserialize, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; use std::{fmt, sync::OnceLock}; -pub(crate) const CALLER: Address = Address::new([0x10; 20]); -pub(crate) const TARGET: Address = Address::new([0x20; 20]); -pub(crate) const BENEFICIARY: Address = Address::new([0x30; 20]); +pub(crate) const CALLER: Address = Address::repeat_byte(0x10); +pub(crate) const TARGET: Address = Address::repeat_byte(0x20); +pub(crate) const BENEFICIARY: Address = Address::repeat_byte(0x30); const CALLER_BALANCE: U256 = U256::from_limbs([0, 0, 1, 0]); -const EIP7702_DELEGATED_TARGET: Address = - Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6]); +const EIP7702_DELEGATED_TARGET: Address = Address::with_last_byte(6); #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] pub(crate) struct EvmCase { @@ -69,7 +71,7 @@ impl Default for CaseGenerator { fixed_eip7702_authority(), EIP7702_DELEGATED_TARGET, Address::ZERO, - Address::new([0xff; 20]), + Address::repeat_byte(0xff), ]; address_pool.extend((1..=10).map(Address::with_last_byte)); let mut call_pool = @@ -96,19 +98,21 @@ impl CaseGenerator { pub(crate) fn generate(&mut self, rng: &mut Gen) -> &EvmCase { let Self { case, program, address_pool, call_pool } = self; let EvmCase { spec, block, tx, extra_txs, features, accounts } = case; - *spec = match rng.range(13) { + *spec = match rng.range(15) { 0 => SpecId::FRONTIER, 1 => SpecId::HOMESTEAD, 2 => SpecId::TANGERINE, 3 => SpecId::SPURIOUS_DRAGON, 4 => SpecId::BYZANTIUM, - 5 => SpecId::ISTANBUL, - 6 => SpecId::BERLIN, - 7 => SpecId::LONDON, - 8 => SpecId::SHANGHAI, - 9 => SpecId::CANCUN, - 10 => SpecId::PRAGUE, - 11 => SpecId::OSAKA, + 5 => SpecId::PETERSBURG, + 6 => SpecId::ISTANBUL, + 7 => SpecId::BERLIN, + 8 => SpecId::LONDON, + 9 => SpecId::MERGE, + 10 => SpecId::SHANGHAI, + 11 => SpecId::CANCUN, + 12 => SpecId::PRAGUE, + 13 => SpecId::OSAKA, _ => SpecId::AMSTERDAM, }; let spec = *spec; @@ -357,10 +361,10 @@ fn generate_access_list(rng: &mut Gen, accounts: &[CaseAccount], list: &mut Acce item.address = account.address; item.storage_keys.clear(); for key in account.storage.keys().take(rng.range_inclusive(0, 3)) { - item.storage_keys.push(B256::from(key.to_be_bytes::<32>())); + item.storage_keys.push(B256::from(*key)); } if item.storage_keys.is_empty() && rng.one_in(2) { - item.storage_keys.push(B256::from(rng.biased_word().to_be_bytes::<32>())); + item.storage_keys.push(B256::from(rng.biased_word())); } } } @@ -389,12 +393,12 @@ fn generate_eip7702_authorization(rng: &mut Gen) -> SignedAuthorization { nonce: 1, }), 3 => signed_eip7702_auth(Authorization { - chain_id: U256::from(1), + chain_id: U256::ONE, address: rng.pick(&[EIP7702_DELEGATED_TARGET, TARGET, Address::with_last_byte(8)]), nonce: 1, }), 4 => signed_eip7702_auth(Authorization { - chain_id: rng.pick(&[U256::from(1), U256::from(2)]), + chain_id: rng.pick(&[U256::ONE, U256::from(2)]), address: EIP7702_DELEGATED_TARGET, nonce: rng.pick(&[0, 2, u64::MAX]), }), @@ -429,7 +433,7 @@ fn add_eip7702_accounts<'a>( if auth.y_parity() > 1 { features.insert(FuzzFeatures::EIP7702_AUTH_BAD_SIGNATURE); } - if auth.chain_id() != &U256::ZERO && auth.chain_id() != &U256::from(1) { + if auth.chain_id() != &U256::ZERO && auth.chain_id() != &U256::ONE { features.insert(FuzzFeatures::EIP7702_AUTH_BAD_CHAIN); } if auth.nonce() != 1 { @@ -447,7 +451,7 @@ fn add_eip7702_accounts<'a>( balance: U256::from(1_000), nonce: 0, code: tiny_callee_code(rng, SpecId::PRAGUE), - storage: HashMap::default(), + storage: IndexMap::default(), }, ); @@ -490,7 +494,7 @@ fn upsert_account(accounts: &mut Vec, account: CaseAccount) { } fn eip7702_authority_account(address: Address, nonce: u64, code: Bytes) -> CaseAccount { - CaseAccount { address, balance: U256::ZERO, nonce, code, storage: HashMap::default() } + CaseAccount { address, balance: U256::ZERO, nonce, code, storage: IndexMap::default() } } fn eip7702_designation(address: Address) -> Bytes { @@ -512,7 +516,7 @@ pub(crate) fn fixed_eip7702_auth() -> SignedAuthorization { static AUTH: OnceLock = OnceLock::new(); AUTH.get_or_init(|| { signed_eip7702_auth(Authorization { - chain_id: U256::from(1), + chain_id: U256::ONE, address: EIP7702_DELEGATED_TARGET, nonce: 1, }) @@ -648,21 +652,18 @@ impl CaseTx { self.caller, ), FuzzTxKind::Eip7702 => Recovered::new_unchecked( - TxEnvelope::Eip7702( - TxEip7702 { - chain_id: 1, - nonce: self.nonce, - gas_limit: self.gas_limit, - max_fee_per_gas: self.gas_price, - max_priority_fee_per_gas: 0, - to: self.target, - value: self.value, - access_list: self.access_list.clone(), - authorization_list: self.eip7702_authorization_list(), - input: self.input.clone(), - } - .into(), - ), + TxEnvelope::from(TxEip7702 { + chain_id: 1, + nonce: self.nonce, + gas_limit: self.gas_limit, + max_fee_per_gas: self.gas_price, + max_priority_fee_per_gas: 0, + to: self.target, + value: self.value, + access_list: self.access_list.clone(), + authorization_list: self.eip7702_authorization_list(), + input: self.input.clone(), + }), self.caller, ), } @@ -737,17 +738,7 @@ pub(crate) struct CaseAccount { pub(crate) balance: U256, pub(crate) nonce: u64, pub(crate) code: Bytes, - #[serde(serialize_with = "serialize_storage")] - pub(crate) storage: HashMap, -} - -fn serialize_storage( - storage: &HashMap, - serializer: S, -) -> Result { - let mut entries = storage.iter().collect::>(); - entries.sort_unstable_by_key(|&(key, _)| key); - serializer.collect_map(entries) + pub(crate) storage: U256IndexMap, } mod spec_serde { @@ -758,7 +749,7 @@ mod spec_serde { where S: Serializer, { - serializer.serialize_str(name(*spec)) + serializer.serialize_str(spec.name()) } pub(super) fn deserialize<'de, D>(deserializer: D) -> Result @@ -766,49 +757,7 @@ mod spec_serde { D: Deserializer<'de>, { let name = String::deserialize(deserializer)?; - from_name(&name).ok_or_else(|| de::Error::custom(format!("unknown spec id {name:?}"))) - } - - const fn name(spec: SpecId) -> &'static str { - match spec { - SpecId::FRONTIER => "FRONTIER", - SpecId::HOMESTEAD => "HOMESTEAD", - SpecId::TANGERINE => "TANGERINE", - SpecId::SPURIOUS_DRAGON => "SPURIOUS_DRAGON", - SpecId::BYZANTIUM => "BYZANTIUM", - SpecId::PETERSBURG => "PETERSBURG", - SpecId::ISTANBUL => "ISTANBUL", - SpecId::BERLIN => "BERLIN", - SpecId::LONDON => "LONDON", - SpecId::MERGE => "MERGE", - SpecId::SHANGHAI => "SHANGHAI", - SpecId::CANCUN => "CANCUN", - SpecId::PRAGUE => "PRAGUE", - SpecId::OSAKA => "OSAKA", - SpecId::AMSTERDAM => "AMSTERDAM", - _ => "CANCUN", - } - } - - fn from_name(name: &str) -> Option { - match name { - "FRONTIER" => Some(SpecId::FRONTIER), - "HOMESTEAD" => Some(SpecId::HOMESTEAD), - "TANGERINE" => Some(SpecId::TANGERINE), - "SPURIOUS_DRAGON" => Some(SpecId::SPURIOUS_DRAGON), - "BYZANTIUM" => Some(SpecId::BYZANTIUM), - "PETERSBURG" => Some(SpecId::PETERSBURG), - "ISTANBUL" => Some(SpecId::ISTANBUL), - "BERLIN" => Some(SpecId::BERLIN), - "LONDON" => Some(SpecId::LONDON), - "MERGE" => Some(SpecId::MERGE), - "SHANGHAI" => Some(SpecId::SHANGHAI), - "CANCUN" => Some(SpecId::CANCUN), - "PRAGUE" => Some(SpecId::PRAGUE), - "OSAKA" => Some(SpecId::OSAKA), - "AMSTERDAM" => Some(SpecId::AMSTERDAM), - _ => None, - } + name.parse().map_err(|_| de::Error::custom(format!("unknown spec id {name:?}"))) } } @@ -822,30 +771,23 @@ mod tests { for seed in 0..512 { let mut rng = Gen::new(seed); let mut fresh_rng = Gen::new(seed); - let mut reused = generator.generate(&mut rng).clone(); - let mut fresh = CaseGenerator::default().generate(&mut fresh_rng).clone(); + let reused = generator.generate(&mut rng).clone(); + let fresh = CaseGenerator::default().generate(&mut fresh_rng).clone(); assert_eq!(rng.bytes(32), fresh_rng.bytes(32), "RNG state at seed {seed}"); - // HashMap iteration can select different storage keys, but not different counts. - for case in [&mut reused, &mut fresh] { - for tx in core::iter::once(&mut case.tx).chain(&mut case.extra_txs) { - for item in &mut tx.access_list.0 { - item.storage_keys.fill(B256::ZERO); - } - } - } assert_eq!(reused, fresh, "seed {seed}"); } } #[test] - fn storage_serialization_is_canonical() { - let mut case = CaseGenerator::default().generate(&mut Gen::new(1)).clone(); - case.accounts[0].storage = (0..32).map(|key| (U256::from(key), U256::ONE)).collect(); - let json = serde_json::to_vec(&case).unwrap(); - case.accounts[0].storage = (0..32).rev().map(|key| (U256::from(key), U256::ONE)).collect(); - assert_eq!(serde_json::to_vec(&case).unwrap(), json); - let decoded = serde_json::from_slice::(&json).unwrap(); - assert_eq!(decoded, case); - assert_eq!(serde_json::to_vec(&decoded).unwrap(), json); + fn serialized_case_is_reproducible_and_round_trips() { + for seed in 0..64 { + let case = CaseGenerator::default().generate(&mut Gen::new(seed)).clone(); + let json = serde_json::to_vec(&case).unwrap(); + let fresh = CaseGenerator::default().generate(&mut Gen::new(seed)).clone(); + assert_eq!(serde_json::to_vec(&fresh).unwrap(), json, "seed {seed}"); + let decoded = serde_json::from_slice::(&json).unwrap(); + assert_eq!(decoded, case, "seed {seed}"); + assert_eq!(serde_json::to_vec(&decoded).unwrap(), json, "seed {seed}"); + } } } diff --git a/crates/cli/src/fuzzer/minimize.rs b/crates/cli/src/fuzzer/minimize.rs index c7582acf1..cdfe8ff67 100644 --- a/crates/cli/src/fuzzer/minimize.rs +++ b/crates/cli/src/fuzzer/minimize.rs @@ -33,7 +33,7 @@ fn minimize_storage(backends: &mut [&mut dyn EvmBackend; 2], case: &mut EvmCase) for account_index in 0..case.accounts.len() { while let Some(key) = case.accounts[account_index].storage.keys().min().copied() { let mut candidate = case.clone(); - candidate.accounts[account_index].storage.remove(&key); + candidate.accounts[account_index].storage.shift_remove(&key); if differs(backends, &candidate) { *case = candidate; } else { diff --git a/crates/cli/src/fuzzer/normalize.rs b/crates/cli/src/fuzzer/normalize.rs index c86c7e93b..efc8d3aaa 100644 --- a/crates/cli/src/fuzzer/normalize.rs +++ b/crates/cli/src/fuzzer/normalize.rs @@ -1,5 +1,8 @@ use crate::fuzzer::case::EvmCase; -use alloy_primitives::{Address, B256, U256, keccak256, map::HashMap}; +use alloy_primitives::{ + Address, B256, U256, keccak256, + map::{AddressMap, HashMap}, +}; use core::convert::Infallible; use evm2::evm::{AccountChangeRef, StateChangeSink, StorageChange, registry::HandlerError}; use revm::{ @@ -32,7 +35,7 @@ impl Outcome { kind: last.kind, gas_used: last.gas_used, output: last.output.clone(), - logs: receipts.iter().flat_map(|receipt| receipt.logs.clone()).collect(), + logs: receipts.iter().flat_map(|receipt| receipt.logs.iter()).cloned().collect(), state: last.state.clone(), error: last.error.clone(), receipts, @@ -95,7 +98,7 @@ impl fmt::Display for FuzzOutcomeKind { #[derive(Clone, Debug, Default, PartialEq, Eq)] pub(crate) struct CanonicalState { - pub(crate) accounts: HashMap>, + pub(crate) accounts: AddressMap>, pub(crate) storage: HashMap<(Address, U256), U256>, } @@ -151,7 +154,9 @@ impl fmt::Display for FuzzError { impl From for FuzzError { fn from(error: HandlerError) -> Self { match error { - HandlerError::IntrinsicGasTooLow { .. } => Self::IntrinsicGasTooLow, + HandlerError::IntrinsicGasTooLow { .. } + | HandlerError::GasLimitBelowFloor { .. } + | HandlerError::IntrinsicGasGreaterThanCap { .. } => Self::IntrinsicGasTooLow, HandlerError::InsufficientFunds { .. } => Self::InsufficientFunds, HandlerError::InvalidNonce { .. } => Self::InvalidNonce, HandlerError::NonceOverflow => Self::NonceOverflow, @@ -242,7 +247,7 @@ impl StateChangeSink for CanonicalState { pub(crate) fn state_from_revm( state: &revm::state::EvmState, - original_accounts: &HashMap, + original_accounts: &AddressMap, ) -> CanonicalState { let mut canonical = CanonicalState::default(); for (&address, account) in state { @@ -293,10 +298,7 @@ pub(crate) fn state_from_revm( canonical } -pub(crate) fn canonical_accounts( - case: &EvmCase, - accounts: &mut HashMap, -) { +pub(crate) fn canonical_accounts(case: &EvmCase, accounts: &mut AddressMap) { accounts.clear(); accounts.extend(case.accounts.iter().map(|account| { ( @@ -311,7 +313,7 @@ pub(crate) fn canonical_accounts( } pub(crate) fn apply_account_changes( - accounts: &mut HashMap, + accounts: &mut AddressMap, state: &CanonicalState, ) { for (&address, account) in &state.accounts { @@ -349,7 +351,7 @@ mod tests { }, ), ( - HandlerError::IntrinsicGasTooLow { required: 66_200, got: 60_000 }, + HandlerError::GasLimitBelowFloor { required: 66_200, got: 60_000 }, InvalidTransaction::GasFloorMoreThanGasLimit { gas_floor: 66_200, gas_limit: 60_000, @@ -368,6 +370,13 @@ mod tests { ), (HandlerError::NonceOverflow, InvalidTransaction::NonceOverflowInTransaction), (HandlerError::UnsupportedTransactionType(4), InvalidTransaction::Eip7702NotSupported), + ( + HandlerError::IntrinsicGasGreaterThanCap { required: 17_000_000, cap: 16_777_216 }, + InvalidTransaction::GasFloorMoreThanGasLimit { + gas_floor: 17_000_000, + gas_limit: 16_777_216, + }, + ), (HandlerError::EmptyBlobs, InvalidTransaction::EmptyBlobs), ( HandlerError::TooManyBlobs { have: 10, max: 6 }, diff --git a/crates/cli/src/fuzzer/precompile.rs b/crates/cli/src/fuzzer/precompile.rs index aad18f1d4..468acadc5 100644 --- a/crates/cli/src/fuzzer/precompile.rs +++ b/crates/cli/src/fuzzer/precompile.rs @@ -10,9 +10,7 @@ pub(crate) struct PrecompileTarget { impl PrecompileTarget { pub(crate) fn address(self) -> Address { - let mut bytes = [0; 20]; - bytes[18..].copy_from_slice(&self.number.to_be_bytes()); - Address::new(bytes) + Address::left_padding_from(&self.number.to_be_bytes()) } pub(crate) const fn feature(self) -> FuzzFeatures { diff --git a/crates/cli/src/fuzzer/program.rs b/crates/cli/src/fuzzer/program.rs index e7187c91b..d5a21e0b3 100644 --- a/crates/cli/src/fuzzer/program.rs +++ b/crates/cli/src/fuzzer/program.rs @@ -1,12 +1,13 @@ use crate::fuzzer::{features::FuzzFeatures, precompile, rng::Gen}; use alloy_primitives::{Address, Bytes, U256}; -use evm2::{SpecId, interpreter::op}; +use evm2::{BaseEvmTypes, OpcodeConfig, SpecId, interpreter::op}; #[derive(Default)] pub(crate) struct Program { code: Vec, stack_height: usize, features: FuzzFeatures, + spec: SpecId, } impl Program { @@ -20,6 +21,7 @@ impl Program { self.code.clear(); self.stack_height = 0; self.features = FuzzFeatures::default(); + self.spec = spec; let statements = rng.range_inclusive(1, 48); for _ in 0..statements { match rng.range(100) { @@ -30,31 +32,16 @@ impl Program { 69..=73 => self.calldata(rng), 74..=79 => self.external_account(rng, spec, addresses), 80..=83 => self.log(rng), - 84..=86 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.precompile_call(rng, spec) - } - 84..=86 => self.literal(rng, spec), - 87..=89 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.generic_call(rng, spec, call_addresses) - } - 87..=89 => self.literal(rng, spec), - 90..=91 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.returndata(rng, spec, call_addresses) - } - 90..=91 => self.literal(rng, spec), - 92..=93 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.create(rng, spec) - } - 92..=93 => self.literal(rng, spec), + 84..=86 => self.precompile_call(rng, spec), + 87..=89 => self.generic_call(rng, spec, call_addresses), + 90..=91 => self.returndata(rng, spec, call_addresses), + 92..=93 => self.create(rng, spec), 94..=95 => self.cancun(rng, spec), 96 => self.jump(rng), 97 if rng.one_in(3) => self.stack_shuffle(rng, spec), 97 if rng.one_in(2) => self.selfdestruct(rng, addresses), 97 => self.stack_cleanup(), - 98 if self.allow_fork_feature(rng, spec, SpecId::SPURIOUS_DRAGON) => { - self.raw_invalidish(rng) - } - 98 => self.literal(rng, spec), + 98 => self.raw_invalidish(rng), _ => self.literal(rng, spec), } while self.stack_height > 16 { @@ -63,21 +50,22 @@ impl Program { } match rng.range(10) { 0 => self.finish_return(false), - 1 if self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) => self.finish_return(true), + 1 if self.allow_opcodes(rng, spec, &[op::REVERT]) => self.finish_return(true), _ => self.emit(op::STOP, 0, 0), } (Bytes::copy_from_slice(&self.code), self.features) } - fn allow_fork_feature(&mut self, rng: &mut Gen, spec: SpecId, since: SpecId) -> bool { - if spec.enables(since) { - return true; - } - if rng.one_in(20) { - self.mark(FuzzFeatures::FORK_INVALID_OPCODE); + /// Returns whether a snippet using `opcodes` may be emitted for `spec`. + /// + /// Opcodes the fork does not define are emitted once in 20 to cover their invalid-opcode + /// behavior. + fn allow_opcodes(&self, rng: &mut Gen, spec: SpecId, opcodes: &[u8]) -> bool { + let config = OpcodeConfig::::base_for_spec(spec); + if opcodes.iter().all(|&opcode| config.is_defined(opcode)) { return true; } - false + rng.one_in(20) } fn mark(&mut self, feature: FuzzFeatures) { @@ -109,14 +97,14 @@ impl Program { op::XOR, op::BYTE, ]; - if self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) { + if self.allow_opcodes(rng, spec, &[op::SHL, op::SHR, op::SAR]) { ops.extend([op::SHL, op::SHR, op::SAR]); } self.emit(rng.pick(&ops), 2, 1); } 17..=18 => { self.push_word(rng.biased_word()); - let opcode = if self.allow_fork_feature(rng, spec, SpecId::OSAKA) { + let opcode = if self.allow_opcodes(rng, spec, &[op::CLZ]) { rng.pick(&[op::ISZERO, op::NOT, op::CLZ]) } else if rng.one_in(2) { op::ISZERO @@ -166,7 +154,7 @@ impl Program { self.push_u64(offset); self.emit(op::CODECOPY, 3, 0); } - 6 if self.allow_fork_feature(rng, spec, SpecId::CANCUN) => { + 6 if self.allow_opcodes(rng, spec, &[op::MCOPY]) => { self.push_u64(rng.pick(&[0, 1, 31, 32, 64])); self.push_u64(rng.pick(&[0, 32, 64, 96])); self.push_u64(offset); @@ -206,13 +194,13 @@ impl Program { op::GASLIMIT, op::GAS, ]; - if self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) { + if self.allow_opcodes(rng, spec, &[op::CHAINID]) { ops.push(op::CHAINID); } - if self.allow_fork_feature(rng, spec, SpecId::LONDON) { + if self.allow_opcodes(rng, spec, &[op::BASEFEE]) { ops.push(op::BASEFEE); } - if self.allow_fork_feature(rng, spec, SpecId::AMSTERDAM) { + if self.allow_opcodes(rng, spec, &[op::SLOTNUM]) { ops.push(op::SLOTNUM); } self.emit(rng.pick(&ops), 0, 1); @@ -251,7 +239,7 @@ impl Program { self.push_address(address); self.emit(op::EXTCODESIZE, 1, 1); } - 2 if self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) => { + 2 if self.allow_opcodes(rng, spec, &[op::EXTCODEHASH]) => { self.push_address(address); self.emit(op::EXTCODEHASH, 1, 1); } @@ -262,7 +250,7 @@ impl Program { self.push_address(address); self.emit(op::EXTCODECOPY, 4, 0); } - _ if self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) => { + _ if self.allow_opcodes(rng, spec, &[op::SELFBALANCE]) => { self.emit(op::SELFBALANCE, 0, 1) } _ => { @@ -307,7 +295,7 @@ impl Program { let return_offset: u64 = rng.pick(&[0, 32, 64, 128]); let return_len: u64 = rng.pick(&[0, 1, 20, 32, 64, 96, 128, 256]); let gas: u64 = rng.pick(&[0, 1, 3_000, 10_000, 50_000, 200_000, 2_000_000]); - if self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) && rng.one_in(2) { + if self.allow_opcodes(rng, spec, &[op::STATICCALL]) && rng.one_in(2) { self.mark(FuzzFeatures::PRECOMPILE_STATICCALL); self.push_u64(return_len); self.push_u64(return_offset); @@ -343,7 +331,7 @@ impl Program { let return_len: u64 = rng.pick(&[0, 1, 4, 32, 64]); let gas: u64 = rng.pick(&[0, 1, 2_300, 10_000, 50_000, 200_000]); match rng.range(4) { - 0 if self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) => { + 0 if self.allow_opcodes(rng, spec, &[op::STATICCALL]) => { self.push_u64(return_len); self.push_u64(return_offset); self.push_u64(input_len); @@ -352,7 +340,7 @@ impl Program { self.push_u64(gas); self.emit(op::STATICCALL, 6, 1); } - 1 if self.allow_fork_feature(rng, spec, SpecId::HOMESTEAD) => { + 1 if self.allow_opcodes(rng, spec, &[op::DELEGATECALL]) => { self.push_u64(return_len); self.push_u64(return_offset); self.push_u64(input_len); @@ -385,7 +373,7 @@ impl Program { } fn returndata(&mut self, rng: &mut Gen, spec: SpecId, call_addresses: &[Address]) { - if !self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) { + if !self.allow_opcodes(rng, spec, &[op::RETURNDATASIZE, op::RETURNDATACOPY]) { self.literal(rng, spec); return; } @@ -404,7 +392,7 @@ impl Program { } fn cancun(&mut self, rng: &mut Gen, spec: SpecId) { - if !self.allow_fork_feature(rng, spec, SpecId::CANCUN) { + if !self.allow_opcodes(rng, spec, &[op::TLOAD, op::TSTORE, op::BLOBHASH, op::BLOBBASEFEE]) { self.literal(rng, spec); return; } @@ -431,7 +419,8 @@ impl Program { 0 => Vec::new(), 1 => vec![op::STOP], 2 => vec![op::PUSH1, 0, op::PUSH1, 0, op::RETURN], - 3 if self.allow_fork_feature(rng, spec, SpecId::BYZANTIUM) => { + 3 if self.allow_opcodes(rng, spec, &[op::REVERT]) => { + self.mark_opcode(op::REVERT); vec![op::PUSH1, 0, op::PUSH1, 0, op::REVERT] } _ => vec![ @@ -453,7 +442,7 @@ impl Program { self.push_u64(offset); self.emit(op::MSTORE, 2, 0); } - if rng.one_in(2) || !self.allow_fork_feature(rng, spec, SpecId::ISTANBUL) { + if rng.one_in(2) || !self.allow_opcodes(rng, spec, &[op::CREATE2]) { self.push_u64(initcode.len() as u64); self.push_u64(offset); self.push_u64(rng.pick(&[0, 0, 0, 1])); @@ -534,7 +523,7 @@ impl Program { while self.stack_height < 2 { self.push_word(rng.biased_word()); } - if self.allow_fork_feature(rng, spec, SpecId::AMSTERDAM) && rng.one_in(3) { + if self.allow_opcodes(rng, spec, &[op::DUPN, op::SWAPN, op::EXCHANGE]) && rng.one_in(3) { self.relative_stack_shuffle(rng); } else if rng.one_in(2) { let n = rng.range_inclusive(1, self.stack_height.min(16)); @@ -601,7 +590,7 @@ impl Program { } fn literal(&mut self, rng: &mut Gen, spec: SpecId) { - if self.allow_fork_feature(rng, spec, SpecId::SHANGHAI) && rng.one_in(8) { + if self.allow_opcodes(rng, spec, &[op::PUSH0]) && rng.one_in(8) { self.emit(op::PUSH0, 0, 1); } else if rng.one_in(8) { self.push_random_width_word(rng); @@ -625,7 +614,7 @@ impl Program { } fn push_address(&mut self, address: Address) { - self.push_word(U256::from_be_slice(address.as_slice())); + self.push_word(address.into_word().into()); } fn push_bytes_word(&mut self, bytes: &[u8]) { @@ -721,6 +710,11 @@ impl Program { op::SELFDESTRUCT => self.mark(FuzzFeatures::SELFDESTRUCT), _ => {} } + if opcode != op::INVALID + && !OpcodeConfig::::base_for_spec(self.spec).is_defined(opcode) + { + self.mark(FuzzFeatures::FORK_INVALID_OPCODE); + } } } @@ -763,4 +757,37 @@ mod tests { assert_eq!(program.features, FuzzFeatures::PUSH); } } + + #[test] + fn pre_spurious_dragon_programs_use_frontier_opcodes() { + let addresses = [Address::with_last_byte(1)]; + let mut rng = Gen::new(1); + let mut program = Program::default(); + let wanted = FuzzFeatures::CALL | FuzzFeatures::CREATE | FuzzFeatures::INVALID; + let mut seen = FuzzFeatures::default(); + for _ in 0..256 { + let (_, features) = + program.generate(&mut rng, SpecId::FRONTIER, &addresses, &addresses); + if !features.contains(FuzzFeatures::FORK_INVALID_OPCODE) { + seen |= features; + } + } + assert!(seen.contains(wanted), "{seen:?}"); + } + + #[test] + fn initcode_with_undefined_opcode_is_tagged() { + let revert_initcode = [op::PUSH1, 0, op::PUSH1, 0, op::REVERT]; + let mut rng = Gen::new(1); + let mut found = false; + for _ in 0..1024 { + let mut program = Program { spec: SpecId::FRONTIER, ..Default::default() }; + program.create(&mut rng, SpecId::FRONTIER); + if program.code.windows(revert_initcode.len()).any(|code| code == revert_initcode) { + found = true; + assert!(program.features.contains(FuzzFeatures::FORK_INVALID_OPCODE)); + } + } + assert!(found); + } } diff --git a/crates/cli/src/fuzzer/rng.rs b/crates/cli/src/fuzzer/rng.rs index 1a19be2a9..2381c4c12 100644 --- a/crates/cli/src/fuzzer/rng.rs +++ b/crates/cli/src/fuzzer/rng.rs @@ -30,7 +30,7 @@ impl Gen { } pub(crate) fn one_in(&mut self, n: usize) -> bool { - self.rng.random_range(..n) == 0 + self.range(n) == 0 } pub(crate) fn pick(&mut self, values: &[T]) -> T { diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 5660bd8ee..ad69a90b1 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -2,4 +2,5 @@ #[allow(missing_docs)] pub mod evm_bench; +pub mod reference; pub mod replay_bench; diff --git a/crates/cli/src/list.rs b/crates/cli/src/list.rs index 46e1784b8..11b42e4e9 100644 --- a/crates/cli/src/list.rs +++ b/crates/cli/src/list.rs @@ -3,9 +3,10 @@ use crate::{ error::{Error, Result}, fixture, }; +use evm2_eest::FixtureKind; pub(crate) fn run(command: List) -> Result<()> { - if fixture::is_binary_path(&command.path) { + if FixtureKind::from_path(&command.path).is_some() { let suite = fixture::read_blockchain(&command.path)?; let mut names = suite.0.keys().map(String::as_str).collect::>(); names.sort_unstable(); @@ -15,14 +16,11 @@ pub(crate) fn run(command: List) -> Result<()> { return Ok(()); } - let input = fixture::read(&command.path)?; - if fixture::detect(&input.json).is_none() { + let input = fixture::read_text(&command.path)?; + if fixture::detect_str(&command.path, &input)?.is_none() { return Err(Error::UnknownFixtureKind { path: command.path }); } - let Some(names) = fixture::test_names(&input.json) else { - return Err(Error::UnknownFixtureKind { path: command.path }); - }; - for name in names { + for name in fixture::test_names(&command.path, &input)? { println!("{name}"); } Ok(()) diff --git a/crates/cli/src/reference.rs b/crates/cli/src/reference.rs new file mode 100644 index 000000000..5630fc2b7 --- /dev/null +++ b/crates/cli/src/reference.rs @@ -0,0 +1,21 @@ +//! Conversions to the reference implementation the fuzzer and replay benchmarks compare against. + +use evm2::SpecId; +use revm::primitives::hardfork::SpecId as ReferenceSpecId; + +/// Returns the reference implementation's spec ID for `spec`. +/// +/// # Panics +/// +/// Panics when the reference implementation has no counterpart of `spec`. +pub fn spec_id(spec: SpecId) -> ReferenceSpecId { + macro_rules! convert { + ([$spec:ident] $($variant:ident $lower:ident,)*) => { + match $spec { + $(SpecId::$variant => ReferenceSpecId::$variant,)* + other => panic!("unsupported spec: {other:?}"), + } + }; + } + evm2::for_each_spec!([spec] convert) +} diff --git a/crates/cli/src/replay/mod.rs b/crates/cli/src/replay/mod.rs index 83d76620b..0f3734847 100644 --- a/crates/cli/src/replay/mod.rs +++ b/crates/cli/src/replay/mod.rs @@ -1,16 +1,15 @@ use crate::{ args::Replay, error::{Error, Result}, - fixture::{self, FixtureKind}, - style, + fixture, style, }; use alloy_primitives::U256; use evm2_eest::{ BlockchainTestBlockFailed, BlockchainTestBlockFinished, BlockchainTestBlockStarted, BlockchainTestCaseStarted, BlockchainTestExecuteConfig, BlockchainTestExecutionMode, BlockchainTestHook, BlockchainTestTransactionFailed, BlockchainTestTransactionFinished, - BlockchainTestTransactionStarted, NameFilter, StateTestExecuteConfig, StateTestExecutionMode, - execute_blockchain_tests_str, execute_blockchain_tests_suite, + BlockchainTestTransactionStarted, FixtureKind, NameFilter, StateTestExecuteConfig, + StateTestExecutionMode, execute_blockchain_tests_str, execute_blockchain_tests_suite, execute_state_tests_str_with_filter, }; use std::{ @@ -145,7 +144,7 @@ fn run_dir(dir: &Path, options: &ReplayOptions) -> Result<()> { /// Replays one fixture file, auto-detecting its kind (binary blockchain, or a /// text state/blockchain test). fn replay_file(path: &Path, options: &ReplayOptions) -> Result { - if fixture::is_binary_path(path) { + if FixtureKind::from_path(path).is_some() { let suite = fixture::read_blockchain(path)?; let mut hook = ReplayProgressHook::default(); let summary = execute_blockchain_tests_suite( @@ -174,7 +173,7 @@ fn replay_file(path: &Path, options: &ReplayOptions) -> Result { let input = fixture::read_text(path)?; match fixture::detect_str(path, &input)? { - Some(FixtureKind::StateTest) => { + Some(FixtureKind::State) => { let summary = execute_state_tests_str_with_filter( path, &input, @@ -190,7 +189,7 @@ fn replay_file(path: &Path, options: &ReplayOptions) -> Result { .map_err(|source| Error::StateTest { source })?; Ok(FileReplay { kind: "state", executed: summary.executed, skipped: summary.skipped }) } - Some(FixtureKind::BlockchainTest) => { + Some(FixtureKind::Blockchain) => { let mut hook = ReplayProgressHook::default(); let summary = execute_blockchain_tests_str( path, @@ -467,14 +466,15 @@ fn ggas(gas_used: u128) -> f64 { #[cfg(test)] mod collect_tests { - use super::collect_json_files; + use super::{NameFilter, PathBuf, collect_json_files, file_selected}; use std::{ fs, + path::Path, sync::atomic::{AtomicUsize, Ordering}, }; /// Creates a unique scratch directory under the system temp dir. - fn scratch_dir() -> std::path::PathBuf { + fn scratch_dir() -> PathBuf { static COUNTER: AtomicUsize = AtomicUsize::new(0); let unique = COUNTER.fetch_add(1, Ordering::Relaxed); let dir = std::env::temp_dir() @@ -510,9 +510,6 @@ mod collect_tests { #[test] fn file_filter_matches_path_or_file_name() { - use super::{NameFilter, file_selected}; - use std::path::Path; - let path = Path::new("test-fixtures/frontier/create/create_one_byte.json"); // An empty filter selects everything. diff --git a/crates/cli/src/replay_bench.rs b/crates/cli/src/replay_bench.rs index a02612452..9cefd6f1e 100644 --- a/crates/cli/src/replay_bench.rs +++ b/crates/cli/src/replay_bench.rs @@ -4,7 +4,8 @@ //! transaction outcomes; timed runs use a no-op hook and include database setup, //! transaction decoding, execution and block commits. -use alloy_eips::{eip7702::SignedAuthorization, eip7840::BlobParams}; +use crate::reference; +use alloy_eips::eip7702::SignedAuthorization; use alloy_primitives::{Address, B256, Bytes, Keccak256, Log, TxKind, U256}; use alloy_rpc_types_eth::{AccessList as RpcAccessList, AccessListItem as RpcAccessListItem}; use evm2::{ @@ -18,8 +19,8 @@ use evm2_eest::{ BlockchainTestExecuteConfig, NameFilter, blockchaintest::{ Block, BlockFinished, BlockHeader, BlockStarted, BlockchainTest, BlockchainTestCase, - CaseStarted, ForkSpec, Hook, Transaction, TransactionFinished, TransactionStarted, - Withdrawal, + CaseStarted, Hook, Transaction, TransactionFinished, TransactionStarted, Withdrawal, + blob_params, }, execute_blockchain_tests_suite, }; @@ -29,7 +30,6 @@ use revm::{ context_interface::{Cfg, block::BlobExcessGasAndPrice, either::Either}, database::{CacheDB, EmptyDB, InMemoryDB as RevmInMemoryDB}, handler::EvmTr, - primitives::hardfork::SpecId as RevmSpecId, state::{AccountInfo as RevmAccountInfo, Bytecode as RevmBytecode}, }; use std::path::Path; @@ -239,7 +239,10 @@ impl ReplayFixture { /// final block's hash. Pre-merge forks and block access lists are unsupported. pub fn from_case(name: String, case: BlockchainTestCase) -> Self { assert_canonical(&name, &case); - let spec = fork_to_spec_id(case.network); + let spec = case + .network + .to_spec_id() + .unwrap_or_else(|| panic!("replay fixture fork {:?} is not supported", case.network)); assert!(spec.enables(SpecId::MERGE), "replay corpus must be post-merge"); Self { name: name.clone(), spec, suite: BlockchainTest([(name, case)].into()) } } @@ -275,7 +278,7 @@ impl ReplayFixture { Path::new(&self.name), &self.suite, BlockchainTestExecuteConfig { - validate_post_state: false, + validate: false, compare_receipt_root: true, ..Default::default() }, @@ -313,7 +316,7 @@ impl ReplayFixture { pub fn execute_revm_on(&self, database: &mut RevmInMemoryDB, hook: &mut dyn Hook) { let case = self.case(); let spec = self.spec; - let revm_spec = revm_spec_id(spec); + let revm_spec = reference::spec_id(spec); let mut cfg = CfgEnv::new(); cfg.set_spec_and_mainnet_gas_params(revm_spec); @@ -326,7 +329,7 @@ impl ReplayFixture { for (block_index, block) in case.blocks.iter().enumerate() { let header = block_header(block).expect("replay fixture block must carry a header"); - let block_env = revm_block_env(header, parent_excess_blob_gas, spec); + let block_env = revm_block_env(&self.name, case, header, parent_excess_blob_gas); let block_number = header.number.saturating_to::(); let transactions = block_transactions(block); @@ -553,69 +556,6 @@ fn block_withdrawals(block: &Block) -> &[Withdrawal] { block.rlp_decoded.as_ref().map(|decoded| decoded.withdrawals.as_slice()).unwrap_or_default() } -fn blob_params_for_timestamp(timestamp: U256, spec: SpecId) -> BlobParams { - const MAINNET_BPO1_TIMESTAMP: u64 = 1_765_290_071; - const MAINNET_BPO2_TIMESTAMP: u64 = 1_767_747_671; - - if spec.enables(SpecId::AMSTERDAM) || timestamp.saturating_to::() >= MAINNET_BPO2_TIMESTAMP - { - BlobParams::bpo2() - } else if timestamp.saturating_to::() >= MAINNET_BPO1_TIMESTAMP { - BlobParams::bpo1() - } else if spec.enables(SpecId::OSAKA) { - BlobParams::osaka() - } else if spec.enables(SpecId::PRAGUE) { - BlobParams::prague() - } else { - BlobParams::cancun() - } -} - -fn fork_to_spec_id(fork: ForkSpec) -> SpecId { - match fork { - ForkSpec::Frontier => SpecId::FRONTIER, - ForkSpec::Homestead => SpecId::HOMESTEAD, - ForkSpec::EIP150 => SpecId::TANGERINE, - ForkSpec::EIP158 => SpecId::SPURIOUS_DRAGON, - ForkSpec::Byzantium => SpecId::BYZANTIUM, - ForkSpec::Constantinople | ForkSpec::ConstantinopleFix => SpecId::PETERSBURG, - ForkSpec::Istanbul => SpecId::ISTANBUL, - ForkSpec::Berlin => SpecId::BERLIN, - ForkSpec::London => SpecId::LONDON, - ForkSpec::Paris - | ForkSpec::MergeEOF - | ForkSpec::MergeMeterInitCode - | ForkSpec::MergePush0 => SpecId::MERGE, - ForkSpec::Shanghai => SpecId::SHANGHAI, - ForkSpec::Cancun => SpecId::CANCUN, - ForkSpec::Prague => SpecId::PRAGUE, - ForkSpec::Osaka => SpecId::OSAKA, - ForkSpec::Amsterdam => SpecId::AMSTERDAM, - other => panic!("replay fixture fork {other:?} is not supported"), - } -} - -fn revm_spec_id(spec: SpecId) -> RevmSpecId { - match spec { - SpecId::FRONTIER => RevmSpecId::FRONTIER, - SpecId::HOMESTEAD => RevmSpecId::HOMESTEAD, - SpecId::TANGERINE => RevmSpecId::TANGERINE, - SpecId::SPURIOUS_DRAGON => RevmSpecId::SPURIOUS_DRAGON, - SpecId::BYZANTIUM => RevmSpecId::BYZANTIUM, - SpecId::PETERSBURG => RevmSpecId::PETERSBURG, - SpecId::ISTANBUL => RevmSpecId::ISTANBUL, - SpecId::BERLIN => RevmSpecId::BERLIN, - SpecId::LONDON => RevmSpecId::LONDON, - SpecId::MERGE => RevmSpecId::MERGE, - SpecId::SHANGHAI => RevmSpecId::SHANGHAI, - SpecId::CANCUN => RevmSpecId::CANCUN, - SpecId::PRAGUE => RevmSpecId::PRAGUE, - SpecId::OSAKA => RevmSpecId::OSAKA, - SpecId::AMSTERDAM => RevmSpecId::AMSTERDAM, - other => panic!("unsupported replay spec: {other:?}"), - } -} - fn authorization_list(raw: &Transaction) -> Option> { let authorizations = raw.authorization_list.as_deref()?; Some( @@ -671,15 +611,21 @@ fn revm_seed_block_hashes(database: &mut RevmInMemoryDB, case: &BlockchainTestCa .insert(case.genesis_block_header.number, case.genesis_block_header.hash); } -fn revm_block_env(header: &BlockHeader, parent_excess_blob_gas: u64, spec: SpecId) -> RevmBlockEnv { +fn revm_block_env( + name: &str, + case: &BlockchainTestCase, + header: &BlockHeader, + parent_excess_blob_gas: u64, +) -> RevmBlockEnv { let excess_blob_gas = header .excess_blob_gas .map(|gas| gas.saturating_to::()) .unwrap_or(parent_excess_blob_gas); // The blob gas price is computed with the same `BlobParams` selection the // evm2 side uses, so both engines see an identical blob base fee. - let blob_gasprice = - blob_params_for_timestamp(header.timestamp, spec).calc_blob_fee(excess_blob_gas); + let blob_gasprice = blob_params(Path::new(name), name, case, header) + .expect("replay fixture must carry the blob schedule") + .map_or(1, |params| params.calc_blob_fee(excess_blob_gas)); RevmBlockEnv { number: header.number, beneficiary: header.coinbase, diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index af5c7a3e3..a614546e3 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -1,5 +1,5 @@ use alloy_consensus::{TxLegacy, transaction::Recovered}; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256, hex, keccak256}; +use alloy_primitives::{Address, B256, Bytes, TxKind, U256, hex, keccak256, map::B256Map}; use clap::ValueEnum; use evm2::{ BaseEvmTypes, Evm, InterpreterRunner, Precompiles, SpecId, Version, @@ -11,22 +11,21 @@ use evm2::{ }; use evm2_cli::evm_bench; use evm2_eest::{StateTestPost, StateTestSuite, StateTestUnit}; -use evm2_jit_context::EvmCompilerFn; +use evm2_jit_context::{EvmCompilerFn, can_run_compiled}; use evm2_jit_llvm::EvmLlvmBackend; use evm2_jit_runtime::{ - EvmCompiler, Linker, OptimizationLevel, eyre, parse_asm, shared_library_path, + EvmCompiler, Linker, OptimizationLevel, eyre, eyre::OptionExt, parse_asm, shared_library_path, }; use std::{ borrow::Cow, - collections::HashMap, hint::black_box, io::{Read, Write}, path::{Path, PathBuf}, sync::Arc, }; -const BENCH_CALLER: Address = Address::new([0x11; 20]); -const BENCH_TARGET: Address = Address::new([0xcc; 20]); +const BENCH_CALLER: Address = Address::repeat_byte(0x11); +const BENCH_TARGET: Address = Address::repeat_byte(0xcc); #[derive(Debug, clap::Args)] pub(crate) struct RunArgs { @@ -35,7 +34,7 @@ pub(crate) struct RunArgs { #[arg(default_value = "1")] n_iters: u64, - /// List available benchmark names and exit. + /// List available benchmarks and exit, as `\t` lines. #[arg(long)] list: bool, @@ -84,8 +83,9 @@ pub(crate) struct RunArgs { out_dir: Option, #[arg(short = 'O', long, default_value = "2")] opt_level: OptimizationLevel, - #[arg(long, value_enum, default_value = "osaka")] - spec_id: SpecIdValueEnum, + /// Spec to run: a fork name such as `osaka`, or `latest` for the newest one. + #[arg(long, value_parser = parse_spec_id, default_value = SpecId::DEFAULT.name())] + spec_id: SpecId, #[arg(long)] debug_assertions: bool, #[arg(long)] @@ -101,15 +101,18 @@ pub(crate) struct RunArgs { /// Disable frame pointers in the compiled function. #[arg(long)] no_frame_pointers: bool, - #[arg(long, default_value = "1000000000")] - gas_limit: u64, + /// Transaction gas limit. Defaults to the fixture's, or the Osaka cap for bytecode. + #[arg(long)] + gas_limit: Option, } impl RunArgs { pub(crate) fn run(self) -> eyre::Result<()> { if self.list { for bench in get_benches() { - println!("{}", bench.name); + if let BenchSource::Fixture(fixture_path) = bench.source { + println!("{}\t{fixture_path}", bench.name); + } } return Ok(()); } @@ -117,7 +120,7 @@ impl RunArgs { let Some(bench_name) = self.bench_name.clone() else { eyre::bail!("missing ; use `--list` to see available benchmarks"); }; - let spec_id = self.spec_id.into(); + let spec_id = self.spec_id; let calldata = self .calldata .as_deref() @@ -170,9 +173,9 @@ impl RunArgs { let default_aot_dir = || std::env::temp_dir().join("evm2-cli").join(&bench_name); let mut aot_dir = None; let mut pending_jit = None; + let entry_bytecode = bench_entry.entry_bytecode()?; { - let bytecode = bench_entry.entry_bytecode()?; let compile_spec_id = bench_entry.spec_id; let mut compiler = EvmCompiler::new_llvm(self.aot)?; @@ -200,7 +203,7 @@ impl RunArgs { compiler.set_frame_pointers(false); } - let parsed = compiler.parse(bytecode.as_slice().into(), compile_spec_id)?; + let parsed = compiler.parse(entry_bytecode.as_slice(), compile_spec_id)?; if self.display || self.parse_only { println!("{name}()\n{parsed:#}"); } @@ -243,8 +246,8 @@ impl RunArgs { } } else if self.load.is_none() { let func = unsafe { compiler.jit_function(f_id)? }; - let mut functions = HashMap::new(); - functions.insert(keccak256(&bytecode), func); + let mut functions = B256Map::default(); + functions.insert(keccak256(&entry_bytecode), func); compiler.clear_ir()?; pending_jit = Some((compiler, functions)); } @@ -260,7 +263,8 @@ impl RunArgs { let (prepared, _compiler, _lib); if let Some(ref load_path) = load_path { let lib; - (prepared, lib) = PreparedBench::load_from_library(&bench_entry, load_path, name)?; + (prepared, lib) = + PreparedBench::load_from_library(&bench_entry, load_path, name, &entry_bytecode)?; _compiler = None; _lib = Some(lib); } else if let Some((mut compiler, functions)) = pending_jit { @@ -278,7 +282,7 @@ impl RunArgs { match self.n_iters { 0 => {} - 1 if !self.interpret || self.jit_only => prepared.sanity_check()?, + 1 if !self.interpret || self.jit_only => prepared.sanity_check(!self.no_gas)?, _ => { if self.interpret || !self.jit_only { bench(self.n_iters, &format!("{name}/interpreter"), || { @@ -369,17 +373,20 @@ impl DotFormat { fn bench(n_iters: u64, name: &str, mut f: impl FnMut() -> eyre::Result) { let warmup = (n_iters / 10).max(10); - perf_control("enable"); for _ in 0..warmup { black_box(f().expect("warmup execution failed")); } + let counting = perf_control("enable"); let t = std::time::Instant::now(); for _ in 0..n_iters { black_box(f().expect("benchmark execution failed")); } let d = t.elapsed(); perf_control("disable"); + if counting { + perf_report(n_iters); + } eprintln!("{name}: {:>9?} ({d:>12?} / {n_iters})", d / n_iters as u32); } @@ -388,8 +395,10 @@ fn bench(n_iters: u64, name: &str, mut f: impl FnMut() -> eyre::Result) { /// /// The variable holds the file descriptors of the control pipe to write and of the pipe that /// acknowledges commands, separated by a comma. -fn perf_control(command: &str) { - let Ok(fds) = std::env::var("EVM2_PERF_CONTROL") else { return }; +/// +/// Returns whether a wrapper received the command. +fn perf_control(command: &str) -> bool { + let Ok(fds) = std::env::var("EVM2_PERF_CONTROL") else { return false }; let (ctl, ack) = fds.split_once(',').expect("invalid EVM2_PERF_CONTROL"); let mut ctl = std::fs::File::options() .write(true) @@ -400,63 +409,28 @@ fn perf_control(command: &str) { ctl.write_all(format!("{command}\n").as_bytes()).expect("failed to write to perf"); // `perf` answers `ack\n` and a NUL. ack.read_exact(&mut [0; 5]).expect("failed to read the perf acknowledgement"); + true } -#[derive(Clone, Copy, Debug, ValueEnum)] -#[clap(rename_all = "lowercase")] -#[allow(non_camel_case_types, clippy::upper_case_acronyms)] -enum SpecIdValueEnum { - FRONTIER, - FRONTIER_THAWING, - HOMESTEAD, - DAO_FORK, - TANGERINE, - SPURIOUS_DRAGON, - BYZANTIUM, - CONSTANTINOPLE, - PETERSBURG, - ISTANBUL, - MUIR_GLACIER, - BERLIN, - LONDON, - ARROW_GLACIER, - GRAY_GLACIER, - MERGE, - SHANGHAI, - CANCUN, - PRAGUE, - OSAKA, - AMSTERDAM, - LATEST, -} - -impl From for SpecId { - fn from(value: SpecIdValueEnum) -> Self { - match value { - SpecIdValueEnum::FRONTIER => Self::FRONTIER, - SpecIdValueEnum::FRONTIER_THAWING => Self::FRONTIER, - SpecIdValueEnum::HOMESTEAD => Self::HOMESTEAD, - SpecIdValueEnum::DAO_FORK => Self::HOMESTEAD, - SpecIdValueEnum::TANGERINE => Self::TANGERINE, - SpecIdValueEnum::SPURIOUS_DRAGON => Self::SPURIOUS_DRAGON, - SpecIdValueEnum::BYZANTIUM => Self::BYZANTIUM, - SpecIdValueEnum::CONSTANTINOPLE => Self::PETERSBURG, - SpecIdValueEnum::PETERSBURG => Self::PETERSBURG, - SpecIdValueEnum::ISTANBUL => Self::ISTANBUL, - SpecIdValueEnum::MUIR_GLACIER => Self::ISTANBUL, - SpecIdValueEnum::BERLIN => Self::BERLIN, - SpecIdValueEnum::LONDON => Self::LONDON, - SpecIdValueEnum::ARROW_GLACIER => Self::LONDON, - SpecIdValueEnum::GRAY_GLACIER => Self::LONDON, - SpecIdValueEnum::MERGE => Self::MERGE, - SpecIdValueEnum::SHANGHAI => Self::SHANGHAI, - SpecIdValueEnum::CANCUN => Self::CANCUN, - SpecIdValueEnum::PRAGUE => Self::PRAGUE, - SpecIdValueEnum::OSAKA => Self::OSAKA, - SpecIdValueEnum::AMSTERDAM => Self::AMSTERDAM, - SpecIdValueEnum::LATEST => Self::OSAKA, - } +/// Tells the `perf stat` wrapper's owner how many iterations its counters covered, by writing +/// the count to the pipe whose file descriptor `EVM2_PERF_REPORT` holds, if any. +/// +/// The pipe carries nothing else, so the report cannot be forged by text the CLI prints, such as a +/// benchmark name. +fn perf_report(n_iters: u64) { + let Ok(fd) = std::env::var("EVM2_PERF_REPORT") else { return }; + let mut report = std::fs::File::options() + .write(true) + .open(format!("/proc/self/fd/{fd}")) + .expect("failed to open the perf report pipe"); + writeln!(report, "{n_iters}").expect("failed to write the perf report"); +} + +fn parse_spec_id(name: &str) -> Result { + if name.eq_ignore_ascii_case("latest") { + return Ok(SpecId::NEXT); } + name.parse().map_err(|_| format!("unknown spec id {name:?}")) } #[derive(Clone, Debug)] @@ -465,7 +439,7 @@ struct Bench { source: BenchSource, spec_id: SpecId, calldata: Option>, - gas_limit: u64, + gas_limit: Option, assert_success: bool, } @@ -485,7 +459,7 @@ impl Bench { source: BenchSource::Fixture(bench.fixture_path), spec_id: spec, calldata: None, - gas_limit: u64::MAX, + gas_limit: None, assert_success: true, }) } @@ -515,7 +489,7 @@ fn fixture_from_bytecode( bytecode: Vec, spec_id: SpecId, calldata: Option>, - gas_limit: u64, + gas_limit: Option, ) -> Bench { Bench { name: name.into(), @@ -538,7 +512,7 @@ struct PreparedBench { block: BlockEnv, db: InMemoryDB, tx: RecoveredTxEnvelope, - functions: Arc>, + functions: Arc>, assert_success: bool, } @@ -547,21 +521,21 @@ type LlvmCompiler = EvmCompiler; impl PreparedBench { fn load(bench: &Bench) -> eyre::Result<(Self, LlvmCompiler)> { let mut compiler = EvmCompiler::new_llvm(false)?; - let prepared = Self::load_with_functions(bench, &mut compiler, HashMap::new())?; + let prepared = Self::load_with_functions(bench, &mut compiler, B256Map::default())?; Ok((prepared, compiler)) } fn load_with_functions( bench: &Bench, compiler: &mut LlvmCompiler, - mut functions: HashMap, + mut functions: B256Map, ) -> eyre::Result { - let (name, accounts, block, db, tx) = parse_bench(bench)?; + let PreparedBenchParts { name, accounts, block, db, tx } = parse_bench(bench)?; for account in &accounts { if account.bytecode.is_empty() || functions.contains_key(&account.code_hash) { continue; } - let name = format!("contract_{}", hex::encode(account.code_hash)); + let name = format!("contract_{:x}", account.code_hash); let func = unsafe { compiler.jit(&name, account.bytecode.original_byte_slice(), bench.spec_id)? }; @@ -584,20 +558,16 @@ impl PreparedBench { bench: &Bench, lib_path: &Path, symbol_name: &str, + entry_bytecode: &[u8], ) -> eyre::Result<(Self, libloading::Library)> { - let (name, accounts, block, db, tx) = parse_bench(bench)?; + let PreparedBenchParts { name, block, db, tx, .. } = parse_bench(bench)?; let lib = unsafe { libloading::Library::new(lib_path) }?; - let mut functions = HashMap::new(); - for account in &accounts { - if account.bytecode.is_empty() || functions.contains_key(&account.code_hash) { - continue; - } - let func = unsafe { - let sym: libloading::Symbol<'_, EvmCompilerFn> = lib.get(symbol_name.as_bytes())?; - *sym - }; - functions.insert(account.code_hash, func); - } + // The library holds only the entry contract; other contracts run on the interpreter. + let func = unsafe { + let sym: libloading::Symbol<'_, EvmCompilerFn> = lib.get(symbol_name.as_bytes())?; + *sym + }; + let functions = B256Map::from_iter([(keccak256(entry_bytecode), func)]); Ok(( Self { @@ -624,12 +594,12 @@ impl PreparedBench { evm.transact(&self.tx).map(evm2::ExecutedTx::commit).map_err(Into::into) } - fn sanity_check(&self) -> eyre::Result<()> { + fn sanity_check(&self, compare_gas: bool) -> eyre::Result<()> { let interpreter = self.run_interpreter()?; let jit = self.run_jit()?; if self.assert_success { eyre::ensure!( - interpreter.status, + interpreter.is_success(), "{} benchmark transaction failed:\n interpreter: {:?}\n JIT: {:?}", self.name, interpreter, @@ -637,7 +607,10 @@ impl PreparedBench { ); } eyre::ensure!( - interpreter.status == jit.status, + interpreter.is_success() == jit.is_success() + && (!compare_gas + || (interpreter.output == jit.output + && interpreter.total_gas_spent == jit.total_gas_spent)), "interpreter and JIT results differ:\n interpreter: {:?}\n JIT: {:?}", interpreter, jit @@ -658,7 +631,7 @@ impl PreparedBench { #[derive(Clone, Debug)] struct FixedJitRunner { - functions: Arc>, + functions: Arc>, } impl InterpreterRunner for FixedJitRunner { @@ -667,8 +640,7 @@ impl InterpreterRunner for FixedJitRunner { interpreter: &mut Interpreter<'frame, 'host, BaseEvmTypes>, host: &mut Evm<'host, BaseEvmTypes>, ) -> Option { - // Compiled code treats the remaining gas as signed, so it cannot hold more than `i64::MAX`. - if interpreter.gas().remaining() > i64::MAX as u64 { + if !can_run_compiled(interpreter) { return None; } let func = *self.functions.get(&interpreter.original_bytecode_hash())?; @@ -683,8 +655,13 @@ struct ParsedAccount { code_hash: B256, } -type PreparedBenchParts = - (Cow<'static, str>, Vec, BlockEnv, InMemoryDB, RecoveredTxEnvelope); +struct PreparedBenchParts { + name: Cow<'static, str>, + accounts: Vec, + block: BlockEnv, + db: InMemoryDB, + tx: RecoveredTxEnvelope, +} fn parse_bench(bench: &Bench) -> eyre::Result { match &bench.source { @@ -699,9 +676,9 @@ fn parse_fixture_bench(bench: &Bench, fixture_path: &str) -> eyre::Result eyre::Result eyre::Result { @@ -726,7 +703,8 @@ fn parse_bytecode_bench(bench: &Bench, bytecode: &[u8]) -> eyre::Result eyre::Result eyre::Result { @@ -766,7 +744,7 @@ fn select_case<'a>( let post = unit .post .iter() - .filter(|(spec_name, _)| spec_name.to_spec_id() == Some(spec_id)) + .filter(|(spec_name, _)| spec_name.to_state_spec_id() == Some(spec_id)) .flat_map(|(_, posts)| posts) .next() .ok_or_else(|| { @@ -783,7 +761,7 @@ fn parse_block(env: &evm2_eest::StateTestEnv) -> BlockEnv { gas_limit: env.current_gas_limit, basefee: env.current_base_fee.unwrap_or_default(), difficulty: env.current_difficulty, - prevrandao: env.current_random.map_or(U256::ZERO, |value| U256::from_be_bytes(value.0)), + prevrandao: env.current_random.map_or(U256::ZERO, |value| value.into()), slot_num: env.slot_number.unwrap_or_default(), ..BlockEnvExt::default() } @@ -808,13 +786,15 @@ fn build_fixture_tx( .gas_limit .get(indexes.gas) .ok_or_else(|| eyre::eyre!("fixture gas index {} does not exist", indexes.gas))?; - let gas_limit = u64_value(gas_limit)?.min(bench.gas_limit()); + let gas_limit = match bench.gas_limit { + Some(gas_limit) => gas_limit, + None => u64_value(gas_limit)?, + }; let value = *raw .value .get(indexes.value) .ok_or_else(|| eyre::eyre!("fixture value index {} does not exist", indexes.value))?; - let caller = - raw.sender.ok_or_else(|| eyre::eyre!("benchmark transaction sender is required"))?; + let caller = raw.sender.ok_or_eyre("benchmark transaction sender is required")?; let tx = TxLegacy { chain_id: None, nonce: u64_value(raw.nonce)?, @@ -827,12 +807,6 @@ fn build_fixture_tx( Ok(Recovered::new_unchecked(TxEnvelope::Legacy(tx), caller)) } -impl Bench { - fn gas_limit(&self) -> u64 { - self.gas_limit.min(Version::base(self.spec_id).tx_gas_limit_cap) - } -} - fn u128_value(value: U256) -> eyre::Result { value.try_into().map_err(|_| eyre::eyre!("fixture value overflows u128")) } @@ -889,7 +863,7 @@ fn read_code_string(contents: &[u8], ext: Option<&str>) -> eyre::Result> #[cfg(test)] mod tests { - use super::{SpecId, SpecIdValueEnum, read_code_string}; + use super::{SpecId, parse_spec_id, read_code_string}; use crate::args::{Args, Command}; use clap::Parser; @@ -899,8 +873,10 @@ mod tests { } #[test] - fn latest_spec_matches_default_run_spec() { - assert_eq!(SpecId::from(SpecIdValueEnum::LATEST), SpecId::OSAKA); + fn spec_id_parser_accepts_names_and_latest() { + assert_eq!(parse_spec_id("osaka"), Ok(SpecId::OSAKA)); + assert_eq!(parse_spec_id("LATEST"), Ok(SpecId::NEXT)); + assert!(parse_spec_id("constantinople").is_err()); } #[test] diff --git a/crates/eest/Cargo.toml b/crates/eest/Cargo.toml index 8b071d054..2dfe4f656 100644 --- a/crates/eest/Cargo.toml +++ b/crates/eest/Cargo.toml @@ -14,12 +14,12 @@ repository.workspace = true workspace = true [dependencies] -evm2.workspace = true +evm2 = { workspace = true, features = ["block"] } evm2-jit-runtime = { workspace = true, features = ["llvm"], optional = true } -alloy-consensus = { workspace = true, features = ["std"] } +alloy-consensus = { workspace = true, features = ["k256", "std"] } alloy-eip7928 = { workspace = true, features = ["std", "serde", "rlp"] } -alloy-eips = { workspace = true, features = ["std"] } +alloy-eips = { workspace = true, features = ["sha2", "std"] } alloy-primitives = { workspace = true, features = ["std", "serde", "rlp"] } alloy-rlp = { workspace = true, features = ["std"] } alloy-rpc-types-eth = { workspace = true, features = ["std"] } diff --git a/crates/eest/README.md b/crates/eest/README.md index 173e41a94..a2ce33944 100644 --- a/crates/eest/README.md +++ b/crates/eest/README.md @@ -9,7 +9,7 @@ test-fixtures/ ├── main/ │ ├── stable/{state_tests,blockchain_tests}/... │ └── develop/{state_tests,blockchain_tests}/... -├── devnet/ +├── eest/ │ └── {state_tests,blockchain_tests}/... └── legacytests/ ├── Cancun/GeneralStateTests/... @@ -22,16 +22,17 @@ Populate it with: ./scripts/setup_test_fixtures.py ``` -By default this downloads the glamsterdam devnet fixtures plus the legacy -Cancun and Constantinople state tests. Set `EVM2_STATETEST_MAIN=1` to also -download the EEST `main` develop suite (stable with `EVM2_STATETEST_STABLE=1`), -or `EVM2_STATETEST_DEVNET_ONLY=1` to skip the legacy fixtures. +By default this downloads the mainline execution-specs release fixtures +(`tests@v21.0.1`, Frontier through Amsterdam) plus the legacy Cancun and +Constantinople state tests. Set `EVM2_STATETEST_MAIN=1` to also +download the EEST `main` develop suite (stable with `EVM2_EEST_STABLE=1`), +or `EVM2_STATETEST_EEST_ONLY=1` to skip the legacy fixtures. The legacy fixtures come from the `ethereum/legacytests` commit that the `ethereum/tests` v17.2 `LegacyTests` submodule pins; override it with -`LEGACY_VERSION`. The runner fails when a default devnet or legacy root is +`LEGACY_VERSION`. The runner fails when a default execution-specs or legacy root is missing, unless the other suite got an explicit root. The `main` roots are -optional. If you clear `DEVNET_VERSION`, set `EVM2_STATETEST_ROOT` and +optional. If you clear `EEST_VERSION`, set `EVM2_STATETEST_ROOT` and `EVM2_BLOCKCHAINTEST_ROOT`. Run all discovered EEST fixtures with the single `eest` test binary: @@ -51,7 +52,7 @@ state and blockchain fixture: cargo st ``` -This is equivalent to: +This runs the whole workspace with all features; to run only the EEST harness: ```sh cargo nextest run -p evm2-eest --all-features --test eest --ignore-default-filter @@ -81,6 +82,9 @@ cargo nextest run -p evm2-eest --features jit --test eest \ --ignore-default-filter blockchain_tests::aot ``` +Compiled files run on a worker pool inside each test, so nextest's thread count +does not limit it. Set `EVM2_EEST_SINGLE_THREAD=1` to use one worker thread. + ## State tests Run all discovered state tests with nextest: @@ -89,13 +93,13 @@ Run all discovered state tests with nextest: cargo nextest run -p evm2-eest --test eest --ignore-default-filter statetests ``` -This runs every downloaded state-test root: `devnet/state_tests`, the legacy +This runs every downloaded state-test root: `eest/state_tests`, the legacy Cancun and Constantinople state tests, and `main/develop/state_tests` when the `main` suite has been downloaded. EEST develop includes the stable fixtures, so stable is not run separately. -Set `EVM2_STATETEST_STABLE=1` to download and run EEST stable fixtures instead -of develop. +Set `EVM2_EEST_STABLE=1` to download and run EEST stable fixtures instead of +develop. Both runners and the setup script read it. ## Blockchain tests @@ -105,13 +109,11 @@ Run all discovered blockchain tests with nextest: cargo nextest run -p evm2-eest --test eest --ignore-default-filter blockchain_tests ``` -This runs every downloaded blockchain-test root: `devnet/blockchain_tests`, +This runs every downloaded blockchain-test root: `eest/blockchain_tests`, plus `main/develop/blockchain_tests` when the `main` suite has been downloaded. -Transition forks are skipped. Blocks with a `blockAccessList` build the block -access list and check it against the fixture, including the header hash. - -Set `EVM2_BLOCKCHAINTEST_STABLE=1` or `EVM2_EEST_STABLE=1` to use EEST stable -instead of develop for main blockchain fixtures. +Supported transition forks switch at block 5 or timestamp 15000. Blocks with a +`blockAccessList` build the block access list and check it against the fixture, +including the header hash. ## Filtering @@ -150,8 +152,8 @@ cargo nextest list -p evm2-eest --test eest --ignore-default-filter ``` For local experiments, `EVM2_STATETEST_ROOT`, `EVM2_BLOCKCHAINTEST_ROOT`, -`EVM2_STATETEST_STABLE`, `EVM2_BLOCKCHAINTEST_STABLE`, `EVM2_EEST_STABLE`, -`EVM2_TEST_FIXTURES`, and `SUBDIR` are supported as optional filters. +`EVM2_EEST_STABLE`, `EVM2_TEST_FIXTURES`, and `SUBDIR` are supported as optional +filters. No hardforks are skipped by default. Set `EVM2_SKIP_FORKS` to a comma-separated list of hardfork names (for example diff --git a/crates/eest/src/additional.rs b/crates/eest/src/additional.rs index 30f2d069a..320415129 100644 --- a/crates/eest/src/additional.rs +++ b/crates/eest/src/additional.rs @@ -9,6 +9,7 @@ use crate::{ execute::{self, ExecuteConfig as StateConfig}, filter::NameFilter, fixture_io, + fixture_kind::FixtureKind, harness::{TestRoot, TestSuite, descend_all}, }; use libtest_mimic::Failed; @@ -39,11 +40,15 @@ fn should_ignore(name: &str) -> bool { fn run_file(path: PathBuf) -> Result<(), Failed> { let input = fixture_io::read_to_string(&path).map_err(|err| format!("{}: {err}", path.display()))?; - match detect_kind(&input) { + match FixtureKind::from_json(&input).map_err(|err| format!("{}: {err}", path.display()))? { Some(FixtureKind::Blockchain) => execute_str( &path, &input, - BlockchainConfig { compare_state_root: true, ..Default::default() }, + BlockchainConfig { + compare_state_root: true, + compare_receipt_root: true, + ..Default::default() + }, &NameFilter::default(), &mut NoopHook, ) @@ -57,25 +62,3 @@ fn run_file(path: PathBuf) -> Result<(), Failed> { None => Err(format!("could not detect fixture kind in {}", path.display()).into()), } } - -enum FixtureKind { - State, - Blockchain, -} - -/// Detects a fixture's kind from the fields of its first case. -fn detect_kind(input: &str) -> Option { - let value: serde_json::Value = serde_json::from_str(input).ok()?; - let first = value.as_object()?.values().find_map(serde_json::Value::as_object)?; - if has_any(first, &["blocks", "genesisBlockHeader", "lastblockhash", "network"]) { - Some(FixtureKind::Blockchain) - } else if has_any(first, &["env", "post", "transaction"]) { - Some(FixtureKind::State) - } else { - None - } -} - -fn has_any(object: &serde_json::Map, fields: &[&str]) -> bool { - fields.iter().any(|field| object.contains_key(*field)) -} diff --git a/crates/eest/src/binary.rs b/crates/eest/src/binary.rs index 070ba40c2..134b634c3 100644 --- a/crates/eest/src/binary.rs +++ b/crates/eest/src/binary.rs @@ -1,7 +1,7 @@ use crate::{ blockchaintest::{ - Account, Block, BlockHash, BlockHeader, BlockchainTest, BlockchainTestCase, DecodedBlock, - ForkSpec, SealEngine, State, Transaction, Withdrawal, + Account, Block, BlockHash, BlockHeader, BlockchainTest, BlockchainTestCase, ChainConfig, + DecodedBlock, ForkSpec, SealEngine, State, Transaction, Withdrawal, }, tx::{AccessListItem, TestAuthorization}, }; @@ -38,6 +38,7 @@ struct BlockchainTestCaseRef<'a> { lastblockhash: &'a B256, network: &'a ForkSpec, seal_engine: &'a SealEngine, + config: &'a Option, } impl<'a> BlockchainTestCaseRef<'a> { @@ -52,6 +53,7 @@ impl<'a> BlockchainTestCaseRef<'a> { lastblockhash: &case.lastblockhash, network: &case.network, seal_engine: &case.seal_engine, + config: &case.config, } } } @@ -238,6 +240,7 @@ struct BlockchainTestCaseWire { lastblockhash: B256, network: ForkSpec, seal_engine: SealEngine, + config: Option, } impl From for BlockchainTestCase { @@ -252,6 +255,7 @@ impl From for BlockchainTestCase { lastblockhash: value.lastblockhash, network: value.network, seal_engine: value.seal_engine, + config: value.config, } } } diff --git a/crates/eest/src/blockchaintest/env.rs b/crates/eest/src/blockchaintest/env.rs index e92c87a79..b66994e38 100644 --- a/crates/eest/src/blockchaintest/env.rs +++ b/crates/eest/src/blockchaintest/env.rs @@ -1,19 +1,15 @@ use crate::{ env::explicit_state_test_root_from_env, fixtures::{ - EEST_STABLE_ENV, apply_subdir, apply_subdir_to_roots, env_flag, fixtures_root, - workspace_relative, + EEST_STABLE_ENV, apply_subdir_to_roots, env_flag, explicit_root_from_env, fixtures_root, }, harness::TestRoot, }; -use std::{env, path::PathBuf}; +use std::path::PathBuf; /// Environment variable for the blockchain test root. pub(crate) const BLOCKCHAIN_TEST_ROOT_ENV: &str = "EVM2_BLOCKCHAINTEST_ROOT"; -/// Environment variable for selecting stable EEST fixtures instead of develop. -pub(crate) const BLOCKCHAINTEST_STABLE_ENV: &str = "EVM2_BLOCKCHAINTEST_STABLE"; - /// Optional environment variable for selecting a subdirectory under the test root. pub(crate) const BLOCKCHAIN_TEST_SUBDIR_ENV: &str = "SUBDIR"; @@ -22,12 +18,7 @@ pub(crate) type BlockchainTestRoot = TestRoot; /// Returns the explicit blockchain-test root configured through environment variables. pub(crate) fn explicit_blockchain_test_root_from_env() -> Option { - env::var_os(BLOCKCHAIN_TEST_ROOT_ENV).map(PathBuf::from).map(workspace_relative).map( - |mut path| { - apply_subdir(&mut path, BLOCKCHAIN_TEST_SUBDIR_ENV); - path - }, - ) + explicit_root_from_env(BLOCKCHAIN_TEST_ROOT_ENV, BLOCKCHAIN_TEST_SUBDIR_ENV) } /// Returns the blockchain-test roots to run by default. @@ -53,16 +44,16 @@ pub(crate) fn blockchain_test_roots() -> Vec { /// Returns the default repo-relative blockchain-test roots. The `main` root is optional. pub(crate) fn default_blockchain_test_roots() -> Vec { let fixtures = fixtures_root(); - let main_path = if env_flag(BLOCKCHAINTEST_STABLE_ENV) || env_flag(EEST_STABLE_ENV) { + let main_path = if env_flag(EEST_STABLE_ENV) { fixtures.join("main/stable/blockchain_tests") } else { fixtures.join("main/develop/blockchain_tests") }; let mut roots = vec![BlockchainTestRoot { - name: "blockchain_tests::devnet".to_string(), - label: "execution-spec-tests devnet", - path: fixtures.join("devnet/blockchain_tests"), + name: "blockchain_tests::eest".to_string(), + label: "execution-specs release", + path: fixtures.join("eest/blockchain_tests"), }]; if main_path.is_dir() { diff --git a/crates/eest/src/blockchaintest/error.rs b/crates/eest/src/blockchaintest/error.rs index c1ccee263..c1a2ed354 100644 --- a/crates/eest/src/blockchaintest/error.rs +++ b/crates/eest/src/blockchaintest/error.rs @@ -1,3 +1,4 @@ +use super::types::{BlobFork, ForkSpec}; use crate::{execution::ExecutionResourceError, tx::TxBuildError}; use evm2::registry::HandlerError; use std::{io, path::PathBuf}; @@ -18,7 +19,7 @@ pub struct TestError { impl TestError { /// Creates an error for an unknown test name. pub(crate) fn unknown(path: impl Into, kind: TestErrorKind) -> Self { - Self { path: path.into().display().to_string(), name: "Unknown".to_string(), kind } + Self::case(path, "Unknown", kind) } /// Creates an error for a named test case. @@ -46,9 +47,29 @@ pub(crate) enum TestErrorKind { /// Numeric value overflowed the target type. #[error("value overflows {0}")] Overflow(&'static str), - /// Sender is required in blockchain tests. + /// The runner does not support the test's fork. + #[error("unsupported fork: {0:?}")] + UnsupportedFork(ForkSpec), + /// The test's chain configuration has no blob parameters for the active fork. + #[error("blob schedule has no entry for {0:?}")] + MissingBlobSchedule(BlobFork), + /// The test's blob schedule has a zero base fee update fraction for the active fork. + #[error("blob schedule entry for {0:?} has a zero base fee update fraction")] + ZeroBlobUpdateFraction(BlobFork), + /// Sender is required when signatures are not validated. #[error("transaction sender is required")] MissingSender, + /// Signed transaction has an invalid signature. + #[error("{0}")] + InvalidSignedTransaction(String), + /// Sender recovered from the signature differs from the fixture's sender. + #[error("sender mismatch: recovered {recovered}, fixture {expected}")] + SenderMismatch { + /// Sender recovered from the signature. + recovered: alloy_primitives::Address, + /// Sender declared by the fixture. + expected: alloy_primitives::Address, + }, /// An ommer is not old enough, or is too old, to receive a block reward. #[error("invalid ommer age: block {block_number}, ommer {ommer_number}")] InvalidOmmerAge { @@ -69,9 +90,6 @@ pub(crate) enum TestErrorKind { /// EVM execution unexpectedly succeeded. #[error("execution succeeded, but expected exception: {0}")] UnexpectedSuccess(String), - /// A system call failed. - #[error("system call failed: {0}")] - SystemCall(&'static str), /// Block gas used in the header does not match the executed transactions. #[error("block gas used mismatch: header {expected}, computed {actual}")] BlockGasUsedMismatch { @@ -131,6 +149,10 @@ impl From for TestErrorKind { TxBuildError::Overflow(name) => Self::Overflow(name), TxBuildError::BuildTransaction(error) => Self::BuildTransaction(error), TxBuildError::SerdeDeserialize(error) => Self::SerdeDeserialize(error), + error @ (TxBuildError::Decode(_) + | TxBuildError::InvalidV(_) + | TxBuildError::ProtectedLegacy + | TxBuildError::Recover(_)) => Self::InvalidSignedTransaction(error.to_string()), } } } diff --git a/crates/eest/src/blockchaintest/execute.rs b/crates/eest/src/blockchaintest/execute.rs index 38e5abf8e..fdb00c282 100644 --- a/crates/eest/src/blockchaintest/execute.rs +++ b/crates/eest/src/blockchaintest/execute.rs @@ -5,55 +5,79 @@ use super::{ TransactionFinished, TransactionStarted, }, types::{ - Account, Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, Transaction, - Withdrawal, + Account, BlobFork, BlobScheduleEntry, Block, BlockHeader, BlockchainTest, + BlockchainTestCase, ForkSpec, Transaction, Withdrawal, }, }; #[cfg(feature = "jit")] use crate::compiled::{self, FileSummary}; use crate::{ dump, - execute::state_root_from_database, + execute::{print_db_stats, state_root_from_database}, execution::ExecutionResources, filter::NameFilter, fixture_io, forks::is_fork_skipped, state::{insert_account_with_storage, parse_bytecode}, trace::{self, Eip3155Tracer}, - tx::{TxFields, build_recovered_tx, rpc_access_list, signed_authorizations}, + tx::{ + TxFields, TxSigner, build_signed_recovered_tx, rpc_access_list, signature_from_vrs, + signed_authorizations, + }, +}; +use alloy_consensus::{ + Eip658Value, EthereumTxEnvelope, Header, Receipt, ReceiptEnvelope, ReceiptWithBloom, TxEip4844, + TxType, }; -use alloy_consensus::{Eip658Value, Receipt, ReceiptEnvelope, Transaction as _, TxType}; use alloy_eip7928::{BlockAccessList, validate_block_access_list}; -use alloy_eips::{eip2718::Typed2718, eip7840::BlobParams}; -use alloy_primitives::{Address, B256, Bytes, KECCAK256_EMPTY, U256}; +use alloy_eips::{ + eip4844::DATA_GAS_PER_BLOB, eip4895::Withdrawal as AlloyWithdrawal, + eip7002::WITHDRAWAL_REQUEST_PREDEPLOY_CODE, eip7251::CONSOLIDATION_REQUEST_PREDEPLOY_CODE, + eip7840::BlobParams, +}; +use alloy_primitives::{Address, B256, Bloom, Bytes, Signature, U256, aliases::U512}; use alloy_rpc_types_eth::AccessList as RpcAccessList; -use anstyle::{AnsiColor, Color, Style}; use evm2::{ - BaseEvmTypes, Evm, Precompiles, SpecId, TxResult, + BaseEvmTypes, Evm, EvmTypes, Precompiles, SpecId, TxResult, + block::{ + BlockExecutor as _, BundleSource, ReceiptBuilder, ReceiptBuilderCtx, + bundle_state::BundleRetention, + ethereum::{EthBlockExecutionCtx, EthBlockExecutor, EthBlockRules}, + }, env::{BlockEnv, BlockEnvExt}, ethereum::{RecoveredTxEnvelope, ethereum_tx_registry}, evm::{ - AccountChangeRef, AccountInfo as EvmAccountInfo, BEACON_ROOTS_ADDRESS, Bal, - BlockStateAccumulator, DbStats, DbStatsCounts, HISTORY_STORAGE_ADDRESS, InMemoryDB, - StateChangeSink, StateChangeSource, SystemTx, Tee, WITHDRAWAL_REQUEST_ADDRESS, + AccountInfo as EvmAccountInfo, BUILDER_DEPOSIT_REQUEST_ADDRESS, + BUILDER_EXIT_REQUEST_ADDRESS, Bal, CONSOLIDATION_REQUEST_ADDRESS, DbStats, DbStatsCounts, + InMemoryDB, WITHDRAWAL_REQUEST_ADDRESS, }, - registry::HandlerError, }; #[cfg(feature = "jit")] use std::path::PathBuf; -use std::{mem, path::Path}; +use std::{ + borrow::Cow, + collections::{BTreeMap, BTreeSet}, + fmt::Write, + mem, + path::Path, +}; pub use crate::execution::ExecutionMode; -const ONE_GWEI: u64 = 1_000_000_000; const ONE_ETHER: u128 = 1_000_000_000_000_000_000; +/// EIP-7934 maximum RLP-encoded block size: `MAX_BLOCK_SIZE` (10 MiB) minus `SAFETY_MARGIN` +/// (2 MiB). +const MAX_RLP_BLOCK_SIZE: usize = 10 * 1024 * 1024 - 2 * 1024 * 1024; + /// Execution options for a single suite. #[derive(Clone, Copy, Debug)] pub struct ExecuteConfig { - /// Whether to validate the full final-state allocation when fixtures contain `postState`. - /// Unexpected live accounts and non-zero storage are rejected. - pub validate_post_state: bool, + /// Whether to run the harness checks that are not part of execution: comparing the final + /// state with the fixture's `postState` (unexpected live accounts and non-zero storage are + /// rejected) and recovering each sender from its signature. When false, the runner trusts + /// the fixture's `sender`. + pub validate: bool, /// Whether to recompute each committed block's state root and compare it to /// the block header's `stateRoot`. pub compare_state_root: bool, @@ -80,7 +104,7 @@ pub struct ExecuteConfig { impl Default for ExecuteConfig { fn default() -> Self { Self { - validate_post_state: true, + validate: true, compare_state_root: false, compare_receipt_root: false, trace: false, @@ -177,12 +201,33 @@ pub fn pre_state( name: &str, test_case: &BlockchainTestCase, ) -> Result { - let mut database = - parse_state(&test_case.pre.0).map_err(|err| TestError::case(path, name, err))?; + let mut pre = Cow::Borrowed(&test_case.pre.0); + if is_captured(test_case) + && fork_schedule(test_case.network).is_some_and(|fork| fork.runs_prague()) + { + // A capture holds only the accounts its transactions touch, but the blocks' system calls + // read the request contracts, which must have code. + for (address, code) in [ + (WITHDRAWAL_REQUEST_ADDRESS, WITHDRAWAL_REQUEST_PREDEPLOY_CODE.clone()), + (CONSOLIDATION_REQUEST_ADDRESS, CONSOLIDATION_REQUEST_PREDEPLOY_CODE.clone()), + ] { + pre.to_mut().entry(address).or_insert_with(|| Account { + code, + nonce: U256::ONE, + ..Account::default() + }); + } + } + let mut database = parse_state(&pre).map_err(|err| TestError::case(path, name, err))?; seed_block_hashes(&mut database, test_case); Ok(database) } +/// Returns whether the case comes from `evm2 capture`, which zeroes the genesis state root. +fn is_captured(test_case: &BlockchainTestCase) -> bool { + test_case.genesis_block_header.state_root.is_zero() +} + /// Executes a blockchain test case on `database`, which must hold the case's [`pre_state`]. /// /// Returns the final state root when `config` needs it. @@ -210,8 +255,7 @@ fn execute_suite_with_resources( let mut summary = ExecuteSummary::default(); for (name, test_case) in &suite.0 { if !test_filter.matches(name) - || test_case.network.is_transition() - || is_blockchain_fork_skipped(fork_to_spec_id(test_case.network)) + || fork_schedule(test_case.network).is_some_and(|schedule| schedule.is_skipped()) { summary.skipped += 1; continue; @@ -261,18 +305,18 @@ fn execute_case( hook: &mut dyn Hook, resources: &ExecutionResources, ) -> Result, TestError> { - let spec = fork_to_spec_id(test_case.network); + let schedule = case_schedule(path, name, test_case)?; + let spec = schedule.initial.spec; let mut parent_block_hash = Some(test_case.genesis_block_header.hash); - let mut parent_excess_blob_gas = - test_case.genesis_block_header.excess_blob_gas.unwrap_or_default().saturating_to::(); - let mut block_env = - block_env_from_header(&test_case.genesis_block_header, parent_excess_blob_gas, spec); + let mut parent_header = &test_case.genesis_block_header; + let mut block_env = block_env_from_header(parent_header, &schedule) + .map_err(|err| TestError::case(path, name, err))?; let total_blocks = test_case.blocks.len(); let mut db_stats_counts = DbStatsCounts::default(); // `evm2 capture` keeps only the accounts the blocks touch and zeroes the genesis state // root, so its blocks' state roots can't be reproduced. Receipt roots still check them. - let compare_state_root = - config.compare_state_root && !test_case.genesis_block_header.state_root.is_zero(); + let captured = is_captured(test_case); + let compare_state_root = config.compare_state_root && !captured; hook.case_started(CaseStarted { name, total_blocks, network: test_case.network }); @@ -280,6 +324,8 @@ fn execute_case( let block_number = block_number(block); let block_gas_used = block_gas_used(block); let total_transactions = block_transactions(block).len(); + let spec = block_header(block) + .map_or(spec, |header| schedule.spec_at(header.number, header.timestamp)); hook.block_started(BlockStarted { block_index, total_blocks, @@ -295,14 +341,17 @@ fn execute_case( block_number, total_transactions, block, + &schedule, spec, database, &mut block_env, &mut parent_block_hash, - &mut parent_excess_blob_gas, + &mut parent_header, + captured, hook, resources, config.trace, + config.validate, compare_state_root, config.compare_receipt_root, if config.db_stats { Some(&mut db_stats_counts) } else { None }, @@ -340,7 +389,7 @@ fn execute_case( )); } - if config.validate_post_state + if config.validate && let Some(expected) = &test_case.post_state { validate_post_state(database, expected).map_err(|err| TestError::case(path, name, err))?; @@ -369,27 +418,30 @@ fn seed_block_hashes(database: &mut InMemoryDB, test_case: &BlockchainTestCase) #[derive(Clone, Copy, Debug, Eq, PartialEq)] enum BlockResolution { - Commit, + Commit(B256), Discard, } #[expect(clippy::too_many_arguments)] -fn execute_block( +fn execute_block<'a>( path: &Path, name: &str, block_index: usize, total_blocks: usize, block_number: Option, total_transactions: usize, - block: &Block, + block: &'a Block, + schedule: &ForkSchedule, spec: SpecId, database: &mut InMemoryDB, block_env: &mut BlockEnv, parent_block_hash: &mut Option, - parent_excess_blob_gas: &mut u64, + parent_header: &mut &'a BlockHeader, + captured: bool, hook: &mut dyn Hook, resources: &ExecutionResources, trace: bool, + validate: bool, compare_state_root: bool, compare_receipt_root: bool, db_stats_counts: Option<&mut DbStatsCounts>, @@ -412,18 +464,54 @@ fn execute_block( let mut block_hash = None; let mut beacon_root = None; - let mut this_excess_blob_gas = None; let mut expected_state_root = None; + let mut blob_params = None; let mut next_block_env = *block_env; if let Some(header) = block_header(block) { block_hash = Some(header.hash); beacon_root = header.parent_beacon_block_root; - next_block_env = block_env_from_header(header, *parent_excess_blob_gas, spec); - this_excess_blob_gas = header.excess_blob_gas.map(|gas| gas.saturating_to::()); + next_block_env = block_env_from_header(header, schedule) + .map_err(|err| TestError::case(path, name, err))?; + blob_params = schedule + .blob_params(header.number, header.timestamp) + .map_err(|err| TestError::case(path, name, err))?; expected_state_root = Some(header.state_root); } + // The blob checks multiply the blob counts by the blob gas, which must not overflow. + if let Some(params) = &blob_params + && (params.max_blob_count == 0 + || params.max_blob_count < params.target_blob_count + || params.max_blob_count.checked_mul(DATA_GAS_PER_BLOB).is_none()) + { + return Err(TestError::case( + path, + name, + TestErrorKind::UnexpectedFailure(format!( + "invalid blob schedule: max {} and target {} must satisfy target <= max, max > 0, and \ + fit in blob gas", + params.max_blob_count, params.target_blob_count + )), + )); + } + let ommers = block_ommers(block) + .iter() + .map(|ommer| Header { + beneficiary: ommer.coinbase, + number: ommer.number.saturating_to(), + ..Header::default() + }) + .collect::>(); + let withdrawals = block_withdrawals(block) + .iter() + .map(|withdrawal| AlloyWithdrawal { + index: withdrawal.index.saturating_to(), + validator_index: withdrawal.validator_index.saturating_to(), + address: withdrawal.address, + amount: withdrawal.amount.saturating_to(), + }) + .collect::>(); let initial_database = mem::take(database); let receipt_base = (compare_receipt_root && !spec.enables(SpecId::BYZANTIUM)) .then(|| initial_database.clone()); @@ -448,17 +536,33 @@ fn execute_block( if trace { evm.set_inspector(Eip3155Tracer::to_stdout()); } - let mut block_state = BlockStateAccumulator::new(); + + let ctx = EthBlockExecutionCtx { + tx_count_hint: Some(block_transactions(block).len()), + parent_hash: parent_block_hash.unwrap_or_default(), + parent_beacon_block_root: beacon_root, + ommers: Cow::Owned(ommers), + withdrawals: Some(Cow::Owned(withdrawals)), + }; + let reward = base_block_reward(spec); + let rules = EthBlockRules { + base_block_reward: (reward != 0).then_some(reward), + max_blob_gas_per_block: blob_params.as_ref().map(BlobParams::max_blob_gas_per_block), + ..EthBlockRules::default() + }; + let mut executor = EthBlockExecutor::new( + evm, + ctx, + rules, + HarnessReceiptBuilder { compute_bloom: compare_receipt_root }, + ); // When the fixture declares an EIP-7928 block access list, build one alongside execution and - // validate it against the fixture. The block access index follows the EIP-7928 layout: index 0 - // for pre-block system calls, `i + 1` for transaction `i`, and the final index for post-block - // rewards/withdrawals. Only Amsterdam+ fixtures carry a block access list, so pre-Amsterdam - // blocks pay nothing. + // validate it against the fixture. Only Amsterdam+ fixtures carry a block access list, so + // pre-Amsterdam blocks pay nothing. let build_bal = block_access_list(block).is_some(); if build_bal { - evm.state_mut().enable_bal_builder(); - evm.state_mut().reset_bal_index(); + executor.enable_block_access_list_builder(); } let result = (|| -> Result { @@ -476,28 +580,21 @@ fn execute_block( )); } - if let Err(err) = pre_block_system_calls( - &mut evm, - &mut block_state, - spec, - next_block_env, - *parent_block_hash, - beacon_root, - ) { - // A failed pre-block system call invalidates the block only when the fixture expects - // an exception; otherwise it is a test failure. - if should_fail { - return Ok(BlockResolution::Discard); - } - return Err(TestError::case(path, name, err)); + // A captured case's genesis header is the first block's own header, not its parent's. + let parent = (!(captured && block_index == 0)).then_some(*parent_header); + if let Some(header) = block_header(block) + && let Err(reason) = + validate_block_header(block, header, parent, blob_params.as_ref(), spec) + { + return reject_block(should_fail, path, name, reason); + } + + if let Err(err) = executor.apply_pre_execution_changes() { + return reject_block(should_fail, path, name, err.to_string()); } let transactions = block_transactions(block); - // EIP-8037: track execution and state gas separately for the block-header gas check. - let mut cumulative_tx_gas_used = 0u64; - let mut block_execution_gas_used = 0u64; - let mut block_state_gas_used = 0u64; - let mut receipts = Vec::with_capacity(transactions.len()); + let mut post_state_roots = Vec::new(); for (transaction_index, raw_tx) in transactions.iter().enumerate() { hook.transaction_started(TransactionStarted { block_index, @@ -506,7 +603,7 @@ fn execute_block( transaction_index, total_transactions, }); - let tx = match build_tx(raw_tx) { + let (tx_env, tx) = match build_tx(raw_tx, spec, validate) { Ok(tx) => tx, Err(_err) if should_fail => { return Ok(BlockResolution::Discard); @@ -524,83 +621,28 @@ fn execute_block( } }; - // EIP-8037: per-dimension block-level inclusion check (execution-specs - // `process_transaction`). A transaction is only includable when its gas limit -- - // capped by the EIP-7825 limit on the execution dimension, uncapped on the state - // dimension -- still fits the block gas remaining on both dimensions. - if spec.enables(SpecId::AMSTERDAM) - && let Some(header) = block_header(block) - { - let block_gas_limit = header.gas_limit.saturating_to::(); - let tx_gas_limit = tx.gas_limit(); - let execution_gas_available = - block_gas_limit.saturating_sub(block_execution_gas_used); - let state_gas_available = block_gas_limit.saturating_sub(block_state_gas_used); - if tx_gas_limit.min(evm.version().tx_gas_limit_cap) > execution_gas_available - || tx_gas_limit > state_gas_available - { - if should_fail { - return Ok(BlockResolution::Discard); - } - return Err(TestError::case( - path, - name, - TestErrorKind::UnexpectedFailure(format!( - "transaction gas limit {tx_gas_limit} exceeds available block gas \ - (execution {execution_gas_available}, state {state_gas_available})" - )), - )); - } - } - - // Transaction `i` is recorded at block access index `i + 1`. - if build_bal { - evm.state_mut().bump_bal_index(); - } - match execute_tx(&mut evm, &mut block_state, &tx) { - Ok(result) => { - cumulative_tx_gas_used = - cumulative_tx_gas_used.saturating_add(result.tx_gas_used()); - block_execution_gas_used = - block_execution_gas_used.saturating_add(result.execution_gas_spent()); - block_state_gas_used = - block_state_gas_used.saturating_add(result.state_gas_spent()); + match executor.execute_transaction((tx_env, &tx)) { + Ok(gas) => { + let receipt = executor.receipts.last().expect("committed transaction receipt"); hook.transaction_finished(TransactionFinished { block_index, total_blocks, block_number, transaction_index, total_transactions, - gas_used: result.tx_gas_used(), - execution_gas_used: result.execution_gas_spent(), - state_gas_used: result.state_gas_spent(), - success: result.status, - logs: &result.logs, + gas_used: gas.tx_gas_used(), + execution_gas_used: gas.regular_gas_used(), + state_gas_used: gas.state_gas_used(), + success: receipt.is_success(), + logs: receipt.logs(), }); - if compare_receipt_root { - let status = if spec.enables(SpecId::BYZANTIUM) { - Eip658Value::Eip658(result.status) - } else { - let mut post_state = receipt_base - .as_ref() - .expect("pre-Byzantium receipt base should be available") - .clone(); - post_state.commit_source(&block_state); - Eip658Value::PostState(state_root_from_database(&post_state)) - }; - let receipt = Receipt { - status, - cumulative_gas_used: cumulative_tx_gas_used, - logs: result.logs, - } - .with_bloom(); - let tx_type = TxType::try_from(tx.inner().ty()) - .expect("executor only builds supported Ethereum transaction types"); - receipts.push(ReceiptEnvelope::from_typed(tx_type, receipt)); + if let Some(base) = &receipt_base { + let mut post_state = base.clone(); + post_state.cache.merge(executor.evm.overlay_db_mut().cache.clone()); + post_state_roots.push(state_root_from_database(&post_state)); } } - Err(err) if should_fail => { - let _ = err; + Err(_err) if should_fail => { return Ok(BlockResolution::Discard); } Err(err) => { @@ -612,23 +654,68 @@ fn execute_block( total_transactions, error: &err, }); - return Err(TestError::case(path, name, err.into())); + return Err(TestError::case( + path, + name, + TestErrorKind::UnexpectedFailure(err.to_string()), + )); } } } + for ommer in block_ommers(block) { + let age = next_block_env.number.checked_sub(ommer.number); + if age.is_none_or(|age| age.is_zero() || age > U256::from(6)) { + let err = TestErrorKind::InvalidOmmerAge { + block_number: next_block_env.number, + ommer_number: ommer.number, + }; + if should_fail { + return Ok(BlockResolution::Discard); + } + return Err(TestError::case(path, name, err)); + } + } + + // A request system contract without code makes the block invalid (execution-specs + // `SYSTEM_CONTRACT_EMPTY`); the executor runs the call as a no-op. + if spec.enables(SpecId::PRAGUE) { + let request_contracts: &[Address] = if spec.enables(SpecId::AMSTERDAM) { + &[ + WITHDRAWAL_REQUEST_ADDRESS, + CONSOLIDATION_REQUEST_ADDRESS, + BUILDER_DEPOSIT_REQUEST_ADDRESS, + BUILDER_EXIT_REQUEST_ADDRESS, + ] + } else { + &[WITHDRAWAL_REQUEST_ADDRESS, CONSOLIDATION_REQUEST_ADDRESS] + }; + for address in request_contracts { + let info = executor.evm.read_account_info(address).map_err(|err| { + TestError::case(path, name, TestErrorKind::UnexpectedFailure(err.to_string())) + })?; + if info.is_none_or(|info| info.is_empty_code_hash()) { + return reject_block( + should_fail, + path, + name, + format!("system contract {address} has no code"), + ); + } + } + } + + let post_execution = match executor.apply_post_execution_changes(0) { + Ok(output) => output, + Err(err) => return reject_block(should_fail, path, name, err.to_string()), + }; + // Validate the block header's gas used against the executed transactions. Under EIP-8037 // (Amsterdam+) execution and state gas are tracked separately and the header records their // max; earlier forks record the cumulative per-transaction gas used (refunds included). - // Block-validation failures on a block that expects an exception resolve to a discard, - // mirroring execution-specs raising `InvalidBlock`. if let Some(expected) = block_gas_used(block) { let expected = expected.saturating_to::(); - let actual = if spec.enables(SpecId::AMSTERDAM) { - block_execution_gas_used.max(block_state_gas_used) - } else { - cumulative_tx_gas_used - }; + let actual = post_execution.gas_used; if actual != expected { if should_fail { return Ok(BlockResolution::Discard); @@ -641,8 +728,40 @@ fn execute_block( } } + if spec.enables(SpecId::CANCUN) + && let Some(header) = block_header(block) + && header.blob_gas_used != Some(U256::from(post_execution.blob_gas_used)) + { + return reject_block( + should_fail, + path, + name, + format!( + "block blob gas used mismatch: header {:?}, computed {}", + header.blob_gas_used, post_execution.blob_gas_used + ), + ); + } + if compare_receipt_root && let Some(header) = block_header(block) { - let got = alloy_consensus::proofs::calculate_receipt_root(&receipts); + let got = if receipt_base.is_some() { + let receipts = executor + .receipts + .iter() + .zip(&post_state_roots) + .map(|(receipt, root)| { + let receipt_root = Receipt { + status: Eip658Value::PostState(*root), + cumulative_gas_used: receipt.cumulative_gas_used(), + logs: receipt.logs().to_vec(), + }; + ReceiptEnvelope::from_typed(receipt.tx_type(), receipt_root.with_bloom()) + }) + .collect::>(); + alloy_consensus::proofs::calculate_receipt_root(&receipts) + } else { + alloy_consensus::proofs::calculate_receipt_root(&executor.receipts) + }; if got != header.receipt_trie { if should_fail { return Ok(BlockResolution::Discard); @@ -655,29 +774,25 @@ fn execute_block( } } - // Post-block rewards and withdrawals are recorded at the final (post-execution) index. - if build_bal { - evm.state_mut().bump_bal_index(); - } - - if let Err(err) = post_block_transition( - &mut evm, - &mut block_state, - spec, - next_block_env, - block_ommers(block), - block_withdrawals(block), - ) { - // A failed post-block system call (e.g. SYSTEM_CONTRACT_CALL_FAILED) invalidates the - // block, mirroring execution-specs raising `InvalidBlock`. - if should_fail { - return Ok(BlockResolution::Discard); + if spec.enables(SpecId::PRAGUE) + && let Some(header) = block_header(block) + { + let computed = post_execution.requests.requests_hash(); + if header.requests_hash != Some(computed) { + return reject_block( + should_fail, + path, + name, + format!( + "requests hash mismatch: header {:?}, computed {computed}", + header.requests_hash + ), + ); } - return Err(TestError::case(path, name, err)); } if let Some(expected_bal) = block_access_list(block) { - let built = evm.state_mut().take_bal_builder().unwrap_or_default(); + let built = executor.take_block_access_list().unwrap_or_default(); if let Err(kind) = check_block_access_list(block_index, built, expected_bal, block_header(block)) { @@ -693,11 +808,20 @@ fn execute_block( return Err(TestError::case(path, name, TestErrorKind::UnexpectedSuccess(expected))); } - Ok(BlockResolution::Commit) + let Some(block_hash) = block_hash else { + return Err(TestError::case( + path, + name, + TestErrorKind::UnexpectedFailure("committed block has no header".into()), + )); + }; + Ok(BlockResolution::Commit(block_hash)) })(); // The EVM was constructed with this concrete database above; recover it before returning so // invalid blocks leave the caller's state unchanged. + let mut evm = executor.evm; + let block_state = executor.block_state; let (mut restored_database, block_db_stats_counts) = if db_stats { let stats = evm .database_as_mut::>() @@ -719,15 +843,16 @@ fn execute_block( } match result { - Ok(BlockResolution::Commit) => { - restored_database.commit_source(&block_state); - restored_database - .insert_block_hash(&next_block_env.number, &block_hash.unwrap_or_default()); + Ok(BlockResolution::Commit(block_hash)) => { + // Only the plain state is applied, so the block's reverts are not built. + let block_state = block_state.into_bundle_with_retention(BundleRetention::PlainState); + restored_database.commit_source(&BundleSource(&block_state)); + restored_database.insert_block_hash(&next_block_env.number, &block_hash); *database = restored_database; *block_env = next_block_env; - *parent_block_hash = block_hash; - if let Some(excess_blob_gas) = this_excess_blob_gas { - *parent_excess_blob_gas = excess_blob_gas; + *parent_block_hash = Some(block_hash); + if let Some(header) = block_header(block) { + *parent_header = header; } if trace || compare_state_root { let got = state_root_from_database(database); @@ -760,25 +885,34 @@ fn execute_block( } } -fn print_db_stats(counts: DbStatsCounts) { - let style = db_stats_style(); - eprintln!("{style}db stats{style:#}: get_account={}", counts.get_account); - eprintln!("{style}db stats{style:#}: get_code_by_hash={}", counts.get_code_by_hash); - eprintln!("{style}db stats{style:#}: get_storage={}", counts.get_storage); - eprintln!( - "{style}db stats{style:#}: get_storage_same_address_repeats={}", - counts.get_storage_same_address_repeats - ); - eprintln!( - "{style}db stats{style:#}: get_storage_same_address_longest_streak={}", - counts.get_storage_same_address_longest_streak - ); - eprintln!("{style}db stats{style:#}: get_block_hash={}", counts.get_block_hash); +/// Builds the same receipts as [`EthReceiptBuilder`](evm2::block::ethereum::EthReceiptBuilder), +/// but computes the logs bloom only when the receipt root is compared, as nothing else reads it. +#[derive(Clone, Copy, Debug)] +struct HarnessReceiptBuilder { + compute_bloom: bool, } -#[inline] -const fn db_stats_style() -> Style { - Style::new().fg_color(Some(Color::Ansi(AnsiColor::BrightCyan))).bold() +impl ReceiptBuilder for HarnessReceiptBuilder { + type Transaction = EthereumTxEnvelope; + type Receipt = ReceiptEnvelope; + + fn build_receipt( + &self, + ctx: ReceiptBuilderCtx>, + ) -> ReceiptEnvelope { + let ReceiptBuilderCtx { tx_type, result, cumulative_gas_used } = ctx; + let receipt = Receipt { + status: Eip658Value::Eip658(result.is_success()), + cumulative_gas_used, + logs: result.logs, + }; + let receipt = if self.compute_bloom { + receipt.with_bloom() + } else { + ReceiptWithBloom::new(receipt, Bloom::ZERO) + }; + ReceiptEnvelope::from_typed(tx_type, receipt) + } } fn block_number(block: &Block) -> Option { @@ -831,176 +965,123 @@ fn block_ommers(block: &Block) -> &[BlockHeader] { block.rlp_decoded.as_ref().map(|decoded| decoded.uncle_headers.as_slice()).unwrap_or_default() } -fn pre_block_system_calls( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, +/// Block-level validity checks that execution-specs performs before executing the transactions: +/// the EIP-7934 block RLP size limit and the EIP-4844 header blob fields. +fn validate_block_header( + block: &Block, + header: &BlockHeader, + parent: Option<&BlockHeader>, + blob_params: Option<&BlobParams>, spec: SpecId, - block: BlockEnv, - parent_block_hash: Option, - parent_beacon_block_root: Option, -) -> Result<(), TestErrorKind> { - if block.number.is_zero() { - return Ok(()); - } - if spec.enables(SpecId::CANCUN) - && let Some(root) = parent_beacon_block_root - { - run_system_call( - evm, - block_state, - BEACON_ROOTS_ADDRESS, - Bytes::copy_from_slice(root.as_slice()), - "eip4788", - )?; - } - if spec.enables(SpecId::PRAGUE) - && let Some(hash) = parent_block_hash - { - run_system_call( - evm, - block_state, - HISTORY_STORAGE_ADDRESS, - Bytes::copy_from_slice(hash.as_slice()), - "eip2935", - )?; +) -> Result<(), String> { + if spec.enables(SpecId::OSAKA) && block.rlp.len() > MAX_RLP_BLOCK_SIZE { + return Err(format!( + "block RLP size {} exceeds the limit {MAX_RLP_BLOCK_SIZE}", + block.rlp.len() + )); } - Ok(()) -} -fn post_block_transition( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - spec: SpecId, - block: BlockEnv, - ommers: &[BlockHeader], - withdrawals: &[Withdrawal], -) -> Result<(), TestErrorKind> { - let reward = block_reward(spec, ommers.len()); - if reward != 0 { - increment_balance(evm, block_state, block.beneficiary, U256::from(reward))?; - - let base_reward = U256::from(base_block_reward(spec)); - for ommer in ommers { - let Some(age) = block.number.checked_sub(ommer.number) else { - return Err(TestErrorKind::InvalidOmmerAge { - block_number: block.number, - ommer_number: ommer.number, - }); - }; - if age.is_zero() || age > U256::from(6) { - return Err(TestErrorKind::InvalidOmmerAge { - block_number: block.number, - ommer_number: ommer.number, - }); - } - let reward = (U256::from(8) - age) * base_reward / U256::from(8); - increment_balance(evm, block_state, ommer.coinbase, reward)?; + // The blob parameters are `None` before Cancun. + let Some(blob_params) = blob_params else { + if header.excess_blob_gas.is_some() || header.blob_gas_used.is_some() { + return Err("pre-Cancun block header carries blob gas fields".to_string()); } - } + return Ok(()); + }; - if spec.enables(SpecId::SHANGHAI) { - for withdrawal in withdrawals { - increment_balance( - evm, - block_state, - withdrawal.address, - withdrawal.amount.saturating_mul(U256::from(ONE_GWEI)), - )?; - } + if header.blob_gas_used.is_none() { + return Err("block header is missing blobGasUsed".to_string()); } - - if spec.enables(SpecId::PRAGUE) { - run_system_call(evm, block_state, WITHDRAWAL_REQUEST_ADDRESS, Bytes::new(), "eip7002")?; - run_system_call( - evm, - block_state, - evm2::evm::CONSOLIDATION_REQUEST_ADDRESS, - Bytes::new(), - "eip7251", - )?; + let Some(parent) = parent else { return Ok(()) }; + let parent_excess = parent.excess_blob_gas.unwrap_or_default(); + let parent_used = parent.blob_gas_used.unwrap_or_default(); + // Reject parent values whose sum the excess blob gas formula cannot add without overflow. + if parent_excess.saturating_add(parent_used) > U256::from(u64::MAX) { + return Err(format!( + "parent blob gas fields out of range: excess {parent_excess}, used {parent_used}" + )); } - - // EIP-8282: builder deposit (request type 0x03) and builder exit (0x04) requests, Amsterdam+. - if spec.enables(SpecId::AMSTERDAM) { - run_system_call( - evm, - block_state, - evm2::evm::BUILDER_DEPOSIT_REQUEST_ADDRESS, - Bytes::new(), - "eip8282_deposit", - )?; - run_system_call( - evm, - block_state, - evm2::evm::BUILDER_EXIT_REQUEST_ADDRESS, - Bytes::new(), - "eip8282_exit", - )?; + let parent_base_fee = parent.base_fee_per_gas.unwrap_or_default(); + let expected_excess_blob_gas = + next_excess_blob_gas(blob_params, parent_excess.to(), parent_used.to(), parent_base_fee); + if header.excess_blob_gas != Some(U256::from(expected_excess_blob_gas)) { + return Err(format!( + "excess blob gas mismatch: header {:?}, expected {expected_excess_blob_gas}", + header.excess_blob_gas + )); } Ok(()) } -fn run_system_call( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - address: Address, - data: Bytes, - label: &'static str, -) -> Result<(), TestErrorKind> { - let executed = evm.system_call(SystemTx::new(address, data))?; - if !executed.result().status { - let _ = executed.discard(); - let has_code = match evm.read_account_info(&address) { - Ok(info) => info.is_some_and(|info| !info.is_empty_code_hash()), - Err(code) => return Err(TestErrorKind::UnexpectedFailure(code.to_string())), - }; - if has_code { - return Err(TestErrorKind::SystemCall(label)); - } - return Ok(()); +/// Returns the excess blob gas of the block after a parent with these values (execution-specs +/// `calculate_excess_blob_gas`, with the EIP-7918 reserve price from Osaka). Unlike +/// [`BlobParams::next_block_excess_blob_gas_osaka`], the reserve price comparison takes a base +/// fee of any width. +fn next_excess_blob_gas( + params: &BlobParams, + parent_excess: u64, + parent_used: u64, + parent_base_fee: U256, +) -> u64 { + let excess = parent_excess + parent_used; + let target = params.target_blob_gas_per_block(); + if excess < target { + return 0; + } + if reserve_price_exceeds_blob_price(params, parent_excess, parent_base_fee) { + // The scaled blob gas is at most `parent_used`, so the sum stays below `u64::MAX`. + let scaled_used = u128::from(parent_used) + * u128::from(params.max_blob_count - params.target_blob_count) + / u128::from(params.max_blob_count); + parent_excess + scaled_used as u64 + } else { + excess - target } - let _ = executed.commit_to(block_state); - Ok(()) -} - -fn execute_tx( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - tx: &RecoveredTxEnvelope, -) -> Result { - Ok(evm.transact(tx)?.commit_to(block_state)) -} - -fn commit_state_changes( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - changes: &S, -) { - let mut sink = Tee::new(evm.overlay_db_mut(), block_state); - let Ok(()) = changes.visit(&mut sink); } -struct AccountStateChange { - address: Address, - original: Option, - current: Option, +/// Returns whether `blob_base_cost * base_fee > DATA_GAS_PER_BLOB * blob_price` for the blob price +/// at this excess blob gas, in unbounded integer arithmetic like execution-specs. The blob price +/// is the EIP-4844 `fake_exponential` sum, which this stops accumulating once it reaches the +/// reserve price, so every product stays far below the 512-bit limit. +fn reserve_price_exceeds_blob_price( + params: &BlobParams, + excess_blob_gas: u64, + base_fee: U256, +) -> bool { + let reserve_price = U512::from(params.blob_base_cost) * U512::from(base_fee); + let denominator = U512::from(params.update_fraction); + let numerator = U512::from(excess_blob_gas); + let gas_per_blob = U512::from(DATA_GAS_PER_BLOB); + let mut term = U512::from(params.min_blob_fee) * denominator; + let mut sum = U512::ZERO; + let mut index = U512::ONE; + while !term.is_zero() { + sum += term; + if gas_per_blob * (sum / denominator) >= reserve_price { + return false; + } + term = term * numerator / (denominator * index); + index += U512::ONE; + } + reserve_price > gas_per_blob * (sum / denominator) } -impl StateChangeSource for AccountStateChange { - fn visit(&self, sink: &mut S) -> Result<(), S::Error> { - sink.account(AccountChangeRef { - address: self.address, - original: self.original.as_ref(), - current: self.current.as_ref(), - created: false, - selfdestructed: false, - }) +/// Resolves an invalid block: it is discarded when the fixture expects an exception, and a test +/// failure otherwise. +fn reject_block( + should_fail: bool, + path: &Path, + name: &str, + reason: String, +) -> Result { + if should_fail { + Ok(BlockResolution::Discard) + } else { + Err(TestError::case(path, name, TestErrorKind::UnexpectedFailure(reason))) } } -fn parse_state( - accounts: &std::collections::BTreeMap, -) -> Result { +fn parse_state(accounts: &BTreeMap) -> Result { let mut database = InMemoryDB::default(); for (address, account) in accounts { let nonce = account.nonce.try_into().map_err(|_| TestErrorKind::Overflow("nonce"))?; @@ -1018,69 +1099,80 @@ fn parse_state( Ok(database) } -fn block_env_from_header(header: &BlockHeader, excess_blob_gas: u64, spec: SpecId) -> BlockEnv { - let excess_blob_gas = - header.excess_blob_gas.map(|gas| gas.saturating_to::()).unwrap_or(excess_blob_gas); - BlockEnvExt { +fn block_env_from_header( + header: &BlockHeader, + schedule: &ForkSchedule, +) -> Result { + let excess_blob_gas = header.excess_blob_gas.unwrap_or_default().saturating_to::(); + let blob_basefee = schedule + .blob_params(header.number, header.timestamp)? + .map_or(U256::ONE, |params| U256::from(params.calc_blob_fee(excess_blob_gas))); + Ok(BlockEnvExt { number: header.number, beneficiary: header.coinbase, timestamp: header.timestamp, gas_limit: header.gas_limit, basefee: header.base_fee_per_gas.unwrap_or_default(), difficulty: header.difficulty, - prevrandao: if header.difficulty.is_zero() { - U256::from_be_slice(header.mix_hash.as_slice()) - } else { - U256::ZERO - }, - blob_basefee: U256::from( - blob_params_for_timestamp(header.timestamp, spec).calc_blob_fee(excess_blob_gas), - ), + prevrandao: if header.difficulty.is_zero() { header.mix_hash.into() } else { U256::ZERO }, + blob_basefee, slot_num: header.slot_number.unwrap_or_default(), ext: (), _non_exhaustive: (), - } -} - -fn blob_params_for_timestamp(timestamp: U256, spec: SpecId) -> BlobParams { - const MAINNET_BPO1_TIMESTAMP: u64 = 1_765_290_071; - const MAINNET_BPO2_TIMESTAMP: u64 = 1_767_747_671; - - if spec.enables(SpecId::AMSTERDAM) || timestamp.to::() >= MAINNET_BPO2_TIMESTAMP { - BlobParams::bpo2() - } else if timestamp.to::() >= MAINNET_BPO1_TIMESTAMP { - BlobParams::bpo1() - } else if spec.enables(SpecId::OSAKA) { - BlobParams::osaka() - } else if spec.enables(SpecId::PRAGUE) { - BlobParams::prague() - } else { - BlobParams::cancun() - } + }) } -fn build_tx(raw: &Transaction) -> Result { - let caller = raw.sender.ok_or(TestErrorKind::MissingSender)?; +fn build_tx( + raw: &Transaction, + spec: SpecId, + validate: bool, +) -> Result<(RecoveredTxEnvelope, EthereumTxEnvelope), TestErrorKind> { let gas_limit = raw.gas_limit.try_into().map_err(|_| TestErrorKind::Overflow("gasLimit"))?; let nonce = raw.nonce.try_into().map_err(|_| TestErrorKind::Overflow("nonce"))?; + let tx_type = raw.tx_type(); + let (signer, chain_id, signature) = if validate { + let (signature, legacy_chain_id) = + signature_from_vrs(raw.r, raw.s, raw.v, tx_type == 0, spec)?; + // A legacy transaction signs its chain ID through `v`, so `v` decides it. + let chain_id = if tx_type == 0 { legacy_chain_id.map(U256::from) } else { raw.chain_id }; + (TxSigner::Signature { signature, spec }, chain_id, signature) + } else { + // The signature only names the transaction (its hash) when the sender is not recovered. + let signature = Signature::new(raw.r, raw.s, raw.v.bit(0)); + ( + TxSigner::Fixture(raw.sender.ok_or(TestErrorKind::MissingSender)?), + raw.chain_id, + signature, + ) + }; - Ok(build_recovered_tx(TxFields { - tx_type: Some(raw.tx_type()), - caller, - kind: raw.kind(), - data: raw.data.clone(), - gas_limit, - nonce, - value: raw.value, - chain_id: raw.chain_id, - gas_price: raw.gas_price, - max_fee_per_gas: raw.max_fee_per_gas, - max_priority_fee_per_gas: raw.max_priority_fee_per_gas, - access_list: access_list(raw)?, - authorization_list: signed_authorizations(raw.authorization_list.as_deref())?, - blob_versioned_hashes: raw.blob_versioned_hashes.clone(), - max_fee_per_blob_gas: raw.max_fee_per_blob_gas, - })?) + let (tx, signed) = build_signed_recovered_tx( + TxFields { + tx_type: Some(tx_type), + kind: raw.kind(), + data: raw.data.clone(), + gas_limit, + nonce, + value: raw.value, + chain_id, + gas_price: raw.gas_price, + max_fee_per_gas: raw.max_fee_per_gas, + max_priority_fee_per_gas: raw.max_priority_fee_per_gas, + access_list: access_list(raw)?, + authorization_list: signed_authorizations(raw.authorization_list.as_deref())?, + blob_versioned_hashes: raw.blob_versioned_hashes.clone(), + max_fee_per_blob_gas: raw.max_fee_per_blob_gas, + }, + signer, + signature, + )?; + if validate + && let Some(expected) = raw.sender + && expected != tx.signer() + { + return Err(TestErrorKind::SenderMismatch { recovered: tx.signer(), expected }); + } + Ok((tx, signed)) } fn access_list(raw: &Transaction) -> Result, TestErrorKind> { @@ -1093,11 +1185,6 @@ fn access_list(raw: &Transaction) -> Result, TestErrorKind Ok(Some(rpc_access_list(access_list.iter()))) } -const fn block_reward(spec: SpecId, ommers: usize) -> u128 { - let reward = base_block_reward(spec); - reward + (reward >> 5) * ommers as u128 -} - const fn base_block_reward(spec: SpecId) -> u128 { if spec.enables(SpecId::MERGE) { 0 @@ -1110,34 +1197,9 @@ const fn base_block_reward(spec: SpecId) -> u128 { } } -fn increment_balance( - evm: &mut Evm<'_, BaseEvmTypes>, - block_state: &mut BlockStateAccumulator, - address: Address, - amount: U256, -) -> Result<(), TestErrorKind> { - let original = match evm.read_account_info(&address) { - Ok(info) => info, - Err(code) => return Err(TestErrorKind::UnexpectedFailure(code.to_string())), - }; - let mut current = original.clone().unwrap_or_default(); - current.balance = current.balance.saturating_add(amount); - if current.code_hash.is_zero() { - current.code_hash = KECCAK256_EMPTY; - } - // EELS `modify_state` destroys an account left empty by the modification, so a zero-amount - // withdrawal neither creates its recipient nor keeps a pre-existing empty one alive. The - // BAL commit below still runs to record the touched address. - let current = (!current.is_empty()).then_some(current); - - let change = AccountStateChange { address, original, current }; - commit_state_changes(evm, block_state, &change); - Ok(()) -} - fn validate_post_state( database: &InMemoryDB, - expected: &std::collections::BTreeMap, + expected: &BTreeMap, ) -> Result<(), TestErrorKind> { for (address, expected_account) in expected { let info = database.cache.accounts.get(address).cloned().flatten().unwrap_or_default(); @@ -1162,9 +1224,9 @@ fn validate_post_state( .unwrap_or_default(); if actual_code != expected_account.code.as_ref() { return Err(TestErrorKind::UnexpectedFailure(format!( - "code mismatch for {address}: got 0x{}, expected 0x{}", - alloy_primitives::hex::encode(actual_code), - alloy_primitives::hex::encode(&expected_account.code) + "code mismatch for {address}: got {}, expected {}", + alloy_primitives::hex::encode_prefixed(actual_code), + alloy_primitives::hex::encode_prefixed(&expected_account.code) ))); } @@ -1258,7 +1320,7 @@ fn check_block_access_list( } } - if built.as_slice() == expected.as_slice() { + if built == *expected { return Ok(()); } Err(TestErrorKind::BlockAccessListMismatch { @@ -1268,18 +1330,10 @@ fn check_block_access_list( } /// Renders a compact per-account summary of how the built and expected block access lists differ. -fn format_bal_diff( - built: &alloy_eip7928::BlockAccessList, - expected: &alloy_eip7928::BlockAccessList, -) -> String { - use std::{ - collections::{BTreeMap, BTreeSet}, - fmt::Write, - }; - - let built: BTreeMap<_, _> = built.iter().map(|account| (account.address, account)).collect(); +fn format_bal_diff(built: &BlockAccessList, expected: &BlockAccessList) -> String { + let built: BTreeMap<_, _> = built.iter().map(|account| (account.address(), account)).collect(); let expected: BTreeMap<_, _> = - expected.iter().map(|account| (account.address, account)).collect(); + expected.iter().map(|account| (account.address(), account)).collect(); let addresses: BTreeSet<_> = built.keys().chain(expected.keys()).copied().collect(); let mut out = String::new(); @@ -1340,58 +1394,341 @@ fn is_blockchain_fork_skipped(spec: SpecId) -> bool { is_fork_skipped(spec) } -fn fork_to_spec_id(fork: ForkSpec) -> SpecId { - match fork { - ForkSpec::Frontier => SpecId::FRONTIER, - ForkSpec::Homestead => SpecId::HOMESTEAD, - ForkSpec::EIP150 => SpecId::TANGERINE, - ForkSpec::EIP158 => SpecId::SPURIOUS_DRAGON, - ForkSpec::Byzantium => SpecId::BYZANTIUM, - ForkSpec::Constantinople | ForkSpec::ConstantinopleFix => SpecId::PETERSBURG, - ForkSpec::Istanbul => SpecId::ISTANBUL, - ForkSpec::Berlin => SpecId::BERLIN, - ForkSpec::London => SpecId::LONDON, - ForkSpec::Paris - | ForkSpec::MergeEOF - | ForkSpec::MergeMeterInitCode - | ForkSpec::MergePush0 => SpecId::MERGE, - ForkSpec::Shanghai => SpecId::SHANGHAI, - ForkSpec::Cancun => SpecId::CANCUN, - ForkSpec::Prague => SpecId::PRAGUE, - ForkSpec::Osaka => SpecId::OSAKA, - ForkSpec::Amsterdam => SpecId::AMSTERDAM, - ForkSpec::FrontierToHomesteadAt5 - | ForkSpec::HomesteadToDaoAt5 - | ForkSpec::HomesteadToEIP150At5 - | ForkSpec::EIP158ToByzantiumAt5 - | ForkSpec::ByzantiumToConstantinopleAt5 - | ForkSpec::ByzantiumToConstantinopleFixAt5 - | ForkSpec::BerlinToLondonAt5 - | ForkSpec::ParisToShanghaiAtTime15k - | ForkSpec::ShanghaiToCancunAtTime15k - | ForkSpec::CancunToPragueAtTime15k - | ForkSpec::PragueToOsakaAtTime15k - | ForkSpec::OsakaToBPO1AtTime15k - | ForkSpec::BPO1ToBPO2AtTime15k - | ForkSpec::BPO2ToBPO3AtTime15k - | ForkSpec::BPO3ToBPO4AtTime15k - | ForkSpec::BPO2ToAmsterdamAtTime15k => unreachable!("transition forks are skipped"), +/// The specs a fork runs, in order. +struct ForkSchedule { + initial: ForkStage, + /// The stage that a transition fork switches to, and when. + transition: Option<(Activation, ForkStage)>, + /// The test's blob parameters by fork. + blob_schedule: BTreeMap, +} + +/// What is active between two transitions: a spec, and the fork that sets the blob parameters. +#[derive(Clone, Copy)] +pub(crate) struct ForkStage { + pub(crate) spec: SpecId, + /// `None` before Cancun. + pub(crate) blob_fork: Option, +} + +/// When a transition fork activates. +#[derive(Clone, Copy)] +enum Activation { + Block(u64), + Timestamp(u64), +} + +impl ForkStage { + /// A stage whose blob fork is the latest one the spec enables. + const fn new(spec: SpecId) -> Self { + let blob_fork = if spec.enables(SpecId::AMSTERDAM) { + Some(BlobFork::Amsterdam) + } else if spec.enables(SpecId::OSAKA) { + Some(BlobFork::Osaka) + } else if spec.enables(SpecId::PRAGUE) { + Some(BlobFork::Prague) + } else if spec.enables(SpecId::CANCUN) { + Some(BlobFork::Cancun) + } else { + None + }; + Self { spec, blob_fork } + } + + /// A stage of a blob parameters only fork, which keeps the Osaka spec. + const fn bpo(blob_fork: BlobFork) -> Self { + Self { spec: SpecId::OSAKA, blob_fork: Some(blob_fork) } } } +impl ForkSchedule { + const fn fixed(spec: SpecId) -> Self { + Self::new(ForkStage::new(spec), None) + } + + const fn transition(initial: SpecId, activation: Activation, spec: SpecId) -> Self { + Self::new(ForkStage::new(initial), Some((activation, ForkStage::new(spec)))) + } + + const fn new(initial: ForkStage, transition: Option<(Activation, ForkStage)>) -> Self { + Self { initial, transition, blob_schedule: BTreeMap::new() } + } + + /// Returns the stage of the block with this number and timestamp. + fn stage_at(&self, number: U256, timestamp: U256) -> ForkStage { + match self.transition { + Some((Activation::Block(at), stage)) if number >= U256::from(at) => stage, + Some((Activation::Timestamp(at), stage)) if timestamp >= U256::from(at) => stage, + _ => self.initial, + } + } + + /// Returns the spec of the block with this number and timestamp. + fn spec_at(&self, number: U256, timestamp: U256) -> SpecId { + self.stage_at(number, timestamp).spec + } + + /// Returns the blob parameters of the block with this number and timestamp, or `None` before + /// Cancun. + fn blob_params( + &self, + number: U256, + timestamp: U256, + ) -> Result, TestErrorKind> { + let stage = self.stage_at(number, timestamp); + let Some(blob_fork) = stage.blob_fork else { return Ok(None) }; + let entry = self + .blob_schedule + .get(&blob_fork) + .ok_or(TestErrorKind::MissingBlobSchedule(blob_fork))?; + if entry.base_fee_update_fraction.is_zero() { + return Err(TestErrorKind::ZeroBlobUpdateFraction(blob_fork)); + } + Ok(Some(entry.params(stage.spec))) + } + + /// Returns whether any block of the fork runs Prague or later. + fn runs_prague(&self) -> bool { + self.initial.spec.enables(SpecId::PRAGUE) + || self.transition.is_some_and(|(_, stage)| stage.spec.enables(SpecId::PRAGUE)) + } + + /// Returns whether any spec of the fork is skipped. + fn is_skipped(&self) -> bool { + is_blockchain_fork_skipped(self.initial.spec) + || self.transition.is_some_and(|(_, stage)| is_blockchain_fork_skipped(stage.spec)) + } +} + +/// Returns the fork schedule of a test case, with the blob parameters from its `config`. +fn case_schedule( + path: &Path, + name: &str, + test_case: &BlockchainTestCase, +) -> Result { + let mut schedule = fork_schedule(test_case.network).ok_or_else(|| { + TestError::case(path, name, TestErrorKind::UnsupportedFork(test_case.network)) + })?; + if let Some(config) = &test_case.config { + schedule.blob_schedule.clone_from(&config.blob_schedule); + } + Ok(schedule) +} + +/// Returns the blob parameters of the block with this header in a test case, or `None` before +/// Cancun. +/// +/// The parameters come from the case's `config.blobSchedule`, and the fork that is active at the +/// header's number and timestamp. +pub fn blob_params( + path: &Path, + name: &str, + test_case: &BlockchainTestCase, + header: &BlockHeader, +) -> Result, TestError> { + case_schedule(path, name, test_case)? + .blob_params(header.number, header.timestamp) + .map_err(|err| TestError::case(path, name, err)) +} + +/// Returns the stage a state test runs a fork under, or `None` for an unsupported fork. +/// +/// A state test runs one transaction under one stage, so a transition fork runs under the stage +/// after the transition. +pub(crate) fn state_stage(fork: ForkSpec) -> Option { + let schedule = fork_schedule(fork)?; + Some(schedule.transition.map_or(schedule.initial, |(_, stage)| stage)) +} + +/// Returns the specs of a fork, or `None` for the forks the runner does not support: the DAO fork, +/// Constantinople and the Merge feature forks. +const fn fork_schedule(fork: ForkSpec) -> Option { + use Activation::{Block, Timestamp}; + const AT_5: Activation = Block(5); + const AT_15K: Activation = Timestamp(15_000); + + Some(match fork { + ForkSpec::BPO1 => ForkSchedule::new(ForkStage::bpo(BlobFork::Bpo1), None), + ForkSpec::BPO2 => ForkSchedule::new(ForkStage::bpo(BlobFork::Bpo2), None), + ForkSpec::FrontierToHomesteadAt5 => { + ForkSchedule::transition(SpecId::FRONTIER, AT_5, SpecId::HOMESTEAD) + } + ForkSpec::HomesteadToEIP150At5 => { + ForkSchedule::transition(SpecId::HOMESTEAD, AT_5, SpecId::TANGERINE) + } + ForkSpec::EIP158ToByzantiumAt5 => { + ForkSchedule::transition(SpecId::SPURIOUS_DRAGON, AT_5, SpecId::BYZANTIUM) + } + ForkSpec::ByzantiumToConstantinopleAt5 | ForkSpec::ByzantiumToConstantinopleFixAt5 => { + ForkSchedule::transition(SpecId::BYZANTIUM, AT_5, SpecId::PETERSBURG) + } + ForkSpec::BerlinToLondonAt5 => { + ForkSchedule::transition(SpecId::BERLIN, AT_5, SpecId::LONDON) + } + ForkSpec::ParisToShanghaiAtTime15k => { + ForkSchedule::transition(SpecId::MERGE, AT_15K, SpecId::SHANGHAI) + } + ForkSpec::ShanghaiToCancunAtTime15k => { + ForkSchedule::transition(SpecId::SHANGHAI, AT_15K, SpecId::CANCUN) + } + ForkSpec::CancunToPragueAtTime15k => { + ForkSchedule::transition(SpecId::CANCUN, AT_15K, SpecId::PRAGUE) + } + ForkSpec::PragueToOsakaAtTime15k => { + ForkSchedule::transition(SpecId::PRAGUE, AT_15K, SpecId::OSAKA) + } + ForkSpec::OsakaToBPO1AtTime15k => ForkSchedule::new( + ForkStage::new(SpecId::OSAKA), + Some((AT_15K, ForkStage::bpo(BlobFork::Bpo1))), + ), + ForkSpec::BPO1ToBPO2AtTime15k => ForkSchedule::new( + ForkStage::bpo(BlobFork::Bpo1), + Some((AT_15K, ForkStage::bpo(BlobFork::Bpo2))), + ), + ForkSpec::BPO2ToBPO3AtTime15k => ForkSchedule::new( + ForkStage::bpo(BlobFork::Bpo2), + Some((AT_15K, ForkStage::bpo(BlobFork::Bpo3))), + ), + ForkSpec::BPO3ToBPO4AtTime15k => ForkSchedule::new( + ForkStage::bpo(BlobFork::Bpo3), + Some((AT_15K, ForkStage::bpo(BlobFork::Bpo4))), + ), + ForkSpec::BPO2ToAmsterdamAtTime15k => ForkSchedule::new( + ForkStage::bpo(BlobFork::Bpo2), + Some((AT_15K, ForkStage::new(SpecId::AMSTERDAM))), + ), + fixed => match fixed.to_spec_id() { + Some(spec) => ForkSchedule::fixed(spec), + None => return None, + }, + }) +} + #[cfg(test)] mod tests { use super::{ - super::types::{SealEngine, State}, + super::types::{ChainConfig, DecodedBlock, SealEngine, State}, *, }; + use alloy_eips::eip7840::BLOB_BASE_COST; + use std::collections::BTreeMap; + + #[cfg(feature = "jit")] + use crate::tx::recover_address; + #[cfg(feature = "jit")] + use alloy_consensus::{SignableTransaction, TxLegacy, crypto::secp256k1}; + #[cfg(feature = "jit")] + use alloy_primitives::{TxKind, to_eip155_v}; #[cfg(feature = "jit")] use evm2::interpreter::op; - use std::collections::BTreeMap; #[cfg(feature = "jit")] const BYTECODE_STORE42: &[u8] = &[op::PUSH1, 0x42, op::PUSH0, op::SSTORE, op::STOP]; + fn cancun_config() -> ChainConfig { + ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([( + BlobFork::Cancun, + BlobScheduleEntry::from(BlobParams::cancun()), + )]), + } + } + + fn test_case(network: ForkSpec, config: Option) -> BlockchainTestCase { + BlockchainTestCase { + genesis_block_header: BlockHeader::default(), + genesis_rlp: None, + blocks: Vec::new(), + post_state: None, + pre: State(BTreeMap::new()), + block_hashes: Vec::new(), + lastblockhash: B256::ZERO, + network, + seal_engine: SealEngine::NoProof, + config, + } + } + + #[test] + fn blob_params_switch_at_the_transition_fork() { + let config = ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([ + (BlobFork::Bpo1, BlobScheduleEntry::from(BlobParams::bpo1())), + (BlobFork::Bpo2, BlobScheduleEntry::from(BlobParams::bpo2())), + ]), + }; + let case = test_case(ForkSpec::BPO1ToBPO2AtTime15k, Some(config)); + let blob_params = |timestamp| { + let header = BlockHeader { timestamp: U256::from(timestamp), ..Default::default() }; + blob_params(Path::new("test.json"), "test", &case, &header).unwrap().unwrap() + }; + assert_eq!(blob_params(14_999).update_fraction, BlobParams::bpo1().update_fraction); + assert_eq!(blob_params(15_000).update_fraction, BlobParams::bpo2().update_fraction); + } + + #[test] + fn blob_params_carry_the_reserve_price_from_osaka() { + let config = |fork, entry| ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([(fork, BlobScheduleEntry::from(entry))]), + }; + let header = BlockHeader::default(); + let osaka_config = config(BlobFork::Osaka, BlobParams::osaka()); + let osaka = test_case(ForkSpec::Osaka, Some(osaka_config)); + let params = blob_params(Path::new("test.json"), "test", &osaka, &header); + assert_eq!(params.unwrap().unwrap().blob_base_cost, BLOB_BASE_COST); + let prague_config = config(BlobFork::Prague, BlobParams::prague()); + let prague = test_case(ForkSpec::Prague, Some(prague_config)); + let params = blob_params(Path::new("test.json"), "test", &prague, &header); + assert_eq!(params.unwrap().unwrap().blob_base_cost, 0); + } + + #[test] + fn blob_params_require_a_schedule_entry_from_cancun() { + let case = test_case(ForkSpec::Cancun, None); + let err = blob_params(Path::new("test.json"), "test", &case, &BlockHeader::default()) + .unwrap_err(); + assert!(matches!(err.kind, TestErrorKind::MissingBlobSchedule(BlobFork::Cancun))); + } + + #[test] + fn blob_params_reject_a_zero_update_fraction() { + let config = ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([( + BlobFork::Cancun, + BlobScheduleEntry { + target: U256::from(3), + max: U256::from(6), + base_fee_update_fraction: U256::ZERO, + }, + )]), + }; + let case = test_case(ForkSpec::Cancun, Some(config)); + let err = blob_params(Path::new("test.json"), "test", &case, &BlockHeader::default()) + .unwrap_err(); + assert!(matches!(err.kind, TestErrorKind::ZeroBlobUpdateFraction(BlobFork::Cancun))); + } + + #[test] + fn unsupported_fork_fails_instead_of_skipping() { + let suite = BlockchainTest(BTreeMap::from([( + "dao".to_string(), + test_case(ForkSpec::HomesteadToDaoAt5, None), + )])); + let err = execute_suite( + Path::new("dao.json"), + &suite, + ExecuteConfig::default(), + &NameFilter::default(), + &mut NoopHook, + ) + .unwrap_err(); + assert!(matches!(err.kind, TestErrorKind::UnsupportedFork(ForkSpec::HomesteadToDaoAt5))); + } + #[test] fn post_state_rejects_omitted_live_accounts() { let address = Address::with_last_byte(1); @@ -1490,6 +1827,7 @@ mod tests { lastblockhash: B256::ZERO, network: ForkSpec::Paris, seal_engine: SealEngine::NoProof, + config: None, }, )])); let run = |suite: &BlockchainTest, config| { @@ -1505,9 +1843,7 @@ mod tests { assert!(matches!(err.kind, TestErrorKind::UnexpectedFailure(message) if message == format!("unexpected account in post-state: {address}"))); - let summary = - run(&suite, ExecuteConfig { validate_post_state: false, ..Default::default() }) - .unwrap(); + let summary = run(&suite, ExecuteConfig { validate: false, ..Default::default() }).unwrap(); assert_eq!(summary.executed, 1); let case = suite.0.get_mut("omitted-account").unwrap(); @@ -1517,17 +1853,6 @@ mod tests { #[test] fn blockchain_tests_apply_ommer_rewards_before_merge() { - use super::{ - super::types::{ - Account, Block, BlockHeader, BlockchainTest, BlockchainTestCase, DecodedBlock, - ForkSpec, SealEngine, State, - }, - ExecuteConfig, NoopHook, ONE_ETHER, execute_suite, - }; - use crate::filter::NameFilter; - use alloy_primitives::{Address, B256, U256}; - use std::{collections::BTreeMap, path::Path}; - let miner = Address::with_last_byte(1); let ommer_miner = Address::with_last_byte(2); let genesis_hash = B256::with_last_byte(1); @@ -1597,6 +1922,7 @@ mod tests { lastblockhash: block_hash, network: fork, seal_engine: SealEngine::NoProof, + config: None, genesis_rlp: None, }, )])); @@ -1615,16 +1941,6 @@ mod tests { #[test] fn receipt_root_check_rejects_invalid_empty_block_root() { - use super::{ - super::types::{ - Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, SealEngine, State, - }, - ExecuteConfig, ExecutionMode, NoopHook, execute_str, - }; - use crate::filter::NameFilter; - use alloy_primitives::{B256, U256}; - use std::{collections::BTreeMap, path::Path}; - let genesis_hash = B256::with_last_byte(1); let suite = BlockchainTest(BTreeMap::from([( "invalid-receipt-root".to_string(), @@ -1636,11 +1952,14 @@ mod tests { }, blocks: vec![Block { block_header: Some(BlockHeader { + parent_beacon_block_root: Some(B256::ZERO), parent_hash: genesis_hash, hash: B256::with_last_byte(2), number: U256::ONE, gas_limit: U256::from(30_000_000), receipt_trie: B256::ZERO, + excess_blob_gas: Some(U256::ZERO), + blob_gas_used: Some(U256::ZERO), ..Default::default() }), ..Default::default() @@ -1651,6 +1970,7 @@ mod tests { lastblockhash: B256::with_last_byte(2), network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), genesis_rlp: None, }, )])); @@ -1672,17 +1992,31 @@ mod tests { } #[test] - fn rejects_block_with_wrong_parent_hash() { - use super::{ - super::types::{ - Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, SealEngine, State, - }, - ExecuteConfig, NoopHook, execute_str, - }; - use crate::filter::NameFilter; - use alloy_primitives::{B256, U256}; - use std::{collections::BTreeMap, path::Path}; + fn next_excess_blob_gas_matches_blob_params_and_keeps_full_width_base_fees() { + let params = BlobParams::osaka(); + for excess in [0, 786_432, 5_000_000, 249_036_800, 400_000_000] { + for used in [0, 131_072, 786_432] { + for base_fee in [0, 1, 1_000_000_000, u64::MAX] { + assert_eq!( + next_excess_blob_gas(¶ms, excess, used, U256::from(base_fee)), + params.next_block_excess_blob_gas_osaka(excess, used, base_fee), + ); + } + } + } + + // The reserve price, 2^256, exceeds the blob price, so the reserve branch applies. + assert_eq!(next_excess_blob_gas(¶ms, 786_432, 0, U256::ONE << 243), 786_432); + // The blob price exceeds `u128`, and 2^145 times it still exceeds this reserve price, so + // the target subtraction applies. + assert_eq!( + next_excess_blob_gas(¶ms, 1_000_000_000, 786_432, U256::ONE << 140), + 1_000_000_000, + ); + } + #[test] + fn rejects_block_with_wrong_parent_hash() { let genesis_hash = B256::with_last_byte(1); let block_hash = B256::with_last_byte(2); let suite = BlockchainTest(BTreeMap::from([( @@ -1709,6 +2043,7 @@ mod tests { lastblockhash: block_hash, network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), genesis_rlp: None, }, )])); @@ -1731,16 +2066,6 @@ mod tests { #[test] fn discards_expected_block_with_wrong_parent_hash() { - use super::{ - super::types::{ - Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, SealEngine, State, - }, - ExecuteConfig, NoopHook, execute_str, - }; - use crate::filter::NameFilter; - use alloy_primitives::{B256, U256}; - use std::{collections::BTreeMap, path::Path}; - let genesis_hash = B256::with_last_byte(1); let suite = BlockchainTest(BTreeMap::from([( "expected-wrong-parent-hash".to_string(), @@ -1767,6 +2092,7 @@ mod tests { lastblockhash: genesis_hash, network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), genesis_rlp: None, }, )])); @@ -1784,16 +2110,6 @@ mod tests { #[test] fn rejects_case_with_wrong_last_block_hash() { - use super::{ - super::types::{ - Block, BlockHeader, BlockchainTest, BlockchainTestCase, ForkSpec, SealEngine, State, - }, - ExecuteConfig, NoopHook, execute_str, - }; - use crate::filter::NameFilter; - use alloy_primitives::{B256, U256}; - use std::{collections::BTreeMap, path::Path}; - let genesis_hash = B256::with_last_byte(1); let block_hash = B256::with_last_byte(2); let suite = BlockchainTest(BTreeMap::from([( @@ -1806,10 +2122,13 @@ mod tests { }, blocks: vec![Block { block_header: Some(BlockHeader { + parent_beacon_block_root: Some(B256::ZERO), parent_hash: genesis_hash, hash: block_hash, number: U256::ONE, gas_limit: U256::from(30_000_000), + excess_blob_gas: Some(U256::ZERO), + blob_gas_used: Some(U256::ZERO), ..Default::default() }), ..Default::default() @@ -1820,6 +2139,7 @@ mod tests { lastblockhash: genesis_hash, network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), genesis_rlp: None, }, )])); @@ -1842,8 +2162,18 @@ mod tests { #[cfg(feature = "jit")] fn execute_simple_storage_block(mode: ExecutionMode) -> ExecuteSummary { - let caller = Address::from([0x11; 20]); - let target = Address::from([0x22; 20]); + let secret = B256::with_last_byte(1); + let caller = recover_address(secret.as_slice()).unwrap(); + let target = Address::repeat_byte(0x22); + let unsigned = TxLegacy { + chain_id: Some(1), + nonce: 0, + gas_price: 0, + gas_limit: 100_000, + to: TxKind::Call(target), + ..Default::default() + }; + let signature = secp256k1::sign_message(secret, unsigned.signature_hash()).unwrap(); let genesis_hash = B256::with_last_byte(1); let block_hash = B256::with_last_byte(2); let mut pre = BTreeMap::new(); @@ -1860,7 +2190,7 @@ mod tests { target, Account { balance: U256::ZERO, - code: Bytes::copy_from_slice(BYTECODE_STORE42), + code: Bytes::from_static(BYTECODE_STORE42), nonce: U256::ZERO, storage: BTreeMap::new(), }, @@ -1882,7 +2212,7 @@ mod tests { target, Account { balance: U256::ZERO, - code: Bytes::copy_from_slice(BYTECODE_STORE42), + code: Bytes::from_static(BYTECODE_STORE42), nonce: U256::ZERO, storage: target_storage, }, @@ -1901,6 +2231,7 @@ mod tests { genesis_rlp: None, blocks: vec![Block { block_header: Some(BlockHeader { + parent_beacon_block_root: Some(B256::ZERO), parent_hash: genesis_hash, hash: block_hash, number: U256::ONE, @@ -1908,6 +2239,8 @@ mod tests { gas_used: U256::from(43_105), base_fee_per_gas: Some(U256::ZERO), timestamp: U256::ONE, + excess_blob_gas: Some(U256::ZERO), + blob_gas_used: Some(U256::ZERO), ..BlockHeader::default() }), transactions: Some(vec![Transaction { @@ -1917,9 +2250,9 @@ mod tests { gas_limit: U256::from(100_000), gas_price: Some(U256::ZERO), nonce: U256::ZERO, - r: U256::ZERO, - s: U256::ZERO, - v: U256::ZERO, + r: signature.r(), + s: signature.s(), + v: U256::from(to_eip155_v(signature.v() as u8, 1)), value: U256::ZERO, to: Some(target), chain_id: Some(U256::ONE), @@ -1939,6 +2272,7 @@ mod tests { lastblockhash: block_hash, network: ForkSpec::Cancun, seal_engine: SealEngine::NoProof, + config: Some(cancun_config()), }, )])); let input = serde_json::to_string(&suite).unwrap(); @@ -1946,7 +2280,7 @@ mod tests { execute_str( Path::new("simple-storage.json"), &input, - ExecuteConfig { validate_post_state: true, mode, ..ExecuteConfig::default() }, + ExecuteConfig { validate: true, mode, ..ExecuteConfig::default() }, &NameFilter::default(), &mut hook, ) @@ -1969,15 +2303,6 @@ mod tests { #[test] fn json_outcome_reports_pass_and_failure() { - use super::{ - super::{ - error::{TestError, TestErrorKind}, - types::ForkSpec, - }, - json_outcome_value, - }; - use alloy_primitives::B256; - let root = B256::with_last_byte(7); let pass = json_outcome_value("case-a", ForkSpec::Byzantium, &Ok(Some(root))); assert_eq!(pass["pass"], true); @@ -1986,7 +2311,7 @@ mod tests { assert_eq!(pass["errorMsg"], ""); let failure: Result, TestError> = Err(TestError::case( - std::path::Path::new("f.json"), + Path::new("f.json"), "case-b", TestErrorKind::UnexpectedFailure("boom".to_string()), )); diff --git a/crates/eest/src/blockchaintest/mod.rs b/crates/eest/src/blockchaintest/mod.rs index f788bb621..9e6768ca8 100644 --- a/crates/eest/src/blockchaintest/mod.rs +++ b/crates/eest/src/blockchaintest/mod.rs @@ -8,9 +8,10 @@ mod runner; mod types; pub use error::TestError; +pub(crate) use execute::state_stage; pub use execute::{ - ExecuteConfig, ExecuteSummary, ExecutionMode, execute_case_on, execute_str, execute_suite, - pre_state, + ExecuteConfig, ExecuteSummary, ExecutionMode, blob_params, execute_case_on, execute_str, + execute_suite, pre_state, }; pub use hook::{ BlockFailed, BlockFinished, BlockStarted, CaseStarted, Hook, NoopHook, TransactionFailed, @@ -19,6 +20,7 @@ pub use hook::{ pub use runner::run; pub(crate) use runner::{IGNORED_TESTS, suites}; pub use types::{ - Account, Block, BlockHash, BlockHeader, BlockchainTest, BlockchainTestCase, DecodedBlock, - ForkSpec, SealEngine, State, Transaction, Withdrawal, + Account, BlobFork, BlobScheduleEntry, Block, BlockHash, BlockHeader, BlockchainTest, + BlockchainTestCase, ChainConfig, DecodedBlock, ForkSpec, SealEngine, State, Transaction, + Withdrawal, }; diff --git a/crates/eest/src/blockchaintest/runner.rs b/crates/eest/src/blockchaintest/runner.rs index 6acb5b4f5..c742e8e15 100644 --- a/crates/eest/src/blockchaintest/runner.rs +++ b/crates/eest/src/blockchaintest/runner.rs @@ -2,7 +2,7 @@ use super::{ env::blockchain_test_roots, execute::{ExecuteConfig, ExecutionMode, execute_test_suite}, }; -use crate::harness::{TestSuite, run_json_harnesses}; +use crate::harness::{TestSuite, contains_any, run_json_harnesses}; #[cfg(feature = "jit")] use crate::{ blockchaintest::execute::execute_test_suites, execution::CompiledMode, harness::compiled_roots, @@ -127,50 +127,18 @@ fn should_descend(path: &Path) -> bool { } fn should_ignore(name: &str) -> bool { - IGNORED_TESTS.iter().any(|ignored| name.contains(ignored)) + contains_any(name, IGNORED_TESTS) } #[rustfmt::skip] pub(crate) const IGNORED_TESTS: &[&str] = &[ - // Block header/blob-gas validation is consensus-level block validation, not EVM execution. - "cancun/eip4844_blobs/test_blob_type_tx_pre_fork.json", - "cancun/eip4844_blobs/test_invalid_blob_gas_used_in_header.json", - "cancun/eip4844_blobs/test_invalid_block_blob_count.json", - // The same tests were renamed into subdirectories on BAL releases. - "cancun/eip4844_blobs/blob_txs/invalid_block_blob_count.json", - "cancun/eip4844_blobs/excess_blob_gas/invalid_blob_gas_used_in_header.json", - "cancun/eip4844_blobs/test_invalid_excess_blob_gas_above_target_change.json", - "cancun/eip4844_blobs/test_invalid_excess_blob_gas_change.json", - "cancun/eip4844_blobs/test_invalid_excess_blob_gas_target_blobs_increase_from_zero.json", - "cancun/eip4844_blobs/test_invalid_negative_excess_blob_gas.json", - "cancun/eip4844_blobs/test_invalid_non_multiple_excess_blob_gas.json", - "cancun/eip4844_blobs/test_invalid_post_fork_block_without_blob_fields.json", - "cancun/eip4844_blobs/test_invalid_pre_fork_block_with_blob_fields.json", - "cancun/eip4844_blobs/blob_txs/blob_type_tx_pre_fork.json", - "cancun/eip4844_blobs/excess_blob_gas_fork_transition/invalid_post_fork_block_without_blob_fields.json", - "cancun/eip4844_blobs/excess_blob_gas_fork_transition/invalid_pre_fork_block_with_blob_fields.json", - "cancun/eip4844_blobs/test_invalid_static_excess_blob_gas.json", - "cancun/eip4844_blobs/test_invalid_static_excess_blob_gas_from_zero_on_blobs_above_target.json", - "cancun/eip4844_blobs/test_invalid_zero_excess_blob_gas_in_header.json", - // The same excess-blob-gas tests were renamed into this subdirectory on BAL releases. - "cancun/eip4844_blobs/excess_blob_gas/", - - // These validate block RLP encoding or full blob sidecar rejection, which this runner does not decode/validate. + // These validate block RLP structure or full blob sidecar rejection, which this runner does not decode/validate. "cancun/eip4844_blobs/test_invalid_blob_tx_contract_creation.json", "cancun/eip4844_blobs/test_reject_valid_full_blob_in_block_rlp.json", // The same tests were renamed into subdirectories on BAL releases. "cancun/eip4844_blobs/blob_txs/invalid_blob_tx_contract_creation.json", "cancun/eip4844_blobs/blob_txs_full/reject_valid_full_blob_in_block_rlp.json", - // These are block-level blob count / fork transition tests, not transaction execution tests. - "cancun/eip4844_blobs/test_invalid_tx_blob_count.json", - "osaka/eip7594_peerdas/test_invalid_max_blobs_per_tx.json", - "osaka/eip7594_peerdas/test_max_blobs_per_tx_fork_transition.json", - // The same tests were renamed into subdirectories on BAL releases. - "cancun/eip4844_blobs/blob_txs/invalid_tx_blob_count.json", - "osaka/eip7594_peerdas/max_blob_per_tx/invalid_max_blobs_per_tx.json", - "osaka/eip7594_peerdas/max_blob_per_tx/max_blobs_per_tx_fork_transition.json", - // Create-collision fixtures need storage-aware collision handling. "eip7610_create_collision", "InitCollision.json", @@ -184,15 +152,6 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "dynamicAccountOverwriteEmpty.json", "dynamicAccountOverwriteEmpty_Paris.json", - // The harness does not track cumulative block gas allowance or validate Osaka block RLP size limits. - "osaka/eip7825_transaction_gas_limit_cap/test_tx_gas_larger_than_block_gas_limit.json", - "osaka/eip7934_block_rlp_limit/test_block_at_rlp_size_limit_boundary.json", - "osaka/eip7934_block_rlp_limit/test_fork_transition_block_rlp_limit.json", - // The same tests were renamed into subdirectories on BAL releases. - "osaka/eip7825_transaction_gas_limit_cap/tx_gas_limit/tx_gas_larger_than_block_gas_limit.json", - "osaka/eip7934_block_rlp_limit/max_block_rlp_size/block_at_rlp_size_limit_boundary.json", - "osaka/eip7934_block_rlp_limit/max_block_rlp_size/fork_transition_block_rlp_limit.json", - // These validate block header fields/roots and belong to consensus-level block validation. "frontier/validation/header/gas_limit_below_minimum.json", // The same test was renamed with a `block_` prefix on glamsterdam-devnet releases. @@ -204,11 +163,12 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "london/validation/test_invalid_header.json", "shanghai/eip4895_withdrawals/test_withdrawals_root.json", - // Prague request/deposit fixtures validate EL request extraction and system-contract block processing. - "prague/eip6110_deposits", - "prague/eip7002_el_triggerable_withdrawals", - "prague/eip7251_consolidations", - "prague/eip7685_general_purpose_el_requests", + // These check which header fields a block carries on either side of a fork, which is + // consensus-level block validation. + "eip7843_slotnum/fork_transition/invalid_post_fork_block_without_slot_number.json", + "eip7843_slotnum/fork_transition/invalid_pre_fork_block_with_slot_number.json", + "eip7928_block_level_access_lists/fork_transition/invalid_post_fork_block_without_bal_hash_field.json", + "eip7928_block_level_access_lists/fork_transition/invalid_pre_fork_block_with_bal_hash_field.json", // This fixture has a block gas limit below the transaction intrinsic gas and belongs to block validation. "static/state_tests/stEIP1559/lowGasLimit.json", diff --git a/crates/eest/src/blockchaintest/types.rs b/crates/eest/src/blockchaintest/types.rs index 0bd17bd7c..e1b05b84b 100644 --- a/crates/eest/src/blockchaintest/types.rs +++ b/crates/eest/src/blockchaintest/types.rs @@ -1,8 +1,11 @@ -#![allow(dead_code)] - use crate::tx::{AccessListItem, TestAuthorization}; use alloy_eip7928::BlockAccessList; +use alloy_eips::{ + eip4844::BLOB_TX_MIN_BLOB_GASPRICE, + eip7840::{BLOB_BASE_COST, BlobParams}, +}; use alloy_primitives::{Address, B256, Bytes, FixedBytes, TxKind, U256}; +use evm2::SpecId; use serde::{Deserialize, Deserializer, Serialize, de}; use std::collections::BTreeMap; @@ -39,6 +42,9 @@ pub struct BlockchainTestCase { /// Seal engine type. #[serde(default)] pub seal_engine: SealEngine, + /// Chain configuration the test ran under. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub config: Option, } /// Historical block hash entry. @@ -311,6 +317,12 @@ pub enum ForkSpec { PragueToOsakaAtTime15k, /// Osaka. Osaka, + /// BPO1 (Osaka with the first blob parameters only fork). + #[serde(rename = "BPO1")] + BPO1, + /// BPO2 (Osaka with the second blob parameters only fork). + #[serde(rename = "BPO2")] + BPO2, /// Osaka to BPO1 transition. OsakaToBPO1AtTime15k, /// BPO1 to BPO2 transition. @@ -326,27 +338,87 @@ pub enum ForkSpec { } impl ForkSpec { - /// Returns true if this fork name represents a transition fork. - pub(crate) const fn is_transition(self) -> bool { - matches!( - self, - Self::FrontierToHomesteadAt5 - | Self::HomesteadToDaoAt5 - | Self::HomesteadToEIP150At5 - | Self::EIP158ToByzantiumAt5 - | Self::ByzantiumToConstantinopleAt5 - | Self::ByzantiumToConstantinopleFixAt5 - | Self::BerlinToLondonAt5 - | Self::ParisToShanghaiAtTime15k - | Self::ShanghaiToCancunAtTime15k - | Self::CancunToPragueAtTime15k - | Self::PragueToOsakaAtTime15k - | Self::OsakaToBPO1AtTime15k - | Self::BPO1ToBPO2AtTime15k - | Self::BPO2ToBPO3AtTime15k - | Self::BPO3ToBPO4AtTime15k - | Self::BPO2ToAmsterdamAtTime15k - ) + /// Returns the spec ID a fork runs under, or `None` for a transition fork, which runs under + /// several specs, and for the forks no runner supports. + /// + /// evm2 has no `CONSTANTINOPLE` spec. Constantinople differs from Petersburg by EIP-1283 net + /// gas metering for `SSTORE`, so running it under Petersburg would charge the wrong gas. The + /// Merge feature forks enable single EIPs on top of Merge, which no spec ID expresses, and the + /// DAO fork applies an irregular state change. + pub const fn to_spec_id(self) -> Option { + Some(match self { + Self::Frontier => SpecId::FRONTIER, + Self::Homestead => SpecId::HOMESTEAD, + Self::EIP150 => SpecId::TANGERINE, + Self::EIP158 => SpecId::SPURIOUS_DRAGON, + Self::Byzantium => SpecId::BYZANTIUM, + Self::ConstantinopleFix => SpecId::PETERSBURG, + Self::Istanbul => SpecId::ISTANBUL, + Self::Berlin => SpecId::BERLIN, + Self::London => SpecId::LONDON, + Self::Paris => SpecId::MERGE, + Self::Shanghai => SpecId::SHANGHAI, + Self::Cancun => SpecId::CANCUN, + Self::Prague => SpecId::PRAGUE, + Self::Osaka | Self::BPO1 | Self::BPO2 => SpecId::OSAKA, + Self::Amsterdam => SpecId::AMSTERDAM, + Self::Constantinople + | Self::MergeEOF + | Self::MergeMeterInitCode + | Self::MergePush0 + | Self::FrontierToHomesteadAt5 + | Self::HomesteadToDaoAt5 + | Self::HomesteadToEIP150At5 + | Self::EIP158ToByzantiumAt5 + | Self::ByzantiumToConstantinopleAt5 + | Self::ByzantiumToConstantinopleFixAt5 + | Self::BerlinToLondonAt5 + | Self::ParisToShanghaiAtTime15k + | Self::ShanghaiToCancunAtTime15k + | Self::CancunToPragueAtTime15k + | Self::PragueToOsakaAtTime15k + | Self::OsakaToBPO1AtTime15k + | Self::BPO1ToBPO2AtTime15k + | Self::BPO2ToBPO3AtTime15k + | Self::BPO3ToBPO4AtTime15k + | Self::BPO2ToAmsterdamAtTime15k => return None, + }) + } + + /// Returns the spec ID a state test runs a fork under, or `None` for an unsupported fork. + /// + /// A state test runs one transaction under one spec, so a transition fork maps to the spec + /// after the transition. The blockchain runner instead runs both specs of a transition. + pub fn to_state_spec_id(self) -> Option { + super::execute::state_stage(self).map(|stage| stage.spec) + } + + /// Returns the fork that runs under `spec`, or `None` if no fork does. + /// + /// Osaka maps to the BPO fork that sets `blob_fork`, which only changes the blob parameters. + pub const fn from_spec_id(spec: SpecId, blob_fork: Option) -> Option { + Some(match spec { + SpecId::FRONTIER => Self::Frontier, + SpecId::HOMESTEAD => Self::Homestead, + SpecId::TANGERINE => Self::EIP150, + SpecId::SPURIOUS_DRAGON => Self::EIP158, + SpecId::BYZANTIUM => Self::Byzantium, + SpecId::PETERSBURG => Self::ConstantinopleFix, + SpecId::ISTANBUL => Self::Istanbul, + SpecId::BERLIN => Self::Berlin, + SpecId::LONDON => Self::London, + SpecId::MERGE => Self::Paris, + SpecId::SHANGHAI => Self::Shanghai, + SpecId::CANCUN => Self::Cancun, + SpecId::PRAGUE => Self::Prague, + SpecId::OSAKA => match blob_fork { + Some(BlobFork::Bpo1) => Self::BPO1, + Some(BlobFork::Bpo2) => Self::BPO2, + _ => Self::Osaka, + }, + SpecId::AMSTERDAM => Self::Amsterdam, + _ => return None, + }) } } @@ -360,6 +432,69 @@ pub enum SealEngine { Ethash, } +/// Chain configuration of a blockchain test case. +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ChainConfig { + /// Chain ID. + pub chainid: U256, + /// Blob parameters of every fork up to the last fork the test runs. + #[serde(default)] + pub blob_schedule: BTreeMap, +} + +/// A fork that sets the blob parameters. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)] +pub enum BlobFork { + /// Cancun. + Cancun, + /// Prague. + Prague, + /// Osaka. + Osaka, + /// BPO1, a fork that only changes the blob parameters. + #[serde(rename = "BPO1")] + Bpo1, + /// BPO2, a fork that only changes the blob parameters. + #[serde(rename = "BPO2")] + Bpo2, + /// BPO3, a fork that only changes the blob parameters. + #[serde(rename = "BPO3")] + Bpo3, + /// BPO4, a fork that only changes the blob parameters. + #[serde(rename = "BPO4")] + Bpo4, + /// Amsterdam. + Amsterdam, +} + +impl BlobFork { + /// Returns the mainnet blob parameters of this fork, or `None` for BPO3 and BPO4, which + /// mainnet has not scheduled. + pub const fn mainnet_params(self) -> Option { + Some(match self { + Self::Cancun => BlobParams::cancun(), + Self::Prague => BlobParams::prague(), + Self::Osaka => BlobParams::osaka(), + Self::Bpo1 => BlobParams::bpo1(), + Self::Bpo2 | Self::Amsterdam => BlobParams::bpo2(), + Self::Bpo3 | Self::Bpo4 => return None, + }) + } +} + +/// Blob parameters of one fork. +#[derive(Clone, Copy, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct BlobScheduleEntry { + /// Target blobs per block. + pub target: U256, + /// Maximum blobs per block. + pub max: U256, + /// Blob base fee update fraction. + pub base_fee_update_fraction: U256, +} + /// Converts an optional address field that may be encoded as an empty string. fn deserialize_maybe_empty<'de, D>(deserializer: D) -> Result, D::Error> where @@ -382,6 +517,33 @@ fn is_empty_bytes(bytes: &Bytes) -> bool { bytes.as_ref().is_empty() } +impl BlobScheduleEntry { + /// Returns the blob parameters of this entry under the spec that is active. + /// + /// The reserve price of EIP-7918 comes from the spec, not from the fixture's schedule. + pub fn params(&self, spec: SpecId) -> BlobParams { + let max_blob_count = self.max.saturating_to(); + BlobParams { + target_blob_count: self.target.saturating_to(), + max_blob_count, + update_fraction: self.base_fee_update_fraction.saturating_to(), + min_blob_fee: BLOB_TX_MIN_BLOB_GASPRICE, + max_blobs_per_tx: max_blob_count, + blob_base_cost: if spec.enables(SpecId::OSAKA) { BLOB_BASE_COST } else { 0 }, + } + } +} + +impl From for BlobScheduleEntry { + fn from(params: BlobParams) -> Self { + Self { + target: U256::from(params.target_blob_count), + max: U256::from(params.max_blob_count), + base_fee_update_fraction: U256::from(params.update_fraction), + } + } +} + impl Transaction { /// Returns this transaction's EIP-2718 type byte. pub(crate) fn tx_type(&self) -> u8 { @@ -393,3 +555,27 @@ impl Transaction { self.to.map_or(TxKind::Create, TxKind::Call) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_spec_id_maps_to_a_fork_that_runs_under_it() { + for value in 0..SpecId::COUNT as u32 { + let spec = SpecId::try_from_u32(value).unwrap(); + let fork = ForkSpec::from_spec_id(spec, None).unwrap(); + assert_eq!(fork.to_spec_id(), Some(spec)); + } + assert_eq!( + ForkSpec::from_spec_id(SpecId::OSAKA, Some(BlobFork::Bpo2)), + Some(ForkSpec::BPO2) + ); + assert_eq!(ForkSpec::BPO2ToAmsterdamAtTime15k.to_spec_id(), None); + assert_eq!(ForkSpec::Constantinople.to_spec_id(), None); + assert_eq!(ForkSpec::Constantinople.to_state_spec_id(), None); + assert_eq!(ForkSpec::MergePush0.to_state_spec_id(), None); + assert_eq!(ForkSpec::BPO1.to_state_spec_id(), Some(SpecId::OSAKA)); + assert_eq!(ForkSpec::BerlinToLondonAt5.to_state_spec_id(), Some(SpecId::LONDON)); + } +} diff --git a/crates/eest/src/compiled.rs b/crates/eest/src/compiled.rs index c9c158d0a..a08f0c731 100644 --- a/crates/eest/src/compiled.rs +++ b/crates/eest/src/compiled.rs @@ -1,4 +1,4 @@ -use crate::execution::ExecutionResources; +use crate::{execution::ExecutionResources, fixtures::env_flag}; use evm2::evm::DbStatsCounts; use std::{ panic::{self, AssertUnwindSafe}, @@ -11,6 +11,9 @@ use std::{ time::{Duration, Instant}, }; +/// Environment variable forcing compiled EEST runs onto a single worker thread. +const SINGLE_THREAD_ENV: &str = "EVM2_EEST_SINGLE_THREAD"; + const WORKER_STACK_SIZE: usize = 16 * 1024 * 1024; #[derive(Clone, Copy, Debug, Default)] @@ -43,7 +46,7 @@ where return Ok(FileSummary::default()); } - let num_threads = if std::env::var_os("SINGLE_THREAD").is_some() { + let num_threads = if env_flag(SINGLE_THREAD_ENV) { 1 } else { thread::available_parallelism().map_or(1, |n| n.get().min(n_files)) diff --git a/crates/eest/src/discover.rs b/crates/eest/src/discover.rs index 5503a0ae0..8a0d90c8e 100644 --- a/crates/eest/src/discover.rs +++ b/crates/eest/src/discover.rs @@ -1,3 +1,4 @@ +use crate::fixture_io::has_extension; use std::path::{Path, PathBuf}; use walkdir::{DirEntry, WalkDir}; @@ -47,6 +48,5 @@ fn should_walk_entry(entry: &DirEntry, should_descend: fn(&Path) -> bool) -> boo } fn is_json_test(path: &Path) -> bool { - path.file_name().is_none_or(|name| name != "index.json") - && path.extension().is_some_and(|ext| ext == "json") + path.file_name().is_none_or(|name| name != "index.json") && has_extension(path, "json") } diff --git a/crates/eest/src/dump.rs b/crates/eest/src/dump.rs index ece4eb4b4..16a04c6d9 100644 --- a/crates/eest/src/dump.rs +++ b/crates/eest/src/dump.rs @@ -67,7 +67,7 @@ mod tests { #[test] fn dumps_accounts_and_non_zero_storage() { let mut db = InMemoryDB::default(); - let address = Address::from([0x11; 20]); + let address = Address::repeat_byte(0x11); let mut info = AccountInfo::default().with_code(parse_bytecode(Bytes::new())); info.balance = U256::from(1_000u64); info.nonce = 7; @@ -75,7 +75,7 @@ mod tests { &mut db, address, info, - [(U256::from(1u64), U256::from(42u64)), (U256::from(2u64), U256::ZERO)], + [(U256::ONE, U256::from(42u64)), (U256::from(2u64), U256::ZERO)], ); let mut buf = Vec::new(); diff --git a/crates/eest/src/env.rs b/crates/eest/src/env.rs index eea444fa2..0fbe1adbb 100644 --- a/crates/eest/src/env.rs +++ b/crates/eest/src/env.rs @@ -1,37 +1,25 @@ use crate::{ blockchaintest::env::explicit_blockchain_test_root_from_env, fixtures::{ - DEVNET_ONLY_ENV, EEST_STABLE_ENV, apply_subdir, apply_subdir_to_roots, env_flag, - fixtures_root, workspace_relative, workspace_root, + EEST_ONLY_ENV, EEST_STABLE_ENV, apply_subdir_to_roots, env_flag, explicit_root_from_env, + fixtures_root, }, harness::TestRoot, }; -use std::{ - env, - path::{Path, PathBuf}, -}; +use std::path::PathBuf; /// Environment variable for the state test root. pub(crate) const STATE_TEST_ROOT_ENV: &str = "EVM2_STATETEST_ROOT"; -/// Environment variable for selecting stable EEST fixtures instead of develop. -pub(crate) const STATETEST_STABLE_ENV: &str = "EVM2_STATETEST_STABLE"; - /// Optional environment variable for selecting a subdirectory under the test root. pub(crate) const STATE_TEST_SUBDIR_ENV: &str = "SUBDIR"; -/// Optional repo-relative ethereum/tests checkout path. -pub(crate) const DEFAULT_ETHEREUM_TESTS_PATH: &str = "tests/ethereum-tests"; - /// A named state-test root. pub(crate) type StateTestRoot = TestRoot; /// Returns the explicit state-test root configured through environment variables. pub(crate) fn explicit_state_test_root_from_env() -> Option { - env::var_os(STATE_TEST_ROOT_ENV).map(PathBuf::from).map(workspace_relative).map(|mut x| { - apply_subdir(&mut x, STATE_TEST_SUBDIR_ENV); - x - }) + explicit_root_from_env(STATE_TEST_ROOT_ENV, STATE_TEST_SUBDIR_ENV) } /// Returns the state-test roots to run by default. @@ -58,16 +46,15 @@ pub(crate) fn state_test_roots() -> Vec { /// exist. pub(crate) fn default_state_test_roots() -> Vec { let fixtures = fixtures_root(); - let ethereum_tests = workspace_root().join(DEFAULT_ETHEREUM_TESTS_PATH); - let eest_path = if env_flag(STATETEST_STABLE_ENV) || env_flag(EEST_STABLE_ENV) { + let eest_path = if env_flag(EEST_STABLE_ENV) { fixtures.join("main/stable/state_tests") } else { fixtures.join("main/develop/state_tests") }; let mut roots = vec![StateTestRoot { - name: "statetests::devnet".to_string(), - label: "execution-spec-tests devnet", - path: fixtures.join("devnet/state_tests"), + name: "statetests::eest".to_string(), + label: "execution-specs release", + path: fixtures.join("eest/state_tests"), }]; if eest_path.is_dir() { @@ -78,7 +65,7 @@ pub(crate) fn default_state_test_roots() -> Vec { }); } - if !env_flag(DEVNET_ONLY_ENV) { + if !env_flag(EEST_ONLY_ENV) { roots.push(StateTestRoot { name: "legacy::cancun".to_string(), label: "legacy Cancun", @@ -91,26 +78,6 @@ pub(crate) fn default_state_test_roots() -> Vec { }); } - if let Some(path) = general_state_tests_path(ðereum_tests) { - roots.push(StateTestRoot { - name: "legacy::ethereum_tests".to_string(), - label: "ethereum/tests GeneralStateTests", - path, - }); - } - apply_subdir_to_roots(&mut roots, STATE_TEST_SUBDIR_ENV); roots } - -fn general_state_tests_path(root: &Path) -> Option { - let sibling = root.parent().map(|parent| parent.join("GeneralStateTests")); - if let Some(path) = sibling - && path.is_dir() - { - return Some(path); - } - - let path = root.join("GeneralStateTests"); - path.is_dir().then_some(path) -} diff --git a/crates/eest/src/error.rs b/crates/eest/src/error.rs index b12759829..583c61a37 100644 --- a/crates/eest/src/error.rs +++ b/crates/eest/src/error.rs @@ -1,4 +1,8 @@ -use crate::{execution::ExecutionResourceError, tx::TxBuildError}; +use crate::{ + blockchaintest::{BlobFork, ForkSpec}, + execution::ExecutionResourceError, + tx::TxBuildError, +}; use alloy_primitives::{B256, Bytes}; use std::{io, path::PathBuf}; use thiserror::Error; @@ -18,7 +22,7 @@ pub struct TestError { impl TestError { /// Creates an error for an unknown test name. pub(crate) fn unknown(path: impl Into, kind: TestErrorKind) -> Self { - Self { path: path.into().display().to_string(), name: "Unknown".to_string(), kind } + Self::case(path, "Unknown", kind) } /// Creates an error for a named test case. @@ -78,12 +82,24 @@ pub(crate) enum TestErrorKind { /// Numeric value overflowed the target type. #[error("value overflows {0}")] Overflow(&'static str), + /// The runner does not support the test's fork. + #[error("unsupported fork: {0:?}")] + UnsupportedFork(ForkSpec), + /// The test's chain configuration has no blob parameters for the fork. + #[error("blob schedule has no entry for {0:?}")] + MissingBlobSchedule(BlobFork), + /// The test's blob schedule has a zero base fee update fraction for the fork. + #[error("blob schedule entry for {0:?} has a zero base fee update fraction")] + ZeroBlobUpdateFraction(BlobFork), /// Transaction part index was invalid. #[error("bad transaction index: {0}")] BadIndex(&'static str), /// Transaction request could not be converted to a consensus transaction. #[error("could not build consensus transaction: {0}")] BuildTransaction(String), + /// Signed transaction could not be decoded or its sender recovered. + #[error("{0}")] + InvalidSignedTransaction(String), /// Execution resource initialization or compilation failed. #[error(transparent)] ExecutionResource(#[from] ExecutionResourceError), @@ -95,6 +111,10 @@ impl From for TestErrorKind { TxBuildError::Overflow(name) => Self::Overflow(name), TxBuildError::BuildTransaction(error) => Self::BuildTransaction(error), TxBuildError::SerdeDeserialize(error) => Self::SerdeDeserialize(error), + error @ (TxBuildError::Decode(_) + | TxBuildError::InvalidV(_) + | TxBuildError::ProtectedLegacy + | TxBuildError::Recover(_)) => Self::InvalidSignedTransaction(error.to_string()), } } } diff --git a/crates/eest/src/execute.rs b/crates/eest/src/execute.rs index ff3f136e3..76c3f914f 100644 --- a/crates/eest/src/execute.rs +++ b/crates/eest/src/execute.rs @@ -1,20 +1,22 @@ #[cfg(feature = "jit")] use crate::compiled::{self, FileSummary}; use crate::{ + blockchaintest::{BlobFork, BlobScheduleEntry, ChainConfig, ForkSpec, state_stage}, dump, error::{TestError, TestErrorKind}, execution::ExecutionResources, filter::NameFilter, fixture_io, forks::is_fork_skipped, - state::{ - insert_account_with_storage, parse_bytecode, storage_for_root, system_contract_has_code, - }, + state::{insert_account_with_storage, parse_bytecode, storage_for_root}, trace::{self, Eip3155Tracer}, - tx::{TxFields, build_recovered_tx, recover_address, rpc_access_list, signed_authorizations}, + tx::{ + TxFields, TxSigner, build_recovered_tx, decode_recovered_tx, recover_address, + rpc_access_list, signed_authorizations, + }, types::{AccountInfo, Env, Test, TestSuite, TestUnit, TransactionParts, TxPartIndices}, }; -use alloy_eips::{eip4844, eip7691}; +use alloy_eips::eip7840::BlobParams; use alloy_primitives::{Address, B256, Bytes, Log, TxKind, U256, keccak256}; use alloy_rpc_types_eth::AccessList as RpcAccessList; use alloy_trie::{ @@ -195,16 +197,28 @@ fn execute_unit( db_stats_counts: &mut DbStatsCounts, ) -> Result<(), TestError> { let state = parse_state(&unit.pre).map_err(|err| TestError::case(path, name, err))?; - for (spec_name, posts) in &unit.post { - let Some(spec) = spec_name.to_spec_id() else { - continue; + for (&fork, posts) in &unit.post { + let Some(stage) = state_stage(fork) else { + // Legacy fixtures run each test under Constantinople and Petersburg, and evm2 only + // supports Petersburg. + if fork == ForkSpec::Constantinople { + continue; + } + return Err(TestError::case(path, name, TestErrorKind::UnsupportedFork(fork))); }; + let spec = stage.spec; if is_fork_skipped(spec) { continue; } - let block = parse_block(&unit.env, spec); + let blob_params = blob_params(stage.blob_fork, spec, unit.config.as_ref()) + .map_err(|err| TestError::case(path, name, err))?; + let block = parse_block(&unit.env, blob_params.as_ref()); for post in posts { - let tx = match build_tx(&unit.transaction, &post.indexes, unit.env.current_chain_id) { + let tx = match &post.txbytes { + Some(bytes) => decode_recovered_tx(bytes, spec).map_err(Into::into), + None => build_tx(&unit.transaction, &post.indexes, unit.env.current_chain_id), + }; + let tx = match tx { Ok(tx) => tx, Err(_) if post.expect_exception.is_some() => { continue; @@ -412,7 +426,7 @@ fn execute_spec( Ok(outcome) } -fn print_db_stats(counts: DbStatsCounts) { +pub(crate) fn print_db_stats(counts: DbStatsCounts) { let style = db_stats_style(); eprintln!("{style}db stats{style:#}: get_account={}", counts.get_account); eprintln!("{style}db stats{style:#}: get_code_by_hash={}", counts.get_code_by_hash); @@ -468,25 +482,13 @@ fn pre_block_system_calls( if spec.enables(SpecId::CANCUN) && let Some(beacon_root) = env.current_beacon_root - && system_contract_has_code(post, BEACON_ROOTS_ADDRESS) { - commit_system_call( - evm, - post, - BEACON_ROOTS_ADDRESS, - Bytes::copy_from_slice(beacon_root.as_slice()), - ); + commit_system_call(evm, post, BEACON_ROOTS_ADDRESS, Bytes::from(beacon_root)); } if spec.enables(SpecId::PRAGUE) && let Some(previous_hash) = env.previous_hash - && system_contract_has_code(post, HISTORY_STORAGE_ADDRESS) { - commit_system_call( - evm, - post, - HISTORY_STORAGE_ADDRESS, - Bytes::copy_from_slice(previous_hash.as_slice()), - ); + commit_system_call(evm, post, HISTORY_STORAGE_ADDRESS, Bytes::from(previous_hash)); } } @@ -497,7 +499,7 @@ fn commit_system_call( data: Bytes, ) { let executed = evm.system_call(SystemTx::new(address, data)).expect("system call errored"); - assert!(executed.result().status, "pre-block system call failed: {address}"); + assert!(executed.result().is_success(), "pre-block system call failed: {address}"); let Ok(_) = executed.commit_with(post); } @@ -541,7 +543,7 @@ fn parse_state(pre: &BTreeMap) -> Result BlockEnv { +fn parse_block(env: &Env, blob_params: Option<&BlobParams>) -> BlockEnv { BlockEnvExt { number: env.current_number, beneficiary: env.current_coinbase, @@ -549,37 +551,39 @@ fn parse_block(env: &Env, spec: SpecId) -> BlockEnv { gas_limit: env.current_gas_limit, basefee: env.current_base_fee.unwrap_or_default(), difficulty: env.current_difficulty, - prevrandao: env - .current_random - .map_or(U256::ZERO, |random| U256::from_be_slice(random.as_slice())), + prevrandao: env.current_random.map_or(U256::ZERO, |random| random.into()), blob_basefee: env .current_excess_blob_gas - .map_or(U256::ONE, |excess| U256::from(blob_basefee(excess, spec))), + .zip(blob_params) + .map_or(U256::ONE, |(excess, params)| { + U256::from(params.calc_blob_fee(excess.saturating_to())) + }), slot_num: env.slot_number.unwrap_or_default(), ext: (), _non_exhaustive: (), } } -fn blob_basefee(excess_blob_gas: U256, spec: SpecId) -> u128 { - let excess_blob_gas = excess_blob_gas.saturating_to::(); - // EIP-4844 defines blob base fee with fake exponential; EIP-7691 changes the - // update fraction from Prague, and the Amsterdam (BPO2) blob schedule changes it again. - if spec.enables(SpecId::AMSTERDAM) { - eip4844::fake_exponential( - eip4844::BLOB_TX_MIN_BLOB_GASPRICE, - excess_blob_gas as u128, - evm2::constants::BLOB_BASE_FEE_UPDATE_FRACTION_AMSTERDAM as u128, - ) - } else if spec.enables(SpecId::PRAGUE) { - eip7691::calc_blob_gasprice(excess_blob_gas) - } else { - eip4844::fake_exponential( - eip4844::BLOB_TX_MIN_BLOB_GASPRICE, - excess_blob_gas as u128, - eip4844::BLOB_GASPRICE_UPDATE_FRACTION, +/// Returns the blob parameters of the fork that sets them, or `None` before Cancun. +/// +/// They come from the test's `config.blobSchedule`. Fixtures without a configuration use the +/// mainnet parameters. +fn blob_params( + blob_fork: Option, + spec: SpecId, + config: Option<&ChainConfig>, +) -> Result, TestErrorKind> { + let Some(blob_fork) = blob_fork else { return Ok(None) }; + let entry = config + .map_or_else( + || blob_fork.mainnet_params().map(BlobScheduleEntry::from), + |config| config.blob_schedule.get(&blob_fork).copied(), ) + .ok_or(TestErrorKind::MissingBlobSchedule(blob_fork))?; + if entry.base_fee_update_fraction.is_zero() { + return Err(TestErrorKind::ZeroBlobUpdateFraction(blob_fork)); } + Ok(Some(entry.params(spec))) } fn build_tx( @@ -602,23 +606,25 @@ fn build_tx( let value = *raw.value.get(indexes.value).ok_or(TestErrorKind::BadIndex("value"))?; let nonce = raw.nonce.try_into().map_err(|_| TestErrorKind::Overflow("nonce"))?; - Ok(build_recovered_tx(TxFields { - tx_type: raw.tx_type, - caller, - kind: TxKind::from(raw.to), - data, - gas_limit, - nonce, - value, - chain_id: raw.chain_id.or(chain_id), - gas_price: raw.gas_price, - max_fee_per_gas: raw.max_fee_per_gas, - max_priority_fee_per_gas: raw.max_priority_fee_per_gas, - access_list: access_list(raw, indexes.data)?, - authorization_list: signed_authorizations(raw.authorization_list.as_deref())?, - blob_versioned_hashes: raw.blob_versioned_hashes.clone(), - max_fee_per_blob_gas: raw.max_fee_per_blob_gas, - })?) + Ok(build_recovered_tx( + TxFields { + tx_type: raw.tx_type, + kind: TxKind::from(raw.to), + data, + gas_limit, + nonce, + value, + chain_id: raw.chain_id.or(chain_id), + gas_price: raw.gas_price, + max_fee_per_gas: raw.max_fee_per_gas, + max_priority_fee_per_gas: raw.max_priority_fee_per_gas, + access_list: access_list(raw, indexes.data)?, + authorization_list: signed_authorizations(raw.authorization_list.as_deref())?, + blob_versioned_hashes: raw.blob_versioned_hashes.clone(), + max_fee_per_blob_gas: raw.max_fee_per_blob_gas, + }, + TxSigner::Fixture(caller), + )?) } fn access_list( @@ -637,7 +643,6 @@ fn access_list( #[cfg(test)] mod tests { use super::*; - use alloy_primitives::LogData; use evm2::interpreter::op; #[cfg(feature = "jit")] @@ -674,6 +679,24 @@ mod tests { ]) } + #[test] + fn bpo_state_tests_use_the_blob_parameters_of_their_fork() { + let config = ChainConfig { + chainid: U256::ONE, + blob_schedule: BTreeMap::from([ + (BlobFork::Osaka, BlobParams::osaka().into()), + (BlobFork::Bpo1, BlobParams::bpo1().into()), + ]), + }; + let stage = state_stage(ForkSpec::BPO1).unwrap(); + let params = blob_params(stage.blob_fork, stage.spec, Some(&config)).unwrap().unwrap(); + assert_eq!(params.update_fraction, BlobParams::bpo1().update_fraction); + assert!(matches!( + blob_params(Some(BlobFork::Bpo2), stage.spec, Some(&config)), + Err(TestErrorKind::MissingBlobSchedule(BlobFork::Bpo2)) + )); + } + #[test] fn logs_hash_matches_empty_logs() { assert_eq!(logs_hash(&[]), keccak256([alloy_rlp::EMPTY_LIST_CODE])); @@ -681,23 +704,24 @@ mod tests { #[test] fn logs_hash_hashes_logs() { - let log = Log { - address: Address::from([0x22; 20]), - data: LogData::new_unchecked(vec![B256::with_last_byte(1)], Bytes::from_static(&[2])), - }; + let log = Log::new_unchecked( + Address::repeat_byte(0x22), + vec![B256::with_last_byte(1)], + Bytes::from_static(&[2]), + ); assert_ne!(logs_hash(&[log]), B256::ZERO); } #[test] fn build_tx_builds_legacy_transaction() { - let caller = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0x11); let raw = TransactionParts { data: vec![Bytes::from_static(&[0xaa])], gas_limit: vec![U256::from(21_000)], gas_price: Some(U256::from(7)), sender: Some(caller), - to: Some(Address::from([0x22; 20])), + to: Some(Address::repeat_byte(0x22)), value: vec![U256::from(3)], ..TransactionParts::default() }; @@ -713,15 +737,15 @@ mod tests { #[test] fn build_tx_builds_eip2930_transaction() { - let caller = Address::from([0x11; 20]); - let access_address = Address::from([0x33; 20]); + let caller = Address::repeat_byte(0x11); + let access_address = Address::repeat_byte(0x33); let raw = TransactionParts { tx_type: Some(1), data: vec![Bytes::new()], gas_limit: vec![U256::from(25_300)], gas_price: Some(U256::from(7)), sender: Some(caller), - to: Some(Address::from([0x22; 20])), + to: Some(Address::repeat_byte(0x22)), value: vec![U256::ZERO], access_lists: vec![Some(vec![crate::tx::AccessListItem { address: access_address, @@ -741,16 +765,16 @@ mod tests { #[test] fn build_tx_uses_indexed_access_list() { - let caller = Address::from([0x11; 20]); - let first_address = Address::from([0x33; 20]); - let second_address = Address::from([0x44; 20]); + let caller = Address::repeat_byte(0x11); + let first_address = Address::repeat_byte(0x33); + let second_address = Address::repeat_byte(0x44); let raw = TransactionParts { tx_type: Some(1), data: vec![Bytes::new(), Bytes::from_static(&[0xaa])], gas_limit: vec![U256::from(25_300)], gas_price: Some(U256::from(7)), sender: Some(caller), - to: Some(Address::from([0x22; 20])), + to: Some(Address::repeat_byte(0x22)), value: vec![U256::ZERO], access_lists: vec![ Some(vec![crate::tx::AccessListItem { @@ -775,8 +799,8 @@ mod tests { #[cfg(feature = "jit")] fn execute_simple_call(mode: ExecutionMode) -> SpecOutcome { - let caller = Address::from([0x11; 20]); - let target = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x22); let env = Env { current_chain_id: Some(U256::ONE), current_coinbase: Address::ZERO, @@ -806,7 +830,7 @@ mod tests { target, AccountInfo { balance: U256::ZERO, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), nonce: 0, storage: BTreeMap::new(), }, @@ -828,7 +852,7 @@ mod tests { let resources = ExecutionResources::new(mode).unwrap(); execute_spec( SpecId::CANCUN, - parse_block(&env, SpecId::CANCUN), + parse_block(&env, Some(&BlobParams::cancun())), parse_state(&pre).unwrap(), &tx, &env, @@ -842,9 +866,9 @@ mod tests { #[cfg(feature = "jit")] fn execute_amsterdam_target(mode: ExecutionMode, target_code: Bytes) -> SpecOutcome { - let caller = Address::from([0x11; 20]); - let target = Address::from([0x22; 20]); - let coinbase = Address::from([0xcb; 20]); + let caller = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x22); + let coinbase = Address::repeat_byte(0xcb); let env = Env { current_chain_id: Some(U256::ONE), current_coinbase: coinbase, @@ -896,7 +920,7 @@ mod tests { let resources = ExecutionResources::new(mode).unwrap(); execute_spec( SpecId::AMSTERDAM, - parse_block(&env, SpecId::AMSTERDAM), + parse_block(&env, Some(&BlobParams::bpo2())), parse_state(&pre).unwrap(), &tx, &env, @@ -910,7 +934,7 @@ mod tests { #[cfg(feature = "jit")] fn execute_amsterdam_value_call_to_empty_coinbase(mode: ExecutionMode) -> SpecOutcome { - execute_amsterdam_target(mode, call_value_to(Address::from([0xcb; 20]))) + execute_amsterdam_target(mode, call_value_to(Address::repeat_byte(0xcb))) } #[cfg(feature = "jit")] diff --git a/crates/eest/src/execution.rs b/crates/eest/src/execution.rs index d418eebde..4941bb772 100644 --- a/crates/eest/src/execution.rs +++ b/crates/eest/src/execution.rs @@ -87,17 +87,19 @@ impl ExecutionResources { } } - /// Fails if a compilation failed, since the runtime then silently runs the interpreter and - /// the test would pass without exercising compiled code. + /// Fails if a compilation failed or a frame fell back to the interpreter, since the test + /// would then pass without exercising compiled code. #[cfg(feature = "jit")] pub(crate) fn check_compilations(&self) -> Result<(), ExecutionResourceError> { - let failed = self.backend.as_ref().map_or(0, |backend| backend.stats().compilations_failed); - if failed == 0 { + let Some(backend) = &self.backend else { return Ok(()) }; + let stats = backend.stats(); + let (failed, fallbacks) = (stats.compilations_failed, stats.interpreter_fallbacks); + if failed == 0 && fallbacks == 0 { return Ok(()); } Err(ExecutionResourceError(format!( - "{failed} compilation(s) failed in this run and fell back to the interpreter; \ - rerun with RUST_LOG=warn for the cause" + "{failed} compilation(s) failed and {fallbacks} frame(s) fell back to the interpreter \ + in this run; rerun with RUST_LOG=warn for the cause" ))) } diff --git a/crates/eest/src/fixture_io.rs b/crates/eest/src/fixture_io.rs index 710221009..c78e8ee01 100644 --- a/crates/eest/src/fixture_io.rs +++ b/crates/eest/src/fixture_io.rs @@ -38,7 +38,7 @@ pub enum FixtureWriteError { } /// Returns true when the path uses the binary fixture extension. -pub fn is_binary_path(path: &Path) -> bool { +pub(crate) fn is_binary_path(path: &Path) -> bool { has_extension(path, "bin") || is_zstd_path(path) && stem_has_extension(path, "bin") } @@ -109,12 +109,10 @@ fn is_zstd_path(path: &Path) -> bool { has_extension(path, "zst") } -fn has_extension(path: &Path, extension: &str) -> bool { +pub(crate) fn has_extension(path: &Path, extension: &str) -> bool { path.extension().is_some_and(|candidate| candidate == extension) } fn stem_has_extension(path: &Path, extension: &str) -> bool { - path.file_stem().is_some_and(|stem| { - Path::new(stem).extension().is_some_and(|candidate| candidate == extension) - }) + path.file_stem().is_some_and(|stem| has_extension(Path::new(stem), extension)) } diff --git a/crates/eest/src/fixture_kind.rs b/crates/eest/src/fixture_kind.rs new file mode 100644 index 000000000..988d37517 --- /dev/null +++ b/crates/eest/src/fixture_kind.rs @@ -0,0 +1,146 @@ +//! Detection of the kind of an EEST fixture file. + +use crate::fixture_io; +use serde::de::{DeserializeSeed, Deserializer, IgnoredAny, MapAccess, Visitor}; +use std::{borrow::Cow, fmt, path::Path}; + +/// Case fields only blockchain tests have. +const BLOCKCHAIN_FIELDS: &[&str] = &["blocks", "genesisBlockHeader", "lastblockhash", "network"]; + +/// Case fields only state tests have. +const STATE_FIELDS: &[&str] = &["env", "post", "transaction"]; + +/// The kind of an EEST fixture. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FixtureKind { + /// A state test. + State, + /// A blockchain test, in JSON or postcard binary form. + Blockchain, +} + +impl FixtureKind { + /// Returns the kind of a binary fixture from its path, or `None` for any other path. + /// + /// The binary format has no header to sniff, and only holds blockchain tests. + pub fn from_path(path: &Path) -> Option { + fixture_io::is_binary_path(path).then_some(Self::Blockchain) + } + + /// Detects the kind of a JSON fixture from the fields of its first case. + /// + /// A case with any of `blocks`, `genesisBlockHeader`, `lastblockhash` or `network` is a + /// blockchain test; otherwise a case with any of `env`, `post` or `transaction` is a state + /// test. Only the first case is inspected, and the rest of the input is skipped without + /// being materialized. Returns `Ok(None)` when the first case has none of these fields, or + /// the input has no case. + pub fn from_json(input: &str) -> Result, serde_json::Error> { + serde_json::Deserializer::from_str(input).deserialize_map(FixtureVisitor) + } +} + +struct FixtureVisitor; + +impl<'de> Visitor<'de> for FixtureVisitor { + type Value = Option; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an EEST fixture object") + } + + fn visit_map>(self, mut map: A) -> Result { + let mut kind = None; + let mut seen_case = false; + while map.next_key::()?.is_some() { + if seen_case { + map.next_value::()?; + } else { + seen_case = true; + kind = map.next_value_seed(CaseSeed)?; + } + } + Ok(kind) + } +} + +struct CaseSeed; + +impl<'de> DeserializeSeed<'de> for CaseSeed { + type Value = Option; + + fn deserialize>(self, deserializer: D) -> Result { + deserializer.deserialize_map(self) + } +} + +impl<'de> Visitor<'de> for CaseSeed { + type Value = Option; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an EEST fixture case object") + } + + fn visit_map>(self, mut map: A) -> Result { + let mut blockchain = false; + let mut state = false; + while let Some(field) = map.next_key::>()? { + blockchain |= BLOCKCHAIN_FIELDS.contains(&&*field); + state |= STATE_FIELDS.contains(&&*field); + map.next_value::()?; + } + Ok(if blockchain { + Some(FixtureKind::Blockchain) + } else if state { + Some(FixtureKind::State) + } else { + None + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn detects_state_json() { + let input = r#"{"case": {"env": {}, "pre": {}, "post": {}, "transaction": {}}}"#; + assert_eq!(FixtureKind::from_json(input).unwrap(), Some(FixtureKind::State)); + } + + #[test] + fn detects_blockchain_json() { + let input = r#"{"case": {"blocks": [], "pre": {}, "network": "Prague"}}"#; + assert_eq!(FixtureKind::from_json(input).unwrap(), Some(FixtureKind::Blockchain)); + } + + #[test] + fn detects_binary_path() { + for path in ["fixture.bin", "fixture.bin.zst"] { + assert_eq!(FixtureKind::from_path(Path::new(path)), Some(FixtureKind::Blockchain)); + } + for path in ["fixture.json", "fixture.json.zst"] { + assert_eq!(FixtureKind::from_path(Path::new(path)), None); + } + } + + #[test] + fn rejects_unknown_input() { + assert_eq!(FixtureKind::from_json("{}").unwrap(), None); + assert_eq!(FixtureKind::from_json(r#"{"case": {"pre": {}}}"#).unwrap(), None); + assert!(FixtureKind::from_json("[]").is_err()); + assert!(FixtureKind::from_json("not json").is_err()); + } + + #[test] + fn decodes_escaped_field_names() { + let input = r#"{"case": {"\u0062locks": []}}"#; + assert_eq!(FixtureKind::from_json(input).unwrap(), Some(FixtureKind::Blockchain)); + } + + #[test] + fn inspects_only_the_first_case() { + let input = r#"{"a": {"pre": {}}, "b": {"env": {}}}"#; + assert_eq!(FixtureKind::from_json(input).unwrap(), None); + } +} diff --git a/crates/eest/src/fixtures.rs b/crates/eest/src/fixtures.rs index 56aa86bd3..8c2d60b5e 100644 --- a/crates/eest/src/fixtures.rs +++ b/crates/eest/src/fixtures.rs @@ -7,11 +7,11 @@ use std::{ /// Environment variable for the downloaded fixture root. pub(crate) const TEST_FIXTURES_ENV: &str = "EVM2_TEST_FIXTURES"; -/// Generic EEST stable selector. +/// Environment variable for selecting stable EEST fixtures instead of develop. pub(crate) const EEST_STABLE_ENV: &str = "EVM2_EEST_STABLE"; -/// Selector for devnet-only runs, where the setup script skips the legacy fixtures. -pub(crate) const DEVNET_ONLY_ENV: &str = "EVM2_STATETEST_DEVNET_ONLY"; +/// Selector for execution-specs-only runs, where the setup script skips the legacy fixtures. +pub(crate) const EEST_ONLY_ENV: &str = "EVM2_STATETEST_EEST_ONLY"; /// Repo-relative fixture root used by the setup script and CI. pub(crate) const DEFAULT_FIXTURES_PATH: &str = "test-fixtures"; @@ -74,3 +74,10 @@ pub(crate) fn apply_subdir_to_roots(roots: &mut Vec, name: &str) { pub(crate) fn workspace_relative(path: PathBuf) -> PathBuf { if path.is_absolute() { path } else { workspace_root().join(path) } } + +/// Returns the explicit test root in the `root_env` variable, with the `subdir_env` filter applied. +pub(crate) fn explicit_root_from_env(root_env: &str, subdir_env: &str) -> Option { + let mut root = workspace_relative(env::var_os(root_env)?.into()); + apply_subdir(&mut root, subdir_env); + Some(root) +} diff --git a/crates/eest/src/forks.rs b/crates/eest/src/forks.rs index 395730cca..d6c0fb342 100644 --- a/crates/eest/src/forks.rs +++ b/crates/eest/src/forks.rs @@ -1,27 +1,23 @@ use evm2::SpecId; use std::{env, sync::OnceLock}; -/// Environment variable listing extra hardforks to skip, comma-separated by name. +/// Environment variable listing hardforks to skip, comma-separated by name. pub(crate) const SKIP_FORKS_ENV: &str = "EVM2_SKIP_FORKS"; -/// Hardforks skipped by default because evm2 does not support them yet. -const UNSUPPORTED_FORKS: &[SpecId] = &[]; - /// Returns whether all test cases targeting `spec` should be skipped. #[inline] pub(crate) fn is_fork_skipped(spec: SpecId) -> bool { skipped_forks().contains(&spec) } -/// Returns the skipped hardforks, combining [`UNSUPPORTED_FORKS`] with any -/// forks named in [`SKIP_FORKS_ENV`]. +/// Returns the hardforks named in [`SKIP_FORKS_ENV`]. fn skipped_forks() -> &'static [SpecId] { static SKIPPED: OnceLock> = OnceLock::new(); SKIPPED.get_or_init(|| { - let mut forks = UNSUPPORTED_FORKS.to_vec(); + let mut forks = Vec::new(); if let Ok(names) = env::var(SKIP_FORKS_ENV) { for name in names.split(',').map(str::trim).filter(|name| !name.is_empty()) { - let Some(spec) = parse_fork(name) else { + let Ok(spec) = name.parse() else { panic!("{SKIP_FORKS_ENV}: unknown hardfork name `{name}`"); }; forks.push(spec); @@ -30,29 +26,3 @@ fn skipped_forks() -> &'static [SpecId] { forks }) } - -/// Parses a case-insensitive hardfork name into a spec ID. -fn parse_fork(name: &str) -> Option { - macro_rules! parse { - ([$name:ident] $($spec:ident $lower:ident,)*) => { - match $name.to_ascii_lowercase().as_str() { - $(stringify!($lower) => Some(SpecId::$spec),)* - _ => None, - } - }; - } - evm2::for_each_spec!([name] parse) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn parses_fork_names() { - assert_eq!(parse_fork("amsterdam"), Some(SpecId::AMSTERDAM)); - assert_eq!(parse_fork("Osaka"), Some(SpecId::OSAKA)); - assert_eq!(parse_fork("SPURIOUS_DRAGON"), Some(SpecId::SPURIOUS_DRAGON)); - assert_eq!(parse_fork("atlantis"), None); - } -} diff --git a/crates/eest/src/harness.rs b/crates/eest/src/harness.rs index cff225fe2..902c43318 100644 --- a/crates/eest/src/harness.rs +++ b/crates/eest/src/harness.rs @@ -169,7 +169,7 @@ fn compiled_eest_subset_allows(subset: Option, name: &str) - let subset = subset.unwrap_or_else(|| default_compiled_eest_subset(name)); match subset { CompiledEestSubset::All => true, - CompiledEestSubset::CiAot => CI_AOT_STATE_DIRS.iter().any(|path| name.contains(path)), + CompiledEestSubset::CiAot => contains_any(name, CI_AOT_STATE_DIRS), CompiledEestSubset::CiSmoke => CI_SMOKE_STATE_FILES.iter().any(|path| name.ends_with(path)), } } @@ -184,6 +184,11 @@ fn default_compiled_eest_subset(name: &str) -> CompiledEestSubset { } } +/// Returns whether `name` contains any of `patterns`. +pub(crate) fn contains_any(name: &str, patterns: &[&str]) -> bool { + patterns.iter().any(|pattern| name.contains(pattern)) +} + const CI_AOT_STATE_DIRS: &[&str] = &[ "frontier/create/", "frontier/opcodes/", diff --git a/crates/eest/src/jit.rs b/crates/eest/src/jit.rs index 16bb02b82..b65535f51 100644 --- a/crates/eest/src/jit.rs +++ b/crates/eest/src/jit.rs @@ -22,6 +22,10 @@ pub(crate) fn make_backend(aot: bool) -> Result { jit_worker_count: cpus, jit_opt_level: OptimizationLevel::None, aot_opt_level: OptimizationLevel::None, + // Every resident program holds memory mappings, and the full corpus compiles more + // distinct programs than a default `vm.max_map_count` allows. A small budget evicts old + // programs. + resident_code_cache_bytes: 8 * 1024 * 1024, ..Default::default() }, ..Default::default() diff --git a/crates/eest/src/lib.rs b/crates/eest/src/lib.rs index be4915d2c..876df39d7 100644 --- a/crates/eest/src/lib.rs +++ b/crates/eest/src/lib.rs @@ -15,6 +15,7 @@ mod execute; mod execution; mod filter; mod fixture_io; +mod fixture_kind; mod fixtures; mod forks; mod harness; @@ -47,15 +48,15 @@ pub use execute::{ }; pub use filter::NameFilter; pub use fixture_io::{ - FixtureReadError, FixtureWriteError, is_binary_path as is_binary_fixture_path, - read_blockchain as read_blockchain_fixture, read_to_string as read_fixture_text, - write_blockchain as write_blockchain_fixture, + FixtureReadError, FixtureWriteError, read_blockchain as read_blockchain_fixture, + read_to_string as read_fixture_text, write_blockchain as write_blockchain_fixture, }; +pub use fixture_kind::FixtureKind; pub use runner::run as run_statetests; pub use tx::{AccessListItem, TestAuthorization}; pub use types::{ - AccountInfo as StateTestAccountInfo, Env as StateTestEnv, SpecName as StateTestSpecName, - Test as StateTestPost, TestSuite as StateTestSuite, TestUnit as StateTestUnit, + AccountInfo as StateTestAccountInfo, Env as StateTestEnv, Test as StateTestPost, + TestSuite as StateTestSuite, TestUnit as StateTestUnit, TransactionParts as StateTestTransactionParts, TxPartIndices as StateTestTxPartIndices, }; diff --git a/crates/eest/src/runner.rs b/crates/eest/src/runner.rs index b51d73505..6f14806f4 100644 --- a/crates/eest/src/runner.rs +++ b/crates/eest/src/runner.rs @@ -1,7 +1,7 @@ use crate::{ env::state_test_roots, execute::{ExecuteConfig, ExecutionMode, execute_test_suite}, - harness::{TestSuite, descend_all, run_json_harnesses}, + harness::{TestSuite, contains_any, descend_all, run_json_harnesses}, }; #[cfg(feature = "jit")] use crate::{execute::execute_test_suites, execution::CompiledMode, harness::compiled_roots}; @@ -136,13 +136,8 @@ pub(crate) const IGNORED_TESTS: &[&str] = &[ "create2collisionStorageParis.json", "dynamicAccountOverwriteEmpty.json", "dynamicAccountOverwriteEmpty_Paris.json", - - // The state-test runner takes the sender from the fixture and does not recover or - // validate legacy-tx signatures, so v/r/s and chain-id rejection tests cannot pass. - "frontier/validation/transaction/bad_v_r_s.json", - "london/eip1559_fee_market_change/tx_type/invalid_chain_id.json", ]; fn should_ignore(name: &str) -> bool { - IGNORED_TESTS.iter().any(|pattern| name.contains(pattern)) + contains_any(name, IGNORED_TESTS) } diff --git a/crates/eest/src/state.rs b/crates/eest/src/state.rs index c9a78320a..aa104442a 100644 --- a/crates/eest/src/state.rs +++ b/crates/eest/src/state.rs @@ -6,7 +6,7 @@ use evm2::{ /// Parses fixture bytecode into evm2 bytecode. pub(crate) fn parse_bytecode(code: alloy_primitives::Bytes) -> Bytecode { - Bytecode::new_raw_checked(code.clone()).unwrap_or_else(|_| Bytecode::new_legacy(code)) + Bytecode::from_stored(code) } /// Inserts an account and its storage into an in-memory database. @@ -22,17 +22,6 @@ pub(crate) fn insert_account_with_storage( } } -/// Returns whether the given system contract exists with non-empty code. -pub(crate) fn system_contract_has_code(database: &InMemoryDB, address: Address) -> bool { - database - .cache - .accounts - .get(&address) - .and_then(Option::as_ref) - .and_then(|info| database.cache.contracts.get(&info.code_hash)) - .is_some_and(|code| !code.is_empty()) -} - /// Returns persistent storage values for trie-root calculation. pub(crate) fn storage_for_root( state: &InMemoryDB, diff --git a/crates/eest/src/trace.rs b/crates/eest/src/trace.rs index 3429198ff..21dfdd1b9 100644 --- a/crates/eest/src/trace.rs +++ b/crates/eest/src/trace.rs @@ -14,7 +14,7 @@ use alloy_primitives::{B256, Bytes, U256}; use evm2::{ EvmTypesHost, Inspector, - interpreter::{Interpreter, Message, MessageResult, opcode::OpCode}, + interpreter::{InstrStop, Interpreter, Message, MessageResult, opcode::OpCode}, }; use serde_json::json; use std::io::{self, Write}; @@ -65,7 +65,12 @@ impl Eip3155Tracer { let Some(step) = self.pending.take() else { return; }; - let gas_cost = step.gas.saturating_sub(interp.gas().remaining()); + // An out-of-gas step consumes the whole remainder it started with. + let gas_cost = if interp.result().is_err_and(InstrStop::is_out_of_gas) { + step.gas + } else { + step.gas.saturating_sub(interp.gas().remaining()) + }; self.emit(&step, gas_cost); } diff --git a/crates/eest/src/tx.rs b/crates/eest/src/tx.rs index 9bda2c0e6..aa3a126bc 100644 --- a/crates/eest/src/tx.rs +++ b/crates/eest/src/tx.rs @@ -1,11 +1,21 @@ -use alloy_consensus::{TypedTransaction, transaction::Recovered}; -use alloy_eips::eip7702::SignedAuthorization; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256}; +use alloy_consensus::{ + EthereumTxEnvelope, SignableTransaction, TxEip4844, TypedTransaction, + crypto::{RecoveryError, secp256k1}, + transaction::Recovered, +}; +use alloy_eips::{ + eip2718::{Decodable2718, Eip2718Error}, + eip7702::SignedAuthorization, +}; +use alloy_primitives::{Address, B256, Bytes, Signature, TxKind, U256}; use alloy_rpc_types_eth::{ AccessList as RpcAccessList, AccessListItem as RpcAccessListItem, TransactionInput, TransactionRequest, }; -use evm2::ethereum::{RecoveredTxEnvelope, TxEnvelope}; +use evm2::{ + SpecId, + ethereum::{RecoveredTxEnvelope, TxEnvelope}, +}; use k256::ecdsa::SigningKey; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -67,14 +77,32 @@ pub(crate) enum TxBuildError { /// JSON decoding failed. #[error(transparent)] SerdeDeserialize(#[from] serde_json::Error), + /// Signed transaction bytes could not be decoded. + #[error("could not decode signed transaction: {0}")] + Decode(#[from] Eip2718Error), + /// Signature `v` value is not valid for the transaction type. + #[error("invalid signature v value: {0}")] + InvalidV(U256), + /// An EIP-155 legacy transaction appears before Spurious Dragon. + #[error("EIP-155 transaction before Spurious Dragon")] + ProtectedLegacy, + /// Sender could not be recovered from the signature. + #[error("could not recover sender: {0}")] + Recover(#[from] RecoveryError), +} + +/// Where a transaction's sender comes from. +pub(crate) enum TxSigner { + /// Sender taken from the fixture, for fixtures that carry no signature. + Fixture(Address), + /// Sender recovered from the signature, as the fork at `spec` validates it. + Signature { signature: Signature, spec: SpecId }, } /// Common transaction fields already selected from a fixture. pub(crate) struct TxFields { /// Explicit transaction type. pub(crate) tx_type: Option, - /// Recovered or explicit sender. - pub(crate) caller: Address, /// Transaction recipient, or create. pub(crate) kind: TxKind, /// Transaction calldata. @@ -104,9 +132,30 @@ pub(crate) struct TxFields { } /// Builds an evm2 recovered transaction envelope from common fixture fields. -pub(crate) fn build_recovered_tx(fields: TxFields) -> Result { +pub(crate) fn build_recovered_tx( + fields: TxFields, + signer: TxSigner, +) -> Result { + let tx = consensus_tx(fields)?; + let caller = tx_caller(&tx, signer)?; + Ok(recovered_envelope(tx, caller)) +} + +/// Builds a fixture transaction like [`build_recovered_tx`], also returning it signed with +/// `signature` as a consensus envelope. +pub(crate) fn build_signed_recovered_tx( + fields: TxFields, + signer: TxSigner, + signature: Signature, +) -> Result<(RecoveredTxEnvelope, EthereumTxEnvelope), TxBuildError> { + let tx = consensus_tx(fields)?; + let caller = tx_caller(&tx, signer)?; + let signed = tx.clone().into_signed(signature).into(); + Ok((recovered_envelope(tx, caller), signed)) +} + +fn consensus_tx(fields: TxFields) -> Result { let mut request = TransactionRequest::default() - .from(fields.caller) .gas_limit(fields.gas_limit) .nonce(fields.nonce) .value(fields.value) @@ -160,9 +209,72 @@ pub(crate) fn build_recovered_tx(fields: TxFields) -> Result Result { + Ok(match signer { + TxSigner::Fixture(caller) => caller, + TxSigner::Signature { signature, spec } => { + recover_sender(&signature, tx.signature_hash(), spec)? + } + }) +} + +/// Decodes an EIP-2718 signed transaction and recovers its sender. +pub(crate) fn decode_recovered_tx( + bytes: &[u8], + spec: SpecId, +) -> Result { + let tx = EthereumTxEnvelope::::decode_2718_exact(bytes)?; + if let EthereumTxEnvelope::Legacy(signed) = &tx + && signed.tx().chain_id.is_some() + && !spec.enables(SpecId::SPURIOUS_DRAGON) + { + return Err(TxBuildError::ProtectedLegacy); + } + let caller = recover_sender(tx.signature(), tx.signature_hash(), spec)?; + Ok(Recovered::new_unchecked(tx.into(), caller)) +} + +/// Splits a fixture `v` value into the signature parity and, for legacy transactions, the EIP-155 +/// chain ID it encodes. A legacy `v` is 27 or 28, or from Spurious Dragon on `35 + 2 * chain_id` +/// plus the parity. +pub(crate) fn signature_from_vrs( + r: U256, + s: U256, + v: U256, + legacy: bool, + spec: SpecId, +) -> Result<(Signature, Option), TxBuildError> { + let invalid = || TxBuildError::InvalidV(v); + let (y_parity, chain_id) = if !legacy && v <= U256::ONE { + (v == U256::ONE, None) + } else if legacy && (v == U256::from(27) || v == U256::from(28)) { + (v == U256::from(28), None) + } else if legacy && v >= U256::from(35) { + if !spec.enables(SpecId::SPURIOUS_DRAGON) { + return Err(TxBuildError::ProtectedLegacy); + } + let encoded = v - U256::from(35); + (encoded.bit(0), Some(u64::try_from(encoded >> 1).map_err(|_| invalid())?)) + } else { + return Err(invalid()); + }; + Ok((Signature::new(r, s, y_parity), chain_id)) +} + +fn recover_sender( + signature: &Signature, + hash: B256, + spec: SpecId, +) -> Result { + // EIP-2 rejects high-`s` signatures from Homestead on. + if spec.enables(SpecId::HOMESTEAD) { + secp256k1::recover_signer(signature, hash) + } else { + secp256k1::recover_signer_unchecked(signature, hash) + } } /// Converts EEST access list items into RPC access list items. @@ -209,7 +321,9 @@ fn recovered_envelope(tx: TypedTransaction, caller: Address) -> RecoveredTxEnvel #[cfg(test)] mod tests { - use super::TestAuthorization; + use super::*; + use alloy_consensus::{TxLegacy, crypto::secp256k1}; + use alloy_eips::eip2718::Encodable2718; use serde_json::json; #[test] @@ -226,4 +340,51 @@ mod tests { assert_eq!(serde_json::to_value(authorization).unwrap(), value); } + + #[test] + fn signature_from_vrs_accepts_the_v_each_fork_allows() { + let (r, s) = (U256::ONE, U256::ONE); + let v = |v: u64| U256::from(v); + let parse = |v: U256, legacy, spec| signature_from_vrs(r, s, v, legacy, spec); + + assert_eq!(parse(v(27), true, SpecId::FRONTIER).unwrap().1, None); + assert!(!parse(v(27), true, SpecId::FRONTIER).unwrap().0.v()); + assert!(parse(v(28), true, SpecId::FRONTIER).unwrap().0.v()); + assert!(matches!( + parse(v(37), true, SpecId::TANGERINE), + Err(TxBuildError::ProtectedLegacy) + )); + let (signature, chain_id) = parse(v(38), true, SpecId::SPURIOUS_DRAGON).unwrap(); + assert!(signature.v()); + assert_eq!(chain_id, Some(1)); + assert!(matches!(parse(v(29), true, SpecId::CANCUN), Err(TxBuildError::InvalidV(_)))); + assert!(matches!(parse(v(27), false, SpecId::CANCUN), Err(TxBuildError::InvalidV(_)))); + assert!(parse(v(1), false, SpecId::CANCUN).unwrap().0.v()); + } + + #[test] + fn signature_from_vrs_keeps_v_at_full_width() { + let encoded = |chain_id: u128| U256::from(chain_id) * U256::from(2) + U256::from(35); + let parse = |v| signature_from_vrs(U256::ONE, U256::ONE, v, true, SpecId::CANCUN); + + // The encoded `v` of the largest chain ID needs 65 bits. + let chain_id = u128::from(u64::MAX); + assert_eq!(parse(encoded(chain_id)).unwrap().1, Some(u64::MAX)); + assert!(matches!(parse(encoded(chain_id + 1)), Err(TxBuildError::InvalidV(_)))); + } + + #[test] + fn decode_recovered_tx_rejects_eip155_before_spurious_dragon() { + let secret = B256::with_last_byte(1); + let tx = TxLegacy { chain_id: Some(1), gas_limit: 21_000, ..Default::default() }; + let signature = secp256k1::sign_message(secret, tx.signature_hash()).unwrap(); + let bytes = tx.into_signed(signature).encoded_2718(); + + assert!(matches!( + decode_recovered_tx(&bytes, SpecId::TANGERINE), + Err(TxBuildError::ProtectedLegacy) + )); + let recovered = decode_recovered_tx(&bytes, SpecId::SPURIOUS_DRAGON).unwrap(); + assert_eq!(recovered.signer(), recover_address(secret.as_slice()).unwrap()); + } } diff --git a/crates/eest/src/types.rs b/crates/eest/src/types.rs index ceefda94d..8e437406d 100644 --- a/crates/eest/src/types.rs +++ b/crates/eest/src/types.rs @@ -1,10 +1,10 @@ -#![allow(dead_code)] - -use crate::tx::{AccessListItem, TestAuthorization}; +use crate::{ + blockchaintest::{ChainConfig, ForkSpec}, + tx::{AccessListItem, TestAuthorization}, +}; use alloy_primitives::{Address, B256, Bytes, U256}; -use evm2::SpecId; use serde::{Deserialize, Deserializer, de}; -use std::collections::BTreeMap; +use std::{collections::BTreeMap, fmt}; /// Top-level state test suite. #[derive(Debug, Deserialize)] @@ -21,12 +21,16 @@ pub struct TestUnit { /// Pre-state accounts. pub pre: BTreeMap, /// Expected post-state roots by fork. - pub post: BTreeMap>, + #[serde(deserialize_with = "deserialize_post")] + pub post: BTreeMap>, /// Transaction parts indexed by each post entry. pub transaction: TransactionParts, /// Expected output. #[serde(default)] pub out: Option, + /// Chain configuration the test ran under. + #[serde(default)] + pub config: Option, } /// State test block environment. @@ -128,91 +132,6 @@ pub struct TransactionParts { pub max_fee_per_blob_gas: Option, } -/// State test fork name. -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Hash)] -pub enum SpecName { - /// Frontier. - Frontier, - /// Frontier to Homestead transition. - FrontierToHomesteadAt5, - /// Homestead. - Homestead, - /// Homestead to DAO transition. - HomesteadToDaoAt5, - /// Homestead to EIP-150 transition. - HomesteadToEIP150At5, - /// EIP-150 aka Tangerine Whistle. - #[serde(alias = "TangerineWhistle")] - EIP150, - /// EIP-158 aka Spurious Dragon. - #[serde(alias = "SpuriousDragon")] - EIP158, - /// EIP-158 to Byzantium transition. - EIP158ToByzantiumAt5, - /// Byzantium. - Byzantium, - /// Skipped Constantinople transition. - ByzantiumToConstantinopleAt5, - /// Byzantium to Petersburg transition. - ByzantiumToConstantinopleFixAt5, - /// Skipped Constantinople. - Constantinople, - /// Petersburg. - ConstantinopleFix, - /// Istanbul. - Istanbul, - /// Berlin. - Berlin, - /// Berlin to London transition. - BerlinToLondonAt5, - /// London. - London, - /// Paris. - Paris, - /// Merge. - Merge, - /// Shanghai. - Shanghai, - /// Cancun. - Cancun, - /// Prague. - Prague, - /// Osaka. - Osaka, - /// Amsterdam. - Amsterdam, -} - -impl SpecName { - /// Converts the state test fork name to the local spec ID. - #[inline] - pub const fn to_spec_id(self) -> Option { - match self { - Self::Frontier => Some(SpecId::FRONTIER), - Self::FrontierToHomesteadAt5 | Self::Homestead => Some(SpecId::HOMESTEAD), - Self::HomesteadToDaoAt5 | Self::HomesteadToEIP150At5 | Self::EIP150 => { - Some(SpecId::TANGERINE) - } - Self::EIP158 => Some(SpecId::SPURIOUS_DRAGON), - Self::EIP158ToByzantiumAt5 | Self::Byzantium => Some(SpecId::BYZANTIUM), - Self::ByzantiumToConstantinopleAt5 - | Self::ByzantiumToConstantinopleFixAt5 - | Self::ConstantinopleFix => Some(SpecId::PETERSBURG), - Self::Istanbul => Some(SpecId::ISTANBUL), - Self::Berlin => Some(SpecId::BERLIN), - Self::BerlinToLondonAt5 | Self::London => Some(SpecId::LONDON), - Self::Paris | Self::Merge => Some(SpecId::MERGE), - Self::Shanghai => Some(SpecId::SHANGHAI), - Self::Cancun => Some(SpecId::CANCUN), - Self::Prague => Some(SpecId::PRAGUE), - Self::Osaka => Some(SpecId::OSAKA), - Self::Amsterdam => Some(SpecId::AMSTERDAM), - // Skip Constantinople due to the pre-Petersburg reentrancy bug. - Self::Constantinople => None, - } - } -} - /// State test post entry. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] @@ -231,7 +150,8 @@ pub struct Test { /// Optional full expected state. #[serde(default)] pub state: BTreeMap, - /// Optional encoded transaction bytes. + /// Signed EIP-2718 transaction bytes. When present they replace the indexed `transaction` + /// fields and the sender is recovered from their signature. pub txbytes: Option, } @@ -289,10 +209,55 @@ where .collect() } +/// Deserializes the post entries, rejecting two keys that name the same fork, such as `Paris` and +/// its alias `Merge`. +fn deserialize_post<'de, D>(deserializer: D) -> Result>, D::Error> +where + D: Deserializer<'de>, +{ + struct PostVisitor; + + impl<'de> de::Visitor<'de> for PostVisitor { + type Value = BTreeMap>; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("a map of fork names to post entries") + } + + fn visit_map(self, mut map: A) -> Result + where + A: de::MapAccess<'de>, + { + let mut post = BTreeMap::new(); + while let Some((fork, tests)) = map.next_entry::>()? { + if post.insert(fork, tests).is_some() { + return Err(de::Error::custom(format!("duplicate post entry for {fork:?}"))); + } + } + Ok(post) + } + } + + deserializer.deserialize_map(PostVisitor) +} + #[cfg(test)] mod tests { use super::*; + #[derive(Debug, Deserialize)] + struct Post(#[serde(deserialize_with = "deserialize_post")] BTreeMap>); + + #[test] + fn post_rejects_two_keys_for_one_fork() { + assert!(serde_json::from_str::(r#"{"Paris": [], "Merge": []}"#).is_err()); + assert!(serde_json::from_str::(r#"{"Merge": [], "Paris": []}"#).is_err()); + assert_eq!( + serde_json::from_str::(r#"{"Paris": [], "Cancun": []}"#).unwrap().0.len(), + 2 + ); + } + #[test] fn transaction_value_allows_bigint_marker() { let input = r#"{ diff --git a/crates/evm2/Cargo.toml b/crates/evm2/Cargo.toml index 640184f03..e2905df48 100644 --- a/crates/evm2/Cargo.toml +++ b/crates/evm2/Cargo.toml @@ -38,6 +38,9 @@ serde = { workspace = true, features = ["derive", "alloc"], optional = true } # "wincode" wincode = { workspace = true, optional = true } +# "block" +alloy-hardforks = { workspace = true, optional = true } + # "async" corosensei = { workspace = true, optional = true } @@ -148,6 +151,9 @@ std = [ async = ["std", "dep:corosensei"] +# Block execution: `evm2::block`. +block = ["dep:alloy-hardforks"] + wincode = ["dep:wincode"] serde = [ @@ -155,6 +161,7 @@ serde = [ "alloy-consensus/serde", "alloy-eip7928/serde", "alloy-eips/serde", + "alloy-hardforks?/serde", "alloy-primitives/serde", "ark-serialize/serde", "bitflags/serde", diff --git a/crates/evm2/build.rs b/crates/evm2/build.rs index 6477bc1a6..fec262a35 100644 --- a/crates/evm2/build.rs +++ b/crates/evm2/build.rs @@ -3,9 +3,7 @@ use std::{ffi::OsString, process::Command}; fn main() { - for cfg in - ["cranelift", "dispatch_packed", "dispatch_single_return", "dispatch_unpacked", "tco"] - { + for cfg in ["cranelift", "dispatch_packed", "dispatch_unpacked", "tco"] { println!("cargo:rustc-check-cfg=cfg({cfg})"); } println!("cargo:rerun-if-changed=build.rs"); @@ -23,7 +21,6 @@ fn main() { DispatchBackend::Auto => unreachable!("auto backend must resolve to a concrete backend"), DispatchBackend::Tco => println!("cargo:rustc-cfg=tco"), DispatchBackend::Packed => println!("cargo:rustc-cfg=dispatch_packed"), - DispatchBackend::SingleReturn => println!("cargo:rustc-cfg=dispatch_single_return"), DispatchBackend::Unpacked => println!("cargo:rustc-cfg=dispatch_unpacked"), } } @@ -32,7 +29,6 @@ enum DispatchBackend { Auto, Tco, Packed, - SingleReturn, Unpacked, } @@ -46,27 +42,20 @@ impl DispatchBackend { "" | "auto" => Self::Auto, "tco" => Self::Tco, "packed" => Self::Packed, - "single_return" | "single-return" => Self::SingleReturn, "unpacked" => Self::Unpacked, _ => panic!( - "invalid EVM2_DISPATCH_BACKEND={value:?}; expected auto, tco, packed, single_return, or unpacked" + "invalid EVM2_DISPATCH_BACKEND={value:?}; expected auto, tco, packed, or unpacked" ), } } fn resolve(self, is_wasm: bool, target_pointer_width: Option, no_tco: bool) -> Self { match self { - Self::Auto => { - if is_wasm { - Self::SingleReturn - } else if !no_tco && rustc_is_nightly() { - Self::Tco - } else if target_pointer_width == Some(64) { - Self::Packed - } else { - Self::Unpacked - } + // `rust-preserve-none` passes arguments on the stack on 32-bit targets. + Self::Auto if !is_wasm && target_pointer_width == Some(64) => { + if !no_tco && rustc_is_nightly() { Self::Tco } else { Self::Packed } } + Self::Auto => Self::Unpacked, concrete => concrete, } } diff --git a/crates/evm2/examples/bal_build.rs b/crates/evm2/examples/bal_build.rs index 97dc754f7..9aabf2daa 100644 --- a/crates/evm2/examples/bal_build.rs +++ b/crates/evm2/examples/bal_build.rs @@ -37,7 +37,11 @@ fn main() { let executed = evm .system_call(SystemTx::new(PRE_SYSTEM_CONTRACT, word(0xbeef))) .expect("pre-block system call should execute"); - assert!(executed.result().status, "pre-block system call failed: {:?}", executed.result()); + assert!( + executed.result().is_success(), + "pre-block system call failed: {:?}", + executed.result() + ); let _ = executed.commit(); // Transaction 0 is recorded at index 1: a plain value transfer to ALICE. @@ -45,7 +49,7 @@ fn main() { assert_eq!(evm.state_mut().bal_index(), idx(1)); let tx0 = transfer(CALLER, ALICE, 1_000_000, 0); let result = evm.transact(&tx0).expect("transaction 0 should execute").commit(); - assert!(result.status, "transaction 0 failed: {result:?}"); + assert!(result.is_success(), "transaction 0 failed: {result:?}"); // Transaction 1 is recorded at index 2: a call that stores 42 at slot 5. evm.state_mut().bump_bal_index(); @@ -61,7 +65,7 @@ fn main() { CALLER, ); let result = evm.transact(&tx1).expect("transaction 1 should execute").commit(); - assert!(result.status); + assert!(result.is_success()); // The post-block transition is recorded at the final index 3. A system call // commits through the same path as a transaction; block rewards and withdrawals, @@ -72,7 +76,7 @@ fn main() { let executed = evm .system_call(SystemTx::new(POST_SYSTEM_CONTRACT, word(0x22))) .expect("post-block system call should execute"); - assert!(executed.result().status); + assert!(executed.result().is_success()); let _ = executed.commit(); // Take the built BAL. Writes carry the block access index they happened at. diff --git a/crates/evm2/examples/bal_parallel.rs b/crates/evm2/examples/bal_parallel.rs index 567cae67c..6c01518ac 100644 --- a/crates/evm2/examples/bal_parallel.rs +++ b/crates/evm2/examples/bal_parallel.rs @@ -73,7 +73,7 @@ fn main() { for (i, handle) in handles.into_iter().enumerate() { let TxResultWithState { result, pending_state: pending, .. } = handle.join().expect("worker thread panicked"); - assert!(result.status); + assert!(result.is_success()); // The pending state still carries the change-set that persistence // consumers (e.g. reth) stream to the database through a @@ -109,7 +109,7 @@ fn build_bal(transactions: &[RecoveredTxEnvelope]) -> Bal { for tx in transactions { evm.state_mut().bump_bal_index(); let result = evm.transact(tx).expect("transaction should execute").commit(); - assert!(result.status); + assert!(result.is_success()); } evm.state_mut().take_bal_builder().expect("builder was enabled") } diff --git a/crates/evm2/examples/custom_evm/config.rs b/crates/evm2/examples/custom_evm/config.rs index 86e326914..e2e51e81f 100644 --- a/crates/evm2/examples/custom_evm/config.rs +++ b/crates/evm2/examples/custom_evm/config.rs @@ -102,7 +102,7 @@ impl EvmConfig } pub const fn custom_version(base_spec_id: SpecId) -> Version { - let mut version = *Version::base(base_spec_id); + let mut version = Version::new(base_spec_id); opcode::install_gas_params(&mut version.gas_params); version } diff --git a/crates/evm2/examples/custom_evm/main.rs b/crates/evm2/examples/custom_evm/main.rs index 1ab7d1e51..2f104cadf 100644 --- a/crates/evm2/examples/custom_evm/main.rs +++ b/crates/evm2/examples/custom_evm/main.rs @@ -55,60 +55,58 @@ fn custom_opcode() -> HandlerResult<()> { println!( "custom opcode: tx_type=0x{:02x} expected status=true stop=Stop gas_used={expected_gas}; got status={} stop={:?} gas_used={}", tx.ty(), - result.status, + result.is_success(), result.stop, result.tx_gas_used(), ); assert_eq!(result.stop, InstrStop::Stop); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), expected_gas); assert!(result.ext.handled_custom_tx); Ok(()) } fn state_change_consumer() -> HandlerResult<()> { - let target = Address::from([0xcc; 20]); + let target = Address::repeat_byte(0xcc); let mut database = InMemoryDB::default(); database.insert_account_info(&target, AccountInfo::default().with_nonce(1)); let mut evm = custom_evm_with_database(database); let mut consumer = ExampleStateConsumer::default(); - let tx = Recovered::new_unchecked( - CustomEnvelope::ExecuteCode(ExecuteCodeTx { - target, - gas_limit: 100_000, - code: Bytes::from_static(&[ - opcode::CUSTOM_OPCODE, - op::PUSH1, - 0x01, - op::SSTORE, - op::STOP, - ]), - }), - Address::ZERO, - ); + let tx = custom_opcode_tx(Bytes::from_static(&[ + opcode::CUSTOM_OPCODE, + op::PUSH1, + 0x01, + op::SSTORE, + op::STOP, + ])); let Ok(result) = evm.transact(&tx)?.commit_with(&mut consumer); let storage_change = consumer.storage_changes.first().expect("storage change observed"); println!( "state consumer: expected status=true accounts=0 storage_writes=1 slot=1 value=0xdead; got status={} accounts={} storage_writes={} slot={} value={:#x}", - result.status, + result.is_success(), consumer.account_changes, consumer.storage_changes.len(), storage_change.key, storage_change.current, ); - assert!(result.status); + assert!(result.is_success()); assert_eq!(consumer.bytecodes, 0); assert_eq!(consumer.account_changes, 0); assert_eq!(consumer.storage_wipes, 0); assert_eq!(consumer.storage_changes.len(), 1); - assert_eq!(storage_change.address, target); - assert_eq!(storage_change.key, U256::from(1)); - assert_eq!(storage_change.original, U256::ZERO); - assert_eq!(storage_change.current, U256::from(0xdead_u64)); + assert_eq!( + *storage_change, + StorageChange { + address: target, + key: U256::ONE, + original: U256::ZERO, + current: U256::from(0xdead_u64), + } + ); Ok(()) } @@ -125,15 +123,17 @@ fn l1_blocknumber_opcode() -> HandlerResult<()> { ])); let result = evm.transact(&tx)?.discard(); - let expected = Bytes::copy_from_slice(&U256::from(CUSTOM_L1_BLOCK_NUMBER).to_be_bytes::<32>()); + let expected = Bytes::from(U256::from(CUSTOM_L1_BLOCK_NUMBER).to_be_bytes::<32>()); println!( "l1 blocknumber opcode: expected status=true stop=Return output={expected:?}; got status={} stop={:?} output={:?}", - result.status, result.stop, result.output, + result.is_success(), + result.stop, + result.output, ); assert_eq!(result.stop, InstrStop::Return); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.output, expected); assert!(result.ext.handled_custom_tx); Ok(()) @@ -147,11 +147,12 @@ fn mainnet_fallback() -> HandlerResult<()> { println!( "mainnet fallback: expected status=false stop=OpcodeNotFound; got status={} stop={:?}", - result.status, result.stop, + result.is_success(), + result.stop, ); assert_eq!(result.stop, InstrStop::OpcodeNotFound); - assert!(!result.status); + assert!(!result.is_success()); Ok(()) } @@ -177,7 +178,7 @@ fn inspector() -> HandlerResult<()> { println!( "inspector: expected status=true initialized=1 steps=5 step_ends=5 logs=1 calls=1 opcodes={expected_opcodes:?}; got status={} initialized={} steps={} step_ends={} logs={} calls={} opcodes={:?}", - result.status, + result.is_success(), inspector_state.initialized, inspector_state.steps, inspector_state.step_ends, @@ -186,7 +187,7 @@ fn inspector() -> HandlerResult<()> { inspector_state.opcodes, ); - assert!(result.status); + assert!(result.is_success()); assert_eq!(inspector_state.initialized, 1); assert_eq!(inspector_state.steps, expected_opcodes.len()); assert_eq!(inspector_state.step_ends, expected_opcodes.len()); @@ -238,7 +239,7 @@ fn custom_execution_config() -> ExecutionConfig { fn custom_opcode_tx(code: Bytes) -> Recovered { Recovered::new_unchecked( CustomEnvelope::ExecuteCode(ExecuteCodeTx { - target: Address::from([0xcc; 20]), + target: Address::repeat_byte(0xcc), gas_limit: 100_000, code, }), diff --git a/crates/evm2/examples/custom_evm/tx.rs b/crates/evm2/examples/custom_evm/tx.rs index 4357cb7b9..e27a348bf 100644 --- a/crates/evm2/examples/custom_evm/tx.rs +++ b/crates/evm2/examples/custom_evm/tx.rs @@ -66,11 +66,12 @@ pub fn execute_code( mut message: MessageExt, ) -> HandlerResult> { // The transaction handler owns policy; the interpreter still executes a normal message. - let tx_env = TxEnvExt { ext: CustomTxEnvExt { label: "execute-code" }, ..TxEnvExt::default() }; - let mut result = req.host.execute_message(&tx_env, &mut message)?; + *req.host.tx_env_mut() = + TxEnvExt { ext: CustomTxEnvExt { label: "execute-code" }, ..TxEnvExt::default() }; + let mut result = req.host.execute_message(&mut message)?; result.ext = CustomMessageResultExt { handled_custom_message: true }; Ok(evm2::TxResult:: { - status: result.stop.is_success(), + status: result.is_success(), total_gas_spent: req.tx.gas_limit - result.gas.remaining(), stop: result.stop, output: result.output, diff --git a/crates/evm2/examples/precompile_subcall.rs b/crates/evm2/examples/precompile_subcall.rs index 2da982aa3..8d40fd92b 100644 --- a/crates/evm2/examples/precompile_subcall.rs +++ b/crates/evm2/examples/precompile_subcall.rs @@ -4,7 +4,7 @@ use alloy_primitives::{Address, Bytes, U256}; use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, evm::{AccountInfo, InMemoryDB, precompile::PrecompileOutput}, interpreter::{GasTracker, Host, InstrStop, Message, MessageExt, MessageKind, Word, op}, precompiles::{Precompile, PrecompileError, PrecompileHalt, PrecompileId, PrecompileResult}, @@ -27,7 +27,7 @@ fn main() { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Return); assert_eq!(result.output.len(), 32); @@ -77,7 +77,7 @@ fn staticcall_precompile( destination: SUBCALL_TARGET, call_target: SUBCALL_TARGET, caller: message.destination, - input: message.input.clone(), + input: Bytes::copy_from_slice(evm.message_input(message)).into(), value: U256::ZERO, code: loaded.code, code_address: SUBCALL_TARGET, @@ -86,12 +86,12 @@ fn staticcall_precompile( ..MessageExt::default() }; - let result = Host::execute_message(evm, &TxEnvExt::default(), &mut child)?; + let result = Host::execute_message(evm, &mut child)?; gas.merge_child_gas(result.gas, result.stop); match result.stop { stop if stop.is_success() => Ok(PrecompileOutput::new(result.output)), - stop if stop.is_revert() => Err(PrecompileError::Revert(result.output)), + stop if stop.is_frame_revert() => Err(PrecompileError::Revert(result.output)), stop => Err(PrecompileHalt::Other(format!("subcall halted with {stop:?}").into()).into()), } } diff --git a/crates/evm2/src/block/bundle_state/account_status.rs b/crates/evm2/src/block/bundle_state/account_status.rs new file mode 100644 index 000000000..4a3dbdcac --- /dev/null +++ b/crates/evm2/src/block/bundle_state/account_status.rs @@ -0,0 +1,388 @@ +/// AccountStatus represents the various states an account can be in after being loaded from the +/// database. +/// +/// After account get loaded from database it can be in a lot of different states +/// while we execute multiple transaction and even blocks over account that is in memory. +/// This structure models all possible states that account can be in. +/// +/// # Variants +/// +/// - `LoadedNotExisting`: the account has been loaded but does not exist. +/// - `Loaded`: the account has been loaded and exists. +/// - `LoadedEmptyEIP161`: the account is loaded and empty, as per EIP-161. +/// - `InMemoryChange`: there are changes in the account that exist only in memory. +/// - `Changed`: the account has been modified. +/// - `Destroyed`: the account has been destroyed. +/// - `DestroyedChanged`: the account has been destroyed and then modified. +/// - `DestroyedAgain`: the account has been destroyed again. +#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum AccountStatus { + /// The account has been loaded but does not exist. + #[default] + LoadedNotExisting, + /// The account has been loaded and exists. + Loaded, + /// The account is loaded and empty, as per EIP-161. + LoadedEmptyEIP161, + /// There are changes in the account that exist only in memory. + InMemoryChange, + /// The account has been modified. + Changed, + /// The account has been destroyed. + Destroyed, + /// The account has been destroyed and then modified. + DestroyedChanged, + /// The account has been destroyed again. + DestroyedAgain, +} + +impl AccountStatus { + /// Account is not modified and just loaded from database. + pub const fn is_not_modified(&self) -> bool { + matches!(self, Self::LoadedNotExisting | Self::Loaded | Self::LoadedEmptyEIP161) + } + + /// Account was destroyed by calling SELFDESTRUCT. + /// This means that full account and storage are inside memory. + pub const fn was_destroyed(&self) -> bool { + matches!(self, Self::Destroyed | Self::DestroyedChanged | Self::DestroyedAgain) + } + + /// This means storage is known, it can be newly created or storage got destroyed. + pub const fn is_storage_known(&self) -> bool { + matches!( + self, + Self::LoadedNotExisting + | Self::InMemoryChange + | Self::Destroyed + | Self::DestroyedChanged + | Self::DestroyedAgain + ) + } + + /// Account is modified but not destroyed. + /// This means that some storage values can be found in both + /// memory and database. + pub const fn is_modified_and_not_destroyed(&self) -> bool { + matches!(self, Self::Changed | Self::InMemoryChange) + } + + /// Returns the next account status on creation. + pub const fn on_created(&self) -> Self { + match self { + // If account was destroyed previously just copy new info to it. + Self::DestroyedAgain + | Self::Destroyed + | Self::DestroyedChanged => Self::DestroyedChanged, + // If account is loaded from db. + Self::LoadedNotExisting + // Loaded empty eip161 to creates is not possible as CREATE2 was added after EIP-161 + | Self::LoadedEmptyEIP161 + | Self::Loaded + | Self::Changed + | Self::InMemoryChange => { + // If account is loaded and not empty this means that account has some balance. + // This means that account cannot be created. + // We are assuming that EVM did necessary checks before allowing account to be created. + Self::InMemoryChange + } + } + } + + /// Returns the next account status on touched empty account post state clear EIP (EIP-161). + pub const fn on_touched_empty_post_eip161(&self) -> Self { + match self { + // Account can be touched but not existing. The status should remain the same. + Self::LoadedNotExisting => Self::LoadedNotExisting, + // Account can be created empty and only then touched. + Self::InMemoryChange | Self::Destroyed | Self::LoadedEmptyEIP161 => Self::Destroyed, + // Transition to destroy the account. + Self::DestroyedAgain | Self::DestroyedChanged => Self::DestroyedAgain, + // Account can become empty. + Self::Changed | Self::Loaded => Self::Destroyed, + } + } + + /// Returns the next account status on change. + pub const fn on_changed(&self, had_no_nonce_and_code: bool) -> Self { + match self { + // If the account was loaded as not existing, promote it to changed. + // This account was likely created by a balance transfer. + Self::LoadedNotExisting => Self::InMemoryChange, + // Change on empty account, should transfer storage if there is any. + // There is possibility that there are storage entries inside db. + // That storage is used in merkle tree calculation before state clear EIP. + Self::LoadedEmptyEIP161 => Self::InMemoryChange, + // The account was loaded as existing. + Self::Loaded => { + if had_no_nonce_and_code { + // Account is fully in memory + Self::InMemoryChange + } else { + // Can be contract and some of storage slots can be present inside db. + Self::Changed + } + } + + // On change, the "changed" type account statuses are preserved. + // Any checks for empty accounts are done outside of this fn. + Self::Changed => Self::Changed, + Self::InMemoryChange => Self::InMemoryChange, + Self::DestroyedChanged => Self::DestroyedChanged, + + // If account is destroyed and then changed this means this is + // balance transfer. + Self::Destroyed | Self::DestroyedAgain => Self::DestroyedChanged, + } + } + + /// Returns the next account status on selfdestruct. + pub const fn on_selfdestructed(&self) -> Self { + match self { + // Non existing account can't be destroyed. + Self::LoadedNotExisting => Self::LoadedNotExisting, + // If account is created and selfdestructed in the same block, mark it as destroyed + // again. Note: There is no big difference between Destroyed and + // DestroyedAgain in this case, but was added for clarity. + Self::DestroyedChanged | Self::DestroyedAgain | Self::Destroyed => Self::DestroyedAgain, + + // Transition to destroyed status. + _ => Self::Destroyed, + } + } + + /// Transition to other state while preserving invariance of this state. + /// + /// It this account was Destroyed and other account is not: + /// - We should mark extended account as destroyed too. + /// - And as other account had some changes, extended account should be marked as + /// DestroyedChanged. + /// + /// If both account are not destroyed and if this account is in memory: + /// - This means that extended account is in memory too. + /// + /// Otherwise, if both are destroyed or other is destroyed: + /// - Sets other status to extended account. + pub fn transition(&mut self, other: Self) { + *self = match (self.was_destroyed(), other.was_destroyed()) { + (true, false) => Self::DestroyedChanged, + (false, false) if *self == Self::InMemoryChange => Self::InMemoryChange, + _ => other, + }; + } +} + +#[cfg(test)] +mod test { + + use super::*; + + #[test] + fn test_account_status() { + // Account not modified + assert!(AccountStatus::Loaded.is_not_modified()); + assert!(AccountStatus::LoadedEmptyEIP161.is_not_modified()); + assert!(AccountStatus::LoadedNotExisting.is_not_modified()); + assert!(!AccountStatus::Changed.is_not_modified()); + assert!(!AccountStatus::InMemoryChange.is_not_modified()); + assert!(!AccountStatus::Destroyed.is_not_modified()); + assert!(!AccountStatus::DestroyedChanged.is_not_modified()); + assert!(!AccountStatus::DestroyedAgain.is_not_modified()); + + // We know full storage + assert!(!AccountStatus::LoadedEmptyEIP161.is_storage_known()); + assert!(AccountStatus::LoadedNotExisting.is_storage_known()); + assert!(AccountStatus::InMemoryChange.is_storage_known()); + assert!(AccountStatus::Destroyed.is_storage_known()); + assert!(AccountStatus::DestroyedChanged.is_storage_known()); + assert!(AccountStatus::DestroyedAgain.is_storage_known()); + assert!(!AccountStatus::Loaded.is_storage_known()); + assert!(!AccountStatus::Changed.is_storage_known()); + + // Account was destroyed + assert!(!AccountStatus::LoadedEmptyEIP161.was_destroyed()); + assert!(!AccountStatus::LoadedNotExisting.was_destroyed()); + assert!(!AccountStatus::InMemoryChange.was_destroyed()); + assert!(AccountStatus::Destroyed.was_destroyed()); + assert!(AccountStatus::DestroyedChanged.was_destroyed()); + assert!(AccountStatus::DestroyedAgain.was_destroyed()); + assert!(!AccountStatus::Loaded.was_destroyed()); + assert!(!AccountStatus::Changed.was_destroyed()); + + // Account modified but not destroyed + assert!(AccountStatus::Changed.is_modified_and_not_destroyed()); + assert!(AccountStatus::InMemoryChange.is_modified_and_not_destroyed()); + assert!(!AccountStatus::Loaded.is_modified_and_not_destroyed()); + assert!(!AccountStatus::LoadedEmptyEIP161.is_modified_and_not_destroyed()); + assert!(!AccountStatus::LoadedNotExisting.is_modified_and_not_destroyed()); + assert!(!AccountStatus::Destroyed.is_modified_and_not_destroyed()); + assert!(!AccountStatus::DestroyedChanged.is_modified_and_not_destroyed()); + assert!(!AccountStatus::DestroyedAgain.is_modified_and_not_destroyed()); + } + + #[test] + fn test_on_created() { + assert_eq!(AccountStatus::Destroyed.on_created(), AccountStatus::DestroyedChanged); + assert_eq!(AccountStatus::DestroyedAgain.on_created(), AccountStatus::DestroyedChanged); + assert_eq!(AccountStatus::DestroyedChanged.on_created(), AccountStatus::DestroyedChanged); + + assert_eq!(AccountStatus::LoadedNotExisting.on_created(), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::Loaded.on_created(), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::LoadedEmptyEIP161.on_created(), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::Changed.on_created(), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::InMemoryChange.on_created(), AccountStatus::InMemoryChange); + } + + #[test] + fn test_on_touched_empty_post_eip161() { + assert_eq!( + AccountStatus::LoadedNotExisting.on_touched_empty_post_eip161(), + AccountStatus::LoadedNotExisting + ); + assert_eq!( + AccountStatus::InMemoryChange.on_touched_empty_post_eip161(), + AccountStatus::Destroyed + ); + assert_eq!( + AccountStatus::Destroyed.on_touched_empty_post_eip161(), + AccountStatus::Destroyed + ); + assert_eq!( + AccountStatus::LoadedEmptyEIP161.on_touched_empty_post_eip161(), + AccountStatus::Destroyed + ); + assert_eq!( + AccountStatus::DestroyedAgain.on_touched_empty_post_eip161(), + AccountStatus::DestroyedAgain + ); + assert_eq!( + AccountStatus::DestroyedChanged.on_touched_empty_post_eip161(), + AccountStatus::DestroyedAgain + ); + assert_eq!(AccountStatus::Loaded.on_touched_empty_post_eip161(), AccountStatus::Destroyed); + assert_eq!(AccountStatus::Changed.on_touched_empty_post_eip161(), AccountStatus::Destroyed); + } + + #[test] + fn test_on_changed() { + assert_eq!( + AccountStatus::LoadedNotExisting.on_changed(true), + AccountStatus::InMemoryChange + ); + assert_eq!( + AccountStatus::LoadedNotExisting.on_changed(false), + AccountStatus::InMemoryChange + ); + + assert_eq!( + AccountStatus::LoadedEmptyEIP161.on_changed(true), + AccountStatus::InMemoryChange + ); + assert_eq!( + AccountStatus::LoadedEmptyEIP161.on_changed(false), + AccountStatus::InMemoryChange + ); + + assert_eq!(AccountStatus::Loaded.on_changed(true), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::Loaded.on_changed(false), AccountStatus::Changed); + + assert_eq!(AccountStatus::Changed.on_changed(true), AccountStatus::Changed); + assert_eq!(AccountStatus::Changed.on_changed(false), AccountStatus::Changed); + + assert_eq!(AccountStatus::InMemoryChange.on_changed(true), AccountStatus::InMemoryChange); + assert_eq!(AccountStatus::InMemoryChange.on_changed(false), AccountStatus::InMemoryChange); + + assert_eq!( + AccountStatus::DestroyedChanged.on_changed(true), + AccountStatus::DestroyedChanged + ); + assert_eq!( + AccountStatus::DestroyedChanged.on_changed(false), + AccountStatus::DestroyedChanged + ); + + assert_eq!(AccountStatus::Destroyed.on_changed(true), AccountStatus::DestroyedChanged); + assert_eq!(AccountStatus::Destroyed.on_changed(false), AccountStatus::DestroyedChanged); + + assert_eq!(AccountStatus::DestroyedAgain.on_changed(true), AccountStatus::DestroyedChanged); + assert_eq!( + AccountStatus::DestroyedAgain.on_changed(false), + AccountStatus::DestroyedChanged + ); + } + + #[test] + fn test_on_selfdestructed() { + assert_eq!( + AccountStatus::LoadedNotExisting.on_selfdestructed(), + AccountStatus::LoadedNotExisting + ); + + assert_eq!( + AccountStatus::DestroyedChanged.on_selfdestructed(), + AccountStatus::DestroyedAgain + ); + assert_eq!( + AccountStatus::DestroyedAgain.on_selfdestructed(), + AccountStatus::DestroyedAgain + ); + assert_eq!(AccountStatus::Destroyed.on_selfdestructed(), AccountStatus::DestroyedAgain); + + assert_eq!(AccountStatus::Loaded.on_selfdestructed(), AccountStatus::Destroyed); + assert_eq!(AccountStatus::LoadedEmptyEIP161.on_selfdestructed(), AccountStatus::Destroyed); + assert_eq!(AccountStatus::InMemoryChange.on_selfdestructed(), AccountStatus::Destroyed); + assert_eq!(AccountStatus::Changed.on_selfdestructed(), AccountStatus::Destroyed); + } + + #[test] + fn test_transition() { + let mut status = AccountStatus::Destroyed; + status.transition(AccountStatus::Loaded); + assert_eq!(status, AccountStatus::DestroyedChanged); + + let mut status = AccountStatus::DestroyedChanged; + status.transition(AccountStatus::InMemoryChange); + assert_eq!(status, AccountStatus::DestroyedChanged); + + let mut status = AccountStatus::DestroyedAgain; + status.transition(AccountStatus::Changed); + assert_eq!(status, AccountStatus::DestroyedChanged); + + let mut status = AccountStatus::InMemoryChange; + status.transition(AccountStatus::Loaded); + assert_eq!(status, AccountStatus::InMemoryChange); + + let mut status = AccountStatus::InMemoryChange; + status.transition(AccountStatus::Changed); + assert_eq!(status, AccountStatus::InMemoryChange); + + let mut status = AccountStatus::Loaded; + status.transition(AccountStatus::Changed); + assert_eq!(status, AccountStatus::Changed); + + let mut status = AccountStatus::LoadedNotExisting; + status.transition(AccountStatus::InMemoryChange); + assert_eq!(status, AccountStatus::InMemoryChange); + + let mut status = AccountStatus::LoadedEmptyEIP161; + status.transition(AccountStatus::Loaded); + assert_eq!(status, AccountStatus::Loaded); + + let mut status = AccountStatus::Destroyed; + status.transition(AccountStatus::DestroyedChanged); + assert_eq!(status, AccountStatus::DestroyedChanged); + + let mut status = AccountStatus::DestroyedAgain; + status.transition(AccountStatus::Destroyed); + assert_eq!(status, AccountStatus::Destroyed); + + let mut status = AccountStatus::Loaded; + status.transition(AccountStatus::Destroyed); + assert_eq!(status, AccountStatus::Destroyed); + + let mut status = AccountStatus::Changed; + status.transition(AccountStatus::DestroyedAgain); + assert_eq!(status, AccountStatus::DestroyedAgain); + } +} diff --git a/crates/evm2/src/block/bundle_state/bundle_account.rs b/crates/evm2/src/block/bundle_state/bundle_account.rs new file mode 100644 index 000000000..11563bc97 --- /dev/null +++ b/crates/evm2/src/block/bundle_state/bundle_account.rs @@ -0,0 +1,384 @@ +use super::{ + AccountRevert, AccountStatus, RevertToSlot, StorageSlot, StorageWithOriginalValues, + TransitionAccount, reverts::AccountInfoRevert, +}; +use crate::evm::AccountInfo; +use alloy_primitives::{ + U256, + map::{HashMap, U256Map}, +}; + +/// Account information focused on creating of database changesets +/// and Reverts. +/// +/// Status is needed as to know from what state we are applying the TransitionAccount. +/// +/// Original account info is needed to know if there was a change. +/// +/// Same thing for storage with original value. +/// +/// On selfdestruct storage original value is ignored. +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct BundleAccount { + /// Current account information. + pub info: Option, + /// Original account information before modifications. + pub original_info: Option, + /// Contains both original and present state. + /// When extracting changeset we compare if original value is different from present value. + /// If it is different we add it to changeset. + /// + /// If Account was destroyed we ignore original value and compare present state with + /// U256::ZERO. + pub storage: StorageWithOriginalValues, + /// Account status. + pub status: AccountStatus, +} + +impl BundleAccount { + /// Create new BundleAccount. + pub const fn new( + original_info: Option, + present_info: Option, + storage: StorageWithOriginalValues, + status: AccountStatus, + ) -> Self { + Self { info: present_info, original_info, storage, status } + } + + /// The approximate size of changes needed to store this account. + /// + /// `1 + storage_len` + pub fn size_hint(&self) -> usize { + 1 + self.storage.len() + } + + /// Return storage slot if it exists. + /// + /// In case we know that account is newly created or destroyed, return + /// `Some(U256::ZERO)` + pub fn storage_slot(&self, slot: U256) -> Option { + let slot = self.storage.get(&slot).map(|s| s.present_value()); + if slot.is_some() { + slot + } else if self.status.is_storage_known() { + Some(U256::ZERO) + } else { + None + } + } + + /// Fetch account info if it exists. + pub fn account_info(&self) -> Option { + self.info.clone() + } + + /// Was this account destroyed. + pub const fn was_destroyed(&self) -> bool { + self.status.was_destroyed() + } + + /// Return true of account info was changed. + pub fn is_info_changed(&self) -> bool { + self.info != self.original_info + } + + /// Return true if contract was changed + pub fn is_contract_changed(&self) -> bool { + self.info.as_ref().map(|a| a.code_hash) != self.original_info.as_ref().map(|a| a.code_hash) + } + + /// Revert account to previous state and return true if account can be removed. + pub fn revert(&mut self, revert: AccountRevert) -> bool { + self.status = revert.previous_status; + + match revert.account { + AccountInfoRevert::DoNothing => (), + AccountInfoRevert::DeleteIt => { + self.info = None; + if self.original_info.is_none() { + self.storage = HashMap::default(); + return true; + } else { + // Set all storage to zero but preserve original values. + self.storage.iter_mut().for_each(|(_, v)| { + v.present_value = U256::ZERO; + }); + return false; + } + } + AccountInfoRevert::RevertTo(info) => self.info = Some(info), + }; + // Revert storage + for (key, slot) in revert.storage { + match slot { + RevertToSlot::Some(value) => { + // Don't overwrite original values if present + // if storage is not present set original value as current value. + self.storage + .entry(key) + .or_insert_with(|| StorageSlot::new(value)) + .present_value = value; + } + RevertToSlot::Destroyed => { + // If it was destroyed this means that storage was created and we need to remove + // it. + self.storage.remove(&key); + } + } + } + false + } + + /// Update to new state and generate AccountRevert that if applied to new state will + /// revert it to previous state. + /// + /// If no revert is present, update is noop. + pub fn update_and_create_revert( + &mut self, + transition: TransitionAccount, + ) -> Option { + let updated_info = transition.info; + let updated_storage = transition.storage; + let updated_status = transition.status; + + // The helper that extends this storage but preserves original value. + let extend_storage = + |this_storage: &mut StorageWithOriginalValues, + storage_update: StorageWithOriginalValues| { + this_storage.reserve(storage_update.len()); + for (key, value) in storage_update { + this_storage.entry(key).or_insert(value).present_value = value.present_value(); + } + }; + + let previous_storage_from_update = + |updated_storage: &StorageWithOriginalValues| -> U256Map { + updated_storage + .iter() + .filter(|s| s.1.is_changed()) + .map(|(key, value)| (*key, RevertToSlot::Some(value.original_value()))) + .collect() + }; + + // Needed for some reverts. + let info_revert = if self.info != updated_info { + AccountInfoRevert::RevertTo(self.info.clone().unwrap_or_default()) + } else { + AccountInfoRevert::DoNothing + }; + + let account_revert = match updated_status { + AccountStatus::Changed => { + let previous_storage = previous_storage_from_update(&updated_storage); + match self.status { + AccountStatus::Changed | AccountStatus::Loaded => { + // Extend the storage. original values is not used inside bundle. + extend_storage(&mut self.storage, updated_storage); + } + AccountStatus::LoadedEmptyEIP161 => { + // Do nothing. + // Only change that can happen from LoadedEmpty to Changed is if balance + // is send to account. So we are only checking account change here. + } + _ => unreachable!("Invalid state transfer to Changed from {self:?}"), + }; + let previous_status = self.status; + self.status = AccountStatus::Changed; + self.info = updated_info; + Some(AccountRevert { + account: info_revert, + storage: previous_storage, + previous_status, + wipe_storage: false, + }) + } + AccountStatus::InMemoryChange => { + let previous_storage = previous_storage_from_update(&updated_storage); + let in_memory_info_revert = match self.status { + AccountStatus::Loaded | AccountStatus::InMemoryChange => { + // From loaded (Or LoadedEmpty) to InMemoryChange can happen if there is + // balance change or new created account but Loaded + // didn't have contract. + extend_storage(&mut self.storage, updated_storage); + info_revert + } + AccountStatus::LoadedEmptyEIP161 => { + self.storage = updated_storage; + info_revert + } + AccountStatus::LoadedNotExisting => { + self.storage = updated_storage; + AccountInfoRevert::DeleteIt + } + _ => unreachable!("Invalid change to InMemoryChange from {self:?}"), + }; + let previous_status = self.status; + self.status = AccountStatus::InMemoryChange; + self.info = updated_info; + Some(AccountRevert { + account: in_memory_info_revert, + storage: previous_storage, + previous_status, + wipe_storage: false, + }) + } + AccountStatus::Loaded + | AccountStatus::LoadedNotExisting + | AccountStatus::LoadedEmptyEIP161 => { + // No changeset, maybe just update data + // Do nothing for now. + None + } + AccountStatus::Destroyed => { + // Clear this storage and move it to the Revert. + let this_storage = core::mem::take(&mut self.storage); + let ret = match self.status { + AccountStatus::InMemoryChange + | AccountStatus::Changed + | AccountStatus::Loaded + | AccountStatus::LoadedEmptyEIP161 => Some(AccountRevert::new_selfdestructed( + self.status, + info_revert, + this_storage, + )), + AccountStatus::LoadedNotExisting => { + // Do nothing as we have LoadedNotExisting -> Destroyed (It is noop) + None + } + _ => unreachable!( + "Invalid transition to Destroyed account from: {self:?} to {updated_info:?} {updated_status:?}" + ), + }; + + if ret.is_some() { + self.status = AccountStatus::Destroyed; + self.info = None; + } + + // Set present to destroyed. + ret + } + AccountStatus::DestroyedChanged => { + // Previous block created account or changed. + // (It was destroyed on previous block or one before). + + // Check common pre destroy paths. + // If common path is there it will drain the storage. + if let Some(revert_state) = AccountRevert::new_selfdestructed_from_bundle( + info_revert.clone(), + self, + &updated_storage, + ) { + // Set to destroyed and revert state. + self.status = AccountStatus::DestroyedChanged; + self.info = updated_info; + self.storage = updated_storage; + + Some(revert_state) + } else { + let ret = match self.status { + AccountStatus::Destroyed | AccountStatus::LoadedNotExisting => { + // From destroyed state new account is made + Some(AccountRevert { + account: AccountInfoRevert::DeleteIt, + storage: previous_storage_from_update(&updated_storage), + previous_status: self.status, + wipe_storage: false, + }) + } + AccountStatus::DestroyedChanged => { + // Account was destroyed in this transition. So we should clear present + // storage and insert it inside revert. + + let previous_storage = if transition.storage_was_destroyed { + let mut storage = core::mem::take(&mut self.storage) + .into_iter() + .map(|t| (t.0, RevertToSlot::Some(t.1.present_value()))) + .collect::>(); + for key in updated_storage.keys() { + // As it is not existing inside Destroyed storage this means + // that previous values must be zero + storage.entry(*key).or_insert(RevertToSlot::Destroyed); + } + storage + } else { + previous_storage_from_update(&updated_storage) + }; + + Some(AccountRevert { + account: info_revert, + storage: previous_storage, + previous_status: AccountStatus::DestroyedChanged, + wipe_storage: false, + }) + } + AccountStatus::DestroyedAgain => { + Some(AccountRevert::new_selfdestructed_again( + // Destroyed again will set empty account. + AccountStatus::DestroyedAgain, + AccountInfoRevert::DeleteIt, + HashMap::default(), + updated_storage.clone(), + )) + } + _ => unreachable!("Invalid state transfer to DestroyedNew from {self:?}"), + }; + self.status = AccountStatus::DestroyedChanged; + self.info = updated_info; + // Extends current storage. + extend_storage(&mut self.storage, updated_storage); + + ret + } + } + AccountStatus::DestroyedAgain => { + // Previous block created account + // (It was destroyed on previous block or one before). + + // Check common pre destroy paths. + // This will drain the storage if it is common transition. + let ret = if let Some(revert_state) = AccountRevert::new_selfdestructed_from_bundle( + info_revert, + self, + &HashMap::default(), + ) { + Some(revert_state) + } else { + match self.status { + AccountStatus::Destroyed + | AccountStatus::DestroyedAgain + | AccountStatus::LoadedNotExisting => { + // From destroyed to destroyed again is noop + // + // From DestroyedAgain to DestroyedAgain is noop + // + // From LoadedNotExisting to DestroyedAgain is noop + // as account is destroyed again + None + } + AccountStatus::DestroyedChanged => { + // From destroyed changed to destroyed again. + Some(AccountRevert::new_selfdestructed_again( + // Destroyed again will set empty account. + AccountStatus::DestroyedChanged, + AccountInfoRevert::RevertTo(self.info.clone().unwrap_or_default()), + core::mem::take(&mut self.storage), + HashMap::default(), + )) + } + _ => unreachable!("Invalid state to DestroyedAgain from {self:?}"), + } + }; + // Set to destroyed and revert state. + self.status = AccountStatus::DestroyedAgain; + self.info = None; + self.storage.clear(); + ret + } + }; + + account_revert.filter(|acc| !acc.is_empty()) + } +} diff --git a/crates/evm2/src/block/bundle_state/bundle_state.rs b/crates/evm2/src/block/bundle_state/bundle_state.rs new file mode 100644 index 000000000..ac033a35a --- /dev/null +++ b/crates/evm2/src/block/bundle_state/bundle_state.rs @@ -0,0 +1,1389 @@ +use super::{ + AccountRevert, AccountStatus, BundleAccount, PlainStateReverts, RevertToSlot, StorageSlot, + TransitionState, + changes::{PlainStorageChangeset, StateChangeset}, + reverts::{AccountInfoRevert, Reverts}, +}; +use crate::{bytecode::Bytecode, evm::AccountInfo}; +use alloc::{ + collections::{BTreeMap, BTreeSet}, + vec::Vec, +}; +use alloy_primitives::{ + Address, B256, KECCAK256_EMPTY, U256, + map::{AddressMap, AddressSet, B256Map, HashMap, U256Map, hash_map::Entry}, +}; +use core::{mem, ops::RangeInclusive}; + +/// This builder is used to help to facilitate the initialization of `BundleState` struct +#[derive(Debug)] +pub struct BundleBuilder { + states: AddressSet, + state_original: AddressMap, + state_present: AddressMap, + state_storage: AddressMap>, + + reverts: BTreeSet<(u64, Address)>, + revert_range: RangeInclusive, + revert_account: HashMap<(u64, Address), Option>>, + revert_storage: HashMap<(u64, Address), Vec<(U256, U256)>>, + + contracts: B256Map, +} + +/// Option for [`BundleState`] when converting it to the plain state. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum OriginalValuesKnown { + /// Checks changed with original values that [`BundleState`] has. + /// + /// If we don't expect parent blocks to be committed or unwinded from database, this option + /// should be used. + Yes, + /// Doesn't check original values, see the implementation of [`BundleState::to_plain_state`] + /// for more info. + /// + /// If the Bundle can be split or extended, we would not be sure about original values, in that + /// case this option should be used. + No, +} +impl OriginalValuesKnown { + /// Original value is not known for sure. + pub const fn is_not_known(&self) -> bool { + matches!(self, Self::No) + } +} + +impl Default for BundleBuilder { + fn default() -> Self { + BundleBuilder { + states: AddressSet::default(), + state_original: AddressMap::default(), + state_present: AddressMap::default(), + state_storage: AddressMap::default(), + reverts: BTreeSet::new(), + revert_range: 0..=0, + revert_account: HashMap::default(), + revert_storage: HashMap::default(), + contracts: B256Map::default(), + } + } +} + +impl BundleBuilder { + /// Creates builder instance. + /// + /// `revert_range` indicates the size of BundleState `reverts` field. + pub fn new(revert_range: RangeInclusive) -> Self { + BundleBuilder { revert_range, ..Default::default() } + } + + /// Applies a transformation to the builder. + pub fn apply(self, f: F) -> Self + where + F: FnOnce(Self) -> Self, + { + f(self) + } + + /// Applies a mutable transformation to the builder. + pub fn apply_mut(&mut self, f: F) -> &mut Self + where + F: FnOnce(&mut Self), + { + f(self); + self + } + + /// Collects address info of BundleState state. + pub fn state_address(mut self, address: Address) -> Self { + self.set_state_address(address); + self + } + + /// Collects account info of BundleState state. + pub fn state_original_account_info(mut self, address: Address, original: AccountInfo) -> Self { + self.set_state_original_account_info(address, original); + self + } + + /// Collects account info of BundleState state. + pub fn state_present_account_info(mut self, address: Address, present: AccountInfo) -> Self { + self.set_state_present_account_info(address, present); + self + } + + /// Collects storage info of BundleState state. + pub fn state_storage(mut self, address: Address, storage: U256Map<(U256, U256)>) -> Self { + self.set_state_storage(address, storage); + self + } + + /// Collects address info of BundleState reverts. + /// + /// `block_number` must respect `revert_range`, or the input + /// will be ignored during the final build process. + pub fn revert_address(mut self, block_number: u64, address: Address) -> Self { + self.set_revert_address(block_number, address); + self + } + + /// Collects account info of BundleState reverts. + /// + /// `block_number` must respect `revert_range`, or the input + /// will be ignored during the final build process. + pub fn revert_account_info( + mut self, + block_number: u64, + address: Address, + account: Option>, + ) -> Self { + self.set_revert_account_info(block_number, address, account); + self + } + + /// Collects storage info of BundleState reverts. + /// + /// `block_number` must respect `revert_range`, or the input + /// will be ignored during the final build process. + pub fn revert_storage( + mut self, + block_number: u64, + address: Address, + storage: Vec<(U256, U256)>, + ) -> Self { + self.set_revert_storage(block_number, address, storage); + self + } + + /// Collects contracts info. + pub fn contract(mut self, address: B256, bytecode: Bytecode) -> Self { + self.set_contract(address, bytecode); + self + } + + /// Sets address info of BundleState state. + pub fn set_state_address(&mut self, address: Address) -> &mut Self { + self.states.insert(address); + self + } + + /// Sets original account info of BundleState state. + pub fn set_state_original_account_info( + &mut self, + address: Address, + original: AccountInfo, + ) -> &mut Self { + self.states.insert(address); + self.state_original.insert(address, original); + self + } + + /// Sets present account info of BundleState state. + pub fn set_state_present_account_info( + &mut self, + address: Address, + present: AccountInfo, + ) -> &mut Self { + self.states.insert(address); + self.state_present.insert(address, present); + self + } + + /// Sets storage info of BundleState state. + pub fn set_state_storage( + &mut self, + address: Address, + storage: U256Map<(U256, U256)>, + ) -> &mut Self { + self.states.insert(address); + self.state_storage.insert(address, storage); + self + } + + /// Sets address info of BundleState reverts. + pub fn set_revert_address(&mut self, block_number: u64, address: Address) -> &mut Self { + self.reverts.insert((block_number, address)); + self + } + + /// Sets account info of BundleState reverts. + pub fn set_revert_account_info( + &mut self, + block_number: u64, + address: Address, + account: Option>, + ) -> &mut Self { + self.reverts.insert((block_number, address)); + self.revert_account.insert((block_number, address), account); + self + } + + /// Sets storage info of BundleState reverts. + pub fn set_revert_storage( + &mut self, + block_number: u64, + address: Address, + storage: Vec<(U256, U256)>, + ) -> &mut Self { + self.reverts.insert((block_number, address)); + self.revert_storage.insert((block_number, address), storage); + self + } + + /// Sets contracts info. + pub fn set_contract(&mut self, address: B256, bytecode: Bytecode) -> &mut Self { + self.contracts.insert(address, bytecode); + self + } + + /// Creates `BundleState` instance based on collected information. + pub fn build(mut self) -> BundleState { + let mut state_size = 0; + let state = self + .states + .into_iter() + .map(|address| { + let storage = self + .state_storage + .remove(&address) + .map(|s| { + s.into_iter() + .map(|(k, (o_val, p_val))| (k, StorageSlot::new_changed(o_val, p_val))) + .collect() + }) + .unwrap_or_default(); + let bundle_account = BundleAccount::new( + self.state_original.remove(&address), + self.state_present.remove(&address), + storage, + AccountStatus::Changed, + ); + state_size += bundle_account.size_hint(); + (address, bundle_account) + }) + .collect(); + + let mut reverts_size = 0; + let mut reverts_map = BTreeMap::new(); + for block_number in self.revert_range { + reverts_map.insert(block_number, Vec::new()); + } + self.reverts.into_iter().for_each(|(block_number, address)| { + let account = + match self.revert_account.remove(&(block_number, address)).unwrap_or_default() { + Some(Some(account)) => AccountInfoRevert::RevertTo(account), + Some(None) => AccountInfoRevert::DeleteIt, + None => AccountInfoRevert::DoNothing, + }; + let storage = self + .revert_storage + .remove(&(block_number, address)) + .map(|s| s.into_iter().map(|(k, v)| (k, RevertToSlot::Some(v))).collect()) + .unwrap_or_default(); + let account_revert = AccountRevert { + account, + storage, + previous_status: AccountStatus::Changed, + wipe_storage: false, + }; + + if let Some(vec) = reverts_map.get_mut(&block_number) { + reverts_size += account_revert.size_hint(); + vec.push((address, account_revert)); + } + }); + + BundleState { + state, + contracts: self.contracts, + reverts: Reverts::new(reverts_map.into_values().collect()), + state_size, + reverts_size, + } + } + + /// Getter for `states` field + pub const fn get_states(&self) -> &AddressSet { + &self.states + } + + /// Mutable getter for `states` field + pub const fn get_states_mut(&mut self) -> &mut AddressSet { + &mut self.states + } + + /// Mutable getter for `state_original` field + pub const fn get_state_original_mut(&mut self) -> &mut AddressMap { + &mut self.state_original + } + + /// Mutable getter for `state_present` field + pub const fn get_state_present_mut(&mut self) -> &mut AddressMap { + &mut self.state_present + } + + /// Mutable getter for `state_storage` field + pub const fn get_state_storage_mut(&mut self) -> &mut AddressMap> { + &mut self.state_storage + } + + /// Mutable getter for `reverts` field + pub const fn get_reverts_mut(&mut self) -> &mut BTreeSet<(u64, Address)> { + &mut self.reverts + } + + /// Mutable getter for `revert_range` field + pub const fn get_revert_range_mut(&mut self) -> &mut RangeInclusive { + &mut self.revert_range + } + + /// Mutable getter for `revert_account` field + pub const fn get_revert_account_mut( + &mut self, + ) -> &mut HashMap<(u64, Address), Option>> { + &mut self.revert_account + } + + /// Mutable getter for `revert_storage` field + pub const fn get_revert_storage_mut( + &mut self, + ) -> &mut HashMap<(u64, Address), Vec<(U256, U256)>> { + &mut self.revert_storage + } + + /// Mutable getter for `contracts` field + pub const fn get_contracts_mut(&mut self) -> &mut B256Map { + &mut self.contracts + } +} + +/// Bundle retention policy for applying substate to the bundle. +#[derive(Debug)] +pub enum BundleRetention { + /// Only plain state is updated. + PlainState, + /// Both, plain state and reverts, are retained + Reverts, +} + +impl BundleRetention { + /// Returns `true` if reverts should be retained. + pub const fn includes_reverts(&self) -> bool { + matches!(self, Self::Reverts) + } +} + +/// Bundle state contain only values that got changed +/// +/// For every account it contains both original and present state. +/// +/// This is needed to decide if there were any changes to the account. +/// +/// Changes are applied and reverts are created when TransitionState is applied to BundleState. +/// +/// And can be used to revert BundleState to the state before transition. +#[derive(Default, Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct BundleState { + /// Account state + pub state: AddressMap, + /// All created contracts in this block. + pub contracts: B256Map, + /// Changes to revert + /// + /// **Note**: Inside vector is *not* sorted by address. + /// + /// But it is unique by address. + pub reverts: Reverts, + /// The size of the plain state in the bundle state + pub state_size: usize, + /// The size of reverts in the bundle state + pub reverts_size: usize, +} + +impl BundleState { + /// Returns builder instance for further manipulation. + pub fn builder(revert_range: RangeInclusive) -> BundleBuilder { + BundleBuilder::new(revert_range) + } + + /// Creates it with new and old values of both Storage and AccountInfo. + pub fn new( + state: impl IntoIterator< + Item = (Address, Option, Option, HashMap), + >, + reverts: impl IntoIterator< + Item = impl IntoIterator< + Item = ( + Address, + Option>, + impl IntoIterator, + ), + >, + >, + contracts: impl IntoIterator, + ) -> Self { + // Create state from iterator. + let mut state_size = 0; + let state = state + .into_iter() + .map(|(address, original, present, storage)| { + let account = BundleAccount::new( + original, + present, + storage + .into_iter() + .map(|(k, (o_val, p_val))| (k, StorageSlot::new_changed(o_val, p_val))) + .collect(), + AccountStatus::Changed, + ); + state_size += account.size_hint(); + (address, account) + }) + .collect(); + + // Create reverts from iterator. + let mut reverts_size = 0; + let reverts = reverts + .into_iter() + .map(|block_reverts| { + block_reverts + .into_iter() + .map(|(address, account, storage)| { + let account = match account { + Some(Some(account)) => AccountInfoRevert::RevertTo(account), + Some(None) => AccountInfoRevert::DeleteIt, + None => AccountInfoRevert::DoNothing, + }; + let revert = AccountRevert { + account, + storage: storage + .into_iter() + .map(|(k, v)| (k, RevertToSlot::Some(v))) + .collect(), + previous_status: AccountStatus::Changed, + wipe_storage: false, + }; + reverts_size += revert.size_hint(); + (address, revert) + }) + .collect::>() + }) + .collect::>(); + + Self { + state, + contracts: contracts.into_iter().collect(), + reverts: Reverts::new(reverts), + state_size, + reverts_size, + } + } + + /// Returns the approximate size of changes in the bundle state. + /// + /// The estimation is not precise, because the information about the number of + /// destroyed entries that need to be removed is not accessible to the bundle state. + pub fn size_hint(&self) -> usize { + self.state_size + self.reverts_size + self.contracts.len() + } + + /// Returns reference to the state. + pub const fn state(&self) -> &AddressMap { + &self.state + } + + /// Checks whether bundle state is empty. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Return number of changed accounts. + pub fn len(&self) -> usize { + self.state.len() + } + + /// Gets account from state. + pub fn account(&self, address: &Address) -> Option<&BundleAccount> { + self.state.get(address) + } + + /// Gets bytecode from state. + pub fn bytecode(&self, hash: &B256) -> Option { + self.contracts.get(hash).cloned() + } + + /// Gets storage value from state. + /// + /// Returns `None` if the account is not present in the bundle state or the slot is not known. + pub fn storage(&self, address: &Address, storage_key: U256) -> Option { + self.account(address).and_then(|account| account.storage_slot(storage_key)) + } + + /// Consumes [`TransitionState`] by applying the changes and creating the + /// reverts. + /// + /// If [`BundleRetention::includes_reverts`] is `true`, then the reverts will + /// be retained. + pub fn apply_transitions_and_create_reverts( + &mut self, + transitions: TransitionState, + retention: BundleRetention, + ) { + let include_reverts = retention.includes_reverts(); + // Pessimistically pre-allocate assuming _all_ accounts changed. + let reverts_capacity = if include_reverts { transitions.transitions.len() } else { 0 }; + let mut reverts = Vec::with_capacity(reverts_capacity); + + self.state.reserve(transitions.transitions.len()); + for (address, transition) in transitions.transitions.into_iter() { + // Add new contract if it was created/changed. + if let Some((hash, new_bytecode)) = transition.has_new_contract() { + self.contracts.insert(hash, new_bytecode.clone()); + } + // Update state and create revert. + let revert = match self.state.entry(address) { + Entry::Occupied(mut entry) => { + let entry = entry.get_mut(); + self.state_size -= entry.size_hint(); + // Update and create revert if it is present + let revert = entry.update_and_create_revert(transition); + // Update the state size + self.state_size += entry.size_hint(); + revert + } + Entry::Vacant(entry) => { + // Make revert from transition account + let present_bundle = transition.present_bundle_account(); + let revert = transition.create_revert(); + if revert.is_some() { + self.state_size += present_bundle.size_hint(); + entry.insert(present_bundle); + } + revert + } + }; + + // Append revert if present. + if let Some(revert) = revert.filter(|_| include_reverts) { + self.reverts_size += revert.size_hint(); + reverts.push((address, revert)); + } + } + + self.reverts.push(reverts); + } + + /// Generate a [`StateChangeset`] from the bundle state without consuming + /// it. + pub fn to_plain_state(&self, is_value_known: OriginalValuesKnown) -> StateChangeset { + // Pessimistically pre-allocate assuming _all_ accounts changed. + let state_len = self.state.len(); + let mut accounts = Vec::with_capacity(state_len); + let mut storage = Vec::with_capacity(state_len); + + for (address, account) in &self.state { + // Append account info if it is changed. + let was_destroyed = account.was_destroyed(); + if is_value_known.is_not_known() || account.is_info_changed() { + let info = + account.info.as_ref().map(|info| AccountInfo { code: None, ..info.clone() }); + accounts.push((*address, info)); + } + + // Append storage changes + + // Note: Assumption is that revert is going to remove whole plain storage from + // database so we can check if plain state was wiped or not. + let mut account_storage_changed = Vec::with_capacity(account.storage.len()); + + for (&key, &slot) in account.storage.iter() { + // If storage was destroyed that means that storage was wiped. + // In that case we need to check if present storage value is different then ZERO. + let destroyed_and_not_zero = was_destroyed && !slot.present_value().is_zero(); + + // If account is not destroyed check if original values was changed, + // so we can update it. + let not_destroyed_and_changed = !was_destroyed && slot.is_changed(); + + if is_value_known.is_not_known() + || destroyed_and_not_zero + || not_destroyed_and_changed + { + account_storage_changed.push((key, slot.present_value())); + } + } + + if !account_storage_changed.is_empty() || was_destroyed { + // Append storage changes to account. + storage.push(PlainStorageChangeset { + address: *address, + wipe_storage: was_destroyed, + storage: account_storage_changed, + }); + } + } + + let contracts = self + .contracts + .iter() + // Remove empty bytecodes + .filter(|(b, _)| **b != KECCAK256_EMPTY) + .map(|(b, code)| (*b, code.clone())) + .collect::>(); + StateChangeset { accounts, storage, contracts } + } + + /// Generates a [`StateChangeset`] and [`PlainStateReverts`] from the bundle + /// state. + pub fn to_plain_state_and_reverts( + &self, + is_value_known: OriginalValuesKnown, + ) -> (StateChangeset, PlainStateReverts) { + (self.to_plain_state(is_value_known), self.reverts.to_plain_state_reverts()) + } + + /// Consumes the bundle state and split it into a [`StateChangeset`] and a + /// [`PlainStateReverts`]. + #[deprecated = "Use `to_plain_state_and_reverts` instead"] + pub fn into_plain_state_and_reverts( + self, + is_value_known: OriginalValuesKnown, + ) -> (StateChangeset, PlainStateReverts) { + self.to_plain_state_and_reverts(is_value_known) + } + + /// Extends the bundle with other state. + /// + /// Updates the `other` state only if `other` is not flagged as destroyed. + pub fn extend_state(&mut self, other_state: AddressMap) { + self.state.reserve(other_state.len()); + for (address, other_account) in other_state { + match self.state.entry(address) { + Entry::Occupied(mut entry) => { + let this = entry.get_mut(); + self.state_size -= this.size_hint(); + + // If other was destroyed. replace `this` storage with + // the `other one. + if other_account.was_destroyed() { + this.storage = other_account.storage; + } else { + // Otherwise extend this storage with other + this.storage.reserve(other_account.storage.len()); + for (key, storage_slot) in other_account.storage { + // Update present value or insert storage slot. + this.storage.entry(key).or_insert(storage_slot).present_value = + storage_slot.present_value(); + } + } + this.info = other_account.info; + this.status.transition(other_account.status); + + // Update the state size + self.state_size += this.size_hint(); + } + Entry::Vacant(entry) => { + // Just insert if empty + self.state_size += other_account.size_hint(); + entry.insert(other_account); + } + } + } + } + /// Extends the state with state that is build on top of it. + /// + /// If storage was wiped in `other` state, copy `this` plain state + /// and put it inside `other` revert (if there is no duplicates of course). + /// + /// If `this` and `other` accounts were both destroyed invalidate second + /// wipe flag (from `other`). + /// + /// As wiping from database should be done only once + /// and we already transferred all potentially missing storages to the `other` revert. + pub fn extend(&mut self, mut other: Self) { + // Iterate over reverts and if its storage is wiped try to add previous bundle + // state as there is potential missing slots. + let mut seen = AddressSet::default(); + for (address, revert) in other.reverts.iter_mut().flatten() { + // `other` was built on a fresh bundle, so its first revert for an account records the + // account as freshly loaded. Revert to the status this bundle accumulated instead. + // Later reverts record statuses local to `other`, so they combine with it. + if let Some(this_account) = self.state.get(address) { + if seen.insert(*address) { + revert.previous_status = this_account.status; + } else { + let mut status = this_account.status; + status.transition(revert.previous_status); + revert.previous_status = status; + } + } + + if revert.wipe_storage { + // If there is wipe storage in `other` revert + // we need to move storage from present state. + if let Some(this_account) = self.state.get_mut(address) { + // As this account was destroyed inside `other` bundle. + // We are fine to wipe/drain this storage and put it inside revert. + // A `Destroyed` slot in `other` means the value before the wipe, which is + // this bundle's present value, not the database value. + for (key, value) in this_account.storage.drain() { + let previous = RevertToSlot::Some(value.present_value()); + revert + .storage + .entry(key) + .and_modify(|slot| { + if *slot == RevertToSlot::Destroyed { + *slot = previous; + } + }) + .or_insert(previous); + } + + // Nullify `other` wipe as primary database wipe is done in `this`. + if this_account.was_destroyed() { + revert.wipe_storage = false; + } + } + } + + // Increment reverts size for each of the updated reverts. + self.reverts_size += revert.size_hint(); + } + // Extension of state + self.extend_state(other.state); + // Contract can be just extended, when counter is introduced we will take into account that. + self.contracts.extend(other.contracts); + // Reverts can be just extended + self.reverts.extend(other.reverts); + } + + /// Takes first N raw reverts from the [`BundleState`]. + pub fn take_n_reverts(&mut self, reverts_to_take: usize) -> Reverts { + // Split is done as [0, num) and [num, len]. + if reverts_to_take > self.reverts.len() { + return self.take_all_reverts(); + } + let this = self.reverts.split_off(reverts_to_take); + self.reverts_size = + this.iter().flatten().fold(0, |acc, (_, revert)| acc + revert.size_hint()); + mem::replace(&mut self.reverts, Reverts::new(this)) + } + + /// Returns and clears all reverts from [`BundleState`]. + pub fn take_all_reverts(&mut self) -> Reverts { + self.reverts_size = 0; + mem::take(&mut self.reverts) + } + + /// Reverts the state changes of the latest transition. + /// + /// **Note**: This is the same as `BundleState::revert(1)` + /// + /// Returns true if the state was reverted. + pub fn revert_latest(&mut self) -> bool { + // Revert the latest recorded state + if let Some(reverts) = self.reverts.pop() { + for (address, revert_account) in reverts.into_iter() { + self.reverts_size -= revert_account.size_hint(); + match self.state.entry(address) { + Entry::Occupied(mut entry) => { + let account = entry.get_mut(); + self.state_size -= account.size_hint(); + if account.revert(revert_account) { + entry.remove(); + } else { + self.state_size += account.size_hint(); + } + } + Entry::Vacant(entry) => { + // Create empty account that we will revert on. + // Only place where this account is not existing is if revert is DeleteIt. + let mut account = BundleAccount::new( + None, + None, + HashMap::default(), + AccountStatus::LoadedNotExisting, + ); + if !account.revert(revert_account) { + self.state_size += account.size_hint(); + entry.insert(account); + } + } + } + } + return true; + } + + false + } + + /// Reverts the state changes by N transitions back. + /// + /// See also [`Self::revert_latest`] + pub fn revert(&mut self, mut num_transitions: usize) { + if num_transitions == 0 { + return; + } + + while self.revert_latest() { + num_transitions -= 1; + if num_transitions == 0 { + // Break the loop. + break; + } + } + } + + /// Prepends present the state with the given [`BundleState`]. + /// + /// It adds changes from the given state but does not override any existing changes. + /// + /// The given state's reverts are discarded; this bundle keeps its own. + pub fn prepend_state(&mut self, mut other: BundleState) { + let other_reverts = other.reverts.len(); + // Take this bundle + let this_bundle = mem::take(self); + // Extend other bundle with this + other.extend(this_bundle); + // Discard other's reverts, which come first + other.take_n_reverts(other_reverts); + // Swap bundles + mem::swap(self, &mut other) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::block::bundle_state::{StorageWithOriginalValues, TransitionAccount}; + use alloc::vec; + + #[test] + fn transition_states() { + // Dummy data + let address = Address::repeat_byte(0x01); + let acc1 = AccountInfo { balance: U256::from(10), nonce: 1, ..Default::default() }; + + let mut bundle_state = BundleState::default(); + + // Have transition from loaded to all other states + + let transition = TransitionAccount { + info: Some(acc1), + ..TransitionAccount::new_empty_eip161(StorageWithOriginalValues::default()) + }; + + // Apply first transition + bundle_state.apply_transitions_and_create_reverts( + TransitionState::single(address, transition.clone()), + BundleRetention::Reverts, + ); + } + + const fn account1() -> Address { + Address::repeat_byte(0x60) + } + + const fn account2() -> Address { + Address::repeat_byte(0x61) + } + + fn slot1() -> U256 { + U256::from(5) + } + + fn slot2() -> U256 { + U256::from(7) + } + + /// Tests bundle one. + fn test_bundle1() -> BundleState { + // Block changes + BundleState::new( + vec![ + ( + account1(), + None, + Some(AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }), + HashMap::from_iter([ + (slot1(), (U256::ZERO, U256::from(10))), + (slot2(), (U256::ZERO, U256::from(15))), + ]), + ), + ( + account2(), + None, + Some(AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }), + HashMap::default(), + ), + ], + vec![vec![ + (account1(), Some(None), vec![(slot1(), U256::ZERO), (slot2(), U256::ZERO)]), + (account2(), Some(None), vec![]), + ]], + vec![], + ) + } + + /// Tests bundle two. + fn test_bundle2() -> BundleState { + // Block changes + BundleState::new( + vec![( + account1(), + None, + Some(AccountInfo { nonce: 3, balance: U256::from(20), ..Default::default() }), + HashMap::from_iter([(slot1(), (U256::ZERO, U256::from(15)))]), + )], + vec![vec![( + account1(), + Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() })), + vec![(slot1(), U256::from(10))], + )]], + vec![], + ) + } + + /// Tests bundle three. + fn test_bundle3() -> BundleState { + BundleState::builder(0..=0) + .state_present_account_info( + account1(), + AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }, + ) + .state_storage( + account1(), + HashMap::from_iter([(slot1(), (U256::ZERO, U256::from(10)))]), + ) + .state_address(account2()) + .state_present_account_info( + account2(), + AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }, + ) + .revert_address(0, account1()) + .revert_account_info(0, account1(), Some(None)) + .revert_storage(0, account1(), vec![(slot1(), U256::ZERO)]) + .revert_account_info(0, account2(), Some(None)) + .build() + } + + /// Tests bundle four. + fn test_bundle4() -> BundleState { + BundleState::builder(0..=0) + .state_present_account_info( + account1(), + AccountInfo { nonce: 3, balance: U256::from(20), ..Default::default() }, + ) + .state_storage( + account1(), + HashMap::from_iter([(slot1(), (U256::ZERO, U256::from(15)))]), + ) + .revert_address(0, account1()) + .revert_account_info( + 0, + account1(), + Some(Some(AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() })), + ) + .revert_storage(0, account1(), vec![(slot1(), U256::from(10))]) + .build() + } + + /// Per-block bundles are built on a fresh bundle, so merging them must still revert an account + /// to the status accumulated over the earlier blocks. + #[test] + fn extend_reverts_to_accumulated_status() { + let address = Address::repeat_byte(0x70); + let info = AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }; + let destroy = TransitionAccount { + info: None, + status: AccountStatus::Destroyed, + previous_info: Some(info.clone()), + previous_status: AccountStatus::Loaded, + storage: HashMap::from_iter([( + slot1(), + StorageSlot::new_changed(U256::from(3), U256::ZERO), + )]), + storage_was_destroyed: true, + }; + let recreate = TransitionAccount { + info: Some(info), + ..TransitionAccount::new_empty_eip161(HashMap::from_iter([( + slot2(), + StorageSlot::new_changed(U256::ZERO, U256::from(5)), + )])) + }; + let block_bundle = |transition| { + let mut bundle = BundleState::default(); + bundle.apply_transitions_and_create_reverts( + TransitionState::single(address, transition), + BundleRetention::Reverts, + ); + bundle + }; + + let first = block_bundle(destroy); + let mut merged = first.clone(); + merged.extend(block_bundle(recreate.clone())); + + // A single bundle built over both blocks tracks the status across them. + let mut cumulative = first.clone(); + cumulative.apply_transitions_and_create_reverts( + TransitionState::single( + address, + TransitionAccount { + status: AccountStatus::DestroyedChanged, + previous_status: AccountStatus::Destroyed, + ..recreate + }, + ), + BundleRetention::Reverts, + ); + assert_eq!(merged.state[&address].status, AccountStatus::DestroyedChanged); + assert_eq!(merged.reverts, cumulative.reverts); + + merged.revert(1); + cumulative.revert(1); + let account = &merged.state[&address]; + assert_eq!(account.status, AccountStatus::Destroyed); + assert!(account.was_destroyed()); + assert_eq!(merged, cumulative); + } + + /// Every block of an extending bundle reverts to the status accumulated over the earlier + /// blocks, not only the first one. + #[test] + fn extend_reverts_later_blocks_to_accumulated_status() { + let address = Address::repeat_byte(0x71); + let info = AccountInfo { nonce: 1, balance: U256::from(10), ..Default::default() }; + let changed = AccountInfo { balance: U256::from(11), ..info.clone() }; + let transition = |previous: &AccountInfo, next: &AccountInfo, from, to| TransitionAccount { + info: Some(next.clone()), + status: to, + previous_info: Some(previous.clone()), + previous_status: from, + storage: HashMap::default(), + storage_was_destroyed: false, + }; + let block_bundle = |transitions: &[TransitionAccount]| { + let mut bundle = BundleState::default(); + for transition in transitions { + bundle.apply_transitions_and_create_reverts( + TransitionState::single(address, transition.clone()), + BundleRetention::Reverts, + ); + } + bundle + }; + let destroy = TransitionAccount { + info: None, + previous_info: Some(info.clone()), + ..transition(&info, &info, AccountStatus::Loaded, AccountStatus::Destroyed) + }; + let recreate = TransitionAccount { + previous_info: None, + ..transition( + &info, + &info, + AccountStatus::LoadedNotExisting, + AccountStatus::InMemoryChange, + ) + }; + let change = transition( + &info, + &changed, + AccountStatus::InMemoryChange, + AccountStatus::InMemoryChange, + ); + + let mut merged = block_bundle(core::slice::from_ref(&destroy)); + merged.extend(block_bundle(&[recreate.clone(), change.clone()])); + + // A single bundle built over every block tracks the status across them. + let mut cumulative = block_bundle(&[destroy]); + for (previous, transition) in + [(AccountStatus::Destroyed, recreate), (AccountStatus::DestroyedChanged, change)] + { + cumulative.apply_transitions_and_create_reverts( + TransitionState::single( + address, + TransitionAccount { + status: AccountStatus::DestroyedChanged, + previous_status: previous, + ..transition + }, + ), + BundleRetention::Reverts, + ); + } + assert_eq!(merged.reverts, cumulative.reverts); + + merged.revert(1); + cumulative.revert(1); + assert_eq!(merged.state[&address].status, AccountStatus::DestroyedChanged); + assert_eq!(merged, cumulative); + } + + fn sanity_path(bundle1: BundleState, bundle2: BundleState) { + let mut extended = bundle1.clone(); + extended.extend(bundle2.clone()); + + let mut reverted = extended.clone(); + // Revert zero does nothing. + reverted.revert(0); + assert_eq!(reverted, extended); + + // Revert by one gives us bundle one. + reverted.revert(1); + assert_eq!(reverted, bundle1); + + // Reverted by additional one gives us empty bundle. + reverted.revert(1); + assert_eq!(reverted, BundleState::default()); + + let mut reverted = extended.clone(); + + // Reverted by bigger number gives us empty bundle + reverted.revert(10); + assert_eq!(reverted, BundleState::default()); + } + + #[test] + fn extend_on_destroyed_values() { + let base_bundle1 = test_bundle1(); + let base_bundle2 = test_bundle2(); + + // test1 + // bundle1 has Destroyed + // bundle2 has Changed + // end should be DestroyedChanged. + let mut b1 = base_bundle1.clone(); + let mut b2 = base_bundle2.clone(); + b1.state.get_mut(&account1()).unwrap().status = AccountStatus::Destroyed; + b2.state.get_mut(&account1()).unwrap().status = AccountStatus::Changed; + b1.extend(b2); + assert_eq!(b1.state.get_mut(&account1()).unwrap().status, AccountStatus::DestroyedChanged); + + // test2 + // bundle1 has Changed + // bundle2 has Destroyed + // end should be Destroyed + let mut b1 = base_bundle1.clone(); + let mut b2 = base_bundle2.clone(); + b1.state.get_mut(&account1()).unwrap().status = AccountStatus::Changed; + b2.state.get_mut(&account1()).unwrap().status = AccountStatus::Destroyed; + b2.reverts[0][0].1.wipe_storage = true; + b1.extend(b2); + assert_eq!(b1.state.get_mut(&account1()).unwrap().status, AccountStatus::Destroyed); + + // test2 extension + // revert of b2 should contains plain state of b1. + let mut revert1 = base_bundle2.reverts[0][0].clone(); + revert1.1.wipe_storage = true; + revert1.1.storage.insert(slot2(), RevertToSlot::Some(U256::from(15))); + + assert_eq!(b1.reverts.as_ref(), vec![base_bundle1.reverts[0].clone(), vec![revert1]],); + + // test3 + // bundle1 has InMemoryChange + // bundle2 has Change + // end should be InMemoryChange. + + let mut b1 = base_bundle1.clone(); + let mut b2 = base_bundle2.clone(); + b1.state.get_mut(&account1()).unwrap().status = AccountStatus::InMemoryChange; + b2.state.get_mut(&account1()).unwrap().status = AccountStatus::Changed; + b1.extend(b2); + assert_eq!(b1.state.get_mut(&account1()).unwrap().status, AccountStatus::InMemoryChange); + } + + #[test] + fn extend_replaces_destroyed_revert_with_previous_value() { + // bundle1 writes slot1 = 10 and slot2 = 15; bundle2 selfdestructs and re-creates + // account1, so its revert wipes storage and marks slot1 as destroyed. + let mut b1 = test_bundle1(); + let mut b2 = test_bundle2(); + b2.state.get_mut(&account1()).unwrap().status = AccountStatus::DestroyedChanged; + let revert = &mut b2.reverts[0][0].1; + revert.wipe_storage = true; + revert.storage.insert(slot1(), RevertToSlot::Destroyed); + + b1.extend(b2); + let revert = &b1.reverts[1][0].1; + assert!(revert.wipe_storage); + assert_eq!(revert.storage.get(&slot1()), Some(&RevertToSlot::Some(U256::from(10)))); + assert_eq!(revert.storage.get(&slot2()), Some(&RevertToSlot::Some(U256::from(15)))); + } + + #[test] + fn test_sanity_path() { + sanity_path(test_bundle1(), test_bundle2()); + sanity_path(test_bundle3(), test_bundle4()); + } + + #[test] + fn test_multi_reverts_with_delete() { + let mut state = BundleState::builder(0..=3) + .revert_address(0, account1()) + .revert_account_info(2, account1(), Some(Some(AccountInfo::default()))) + .revert_account_info(3, account1(), Some(None)) + .build(); + + state.revert_latest(); + // State for account one was deleted + assert_eq!(state.account(&account1()), None); + + state.revert_latest(); + // State is set to + assert_eq!( + state.account(&account1()), + Some(&BundleAccount::new( + None, + Some(AccountInfo::default()), + HashMap::default(), + AccountStatus::Changed + )) + ); + } + + #[test] + fn test_revert_capacity() { + let state = BundleState::builder(0..=3) + .revert_address(0, account1()) + .revert_address(2, account2()) + .revert_account_info(0, account1(), Some(None)) + .revert_account_info(2, account2(), None) + .revert_storage(0, account1(), vec![(slot1(), U256::from(10))]) + .build(); + + assert_eq!(state.reverts.len(), 4); + assert_eq!(state.reverts[1], vec![]); + assert_eq!(state.reverts[3], vec![]); + assert_eq!(state.reverts[0].len(), 1); + assert_eq!(state.reverts[2].len(), 1); + + let (addr1, revert1) = &state.reverts[0][0]; + assert_eq!(addr1, &account1()); + assert_eq!(revert1.account, AccountInfoRevert::DeleteIt); + + let (addr2, revert2) = &state.reverts[2][0]; + assert_eq!(addr2, &account2()); + assert_eq!(revert2.account, AccountInfoRevert::DoNothing); + } + + #[test] + fn take_reverts() { + let bundle1 = test_bundle1(); + let bundle2 = test_bundle2(); + + let mut extended = bundle1.clone(); + extended.extend(bundle2.clone()); + + // Check that we have two reverts + assert_eq!(extended.reverts.len(), 2); + + // Take all by big N + let mut extended2 = extended.clone(); + assert_eq!(extended2.take_n_reverts(100), extended.reverts); + + // Take all reverts + let mut extended2 = extended.clone(); + assert_eq!(extended2.take_all_reverts(), extended.reverts); + + // Take zero revert + let taken_reverts = extended.take_n_reverts(0); + assert_eq!(taken_reverts, Reverts::default()); + assert_eq!(extended.reverts.len(), 2); + + // Take one revert + let taken_reverts = extended.take_n_reverts(1); + assert_eq!(taken_reverts, bundle1.reverts); + + // Take last revert + let taken_reverts = extended.take_n_reverts(1); + assert_eq!(taken_reverts, bundle2.reverts); + } + + #[test] + fn prepend_state() { + let address1 = account1(); + let address2 = account2(); + + let account1 = AccountInfo { nonce: 1, ..Default::default() }; + let account1_changed = AccountInfo { nonce: 1, ..Default::default() }; + let account2 = AccountInfo { nonce: 1, ..Default::default() }; + + let present_state = BundleState::builder(2..=2) + .state_present_account_info(address1, account1_changed.clone()) + .revert_account_info(2, address1, Some(None)) + .build(); + assert_eq!(present_state.reverts.len(), 1); + let present_reverts = present_state.reverts.clone(); + let previous_state = BundleState::builder(1..=1) + .state_present_account_info(address1, account1) + .state_present_account_info(address2, account2.clone()) + .revert_account_info(1, address2, Some(None)) + .build(); + assert_eq!(previous_state.reverts.len(), 1); + + let mut test = present_state; + + test.prepend_state(previous_state); + + assert_eq!(test.len(), 2); + // The receiver keeps its own reverts. + assert_eq!(test.reverts, present_reverts); + // Account1 is not overwritten. + assert_eq!(test.account(&address1).unwrap().info, Some(account1_changed)); + // Account2 got inserted + assert_eq!(test.account(&address2).unwrap().info, Some(account2)); + } + + #[test] + fn test_getters() { + let mut builder = BundleState::builder(0..=3); + + // Test get_states and get_states_mut + assert!(builder.get_states().is_empty()); + builder.get_states_mut().insert(account1()); + assert!(builder.get_states().contains(&account1())); + + // Test get_state_original_mut + assert!(builder.get_state_original_mut().is_empty()); + builder.get_state_original_mut().insert(account1(), AccountInfo::default()); + assert!(builder.get_state_original_mut().contains_key(&account1())); + + // Test get_state_present_mut + assert!(builder.get_state_present_mut().is_empty()); + builder.get_state_present_mut().insert(account1(), AccountInfo::default()); + assert!(builder.get_state_present_mut().contains_key(&account1())); + + // Test get_state_storage_mut + assert!(builder.get_state_storage_mut().is_empty()); + builder.get_state_storage_mut().insert(account1(), HashMap::default()); + assert!(builder.get_state_storage_mut().contains_key(&account1())); + + // Test get_reverts_mut + assert!(builder.get_reverts_mut().is_empty()); + builder.get_reverts_mut().insert((0, account1())); + assert!(builder.get_reverts_mut().contains(&(0, account1()))); + + // Test get_revert_range_mut + assert_eq!(builder.get_revert_range_mut().clone(), 0..=3); + + // Test get_revert_account_mut + assert!(builder.get_revert_account_mut().is_empty()); + builder.get_revert_account_mut().insert((0, account1()), Some(None)); + assert!(builder.get_revert_account_mut().contains_key(&(0, account1()))); + + // Test get_revert_storage_mut + assert!(builder.get_revert_storage_mut().is_empty()); + builder.get_revert_storage_mut().insert((0, account1()), vec![(slot1(), U256::ZERO)]); + assert!(builder.get_revert_storage_mut().contains_key(&(0, account1()))); + + // Test get_contracts_mut + assert!(builder.get_contracts_mut().is_empty()); + builder.get_contracts_mut().insert(B256::default(), Bytecode::default()); + assert!(builder.get_contracts_mut().contains_key(&B256::default())); + } +} diff --git a/crates/evm2/src/block/bundle_state/changes.rs b/crates/evm2/src/block/bundle_state/changes.rs new file mode 100644 index 000000000..7c131520c --- /dev/null +++ b/crates/evm2/src/block/bundle_state/changes.rs @@ -0,0 +1,75 @@ +use super::RevertToSlot; +use crate::{bytecode::Bytecode, evm::AccountInfo}; +use alloc::vec::Vec; +use alloy_primitives::{Address, B256, U256}; + +/// `accounts`/`storages`/`contracts` for inclusion into database. +/// +/// Structure is made so it is easier to apply directly to database +/// that mostly have separate tables to store `accounts`/`storages`/`contracts` data. +/// +/// **Note**: That data is **not** sorted. Some database benefit of faster inclusion +/// and smaller footprint if data is inserted in sorted order. +#[derive(Clone, Debug, Default)] +pub struct StateChangeset { + /// Vector of **not** sorted accounts information. + pub accounts: Vec<(Address, Option)>, + /// Vector of **not** sorted storage. + pub storage: Vec, + /// Vector of contracts by bytecode hash. **not** sorted. + pub contracts: Vec<(B256, Bytecode)>, +} + +/// Plain storage changeset. +/// +/// Used to apply storage changes of plain state to the database. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub struct PlainStorageChangeset { + /// Address of account + pub address: Address, + /// Wipe storage + pub wipe_storage: bool, + /// Storage key value pairs + pub storage: Vec<(U256, U256)>, +} + +/// Plain Storage Revert. +/// +/// [`PlainStorageRevert`] contains old values of changed storage. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub struct PlainStorageRevert { + /// Address of account + pub address: Address, + /// Whether storage is wiped in this revert + /// + /// **Note**: Wiped flag is set on first known selfdestruct and would require clearing the + /// state of this storage from database (And moving it to revert). + pub wiped: bool, + /// Contains the storage key and old values of that storage + /// + /// **Note**: Reverts are **not** sorted. + pub storage_revert: Vec<(U256, RevertToSlot)>, +} + +/// Plain state reverts are used to easily store reverts into database. +/// +/// Note that accounts are assumed **not** sorted. +#[derive(Clone, Debug, Default)] +pub struct PlainStateReverts { + /// Vector of account with removed contracts bytecode. + /// + /// **Note**: If AccountInfo is None means that account needs to be removed. + pub accounts: Vec)>>, + /// Vector of storage with its address. + pub storage: Vec>, +} + +impl PlainStateReverts { + /// Constructs new [`PlainStateReverts`] with pre-allocated capacity. + pub fn with_capacity(capacity: usize) -> Self { + Self { accounts: Vec::with_capacity(capacity), storage: Vec::with_capacity(capacity) } + } +} + +/// Storage reverts +pub type StorageRevert = Vec)>>; diff --git a/crates/evm2/src/block/bundle_state/mod.rs b/crates/evm2/src/block/bundle_state/mod.rs new file mode 100644 index 000000000..3475865e5 --- /dev/null +++ b/crates/evm2/src/block/bundle_state/mod.rs @@ -0,0 +1,45 @@ +//! Block execution changes with the reverts needed to persist and unwind them. +//! +//! Ported from the `revm-database` crate of revm (), +//! Copyright (c) 2021-2026 draganrakita, licensed under the MIT License (see `LICENSES/MIT.txt`). +//! The code is kept close to upstream, so upstream style is allowed below. + +#![allow( + missing_copy_implementations, + clippy::collapsible_if, + clippy::doc_markdown, + clippy::explicit_into_iter_loop, + clippy::explicit_iter_loop, + clippy::if_not_else, + clippy::match_same_arms, + clippy::module_inception, + clippy::redundant_clone, + clippy::redundant_else, + clippy::use_self +)] + +mod account_status; +pub use account_status::AccountStatus; + +mod bundle_account; +pub use bundle_account::BundleAccount; + +mod bundle_state; +pub use bundle_state::{BundleBuilder, BundleRetention, BundleState, OriginalValuesKnown}; + +mod changes; +pub use changes::{ + PlainStateReverts, PlainStorageChangeset, PlainStorageRevert, StateChangeset, StorageRevert, +}; + +mod plain_account; +pub use plain_account::{PlainAccount, PlainStorage, StorageSlot, StorageWithOriginalValues}; + +mod reverts; +pub use reverts::{AccountInfoRevert, AccountRevert, RevertToSlot, Reverts}; + +mod transition_account; +pub use transition_account::TransitionAccount; + +mod transition_state; +pub use transition_state::TransitionState; diff --git a/crates/evm2/src/block/bundle_state/plain_account.rs b/crates/evm2/src/block/bundle_state/plain_account.rs new file mode 100644 index 000000000..84473d22a --- /dev/null +++ b/crates/evm2/src/block/bundle_state/plain_account.rs @@ -0,0 +1,82 @@ +use crate::evm::AccountInfo; +use alloy_primitives::{ + U256, + map::{HashMap, U256Map}, +}; + +/// Plain account of StateDatabase. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct PlainAccount { + /// Account information. + pub info: AccountInfo, + /// Account storage. + pub storage: PlainStorage, +} + +impl PlainAccount { + /// Creates a new empty account with the given storage. + pub fn new_empty_with_storage(storage: PlainStorage) -> Self { + Self { info: AccountInfo::default(), storage } + } + + /// Consumes the account and returns its components. + pub fn into_components(self) -> (AccountInfo, PlainStorage) { + (self.info, self.storage) + } +} + +/// This type keeps track of the current value of a storage slot. +#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct StorageSlot { + /// The value of the storage slot before it was changed. + /// + /// When the slot is first loaded, this is the original value. + /// + /// If the slot was not changed, this is equal to the present value. + pub previous_or_original_value: U256, + /// When loaded with sload present value is set to original value + pub present_value: U256, +} + +impl StorageSlot { + /// Creates a new _unchanged_ `StorageSlot` for the given value. + pub const fn new(original: U256) -> Self { + Self { previous_or_original_value: original, present_value: original } + } + + /// Creates a new _changed_ `StorageSlot`. + pub const fn new_changed(previous_or_original_value: U256, present_value: U256) -> Self { + Self { previous_or_original_value, present_value } + } + + /// Returns true if the present value differs from the original value + pub fn is_changed(&self) -> bool { + self.previous_or_original_value != self.present_value + } + + /// Returns the original value of the storage slot. + pub const fn original_value(&self) -> U256 { + self.previous_or_original_value + } + + /// Returns the current value of the storage slot. + pub const fn present_value(&self) -> U256 { + self.present_value + } +} + +/// This storage represent values that are before block changed. +/// +/// Note: Storage that we get EVM contains original values before block changed. +pub type StorageWithOriginalValues = U256Map; + +/// Simple plain storage that does not have previous value. +/// This is used for loading from database, cache and for bundle state. +pub type PlainStorage = U256Map; + +impl From for PlainAccount { + fn from(info: AccountInfo) -> Self { + Self { info, storage: HashMap::default() } + } +} diff --git a/crates/evm2/src/block/bundle_state/reverts.rs b/crates/evm2/src/block/bundle_state/reverts.rs new file mode 100644 index 000000000..7eff71826 --- /dev/null +++ b/crates/evm2/src/block/bundle_state/reverts.rs @@ -0,0 +1,375 @@ +use super::{ + AccountStatus, BundleAccount, PlainStateReverts, StorageWithOriginalValues, + changes::PlainStorageRevert, +}; +use crate::evm::AccountInfo; +use alloc::vec::Vec; +use alloy_primitives::{Address, U256, map::U256Map}; +use core::{ + cmp::Ordering, + ops::{Deref, DerefMut}, +}; + +/// Contains reverts of multiple account in multiple transitions (Transitions as a block). +#[derive(Clone, Debug, Default, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct Reverts(Vec>); + +impl Deref for Reverts { + type Target = Vec>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for Reverts { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl Reverts { + /// Creates new reverts. + pub const fn new(reverts: Vec>) -> Self { + Self(reverts) + } + + /// Sorts account inside transition by their address. + pub fn sort(&mut self) { + for revert in &mut self.0 { + revert.sort_by_key(|(address, _)| *address); + } + } + + /// Extends reverts with other reverts. + pub fn extend(&mut self, other: Reverts) { + self.0.extend(other.0); + } + + /// Generates a [`PlainStateReverts`]. + /// + /// Note that account are sorted by address. + pub fn to_plain_state_reverts(&self) -> PlainStateReverts { + let mut state_reverts = PlainStateReverts::with_capacity(self.0.len()); + for reverts in &self.0 { + // Pessimistically pre-allocate assuming _all_ accounts changed. + let mut accounts = Vec::with_capacity(reverts.len()); + let mut storage = Vec::with_capacity(reverts.len()); + for (address, revert_account) in reverts { + match &revert_account.account { + AccountInfoRevert::RevertTo(acc) => { + // Cloning is cheap, because account info has 3 small + // fields and a Bytes + accounts.push((*address, Some(acc.clone()))) + } + AccountInfoRevert::DeleteIt => accounts.push((*address, None)), + AccountInfoRevert::DoNothing => (), + } + if revert_account.wipe_storage || !revert_account.storage.is_empty() { + storage.push(PlainStorageRevert { + address: *address, + wiped: revert_account.wipe_storage, + storage_revert: revert_account + .storage + .iter() + .map(|(k, v)| (*k, *v)) + .collect::>(), + }); + } + } + state_reverts.accounts.push(accounts); + state_reverts.storage.push(storage); + } + state_reverts + } + + /// Compare two Reverts instances, ignoring the order of elements + pub fn content_eq(&self, other: &Self) -> bool { + if self.0.len() != other.0.len() { + return false; + } + + for (self_transition, other_transition) in self.0.iter().zip(other.0.iter()) { + if self_transition.len() != other_transition.len() { + return false; + } + + let mut self_transition = self_transition.clone(); + let mut other_transition = other_transition.clone(); + // Sort both transitions + self_transition.sort_by(|(addr1, revert1), (addr2, revert2)| { + addr1.cmp(addr2).then_with(|| revert1.cmp(revert2)) + }); + other_transition.sort_by(|(addr1, revert1), (addr2, revert2)| { + addr1.cmp(addr2).then_with(|| revert1.cmp(revert2)) + }); + + // Compare sorted transitions + if self_transition != other_transition { + return false; + } + } + + true + } + + /// Consume reverts and create [`PlainStateReverts`]. + /// + /// Note that account are sorted by address. + #[deprecated = "Use `to_plain_state_reverts` instead"] + pub fn into_plain_state_reverts(self) -> PlainStateReverts { + self.to_plain_state_reverts() + } +} + +impl PartialEq for Reverts { + fn eq(&self, other: &Self) -> bool { + self.content_eq(other) + } +} + +/// Assumption is that Revert can return full state from any future state to any past state. +/// +/// # Note +/// It is created when new account state is applied to old account state. +/// +/// And it is used to revert new account state to the old account state. +/// +/// [`AccountRevert`] is structured in this way as we need to save it inside database. +/// +/// And we need to be able to read it from database. +#[derive(Clone, Default, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct AccountRevert { + /// Account information revert. + pub account: AccountInfoRevert, + /// Storage slots to revert. + pub storage: U256Map, + /// Previous account status before the change. + pub previous_status: AccountStatus, + /// Whether to wipe the storage. + pub wipe_storage: bool, +} + +impl AccountRevert { + /// The approximate size of changes needed to store this account revert. + /// + /// `1 + storage_reverts_len` + pub fn size_hint(&self) -> usize { + 1 + self.storage.len() + } + + /// Very similar to new_selfdestructed but it will add additional zeros + /// ([`RevertToSlot::Destroyed`]) for the storage that are set if account is again created. + pub fn new_selfdestructed_again( + status: AccountStatus, + account: AccountInfoRevert, + mut previous_storage: StorageWithOriginalValues, + updated_storage: StorageWithOriginalValues, + ) -> Self { + // Take present storage values as the storages that we are going to revert to. + // As those values got destroyed. + let mut previous_storage: U256Map = previous_storage + .drain() + .map(|(key, value)| (key, RevertToSlot::Some(value.present_value()))) + .collect(); + for (key, _) in updated_storage { + previous_storage.entry(key).or_insert(RevertToSlot::Destroyed); + } + AccountRevert { + account, + storage: previous_storage, + previous_status: status, + wipe_storage: false, + } + } + + /// Creates revert for states that were before selfdestruct. + pub fn new_selfdestructed_from_bundle( + account_info_revert: AccountInfoRevert, + bundle_account: &mut BundleAccount, + updated_storage: &StorageWithOriginalValues, + ) -> Option { + match bundle_account.status { + AccountStatus::InMemoryChange + | AccountStatus::Changed + | AccountStatus::LoadedEmptyEIP161 + | AccountStatus::Loaded => { + let mut ret = AccountRevert::new_selfdestructed_again( + bundle_account.status, + account_info_revert, + core::mem::take(&mut bundle_account.storage), + updated_storage.clone(), + ); + ret.wipe_storage = true; + Some(ret) + } + _ => None, + } + } + + /// Create new selfdestruct revert. + pub fn new_selfdestructed( + status: AccountStatus, + account: AccountInfoRevert, + mut storage: StorageWithOriginalValues, + ) -> Self { + // Zero all present storage values and save present values to AccountRevert. + let previous_storage = storage + .iter_mut() + .map(|(key, value)| { + // Take previous value and set ZERO as storage got destroyed. + (*key, RevertToSlot::Some(value.present_value())) + }) + .collect(); + + Self { account, storage: previous_storage, previous_status: status, wipe_storage: true } + } + + /// Returns `true` if there is nothing to revert, + /// by checking that: + /// * both account info and storage have been left untouched + /// * we don't need to wipe storage + pub fn is_empty(&self) -> bool { + self.account == AccountInfoRevert::DoNothing + && self.storage.is_empty() + && !self.wipe_storage + } +} + +/// Implements partial ordering for AccountRevert +impl PartialOrd for AccountRevert { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +/// Implements total ordering for AccountRevert +impl Ord for AccountRevert { + fn cmp(&self, other: &Self) -> Ordering { + // First compare accounts + if let Some(ord) = self.account.partial_cmp(&other.account) { + if ord != Ordering::Equal { + return ord; + } + } + + // Convert HashMaps to sorted vectors for comparison + let mut self_storage: Vec<_> = self.storage.iter().collect(); + let mut other_storage: Vec<_> = other.storage.iter().collect(); + + // Sort by key and then by value + self_storage.sort_by(|(k1, v1), (k2, v2)| k1.cmp(k2).then_with(|| v1.cmp(v2))); + other_storage.sort_by(|(k1, v1), (k2, v2)| k1.cmp(k2).then_with(|| v1.cmp(v2))); + + // Compare each element + for (self_entry, other_entry) in self_storage.iter().zip(other_storage.iter()) { + let key_ord = self_entry.0.cmp(other_entry.0); + if key_ord != Ordering::Equal { + return key_ord; + } + let value_ord = self_entry.1.cmp(other_entry.1); + if value_ord != Ordering::Equal { + return value_ord; + } + } + + // If one vector is longer than the other, or if all elements are equal + self_storage + .len() + .cmp(&other_storage.len()) + .then_with(|| self.previous_status.cmp(&other.previous_status)) + .then_with(|| self.wipe_storage.cmp(&other.wipe_storage)) + } +} + +/// Depending on previous state of account info this +/// will tell us what to do on revert. +#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum AccountInfoRevert { + #[default] + /// Nothing changed + DoNothing, + /// Account was created and on revert we need to remove it with all storage. + DeleteIt, + /// Account was changed and on revert we need to put old state. + RevertTo(AccountInfo), +} + +impl PartialOrd for AccountInfoRevert { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +/// Orders by variant (`DoNothing`, `DeleteIt`, `RevertTo`), then by the [`AccountInfo`] fields that +/// its `Eq` compares. +impl Ord for AccountInfoRevert { + fn cmp(&self, other: &Self) -> Ordering { + match (self, other) { + (Self::RevertTo(a), Self::RevertTo(b)) => { + let order = a + .balance + .cmp(&b.balance) + .then_with(|| a.nonce.cmp(&b.nonce)) + .then_with(|| a.code_hash.cmp(&b.code_hash)); + #[cfg(feature = "account-ext")] + let order = order.then_with(|| a.extension.as_ref().cmp(b.extension.as_ref())); + order + } + (Self::DoNothing, Self::DoNothing) | (Self::DeleteIt, Self::DeleteIt) => { + Ordering::Equal + } + (Self::DoNothing, _) | (Self::DeleteIt, Self::RevertTo(_)) => Ordering::Less, + (_, Self::DoNothing) | (Self::RevertTo(_), Self::DeleteIt) => Ordering::Greater, + } + } +} + +/// So storage can have multiple types: +/// * Zero, on revert remove plain state. +/// * Value, on revert set this value +/// * Destroyed, should be removed on revert but on Revert set it as zero. +/// +/// **Note**: It is completely different state if Storage is Zero or Some or if Storage was +/// Destroyed. +/// +/// Because if it is destroyed, previous values can be found in database or it can be zero. +#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum RevertToSlot { + /// Revert to this value. + Some(U256), + /// Storage was destroyed. + Destroyed, +} + +impl RevertToSlot { + /// Returns the previous value to set on revert. + pub const fn to_previous_value(self) -> U256 { + match self { + RevertToSlot::Some(value) => value, + RevertToSlot::Destroyed => U256::ZERO, + } + } +} + +#[cfg(test)] +mod test { + use super::*; + use alloc::vec; + + #[test] + fn content_eq_ignores_order_of_same_address() { + let address = Address::with_last_byte(1); + let do_nothing = AccountRevert::default(); + let delete_it = + AccountRevert { account: AccountInfoRevert::DeleteIt, ..Default::default() }; + let a = + Reverts::new(vec![vec![(address, do_nothing.clone()), (address, delete_it.clone())]]); + let b = Reverts::new(vec![vec![(address, delete_it), (address, do_nothing)]]); + assert_eq!(a, b); + } +} diff --git a/crates/evm2/src/block/bundle_state/transition_account.rs b/crates/evm2/src/block/bundle_state/transition_account.rs new file mode 100644 index 000000000..5580943fe --- /dev/null +++ b/crates/evm2/src/block/bundle_state/transition_account.rs @@ -0,0 +1,112 @@ +use super::{AccountRevert, AccountStatus, BundleAccount, StorageWithOriginalValues}; +use crate::{bytecode::Bytecode, evm::AccountInfo}; +use alloy_primitives::{B256, U256}; + +/// Account Created when EVM state is merged to cache state. +/// And it is sent to Block state. +/// +/// It is used when block state gets merged to bundle state to +/// create needed Reverts. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub struct TransitionAccount { + /// Account information, if account exists. + pub info: Option, + /// Current account status. + pub status: AccountStatus, + /// Previous account info is needed for account that got initially loaded. + /// Initially loaded account are not present inside bundle and are needed + /// to generate Reverts. + pub previous_info: Option, + /// Mostly needed when previous status Loaded/LoadedEmpty. + pub previous_status: AccountStatus, + /// Storage contains both old and new account + pub storage: S, + /// If there is transition that clears the storage we should mark it here and + /// delete all storages in BundleState. This flag is needed if we have transition + /// between Destroyed states from DestroyedChanged-> DestroyedAgain-> DestroyedChanged + /// in the end transition that we would have would be `DestroyedChanged->DestroyedChanged` + /// and with only that info we couldn't decide what to do. + pub storage_was_destroyed: bool, +} + +impl TransitionAccount { + /// Create new LoadedEmpty account. + pub fn new_empty_eip161(storage: StorageWithOriginalValues) -> Self { + Self { + info: Some(AccountInfo::default()), + status: AccountStatus::InMemoryChange, + previous_info: None, + previous_status: AccountStatus::LoadedNotExisting, + storage, + storage_was_destroyed: false, + } + } + + /// Return new contract bytecode if it is changed or newly created. + pub fn has_new_contract(&self) -> Option<(B256, &Bytecode)> { + let present_new_codehash = self.info.as_ref().map(|info| &info.code_hash); + let previous_codehash = self.previous_info.as_ref().map(|info| &info.code_hash); + if present_new_codehash != previous_codehash { + return self + .info + .as_ref() + .and_then(|info| info.code.as_ref().map(|c| (info.code_hash, c))); + } + None + } + + /// Return the balance of account before transition. + pub fn previous_balance(&self) -> U256 { + self.previous_info.as_ref().map(|info| info.balance).unwrap_or_default() + } + + /// Return the balance of account after transition. + pub fn current_balance(&self) -> U256 { + self.info.as_ref().map(|info| info.balance).unwrap_or_default() + } + + /// Consume Self and create account revert from it. + pub fn create_revert(self) -> Option { + let mut previous_account = self.original_bundle_account(); + previous_account.update_and_create_revert(self) + } + + /// Present bundle account + pub fn present_bundle_account(&self) -> BundleAccount { + BundleAccount { + info: self.info.clone(), + original_info: self.previous_info.clone(), + storage: self.storage.clone(), + status: self.status, + } + } + + /// Original bundle account + fn original_bundle_account(&self) -> BundleAccount { + BundleAccount { + info: self.previous_info.clone(), + original_info: self.previous_info.clone(), + storage: StorageWithOriginalValues::default(), + status: self.previous_status, + } + } +} + +impl TransitionAccount { + /// Map the storage of the transition account. + pub fn map_storage(self, f: F) -> TransitionAccount + where + F: FnOnce(S) -> N, + { + let Self { info, status, previous_info, previous_status, storage, storage_was_destroyed } = + self; + TransitionAccount { + info, + status, + previous_info, + previous_status, + storage: f(storage), + storage_was_destroyed, + } + } +} diff --git a/crates/evm2/src/block/bundle_state/transition_state.rs b/crates/evm2/src/block/bundle_state/transition_state.rs new file mode 100644 index 000000000..8c6c45f07 --- /dev/null +++ b/crates/evm2/src/block/bundle_state/transition_state.rs @@ -0,0 +1,34 @@ +use super::TransitionAccount; +use alloy_primitives::{ + Address, + map::{AddressMap, HashMap}, +}; + +/// State of accounts in transition between transaction executions. +#[derive(Clone, Default, Debug, PartialEq, Eq)] +pub struct TransitionState { + /// Block state account with account state + pub transitions: AddressMap, +} + +impl TransitionState { + /// Create new transition state containing one [`TransitionAccount`]. + pub fn single(address: Address, transition: TransitionAccount) -> Self { + let mut transitions = HashMap::default(); + transitions.insert(address, transition); + TransitionState { transitions } + } + + /// Take the contents of this [`TransitionState`] and replace it with an + /// empty one. + /// + /// See [core::mem::take]. + pub fn take(&mut self) -> TransitionState { + core::mem::take(self) + } + + /// Clear the transition state. + pub fn clear(&mut self) { + self.transitions.clear(); + } +} diff --git a/crates/evm2/src/block/error.rs b/crates/evm2/src/block/error.rs new file mode 100644 index 000000000..f97aa8d98 --- /dev/null +++ b/crates/evm2/src/block/error.rs @@ -0,0 +1,266 @@ +//! Block execution errors. + +use crate::{AnyError, DatabaseError, registry::HandlerError}; +use alloc::{ + boxed::Box, + string::{String, ToString}, +}; +use alloy_primitives::B256; +use core::{any::Any, error::Error}; + +/// Abstraction over transaction validation errors. +pub trait InvalidTxError: Error + Send + Sync + Any + 'static { + /// Returns whether the transaction nonce is lower than expected. + fn is_nonce_too_low(&self) -> bool { + false + } + + /// Returns whether the transaction gas limit is higher than allowed. + fn is_gas_limit_too_high(&self) -> bool { + false + } + + /// Returns whether the transaction gas limit is lower than required. + fn is_gas_limit_too_low(&self) -> bool { + false + } + + /// Returns the error as [`Any`] for consumers that need engine-specific downcasts. + fn as_any(&self) -> &(dyn Any + 'static); +} + +/// Block validation error. +#[derive(Debug, thiserror::Error)] +pub enum BlockValidationError { + /// EVM error with transaction hash and message. + #[error("EVM reported invalid transaction ({hash}): {error}")] + InvalidTx { + /// The hash of the transaction. + hash: B256, + /// The EVM error. + error: Box, + }, + /// EVM error that is not a transaction validation error. + #[error("EVM execution failed: {error}")] + EVM { + /// The hash of the transaction. + hash: B256, + /// The EVM error. + error: AnyError, + }, + /// Error when incrementing balance in post execution. + #[error("incrementing balance in post execution failed")] + IncrementBalanceFailed, + /// Error when transaction gas limit exceeds available block gas. + #[error( + "transaction gas limit {transaction_gas_limit} is more than blocks available gas {block_available_gas}" + )] + TransactionGasLimitMoreThanAvailableBlockGas { + /// The transaction's gas limit. + transaction_gas_limit: u64, + /// The available block gas. + block_available_gas: u64, + }, + /// Error when transaction blob gas exceeds available block blob gas. + #[error( + "transaction blob gas {transaction_blob_gas} is more than blocks available blob gas {block_available_blob_gas}" + )] + TransactionBlobGasMoreThanAvailableBlockBlobGas { + /// The transaction's blob gas. + transaction_blob_gas: u64, + /// The available block blob gas. + block_available_blob_gas: u64, + }, + /// Error for EIP-4788 when parent beacon block root is missing. + #[error("EIP-4788 parent beacon block root missing for active Cancun block")] + MissingParentBeaconBlockRoot, + /// Error for Cancun genesis block when parent beacon block root is not zero. + #[error( + "the parent beacon block root is not zero for Cancun genesis block: {parent_beacon_block_root}" + )] + CancunGenesisParentBeaconBlockRootNotZero { + /// The beacon block root. + parent_beacon_block_root: B256, + }, + /// EVM error during EIP-4788 beacon root contract call. + #[error("failed to apply beacon root contract call at {parent_beacon_block_root}: {message}")] + BeaconRootContractCall { + /// The beacon block root. + parent_beacon_block_root: Box, + /// The error message. + message: String, + }, + /// EVM error during EIP-2935 blockhash contract call. + #[error("failed to apply blockhash contract call: {message}")] + BlockHashContractCall { + /// The error message. + message: String, + }, + /// EVM error during withdrawal requests contract call. + #[error("failed to apply withdrawal requests contract call: {message}")] + WithdrawalRequestsContractCall { + /// The error message. + message: String, + }, + /// EVM error during consolidation requests contract call. + #[error("failed to apply consolidation requests contract call: {message}")] + ConsolidationRequestsContractCall { + /// The error message. + message: String, + }, + /// Error when decoding deposit requests from receipts. + #[error("failed to decode deposit requests from receipts: {_0}")] + DepositRequestDecode(String), + /// Error when block's total gas used exceeds the block gas limit. + #[error("block gas used exceeds block gas limit")] + BlockGasExceeded, + /// Arbitrary block validation error. + #[error(transparent)] + Other(AnyError), +} + +impl BlockValidationError { + /// Create a new [`BlockValidationError::Other`] variant. + pub fn other(error: E) -> Self + where + E: Error + Send + Sync + 'static, + { + Self::Other(AnyError::new(error)) + } + + /// Create a new [`BlockValidationError::Other`] variant from a message. + pub fn msg(msg: impl core::fmt::Display) -> Self { + Self::Other(AnyError::from(msg.to_string())) + } +} + +/// Block executor errors. +#[derive(Debug, thiserror::Error)] +pub enum BlockExecutionError { + /// Validation error. + #[error(transparent)] + Validation(#[from] BlockValidationError), + /// Internal, i.e. non consensus or validation related block executor error. + #[error(transparent)] + Internal(#[from] InternalBlockExecutionError), +} + +impl BlockExecutionError { + /// Create a new [`BlockExecutionError::Internal`] variant. + pub fn other(error: E) -> Self + where + E: Error + Send + Sync + 'static, + { + Self::Internal(InternalBlockExecutionError::other(error)) + } + + /// Create a new [`BlockExecutionError::Internal`] variant from a message. + pub fn msg(msg: impl core::fmt::Display) -> Self { + Self::Internal(InternalBlockExecutionError::msg(msg)) + } + + /// Returns the inner [`BlockValidationError`] if this is a validation error. + pub const fn as_validation(&self) -> Option<&BlockValidationError> { + match self { + Self::Validation(err) => Some(err), + _ => None, + } + } + + /// Returns the inner [`InternalBlockExecutionError`] if this is an internal error. + pub const fn as_internal(&self) -> Option<&InternalBlockExecutionError> { + match self { + Self::Internal(err) => Some(err), + _ => None, + } + } +} + +/// Internal, i.e. not validation or consensus related, block executor errors. +#[derive(Debug, thiserror::Error)] +pub enum InternalBlockExecutionError { + /// EVM error occurred when executing a transaction. + #[error("internal EVM error occurred when executing transaction {hash}: {error}")] + EVM { + /// The hash of the transaction. + hash: B256, + /// The EVM error. + error: AnyError, + }, + /// Arbitrary block executor error. + #[error(transparent)] + Other(AnyError), +} + +impl InternalBlockExecutionError { + /// Create a new [`InternalBlockExecutionError::Other`] variant. + pub fn other(error: E) -> Self + where + E: Error + Send + Sync + 'static, + { + Self::Other(AnyError::new(error)) + } + + /// Create a new [`InternalBlockExecutionError::Other`] from a message. + pub fn msg(msg: impl core::fmt::Display) -> Self { + Self::Other(AnyError::from(msg.to_string())) + } + + /// Returns the arbitrary error if this is [`InternalBlockExecutionError::Other`]. + pub const fn as_other(&self) -> Option<&AnyError> { + match self { + Self::Other(err) => Some(err), + _ => None, + } + } + + /// Returns a reference to the [`InternalBlockExecutionError::Other`] value if it has type `T`. + pub fn downcast_other(&self) -> Option<&T> { + self.as_other()?.downcast_ref() + } + + /// Returns true if this is an [`InternalBlockExecutionError::Other`] of type `T`. + pub fn is_other(&self) -> bool { + self.downcast_other::().is_some() + } + + /// Returns the EVM error and transaction hash if this is [`InternalBlockExecutionError::EVM`]. + pub const fn as_evm(&self) -> Option<(&B256, &AnyError)> { + match self { + Self::EVM { hash, error } => Some((hash, error)), + _ => None, + } + } + + /// Returns a reference to the inner EVM error if it is of type `T`. + pub fn downcast_evm(&self) -> Option<&T> { + let (_, err) = self.as_evm()?; + err.downcast_ref() + } + + /// Returns true if this is an [`InternalBlockExecutionError::EVM`] error of type `T`. + pub fn is_evm(&self) -> bool { + self.downcast_evm::().is_some() + } +} + +/// Converts a handler error into a block error without transaction hash context. +/// Validation errors retain the original handler error for downstream classification. +pub fn map_handler_error(error: HandlerError) -> BlockExecutionError { + match error { + HandlerError::Database(error) => map_database_error(error), + HandlerError::Fatal(_) | HandlerError::WrongTransactionType { .. } => { + BlockExecutionError::other(error) + } + error => BlockValidationError::other(error).into(), + } +} + +/// Converts an owned database error using its internal-failure classification. +pub fn map_database_error(error: DatabaseError) -> BlockExecutionError { + if error.is_fatal() { + BlockExecutionError::other(error) + } else { + BlockValidationError::other(error).into() + } +} diff --git a/crates/evm2/src/block/ethereum/dao_fork.rs b/crates/evm2/src/block/ethereum/dao_fork.rs new file mode 100644 index 000000000..03a8a7b4e --- /dev/null +++ b/crates/evm2/src/block/ethereum/dao_fork.rs @@ -0,0 +1,128 @@ +//! DAO Fork related constants from [EIP-779](https://eips.ethereum.org/EIPS/eip-779). +//! It happened on Ethereum block `1_920_000`. + +use alloy_primitives::{Address, address}; + +/// Dao hardfork beneficiary that received ether from accounts from DAO and DAO creator children. +pub static DAO_HARDFORK_BENEFICIARY: Address = + address!("0xbf4ed7b27f1d666546e30d74d50d173d20bca754"); + +/// Accounts drained into the beneficiary at the DAO hardfork. +pub static DAO_HARDFORK_ACCOUNTS: [Address; 116] = [ + address!("0xd4fe7bc31cedb7bfb8a345f31e668033056b2728"), + address!("0xb3fb0e5aba0e20e5c49d252dfd30e102b171a425"), + address!("0x2c19c7f9ae8b751e37aeb2d93a699722395ae18f"), + address!("0xecd135fa4f61a655311e86238c92adcd779555d2"), + address!("0x1975bd06d486162d5dc297798dfc41edd5d160a7"), + address!("0xa3acf3a1e16b1d7c315e23510fdd7847b48234f6"), + address!("0x319f70bab6845585f412ec7724b744fec6095c85"), + address!("0x06706dd3f2c9abf0a21ddcc6941d9b86f0596936"), + address!("0x5c8536898fbb74fc7445814902fd08422eac56d0"), + address!("0x6966ab0d485353095148a2155858910e0965b6f9"), + address!("0x779543a0491a837ca36ce8c635d6154e3c4911a6"), + address!("0x2a5ed960395e2a49b1c758cef4aa15213cfd874c"), + address!("0x5c6e67ccd5849c0d29219c4f95f1a7a93b3f5dc5"), + address!("0x9c50426be05db97f5d64fc54bf89eff947f0a321"), + address!("0x200450f06520bdd6c527622a273333384d870efb"), + address!("0xbe8539bfe837b67d1282b2b1d61c3f723966f049"), + address!("0x6b0c4d41ba9ab8d8cfb5d379c69a612f2ced8ecb"), + address!("0xf1385fb24aad0cd7432824085e42aff90886fef5"), + address!("0xd1ac8b1ef1b69ff51d1d401a476e7e612414f091"), + address!("0x8163e7fb499e90f8544ea62bbf80d21cd26d9efd"), + address!("0x51e0ddd9998364a2eb38588679f0d2c42653e4a6"), + address!("0x627a0a960c079c21c34f7612d5d230e01b4ad4c7"), + address!("0xf0b1aa0eb660754448a7937c022e30aa692fe0c5"), + address!("0x24c4d950dfd4dd1902bbed3508144a54542bba94"), + address!("0x9f27daea7aca0aa0446220b98d028715e3bc803d"), + address!("0xa5dc5acd6a7968a4554d89d65e59b7fd3bff0f90"), + address!("0xd9aef3a1e38a39c16b31d1ace71bca8ef58d315b"), + address!("0x63ed5a272de2f6d968408b4acb9024f4cc208ebf"), + address!("0x6f6704e5a10332af6672e50b3d9754dc460dfa4d"), + address!("0x77ca7b50b6cd7e2f3fa008e24ab793fd56cb15f6"), + address!("0x492ea3bb0f3315521c31f273e565b868fc090f17"), + address!("0x0ff30d6de14a8224aa97b78aea5388d1c51c1f00"), + address!("0x9ea779f907f0b315b364b0cfc39a0fde5b02a416"), + address!("0xceaeb481747ca6c540a000c1f3641f8cef161fa7"), + address!("0xcc34673c6c40e791051898567a1222daf90be287"), + address!("0x579a80d909f346fbfb1189493f521d7f48d52238"), + address!("0xe308bd1ac5fda103967359b2712dd89deffb7973"), + address!("0x4cb31628079fb14e4bc3cd5e30c2f7489b00960c"), + address!("0xac1ecab32727358dba8962a0f3b261731aad9723"), + address!("0x4fd6ace747f06ece9c49699c7cabc62d02211f75"), + address!("0x440c59b325d2997a134c2c7c60a8c61611212bad"), + address!("0x4486a3d68fac6967006d7a517b889fd3f98c102b"), + address!("0x9c15b54878ba618f494b38f0ae7443db6af648ba"), + address!("0x27b137a85656544b1ccb5a0f2e561a5703c6a68f"), + address!("0x21c7fdb9ed8d291d79ffd82eb2c4356ec0d81241"), + address!("0x23b75c2f6791eef49c69684db4c6c1f93bf49a50"), + address!("0x1ca6abd14d30affe533b24d7a21bff4c2d5e1f3b"), + address!("0xb9637156d330c0d605a791f1c31ba5890582fe1c"), + address!("0x6131c42fa982e56929107413a9d526fd99405560"), + address!("0x1591fc0f688c81fbeb17f5426a162a7024d430c2"), + address!("0x542a9515200d14b68e934e9830d91645a980dd7a"), + address!("0xc4bbd073882dd2add2424cf47d35213405b01324"), + address!("0x782495b7b3355efb2833d56ecb34dc22ad7dfcc4"), + address!("0x58b95c9a9d5d26825e70a82b6adb139d3fd829eb"), + address!("0x3ba4d81db016dc2890c81f3acec2454bff5aada5"), + address!("0xb52042c8ca3f8aa246fa79c3feaa3d959347c0ab"), + address!("0xe4ae1efdfc53b73893af49113d8694a057b9c0d1"), + address!("0x3c02a7bc0391e86d91b7d144e61c2c01a25a79c5"), + address!("0x0737a6b837f97f46ebade41b9bc3e1c509c85c53"), + address!("0x97f43a37f595ab5dd318fb46e7a155eae057317a"), + address!("0x52c5317c848ba20c7504cb2c8052abd1fde29d03"), + address!("0x4863226780fe7c0356454236d3b1c8792785748d"), + address!("0x5d2b2e6fcbe3b11d26b525e085ff818dae332479"), + address!("0x5f9f3392e9f62f63b8eac0beb55541fc8627f42c"), + address!("0x057b56736d32b86616a10f619859c6cd6f59092a"), + address!("0x9aa008f65de0b923a2a4f02012ad034a5e2e2192"), + address!("0x304a554a310c7e546dfe434669c62820b7d83490"), + address!("0x914d1b8b43e92723e64fd0a06f5bdb8dd9b10c79"), + address!("0x4deb0033bb26bc534b197e61d19e0733e5679784"), + address!("0x07f5c1e1bc2c93e0402f23341973a0e043f7bf8a"), + address!("0x35a051a0010aba705c9008d7a7eff6fb88f6ea7b"), + address!("0x4fa802324e929786dbda3b8820dc7834e9134a2a"), + address!("0x9da397b9e80755301a3b32173283a91c0ef6c87e"), + address!("0x8d9edb3054ce5c5774a420ac37ebae0ac02343c6"), + address!("0x0101f3be8ebb4bbd39a2e3b9a3639d4259832fd9"), + address!("0x5dc28b15dffed94048d73806ce4b7a4612a1d48f"), + address!("0xbcf899e6c7d9d5a215ab1e3444c86806fa854c76"), + address!("0x12e626b0eebfe86a56d633b9864e389b45dcb260"), + address!("0xa2f1ccba9395d7fcb155bba8bc92db9bafaeade7"), + address!("0xec8e57756626fdc07c63ad2eafbd28d08e7b0ca5"), + address!("0xd164b088bd9108b60d0ca3751da4bceb207b0782"), + address!("0x6231b6d0d5e77fe001c2a460bd9584fee60d409b"), + address!("0x1cba23d343a983e9b5cfd19496b9a9701ada385f"), + address!("0xa82f360a8d3455c5c41366975bde739c37bfeb8a"), + address!("0x9fcd2deaff372a39cc679d5c5e4de7bafb0b1339"), + address!("0x005f5cee7a43331d5a3d3eec71305925a62f34b6"), + address!("0x0e0da70933f4c7849fc0d203f5d1d43b9ae4532d"), + address!("0xd131637d5275fd1a68a3200f4ad25c71a2a9522e"), + address!("0xbc07118b9ac290e4622f5e77a0853539789effbe"), + address!("0x47e7aa56d6bdf3f36be34619660de61275420af8"), + address!("0xacd87e28b0c9d1254e868b81cba4cc20d9a32225"), + address!("0xadf80daec7ba8dcf15392f1ac611fff65d94f880"), + address!("0x5524c55fb03cf21f549444ccbecb664d0acad706"), + address!("0x40b803a9abce16f50f36a77ba41180eb90023925"), + address!("0xfe24cdd8648121a43a7c86d289be4dd2951ed49f"), + address!("0x17802f43a0137c506ba92291391a8a8f207f487d"), + address!("0x253488078a4edf4d6f42f113d1e62836a942cf1a"), + address!("0x86af3e9626fce1957c82e88cbf04ddf3a2ed7915"), + address!("0xb136707642a4ea12fb4bae820f03d2562ebff487"), + address!("0xdbe9b615a3ae8709af8b93336ce9b477e4ac0940"), + address!("0xf14c14075d6c4ed84b86798af0956deef67365b5"), + address!("0xca544e5c4687d109611d0f8f928b53a25af72448"), + address!("0xaeeb8ff27288bdabc0fa5ebb731b6f409507516c"), + address!("0xcbb9d3703e651b0d496cdefb8b92c25aeb2171f7"), + address!("0x6d87578288b6cb5549d5076a207456a1f6a63dc0"), + address!("0xb2c6f0dfbb716ac562e2d85d6cb2f8d5ee87603e"), + address!("0xaccc230e8a6e5be9160b8cdf2864dd2a001c28b6"), + address!("0x2b3455ec7fedf16e646268bf88846bd7a2319bb2"), + address!("0x4613f3bca5c44ea06337a9e439fbc6d42e501d0a"), + address!("0xd343b217de44030afaa275f54d31a9317c7f441e"), + address!("0x84ef4b2357079cd7a7c69fd7a37cd0609a679106"), + address!("0xda2fef9e4a3230988ff17df2165440f37e8b1708"), + address!("0xf4c64518ea10f995918a454158c6b61407ea345c"), + address!("0x7602b46df5390e432ef1c307d4f2c9ff6d65cc97"), + address!("0xbb9bc244d798123fde783fcc1c72d3bb8c189413"), + address!("0x807640a13483f8ac783c557fcdf27be11ea4ac7a"), +]; diff --git a/crates/evm2/src/block/ethereum/execution.rs b/crates/evm2/src/block/ethereum/execution.rs new file mode 100644 index 000000000..b8124f14a --- /dev/null +++ b/crates/evm2/src/block/ethereum/execution.rs @@ -0,0 +1,723 @@ +//! EVM-backed Ethereum execution helpers. + +use super::dao_fork; +use crate::{ + AnyError, Evm, EvmTypes, SpecId, TxResult, TxResultWithState, TxStateChanges, + block::{ + BlockExecutionError, BlockState, BlockValidationError, CommitChanges, + InternalBlockExecutionError, InvalidTxError, StateUpdate, map_database_error, + map_handler_error, + }, + evm::{ + AccountChangeRef, AccountInfo, BEACON_ROOTS_ADDRESS, BUILDER_DEPOSIT_REQUEST_ADDRESS, + BUILDER_EXIT_REQUEST_ADDRESS, CONSOLIDATION_REQUEST_ADDRESS, HISTORY_STORAGE_ADDRESS, + StateChangeSink, SystemTx, WITHDRAWAL_REQUEST_ADDRESS, + }, + interpreter::InstrStop, + registry::HandlerError, +}; +use alloc::{ + boxed::Box, + string::{String, ToString}, + vec::Vec, +}; +use alloy_consensus::{ + BlockHeader, Header, TxReceipt, constants::ETH_TO_WEI, transaction::Recovered, +}; +use alloy_eips::{ + eip2718::Typed2718, + eip4895::Withdrawal, + eip6110::{DEPOSIT_REQUEST_TYPE, MAINNET_DEPOSIT_CONTRACT_ADDRESS}, + eip7002::WITHDRAWAL_REQUEST_TYPE, + eip7251::CONSOLIDATION_REQUEST_TYPE, + eip7685::Requests, +}; +use alloy_hardforks::EthereumHardforks; +use alloy_primitives::{Address, B256, Bytes, Log, U256, b256, map::AddressMap}; +use core::any::Any; + +/// Size of one encoded EIP-6110 deposit request. +pub const DEPOSIT_BYTES_SIZE: usize = 48 + 32 + 8 + 96 + 8; +/// EIP-8282 builder deposit request type. +pub const BUILDER_DEPOSIT_REQUEST_TYPE: u8 = 0x03; +/// EIP-8282 builder exit request type. +pub const BUILDER_EXIT_REQUEST_TYPE: u8 = 0x04; + +/// First topic of the deposit contract's `DepositEvent`. +const DEPOSIT_EVENT_SIGNATURE_HASH: B256 = + b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"); + +/// ABI layout of a `DepositEvent` payload: the offset of each dynamic field's length word and +/// the field's size, in the order the consensus layer consumes them (public key, withdrawal +/// credentials, amount, signature, index). +const DEPOSIT_EVENT_FIELDS: [(usize, usize); 5] = + [(160, 48), (256, 32), (320, 8), (384, 96), (512, 8)]; + +/// Length of an ABI-encoded `DepositEvent` payload. +const DEPOSIT_EVENT_LENGTH: usize = 576; + +/// Ethereum transaction validation error returned by evm2 handlers. +#[derive(Debug, Clone, PartialEq, Eq)] +struct EthInvalidTxError(HandlerError); + +impl core::fmt::Display for EthInvalidTxError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + self.0.fmt(f) + } +} + +impl core::error::Error for EthInvalidTxError {} + +impl InvalidTxError for EthInvalidTxError { + fn is_nonce_too_low(&self) -> bool { + matches!(self.0, HandlerError::InvalidNonce { expected, got } if got < expected) + } + + fn is_gas_limit_too_high(&self) -> bool { + matches!( + self.0, + HandlerError::GasLimitMoreThanBlock { .. } + | HandlerError::TxGasLimitGreaterThanCap { .. } + ) + } + + fn is_gas_limit_too_low(&self) -> bool { + matches!( + self.0, + HandlerError::IntrinsicGasTooLow { .. } | HandlerError::GasLimitBelowFloor { .. } + ) + } + + /// Exposes the wrapped [`HandlerError`] so RPC layers can recover the structured error. + fn as_any(&self) -> &(dyn Any + 'static) { + &self.0 + } +} + +impl TryFrom for EthInvalidTxError { + type Error = HandlerError; + + fn try_from(error: HandlerError) -> Result { + match error { + HandlerError::Database(_) + | HandlerError::Fatal(_) + | HandlerError::WrongTransactionType { .. } => Err(error), + HandlerError::External(_) + | HandlerError::UnsupportedTransactionType(_) + | HandlerError::InvalidNonce { .. } + | HandlerError::InvalidChainId { .. } + | HandlerError::MissingChainId + | HandlerError::IntrinsicGasTooLow { .. } + | HandlerError::GasLimitBelowFloor { .. } + | HandlerError::IntrinsicGasGreaterThanCap { .. } + | HandlerError::InsufficientFunds { .. } + | HandlerError::RejectCallerWithCode + | HandlerError::NonceOverflow + | HandlerError::GasLimitMoreThanBlock { .. } + | HandlerError::TxGasLimitGreaterThanCap { .. } + | HandlerError::CreateInitCodeSizeLimit { .. } + | HandlerError::FeeCapLessThanBaseFee { .. } + | HandlerError::EmptyAuthorizationList + | HandlerError::BlobFeeCapLessThanBlobBaseFee { .. } + | HandlerError::EmptyBlobs + | HandlerError::TooManyBlobs { .. } + | HandlerError::BlobVersionNotSupported + | HandlerError::PriorityFeeGreaterThanMaxFee => Ok(Self(error)), + } + } +} + +/// Additional block-level execution context. +#[derive(Debug, Clone, Copy, Default)] +pub struct BlockExecutionContext<'a> { + /// Pre-block system calls to run before transaction execution. + pub system_calls: Option, + /// Pre-merge ommer headers included in the block. + pub ommers: Option<&'a [Header]>, + /// Post-block withdrawals to apply after transaction execution. + pub withdrawals: Option<&'a [Withdrawal]>, + /// Deposit contract address used to derive EIP-6110 deposit requests from receipts. + pub deposit_contract_address: Option
, +} + +/// Inputs required by Ethereum pre-block system calls. +#[derive(Debug, Clone, Copy)] +pub struct BlockSystemCalls { + /// Parent block hash for EIP-2935 history storage. + pub parent_hash: B256, + /// Parent beacon block root for EIP-4788 beacon roots. + pub parent_beacon_block_root: Option, +} + +/// Converts a transaction's handler error into a block error with its hash. +pub fn map_transaction_error(error: HandlerError, hash: B256) -> BlockExecutionError { + let error = match EthInvalidTxError::try_from(error) { + Ok(error) => { + return BlockValidationError::InvalidTx { hash, error: Box::new(error) }.into(); + } + Err(error) => error, + }; + if let HandlerError::Database(database_error) = &error + && !database_error.is_fatal() + { + BlockValidationError::EVM { hash, error: AnyError::new(error) }.into() + } else { + InternalBlockExecutionError::EVM { hash, error: AnyError::new(error) }.into() + } +} + +fn send_state_update(state: StateUpdate, on_state_update: &mut impl FnMut(StateUpdate)) { + if !state.is_empty() { + on_state_update(state); + } +} + +/// Executes a transaction and commits it when `commit` accepts its result. +pub fn execute_transaction_with_condition( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + transaction: &Recovered, + commit: impl FnOnce(&TxResult, TxStateChanges<'_>) -> CommitChanges, +) -> Result>, HandlerError> +where + T::Tx: Typed2718, +{ + let mut update = StateUpdate::default(); + let result = evm.transact(transaction).map(|executed| { + if commit(executed.result(), executed.changes()).should_commit() { + for changes in executed.account_changes() { + block_state.commit_account_changes(changes, stream_state.then_some(&mut update)); + } + Some(executed.commit()) + } else { + let _ = executed.discard(); + None + } + }); + if stream_state { + send_state_update(update, on_state_update); + } + result +} + +/// Executes a transaction and detaches its state changes. +pub fn execute_transaction_without_commit( + evm: &mut Evm<'_, T>, + transaction: &Recovered, +) -> Result, HandlerError> +where + T::Tx: Typed2718, +{ + evm.transact(transaction).map(|executed| executed.detach()) +} + +/// Commits a detached transaction into the EVM and the block state. +pub fn commit_detached_transaction( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + output: TxResultWithState, +) -> TxResult { + let TxResultWithState { result, pending_state, .. } = output; + accumulate_pending_state(block_state, stream_state, on_state_update, &pending_state); + // Reattach the finalized transaction so evm2 retains its account capacity and recycles + // storage maps for the next transaction instead of dropping the detached allocations. + evm.state_mut().set_pending_state(pending_state); + evm.state_mut().commit_transaction(); + result +} + +/// Commits detached state into the EVM overlay and the block state. +pub fn commit_pending_state( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + pending_state: &evm2::evm::PendingState, +) { + accumulate_pending_state(block_state, stream_state, on_state_update, pending_state); + evm.overlay_db_mut().commit_pending(pending_state); +} + +fn accumulate_pending_state( + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + pending_state: &evm2::evm::PendingState, +) { + let mut update = StateUpdate::default(); + for changes in pending_state.account_changes() { + block_state.commit_account_changes(changes, stream_state.then_some(&mut update)); + } + if stream_state { + send_state_update(update, on_state_update); + } +} + +/// Runs the EIP-2935 and EIP-4788 pre-block system calls. +pub fn pre_execution_system_call_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + spec_id: SpecId, + block_number: u64, + context: BlockExecutionContext<'_>, +) -> Result<(), BlockExecutionError> { + let Some(system_calls) = context.system_calls else { + return Ok(()); + }; + + if spec_id.enables(SpecId::CANCUN) { + let parent_beacon_block_root = system_calls + .parent_beacon_block_root + .ok_or(BlockValidationError::MissingParentBeaconBlockRoot)?; + + if block_number == 0 { + if !parent_beacon_block_root.is_zero() { + return Err(BlockValidationError::CancunGenesisParentBeaconBlockRootNotZero { + parent_beacon_block_root, + } + .into()); + } + } else { + let _ = execute_system_call( + evm, + block_state, + stream_state, + on_state_update, + BEACON_ROOTS_ADDRESS, + parent_beacon_block_root.0.into(), + )?; + } + } + + if spec_id.enables(SpecId::PRAGUE) && block_number != 0 { + let _ = execute_system_call( + evm, + block_state, + stream_state, + on_state_update, + HISTORY_STORAGE_ADDRESS, + system_calls.parent_hash.0.into(), + )?; + } + + Ok(()) +} + +/// Collects the block's EIP-6110 deposit requests from its receipts. +pub fn block_requests_from_receipts( + spec_id: SpecId, + context: BlockExecutionContext<'_>, + receipts: &[R], +) -> Result +where + R: TxReceipt, +{ + let mut requests = Requests::default(); + if context.system_calls.is_none() || !spec_id.enables(SpecId::PRAGUE) { + return Ok(requests); + } + + let deposit_requests = parse_deposit_requests_from_receipts( + context.deposit_contract_address.unwrap_or(MAINNET_DEPOSIT_CONTRACT_ADDRESS), + receipts, + )?; + requests.push_request_with_type(DEPOSIT_REQUEST_TYPE, deposit_requests); + + Ok(requests) +} + +// TODO: Replace the local deposit event and encoding with alloy-eips helpers once +// https://github.com/alloy-rs/alloy/pull/4244 is released. +fn parse_deposit_requests_from_receipts( + deposit_contract_address: Address, + receipts: &[R], +) -> Result, BlockExecutionError> +where + R: TxReceipt, +{ + let mut out = Vec::new(); + for receipt in receipts { + for log in receipt.logs() { + if log.address != deposit_contract_address + || log.topics().first() != Some(&DEPOSIT_EVENT_SIGNATURE_HASH) + { + continue; + } + extract_deposit_data(&log.data.data, &mut out) + .map_err(BlockValidationError::DepositRequestDecode)?; + } + } + + Ok(out) +} + +/// Strips the ABI framing from a `DepositEvent` payload, appending its fields to `out`. A payload +/// that is not the canonical encoding makes the block invalid (EIP-6110). +fn extract_deposit_data(data: &[u8], out: &mut Vec) -> Result<(), String> { + if data.len() != DEPOSIT_EVENT_LENGTH { + return Err("invalid deposit event data length".to_string()); + } + let word = |offset: usize| U256::from_be_slice(&data[offset..offset + 32]); + for (index, (offset, _)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { + if word(index * 32) != U256::from(offset) { + return Err(alloc::format!("invalid offset for deposit field {index}")); + } + } + out.reserve(DEPOSIT_BYTES_SIZE); + for (index, (offset, size)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { + if word(offset) != U256::from(size) { + return Err(alloc::format!("invalid size for deposit field {index}")); + } + out.extend_from_slice(&data[offset + 32..offset + 32 + size]); + } + Ok(()) +} + +/// Runs the EIP-7002, EIP-7251 and EIP-8282 request system calls. +pub fn post_execution_system_call_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + spec_id: SpecId, + context: BlockExecutionContext<'_>, + requests: &mut Requests, +) -> Result<(), BlockExecutionError> { + if context.system_calls.is_none() || !spec_id.enables(SpecId::PRAGUE) { + return Ok(()); + } + + let withdrawal_requests = execute_system_call( + evm, + block_state, + stream_state, + on_state_update, + WITHDRAWAL_REQUEST_ADDRESS, + Bytes::new(), + )?; + requests.push_request_with_type( + WITHDRAWAL_REQUEST_TYPE, + withdrawal_requests.output.iter().copied(), + ); + + let consolidation_requests = execute_system_call( + evm, + block_state, + stream_state, + on_state_update, + CONSOLIDATION_REQUEST_ADDRESS, + Bytes::new(), + )?; + requests.push_request_with_type( + CONSOLIDATION_REQUEST_TYPE, + consolidation_requests.output.iter().copied(), + ); + + if spec_id.enables(SpecId::AMSTERDAM) { + let builder_deposit_requests = execute_system_call( + evm, + block_state, + stream_state, + on_state_update, + BUILDER_DEPOSIT_REQUEST_ADDRESS, + Bytes::new(), + )?; + requests.push_request_with_type( + BUILDER_DEPOSIT_REQUEST_TYPE, + builder_deposit_requests.output.iter().copied(), + ); + + let builder_exit_requests = execute_system_call( + evm, + block_state, + stream_state, + on_state_update, + BUILDER_EXIT_REQUEST_ADDRESS, + Bytes::new(), + )?; + requests.push_request_with_type( + BUILDER_EXIT_REQUEST_TYPE, + builder_exit_requests.output.iter().copied(), + ); + } + + Ok(()) +} + +fn execute_system_call( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + address: Address, + data: Bytes, +) -> Result, BlockExecutionError> { + let executed = evm.system_call(SystemTx::new(address, data)).map_err(map_handler_error)?; + // History and beacon-root calls accept reverts and halts, unlike request-producing calls. + if !executed.result().is_success() + && !matches!(address, BEACON_ROOTS_ADDRESS | HISTORY_STORAGE_ADDRESS) + { + let reason = executed.result().stop; + let _ = executed.discard(); + return Err(match address { + WITHDRAWAL_REQUEST_ADDRESS => BlockValidationError::WithdrawalRequestsContractCall { + message: alloc::format!("{reason:?}"), + }, + CONSOLIDATION_REQUEST_ADDRESS => { + BlockValidationError::ConsolidationRequestsContractCall { + message: alloc::format!("{reason:?}"), + } + } + _ => BlockValidationError::other(SystemCallFailed { address, reason }), + } + .into()); + } + let outcome = executed.detach(); + Ok(commit_detached_transaction(evm, block_state, stream_state, on_state_update, outcome)) +} + +fn commit_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + changes: &[(Address, Option, Option)], +) { + let mut pending_state = evm2::evm::PendingState::default(); + for (address, original, current) in changes { + let change = AccountChangeRef { + address: *address, + original: original.as_ref(), + current: current.as_ref(), + created: false, + selfdestructed: false, + }; + let Ok(()) = evm.overlay_db_mut().account(change); + pending_state.insert_account(*address, original.clone(), current.clone()); + } + accumulate_pending_state(block_state, stream_state, on_state_update, &pending_state); +} + +/// Drains the DAO accounts into the refund beneficiary. Runs before the block's transactions. +/// +/// The fork predates EIP-161, so drained accounts stay, empty or absent ones included. +pub fn dao_fork_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), +) -> Result<(), BlockExecutionError> { + let mut drained_balance = U256::ZERO; + let mut changes = Vec::new(); + for address in dao_fork::DAO_HARDFORK_ACCOUNTS { + let original = evm.read_account_info(&address).map_err(map_database_error)?; + let mut current = original.clone().unwrap_or_else(AccountInfo::empty); + drained_balance = drained_balance.saturating_add(current.balance); + current.balance = U256::ZERO; + if original.as_ref() != Some(¤t) { + changes.push((address, original, Some(current))); + } + } + commit_state_changes(evm, block_state, stream_state, on_state_update, &changes); + + // The beneficiary may also be drained, so read its balance after the drain. + let mut balance_increments = AddressMap::default(); + balance_increments.insert(dao_fork::DAO_HARDFORK_BENEFICIARY, drained_balance); + apply_balance_increments( + evm, + block_state, + stream_state, + on_state_update, + false, + balance_increments, + ) +} + +/// Applies block and ommer rewards and withdrawals. +#[expect(clippy::too_many_arguments)] +pub fn post_block_balance_state_changes( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + spec_id: SpecId, + base_block_reward: Option, + block_number: u64, + block_beneficiary: Address, + ommers: Option<&[Header]>, + withdrawals: Option<&[Withdrawal]>, +) -> Result<(), BlockExecutionError> { + let mut balance_increments = AddressMap::::default(); + + if let Some(base_block_reward) = base_block_reward { + let ommers = ommers.unwrap_or_default(); + for ommer in ommers { + *balance_increments.entry(ommer.beneficiary()).or_default() += + U256::from(ommer_reward(base_block_reward, block_number, ommer.number())); + } + *balance_increments.entry(block_beneficiary).or_default() += + U256::from(block_reward(base_block_reward, ommers.len())); + } + + for withdrawal in withdrawals.into_iter().flatten() { + *balance_increments.entry(withdrawal.address).or_default() += withdrawal.amount_wei(); + } + + apply_balance_increments( + evm, + block_state, + stream_state, + on_state_update, + spec_id.enables(SpecId::SPURIOUS_DRAGON), + balance_increments, + ) +} + +/// Adds each increment to its account's balance. With `eip161`, a zero increment removes an empty +/// account. +fn apply_balance_increments( + evm: &mut Evm<'_, T>, + block_state: &mut BlockState, + stream_state: bool, + on_state_update: &mut impl FnMut(StateUpdate), + eip161: bool, + balance_increments: AddressMap, +) -> Result<(), BlockExecutionError> { + let mut changes = Vec::with_capacity(balance_increments.len()); + for (address, increment) in balance_increments { + let original = evm.read_account_info(&address).map_err(map_database_error)?; + let current = + if eip161 && increment.is_zero() && original.as_ref().is_none_or(AccountInfo::is_empty) + { + // The increment only touches the account, which EIP-161 removes when empty. + None + } else { + let mut current = original.clone().unwrap_or_else(AccountInfo::empty); + current.balance = current.balance.saturating_add(increment); + Some(current) + }; + if original == current { + // Zero-value withdrawals still count as account accesses in the BAL. + let Ok(()) = evm.overlay_db_mut().account_read(address, original.as_ref()); + continue; + } + changes.push((address, original, current)); + } + commit_state_changes(evm, block_state, stream_state, on_state_update, &changes); + Ok(()) +} + +/// Returns the base block reward at `block_number`, or `None` after the merge. +pub fn base_block_reward(chain_spec: &C, block_number: u64) -> Option +where + C: EthereumHardforks + ?Sized, +{ + if chain_spec.is_paris_active_at_block(block_number) { + None + } else if chain_spec.is_constantinople_active_at_block(block_number) { + Some(ETH_TO_WEI * 2) + } else if chain_spec.is_byzantium_active_at_block(block_number) { + Some(ETH_TO_WEI * 3) + } else { + Some(ETH_TO_WEI * 5) + } +} + +/// Returns the block reward with the ommer inclusion bonus. +pub const fn block_reward(base_block_reward: u128, ommers: usize) -> u128 { + base_block_reward + (base_block_reward >> 5) * ommers as u128 +} + +/// Returns the reward of an ommer included at `block_number`. +pub fn ommer_reward(base_block_reward: u128, block_number: u64, ommer_block_number: u64) -> u128 { + let distance = 8u64.saturating_add(ommer_block_number).saturating_sub(block_number); + (u128::from(distance) * base_block_reward) >> 3 +} + +/// A system contract reverted or halted without producing a successful result. +#[derive(Debug)] +struct SystemCallFailed { + address: Address, + reason: InstrStop, +} + +impl core::fmt::Display for SystemCallFailed { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "EVM system call to {} failed: {:?}", self.address, self.reason) + } +} + +impl core::error::Error for SystemCallFailed {} + +#[cfg(test)] +mod tests { + use super::*; + use alloc::vec; + + fn deposit_log(data: Vec) -> Log { + Log::new_unchecked( + MAINNET_DEPOSIT_CONTRACT_ADDRESS, + vec![DEPOSIT_EVENT_SIGNATURE_HASH], + data.into(), + ) + } + + /// Encodes a canonical `DepositEvent` payload whose fields are filled with `fill`. + fn deposit_data(fill: u8) -> Vec { + let mut data = vec![0; DEPOSIT_EVENT_LENGTH]; + for (index, (offset, size)) in DEPOSIT_EVENT_FIELDS.into_iter().enumerate() { + data[index * 32..index * 32 + 32] + .copy_from_slice(&U256::from(offset).to_be_bytes::<32>()); + data[offset..offset + 32].copy_from_slice(&U256::from(size).to_be_bytes::<32>()); + data[offset + 32..offset + 32 + size].fill(fill); + } + data + } + + fn requests(log: Log) -> Result, BlockExecutionError> { + let receipt = alloy_consensus::Receipt { + status: true.into(), + cumulative_gas_used: 0, + logs: vec![log], + }; + parse_deposit_requests_from_receipts(MAINNET_DEPOSIT_CONTRACT_ADDRESS, &[receipt]) + } + + #[test] + fn parses_canonical_deposit() { + assert_eq!(requests(deposit_log(deposit_data(7))).unwrap(), vec![7; DEPOSIT_BYTES_SIZE]); + } + + #[test] + fn rejects_non_canonical_deposits() { + let mut short = deposit_data(7); + short.pop(); + let mut bad_offset = deposit_data(7); + bad_offset[31] = 0; + let mut bad_size = deposit_data(7); + bad_size[160 + 31] = 47; + for data in [short, bad_offset, bad_size] { + assert!(matches!( + requests(deposit_log(data)), + Err(BlockExecutionError::Validation(BlockValidationError::DepositRequestDecode(_))) + )); + } + } + + #[test] + fn classifies_gas_errors() { + let floor = EthInvalidTxError(HandlerError::GasLimitBelowFloor { required: 2, got: 1 }); + assert!(floor.is_gas_limit_too_low()); + assert!(!floor.is_gas_limit_too_high()); + let cap = + EthInvalidTxError(HandlerError::IntrinsicGasGreaterThanCap { required: 2, cap: 1 }); + assert!(!cap.is_gas_limit_too_low()); + assert!(!cap.is_gas_limit_too_high()); + } +} diff --git a/crates/evm2/src/block/ethereum/mod.rs b/crates/evm2/src/block/ethereum/mod.rs new file mode 100644 index 000000000..cc3fe2424 --- /dev/null +++ b/crates/evm2/src/block/ethereum/mod.rs @@ -0,0 +1,626 @@ +//! Ethereum block execution. + +use crate::{ + BaseEvmTypes, Evm, EvmFeatures, EvmTypes, SpecId, TxResult, TxResultWithState, TxStateChanges, + block::{ + BlockExecutionError, BlockExecutionOutput, BlockExecutionResult, BlockExecutor, BlockState, + BlockTransactionResult, BlockValidationError, BundleState, CommitChanges, ExecutableTx, + GasOutput, ReceiptBuilder, ReceiptBuilderCtx, StateUpdate, + }, + evm::{Bal, PendingState}, + interpreter::Host, +}; +use alloc::{borrow::Cow, boxed::Box, sync::Arc, vec::Vec}; +use alloy_consensus::{ + Eip658Value, EthereumTxEnvelope, Header, Receipt, ReceiptEnvelope, Transaction, TxEip4844, + TxType, + transaction::{TransactionEnvelope, TxHashRef}, +}; +use alloy_eip7928::BlockAccessIndex; +use alloy_eips::{eip2718::Typed2718, eip4895::Withdrawal, eip7685::Requests}; +use alloy_hardforks::{EthereumHardfork, EthereumHardforks}; +use alloy_primitives::{Address, B256}; +use core::borrow::Borrow; + +pub mod dao_fork; + +pub mod execution; +use execution::{ + BlockExecutionContext, BlockSystemCalls, block_requests_from_receipts, + commit_detached_transaction, commit_pending_state, dao_fork_state_changes, + execute_transaction_without_commit, map_transaction_error, post_block_balance_state_changes, + post_execution_system_call_state_changes, pre_execution_system_call_state_changes, +}; + +#[cfg(test)] +mod tests; + +/// Block inputs of [`EthBlockExecutor`] that are not in the EVM's block environment. +#[derive(Clone, Debug, Default)] +pub struct EthBlockExecutionCtx<'a> { + /// Optional transaction count hint. + pub tx_count_hint: Option, + /// Parent block hash. + pub parent_hash: B256, + /// Parent beacon block root. + pub parent_beacon_block_root: Option, + /// Ommer headers. + pub ommers: Cow<'a, [Header]>, + /// Withdrawals. + pub withdrawals: Option>, +} + +/// Chain rules of [`EthBlockExecutor`] that depend on the block number. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct EthBlockRules { + /// Block reward before the merge. + pub base_block_reward: Option, + /// Whether the block is the DAO fork block. + pub dao_fork_transition: bool, + /// EIP-6110 deposit contract. `None` means Ethereum mainnet's, which is wrong for other chains + /// with deposits, such as Sepolia or Hoodi. + pub deposit_contract_address: Option
, + /// Blob gas the block's transactions may use in total (EIP-4844). `None` leaves it unchecked. + pub max_blob_gas_per_block: Option, +} + +impl EthBlockRules { + /// Reads the rules of block `block_number` from a chain's hardforks. + pub fn new( + chain: &C, + block_number: u64, + deposit_contract_address: Option
, + ) -> Self { + Self { + base_block_reward: execution::base_block_reward(chain, block_number), + dao_fork_transition: chain + .ethereum_fork_activation(EthereumHardfork::Dao) + .transitions_at_block(block_number), + deposit_contract_address, + max_blob_gas_per_block: None, + } + } +} + +/// Builds [`ReceiptEnvelope`]s for Ethereum transactions without blob sidecars. +/// +/// Receipts carry an EIP-658 status, so their root matches headers only from Byzantium on; earlier +/// receipts carry the intermediate state root instead. +#[derive(Clone, Copy, Debug, Default)] +#[non_exhaustive] +pub struct EthReceiptBuilder; + +impl ReceiptBuilder for EthReceiptBuilder { + type Transaction = EthereumTxEnvelope; + type Receipt = ReceiptEnvelope; + + fn build_receipt( + &self, + ctx: ReceiptBuilderCtx>, + ) -> ReceiptEnvelope { + let ReceiptBuilderCtx { tx_type, result, cumulative_gas_used } = ctx; + let receipt = Receipt { + status: Eip658Value::Eip658(result.is_success()), + cumulative_gas_used, + logs: result.logs, + }; + ReceiptEnvelope::from_typed(tx_type, receipt.with_bloom()) + } +} + +/// Detached Ethereum transaction result with the metadata needed for canonical receipt commit. +#[derive(Debug)] +pub struct EthTransactionResultWithState { + result: TxResultWithState, + tx_type: Ty, + blob_gas_used: u64, +} + +impl EthTransactionResultWithState { + /// Creates a detached transaction result with its receipt metadata. + pub const fn new(result: TxResultWithState, tx_type: Ty, blob_gas_used: u64) -> Self { + Self { result, tx_type, blob_gas_used } + } + + /// Returns the detached EVM transaction result. + pub const fn result(&self) -> &TxResultWithState { + &self.result + } + + /// Splits the result into its parts. + pub fn into_parts(self) -> (TxResultWithState, Ty, u64) { + (self.result, self.tx_type, self.blob_gas_used) + } +} + +impl BlockTransactionResult for EthTransactionResultWithState { + fn result(&self) -> &TxResultWithState { + &self.result + } + + fn into_result(self) -> TxResultWithState { + self.result + } +} + +type StateUpdateHook = Option>; + +/// Post-execution output of one block, before its receipts and state are taken. +#[derive(Debug)] +pub struct EthPostExecutionOutput { + /// EIP-7685 requests of the block. + pub requests: Requests, + /// Gas used by the block. + pub gas_used: u64, + /// Blob gas used by the block. + pub blob_gas_used: u64, +} + +/// Configured Ethereum block executor backed by evm2. +#[expect(missing_debug_implementations)] +pub struct EthBlockExecutor<'a, T = BaseEvmTypes, R = EthReceiptBuilder> +where + T: EvmTypes, + R: ReceiptBuilder, +{ + /// EVM containing the execution overlay and optional block access list builder. + pub evm: Evm<'a, T>, + ctx: EthBlockExecutionCtx<'a>, + rules: EthBlockRules, + receipt_builder: R, + spec_id: SpecId, + /// Accumulated state changes, including state committed through the executor's hooks. + pub block_state: BlockState, + state_update_hook: StateUpdateHook, + /// Transaction receipts in execution order. + pub receipts: Vec, + /// Total receipt gas used. + pub cumulative_gas_used: u64, + /// Regular gas used for Amsterdam block-capacity accounting. + pub block_regular_gas_used: u64, + /// State gas used for Amsterdam block-capacity accounting. + pub block_state_gas_used: u64, + separate_block_gas: bool, + /// Total blob gas used. + pub blob_gas_used: u64, + bal_index_offset: u64, +} + +impl<'a, T, R> EthBlockExecutor<'a, T, R> +where + T: EvmTypes, + R: ReceiptBuilder, +{ + /// Creates an executor for the block in `evm`'s block environment. + pub fn new( + evm: Evm<'a, T>, + ctx: EthBlockExecutionCtx<'a>, + rules: EthBlockRules, + receipt_builder: R, + ) -> Self { + // The executor stores evm2's base spec ID, while custom type families may expose a + // richer host spec ID that converts into it. + #[allow(clippy::useless_conversion)] + let spec_id = evm.spec_id().into(); + let separate_block_gas = evm.version().feature(EvmFeatures::EIP8037); + let receipts = Vec::with_capacity(ctx.tx_count_hint.unwrap_or_default()); + Self { + evm, + ctx, + rules, + receipt_builder, + spec_id, + block_state: BlockState::new(), + state_update_hook: None, + receipts, + cumulative_gas_used: 0, + block_regular_gas_used: 0, + block_state_gas_used: 0, + separate_block_gas, + blob_gas_used: 0, + bal_index_offset: 0, + } + } + + const fn block_context<'ctx>( + rules: &EthBlockRules, + parent_hash: B256, + parent_beacon_block_root: Option, + ommers: &'ctx [Header], + withdrawals: Option<&'ctx [Withdrawal]>, + ) -> BlockExecutionContext<'ctx> { + BlockExecutionContext { + system_calls: Some(BlockSystemCalls { parent_hash, parent_beacon_block_root }), + ommers: Some(ommers), + withdrawals, + deposit_contract_address: rules.deposit_contract_address, + } + } + + const fn block_access_list_builder_enabled(&self) -> bool { + self.evm.state().bal_builder().is_some() + } + + /// Returns the block execution context. + pub const fn context(&self) -> &EthBlockExecutionCtx<'a> { + &self.ctx + } + + /// Returns the chain rules of the block. + pub const fn rules(&self) -> &EthBlockRules { + &self.rules + } + + /// Returns the accumulated block state. + pub const fn block_state(&self) -> &BlockState { + &self.block_state + } + + /// Commits detached state without recording a transaction receipt or gas usage. + pub fn commit_pending_state(&mut self, state: &PendingState) { + let stream_state = self.state_update_hook.is_some(); + commit_pending_state( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + state, + ); + } + + /// Sets the Ethereum fork used for block system calls and deposit request processing. + pub const fn set_spec_id(&mut self, spec_id: SpecId) { + self.spec_id = spec_id; + } + + /// Starts a new block on the same EVM and block state, as when executing several blocks as + /// one: reads the spec and gas rules from the EVM's current configuration, installs `ctx` + /// and `rules`, offsets the block access indices by `bal_index_offset`, and resets the gas + /// counters. Receipts and block state carry over. + /// + /// Finish each block with [`Self::apply_post_execution_changes`] from its first receipt and the + /// last one with [`BlockExecutor::into_state`]: [`BlockExecutor::finish`] applies + /// post-execution changes again, over every receipt. + pub fn start_block( + &mut self, + ctx: EthBlockExecutionCtx<'a>, + rules: EthBlockRules, + bal_index_offset: u64, + ) { + #[allow(clippy::useless_conversion)] + let spec_id = self.evm.spec_id().into(); + self.spec_id = spec_id; + self.separate_block_gas = self.evm.version().feature(EvmFeatures::EIP8037); + self.ctx = ctx; + self.rules = rules; + self.bal_index_offset = bal_index_offset; + self.reset_gas_used(); + } + + /// Resets the block's gas counters. + pub const fn reset_gas_used(&mut self) { + self.cumulative_gas_used = 0; + self.block_regular_gas_used = 0; + self.block_state_gas_used = 0; + self.blob_gas_used = 0; + } + + /// Applies the block's post-execution changes without consuming the executor: requests from + /// the receipts from `receipts_start` on, rewards and withdrawals, then the request system + /// calls (execution-specs order). + pub fn apply_post_execution_changes( + &mut self, + receipts_start: usize, + ) -> Result { + self.set_transaction_block_access_index(); + let context = Self::block_context( + &self.rules, + self.ctx.parent_hash, + self.ctx.parent_beacon_block_root, + &self.ctx.ommers, + None, + ); + let mut requests = + block_requests_from_receipts(self.spec_id, context, &self.receipts[receipts_start..])?; + let stream_state = self.state_update_hook.is_some(); + let withdrawals = + self.ctx.withdrawals.as_deref().filter(|_| self.spec_id.enables(SpecId::SHANGHAI)); + let context = Self::block_context( + &self.rules, + self.ctx.parent_hash, + self.ctx.parent_beacon_block_root, + &self.ctx.ommers, + withdrawals, + ); + let block_number = self.evm.block_env().number.to::(); + let block_beneficiary = self.evm.block_env().beneficiary; + post_block_balance_state_changes( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + self.spec_id, + self.rules.base_block_reward, + block_number, + block_beneficiary, + context.ommers, + context.withdrawals, + )?; + + post_execution_system_call_state_changes( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + self.spec_id, + context, + &mut requests, + )?; + + Ok(EthPostExecutionOutput { + requests, + gas_used: final_block_gas_used( + self.separate_block_gas, + self.cumulative_gas_used, + self.block_regular_gas_used, + self.block_state_gas_used, + ), + blob_gas_used: self.blob_gas_used, + }) + } + + /// Validates that a transaction's blobs fit the block's remaining blob gas. + fn validate_transaction_blob_gas( + &self, + transaction_blob_gas: u64, + ) -> Result<(), BlockExecutionError> { + let Some(max_blob_gas) = self.rules.max_blob_gas_per_block else { return Ok(()) }; + let block_available_blob_gas = max_blob_gas.saturating_sub(self.blob_gas_used); + if transaction_blob_gas > block_available_blob_gas { + return Err(BlockValidationError::TransactionBlobGasMoreThanAvailableBlockBlobGas { + transaction_blob_gas, + block_available_blob_gas, + } + .into()); + } + Ok(()) + } + + #[inline] + fn set_transaction_block_access_index(&mut self) { + if self.block_access_list_builder_enabled() { + let index = self.receipts.len() as u64 + 1 + self.bal_index_offset; + self.set_block_access_index(BlockAccessIndex::new(index)); + } + } + + fn record_receipt( + &mut self, + tx_type: ::TxType, + blob_gas_used: u64, + outcome: TxResult, + ) -> GasOutput { + let tx_gas_used = outcome.tx_gas_used(); + let regular_gas_used = outcome.execution_gas_spent(); + let state_gas_used = outcome.state_gas_spent(); + self.block_regular_gas_used = self.block_regular_gas_used.saturating_add(regular_gas_used); + self.block_state_gas_used = self.block_state_gas_used.saturating_add(state_gas_used); + self.cumulative_gas_used += tx_gas_used; + self.blob_gas_used += blob_gas_used; + self.receipts.push(self.receipt_builder.build_receipt::(ReceiptBuilderCtx { + tx_type, + result: outcome, + cumulative_gas_used: self.cumulative_gas_used, + })); + GasOutput::new_with_regular(tx_gas_used, regular_gas_used, state_gas_used) + } +} + +impl<'a, T, R> BlockExecutor for EthBlockExecutor<'a, T, R> +where + T: EvmTypes, + R: ReceiptBuilder, +{ + type Transaction = R::Transaction; + type Receipt = R::Receipt; + type EvmTypes = T; + type Evm = Evm<'a, T>; + type TransactionResultWithState = + EthTransactionResultWithState::TxType>; + + fn evm(&self) -> &Self::Evm { + &self.evm + } + + fn evm_mut(&mut self) -> &mut Self::Evm { + &mut self.evm + } + + fn set_state_hook(&mut self, hook: impl FnMut(StateUpdate) + Send + 'static) -> bool { + self.state_update_hook = Some(Box::new(hook)); + true + } + + fn set_block_access_list(&mut self, block_access_list: Arc) { + self.evm.state_mut().set_bal(block_access_list); + } + + fn set_block_access_index(&mut self, index: BlockAccessIndex) { + self.evm.state_mut().set_bal_index(index); + } + + fn enable_block_access_list_builder(&mut self) { + self.evm.state_mut().enable_bal_builder(); + self.evm.state_mut().reset_bal_index(); + } + + fn take_block_access_list(&mut self) -> Option { + self.evm.state_mut().take_bal_builder() + } + + fn apply_pre_execution_changes(&mut self) -> Result<(), BlockExecutionError> { + let context = Self::block_context( + &self.rules, + self.ctx.parent_hash, + self.ctx.parent_beacon_block_root, + &self.ctx.ommers, + None, + ); + let block_number = self.evm.block_env().number.to::(); + let stream_state = self.state_update_hook.is_some(); + if self.rules.dao_fork_transition { + core::hint::cold_path(); + dao_fork_state_changes( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + )?; + } + pre_execution_system_call_state_changes( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + self.spec_id, + block_number, + context, + ) + } + + fn into_state(self) -> BundleState { + self.block_state.into_bundle() + } + + fn validate_transaction_gas_limit( + &mut self, + transaction_gas_limit: u64, + ) -> Result<(), BlockExecutionError> { + let block_gas_limit = self.evm.block_env().gas_limit.to::(); + let regular_limit = transaction_gas_limit.min(self.evm.version().tx_gas_limit_cap); + let unavailable = if self.separate_block_gas { + let regular_available = block_gas_limit.saturating_sub(self.block_regular_gas_used); + let state_available = block_gas_limit.saturating_sub(self.block_state_gas_used); + if regular_limit > regular_available { + Some((transaction_gas_limit, regular_available)) + } else if transaction_gas_limit > state_available { + Some((transaction_gas_limit, state_available)) + } else { + None + } + } else { + let available = block_gas_limit.saturating_sub(self.cumulative_gas_used); + (regular_limit > available).then_some((transaction_gas_limit, available)) + }; + if let Some((transaction_gas_limit, block_available_gas)) = unavailable { + return Err(BlockValidationError::TransactionGasLimitMoreThanAvailableBlockGas { + transaction_gas_limit, + block_available_gas, + } + .into()); + } + Ok(()) + } + + fn execute_transaction_with_commit_condition( + &mut self, + tx: impl ExecutableTx, + commit: impl FnOnce(&TxResult, TxStateChanges<'_>) -> CommitChanges, + ) -> Result, BlockExecutionError> { + let (transaction, tx) = tx.into_parts(); + let tx = tx.borrow(); + self.set_transaction_block_access_index(); + self.validate_transaction_gas_limit(tx.gas_limit())?; + let blob_gas_used = tx.blob_gas_used().unwrap_or_default(); + self.validate_transaction_blob_gas(blob_gas_used)?; + let tx_type = tx.tx_type(); + let Some(outcome) = execution::execute_transaction_with_condition( + &mut self.evm, + &mut self.block_state, + self.state_update_hook.is_some(), + &mut |state| emit_state(&mut self.state_update_hook, state), + &transaction, + commit, + ) + .map_err(|error| map_transaction_error(error, *tx.tx_hash()))? + else { + return Ok(None); + }; + Ok(Some(self.record_receipt(tx_type, blob_gas_used, outcome))) + } + + fn execute_transaction_without_commit( + &mut self, + tx: impl ExecutableTx, + ) -> Result { + let (transaction, tx) = tx.into_parts(); + let tx = tx.borrow(); + self.set_transaction_block_access_index(); + self.validate_transaction_gas_limit(tx.gas_limit())?; + let blob_gas_used = tx.blob_gas_used().unwrap_or_default(); + self.validate_transaction_blob_gas(blob_gas_used)?; + let tx_type = tx.tx_type(); + let result = execute_transaction_without_commit(&mut self.evm, &transaction) + .map_err(|err| map_transaction_error(err, *tx.tx_hash()))?; + Ok(EthTransactionResultWithState::new(result, tx_type, blob_gas_used)) + } + + fn commit_transaction( + &mut self, + output: Self::TransactionResultWithState, + ) -> Result { + let EthTransactionResultWithState { result, tx_type, blob_gas_used } = output; + self.set_transaction_block_access_index(); + let stream_state = self.state_update_hook.is_some(); + let outcome = commit_detached_transaction( + &mut self.evm, + &mut self.block_state, + stream_state, + &mut |state| emit_state(&mut self.state_update_hook, state), + result, + ); + Ok(self.record_receipt(tx_type, blob_gas_used, outcome)) + } + + fn receipts(&self) -> &[Self::Receipt] { + &self.receipts + } + + fn finish_with_block_access_list( + mut self, + ) -> Result<(BlockExecutionOutput, Option), BlockExecutionError> { + let EthPostExecutionOutput { requests, gas_used, blob_gas_used } = + self.apply_post_execution_changes(0)?; + let block_access_list = self.take_block_access_list(); + let output = BlockExecutionOutput::new( + BlockExecutionResult { receipts: self.receipts, requests, gas_used, blob_gas_used }, + self.block_state.into_bundle(), + ); + Ok((output, block_access_list)) + } +} + +/// Returns the block header's gas used: the larger of regular and state gas under EIP-8037, +/// receipt gas otherwise. +pub const fn final_block_gas_used( + separate_block_gas: bool, + cumulative_gas_used: u64, + block_regular_gas_used: u64, + block_state_gas_used: u64, +) -> u64 { + if separate_block_gas { + if block_regular_gas_used > block_state_gas_used { + block_regular_gas_used + } else { + block_state_gas_used + } + } else { + cumulative_gas_used + } +} + +fn emit_state(hook: &mut StateUpdateHook, state: StateUpdate) { + if let Some(hook) = hook.as_mut() { + hook(state); + } +} diff --git a/crates/evm2/src/block/ethereum/tests.rs b/crates/evm2/src/block/ethereum/tests.rs new file mode 100644 index 000000000..01fa9a70a --- /dev/null +++ b/crates/evm2/src/block/ethereum/tests.rs @@ -0,0 +1,281 @@ +use super::*; +use crate::{ + BaseEvmTypes, Precompiles, SpecId, + block::BlockExecutionError, + bytecode::Bytecode, + env::BlockEnvExt, + ethereum::ethereum_tx_registry, + evm::{ + AccountInfo, BEACON_ROOTS_ADDRESS, CONSOLIDATION_REQUEST_ADDRESS, HISTORY_STORAGE_ADDRESS, + InMemoryDB, WITHDRAWAL_REQUEST_ADDRESS, + }, + utils::b256_to_word, +}; +use alloc::vec; +use alloy_consensus::{ + SignableTransaction, TxLegacy, constants::ETH_TO_WEI, transaction::Recovered, +}; +use alloy_eips::{ + eip2935::HISTORY_STORAGE_CODE, eip4788::BEACON_ROOTS_CODE, + eip7002::WITHDRAWAL_REQUEST_PREDEPLOY_CODE, eip7251::CONSOLIDATION_REQUEST_PREDEPLOY_CODE, +}; +use alloy_primitives::{Signature, TxKind, U256}; + +const SENDER: Address = Address::repeat_byte(0x01); +const RECIPIENT: Address = Address::repeat_byte(0x02); +const BENEFICIARY: Address = Address::repeat_byte(0x03); +const PARENT_HASH: B256 = B256::repeat_byte(0x11); +const PARENT_BEACON_BLOCK_ROOT: B256 = B256::repeat_byte(0x22); + +fn database() -> InMemoryDB { + let mut database = InMemoryDB::default(); + let sender = AccountInfo { balance: U256::from(ETH_TO_WEI), ..AccountInfo::default() }; + database.insert_account_info(&SENDER, sender); + for (address, code) in [ + (HISTORY_STORAGE_ADDRESS, &HISTORY_STORAGE_CODE), + (BEACON_ROOTS_ADDRESS, &BEACON_ROOTS_CODE), + (WITHDRAWAL_REQUEST_ADDRESS, &WITHDRAWAL_REQUEST_PREDEPLOY_CODE), + (CONSOLIDATION_REQUEST_ADDRESS, &CONSOLIDATION_REQUEST_PREDEPLOY_CODE), + ] { + let code = Bytecode::new_legacy(code.clone()); + database.insert_account_info(&address, AccountInfo::default().with_code(code)); + } + database +} + +fn evm(spec: SpecId, gas_limit: u64) -> Evm<'static, BaseEvmTypes> { + evm_with(spec, gas_limit, database()) +} + +fn evm_with(spec: SpecId, gas_limit: u64, database: InMemoryDB) -> Evm<'static, BaseEvmTypes> { + let block = BlockEnvExt { + number: U256::ONE, + timestamp: U256::from(12), + beneficiary: BENEFICIARY, + gas_limit: U256::from(gas_limit), + ..BlockEnvExt::default() + }; + Evm::new(spec, block, ethereum_tx_registry(spec), database, Precompiles::base(spec)) +} + +fn ctx<'a>() -> EthBlockExecutionCtx<'a> { + EthBlockExecutionCtx { + parent_hash: PARENT_HASH, + parent_beacon_block_root: Some(PARENT_BEACON_BLOCK_ROOT), + ..EthBlockExecutionCtx::default() + } +} + +fn executor<'a>( + spec: SpecId, + gas_limit: u64, + ctx: EthBlockExecutionCtx<'a>, + rules: EthBlockRules, +) -> EthBlockExecutor<'a> { + let evm = evm(spec, gas_limit); + EthBlockExecutor::new(evm, ctx, rules, EthReceiptBuilder) +} + +fn transfer(gas_limit: u64) -> Recovered> { + let tx = TxLegacy { + nonce: 0, + gas_price: 1, + gas_limit, + to: TxKind::Call(RECIPIENT), + value: U256::ONE, + ..TxLegacy::default() + }; + let signature = Signature::new(U256::ONE, U256::ONE, false); + Recovered::new_unchecked(EthereumTxEnvelope::Legacy(tx.into_signed(signature)), SENDER) +} + +fn blob_transfer() -> Recovered> { + let tx = TxEip4844 { + gas_limit: 21_000, + to: RECIPIENT, + blob_versioned_hashes: vec![B256::ZERO], + ..TxEip4844::default() + }; + let signature = Signature::new(U256::ONE, U256::ONE, false); + Recovered::new_unchecked(EthereumTxEnvelope::Eip4844(tx.into_signed(signature)), SENDER) +} + +fn balance(state: &BundleState, address: Address) -> Option { + state.account(&address).and_then(|account| account.info.as_ref()).map(|info| info.balance) +} + +fn storage(state: &BundleState, address: Address, slot: U256) -> Option { + state.storage(&address, slot) +} + +#[test] +fn executes_prague_block() { + let mut executor = executor(SpecId::PRAGUE, 30_000_000, ctx(), EthBlockRules::default()); + executor.apply_pre_execution_changes().unwrap(); + let gas = executor.execute_transaction(&transfer(21_000)).unwrap(); + assert_eq!(gas.tx_gas_used(), 21_000); + + let output = executor.finish().unwrap(); + let [receipt] = output.result.receipts.as_slice() else { panic!("expected one receipt") }; + assert!(receipt.is_success()); + assert_eq!(receipt.cumulative_gas_used(), 21_000); + assert_eq!(output.result.gas_used, 21_000); + assert!(output.result.requests.is_empty()); + + let state = &output.state; + assert_eq!(balance(state, RECIPIENT), Some(U256::ONE)); + // EIP-2935 stores the parent hash at slot `(number - 1) % 8191`. + let parent_hash = b256_to_word(PARENT_HASH); + assert_eq!(storage(state, HISTORY_STORAGE_ADDRESS, U256::ZERO), Some(parent_hash)); + // EIP-4788 stores the timestamp at `timestamp % 8191` and the root 8191 slots later. + let root = b256_to_word(PARENT_BEACON_BLOCK_ROOT); + assert_eq!(storage(state, BEACON_ROOTS_ADDRESS, U256::from(12)), Some(U256::from(12))); + assert_eq!(storage(state, BEACON_ROOTS_ADDRESS, U256::from(12 + 8191)), Some(root)); +} + +#[test] +fn rejects_cancun_block_without_beacon_root() { + let ctx = EthBlockExecutionCtx { parent_beacon_block_root: None, ..ctx() }; + let mut executor = executor(SpecId::CANCUN, 30_000_000, ctx, EthBlockRules::default()); + let error = executor.apply_pre_execution_changes().unwrap_err(); + assert!(matches!( + error, + BlockExecutionError::Validation(BlockValidationError::MissingParentBeaconBlockRoot) + )); +} + +#[test] +fn rejects_transaction_over_block_gas() { + let mut executor = executor(SpecId::PRAGUE, 30_000, ctx(), EthBlockRules::default()); + executor.apply_pre_execution_changes().unwrap(); + executor.execute_transaction(&transfer(21_000)).unwrap(); + let error = executor.execute_transaction(&transfer(21_000)).unwrap_err(); + assert!(matches!( + error, + BlockExecutionError::Validation( + BlockValidationError::TransactionGasLimitMoreThanAvailableBlockGas { + transaction_gas_limit: 21_000, + block_available_gas: 9_000, + } + ) + )); +} + +#[test] +fn discards_uncommitted_transaction() { + let mut executor = executor(SpecId::PRAGUE, 30_000_000, ctx(), EthBlockRules::default()); + executor.apply_pre_execution_changes().unwrap(); + let gas = executor + .execute_transaction_with_commit_condition(&transfer(21_000), |_, _| CommitChanges::No) + .unwrap(); + assert_eq!(gas, None); + + let output = executor.finish().unwrap(); + assert!(output.result.receipts.is_empty()); + assert_eq!(balance(&output.state, RECIPIENT), None); +} + +#[test] +fn pays_block_and_ommer_rewards() { + let ommer_beneficiary = Address::repeat_byte(0x04); + let ommers = [Header { beneficiary: ommer_beneficiary, number: 0, ..Header::default() }]; + let ctx = EthBlockExecutionCtx { ommers: Cow::Borrowed(&ommers), ..ctx() }; + let reward = 2 * ETH_TO_WEI; + let rules = EthBlockRules { base_block_reward: Some(reward), ..EthBlockRules::default() }; + let mut executor = executor(SpecId::PETERSBURG, 30_000_000, ctx, rules); + executor.apply_pre_execution_changes().unwrap(); + + let output = executor.finish().unwrap(); + assert_eq!(balance(&output.state, BENEFICIARY), Some(U256::from(reward + reward / 32))); + // An ommer one block back earns 7/8 of the block reward. + assert_eq!(balance(&output.state, ommer_beneficiary), Some(U256::from(reward * 7 / 8))); +} + +#[test] +fn applies_withdrawals_from_shanghai() { + let withdrawals = [Withdrawal { address: RECIPIENT, amount: 5, ..Withdrawal::default() }]; + let ctx = EthBlockExecutionCtx { withdrawals: Some(Cow::Borrowed(&withdrawals)), ..ctx() }; + let mut shanghai = + executor(SpecId::SHANGHAI, 30_000_000, ctx.clone(), EthBlockRules::default()); + shanghai.apply_pre_execution_changes().unwrap(); + let output = shanghai.finish().unwrap(); + assert_eq!(balance(&output.state, RECIPIENT), Some(U256::from(5_000_000_000u64))); + + let mut merge = executor(SpecId::MERGE, 30_000_000, ctx, EthBlockRules::default()); + merge.apply_pre_execution_changes().unwrap(); + let output = merge.finish().unwrap(); + assert_eq!(balance(&output.state, RECIPIENT), None); +} + +#[test] +fn reads_rules_from_hardforks() { + let mainnet = alloy_hardforks::EthereumChainHardforks::mainnet(); + let dao = EthBlockRules::new(&mainnet, 1_920_000, None); + assert_eq!(dao.base_block_reward, Some(5 * ETH_TO_WEI)); + assert!(dao.dao_fork_transition); + let merged = EthBlockRules::new(&mainnet, 15_537_394, None); + assert_eq!(merged, EthBlockRules::default()); +} + +#[test] +fn request_contract_without_code_produces_no_requests() { + let mut database = database(); + database.insert_account_info(&WITHDRAWAL_REQUEST_ADDRESS, AccountInfo::default()); + database.insert_account_info(&CONSOLIDATION_REQUEST_ADDRESS, AccountInfo::default()); + let evm = evm_with(SpecId::PRAGUE, 30_000_000, database); + let mut executor = + EthBlockExecutor::new(evm, ctx(), EthBlockRules::default(), EthReceiptBuilder); + executor.apply_pre_execution_changes().unwrap(); + let output = executor.finish().unwrap(); + assert!(output.result.requests.is_empty()); +} + +#[test] +fn rejects_transaction_over_block_blob_gas() { + let rules = EthBlockRules { max_blob_gas_per_block: Some(0), ..EthBlockRules::default() }; + let mut executor = executor(SpecId::CANCUN, 30_000_000, ctx(), rules); + executor.apply_pre_execution_changes().unwrap(); + let error = executor.execute_transaction(&blob_transfer()).unwrap_err(); + assert!(matches!( + error, + BlockExecutionError::Validation( + BlockValidationError::TransactionBlobGasMoreThanAvailableBlockBlobGas { + transaction_blob_gas: 131_072, + block_available_blob_gas: 0, + } + ) + )); +} + +#[test] +fn drains_dao_accounts_before_transactions() { + let drained = dao_fork::DAO_HARDFORK_ACCOUNTS[0]; + let mut database = database(); + let balance_before = AccountInfo { balance: U256::from(7), nonce: 1, ..AccountInfo::default() }; + database.insert_account_info(&drained, balance_before); + let evm = evm_with(SpecId::HOMESTEAD, 30_000_000, database); + let rules = EthBlockRules { dao_fork_transition: true, ..EthBlockRules::default() }; + let mut executor = EthBlockExecutor::new(evm, ctx(), rules, EthReceiptBuilder); + executor.apply_pre_execution_changes().unwrap(); + + let state = executor.into_state(); + assert_eq!(balance(&state, drained), Some(U256::ZERO)); + assert_eq!(balance(&state, dao_fork::DAO_HARDFORK_BENEFICIARY), Some(U256::from(7))); + // Drained accounts stay, empty ones and absent ones included. + assert_eq!(balance(&state, dao_fork::DAO_HARDFORK_ACCOUNTS[1]), Some(U256::ZERO)); +} + +#[test] +fn zero_reward_removes_empty_account_from_spurious_dragon() { + let mut database = database(); + database.insert_account_info(&BENEFICIARY, AccountInfo::default()); + let rules = EthBlockRules { base_block_reward: Some(0), ..EthBlockRules::default() }; + let run = |spec| { + let evm = evm_with(spec, 30_000_000, database.clone()); + let mut executor = EthBlockExecutor::new(evm, ctx(), rules, EthReceiptBuilder); + executor.apply_pre_execution_changes().unwrap(); + executor.finish().unwrap().state + }; + assert!(run(SpecId::SPURIOUS_DRAGON).account(&BENEFICIARY).unwrap().info.is_none()); + assert!(run(SpecId::TANGERINE).account(&BENEFICIARY).is_none()); +} diff --git a/crates/evm2/src/block/mod.rs b/crates/evm2/src/block/mod.rs new file mode 100644 index 000000000..3501fb08e --- /dev/null +++ b/crates/evm2/src/block/mod.rs @@ -0,0 +1,314 @@ +//! Block execution. +//! +//! [`BlockExecutor`] executes the transactions of one block on an [`Evm`](crate::Evm), applies +//! the block's pre- and post-execution changes, and collects receipts and the block's +//! [`BundleState`]. [`ethereum::EthBlockExecutor`] implements it with Ethereum's block rules. + +use crate::{EvmTypes, EvmTypesHost, TxResult, TxResultWithState, TxStateChanges, evm::Bal}; +use alloc::{sync::Arc, vec::Vec}; +use alloy_consensus::transaction::{Recovered, TransactionEnvelope}; +use alloy_eip7928::BlockAccessIndex; +use alloy_eips::eip7685::Requests; +use alloy_primitives::Log; +use core::borrow::Borrow; + +pub mod bundle_state; +pub use bundle_state::BundleState; + +mod error; +pub use error::{ + BlockExecutionError, BlockValidationError, InternalBlockExecutionError, InvalidTxError, + map_database_error, map_handler_error, +}; + +pub mod ethereum; + +mod state; +pub use state::{AccountUpdate, BlockState, BundleSource, StateUpdate, TransactionChanges}; + +/// The transaction environment executed by a [`BlockExecutor`]. +pub type EvmTxOf = <::EvmTypes as EvmTypesHost>::Tx; + +/// Gas used by a successfully executed transaction. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct GasOutput { + /// Gas used for receipt accounting. + tx_gas_used: u64, + /// Regular gas used for Amsterdam block-capacity accounting. + regular_gas_used: u64, + /// Gas used for state-capacity accounting. + state_gas_used: u64, +} + +impl GasOutput { + /// Creates a new gas output. + pub const fn new(tx_gas_used: u64, state_gas_used: u64) -> Self { + Self { tx_gas_used, regular_gas_used: tx_gas_used, state_gas_used } + } + + /// Creates a gas output with independent receipt, regular, and state gas accounting. + pub const fn new_with_regular( + tx_gas_used: u64, + regular_gas_used: u64, + state_gas_used: u64, + ) -> Self { + Self { tx_gas_used, regular_gas_used, state_gas_used } + } + + /// Returns transaction gas used for receipt accounting. + pub const fn tx_gas_used(&self) -> u64 { + self.tx_gas_used + } + + /// Returns regular gas used for Amsterdam block-capacity accounting. + pub const fn regular_gas_used(&self) -> u64 { + self.regular_gas_used + } + + /// Returns state gas used for state-capacity accounting. + pub const fn state_gas_used(&self) -> u64 { + self.state_gas_used + } +} + +impl From for GasOutput { + fn from(gas_used: u64) -> Self { + Self::new(gas_used, gas_used) + } +} + +/// Whether a block executor commits an executed transaction. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum CommitChanges { + /// Transaction should be committed into block executor's state. + Yes, + /// Transaction should not be committed. + No, +} + +impl CommitChanges { + /// Returns `true` if transaction should be committed into block executor's state. + pub const fn should_commit(self) -> bool { + matches!(self, Self::Yes) + } +} + +/// Context for building a transaction receipt from an execution result. +#[derive(Clone, Debug)] +pub struct ReceiptBuilderCtx { + /// Transaction type used by the receipt. + pub tx_type: TxType, + /// Raw transaction execution result. + pub result: TransactionResult, + /// Cumulative gas used by this transaction and all prior transactions in the block. + pub cumulative_gas_used: u64, +} + +/// Builds chain-specific receipts from raw transaction execution results. +#[auto_impl::auto_impl(&, Arc)] +pub trait ReceiptBuilder { + /// Consensus transaction type accepted by the executor. + type Transaction: TransactionEnvelope; + /// Receipt produced by this builder. + type Receipt: alloy_consensus::TxReceipt; + + /// Builds a receipt for the transaction execution result. + fn build_receipt( + &self, + ctx: ReceiptBuilderCtx<::TxType, TxResult>, + ) -> Self::Receipt; +} + +/// A transaction a [`BlockExecutor`] can execute: the EVM transaction environment with its +/// recovered signer, and the consensus transaction it was built from. +pub trait ExecutableTx { + /// Consensus transaction accessor. + type Tx: Borrow; + + /// Splits the transaction into its EVM environment and consensus transaction. + fn into_parts(self) -> (Recovered>, Self::Tx); +} + +impl ExecutableTx for (Recovered>, Tx) +where + E: BlockExecutor, + Tx: Borrow, +{ + type Tx = Tx; + + fn into_parts(self) -> (Recovered>, Tx) { + self + } +} + +impl<'a, E> ExecutableTx for &'a Recovered +where + E: BlockExecutor, + EvmTxOf: From, +{ + type Tx = &'a E::Transaction; + + fn into_parts(self) -> (Recovered>, &'a E::Transaction) { + let tx = Recovered::new_unchecked(self.clone_inner().into(), self.signer()); + (tx, self.inner()) + } +} + +/// A detached block transaction output containing its raw execution result. +pub trait BlockTransactionResult { + /// Returns the raw transaction execution result. + fn result(&self) -> &TxResultWithState; + + /// Consumes the output and returns the raw transaction execution result. + fn into_result(self) -> TxResultWithState; +} + +/// The result of executing a block. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct BlockExecutionResult { + /// All the receipts of the transactions in the block. + pub receipts: Vec, + /// All the EIP-7685 requests in the block. + pub requests: Requests, + /// The total gas used by the block. + pub gas_used: u64, + /// Blob gas used by the block. + pub blob_gas_used: u64, +} + +impl Default for BlockExecutionResult { + fn default() -> Self { + Self { receipts: Vec::new(), requests: Requests::default(), gas_used: 0, blob_gas_used: 0 } + } +} + +/// [`BlockExecutionResult`] combined with state. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct BlockExecutionOutput { + /// All the receipts of the transactions in the block. + pub result: BlockExecutionResult, + /// The changed state of the block after execution. + pub state: BundleState, +} + +impl BlockExecutionOutput { + /// Combines execution results with their persistent state. + pub const fn new(result: BlockExecutionResult, state: BundleState) -> Self { + Self { result, state } + } +} + +/// A configured block executor. +pub trait BlockExecutor: Sized { + /// Consensus transaction type executed by this executor. + type Transaction; + /// Receipt type produced by this executor. + type Receipt; + /// Runtime EVM type family. + type EvmTypes: EvmTypes; + /// EVM instance used by this executor. + type Evm; + /// Owned transaction execution result and detached state changes. + type TransactionResultWithState: BlockTransactionResult; + + /// Returns the underlying EVM. + fn evm(&self) -> &Self::Evm; + + /// Returns the underlying EVM mutably. + fn evm_mut(&mut self) -> &mut Self::Evm; + + /// Sets a hook for state updates emitted during block execution, including pre-execution + /// block changes and transaction changes. + /// + /// Returns `true` if the hook was installed. + fn set_state_hook(&mut self, _hook: impl FnMut(StateUpdate) + Send + 'static) -> bool { + false + } + + /// Attaches an EIP-7928 block access list for indexed state reads. + fn set_block_access_list(&mut self, block_access_list: Arc); + + /// Sets the EIP-7928 block access index used by state reads and BAL construction. + fn set_block_access_index(&mut self, index: BlockAccessIndex); + + /// Enables construction of an EIP-7928 block access list from committed state changes. + fn enable_block_access_list_builder(&mut self); + + /// Takes the block access list built from committed state changes. + fn take_block_access_list(&mut self) -> Option; + + /// Applies pre-execution block changes. + fn apply_pre_execution_changes(&mut self) -> Result<(), BlockExecutionError>; + + /// Consumes the executor and returns the state accumulated so far without applying + /// post-execution block changes. + fn into_state(self) -> BundleState; + + /// Executes a transaction, invokes `f` with the borrowed execution result and state changes, + /// and commits changes when `f` returns [`CommitChanges::Yes`]. + fn execute_transaction_with_commit_condition( + &mut self, + tx: impl ExecutableTx, + f: impl FnOnce(&TxResult, TxStateChanges<'_>) -> CommitChanges, + ) -> Result, BlockExecutionError> { + let output = self.execute_transaction_without_commit(tx)?; + let result = output.result(); + if f(&result.result, result.pending_state.changes()).should_commit() { + self.commit_transaction(output).map(Some) + } else { + Ok(None) + } + } + + /// Validates transaction gas admission against the executor's current block budget. + fn validate_transaction_gas_limit(&mut self, gas_limit: u64) + -> Result<(), BlockExecutionError>; + + /// Executes a transaction and detaches its state changes without committing them. + fn execute_transaction_without_commit( + &mut self, + tx: impl ExecutableTx, + ) -> Result; + + /// Commits detached transaction state and records its receipt and gas accounting. + fn commit_transaction( + &mut self, + output: Self::TransactionResultWithState, + ) -> Result; + + /// Executes a transaction, invokes `f` with the borrowed execution result, and commits + /// changes. + fn execute_transaction_with_result_closure( + &mut self, + tx: impl ExecutableTx, + f: impl FnOnce(&TxResult), + ) -> Result { + self.execute_transaction_with_commit_condition(tx, |result, _| { + f(result); + CommitChanges::Yes + }) + .map(Option::unwrap_or_default) + } + + /// Executes a transaction and commits changes. + fn execute_transaction( + &mut self, + tx: impl ExecutableTx, + ) -> Result { + self.execute_transaction_with_result_closure(tx, |_| {}) + } + + /// Returns receipts accumulated so far. + fn receipts(&self) -> &[Self::Receipt]; + + /// Finishes block execution and returns the output with the block access list built during + /// execution, if its builder was enabled. + fn finish_with_block_access_list( + self, + ) -> Result<(BlockExecutionOutput, Option), BlockExecutionError>; + + /// Finishes block execution and returns the output. + fn finish(self) -> Result, BlockExecutionError> { + self.finish_with_block_access_list().map(|(output, _)| output) + } +} diff --git a/crates/evm2/src/block/state.rs b/crates/evm2/src/block/state.rs new file mode 100644 index 000000000..6e2b45b07 --- /dev/null +++ b/crates/evm2/src/block/state.rs @@ -0,0 +1,701 @@ +//! Block state accumulated from committed transactions. + +use super::bundle_state::{ + AccountStatus, BundleRetention, BundleState, StorageSlot, TransitionAccount, TransitionState, +}; +use crate::{bytecode::Bytecode, evm::AccountInfo}; +use alloc::vec::Vec; +use alloy_primitives::{ + Address, B256, U256, + map::{AddressMap, B256Map, U256Map, hash_map::Entry}, +}; + +/// One block's account transitions, finalized into a [`BundleState`] with their reverts. +#[derive(Debug, Default)] +pub struct BlockState { + transitions: AddressMap, + contracts: B256Map, +} + +impl BlockState { + /// Creates an empty block accumulator. + pub fn new() -> Self { + Self::default() + } + + /// Applies a collected transaction to the block's transitions. + pub fn commit(&mut self, changes: &TransactionChanges) { + self.contracts.extend(changes.contracts.iter().map(|(hash, code)| (*hash, code.clone()))); + for (address, account) in &changes.accounts { + self.commit_account( + *address, + account.original.clone(), + (!account.selfdestructed).then(|| account.info.clone()), + account.created, + account.wiped, + account.storage.iter().filter_map(|(key, slot)| { + // A wipe reinserts every nonzero value that follows it. + (slot.is_changed() || account.wiped && !slot.present_value().is_zero()) + .then_some((*key, *slot)) + }), + ); + } + } + + /// Commits one account's finalized evm2 transaction changes into the block without + /// materializing EVM state. Accounts that were only loaded are skipped. + /// + /// When `update` is set, the committed account is also recorded into it for execution state + /// hooks. + pub fn commit_account_changes( + &mut self, + changes: crate::evm::AccountChanges<'_>, + update: Option<&mut StateUpdate>, + ) { + if !changes.is_changed() { + return; + } + if let Some((hash, code)) = changes.code { + self.contracts.entry(hash).or_insert_with(|| code.clone()); + } + let address = changes.address; + let created = changes.created; + let wiped = changes.storage.wiped; + let original = changes.original.map(AccountInfo::clone_no_code); + let current = changes.current.map(AccountInfo::clone_no_code); + let slots = changes + .storage + .changed_slots() + .map(|(&key, value)| (key, StorageSlot::new_changed(value.original, value.current))); + let Some(update) = update else { + self.commit_account(address, original, current, created, wiped, slots); + return; + }; + let storage: Vec<_> = slots.collect(); + self.commit_account( + address, + original.clone(), + current.clone(), + created, + wiped, + storage.iter().copied(), + ); + update.accounts.push(AccountUpdate { address, original, current, created, wiped, storage }); + } + + fn commit_account( + &mut self, + address: Address, + original: Option, + current: Option, + created: bool, + wiped: bool, + storage: impl Iterator, + ) { + let entry = self.transitions.entry(address); + let previous_status = match &entry { + Entry::Occupied(entry) => entry.get().status, + Entry::Vacant(_) => loaded_status(original.as_ref()), + }; + let status = if current.is_none() { + previous_status.on_selfdestructed() + } else if wiped { + AccountStatus::DestroyedChanged + } else if created { + previous_status.on_created() + } else { + previous_status.on_changed( + original.as_ref().is_none_or(|i| i.nonce == 0 && i.is_empty_code_hash()), + ) + }; + match entry { + Entry::Vacant(entry) => { + entry.insert(TransitionAccount { + info: current, + status, + previous_info: original, + previous_status, + storage: storage.collect(), + storage_was_destroyed: wiped, + }); + } + Entry::Occupied(mut entry) => { + let account = entry.get_mut(); + account.info = current; + account.status = status; + // A deletion or wipe starts a new storage lifetime. + if wiped + || matches!(status, AccountStatus::Destroyed | AccountStatus::DestroyedAgain) + { + account.storage.clear(); + account.storage.extend(storage); + account.storage_was_destroyed = true; + } else { + // After a wipe, every nonzero value is reinserted, so a slot that returns to + // its original value stays. + let wiped_lifetime = account.storage_was_destroyed || status.was_destroyed(); + for (key, slot) in storage { + match account.storage.entry(key) { + Entry::Vacant(entry) => { + entry.insert(slot); + } + Entry::Occupied(mut entry) => { + if !wiped_lifetime + && entry.get().original_value() == slot.present_value() + { + entry.remove(); + } else { + entry.get_mut().present_value = slot.present_value(); + } + } + } + } + } + } + } + } + + /// Finalizes one block's transitions and reverts. + pub fn into_bundle(self) -> BundleState { + self.into_bundle_with_retention(BundleRetention::Reverts) + } + + /// Finalizes one block's transitions, creating reverts only if `retention` asks for them. + pub fn into_bundle_with_retention(self, retention: BundleRetention) -> BundleState { + let mut bundle = BundleState::default(); + bundle.apply_transitions_and_create_reverts( + TransitionState { transitions: self.transitions }, + retention, + ); + bundle.contracts.extend(self.contracts); + bundle + } +} + +/// Transaction changes collected from an evm2 change stream for [`BlockState::commit`]. +#[derive(Debug, Default)] +pub struct TransactionChanges { + accounts: AddressMap, + contracts: B256Map, +} + +/// One account's changes within a [`TransactionChanges`]. +#[derive(Debug)] +struct TransactionAccount { + /// Account at the start of the transaction, without code. `None` means it did not exist. + original: Option, + /// Account after the transaction, without code. + info: AccountInfo, + created: bool, + selfdestructed: bool, + wiped: bool, + storage: U256Map, +} + +impl Default for TransactionAccount { + fn default() -> Self { + Self { + original: Some(AccountInfo::empty()), + info: AccountInfo::empty(), + created: false, + selfdestructed: false, + wiped: false, + storage: U256Map::default(), + } + } +} + +impl TransactionAccount { + fn update(&mut self, change: crate::evm::AccountChangeRef<'_>) { + self.original = change.original.map(AccountInfo::clone_no_code); + self.info = + change.current.map(AccountInfo::clone_no_code).unwrap_or_else(AccountInfo::empty); + self.created |= change.created; + self.selfdestructed |= change.current.is_none(); + } +} + +impl crate::evm::StateChangeSink for TransactionChanges { + type Error = core::convert::Infallible; + fn bytecode(&mut self, hash: B256, code: &Bytecode) -> Result<(), Self::Error> { + self.contracts.entry(hash).or_insert_with(|| code.clone()); + Ok(()) + } + fn storage_wipe(&mut self, address: Address) -> Result<(), Self::Error> { + let account = self.accounts.entry(address).or_default(); + account.wiped = true; + account.storage.clear(); + Ok(()) + } + fn storage(&mut self, change: crate::evm::StorageChange) -> Result<(), Self::Error> { + self.accounts + .entry(change.address) + .or_default() + .storage + .insert(change.key, StorageSlot::new_changed(change.original, change.current)); + Ok(()) + } + fn account(&mut self, change: crate::evm::AccountChangeRef<'_>) -> Result<(), Self::Error> { + self.accounts.entry(change.address).or_default().update(change); + Ok(()) + } + fn account_read( + &mut self, + address: Address, + info: Option<&AccountInfo>, + ) -> Result<(), Self::Error> { + if let Some(account) = self.accounts.get_mut(&address) { + account.update(crate::evm::AccountChangeRef { + address, + original: info, + current: info, + created: false, + selfdestructed: false, + }); + } + Ok(()) + } +} + +/// Per-transaction state changes streamed to execution state hooks. +/// +/// Holds only the accounts a transaction committed, with account info but without bytecode, so +/// building it does not copy the transaction's state maps. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct StateUpdate { + /// Committed accounts in stream order. + pub accounts: Vec, +} + +impl StateUpdate { + /// Returns whether the update commits no accounts. + pub const fn is_empty(&self) -> bool { + self.accounts.is_empty() + } + + /// Returns the update for `address`, if the transaction committed it. + pub fn account(&self, address: &Address) -> Option<&AccountUpdate> { + self.accounts.iter().find(|account| account.address == *address) + } +} + +/// One committed account in a [`StateUpdate`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AccountUpdate { + /// Account address. + pub address: Address, + /// Account at the start of the transaction, without code. `None` means it did not exist. + pub original: Option, + /// Account after the transaction, without code. `None` is a deletion. + pub current: Option, + /// Whether the account was created during the transaction. + pub created: bool, + /// Whether the account's prior storage was wiped. + pub wiped: bool, + /// Storage slots whose value changed. + pub storage: Vec<(U256, StorageSlot)>, +} + +/// Visits a persisted bundle as native changes when seeding an execution cache. +#[derive(Debug)] +pub struct BundleSource<'a>(pub &'a BundleState); + +impl crate::evm::StateChangeSource for BundleSource<'_> { + fn visit(&self, sink: &mut S) -> Result<(), S::Error> { + for (hash, code) in &self.0.contracts { + sink.bytecode(*hash, code)?; + } + for (address, account) in self.0.state() { + let current = account.info.as_ref(); + if account.was_destroyed() { + sink.storage_wipe(*address)?; + } + sink.account(crate::evm::AccountChangeRef { + address: *address, + original: account.original_info.as_ref(), + current, + created: account.status.is_storage_known() && current.is_some(), + selfdestructed: current.is_none(), + })?; + for (key, value) in &account.storage { + sink.storage(crate::evm::StorageChange { + address: *address, + key: *key, + original: value.original_value(), + current: value.present_value(), + })?; + } + } + Ok(()) + } +} + +fn loaded_status(info: Option<&AccountInfo>) -> AccountStatus { + match info { + None => AccountStatus::LoadedNotExisting, + Some(info) if info.is_empty() => AccountStatus::LoadedEmptyEIP161, + Some(_) => AccountStatus::Loaded, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + block::bundle_state::OriginalValuesKnown, + evm::{AccountChangeRef, AccountChanges, StateChangeSink, StorageChange, StorageOverlay}, + test_utils::storage_slot, + }; + + /// Builds a storage overlay from `(key, original, current)` slots. + fn overlay(wiped: bool, slots: &[(U256, U256, U256)]) -> StorageOverlay { + let mut storage = StorageOverlay { wiped, ..Default::default() }; + for &(key, original, current) in slots { + storage.slots.insert(key, storage_slot(original, current)); + } + storage + } + + #[cfg(feature = "account-ext")] + #[test] + fn account_extensions_survive_native_updates_and_reverts() { + let address = Address::with_last_byte(1); + let original = AccountInfo { + balance: U256::from(10), + extension: crate::evm::AccountExtension::copy_from_slice(b"original extension"), + ..Default::default() + }; + let mut updated = original.clone(); + updated.balance = U256::from(20); + updated.extension = crate::evm::AccountExtension::copy_from_slice(b"updated extension"); + let mut state = BlockState::new(); + state.commit_account_changes( + AccountChanges { + address, + original: Some(&original), + current: Some(&updated), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &StorageOverlay::default(), + }, + None, + ); + let mut bundle = state.into_bundle(); + let account = bundle.state.get(&address).unwrap(); + assert_eq!( + account.original_info.as_ref().unwrap().extension.as_ref(), + b"original extension" + ); + assert_eq!(account.info.as_ref().unwrap().extension.as_ref(), b"updated extension"); + assert!(bundle.revert_latest()); + assert_eq!( + bundle.account(&address).unwrap().info.as_ref().unwrap().extension.as_ref(), + b"original extension" + ); + } + + #[test] + fn native_sink_preserves_storage_lifetimes_and_reverts() { + let address = Address::with_last_byte(1); + let code = evm2::bytecode::Bytecode::new_raw(alloy_primitives::bytes!("60015b00")); + let info = AccountInfo::default().with_nonce(1).with_code(code.clone()); + let mut converted = BlockState::new(); + let mut native = BlockState::new(); + // Storage-only writes, restoration, deletion, recreation, a surviving storage wipe, + // and another deletion exercise both occupied and vacant transition entries. + for step in 0..7 { + let slots: &[_] = if step < 2 || step == 3 || step == 4 { + let (original, current) = if step == 1 { (8, 7) } else { (7, 8) }; + &[(U256::from(3), U256::from(original), U256::from(current))] + } else { + &[] + }; + let storage = overlay(step >= 2, slots); + let changes = if step < 2 { + AccountChanges { + address, + original: Some(&info), + current: Some(&info), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &storage, + } + } else { + AccountChanges { + address, + original: (step != 3 && step != 6).then_some(&info), + current: (step == 3 || step == 4).then_some(&info), + created: step == 3, + selfdestructed: step != 3, + touched: false, + code: (step == 3).then(|| (code.hash_slow(), &code)), + storage: &storage, + } + }; + let empty = StorageOverlay::default(); + // Reads alone must never introduce an account transition. + let read = AccountChanges { + address: Address::with_last_byte(2), + original: Some(&info), + current: Some(&info), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &empty, + }; + let mut transaction = TransactionChanges::default(); + let Ok(()) = changes.visit(&mut transaction); + let Ok(()) = read.visit(&mut transaction); + converted.commit(&transaction); + native.commit_account_changes(changes, None); + native.commit_account_changes(read, None); + assert_eq!(native.transitions, converted.transitions, "step {step}"); + assert_eq!(native.contracts, converted.contracts); + } + let mut native = native.into_bundle(); + let mut converted = converted.into_bundle(); + assert_eq!(native, converted); + assert!(native.revert_latest()); + assert!(converted.revert_latest()); + assert_eq!(native, converted); + } + + #[test] + fn stream_commit_keeps_unchanged_slot_after_wipe() { + let address = Address::with_last_byte(1); + let info = AccountInfo { nonce: 1, ..Default::default() }; + let mut changes = TransactionChanges::default(); + changes.storage_wipe(address).unwrap(); + changes + .storage(StorageChange { + address, + key: U256::from(3), + original: U256::from(5), + current: U256::from(5), + }) + .unwrap(); + changes.account_read(address, Some(&info)).unwrap(); + let mut block = BlockState::new(); + block.commit(&changes); + let bundle = block.into_bundle(); + assert_eq!(bundle.storage(&address, U256::from(3)), Some(U256::from(5))); + } + + #[test] + fn created_account_keeps_its_storage_wipe() { + let address = Address::with_last_byte(1); + let info = AccountInfo { balance: U256::ONE, ..Default::default() }; + let storage = overlay(true, &[]); + let mut block = BlockState::new(); + block.commit_account_changes( + AccountChanges { + address, + original: None, + current: Some(&info), + created: true, + selfdestructed: true, + touched: false, + code: None, + storage: &storage, + }, + None, + ); + let bundle = block.into_bundle(); + assert!(bundle.account(&address).unwrap().was_destroyed()); + let changeset = bundle.to_plain_state(OriginalValuesKnown::Yes); + assert!(changeset.storage.iter().any(|storage| storage.wipe_storage)); + } + + #[test] + fn slot_returning_to_original_after_wipe_is_reinserted() { + let address = Address::with_last_byte(1); + let info = AccountInfo { nonce: 1, ..Default::default() }; + let mut block = BlockState::new(); + for (wiped, slot) in [ + (true, (U256::from(3), U256::from(5), U256::from(6))), + (false, (U256::from(3), U256::from(6), U256::from(5))), + ] { + let storage = overlay(wiped, &[slot]); + block.commit_account_changes( + AccountChanges { + address, + original: Some(&info), + current: Some(&info), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &storage, + }, + None, + ); + } + let bundle = block.into_bundle(); + assert_eq!(bundle.storage(&address, U256::from(3)), Some(U256::from(5))); + } + + #[test] + fn live_account_wipe_drops_earlier_slots() { + let address = Address::with_last_byte(1); + let info = AccountInfo { nonce: 1, ..Default::default() }; + let mut block = BlockState::new(); + for (wiped, slots) in [ + (false, &[(U256::from(3), U256::ZERO, U256::ONE)][..]), + (true, &[(U256::from(4), U256::ZERO, U256::from(2))][..]), + ] { + let storage = overlay(wiped, slots); + block.commit_account_changes( + AccountChanges { + address, + original: Some(&info), + current: Some(&info), + created: false, + selfdestructed: false, + touched: false, + code: None, + storage: &storage, + }, + None, + ); + } + let bundle = block.into_bundle(); + let account = bundle.account(&address).unwrap(); + assert_eq!(account.storage_slot(U256::from(3)), Some(U256::ZERO)); + assert_eq!(account.storage_slot(U256::from(4)), Some(U256::from(2))); + } + + #[test] + fn cached_bytecode_does_not_change_bundle_output() { + let address = Address::with_last_byte(1); + let code = evm2::bytecode::Bytecode::new_raw(alloy_primitives::bytes!("60015b00")); + let info = AccountInfo { + balance: U256::from(5), + code_hash: code.hash_slow(), + ..Default::default() + }; + let bundle = |info: &AccountInfo| { + let mut changes = TransactionChanges::default(); + changes + .account(AccountChangeRef { + address, + original: None, + current: Some(info), + created: false, + selfdestructed: false, + }) + .unwrap(); + let mut block = BlockState::new(); + block.commit(&changes); + block.into_bundle() + }; + let uncached = bundle(&info); + let cached = bundle(&AccountInfo { code: Some(code), ..info }); + assert_eq!(uncached, cached); + assert!(cached.contracts.is_empty()); + } + + #[test] + fn deployed_bytecode_is_retained_separately_from_account_metadata() { + let address = Address::with_last_byte(1); + let code = evm2::bytecode::Bytecode::new_raw(alloy_primitives::bytes!("60015b00")); + let hash = code.hash_slow(); + let info = AccountInfo { + nonce: 1, + code_hash: hash, + code: Some(code.clone()), + ..Default::default() + }; + let mut changes = TransactionChanges::default(); + changes.bytecode(hash, &code).unwrap(); + changes + .account(AccountChangeRef { + address, + original: None, + current: Some(&info), + created: true, + selfdestructed: false, + }) + .unwrap(); + let mut block = BlockState::new(); + block.commit(&changes); + let bundle = block.into_bundle(); + assert_eq!(bundle.contracts[&hash].original_bytes(), code.original_bytes()); + assert_eq!(bundle.account(&address).unwrap().info.as_ref().unwrap().code_hash, hash); + } + + #[test] + fn storage_only_change_retains_account_and_reverts() { + let address = Address::with_last_byte(1); + let info = AccountInfo { nonce: 1, ..Default::default() }; + let mut changes = TransactionChanges::default(); + changes + .storage(StorageChange { + address, + key: U256::from(3), + original: U256::from(7), + current: U256::from(8), + }) + .unwrap(); + changes.account_read(address, Some(&info)).unwrap(); + let mut block = BlockState::new(); + block.commit(&changes); + let mut bundle = block.into_bundle(); + assert_eq!(bundle.account(&address).unwrap().info.as_ref().unwrap().nonce, 1); + assert_eq!(bundle.storage(&address, U256::from(3)), Some(U256::from(8))); + assert_eq!(bundle.reverts.len(), 1); + assert!(bundle.revert_latest()); + assert_eq!(bundle.storage(&address, U256::from(3)), Some(U256::from(7))); + } + + #[test] + fn destroy_then_recreate_retains_parent_storage_wipe() { + let address = Address::with_last_byte(1); + let original = AccountInfo { nonce: 1, balance: U256::from(7), ..Default::default() }; + let recreated = AccountInfo { nonce: 1, balance: U256::from(9), ..Default::default() }; + let mut block = BlockState::new(); + let mut destroyed = TransactionChanges::default(); + destroyed + .account(AccountChangeRef { + address, + original: Some(&original), + current: None, + created: false, + selfdestructed: true, + }) + .unwrap(); + block.commit(&destroyed); + let mut created = TransactionChanges::default(); + created.storage_wipe(address).unwrap(); + created + .storage(StorageChange { + address, + key: U256::from(3), + original: U256::ZERO, + current: U256::from(8), + }) + .unwrap(); + created + .account(AccountChangeRef { + address, + original: None, + current: Some(&recreated), + created: true, + selfdestructed: false, + }) + .unwrap(); + block.commit(&created); + let bundle = block.into_bundle(); + let account = bundle.account(&address).unwrap(); + assert!(account.was_destroyed()); + assert_eq!(account.original_info.as_ref().unwrap().balance, U256::from(7)); + assert_eq!(account.info.as_ref().unwrap().balance, U256::from(9)); + assert_eq!(account.storage_slot(U256::from(4)), Some(U256::ZERO)); + assert_eq!(account.storage_slot(U256::from(3)), Some(U256::from(8))); + assert!(bundle.reverts[0][0].1.wipe_storage); + } +} diff --git a/crates/evm2/src/bytecode/analysis.rs b/crates/evm2/src/bytecode/analysis.rs index fa8c236f3..e31f9b112 100644 --- a/crates/evm2/src/bytecode/analysis.rs +++ b/crates/evm2/src/bytecode/analysis.rs @@ -10,9 +10,13 @@ mod aarch64; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] mod x86; -/// Analyzes the original bytecode to produce a jump table. +/// Analyzes the original, unpadded legacy bytecode to produce its jump table. +/// +/// A `JUMPDEST` byte is a valid jump destination unless it is part of the data of a +/// `PUSH1..=PUSH32` instruction. This applies to every fork: the immediates of +/// `DUPN`/`SWAPN`/`EXCHANGE` are scanned as if they were opcodes. #[inline] -pub(super) fn analyze_legacy(bytecode: &[u8]) -> JumpTable { +pub fn analyze_legacy(bytecode: &[u8]) -> JumpTable { let mut jumps = JumpTable::new(bytecode.len()); let table = jumps.as_mut_slice(); let pc = analyze_simd(bytecode, table); @@ -463,7 +467,7 @@ mod tests { #[test] fn padding_releases_shared_input() { - let raw = Bytes::copy_from_slice(&[op::PUSH32]); + let raw = Bytes::from([op::PUSH32]); let padded = pad_legacy(raw.clone()); assert!(raw.is_unique()); assert_ne!(padded.as_ptr(), raw.as_ptr()); diff --git a/crates/evm2/src/bytecode/mod.rs b/crates/evm2/src/bytecode/mod.rs index 182c8564c..145b49e84 100644 --- a/crates/evm2/src/bytecode/mod.rs +++ b/crates/evm2/src/bytecode/mod.rs @@ -7,7 +7,7 @@ use crate::{ }; use alloc::sync::Arc; use alloy_primitives::{Address, B256, Bytes, KECCAK256_EMPTY, keccak256}; -use analysis::{analyze_legacy, pad_legacy}; +use analysis::pad_legacy; use core::{cmp::Ordering, fmt, hash}; use thiserror::Error; @@ -17,6 +17,7 @@ mod jump_table; #[cfg(feature = "serde")] mod serde_impl; +pub use analysis::analyze_legacy; pub use jump_table::{JumpTable, JumpTableRef}; /// EIP-7702 decode errors. @@ -196,13 +197,7 @@ impl Bytecode { #[inline] pub fn new_eip7702(address: Address) -> Self { let raw: Bytes = [EIP7702_MAGIC_BYTES, &[EIP7702_VERSION], &address[..]].concat().into(); - Self(Some(Arc::new(BytecodeInner { - kind: BytecodeKind::Eip7702, - original_len: raw.len(), - bytecode: raw, - jump_table: OnceLock::new(), - hash: OnceLock::new(), - }))) + Self::from_valid_eip7702(raw) } /// Creates a new raw [`Bytecode`]. @@ -217,11 +212,31 @@ impl Bytecode { } } + /// Creates a new [`Bytecode`] from code read from state. + /// + /// Valid EIP-7702 delegation designators become EIP-7702 bytecode and anything else is legacy + /// code. Unlike [`Self::new_raw_checked`], code starting with the EIP-7702 magic that is not a + /// valid designator is accepted as legacy, since it may have been deployed before EIP-3541. + #[inline] + pub fn from_stored(bytes: Bytes) -> Self { + if Self::check_eip7702(&bytes).is_ok() { + Self::from_valid_eip7702(bytes) + } else { + Self::new_legacy(bytes) + } + } + /// Creates a new EIP-7702 [`Bytecode`] from raw bytes. /// /// Returns an error if the bytes are not valid EIP-7702 bytecode. #[inline] pub fn new_eip7702_raw(bytes: Bytes) -> Result { + Self::check_eip7702(&bytes)?; + Ok(Self::from_valid_eip7702(bytes)) + } + + /// Checks that `bytes` is a valid EIP-7702 delegation designator. + fn check_eip7702(bytes: &[u8]) -> Result<(), Eip7702DecodeError> { if bytes.len() != EIP7702_BYTECODE_LEN { return Err(Eip7702DecodeError::InvalidLength); } @@ -231,13 +246,18 @@ impl Bytecode { if bytes[2] != EIP7702_VERSION { return Err(Eip7702DecodeError::UnsupportedVersion); } - Ok(Self(Some(Arc::new(BytecodeInner { + Ok(()) + } + + /// Wraps `bytes`, which [`Self::check_eip7702`] accepted, as EIP-7702 bytecode. + fn from_valid_eip7702(bytes: Bytes) -> Self { + Self(Some(Arc::new(BytecodeInner { kind: BytecodeKind::Eip7702, original_len: bytes.len(), bytecode: bytes, jump_table: OnceLock::new(), hash: OnceLock::new(), - })))) + }))) } /// Create new checked bytecode from pre-analyzed components. @@ -335,6 +355,20 @@ impl Bytecode { *self.inner().hash.get_or_init(|| keccak256(self.original_byte_slice())) } + /// Caches `hash` as the hash of the original bytecode unless one is already cached. + /// + /// `hash` must be the keccak256 of [`Self::original_byte_slice`]. Does nothing for empty + /// bytecode or the zero hash. + #[inline] + pub fn seed_hash(&self, hash: B256) { + if self.is_empty() || hash.is_zero() { + return; + } + if self.inner().hash.set(hash).is_err() { + debug_assert_eq!(self.hash_slow(), hash, "seeded hash differs from cached hash"); + } + } + /// Returns a reference to the potentially padded bytecode bytes. #[inline] pub fn bytes(&self) -> &Bytes { @@ -472,13 +506,13 @@ mod tests { raw[..2].copy_from_slice(EIP7702_MAGIC_BYTES); raw[2] = 1; assert_eq!( - Bytecode::new_eip7702_raw(Bytes::copy_from_slice(&raw)), + Bytecode::new_eip7702_raw(Bytes::from(raw)), Err(Eip7702DecodeError::UnsupportedVersion) ); raw[2] = EIP7702_VERSION; raw[3..7].copy_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF]); - let raw = Bytes::copy_from_slice(&raw); + let raw = Bytes::from(raw); let bytecode = Bytecode::new_eip7702_raw(raw.clone()).unwrap(); assert!(bytecode.is_eip7702()); assert_eq!(bytecode.eip7702_address(), Some(Address::from_slice(&raw[3..]))); @@ -487,13 +521,58 @@ mod tests { #[test] fn eip7702_from_address() { - let address = Address::new([0x01; 20]); + let address = Address::repeat_byte(0x01); let bytecode = Bytecode::new_eip7702(address); assert_eq!(bytecode.eip7702_address(), Some(address)); assert_eq!(bytecode.original_bytes().len(), EIP7702_BYTECODE_LEN); assert_eq!(&bytecode.original_byte_slice()[..3], &[0xEF, 0x01, 0x00]); } + #[test] + fn from_stored_accepts_non_designator_magic_as_legacy() { + let address = Address::repeat_byte(0x01); + let designator = Bytecode::new_eip7702(address).original_bytes(); + let from_designator = Bytecode::from_stored(designator.clone()); + assert_eq!(from_designator.kind(), BytecodeKind::Eip7702); + assert_eq!(from_designator.eip7702_address(), Some(address)); + + for bytes in [ + Bytes::from_static(&[0xEF, 0x01, 0x00, 0x60, 0x01]), + designator.slice(..EIP7702_BYTECODE_LEN - 1), + Bytes::from([designator.as_ref(), &[op::STOP]].concat()), + Bytes::from_static(&[0xEF, 0x01, 0x01]), + ] { + assert!(Bytecode::new_raw_checked(bytes.clone()).is_err()); + let bytecode = Bytecode::from_stored(bytes.clone()); + assert_eq!(bytecode.kind(), BytecodeKind::Legacy); + assert_eq!(bytecode.original_bytes(), bytes); + } + } + + #[test] + fn seed_hash_sets_only_an_empty_cache() { + let bytecode = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + let seed = B256::repeat_byte(0x5e); + bytecode.seed_hash(seed); + assert_eq!(bytecode.hash_slow(), seed); + + let empty = Bytecode::new(); + empty.seed_hash(seed); + assert_eq!(empty.hash_slow(), KECCAK256_EMPTY); + let bytecode = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + bytecode.seed_hash(B256::ZERO); + assert_eq!(bytecode.hash_slow(), keccak256([0x60, 0x01])); + } + + #[test] + #[cfg(debug_assertions)] + #[should_panic(expected = "seeded hash differs from cached hash")] + fn seed_hash_panics_on_conflicting_hash_in_debug() { + let bytecode = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + bytecode.seed_hash(B256::repeat_byte(0x5e)); + bytecode.seed_hash(B256::repeat_byte(0x5f)); + } + #[test] fn analysis_is_lazy_and_shared() { let raw = Bytes::from_static(&[op::JUMPDEST, op::PUSH1, op::JUMPDEST, op::PUSH32]); @@ -567,7 +646,7 @@ mod tests { fn existing_zero_suffix_is_original_code() { let mut raw = [0; 34]; raw[0] = op::PUSH32; - let raw = Bytes::copy_from_slice(&raw); + let raw = Bytes::from(raw); let bytecode = Bytecode::new_legacy(raw.clone()); assert_eq!(bytecode.bytes().as_ptr(), raw.as_ptr()); assert_eq!(bytecode.len(), raw.len()); diff --git a/crates/evm2/src/bytecode/serde_impl.rs b/crates/evm2/src/bytecode/serde_impl.rs index e9489421f..7dc23f2eb 100644 --- a/crates/evm2/src/bytecode/serde_impl.rs +++ b/crates/evm2/src/bytecode/serde_impl.rs @@ -44,7 +44,7 @@ impl<'de> Deserialize<'de> for Bytecode { fn deserialize>(deserializer: D) -> Result { if deserializer.is_human_readable() { match BytecodeSerde::deserialize(deserializer)? { - BytecodeSerde::New(bytes) => Ok(from_original_bytes(bytes)), + BytecodeSerde::New(bytes) => Ok(Self::from_stored(bytes)), BytecodeSerde::Old(bytecode_serde_old) => from_old(bytecode_serde_old), } } else { @@ -63,7 +63,7 @@ impl<'de> Visitor<'de> for BytecodeVisitor { } fn visit_bytes(self, value: &[u8]) -> Result { - Ok(from_original_bytes(Bytes::copy_from_slice(value))) + Ok(Bytecode::from_stored(Bytes::copy_from_slice(value))) } // Self-describing binary formats such as MessagePack may hold revm's enum encoding. @@ -86,19 +86,11 @@ fn from_old(old: BytecodeSerdeOld) -> Result { } } -/// Decodes bytecode from its serialized original bytes. -/// -/// Legacy code may start with the EIP-7702 magic without being a valid delegation (e.g. code -/// deployed before EIP-3541), so it falls back to legacy instead of failing to round-trip. -fn from_original_bytes(bytes: Bytes) -> Bytecode { - Bytecode::new_raw_checked(bytes.clone()).unwrap_or_else(|_| Bytecode::new_legacy(bytes)) -} - #[cfg(test)] mod tests { use super::*; use crate::bytecode::BytecodeKind; - use alloy_primitives::hex; + use alloy_primitives::{bytes, hex}; #[test] fn serde_roundtrip() { @@ -115,7 +107,7 @@ mod tests { #[test] fn serde_roundtrip_legacy_with_eip7702_magic() { - let bytes = Bytes::from_static(&hex!("ef0100")); + let bytes = bytes!("ef0100"); let bytecode = Bytecode::new_legacy(bytes.clone()); let json = serde_json::to_string(&bytecode).unwrap(); @@ -188,7 +180,7 @@ mod tests { #[test] fn messagepack_revm_cases() { - let legacy = Bytes::from_static(&hex!("5b6000")); + let legacy = bytes!("5b6000"); let delegated_address = Address::with_last_byte(0x42); let cases = [ (revm_bytecode::Bytecode::new_legacy(legacy.clone()), Bytecode::new_legacy(legacy)), diff --git a/crates/evm2/src/constants.rs b/crates/evm2/src/constants.rs index d99737f27..e44d46e7e 100644 --- a/crates/evm2/src/constants.rs +++ b/crates/evm2/src/constants.rs @@ -21,6 +21,11 @@ pub const MAX_CODE_SIZE_AMSTERDAM: usize = 0x10000; /// Maximum contract creation initcode size since Amsterdam. pub const MAX_INITCODE_SIZE_AMSTERDAM: usize = 2 * MAX_CODE_SIZE_AMSTERDAM; +/// Maximum transaction gas limit, including the state-gas reservoir, since Amsterdam. +/// +/// EIP-8037 caps `tx.gas` at `2^32 - 1` (execution-specs `TX_MAX_TOTAL_GAS_LIMIT`). +pub const MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM: u64 = (1 << 32) - 1; + /// Amsterdam blob base fee update fraction (BPO2 blob schedule). pub const BLOB_BASE_FEE_UPDATE_FRACTION_AMSTERDAM: u64 = 11_684_671; diff --git a/crates/evm2/src/ethereum/eip1559.rs b/crates/evm2/src/ethereum/eip1559.rs index 0bffc0178..8fb93e81c 100644 --- a/crates/evm2/src/ethereum/eip1559.rs +++ b/crates/evm2/src/ethereum/eip1559.rs @@ -72,7 +72,7 @@ pub fn prepare_with_hooks>( validate_execution_gas_limit_cap(req.host.version(), tx.gas_limit, intrinsic, floor_gas)?; let max_gas_cost = U256::from(tx.gas_limit) * max_fee_per_gas; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -85,7 +85,7 @@ pub fn prepare_with_hooks>( let effective_gas_cost = U256::from(tx.gas_limit) * gas_price; H::before_execution(req.host, req.envelope, caller, effective_gas_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared EIP-1559 transaction. @@ -93,12 +93,13 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, TxEip1559>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner(); let (execution_gas_limit, reservoir) = initial_gas_and_reservoir(req.host.version(), tx.gas_limit, intrinsic, initial_state_gas); - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(req.host.version().chain_id), @@ -106,16 +107,17 @@ pub fn execute_prepared>( }; let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir); - let frame = - prepare_initial_frame(req.host, caller, tx.nonce, tx.to, &tx.input, tx.value, &mut tx_gas)?; - let result = execute_initial_frame( + let frame = prepare_initial_frame( req.host, - &tx_env, - frame, + caller, + sender_nonce, + tx.to, + &tx.input, + tx.value, &mut tx_gas, - execution_gas_limit, - reservoir, )?; + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( req.host, req.envelope, diff --git a/crates/evm2/src/ethereum/eip2930.rs b/crates/evm2/src/ethereum/eip2930.rs index 655a6370d..a9227f86c 100644 --- a/crates/evm2/src/ethereum/eip2930.rs +++ b/crates/evm2/src/ethereum/eip2930.rs @@ -67,7 +67,7 @@ pub fn prepare_with_hooks>( validate_execution_gas_limit_cap(req.host.version(), tx.gas_limit, intrinsic, floor_gas)?; let max_gas_cost = U256::from(tx.gas_limit) * gas_price; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -79,7 +79,7 @@ pub fn prepare_with_hooks>( H::before_execution(req.host, req.envelope, caller, max_gas_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared EIP-2930 transaction. @@ -87,12 +87,13 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, TxEip2930>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner(); let (execution_gas_limit, reservoir) = initial_gas_and_reservoir(req.host.version(), tx.gas_limit, intrinsic, initial_state_gas); - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(req.host.version().chain_id), @@ -100,16 +101,17 @@ pub fn execute_prepared>( }; let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir); - let frame = - prepare_initial_frame(req.host, caller, tx.nonce, tx.to, &tx.input, tx.value, &mut tx_gas)?; - let result = execute_initial_frame( + let frame = prepare_initial_frame( req.host, - &tx_env, - frame, + caller, + sender_nonce, + tx.to, + &tx.input, + tx.value, &mut tx_gas, - execution_gas_limit, - reservoir, )?; + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( req.host, req.envelope, diff --git a/crates/evm2/src/ethereum/eip4844.rs b/crates/evm2/src/ethereum/eip4844.rs index 1c9812a99..c72ac33ab 100644 --- a/crates/evm2/src/ethereum/eip4844.rs +++ b/crates/evm2/src/ethereum/eip4844.rs @@ -13,7 +13,6 @@ use crate::{ evm::handler::{DefaultTxHandlerHooks, GasSettlement, TxHandlerHooks}, interpreter::GasTracker, registry::{HandlerError, HandlerResult, TxRequest}, - utils::b256_to_word, }; use alloy_consensus::transaction::TxEip4844Variant; use alloy_eips::eip4844::{DATA_GAS_PER_BLOB, VERSIONED_HASH_VERSION_KZG}; @@ -84,7 +83,7 @@ pub fn prepare_with_hooks>( let blob_gas_cost = U256::from(DATA_GAS_PER_BLOB) * U256::from(tx.blob_versioned_hashes.len()); let max_blob_gas_cost = blob_gas_cost * max_fee_per_blob_gas; let max_gas_cost = U256::from(tx.gas_limit) * max_fee_per_gas; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -98,7 +97,7 @@ pub fn prepare_with_hooks>( let blob_basefee_cost = blob_gas_cost * req.host.block.blob_basefee; H::before_execution(req.host, req.envelope, caller, effective_gas_cost + blob_basefee_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared EIP-4844 transaction. @@ -106,16 +105,17 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, TxEip4844Variant>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner().tx(); let (execution_gas_limit, reservoir) = initial_gas_and_reservoir(req.host.version(), tx.gas_limit, intrinsic, initial_state_gas); - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(req.host.version().chain_id), - blob_hashes: tx.blob_versioned_hashes.iter().copied().map(b256_to_word).collect(), + blob_hashes: tx.blob_versioned_hashes.iter().copied().map(Into::into).collect(), ext: T::TxEnvExt::default(), _non_exhaustive: (), }; @@ -124,20 +124,14 @@ pub fn execute_prepared>( let frame = prepare_initial_frame( req.host, caller, - tx.nonce, + sender_nonce, tx.to.into(), &tx.input, tx.value, &mut tx_gas, )?; - let result = execute_initial_frame( - req.host, - &tx_env, - frame, - &mut tx_gas, - execution_gas_limit, - reservoir, - )?; + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( req.host, req.envelope, @@ -223,7 +217,7 @@ mod tests { gas_limit: 21_000, max_fee_per_gas: 2, max_fee_per_blob_gas, - blob_versioned_hashes: vec![B256::from([1; 32])], + blob_versioned_hashes: vec![B256::repeat_byte(1)], to: Address::with_last_byte(0xbb), ..Default::default() } @@ -243,7 +237,7 @@ mod tests { let result = evm.transact(&tx).map(|executed| executed.commit()); assert_eq!(result.as_ref().map(|_| ()).map_err(Clone::clone), expected); if let Ok(result) = result { - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), 21_000); // Disabling validation does not disable charging the actual blob base fee. let account = evm.state_mut().account(&caller).unwrap(); @@ -255,7 +249,7 @@ mod tests { #[rstest::rstest] #[case(vec![], HandlerError::EmptyBlobs)] #[case(vec![B256::ZERO], HandlerError::BlobVersionNotSupported)] - #[case(vec![B256::from([1; 32]); 7], HandlerError::TooManyBlobs { have: 7, max: 6 })] + #[case(vec![B256::repeat_byte(1); 7], HandlerError::TooManyBlobs { have: 7, max: 6 })] fn blob_fee_simulation_still_validates_blobs( #[case] blobs: alloc::vec::Vec, #[case] expected: HandlerError, diff --git a/crates/evm2/src/ethereum/eip7702.rs b/crates/evm2/src/ethereum/eip7702.rs index c40ca1414..5a386b908 100644 --- a/crates/evm2/src/ethereum/eip7702.rs +++ b/crates/evm2/src/ethereum/eip7702.rs @@ -1,9 +1,9 @@ use super::{ - InitialFrame, LazyAuthorization, PreparedTx, access_list_counts, effective_gas_price, - initial_gas_and_reservoir, intrinsic_and_floor_gas, prepare_initial_frame, runtime_oog_result, - settle_initial_frame_gas, validate_block_gas_limit, validate_chain_id, - validate_create_initcode, validate_execution_gas_limit_cap, validate_floor_gas, - validate_gas_price, validate_intrinsic_gas, validate_nonce_not_overflow, validate_priority_fee, + LazyAuthorization, PreparedTx, access_list_counts, effective_gas_price, execute_initial_frame, + halted_initial_frame, initial_gas_and_reservoir, intrinsic_and_floor_gas, + prepare_initial_frame, validate_block_gas_limit, validate_chain_id, validate_create_initcode, + validate_execution_gas_limit_cap, validate_floor_gas, validate_gas_price, + validate_intrinsic_gas, validate_nonce_not_overflow, validate_priority_fee, validate_sender_and_bump_nonce, validate_tx_gas_limit_cap, warm_access_list, warm_base_accounts, }; @@ -11,7 +11,7 @@ use crate::{ Evm, EvmFeatures, EvmTypes, TxResult, Version, env::TxEnvExt, evm::handler::{DefaultTxHandlerHooks, GasSettlement, TxHandlerHooks}, - interpreter::{GasTracker, Host, InstrStop, MessageResult, gas::EIP8038_ACCOUNT_WRITE}, + interpreter::{GasTracker, InstrStop, MessageResult, gas::EIP8038_ACCOUNT_WRITE}, registry::{HandlerError, HandlerResult, TxRequest}, version::GasId, }; @@ -40,7 +40,8 @@ pub fn prepare_with_hooks>( let caller = req.tx.signer(); let tx = req.tx.inner(); let envelope = req.envelope; - if tx.authorization_list.is_empty() { + let auth_count = tx.authorizations().len(); + if auth_count == 0 { return Err(HandlerError::EmptyAuthorizationList); } let max_fee_per_gas = U256::from(tx.max_fee_per_gas); @@ -65,7 +66,7 @@ pub fn prepare_with_hooks>( access_list_storage_keys, tx.value, ); - intrinsic += eip7702_authorization_gas(req.host.version(), tx.authorization_list.len()); + intrinsic += eip7702_authorization_gas(req.host.version(), auth_count); // EIP-2780 (ethereum/EIPs#11844): the per-auth state-dependent charges are applied at the // runtime gas phase, so no state gas is charged at the intrinsic phase (pre-Amsterdam there is // none either). A transaction that passes the intrinsic check but cannot afford the runtime @@ -83,7 +84,7 @@ pub fn prepare_with_hooks>( validate_execution_gas_limit_cap(req.host.version(), tx.gas_limit, intrinsic, floor_gas)?; let max_gas_cost = U256::from(tx.gas_limit) * max_fee_per_gas; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -96,7 +97,7 @@ pub fn prepare_with_hooks>( let effective_gas_cost = U256::from(tx.gas_limit) * gas_price; H::before_execution(req.host, envelope, caller, effective_gas_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared EIP-7702 transaction. @@ -104,11 +105,12 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, super::LazyTxEip7702>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner(); let envelope = req.envelope; let chain_id = req.host.version().chain_id; - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(chain_id), @@ -136,11 +138,11 @@ pub fn execute_prepared>( let (auth_oog, state_refund, execution_refund) = if req.host.feature(EvmFeatures::EIP2780) { let mut auth_charges = RuntimeAuthCharges::new(req.host.version(), &mut tx_gas, caller, tx.to, tx.value); - let oog = apply_auth_list(req.host, chain_id, &tx.authorization_list, &mut auth_charges)?; + let oog = apply_auth_list(req.host, chain_id, tx.authorizations(), &mut auth_charges)?; (oog, 0, 0) } else { let mut auth_refunds = AuthRefunds::new(req.host.version()); - apply_auth_list(req.host, chain_id, &tx.authorization_list, &mut auth_refunds)?; + apply_auth_list(req.host, chain_id, tx.authorizations(), &mut auth_refunds)?; let AuthRefunds { state_refund, execution_refund, .. } = auth_refunds; tx_gas.set_reservoir(tx_gas.reservoir() + state_refund); (false, state_refund, execution_refund) @@ -170,40 +172,43 @@ pub fn execute_prepared>( }, ) }; - // Settles the transaction as an out-of-gas halt when the runtime gas phase (the authorization - // charges or the first-frame recipient charge) runs out of gas: reverts the authorization - // checkpoint to drop the applied delegations, then consumes all execution gas and returns the - // reservoir. Unreachable pre-Amsterdam, where no runtime charge is attempted. - let settle_oog = |host: &mut Evm<'_, T>| { - let features = host.version().features; - host.state.rollback(runtime_checkpoint, features); - settle(host, runtime_oog_result(execution_gas_limit, reservoir)) + let frame = if auth_oog { + halted_initial_frame( + req.host, + caller, + tx.nonce, + tx.to.into(), + &tx.input, + tx.value, + &GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir), + ) + } else { + prepare_initial_frame( + req.host, + caller, + sender_nonce, + tx.to.into(), + &tx.input, + tx.value, + &mut tx_gas, + )? }; - - if auth_oog { - return settle_oog(req.host); + // A runtime gas phase that ran out of gas (the authorization charges or the first-frame + // recipient charge) settles the transaction as an out-of-gas halt: the authorization + // checkpoint reverts to drop the applied delegations, then the frame halts without executing, + // consuming all execution gas and returning the reservoir. Unreachable pre-Amsterdam, where no + // runtime charge is attempted. + if frame.runtime_oog { + let features = req.host.version().features; + req.host.state.rollback(runtime_checkpoint, features); } - let Some(InitialFrame { mut message, charged_state_gas }) = prepare_initial_frame( - req.host, - caller, - tx.nonce, - tx.to.into(), - &tx.input, - tx.value, - &mut tx_gas, - )? - else { - // A depth-0 recipient charge that ran out of gas is part of the runtime gas phase, so it - // drops the delegations too. - return settle_oog(req.host); - }; // Failed execution has already been rolled back to the message's own checkpoint (past the // applied delegations, which stay) inside `execute_message`. The settle merges the frame gas // into `tx_gas`, which carries the authorization state gas into the block state-gas // accounting. - let mut result = req.host.execute_message(&tx_env, &mut message)?; - settle_initial_frame_gas(&mut tx_gas, &mut result, charged_state_gas); + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; settle(req.host, result) } @@ -459,14 +464,14 @@ impl AuthAccounting for AuthRefunds { /// their delegation is applied. An accounting out-of-gas aborts the list without loading the /// remaining authorities — keeping them out of the EIP-7928 block access list — and returns /// `true`; the caller is responsible for rolling back the partially applied delegations. -pub fn apply_auth_list<'a, 'b, T: EvmTypes, A: Eip7702Authorization + 'b>( +pub fn apply_auth_list<'a, T: EvmTypes, A: Eip7702Authorization>( host: &mut Evm<'a, T>, chain_id: u64, - authorizations: impl IntoIterator, + authorizations: impl IntoIterator, accounting: &mut impl AuthAccounting, ) -> HandlerResult { for authorization in authorizations { - let Some((authority, auth)) = validate_one_auth(host, chain_id, authorization)? else { + let Some((authority, auth)) = validate_one_auth(host, chain_id, &authorization)? else { accounting.rejected(); continue; }; @@ -496,7 +501,7 @@ pub trait Eip7702Authorization { fn authority(&self) -> Option
; } -impl Eip7702Authorization for LazyAuthorization { +impl Eip7702Authorization for LazyAuthorization<'_> { fn chain_id(&self) -> U256 { *Self::chain_id(self) } diff --git a/crates/evm2/src/ethereum/lazy_eip7702.rs b/crates/evm2/src/ethereum/lazy_eip7702.rs index aa366873f..7824fee82 100644 --- a/crates/evm2/src/ethereum/lazy_eip7702.rs +++ b/crates/evm2/src/ethereum/lazy_eip7702.rs @@ -1,46 +1,52 @@ -use crate::precompiles::secp256k1::ecrecover_bytes; +use crate::precompiles::crypto; use alloc::vec::Vec; use alloy_consensus::{Transaction, TxEip7702, TxType}; use alloy_eips::{ Typed2718, eip2930::AccessList, - eip7702::{ - Authorization, RecoveredAuthority, RecoveredAuthorization, SignedAuthorization, - constants::SECP256K1N_HALF, - }, + eip7702::{Authorization, SignedAuthorization, constants::SECP256K1N_HALF}, }; use alloy_primitives::{Address, B256, Bytes, ChainId, TxKind, U256}; -/// EIP-7702 authorization that may already have its authority recovered. -#[derive(Clone, Debug, Eq, PartialEq, Hash)] -pub enum LazyAuthorization { +/// Borrowed EIP-7702 authorization of a [`LazyTxEip7702`], with its authority possibly recovered. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +pub enum LazyAuthorization<'a> { /// Signed authorization whose authority is recovered on demand. - Signed(SignedAuthorization), - /// Authorization with a cached recovery result. - Recovered(RecoveredAuthorization), + Signed(&'a SignedAuthorization), + /// Signed authorization with a cached recovery result. + Recovered { + /// The signed authorization. + authorization: &'a SignedAuthorization, + /// The cached authority, or `None` if the signature is invalid. + authority: Option
, + }, } -impl LazyAuthorization { - /// Returns the inner unsigned authorization. - pub fn inner(&self) -> &Authorization { - match self { - Self::Signed(authorization) => authorization.inner(), - Self::Recovered(authorization) => authorization, +impl<'a> LazyAuthorization<'a> { + /// Returns the signed authorization. + const fn signed(&self) -> &'a SignedAuthorization { + match *self { + Self::Signed(authorization) | Self::Recovered { authorization, .. } => authorization, } } + /// Returns the inner unsigned authorization. + pub const fn inner(&self) -> &'a Authorization { + self.signed().inner() + } + /// Returns the authorization chain ID. - pub fn chain_id(&self) -> &U256 { + pub const fn chain_id(&self) -> &'a U256 { self.inner().chain_id() } /// Returns the delegated address. - pub fn address(&self) -> &Address { + pub const fn address(&self) -> &'a Address { self.inner().address() } /// Returns the authorization nonce. - pub fn nonce(&self) -> u64 { + pub const fn nonce(&self) -> u64 { self.inner().nonce() } @@ -49,45 +55,17 @@ impl LazyAuthorization { /// Signed authorizations perform recovery on demand. Recovered authorizations return their /// cached result without doing secp256k1 recovery. pub fn authority(&self) -> Option
{ - match self { + match *self { Self::Signed(authorization) => recover_authority(authorization), - Self::Recovered(authorization) => authorization.authority(), - } - } - - /// Returns the signed authorization if this authorization has not been recovered yet. - pub const fn as_signed(&self) -> Option<&SignedAuthorization> { - match self { - Self::Signed(authorization) => Some(authorization), - Self::Recovered(_) => None, + Self::Recovered { authority, .. } => authority, } } - - /// Returns the recovered authorization if this authorization has a cached recovery result. - pub const fn as_recovered(&self) -> Option<&RecoveredAuthorization> { - match self { - Self::Recovered(authorization) => Some(authorization), - Self::Signed(_) => None, - } - } -} - -impl From for LazyAuthorization { - fn from(value: SignedAuthorization) -> Self { - Self::Signed(value) - } -} - -impl From for LazyAuthorization { - fn from(value: RecoveredAuthorization) -> Self { - Self::Recovered(value) - } } /// EIP-7702 transaction used by the executable Ethereum transaction model. /// -/// This preserves the consensus [`TxEip7702`] fields but stores authorization list entries as -/// signed-or-recovered values so callers can supply cached EIP-7702 authority recovery results. +/// This preserves the consensus [`TxEip7702`] fields and optionally caches the recovered authority +/// of each authorization, so callers can supply EIP-7702 authority recovery results. #[derive(Clone, Debug, Default, Eq, PartialEq, Hash)] pub struct LazyTxEip7702 { /// EIP-155 replay protection chain ID. @@ -106,42 +84,51 @@ pub struct LazyTxEip7702 { pub value: U256, /// EIP-2930 access list. pub access_list: AccessList, - /// EIP-7702 authorizations, either signed or with cached recovery results. - pub authorization_list: Vec, - signed_authorization_list: Vec, /// Transaction input calldata. pub input: Bytes, + authorization_list: Vec, + /// Recovered authority of each entry of `authorization_list`, or `None` if not recovered yet. + /// An authority is `None` if its signature is invalid. + authorities: Option>>, } impl LazyTxEip7702 { /// Converts a consensus transaction while keeping signed authorizations unresolved. pub fn from_signed_authorizations(tx: TxEip7702) -> Self { - let authorization_list = tx.authorization_list.iter().cloned().map(Into::into).collect(); - Self::from_authorizations(tx, authorization_list) + Self::new(tx, None) } /// Converts a consensus transaction and eagerly recovers all authorization authorities. /// - /// Invalid authorization signatures are cached as invalid recovered authorizations so execution - /// skips them in the same way as a failed on-demand recovery. + /// Invalid authorization signatures are cached as invalid authorities so execution skips them + /// in the same way as a failed on-demand recovery. pub fn from_recovered_authorizations(tx: TxEip7702) -> Self { - let authorization_list = tx - .authorization_list - .iter() - .map(|authorization| { - let authority = recover_authority(authorization) - .map_or(RecoveredAuthority::Invalid, RecoveredAuthority::Valid); - RecoveredAuthorization::new_unchecked(authorization.inner().clone(), authority) - .into() - }) - .collect(); - Self::from_authorizations(tx, authorization_list) - } - - /// Converts a consensus transaction using an externally supplied authorization list. + let authorities = tx.authorization_list.iter().map(recover_authority).collect(); + Self::new(tx, Some(authorities)) + } + + /// Converts a consensus transaction using externally supplied recovered authorities, one per + /// authorization of `tx`; `None` marks an invalid signature. + /// + /// The caller is responsible for ensuring that each authority matches the signature of its + /// authorization. /// - /// The caller is responsible for ensuring that `authorization_list` corresponds to `tx`. - pub fn from_authorizations(tx: TxEip7702, authorization_list: Vec) -> Self { + /// # Panics + /// + /// Panics if `authorities` and the authorization list of `tx` differ in length. + pub fn from_cached_recovered_authorizations( + tx: TxEip7702, + authorities: Vec>, + ) -> Self { + assert_eq!( + tx.authorization_list.len(), + authorities.len(), + "one recovered authority per authorization" + ); + Self::new(tx, Some(authorities)) + } + + fn new(tx: TxEip7702, authorities: Option>>) -> Self { let TxEip7702 { chain_id, nonce, @@ -151,7 +138,7 @@ impl LazyTxEip7702 { to, value, access_list, - authorization_list: signed_authorization_list, + authorization_list, input, } = tx; Self { @@ -163,20 +150,23 @@ impl LazyTxEip7702 { to, value, access_list, - authorization_list, - signed_authorization_list, input, + authorization_list, + authorities, } } - /// Converts a consensus transaction using externally supplied recovered authorizations. - /// - /// The caller is responsible for ensuring that `authorization_list` corresponds to `tx`. - pub fn from_cached_recovered_authorizations( - tx: TxEip7702, - authorization_list: Vec, - ) -> Self { - Self::from_authorizations(tx, authorization_list.into_iter().map(Into::into).collect()) + /// Returns the authorizations, each with its cached authority if recovered. + pub fn authorizations(&self) -> impl ExactSizeIterator> { + let authorities = self.authorities.as_deref(); + self.authorization_list.iter().enumerate().map( + move |(i, authorization)| match authorities { + Some(authorities) => { + LazyAuthorization::Recovered { authorization, authority: authorities[i] } + } + None => LazyAuthorization::Signed(authorization), + }, + ) } /// Calculates a heuristic for the in-memory size of the transaction. @@ -185,8 +175,8 @@ impl LazyTxEip7702 { size_of::() + self.access_list.size() + self.input.len() - + self.authorization_list.capacity() * size_of::() - + self.signed_authorization_list.capacity() * size_of::() + + self.authorization_list.capacity() * size_of::() + + self.authorities.as_ref().map_or(0, |a| a.capacity() * size_of::>()) } } @@ -272,23 +262,25 @@ impl Transaction for LazyTxEip7702 { } fn authorization_list(&self) -> Option<&[SignedAuthorization]> { - Some(&self.signed_authorization_list) + Some(&self.authorization_list) } } -/// Recovers the authority of a signed authorization with the same secp256k1 backend as the -/// ecrecover precompile, instead of whichever backend alloy-primitives was built with. +/// Recovers the authority of a signed authorization with the installed +/// [`Crypto`](crate::precompiles::Crypto) provider, like the ecrecover precompile does, instead of +/// whichever backend alloy-primitives was built with. /// /// Keeps the checks of [`SignedAuthorization::recover_authority`]: the y-parity must be 0 or 1, -/// `s` must not exceed secp256k1n/2, and the backend rejects a zero or out-of-range `r` or `s`. +/// `s` must not exceed secp256k1n/2, and the provider rejects a zero or out-of-range `r` or `s`. fn recover_authority(authorization: &SignedAuthorization) -> Option
{ let signature = authorization.signature().ok()?; if signature.s() > SECP256K1N_HALF { return None; } let rsy = signature.as_rsy(); - let hash = ecrecover_bytes(rsy.first_chunk()?, rsy[64], &authorization.signature_hash())?; - Some(Address::from_word(hash.into())) + let hash = + crypto().secp256k1_ecrecover(rsy.first_chunk()?, rsy[64], &authorization.signature_hash()); + Some(Address::from_word(hash.ok()?.into())) } #[cfg(test)] @@ -302,20 +294,17 @@ mod tests { let key = SigningKey::from_slice(&[0x11; 32]).unwrap(); let expected = Address::from_raw_public_key(&key.verifying_key().to_sec1_point(false).as_bytes()[1..]); - let inner = Authorization { - chain_id: U256::from(1), - address: Address::with_last_byte(0xbb), - nonce: 7, - }; + let inner = + Authorization { chain_id: U256::ONE, address: Address::with_last_byte(0xbb), nonce: 7 }; let (sig, recid) = key.sign_prehash_recoverable(inner.signature_hash().as_slice()); let r = U256::from_be_slice(&sig.r().to_bytes()); let s = U256::from_be_slice(&sig.s().to_bytes()); let y = recid.to_byte(); - let n = SECP256K1N_HALF * U256::from(2) + U256::from(1); + let n = SECP256K1N_HALF * U256::from(2) + U256::ONE; let signed = |y, r, s| SignedAuthorization::new_unchecked(inner.clone(), y, r, s); let valid = signed(y, r, s); - assert_eq!(LazyAuthorization::from(valid.clone()).authority(), Some(expected)); + assert_eq!(LazyAuthorization::Signed(&valid).authority(), Some(expected)); // Without the low-s check, the high-s signature would recover `expected` too. let authorization_list = vec![ valid, @@ -328,14 +317,17 @@ mod tests { let tx = TxEip7702 { authorization_list, ..Default::default() }; let signed = LazyTxEip7702::from_signed_authorizations(tx.clone()); let recovered = LazyTxEip7702::from_recovered_authorizations(tx.clone()); - for (i, authorization) in tx.authorization_list.iter().enumerate() { + let entries = signed.authorizations().zip(recovered.authorizations()); + for (i, (authorization, (signed, recovered))) in + tx.authorization_list.iter().zip(entries).enumerate() + { let want = authorization.recover_authority().ok(); assert_eq!(want.is_some(), i == 0, "case {i}"); - assert_eq!(signed.authorization_list[i].authority(), want, "signed case {i}"); - assert_eq!(recovered.authorization_list[i].authority(), want, "recovered case {i}"); + assert_eq!(signed, LazyAuthorization::Signed(authorization), "signed entry {i}"); + assert_eq!(signed.authority(), want, "signed case {i}"); assert_eq!( - recovered.authorization_list[i], - LazyAuthorization::from(authorization.clone().into_recovered()), + recovered, + LazyAuthorization::Recovered { authorization, authority: want }, "recovered entry {i}" ); } diff --git a/crates/evm2/src/ethereum/legacy.rs b/crates/evm2/src/ethereum/legacy.rs index 13f0f667b..2ed1c126b 100644 --- a/crates/evm2/src/ethereum/legacy.rs +++ b/crates/evm2/src/ethereum/legacy.rs @@ -59,7 +59,7 @@ pub fn prepare_with_hooks>( validate_execution_gas_limit_cap(req.host.version(), tx.gas_limit, intrinsic, floor_gas)?; let max_gas_cost = U256::from(tx.gas_limit) * gas_price; - validate_sender_and_bump_nonce( + let sender_nonce = validate_sender_and_bump_nonce( req.host, caller, tx.nonce, @@ -70,7 +70,7 @@ pub fn prepare_with_hooks>( H::before_execution(req.host, req.envelope, caller, max_gas_cost)?; - Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas }) + Ok(PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce }) } /// Executes and settles a prepared legacy transaction. @@ -78,12 +78,13 @@ pub fn execute_prepared>( req: TxRequest<'_, '_, T, TxLegacy>, prepared: PreparedTx, ) -> HandlerResult> { - let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas } = prepared; + let PreparedTx { caller, gas_price, intrinsic, initial_state_gas, floor_gas, sender_nonce } = + prepared; let tx = req.tx.inner(); let (execution_gas_limit, reservoir) = initial_gas_and_reservoir(req.host.version(), tx.gas_limit, intrinsic, initial_state_gas); - let tx_env = TxEnvExt { + *req.host.tx_env_mut() = TxEnvExt { origin: caller, gas_price, chain_id: U256::from(req.host.version().chain_id), @@ -91,16 +92,17 @@ pub fn execute_prepared>( }; let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(execution_gas_limit, reservoir); - let frame = - prepare_initial_frame(req.host, caller, tx.nonce, tx.to, &tx.input, tx.value, &mut tx_gas)?; - let result = execute_initial_frame( + let frame = prepare_initial_frame( req.host, - &tx_env, - frame, + caller, + sender_nonce, + tx.to, + &tx.input, + tx.value, &mut tx_gas, - execution_gas_limit, - reservoir, )?; + let result = + execute_initial_frame(req.host, frame, &mut tx_gas, execution_gas_limit, reservoir)?; H::settle_transaction( req.host, req.envelope, diff --git a/crates/evm2/src/ethereum/mod.rs b/crates/evm2/src/ethereum/mod.rs index 758075801..052876d53 100644 --- a/crates/evm2/src/ethereum/mod.rs +++ b/crates/evm2/src/ethereum/mod.rs @@ -17,7 +17,6 @@ pub use lazy_eip7702::{LazyAuthorization, LazyTxEip7702}; use crate::{ Evm, EvmFeatures, EvmTypes, HostError, SpecId, TxResult, TxResultExt, Version, bytecode::Bytecode, - env::TxEnv, evm::{ AccountInfo, handler::{DefaultTxHandlerHooks, GasSettlement}, @@ -27,7 +26,7 @@ use crate::{ MessageResultExt, gas::WARM_STORAGE_READ_COST, }, registry::{HandlerError, HandlerResult, TxRegistry, handler}, - utils::num_words, + utils::{b256_to_word, num_words}, version::GasId, }; use alloy_consensus::{ @@ -64,6 +63,7 @@ pub struct PreparedTx { pub(super) intrinsic: u64, pub(super) initial_state_gas: u64, pub(super) floor_gas: u64, + pub(super) sender_nonce: u64, } impl From> for TxEnvelope { @@ -354,8 +354,8 @@ pub const fn validate_execution_gas_limit_cap( if tx_gas_limit > cap { let required_execution_gas = if intrinsic > floor_gas { intrinsic } else { floor_gas }; if required_execution_gas > cap { - return Err(HandlerError::TxGasLimitGreaterThanCap { - gas_limit: required_execution_gas, + return Err(HandlerError::IntrinsicGasGreaterThanCap { + required: required_execution_gas, cap, }); } @@ -420,12 +420,13 @@ pub const fn validate_intrinsic_gas( /// Validates that the gas limit covers the calldata floor gas. pub const fn validate_floor_gas(gas_limit: u64, floor_gas: u64) -> HandlerResult<()> { if gas_limit < floor_gas { - return Err(HandlerError::IntrinsicGasTooLow { required: floor_gas, got: gas_limit }); + return Err(HandlerError::GasLimitBelowFloor { required: floor_gas, got: gas_limit }); } Ok(()) } /// Loads and validates the sender account, then marks it warm and bumps its nonce. +/// Returns the sender's pre-bump state nonce. /// /// One account handle does all three, so the sender is looked up and journaled once. pub fn validate_sender_and_bump_nonce<'a, T: EvmTypes>( @@ -433,7 +434,7 @@ pub fn validate_sender_and_bump_nonce<'a, T: EvmTypes>( caller: Address, nonce: u64, max_upfront: U256, -) -> HandlerResult<()> { +) -> HandlerResult { let has_nonce_check = host.feature(EvmFeatures::NONCE_CHECK); let has_balance_check = host.feature(EvmFeatures::BALANCE_CHECK); let has_balance_top_up = host.feature(EvmFeatures::BALANCE_TOP_UP); @@ -460,8 +461,9 @@ pub fn validate_sender_and_bump_nonce<'a, T: EvmTypes>( } // The sender is warm from the start of the transaction (EIP-2929). sender.warm(); + let sender_nonce = sender.nonce(); sender.bump_nonce(); - Ok(()) + Ok(sender_nonce) } /// Warms the accounts required by every transaction other than the sender, which @@ -479,10 +481,8 @@ pub fn warm_base_accounts<'a, T: EvmTypes>(host: &mut Evm<'a, T>, to: TxKind) { /// Warms every account and storage key in an access list. pub fn warm_access_list<'a, T: EvmTypes>(host: &mut Evm<'a, T>, access_list: &AccessList) { for item in access_list.iter() { - host.state.prewarm_storage( - &item.address, - item.storage_keys.iter().map(|key| U256::from_be_bytes(key.0)), - ); + host.state + .prewarm_storage(&item.address, item.storage_keys.iter().map(|key| b256_to_word(*key))); } } @@ -538,13 +538,17 @@ pub fn initial_gas_and_reservoir( /// The first frame of a transaction, prepared by [`prepare_initial_frame`]. #[derive(Clone, Debug)] pub struct InitialFrame { - /// Top-level call or create message, carrying the resolved bytecode to run. + /// Top-level call or create message, carrying the resolved bytecode to run unless + /// [`runtime_oog`](Self::runtime_oog). pub message: Message, /// EIP-8037 state gas charged on the transaction-level tracker for the account leaf this /// frame creates (the call recipient's new-account charge or the create target's /// account-creation charge), zero when nothing was charged. Refunded by /// [`settle_initial_frame_gas`] when the frame fails and the leaf is not created. pub charged_state_gas: u64, + /// Whether the runtime gas phase ran out of gas, so the frame halts without executing + /// ([`execute_initial_frame`]). Its message then carries no resolved bytecode. + pub runtime_oog: bool, } /// Completes the EIP-2780 runtime gas phase on the transaction-level `tx_gas` and builds the @@ -562,9 +566,9 @@ pub struct InitialFrame { /// the CREATE opcode instead. A delegated recipient is still resolved (for free) so the frame /// runs the delegate's code. /// -/// The frame's `gas_limit`/`reservoir` snapshot `tx_gas` after the charges. Returns `None` when -/// a charge runs out of gas: the transaction stays valid but is included as an out-of-gas halt -/// without entering execution ([`runtime_oog_result`]). +/// The frame's `gas_limit`/`reservoir` snapshot `tx_gas` after the charges. When a charge runs out +/// of gas the transaction stays valid but is included as an out-of-gas halt: the returned frame +/// is [`halted_initial_frame`], which [`execute_initial_frame`] halts without entering execution. pub fn prepare_initial_frame<'a, T: EvmTypes>( host: &mut Evm<'a, T>, caller: Address, @@ -573,7 +577,12 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( input: &Bytes, value: U256, tx_gas: &mut GasTracker, -) -> HandlerResult>> { +) -> HandlerResult> { + // A failed charge leaves `tx_gas` out of range, so the halted frame reports the budget the + // phase started with. + let budget = *tx_gas; + let halted = + |host: &Evm<'a, T>| halted_initial_frame(host, caller, nonce, to, input, value, &budget); let mut charged_state_gas = 0; let message = match to { TxKind::Call(to) => { @@ -587,7 +596,7 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( if host.feature(EvmFeatures::EIP2780) && !value.is_zero() && recipient_is_empty { let new_account_state_gas = host.version().gas_params.new_account_state_gas(); if tx_gas.spend_state(new_account_state_gas).is_err() { - return Ok(None); + return Ok(halted(host)); } charged_state_gas = new_account_state_gas; } @@ -604,17 +613,17 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( // list. let cold_additional = host.version().gas_params.cold_account_additional_cost(); if tx_gas.spend(u64::from(WARM_STORAGE_READ_COST)).is_err() { - return Ok(None); + return Ok(halted(host)); } let skip_cold_load = tx_gas.remaining() < cold_additional; let load = match Host::load_account(host, &delegated_address, true, skip_cold_load) { Ok(load) => load, - Err(HostError::Halt(_)) => return Ok(None), + Err(HostError::Halt(_)) => return Ok(halted(host)), Err(HostError::Execution(error)) => return Err(error.into()), }; if load.is_cold && tx_gas.spend(cold_additional).is_err() { - return Ok(None); + return Ok(halted(host)); } code = load.code; } else { @@ -626,13 +635,13 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( } MessageExt { kind: MessageKind::Call, - depth: host.version().initial_call_depth, + depth: host.initial_call_depth(), gas_limit: tx_gas.remaining(), reservoir: tx_gas.reservoir(), destination: to, call_target: to, caller, - input: input.clone(), + input: input.clone().into(), value, code, code_address, @@ -650,20 +659,20 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( if !target_alive { let create_state_gas = host.version().gas_params.create_state_gas(); if tx_gas.spend_state(create_state_gas).is_err() { - return Ok(None); + return Ok(halted(host)); } charged_state_gas = create_state_gas; } } MessageExt { kind: MessageKind::Create, - depth: host.version().initial_call_depth, + depth: host.initial_call_depth(), gas_limit: tx_gas.remaining(), reservoir: tx_gas.reservoir(), destination, call_target: destination, caller, - input: input.clone(), + input: input.clone().into(), value, code: Bytecode::new_legacy(input.clone()), code_address: destination, @@ -674,8 +683,45 @@ pub fn prepare_initial_frame<'a, T: EvmTypes>( } } }; - debug_assert_eq!(message.depth, host.version().initial_call_depth); - Ok(Some(InitialFrame { message, charged_state_gas })) + debug_assert_eq!(message.depth, host.initial_call_depth()); + Ok(InitialFrame { message, charged_state_gas, runtime_oog: false }) +} + +/// Builds the first frame of a transaction whose runtime gas phase ran out of gas. +/// +/// The message names the call or create the transaction would have run, with the `budget` the +/// runtime gas phase started with and no resolved bytecode, and it reads no account state: the +/// recipient of an unaffordable authorization charge stays out of the EIP-7928 block access list. +/// [`execute_initial_frame`] halts it with out-of-gas, reporting the frame to the inspector. +pub fn halted_initial_frame( + host: &Evm<'_, T>, + caller: Address, + nonce: u64, + to: TxKind, + input: &Bytes, + value: U256, + budget: &GasTracker, +) -> InitialFrame { + let (kind, destination) = match to { + TxKind::Call(to) => (MessageKind::Call, to), + TxKind::Create => (MessageKind::Create, caller.create(nonce)), + }; + let message = MessageExt { + kind, + depth: host.initial_call_depth(), + gas_limit: budget.remaining(), + reservoir: budget.reservoir(), + destination, + call_target: destination, + caller, + input: input.clone().into(), + value, + code_address: destination, + caller_is_static: false, + salt: B256::ZERO, + ..MessageExt::default() + }; + InitialFrame { message, charged_state_gas: 0, runtime_oog: true } } /// Settles the first frame's result into the transaction-level `tx_gas` and writes the settled @@ -696,7 +742,7 @@ pub const fn settle_initial_frame_gas( ) { tx_gas.spend_all(); tx_gas.merge_child_gas(result.gas, result.stop); - if charged_state_gas != 0 && !result.stop.is_success() { + if charged_state_gas != 0 && !result.is_success() { tx_gas.refill_reservoir(charged_state_gas); if result.stop.is_halt() { tx_gas.spend_all(); @@ -706,27 +752,36 @@ pub const fn settle_initial_frame_gas( } /// Executes the prepared first frame and settles its gas into the transaction-level tracker. +/// +/// A frame whose runtime gas phase ran out of gas ([`InitialFrame::runtime_oog`]) is halted +/// without executing ([`Evm::execute_halted_message`]) and settled as [`runtime_oog_result`]. pub fn execute_initial_frame( host: &mut Evm<'_, T>, - tx_env: &TxEnv, - frame: Option>, + frame: InitialFrame, tx_gas: &mut GasTracker, execution_gas_limit: u64, reservoir: u64, ) -> Result, crate::ExecutionError> { - let Some(InitialFrame { mut message, charged_state_gas }) = frame else { - return Ok(runtime_oog_result(execution_gas_limit, reservoir)); - }; + let InitialFrame { mut message, charged_state_gas, runtime_oog } = frame; + if runtime_oog { + // The hooks see the frame burn the budget it was given; the transaction itself burns all + // its execution gas and returns the reservoir, dropping the phase's partial charges. + let halted = runtime_oog_result(message.gas_limit, message.reservoir); + let mut result = host.execute_halted_message(&mut message, halted)?; + result.gas = runtime_oog_result::<()>(execution_gas_limit, reservoir).gas; + return Ok(result); + } // Failed execution has already been rolled back to the message's own checkpoint inside // `execute_message`; the settle merges the frame gas into the transaction-level gas. - let mut result = host.execute_message(tx_env, &mut message)?; + let mut result = host.execute_message(&mut message)?; settle_initial_frame_gas(tx_gas, &mut result, charged_state_gas); Ok(result) } /// Builds the result for a transaction whose EIP-2780 runtime gas phase ran out of gas -/// ([`prepare_initial_frame`] returned `None`, or the EIP-7702 authorization charges bailed). +/// ([`prepare_initial_frame`] returned a [halted frame](halted_initial_frame), or the EIP-7702 +/// authorization charges bailed). /// /// The transaction is valid but cannot afford the state-dependent runtime charges: it is /// included as an out-of-gas halt that consumes all execution gas and returns the reservoir, @@ -786,26 +841,19 @@ pub fn finalize_gas<'a, T: EvmTypes>( } = settlement; let max_refund_quotient = host.version().gas_params.get(GasId::MaxRefundQuotient); - // Self-contained gas breakdown for the result. `total_gas_spent` is defined so that - // `TxResult::tx_gas_used` reproduces the finalized gas used. State gas is execution state gas - // plus the upfront `initial_state_gas`, less the EIP-7702 per-authorization `state_refund`. - let total_gas_spent = - tx_gas_limit.saturating_sub(result.gas.remaining()).saturating_sub(result.gas.reservoir()); let refunded = result.final_refund(tx_gas_limit, max_refund_quotient); // EIP-7623: when the calldata floor exceeds spent-minus-refund, `TxResult::tx_gas_used` // resolves to the floor. `total_gas_spent` stays pre-refund and pre-floor: block-level // execution gas (EIP-7778/EIP-8037) accumulates `tx_gas_used_before_refund` per // execution-specs, without the floor clamp. // - // The settled tracker's state gas is self-consistent: a failed frame's state charges were - // rolled back before it was merged, while unconditional pre-execution charges (the EIP-7702 - // authorizations, whose delegations survive an execution failure) remain. The upfront - // `initial_state_gas` is likewise added unconditionally. - let state_gas_spent = - (result.gas.state_gas_spent().saturating_add_unsigned(initial_state_gas).max(0) as u64) - .saturating_sub(state_refund); + // The per-authorization `state_refund` is EIP-7702 state gas returned on top of the settled + // tracker's. + let (total_gas_spent, state_gas_spent) = + gas_spent_breakdown(tx_gas_limit, &result.gas, initial_state_gas); + let state_gas_spent = state_gas_spent.saturating_sub(state_refund); Ok(TxResultExt { - status: result.stop.is_success(), + status: result.is_success(), total_gas_spent, state_gas_spent, refunded, @@ -818,6 +866,27 @@ pub fn finalize_gas<'a, T: EvmTypes>( }) } +/// Returns `(total_gas_spent, state_gas_spent)` for a settled transaction-level gas tracker. +/// +/// `tx_gas_limit` is the execution gas limit plus the state-gas reservoir the transaction started +/// with. `total_gas_spent` is defined so that `TxResult::tx_gas_used` reproduces the finalized gas +/// used. `state_gas_spent` is the tracker's state gas plus the upfront `initial_state_gas`. +/// +/// The settled tracker's state gas is self-consistent: a failed frame's state charges were rolled +/// back before it was merged, while unconditional pre-execution charges (the EIP-7702 +/// authorizations, whose delegations survive an execution failure) remain. The upfront +/// `initial_state_gas` is likewise added unconditionally. +pub(crate) fn gas_spent_breakdown( + tx_gas_limit: u64, + gas: &GasTracker, + initial_state_gas: u64, +) -> (u64, u64) { + let total_gas_spent = + tx_gas_limit.saturating_sub(gas.remaining()).saturating_sub(gas.reservoir()); + let state_gas_spent = gas.state_gas_spent().saturating_add_unsigned(initial_state_gas).max(0); + (total_gas_spent, state_gas_spent as u64) +} + /// Returns the account and storage-key counts in an access list. pub fn access_list_counts(access_list: &AccessList) -> (u64, u64) { (access_list.len() as u64, access_list.storage_keys_count() as u64) @@ -914,12 +983,13 @@ mod tests { use super::*; use crate::{ BaseEvmTypes, ExecutionConfig, Precompiles, - env::{BlockEnvExt, TxEnvExt}, - evm::InMemoryDB, + constants::MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, + env::BlockEnvExt, + evm::{InMemoryDB, handler::TxHandlerHooks}, interpreter::{Host, InstrStop, op}, registry::TxRegistry, }; - use alloc::{string::ToString, vec}; + use alloc::{format, string::ToString, vec}; use alloy_consensus::{TxEip2930, transaction::Recovered}; use alloy_eips::eip2930::AccessList; @@ -927,8 +997,6 @@ mod tests { fn adjusted_floor_gas_controls_validation_and_settlement( #[values(31_049, 31_050)] gas_limit: u64, ) { - use crate::evm::handler::TxHandlerHooks; - struct CalldataSurcharge; impl TxHandlerHooks for CalldataSurcharge { @@ -975,16 +1043,16 @@ mod tests { ); let validation = evm.validate_tx(&tx); - let result = evm.transact(&tx).map(crate::evm::ExecutedTx::discard); + let result = evm.call_tx(&tx); assert_eq!(validation, result.as_ref().map(|_| ()).map_err(Clone::clone)); if gas_limit < 31_050 { assert_eq!( result, - Err(HandlerError::IntrinsicGasTooLow { required: 31_050, got: gas_limit }) + Err(HandlerError::GasLimitBelowFloor { required: 31_050, got: gas_limit }) ); } else { let result = result.unwrap(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.total_gas_spent, 31_020); assert_eq!(result.floor_gas, 31_050); assert_eq!(result.tx_gas_used(), 31_050); @@ -1079,6 +1147,19 @@ mod tests { ); } + #[test] + fn gas_limit_below_floor_is_not_an_intrinsic_gas_error() { + assert_eq!( + validate_intrinsic_gas(20_999, 21_000, 0), + Err(HandlerError::IntrinsicGasTooLow { required: 21_000, got: 20_999 }) + ); + assert_eq!( + validate_floor_gas(21_009, 21_010), + Err(HandlerError::GasLimitBelowFloor { required: 21_010, got: 21_009 }) + ); + assert_eq!(validate_floor_gas(21_010, 21_010), Ok(())); + } + #[test] fn floor_gas_charges_prague_calldata_tokens() { let input = Bytes::from_static(&[0, 1, 2]); @@ -1211,8 +1292,8 @@ mod tests { caller, ); - assert!(evm.transact(&tx).unwrap().commit().status); - let reward = evm.state.account_info_untracked(&beneficiary).unwrap().unwrap().balance; + assert!(evm.transact(&tx).unwrap().commit().is_success()); + let reward = evm.read_account_info(&beneficiary).unwrap().unwrap().balance; assert_eq!(reward, U256::from(21_000 * beneficiary_gas_price)); } @@ -1253,7 +1334,7 @@ mod tests { ); assert!(validate_sender_and_bump_nonce(&mut evm, caller, 0, U256::from(100)).is_ok()); - let sender = evm.state.account_info_untracked(&caller).unwrap().unwrap(); + let sender = evm.read_account_info(&caller).unwrap().unwrap(); assert_eq!((sender.balance, sender.nonce), (U256::ZERO, 1)); } @@ -1282,10 +1363,7 @@ mod tests { ); assert!(validate_sender_and_bump_nonce(&mut evm, caller, 0, U256::from(100)).is_ok()); - assert_eq!( - evm.state.account_info_untracked(&caller).unwrap().unwrap().balance, - U256::from(100) - ); + assert_eq!(evm.read_account_info(&caller).unwrap().unwrap().balance, U256::from(100)); } #[test] @@ -1308,7 +1386,7 @@ mod tests { assert!(validate_sender_and_bump_nonce(&mut evm, caller, 0, U256::from(100)).is_ok()); charge_upfront(&mut evm, caller, U256::from(100)).unwrap(); - assert_eq!(evm.state.account_info_untracked(&caller).unwrap().unwrap().balance, U256::ZERO); + assert_eq!(evm.read_account_info(&caller).unwrap().unwrap().balance, U256::ZERO); } #[test] @@ -1341,7 +1419,7 @@ mod tests { ); let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(100_000, 0); - let InitialFrame { mut message, charged_state_gas } = prepare_initial_frame( + let InitialFrame { mut message, charged_state_gas, .. } = prepare_initial_frame( &mut evm, caller, 0, @@ -1350,13 +1428,12 @@ mod tests { U256::ZERO, &mut tx_gas, ) - .unwrap() .unwrap(); assert_eq!(message.destination, target); assert_eq!(message.code_address, delegated); assert_eq!(charged_state_gas, 0); - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Return); assert_eq!(result.output.len(), 32); @@ -1379,6 +1456,14 @@ mod tests { }) ); assert_eq!(validate_tx_gas_limit_cap(amsterdam, tx_gas_limit), Ok(())); + assert_eq!(validate_tx_gas_limit_cap(amsterdam, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM), Ok(())); + assert_eq!( + validate_tx_gas_limit_cap(amsterdam, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + 1), + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + 1, + cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + }) + ); assert_eq!( validate_execution_gas_limit_cap(amsterdam, tx_gas_limit, intrinsic, floor_gas), Ok(()) @@ -1390,8 +1475,8 @@ mod tests { amsterdam.tx_gas_limit_cap + 1, floor_gas, ), - Err(HandlerError::TxGasLimitGreaterThanCap { - gas_limit: amsterdam.tx_gas_limit_cap + 1, + Err(HandlerError::IntrinsicGasGreaterThanCap { + required: amsterdam.tx_gas_limit_cap + 1, cap: amsterdam.tx_gas_limit_cap }) ); @@ -1407,9 +1492,87 @@ mod tests { ); } + #[test] + fn amsterdam_total_gas_cap_is_configurable() { + let gas_limit = 1u64 << 32; + + // The default keeps the EIP-8037 cap from #862. + let amsterdam = Version::base(SpecId::AMSTERDAM); + assert_eq!( + validate_tx_gas_limit_cap(amsterdam, gas_limit), + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit, + cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + }) + ); + + // A chain with larger blocks can raise the cap above the execution cap. + let mut raised = Version::new(SpecId::AMSTERDAM); + raised.tx_total_gas_cap = 10_000_000_000; + assert_eq!(validate_tx_gas_limit_cap(&raised, gas_limit), Ok(())); + assert_eq!( + validate_tx_gas_limit_cap(&raised, raised.tx_total_gas_cap + 1), + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit: raised.tx_total_gas_cap + 1, + cap: raised.tx_total_gas_cap + }) + ); + + // `u64::MAX` disables the total cap. + let mut disabled = Version::new(SpecId::AMSTERDAM); + disabled.tx_total_gas_cap = u64::MAX; + assert_eq!(validate_tx_gas_limit_cap(&disabled, gas_limit), Ok(())); + assert_eq!(validate_tx_gas_limit_cap(&disabled, u64::MAX), Ok(())); + + // Without EIP-8037 the field is unused and the execution cap bounds `tx.gas`. + let mut without_eip8037 = Version::new(SpecId::AMSTERDAM); + without_eip8037.features.remove(EvmFeatures::EIP8037); + without_eip8037.tx_total_gas_cap = u64::MAX; + assert_eq!( + validate_tx_gas_limit_cap(&without_eip8037, gas_limit), + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit, + cap: without_eip8037.tx_gas_limit_cap + }) + ); + } + + #[rstest::rstest] + fn amsterdam_handler_rejects_gas_limit_above_total_cap( + #[values(MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM + 1)] + gas_limit: u64, + ) { + let mut evm = Evm::::new( + SpecId::AMSTERDAM, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::AMSTERDAM), + InMemoryDB::default(), + Precompiles::base(SpecId::AMSTERDAM), + ); + let tx = Recovered::new_unchecked( + TxEnvelope::Eip2930(TxEip2930 { + chain_id: 1, + gas_limit, + to: TxKind::Call(Address::with_last_byte(0xbb)), + ..Default::default() + }), + Address::with_last_byte(0xaa), + ); + + let expected = if gas_limit > MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM { + Err(HandlerError::TxGasLimitGreaterThanCap { + gas_limit, + cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, + }) + } else { + Ok(()) + }; + assert_eq!(evm.validate_tx(&tx), expected); + } + #[rstest::rstest] #[case::call_zero_value(TxKind::Call(Address::ZERO), U256::ZERO, 15_000, 12_700)] - #[case::call_with_value(TxKind::Call(Address::ZERO), U256::from(1), 21_000, 18_700)] + #[case::call_with_value(TxKind::Call(Address::ZERO), U256::ONE, 21_000, 18_700)] #[case::self_transfer_zero_value( TxKind::Call(Address::with_last_byte(0xaa)), U256::ZERO, @@ -1418,12 +1581,12 @@ mod tests { )] #[case::self_transfer_with_value( TxKind::Call(Address::with_last_byte(0xaa)), - U256::from(1), + U256::ONE, 12_000, 10_000 )] #[case::create_zero_value(TxKind::Create, U256::ZERO, 24_000, 22_000)] - #[case::create_with_value(TxKind::Create, U256::from(1), 24_000, 22_000)] + #[case::create_with_value(TxKind::Create, U256::ONE, 24_000, 22_000)] fn eip2780_intrinsic_and_floor_gas_read_base_costs_from_gas_params( #[case] to: TxKind, #[case] value: U256, @@ -1450,4 +1613,112 @@ mod tests { assert_eq!(floor, base + floor_calldata_gas); } } + + #[rstest::rstest] + fn underfunded_top_level_value_depends_on_balance_check( + #[values(false, true)] balance_check: bool, + #[values(false, true)] fee_charge: bool, + ) { + let caller = Address::with_last_byte(0xaa); + let recipient = Address::with_last_byte(0xbb); + for spec in [SpecId::PRAGUE, SpecId::AMSTERDAM] { + for to in [TxKind::Create, TxKind::Call(recipient)] { + for value in [0, 1, 2] { + for input in [ + Bytes::from_static(&[op::STOP]), + Bytes::from_static(&[op::PUSH0, op::PUSH0, op::REVERT]), + ] { + for tx_type in 0..=2 { + let mut version = Version::new(spec); + version.features.remove(EvmFeatures::BALANCE_TOP_UP); + version.features.set(EvmFeatures::BALANCE_CHECK, balance_check); + version.features.set(EvmFeatures::FEE_CHARGE, fee_charge); + let mut database = InMemoryDB::default(); + database.insert_account_info( + &caller, + AccountInfo::default().with_nonce(10).with_balance(U256::ONE), + ); + database.insert_account_info( + &recipient, + AccountInfo::default() + .with_code(Bytecode::new_legacy(input.clone())), + ); + let tx = match tx_type { + 0 => TxEnvelope::Legacy(TxLegacy { + nonce: 10, + gas_limit: 500_000, + to, + value: U256::from(value), + input: input.clone(), + ..Default::default() + }), + 1 => TxEnvelope::Eip2930(TxEip2930 { + chain_id: version.chain_id, + nonce: 10, + gas_limit: 500_000, + to, + value: U256::from(value), + input: input.clone(), + ..Default::default() + }), + _ => TxEnvelope::Eip1559(TxEip1559 { + chain_id: version.chain_id, + nonce: 10, + gas_limit: 500_000, + to, + value: U256::from(value), + input: input.clone(), + ..Default::default() + }), + }; + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(spec, version), + spec, + BlockEnvExt { + gas_limit: U256::from(10_000_000), + ..Default::default() + }, + ethereum_tx_registry(spec), + database, + Precompiles::base(spec), + ); + let tx = Recovered::new_unchecked(tx, caller); + let case = format!("{spec:?} {to:?} value={value} type={tx_type}"); + let caller_before = evm.read_account_info(&caller).unwrap(); + let recipient_before = evm.read_account_info(&recipient).unwrap(); + if balance_check && value == 2 { + assert_eq!( + evm.call_tx(&tx), + Err(HandlerError::InsufficientFunds { + fee: U256::from(2), + balance: U256::ONE + }), + "{case}" + ); + assert_eq!( + evm.read_account_info(&caller).unwrap(), + caller_before, + "{case}" + ); + assert_eq!( + evm.read_account_info(&recipient).unwrap(), + recipient_before, + "{case}" + ); + continue; + } + let result = evm.transact(&tx).unwrap().commit(); + if value == 2 { + assert_eq!(result.stop, InstrStop::OutOfFunds, "{case}"); + } else { + assert_eq!(result.is_success(), input[0] == op::STOP, "{case}"); + } + let nonce = evm.read_account_info(&caller).unwrap().unwrap().nonce; + assert_eq!(nonce, 11, "{case}"); + } + } + } + } + } + } } diff --git a/crates/evm2/src/evm/async.rs b/crates/evm2/src/evm/async.rs index 079b3f54e..2b84b34f2 100644 --- a/crates/evm2/src/evm/async.rs +++ b/crates/evm2/src/evm/async.rs @@ -535,8 +535,7 @@ impl fmt::Debug for AsyncDb { mod tests { use super::*; use crate::{ - BaseEvmTypes, Evm, ExecutionConfig, Inspector, PrecompileError, Precompiles, SpecId, - TxResult, TxResultExt, Version, + BaseEvmTypes, Evm, Inspector, PrecompileError, Precompiles, SpecId, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, env::BlockEnvExt, @@ -544,10 +543,11 @@ mod tests { interpreter::{GasTracker, InstrStop, Interpreter, Message, MessageResult, Word, op}, precompile::PrecompileOutput, precompiles::PrecompileId, - registry::{HandlerError, HandlerResult, TxRegistry, TxRequest, handler}, + registry::{HandlerError, TxRegistry, handler}, + test_utils::{handle_test_tx, test_tx}, }; use alloy_consensus::{TxLegacy, transaction::Recovered}; - use alloy_primitives::{Address, B256, Bytes, TxKind, hex}; + use alloy_primitives::{Address, B256, Bytes, TxKind, bytes}; use core::{ assert_matches, convert::Infallible, fmt, future::Future, pin::Pin, ptr::NonNull, task::Poll, @@ -791,6 +791,30 @@ mod tests { drop(evm.transact_async_send(&tx)); } + #[test] + fn transaction_async_send_survives_state_mut_access() { + let registry = TxRegistry::new().with_handler( + TEST_TX_TYPE, + crate::ethereum::TxEnvelope::as_legacy, + handler(|_| Ok(()), |req, ()| handle_test_tx(req)), + ); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + registry, + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + evm.evm_is_send::>(); + evm.state_mut(); + evm.overlay_db_mut(); + let tx = test_tx(41); + + let result = poll_ready(assert_send(evm.transact_async_send(&tx))).unwrap(); + + assert_eq!(result.result().tx_gas_used(), 42); + } + #[test] fn transaction_async_accepts_non_send_erased_fields() { let marker = Rc::new(()); @@ -870,7 +894,7 @@ mod tests { #[test] fn system_call_async_to_missing_code_is_noop() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -883,13 +907,13 @@ mod tests { .unwrap() .discard(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), 0); } #[test] fn system_call_async_send_future_is_send() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -905,7 +929,7 @@ mod tests { .unwrap() .discard(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), 0); } @@ -913,7 +937,7 @@ mod tests { #[should_panic = "async EVM execution requires EVM erased fields to be verified as Send with Evm::evm_is_send"] fn system_call_async_send_panics_after_overlay_db_mut_replaces_database() { let marker = Rc::new(()); - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -929,7 +953,7 @@ mod tests { #[test] fn system_call_async_succeeds_after_database_error() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -951,13 +975,13 @@ mod tests { .unwrap() .discard(); - assert!(result.status); + assert!(result.is_success()); } #[test] fn dropping_transact_async_discards_nonce_after_cancelled_db_future() { let caller = Address::ZERO; - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[op::PUSH1, 0, op::SLOAD, op::STOP])); let mut evm = Evm::::new( SpecId::OSAKA, @@ -1041,12 +1065,11 @@ mod tests { } } - let caller = Address::from([0xaa; 20]); - let target = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0xaa); + let target = Address::repeat_byte(0x11); // SLOAD 0, add 1, SSTORE 0, then CALL self with all but 1024 gas remaining. - let code = Bytecode::new_legacy(Bytes::from_static(&hex!( - "60005460010160005560006000600060006000306104005a03f100" - ))); + let code = + Bytecode::new_legacy(bytes!("60005460010160005560006000600060006000306104005a03f100")); let mut database = InMemoryDB::default(); database.insert_account_info( &caller, @@ -1056,16 +1079,14 @@ mod tests { &target, AccountInfo { nonce: 1, ..AccountInfo::default().with_code(code) }, ); - let mut version = Version::new(SpecId::FRONTIER); - version.initial_call_depth = initial_call_depth; - let mut evm = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(SpecId::FRONTIER, version), + let mut evm = Evm::::new( SpecId::FRONTIER, BlockEnvExt::default(), crate::ethereum::ethereum_tx_registry(SpecId::FRONTIER), database, Precompiles::base(SpecId::FRONTIER), ); + evm.set_initial_call_depth(initial_call_depth); evm.set_inspector(DepthInspector::default()); let tx = Recovered::new_unchecked( crate::ethereum::TxEnvelope::Legacy(TxLegacy { @@ -1078,7 +1099,7 @@ mod tests { let result = poll_ready(evm.transact_async(&tx)).unwrap().commit(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(result.tx_gas_used(), 5_291_175); assert_eq!( evm.state_mut().storage_slot_untracked(&target, &Word::ZERO).unwrap(), @@ -1098,22 +1119,6 @@ mod tests { const TEST_TX_TYPE: u8 = 0x00; - fn test_tx(value: u64) -> crate::ethereum::RecoveredTxEnvelope { - Recovered::new_unchecked( - crate::ethereum::TxEnvelope::Legacy(TxLegacy { nonce: value, ..TxLegacy::default() }), - Address::ZERO, - ) - } - - fn handle_test_tx(req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>) -> HandlerResult { - let _ = req.host.spec_id(); - Ok(TxResultExt { - status: true, - total_gas_spent: req.tx.nonce + 1, - ..TxResultExt::default() - }) - } - fn poll_ready(future: F) -> F::Output { let mut future = core::pin::pin!(future); let waker = Waker::noop(); diff --git a/crates/evm2/src/evm/bal/account.rs b/crates/evm2/src/evm/bal/account.rs index 60aea3c3f..ca26d5f50 100644 --- a/crates/evm2/src/evm/bal/account.rs +++ b/crates/evm2/src/evm/bal/account.rs @@ -6,7 +6,7 @@ use super::{ }; use crate::{ bytecode::{Bytecode, BytecodeDecodeError}, - evm::state::{AccountInfo, StorageSlot}, + evm::state::AccountInfo, }; use alloc::vec::Vec; use alloy_eip7928::{ @@ -108,10 +108,10 @@ impl From for AlloyAccountChanges { .collect::>(); let mut balance_changes = account.account_info.balance.changes; - balance_changes.sort_unstable_by_key(|change| change.block_access_index); + balance_changes.sort_unstable_by_key(|change| change.block_access_index()); let mut nonce_changes = account.account_info.nonce.changes; - nonce_changes.sort_unstable_by_key(|change| change.block_access_index); + nonce_changes.sort_unstable_by_key(|change| change.block_access_index()); let mut code_changes = account .account_info @@ -120,7 +120,7 @@ impl From for AlloyAccountChanges { .into_iter() .map(AlloyCodeChange::from) .collect::>(); - code_changes.sort_unstable_by_key(|change| change.block_access_index); + code_changes.sort_unstable_by_key(|change| change.block_access_index()); Self { address: Address::ZERO, @@ -226,11 +226,11 @@ impl AccountInfoBal { self.nonce.update(index, &original.nonce, present.nonce); self.balance.update(index, &original.balance, present.balance); if original.code_hash != present.code_hash { - self.code.update_with_key( + self.code_update( index, &(original.code_hash, original.code.clone().unwrap_or_default()), - (present.code_hash, present.code.clone().unwrap_or_default()), - |i| &i.0, + present.code_hash, + present.code.clone().unwrap_or_default(), ); } } @@ -320,20 +320,6 @@ impl StorageBal { } } - /// Update storage from an account's pending [`StorageSlot`] overlay: a changed slot records a - /// write at `bal_index`, a loaded-but-unchanged slot records a read. - #[inline] - pub fn update_pending(&mut self, bal_index: BlockAccessIndex, slots: &U256Map) { - self.storage.reserve(slots.len()); - for (key, slot) in slots { - self.storage.entry(*key).or_default().update( - bal_index, - &slot.value.original, - slot.value.current, - ); - } - } - /// Update reads with new storage keys. /// /// It will expend inner map with new reads. diff --git a/crates/evm2/src/evm/bal/bal_context.rs b/crates/evm2/src/evm/bal/bal_context.rs index 31f72bdeb..2d8bffe55 100644 --- a/crates/evm2/src/evm/bal/bal_context.rs +++ b/crates/evm2/src/evm/bal/bal_context.rs @@ -1,15 +1,16 @@ //! EIP-7928 Block Access List state carried alongside the accepted-overlay database. -use super::{AccountBal, Bal, BalError, BlockAccessIndex}; +use super::{AccountBal, Bal, BalError, BlockAccessIndex, list::BalWriter}; use crate::{ evm::state::{ - Account, AccountChangeRef, AccountInfo, PendingState, StateChangeSink, StorageChange, + AccountChangeRef, AccountInfo, PendingState, StateChangeSink, StateChangeSource, + StorageChange, TxStateChanges, }, interpreter::Word, }; use alloc::sync::Arc; use alloy_eip7928::{BalAccountInfo, BlockAccessList}; -use alloy_primitives::{Address, map::AddressMap}; +use alloy_primitives::Address; use core::convert::Infallible; /// Result of an EIP-7928 BAL lookup during a read. @@ -68,7 +69,7 @@ impl BalContext { /// Attaches an EIP-7928 BAL consulted on reads, and returns `self`. #[inline] pub fn with_bal(mut self, bal: Arc) -> Self { - self.bal = Some(bal); + self.set_bal(bal); self } @@ -111,7 +112,7 @@ impl BalContext { /// Enables EIP-7928 BAL construction, installing an empty builder, and returns `self`. #[inline] pub fn with_bal_builder(mut self) -> Self { - self.bal_builder = Some(Bal::new()); + self.enable_bal_builder(); self } @@ -169,23 +170,26 @@ impl BalContext { /// are recorded as BAL reads; changed ones as writes. #[inline] pub fn commit_pending(&mut self, pending: &PendingState) { - self.commit(&pending.accounts); + self.commit(pending.changes()); } - /// Folds a committed transaction's pending accounts and storage overlays into the BAL builder - /// at the current [`Self::bal_index`]. + /// Folds a committed transaction's changes into the BAL builder at the current + /// [`Self::bal_index`]. /// /// Same as [`Self::commit_pending`], operating on the transaction layers directly so the /// overlay need not be detached. No-op when BAL construction is disabled. #[inline] - pub(crate) fn commit(&mut self, accounts: &AddressMap) { - let index = self.bal_index; - let Some(bal) = self.bal_builder.as_mut() else { + pub(crate) fn commit(&mut self, changes: TxStateChanges<'_>) { + let Some(mut writer) = self.writer() else { return; }; - for (&address, account) in accounts { - bal.update_account(index, address, account); - } + let Ok(()) = changes.visit(&mut writer); + } + + #[inline] + fn writer(&mut self) -> Option> { + let index = self.bal_index; + self.bal_builder.as_mut().map(|bal| BalWriter::new(bal, index)) } /// Takes the built BAL, resetting the block access index. Returns `None` when BAL construction @@ -321,58 +325,28 @@ pub enum BalAccountLookup { NotCovered, } -/// Folds streamed state changes into the BAL builder at the current [`BalContext::bal_index`]. -/// -/// This is the sink shape of the [`Self::commit_pending`] fold: changed accounts and storage -/// slots are -/// recorded as writes, loaded-but-unchanged ones -- the read callbacks -- as reads. The bytecode -/// and storage-wipe callbacks need no BAL action: code changes surface through -/// [`StateChangeSink::account`], and a wiped account's storage surfaces through the storage -/// callbacks. Every callback is a no-op when BAL construction is disabled. +/// Folds streamed state changes into the BAL builder at the current [`BalContext::bal_index`], as +/// [`Bal::commit`] does. Every callback is a no-op when BAL construction is disabled. impl StateChangeSink for BalContext { type Error = Infallible; #[inline] fn account(&mut self, change: AccountChangeRef<'_>) -> Result<(), Self::Error> { - let index = self.bal_index; - if let Some(bal) = self.bal_builder.as_mut() { - let original = change.original.cloned().unwrap_or_default(); - let current = change.current.cloned().unwrap_or_default(); - bal.accounts - .entry(change.address) - .or_default() - .account_info - .update(index, &original, ¤t); - } - Ok(()) + self.writer().map_or(Ok(()), |mut writer| writer.account(change)) } #[inline] fn storage(&mut self, change: StorageChange) -> Result<(), Self::Error> { - let index = self.bal_index; - if let Some(bal) = self.bal_builder.as_mut() { - bal.accounts - .entry(change.address) - .or_default() - .storage - .storage - .entry(change.key) - .or_default() - .update(index, &change.original, change.current); - } - Ok(()) + self.writer().map_or(Ok(()), |mut writer| writer.storage(change)) } #[inline] fn account_read( &mut self, address: Address, - _info: Option<&AccountInfo>, + info: Option<&AccountInfo>, ) -> Result<(), Self::Error> { - if let Some(bal) = self.bal_builder.as_mut() { - bal.accounts.entry(address).or_default(); - } - Ok(()) + self.writer().map_or(Ok(()), |mut writer| writer.account_read(address, info)) } #[inline] @@ -380,12 +354,9 @@ impl StateChangeSink for BalContext { &mut self, address: Address, key: Word, - _value: Word, + value: Word, ) -> Result<(), Self::Error> { - if let Some(bal) = self.bal_builder.as_mut() { - bal.accounts.entry(address).or_default().storage.storage.entry(key).or_default(); - } - Ok(()) + self.writer().map_or(Ok(()), |mut writer| writer.storage_read(address, key, value)) } } @@ -427,7 +398,7 @@ mod tests { let code = Bytecode::new_raw(bytes!("6001")); let mut account = AccountBal::default(); account.account_info.balance = vec![ - BalanceChange::new(BlockAccessIndex::new(0), U256::from(1)), + BalanceChange::new(BlockAccessIndex::new(0), U256::ONE), BalanceChange::new(BlockAccessIndex::new(1), U256::from(7)), // Post-execution for a two-transaction block. BalanceChange::new(BlockAccessIndex::new(3), U256::from(42)), @@ -443,7 +414,7 @@ mod tests { for (index, balance, nonce) in [ (0, None, None), - (1, Some(U256::from(1)), None), + (1, Some(U256::ONE), None), (2, Some(U256::from(7)), None), (3, Some(U256::from(7)), Some(5)), ] { diff --git a/crates/evm2/src/evm/bal/changes.rs b/crates/evm2/src/evm/bal/changes.rs index 25e443116..3257fcfb8 100644 --- a/crates/evm2/src/evm/bal/changes.rs +++ b/crates/evm2/src/evm/bal/changes.rs @@ -134,7 +134,7 @@ impl TryFrom<&AlloyCodeChange> for BalCodeChange { fn try_from(change: &AlloyCodeChange) -> Result { let bytecode = Bytecode::new_raw_checked(change.new_code().clone())?; let hash = bytecode.hash_slow(); - Ok(Self { block_access_index: change.block_access_index, code: (hash, bytecode) }) + Ok(Self { block_access_index: change.block_access_index(), code: (hash, bytecode) }) } } @@ -380,14 +380,14 @@ mod tests { #[test] fn update_drops_change_reverted_at_same_index() { let mut changes = BalChanges::::default(); - changes.update(idx(1), &U256::ZERO, U256::from(1)); - changes.update(idx(1), &U256::from(1), U256::ZERO); + changes.update(idx(1), &U256::ZERO, U256::ONE); + changes.update(idx(1), &U256::ONE, U256::ZERO); assert!(changes.is_empty()); - changes.update(idx(1), &U256::ZERO, U256::from(1)); - changes.update(idx(2), &U256::from(1), U256::from(2)); - changes.update(idx(2), &U256::from(2), U256::from(1)); - assert_eq!(changes.changes, vec![StorageChange::new(idx(1), U256::from(1))]); + changes.update(idx(1), &U256::ZERO, U256::ONE); + changes.update(idx(2), &U256::ONE, U256::from(2)); + changes.update(idx(2), &U256::from(2), U256::ONE); + assert_eq!(changes.changes, vec![StorageChange::new(idx(1), U256::ONE)]); let code = Bytecode::new_raw(bytes!("6001")); let mut changes = BalChanges::::default(); diff --git a/crates/evm2/src/evm/bal/list.rs b/crates/evm2/src/evm/bal/list.rs index 3ffe51e88..0c90ea69a 100644 --- a/crates/evm2/src/evm/bal/list.rs +++ b/crates/evm2/src/evm/bal/list.rs @@ -1,16 +1,21 @@ //! The top-level Block Access List structure. -use super::{AccountBal, BalError, BlockAccessIndex}; +use super::{AccountBal, BlockAccessIndex}; use crate::{ bytecode::BytecodeDecodeError, - evm::state::{Account, AccountInfo, PendingState}, + evm::state::{ + AccountChangeRef, AccountInfo, PendingState, StateChangeSink, StateChangeSource, + StorageChange, + }, + interpreter::Word, }; use alloc::vec::Vec; use alloy_eip7928::{ AccountChanges as AlloyAccountChanges, BlockAccessList as AlloyBal, CodeChange as AlloyCodeChange, SlotChanges as AlloySlotChanges, }; -use alloy_primitives::{Address, U256, map::AddressMap}; +use alloy_primitives::{Address, map::AddressMap}; +use core::convert::Infallible; /// BAL structure. #[derive(Debug, Default, Clone, PartialEq, Eq)] @@ -71,7 +76,7 @@ impl Bal { changes.sort(); changes })); - alloy_bal.sort_unstable_by_key(|account| account.address); + alloy_bal.sort_unstable_by_key(|account| account.address()); alloy_bal } @@ -80,56 +85,84 @@ impl Bal { /// This is the fold [`Evm`](crate::Evm) applies on transaction commit when its BAL builder is /// enabled: loaded-but-unchanged accounts and storage slots are recorded as reads, changed /// ones as writes at `bal_index`. Apply pending states in transaction order, since writes - /// must be appended with ascending indices. - pub fn commit(&mut self, bal_index: BlockAccessIndex, pending: PendingState) { - for (&address, account) in &pending.accounts { - self.update_account(bal_index, address, account); - } - } - - /// Extend BAL with one pending account overlay entry: its transaction-boundary original info - /// against its present info, and its loaded storage slots. An absent side is the non-existent - /// (default) account, so an account removed by the transaction records zeroed writes. + /// must be appended with ascending indices. Commit each transaction once, at its own index: a + /// wiped slot is recorded as a read, which does not retract a write an earlier commit made at + /// the same index. /// /// A selfdestructed account needs no special-casing: transaction finalization already resolved /// its present info to the EIP-8246 balance-only remnant or to a removed account, and its /// destroyed storage writes surface as reads (execution-specs `destroy_storage`). + pub fn commit(&mut self, bal_index: BlockAccessIndex, pending: PendingState) { + let Ok(()) = pending.visit(&mut BalWriter::new(self, bal_index)); + } +} + +/// Folds streamed state changes into a [`Bal`] at one block access index. +/// +/// Changed accounts and storage slots are recorded as writes, loaded-but-unchanged ones -- the +/// read callbacks -- as reads. An absent account side is the non-existent (default) account, so an +/// account removed by the transaction records zeroed writes. The bytecode and storage-wipe +/// callbacks need no BAL action: code changes surface through [`StateChangeSink::account`], and a +/// wiped account's storage surfaces through the storage callbacks. +#[derive(Debug)] +pub(super) struct BalWriter<'a> { + bal: &'a mut Bal, + index: BlockAccessIndex, +} + +impl<'a> BalWriter<'a> { #[inline] - pub(crate) fn update_account( - &mut self, - bal_index: BlockAccessIndex, - address: Address, - account: &Account, - ) { - let bal_account = self.accounts.entry(address).or_default(); + pub(super) const fn new(bal: &'a mut Bal, index: BlockAccessIndex) -> Self { + Self { bal, index } + } +} + +impl StateChangeSink for BalWriter<'_> { + type Error = Infallible; + + #[inline] + fn account(&mut self, change: AccountChangeRef<'_>) -> Result<(), Self::Error> { let absent = AccountInfo::default(); - bal_account.account_info.update( - bal_index, - account.original.as_ref().unwrap_or(&absent), - account.present.as_ref().unwrap_or(&absent), + self.bal.accounts.entry(change.address).or_default().account_info.update( + self.index, + change.original.unwrap_or(&absent), + change.current.unwrap_or(&absent), ); - bal_account.storage.update_pending(bal_index, &account.storage.slots); + Ok(()) } - /// Populate storage slot from BAL by account address. - /// - /// If the account is not found in the BAL, or the slot is not listed under it, an error is - /// returned. #[inline] - pub fn populate_storage_slot( - &self, - account_address: Address, - bal_index: BlockAccessIndex, - key: U256, - value: &mut U256, - ) -> Result<(), BalError> { - let Some(bal_account) = self.accounts.get(&account_address) else { - return Err(BalError::AccountNotFound { address: account_address }); - }; + fn storage(&mut self, change: StorageChange) -> Result<(), Self::Error> { + self.bal + .accounts + .entry(change.address) + .or_default() + .storage + .storage + .entry(change.key) + .or_default() + .update(self.index, &change.original, change.current); + Ok(()) + } - if let Some(bal_value) = bal_account.storage.get(&account_address, key, bal_index)? { - *value = bal_value; - }; + #[inline] + fn account_read( + &mut self, + address: Address, + _info: Option<&AccountInfo>, + ) -> Result<(), Self::Error> { + self.bal.accounts.entry(address).or_default(); + Ok(()) + } + + #[inline] + fn storage_read( + &mut self, + address: Address, + key: Word, + _value: Word, + ) -> Result<(), Self::Error> { + self.bal.accounts.entry(address).or_default().storage.storage.entry(key).or_default(); Ok(()) } } @@ -158,7 +191,7 @@ impl From for AlloyBal { .into_iter() .map(|(address, account)| AlloyAccountChanges { address, ..account.into() }), ); - alloy_bal.sort_unstable_by_key(|a| a.address); + alloy_bal.sort_unstable_by_key(|a| a.address()); alloy_bal } } @@ -180,7 +213,7 @@ impl TryFrom<&[AlloyAccountChanges]> for Bal { let mut accounts = AddressMap::with_capacity_and_hasher(alloy_bal.len(), Default::default()); for alloy_account in alloy_bal { - accounts.insert(alloy_account.address, AccountBal::try_from(alloy_account)?); + accounts.insert(alloy_account.address(), AccountBal::try_from(alloy_account)?); } Ok(Self { accounts }) @@ -203,7 +236,7 @@ impl TryFrom for Bal { let mut accounts = AddressMap::with_capacity_and_hasher(alloy_bal.len(), Default::default()); for alloy_account in alloy_bal { - let address = alloy_account.address; + let address = alloy_account.address(); accounts.insert(address, AccountBal::try_from(alloy_account)?); } @@ -236,7 +269,12 @@ impl core::fmt::Display for Bal { } else { writeln!(f, " Nonce writes:")?; for change in &account.account_info.nonce.changes { - writeln!(f, " [{}] -> {}", change.block_access_index, change.new_nonce)?; + writeln!( + f, + " [{}] -> {}", + change.block_access_index(), + change.new_nonce() + )?; } } @@ -249,7 +287,8 @@ impl core::fmt::Display for Bal { writeln!( f, " [{}] -> {}", - change.block_access_index, change.post_balance + change.block_access_index(), + change.post_balance() )?; } } @@ -307,8 +346,9 @@ mod tests { bytecode::Bytecode, evm::{ bal::{AccountInfoBal, BalChanges, BalCodeChange, StorageBal}, - state::{Account, AccountInfo, StorageOverlay, StorageSlot, Tracked}, + state::{Account, AccountFlags, AccountInfo, StorageOverlay}, }, + test_utils::storage_slot, }; use alloc::{vec, vec::Vec}; use alloy_eip7928::{ @@ -316,10 +356,7 @@ mod tests { CodeChange as AlloyCodeChange, NonceChange as AlloyNonceChange, SlotChanges as AlloySlotChanges, StorageChange as AlloyStorageChange, }; - use alloy_primitives::{ - B256, Bytes, U256, - map::{AddressSet, U256Map}, - }; + use alloy_primitives::{B256, Bytes, U256, map::U256Map}; fn code(byte: u8) -> (B256, Bytecode) { let bytecode = Bytecode::new_raw(vec![byte].into()); @@ -359,7 +396,7 @@ mod tests { AlloyStorageChange::new(idx(6), U256::from(60)), ]), ), - (U256::from(1), BalChanges::default()), + (U256::ONE, BalChanges::default()), ( U256::from(2), BalChanges::from(vec![ @@ -383,12 +420,12 @@ mod tests { assert_eq!(alloy_bal, AlloyBal::from(bal)); assert_eq!( - alloy_bal.iter().map(|account| account.address).collect::>(), + alloy_bal.iter().map(|account| account.address()).collect::>(), vec![low_address, high_address] ); let account = &alloy_bal[0]; - assert_eq!(account.storage_reads, vec![U256::from(1), U256::from(3)]); + assert_eq!(account.storage_reads, vec![U256::ONE, U256::from(3)]); assert_eq!( account.storage_changes.iter().map(|slot| slot.slot).collect::>(), vec![U256::from(2), U256::from(4)] @@ -413,7 +450,7 @@ mod tests { account .balance_changes .iter() - .map(|change| change.block_access_index) + .map(|change| change.block_access_index()) .collect::>(), vec![idx(2), idx(5)] ); @@ -421,12 +458,16 @@ mod tests { account .nonce_changes .iter() - .map(|change| change.block_access_index) + .map(|change| change.block_access_index()) .collect::>(), vec![idx(4), idx(9)] ); assert_eq!( - account.code_changes.iter().map(|change| change.block_access_index).collect::>(), + account + .code_changes + .iter() + .map(|change| change.block_access_index()) + .collect::>(), vec![idx(3), idx(7)] ); assert_eq!(account.code_changes[0].new_code().as_ref(), &[3]); @@ -440,29 +481,21 @@ mod tests { assert!(AlloyBal::from(&bal).is_empty()); } - fn slot(original: U256, current: U256) -> StorageSlot { - StorageSlot { value: Tracked::from_parts(original, current), ..Default::default() } - } - #[test] fn commit_builds_bal_from_pending_overlay() { let address = Address::with_last_byte(1); // A freshly created account: no original info, present nonce/balance set, and one changed // storage slot plus one loaded-but-unchanged (read) slot. - let account = Account { - original: None, - present: Some(AccountInfo::default().with_nonce(1).with_balance(U256::from(100))), - ..Default::default() - }; + let account = Account::new( + None, + Some(AccountInfo::default().with_nonce(1).with_balance(U256::from(100))), + ); let mut overlay = StorageOverlay::default(); - overlay.slots.insert(U256::from(5), slot(U256::ZERO, U256::from(42))); - overlay.slots.insert(U256::from(6), slot(U256::from(7), U256::from(7))); + overlay.slots.insert(U256::from(5), storage_slot(U256::ZERO, U256::from(42))); + overlay.slots.insert(U256::from(6), storage_slot(U256::from(7), U256::from(7))); let account = Account { storage: overlay, ..account }; - let pending = PendingState { - accounts: AddressMap::from_iter([(address, account)]), - selfdestructs: Default::default(), - }; + let pending = PendingState { accounts: AddressMap::from_iter([(address, account)]) }; let mut bal = Bal::new(); bal.commit(idx(1), pending); @@ -492,18 +525,12 @@ mod tests { // (`present: None`), storage overlay wiped with the prior writes turned into // loaded-but-unchanged slots surfacing as reads. The BAL derives from that overlay // without special-casing. - let account = Account { - original: Some(AccountInfo::default().with_balance(U256::from(100))), - present: None, - ..Default::default() - }; + let account = + Account::new(Some(AccountInfo::default().with_balance(U256::from(100))), None); let mut overlay = StorageOverlay { wiped: true, ..Default::default() }; - overlay.slots.insert(U256::from(5), slot(U256::from(42), U256::from(42))); - let account = Account { storage: overlay, ..account }; - let pending = PendingState { - accounts: AddressMap::from_iter([(address, account)]), - selfdestructs: AddressSet::from_iter([address]), - }; + overlay.slots.insert(U256::from(5), storage_slot(U256::from(42), U256::from(42))); + let account = Account { storage: overlay, flags: AccountFlags::DESTROYED, ..account }; + let pending = PendingState { accounts: AddressMap::from_iter([(address, account)]) }; let mut bal = Bal::new(); bal.commit(idx(2), pending); @@ -518,6 +545,35 @@ mod tests { assert!(account.storage.storage.get(&U256::from(5)).unwrap().is_empty()); } + #[test] + fn commit_records_wiped_slot_zeroing_as_read() { + let address = Address::with_last_byte(1); + + // A wiped account whose slot started nonzero and ended zero: the wipe is not a write. + // Only the slot that is nonzero after the wipe is recorded as one. + let info = AccountInfo::default().with_balance(U256::from(100)); + let mut overlay = StorageOverlay { wiped: true, ..Default::default() }; + overlay.slots.insert(U256::from(5), storage_slot(U256::from(42), U256::ZERO)); + overlay.slots.insert(U256::from(6), storage_slot(U256::from(7), U256::from(8))); + let account = Account { + original: Some(info.clone()), + present: Some(info), + storage: overlay, + ..Default::default() + }; + let pending = PendingState { accounts: AddressMap::from_iter([(address, account)]) }; + + let mut bal = Bal::new(); + bal.commit(idx(1), pending); + + let storage = &bal.accounts.get(&address).unwrap().storage.storage; + assert!(storage.get(&U256::from(5)).unwrap().is_empty()); + assert_eq!( + storage.get(&U256::from(6)).unwrap().changes, + vec![AlloyStorageChange::new(idx(1), U256::from(8))] + ); + } + #[test] fn try_from_alloy_decodes_block_access_list() { let address = Address::with_last_byte(1); @@ -542,7 +598,7 @@ mod tests { let alloy_bal = vec![AlloyAccountChanges { address, storage_changes: vec![AlloySlotChanges::new( - U256::from(1), + U256::ONE, vec![AlloyStorageChange::new(idx(1), U256::from(10))], )], storage_reads: vec![U256::from(2)], diff --git a/crates/evm2/src/evm/config.rs b/crates/evm2/src/evm/config.rs index 08dcb70a5..fed42dd91 100644 --- a/crates/evm2/src/evm/config.rs +++ b/crates/evm2/src/evm/config.rs @@ -2,7 +2,6 @@ use crate::{ Evm, OpcodeConfig, OpcodeView, SpecId, - constants::CALL_DEPTH_LIMIT, ethereum::TxEnvelope, interpreter::{ Host, @@ -189,17 +188,17 @@ impl ExecutionConfig { /// Replaces the runtime version data while keeping the same dispatch table. /// + /// Clamps [`Version::memory_limit`] to [`u32::MAX`]. + /// /// # Panics /// - /// Panics if [`Version::initial_call_depth`] leaves no room for the full call budget and - /// the attempted-too-deep message, i.e. if it exceeds `u16::MAX - CALL_DEPTH_LIMIT - 1` - /// (64510). + /// Panics if `version` is invalid; see [`Version::assert_valid`]. #[inline] - pub const fn with_version(mut self, version: Version) -> Self { - assert!( - version.initial_call_depth < u16::MAX - CALL_DEPTH_LIMIT, - "initial call depth must leave room for 1024 descendants and the rejected attempt" - ); + pub const fn with_version(mut self, mut version: Version) -> Self { + version.assert_valid(); + if version.memory_limit > u32::MAX as u64 { + version.memory_limit = u32::MAX as u64; + } self.version = version; self } diff --git a/crates/evm2/src/evm/db/cache.rs b/crates/evm2/src/evm/db/cache.rs index 4300e99e6..670163a7d 100644 --- a/crates/evm2/src/evm/db/cache.rs +++ b/crates/evm2/src/evm/db/cache.rs @@ -8,7 +8,7 @@ use crate::{ bal::BalContext, state::{ Account, AccountChangeRef, AccountInfo, PendingState, StateChangeSink, - StateChangeSource, StorageChange, + StateChangeSource, StorageChange, TxStateChanges, }, }, interpreter::Word, @@ -88,7 +88,6 @@ impl Default for Cache { fn default() -> Self { let mut contracts = B256Map::default(); contracts.insert(KECCAK256_EMPTY, Bytecode::default()); - contracts.insert(B256::ZERO, Bytecode::default()); Self { accounts: AddressMap::default(), contracts, @@ -118,7 +117,7 @@ pub struct CacheDB { pub _non_exhaustive: (), } -impl Default for CacheDB { +impl Default for InMemoryDB { #[inline] fn default() -> Self { Self::new(EmptyDB::default()) @@ -159,7 +158,7 @@ impl CacheDB { pub(crate) fn commit(&mut self, accounts: &AddressMap) { // When BAL construction is enabled, fold the transaction's pending post-state into the // builder before applying the changes to the cache. - self.bal_context.commit(accounts); + self.bal_context.commit(TxStateChanges::new(accounts)); for (&address, entry) in accounts { let overlay = &entry.storage; @@ -199,6 +198,7 @@ impl CacheDB { #[inline] fn insert_contract_inner(contracts: &mut B256Map, info: &mut AccountInfo) { + normalize_code_hash(info); if let Some(code) = &info.code && !code.is_empty() { @@ -207,9 +207,6 @@ impl CacheDB { } contracts.entry(info.code_hash).or_insert_with(|| code.clone()); } - if info.code_hash.is_zero() { - info.code_hash = KECCAK256_EMPTY; - } } /// Inserts account info. @@ -251,6 +248,19 @@ impl CacheDB { pub fn insert_block_hash(&mut self, number: &Word, hash: &B256) { self.cache.block_hashes.insert(*number, *hash); } + + /// Checks that the attached read BAL, if any, covers storage slot `key`. + /// + /// Used by reads that know the slot's value without consulting storage, such as a slot of an + /// account created in the transaction, so they keep the coverage check of + /// [`DynDatabase::get_storage`]. + #[inline] + pub(crate) fn check_bal_storage(&self, address: &Address, key: &Word) -> DbResult<()> { + match self.bal_context.bal_storage(address, key) { + Ok(_) => Ok(()), + Err(err) => Err(DatabaseError::new(err, false)), + } + } } /// Applies streamed changes to the cache and forwards every callback to [`BalContext`]'s own sink @@ -348,6 +358,11 @@ impl DynDatabase for CacheDB { } }; + // The cache fields are public, so entries written directly can still carry a zero hash. + if let Some(info) = &mut account { + normalize_code_hash(info); + } + // Apply the resolved BAL account's info at the current block access index. if let Some(bal_account) = bal_account { self.bal_context.populate_bal_account(bal_account, &mut account); @@ -357,6 +372,7 @@ impl DynDatabase for CacheDB { #[inline] fn get_code_by_hash(&mut self, code_hash: &B256) -> DbResult { + let code_hash = if code_hash.is_zero() { &KECCAK256_EMPTY } else { code_hash }; match self.cache.contracts.entry(*code_hash) { Entry::Occupied(entry) => Ok(entry.get().clone()), Entry::Vacant(entry) => Ok(entry.insert(self.db.get_code_by_hash(code_hash)?).clone()), @@ -414,6 +430,14 @@ impl DynDatabase for CacheDB { } } +/// Maps a zero code hash, which some databases use for no code, to the empty code hash. +#[inline] +fn normalize_code_hash(info: &mut AccountInfo) { + if info.code_hash.is_zero() { + info.code_hash = KECCAK256_EMPTY; + } +} + mod typed { use super::*; use crate::evm::Database; @@ -537,7 +561,7 @@ mod tests { } impl crate::evm::Database for CountingDB { - type Error = core::convert::Infallible; + type Error = Infallible; fn get_account(&mut self, _address: &Address) -> Result, Self::Error> { self.account_loads += 1; @@ -565,6 +589,29 @@ mod tests { } } + #[test] + fn cache_db_normalizes_zero_code_hash() { + let address = Address::with_last_byte(1); + let zero_hash = AccountInfo { code_hash: B256::ZERO, ..AccountInfo::empty() }; + + let db = CountingDB { account: Some(zero_hash.clone()), ..CountingDB::default() }; + let mut cache = CacheDB::new(crate::evm::Db::new(db)); + let loaded = cache.get_account(&address).unwrap().unwrap(); + assert_eq!(loaded.code_hash, KECCAK256_EMPTY); + + let mut cache = InMemoryDB::default(); + cache.insert_account_info(&address, zero_hash.clone()); + assert_eq!(cache.account_info(&address).unwrap().code_hash, KECCAK256_EMPTY); + + let mut incoming = Cache::default(); + incoming.accounts.insert(address, Some(zero_hash)); + let mut cache = InMemoryDB::default(); + cache.cache.merge(incoming); + let loaded = cache.get_account(&address).unwrap().unwrap(); + assert_eq!(loaded.code_hash, KECCAK256_EMPTY); + assert_eq!(cache.get_code_by_hash(&B256::ZERO).unwrap(), Bytecode::default()); + } + #[test] fn cache_db_caches_wrapped_db_reads() { let address = Address::with_last_byte(1); diff --git a/crates/evm2/src/evm/db/mod.rs b/crates/evm2/src/evm/db/mod.rs index 2ed888343..7e8a7cee6 100644 --- a/crates/evm2/src/evm/db/mod.rs +++ b/crates/evm2/src/evm/db/mod.rs @@ -547,7 +547,7 @@ mod tests { } fn get_block_hash(&mut self, _number: &Word) -> DbResult { - Ok(B256::from([*self.value; 32])) + Ok(B256::repeat_byte(*self.value)) } } @@ -556,7 +556,7 @@ mod tests { let value = 1; let mut erased = boxed_dyn_database(BorrowingDb { value: &value }); - assert_eq!(erased.get_block_hash(&Word::ZERO).unwrap(), B256::from([value; 32])); + assert_eq!(erased.get_block_hash(&Word::ZERO).unwrap(), B256::repeat_byte(value)); } #[test] diff --git a/crates/evm2/src/evm/inspector.rs b/crates/evm2/src/evm/inspector.rs index 0e9e70b0d..495e565ec 100644 --- a/crates/evm2/src/evm/inspector.rs +++ b/crates/evm2/src/evm/inspector.rs @@ -9,6 +9,101 @@ use alloc::boxed::Box; use alloy_primitives::{Address, Log, U256}; use auto_impl::auto_impl; +/// Set of opcodes. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct OpcodeSet(U256); + +impl OpcodeSet { + /// Set containing no opcode. + pub const EMPTY: Self = Self(U256::ZERO); + + /// Set containing every opcode value. + pub const ALL: Self = Self(U256::MAX); + + /// Returns whether the set contains `opcode`. + #[inline] + pub const fn contains(&self, opcode: u8) -> bool { + self.0.bit(opcode as usize) + } + + /// Adds `opcode` to the set. + #[inline] + pub const fn insert(&mut self, opcode: u8) { + self.0.set_bit(opcode as usize, true); + } + + /// Returns the opcodes in `self` or `other`. + #[inline] + pub fn union(self, other: Self) -> Self { + Self(self.0 | other.0) + } +} + +impl FromIterator for OpcodeSet { + fn from_iter>(opcodes: I) -> Self { + let mut set = Self::EMPTY; + for opcode in opcodes { + set.insert(opcode); + } + set + } +} + +/// What an [`Inspector`] observes of execution. +/// +/// Returned by [`Inspector::inspector_config`]. The EVM reads it when a frame starts and after each +/// `hooked` instruction of the frame, such as a CALL or CREATE, and runs the frame so that it only +/// pays for what the config observes. The default observes everything. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct InspectorConfig { + steps: OpcodeSet, +} + +impl InspectorConfig { + /// Creates a config that observes everything. + #[inline] + pub const fn new() -> Self { + Self { steps: OpcodeSet::ALL } + } + + /// Creates a config that calls no step hooks. + #[inline] + pub const fn without_steps() -> Self { + Self { steps: OpcodeSet::EMPTY } + } + + /// Sets the opcodes for which [`Inspector::step`] and [`Inspector::step_end`] are called. + /// + /// The EVM may call the step hooks for more opcodes than set, for example when several + /// inspectors share a frame, so a hook must ignore the opcodes it does not handle. Call, + /// create, log and self-destruct hooks run regardless. + #[inline] + pub const fn with_steps(mut self, steps: OpcodeSet) -> Self { + self.steps = steps; + self + } + + /// Returns the opcodes for which [`Inspector::step`] and [`Inspector::step_end`] are called. + #[inline] + pub const fn steps(&self) -> OpcodeSet { + self.steps + } + + /// Returns a config that observes everything either `self` or `other` observes. + #[inline] + pub fn union(self, other: Self) -> Self { + self.with_steps(self.steps.union(other.steps)) + } +} + +impl Default for InspectorConfig { + /// Returns a config that observes everything. + #[inline] + fn default() -> Self { + Self::new() + } +} + /// EVM execution inspector. #[auto_impl(&mut, Box)] pub trait Inspector: NonStaticAny { @@ -18,6 +113,17 @@ pub trait Inspector: NonStaticAny { let _ = interp; } + /// Returns what this inspector observes, as an [`InspectorConfig`]. + /// + /// Read when a frame starts and after each `hooked` instruction of the frame returns, such as a + /// CALL or CREATE, so a child message may change the answer. A change that a hook makes + /// itself, such as in [`Self::step`], takes effect after the next `hooked` instruction of the + /// frame. The default observes everything. + #[inline] + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::new() + } + /// Called before each instruction executes. #[inline] fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { @@ -25,6 +131,8 @@ pub trait Inspector: NonStaticAny { } /// Called after each instruction executes. + /// + /// If the instruction ran out of gas, the interpreter's remaining gas reads 0. #[inline] fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { let _ = interp; @@ -41,6 +149,10 @@ pub trait Inspector: NonStaticAny { /// /// The interpreter is the currently running frame whose instruction produced the message; for /// the top-level message it is a frame initialized with the message itself. + /// + /// A first frame that runs out of gas in the transaction's runtime gas phase halts without + /// executing: this hook still fires for it, with a message that carries no bytecode, a + /// returned result is ignored, and [`Inspector::call_end`] receives the out-of-gas result. #[inline] fn call( &mut self, @@ -69,6 +181,10 @@ pub trait Inspector: NonStaticAny { /// /// The interpreter is the currently running frame whose instruction produced the message; for /// the top-level message it is a frame initialized with the message itself. + /// + /// A first frame that runs out of gas in the transaction's runtime gas phase halts without + /// executing: this hook still fires for it, with a message that carries no bytecode, a + /// returned result is ignored, and [`Inspector::create_end`] receives the out-of-gas result. #[inline] fn create( &mut self, @@ -121,7 +237,195 @@ pub(crate) fn boxed_inspector<'a, T: EvmTypesHost>( #[derive(Clone, Debug, Default)] pub struct NoopInspector(()); -impl Inspector for NoopInspector {} +impl Inspector for NoopInspector { + #[inline] + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::without_steps() + } +} + +/// Runs both inspectors, `L` first. +/// +/// Every hook is forwarded to both, and the steps are those of either inspector. When both `call` +/// or `create` hooks return a result, the result from `L` wins. +impl, R: Inspector> Inspector for (L, R) { + #[inline] + fn inspector_config(&self) -> InspectorConfig { + self.0.inspector_config().union(self.1.inspector_config()) + } + + #[inline] + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { + self.0.initialize_interp(interp); + self.1.initialize_interp(interp); + } + + #[inline] + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { + self.0.step(interp); + self.1.step(interp); + } + + #[inline] + fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { + self.0.step_end(interp); + self.1.step_end(interp); + } + + #[inline] + fn log(&mut self, log: &Log, host: &mut T::Host<'_>) { + self.0.log(log, host); + self.1.log(log, host); + } + + #[inline] + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + let first = self.0.call(interp, message); + let second = self.1.call(interp, message); + first.or(second) + } + + #[inline] + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + self.0.call_end(interp, message, result); + self.1.call_end(interp, message, result); + } + + #[inline] + fn create( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + let first = self.0.create(interp, message); + let second = self.1.create(interp, message); + first.or(second) + } + + #[inline] + fn create_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + self.0.create_end(interp, message, result); + self.1.create_end(interp, message, result); + } + + #[inline] + fn selfdestruct( + &mut self, + contract: &Address, + target: &Address, + value: &U256, + host: &mut T::Host<'_>, + ) { + self.0.selfdestruct(contract, target, value, host); + self.1.selfdestruct(contract, target, value, host); + } +} + +/// Forwards every hook to the inner inspector when present; `None` behaves like +/// [`NoopInspector`] and steps nothing. +impl> Inspector for Option { + #[inline] + fn inspector_config(&self) -> InspectorConfig { + self.as_ref().map_or_else(InspectorConfig::without_steps, I::inspector_config) + } + + #[inline] + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { + if let Some(inspector) = self { + inspector.initialize_interp(interp); + } + } + + #[inline] + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { + if let Some(inspector) = self { + inspector.step(interp); + } + } + + #[inline] + fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { + if let Some(inspector) = self { + inspector.step_end(interp); + } + } + + #[inline] + fn log(&mut self, log: &Log, host: &mut T::Host<'_>) { + if let Some(inspector) = self { + inspector.log(log, host); + } + } + + #[inline] + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + self.as_mut()?.call(interp, message) + } + + #[inline] + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + if let Some(inspector) = self { + inspector.call_end(interp, message, result); + } + } + + #[inline] + fn create( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + self.as_mut()?.create(interp, message) + } + + #[inline] + fn create_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + if let Some(inspector) = self { + inspector.create_end(interp, message, result); + } + } + + #[inline] + fn selfdestruct( + &mut self, + contract: &Address, + target: &Address, + value: &U256, + host: &mut T::Host<'_>, + ) { + if let Some(inspector) = self { + inspector.selfdestruct(contract, target, value, host); + } + } +} impl<'a, T: EvmTypesHost> core::ops::Deref for dyn Inspector + 'a { type Target = dyn NonStaticAny + 'a; @@ -143,25 +447,29 @@ impl<'a, T: EvmTypesHost> core::ops::DerefMut for dyn Inspector + 'a { mod tests { use super::*; use crate::{ - BaseEvmTypes, DatabaseError, Evm, EvmTypesHost, ExecutionConfig, Precompiles, SpecId, - Version, + BaseEvmConfig, BaseEvmTypes, DatabaseError, Evm, EvmConfig, EvmTypesHost, ExecutionConfig, + OpcodeConfig, Precompiles, SpecId, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, ethereum::{TxEnvelope, ethereum_tx_registry}, evm::{AccountInfo, EmptyDB, InMemoryDB, SYSTEM_ADDRESS, State, StateCheckpoint}, interpreter::{ - GasTracker, Host, InstrStop, Interpreter, Message, MessageExt, MessageResult, - MessageResultExt, Word, op, + GasTracker, Host, InstrStop, Interpreter, Message, MessageExt, MessageInput, + MessageKind, MessageResult, MessageResultExt, Word, instructions as instr, op, }, registry::TxRegistry, - test_utils::{TestHost, TestTypes, legacy_bytecode, push, push_all}, - utils::address_to_word, + test_utils::{ + TestHost, TestTypes, call_code, create_code, legacy_bytecode, memory_input_call_code, + push, push_all, return_top_word, + }, + utils::{WordExt, address_to_word}, }; use alloc::{boxed::Box, vec, vec::Vec}; use alloy_consensus::{TxLegacy, transaction::Recovered}; use alloy_primitives::{Address, Bytes, Log, TxKind, U256}; use core::assert_matches; + use evm2_macros::instruction; #[derive(Default)] struct SelfdestructInspector { @@ -213,6 +521,7 @@ mod tests { _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, message: &mut Message, ) -> Option> { + assert_eq!(message.code_address, message.destination); self.create_depths.push(message.depth); self.create_destinations.push(message.destination); None @@ -390,42 +699,13 @@ mod tests { Precompiles::base(spec_id), ); evm.set_inspector(inspector); - let tx_env = TxEnvExt::default(); let bytecode = legacy_bytecode(code); let mut message = MessageExt { gas_limit, code: bytecode, ..message.clone() }; - let result = Host::execute_message(&mut evm, &tx_env, &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); let inspector = evm.clear_inspector_as::().unwrap(); (result, inspector, evm) } - /// Appends code that returns the word at the top of the stack as the frame output. - fn return_top_word(code: &mut Vec) { - code.extend([op::PUSH0, op::MSTORE, op::PUSH1, 32, op::PUSH0, op::RETURN]); - } - - fn call_code(target: Address) -> Vec { - let mut code = Vec::new(); - push_all( - &mut code, - [ - Word::ZERO, - Word::ZERO, - Word::ZERO, - Word::ZERO, - Word::ZERO, - address_to_word(&target), - Word::from(1000), - ], - ); - code - } - - fn create_code() -> Vec { - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); - code - } - #[test] fn empty_calls_do_not_inspect_padding_stop() { for nested in [false, true] { @@ -484,51 +764,672 @@ mod tests { assert_eq!(inspector.step_ends, 1); } - #[test] - fn inspector_gas_edits_survive_dispatch() { - #[derive(Default)] - struct GasInspector { - steps: usize, - ends: usize, + /// Records the stack and gas around the steps its config asks for. + #[derive(Default)] + struct ConfigInspector { + before: OpcodeSet, + after: Option, + switched: bool, + /// Opcodes between their `step` and `step_end`, innermost frame last. + running: Vec, + steps: Vec<(u8, u64, Vec)>, + step_ends: Vec<(u8, u64, Vec)>, + calls: usize, + creates: usize, + /// The pc, opcode and stack that the call and create hooks of child messages see. + views: Vec<(usize, u8, Vec)>, + logs: Vec, + /// Stops the parent frame when a child message ends. + stop_after_child: bool, + /// Stops the frame in `step_end` of this opcode. + stop_in_step_end: Option, + /// Stops every frame before it runs. + stop_on_init: bool, + /// Answers the calls of child messages in the `call` hook, so no child frame runs. + answer_calls: Option>, + } + + impl ConfigInspector { + fn new(steps: OpcodeSet) -> Self { + Self { before: steps, ..Default::default() } } - impl Inspector for GasInspector { - fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { - let mut gas = interp.gas(); - gas.set_remaining(100); - gas.set_refunded(7); - interp.set_gas(gas); - } + /// Uses `after` instead of `before` once a child message starts. + fn switching_to(mut self, after: OpcodeSet) -> Self { + self.after = Some(after); + self + } - fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { - let expected = [100, 128, 156, 184, 212, 236][self.steps]; - assert_eq!(interp.gas().remaining(), expected); - assert_eq!(interp.gas().refunded(), 7); - interp.gas_mut().set_remaining(expected + 10); - self.steps += 1; + /// Answers the calls of child messages with `result` in the `call` hook. + fn answering_calls(mut self, result: MessageResult) -> Self { + self.answer_calls = Some(result); + self + } + } + + impl Inspector for ConfigInspector { + fn inspector_config(&self) -> InspectorConfig { + let steps = match self.after { + Some(after) if self.switched => after, + _ => self.before, + }; + InspectorConfig::new().with_steps(steps) + } + + fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + let opcode = interp.opcode(); + self.running.push(opcode); + self.steps.push((opcode, interp.gas().remaining(), interp.stack().to_vec())); + } + + fn step_end(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + let opcode = self.running.pop().unwrap(); + self.step_ends.push((opcode, interp.gas().remaining(), interp.stack().to_vec())); + if self.stop_in_step_end == Some(opcode) { + interp.set_stop(InstrStop::Revert); } + } - fn step_end(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { - let expected = [108, 136, 164, 192, 216, 246][self.ends]; - assert_eq!(interp.gas().remaining(), expected); - if self.ends == 1 { - assert_eq!(interp.stack().last(), Some(&Word::from(136))); - } - interp.gas_mut().set_remaining(expected + 20); - self.ends += 1; + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + if self.stop_on_init { + interp.set_stop(InstrStop::Revert); } } - let (result, inspector, _) = run_evm_with_inspector( - Vec::from([op::PUSH0, op::GAS, op::POP, op::PUSH0, op::MSTORE, op::STOP]), - &MessageExt::default(), - 1000, - GasInspector::default(), - ); - assert_eq!(result.stop, InstrStop::Stop); - assert_eq!(result.gas.remaining(), 266); - assert_eq!(result.gas.refunded(), 7); - assert_eq!((inspector.steps, inspector.ends), (6, 6)); + fn log(&mut self, log: &Log, _host: &mut Evm<'_, BaseEvmTypes>) { + self.logs.push(log.clone()); + } + + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + self.calls += 1; + if message.depth > 0 { + self.views.push((interp.pc(), interp.opcode(), interp.stack().to_vec())); + } + // Like a cheatcode that changes what its caller's inspector wants to see. + self.switched |= message.depth > 0; + if message.depth > 0 + && let Some(answer) = &self.answer_calls + { + let mut result = answer.clone(); + result.gas.set_remaining(message.gas_limit); + return Some(result); + } + None + } + + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &Message, + _result: &mut MessageResult, + ) { + if self.stop_after_child && message.depth > 0 { + interp.set_stop(InstrStop::Revert); + } + } + + fn create( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + self.creates += 1; + if message.depth > 0 { + self.views.push((interp.pc(), interp.opcode(), interp.stack().to_vec())); + } + None + } + } + + fn opcode_set(opcodes: impl IntoIterator) -> OpcodeSet { + opcodes.into_iter().collect() + } + + /// Runs `code` through the first call of a frame without an inspector. + fn run_evm_uninspected( + db: InMemoryDB, + code: Vec, + gas_limit: u64, + ) -> (MessageResult, Vec) { + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + db, + Precompiles::base(SpecId::OSAKA), + ); + let mut message = + MessageExt { gas_limit, code: legacy_bytecode(code), ..Default::default() }; + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + (result, evm.logs().to_vec()) + } + + /// Code that stores, loads, logs, calls a child that loads a slot, and creates. + fn config_test_code() -> (InMemoryDB, Vec) { + let child = Address::repeat_byte(0x66); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &child, + AccountInfo::default().with_code(legacy_bytecode(vec![ + op::PUSH0, + op::SLOAD, + op::POP, + op::STOP, + ])), + ); + let mut code = vec![op::PUSH1, 5, op::PUSH0, op::SSTORE, op::PUSH0, op::SLOAD, op::POP]; + code.extend([op::PUSH1, 0xaa, op::PUSH0, op::MSTORE8, op::PUSH1, 1, op::PUSH0, op::LOG0]); + code.extend(call_code(child)); + code.extend([op::CALL, op::POP]); + code.extend(create_code()); + code.extend([op::CREATE, op::POP, op::PUSH0, op::SLOAD, op::POP, op::STOP]); + (db, code) + } + + #[test] + fn sparse_steps_match_full_inspector_at_those_opcodes() { + let (db, code) = config_test_code(); + let (expected, expected_logs) = run_evm_uninspected(db.clone(), code.clone(), 1_000_000); + let (result, full, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + ConfigInspector::new(OpcodeSet::ALL), + ); + assert_eq!(result, expected); + assert_eq!(full.logs, expected_logs); + + for opcodes in [ + vec![op::SLOAD, op::SSTORE, op::CALL, op::CREATE], + vec![op::SLOAD], + vec![op::CALL, op::CREATE, op::CREATE2], + vec![op::POP, op::STOP, op::LOG0], + vec![op::INVALID], + ] { + let steps = opcode_set(opcodes); + let (result, sparse, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + ConfigInspector::new(steps), + ); + let filter = |records: &[(u8, u64, Vec)]| { + records + .iter() + .filter(|record| steps.contains(record.0)) + .cloned() + .collect::>() + }; + assert_eq!(result, expected, "{steps:?}"); + assert_eq!(sparse.logs, expected_logs); + assert_eq!(sparse.steps, filter(&full.steps)); + assert_eq!(sparse.step_ends, filter(&full.step_ends)); + assert_eq!((sparse.calls, sparse.creates), (full.calls, full.creates)); + } + } + + #[test] + fn message_hook_can_stop_the_parent_without_steps() { + let (db, code) = config_test_code(); + let run = |steps| { + let inspector = + ConfigInspector { stop_after_child: true, ..ConfigInspector::new(steps) }; + run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + inspector, + ) + }; + let (full, _, _) = run(OpcodeSet::ALL); + assert_eq!(full.stop, InstrStop::Revert); + for steps in [OpcodeSet::EMPTY, opcode_set([op::SLOAD]), opcode_set([op::CALL])] { + let (result, inspector, _) = run(steps); + assert_eq!(result, full); + // The frame stopped at the CALL, before the later CREATE. + assert_eq!(inspector.creates, 0); + } + // The stop wins over a reload for the changed steps. + let inspector = ConfigInspector { + stop_after_child: true, + ..ConfigInspector::new(OpcodeSet::EMPTY).switching_to(OpcodeSet::ALL) + }; + let (result, inspector, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + inspector, + ); + assert_eq!(result, full); + assert_eq!(inspector.creates, 0); + } + + #[test] + fn step_end_stop_wins_over_a_reload() { + let (db, code) = config_test_code(); + let run = |inspector| { + run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + inspector, + ) + }; + let (full, _, _) = run(ConfigInspector { + stop_in_step_end: Some(op::CALL), + ..ConfigInspector::new(OpcodeSet::ALL) + }); + assert_eq!(full.stop, InstrStop::Revert); + // The child message of the stepped CALL changes the steps while its `step_end` stops. + let (result, inspector, _) = run(ConfigInspector { + stop_in_step_end: Some(op::CALL), + ..ConfigInspector::new(opcode_set([op::CALL])).switching_to(opcode_set([op::SSTORE])) + }); + assert_eq!(result, full); + // The frame stopped at the CALL, before the later CREATE. + assert_eq!(inspector.creates, 0); + } + + #[test] + fn message_hooks_see_the_message_instruction_whatever_the_steps() { + let (db, code) = config_test_code(); + let views = |steps| { + run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + ConfigInspector::new(steps), + ) + .1 + .views + }; + let full = views(OpcodeSet::ALL); + assert_eq!(full.iter().map(|view| view.1).collect::>(), [op::CALL, op::CREATE]); + assert_eq!(full[0].2.len(), 7); + assert_eq!(full[1].2.len(), 3); + for steps in [OpcodeSet::EMPTY, opcode_set([op::SLOAD]), opcode_set([op::CALL])] { + assert_eq!(views(steps), full, "{steps:?}"); + } + } + + #[test] + fn stop_set_before_the_run_ends_the_frame_without_steps() { + let code = vec![op::PUSH1, 1, op::PUSH0, op::SSTORE, op::STOP]; + let run = |steps| { + let inspector = ConfigInspector { stop_on_init: true, ..ConfigInspector::new(steps) }; + run_evm_with_inspector(code.clone(), &MessageExt::default(), 100_000, inspector).0 + }; + let full = run(OpcodeSet::ALL); + assert_eq!((full.stop, full.gas.remaining()), (InstrStop::Revert, 100_000)); + for steps in [OpcodeSet::EMPTY, opcode_set([op::SLOAD]), opcode_set([op::SSTORE])] { + assert_eq!(run(steps), full); + } + } + + /// The base config with `CALL` also installed at [`CUSTOM_CALL`]. + struct CustomCallConfig; + + const CUSTOM_CALL: u8 = 0xb0; + + impl EvmConfig for CustomCallConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::>(CUSTOM_CALL, 100); + config + }; + } + + /// The base config with `CALL` installed at `PUSH30`, an opcode with immediate bytes. + struct PushSlotCallConfig; + + impl EvmConfig for PushSlotCallConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::>(op::PUSH30, 100); + config + }; + } + + /// A hooked call that pushes a word after `call_with`. + #[instruction(no_stack_preamble, hooked, EvmTypes = BaseEvmTypes)] + fn call_then_push(cx: _) -> Result { + instr::call_with( + stack.reborrow(), + cx.gas, + cx.state, + MessageKind::Call, + |_, _, _| Ok(()), + |_, _, _, _| Ok(()), + )?; + stack.push_u64(0x77)?; + Ok(()) + } + + /// The base config with the call that pushes a word installed at [`CUSTOM_CALL`]. + struct CallThenPushConfig; + + impl EvmConfig for CallThenPushConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::(CUSTOM_CALL, 100); + config + }; + } + + /// A hooked call that runs its child message through the host instead of `call_with`. + #[instruction(no_stack_preamble, hooked, EvmTypes = BaseEvmTypes)] + fn host_call(cx: _) -> Result { + let [_, to, _, _, _, _, _] = stack.popn::<7>()?; + let to = to.to_address(); + let current = cx.state.message(); + let mut message = MessageExt { + kind: MessageKind::Call, + depth: current.depth + 1, + gas_limit: 10_000, + destination: to, + call_target: to, + caller: current.destination, + code_address: to, + ..Default::default() + }; + let result = + cx.state.host().execute_message(&mut message).map_err(|error| cx.state.fail(error))?; + stack.push_u64(result.stop.is_success() as u64) + } + + /// The base config with the call through the host installed at [`CUSTOM_CALL`]. + struct HostCallConfig; + + impl EvmConfig for HostCallConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::(CUSTOM_CALL, 100); + config + }; + } + + /// A call whose `before` hook overwrites the caller's memory. + #[instruction(no_stack_preamble, hooked, EvmTypes = BaseEvmTypes)] + fn overwriting_call(cx: _) -> Result { + instr::call_with( + stack, + cx.gas, + cx.state, + MessageKind::Call, + |_, state, _| { + state.memory().set(0, &[0xbb; 32]); + Ok(()) + }, + |_, _, _, _| Ok(()), + ) + } + + /// The base config with [`overwriting_call`] installed at [`CUSTOM_CALL`]. + struct OverwritingCallConfig; + + impl EvmConfig for OverwritingCallConfig { + const BASE_SPEC_ID: SpecId = SpecId::OSAKA; + const OPCODE_CONFIG: &'static OpcodeConfig = &{ + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::(CUSTOM_CALL, 100); + config + }; + } + + /// Runs a call to a child through `message_op`, which `C` installs as a call, followed by + /// `immediates`. + fn run_custom_call>( + inspector: ConfigInspector, + message_op: u8, + immediates: &[u8], + ) -> (MessageResult, ConfigInspector) { + let mut code = call_code(Address::repeat_byte(0x66)); + code.push(message_op); + code.extend(immediates); + code.extend([op::GAS, op::POP, op::STOP]); + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_config::(), + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + evm.set_inspector(inspector); + let mut message = + MessageExt { gas_limit: 100_000, code: legacy_bytecode(code), ..Default::default() }; + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + (result, *evm.clear_inspector_as::().unwrap()) + } + + /// Asserts that the frame steps the set that the child message of [`CUSTOM_CALL`] switches to, + /// from the opcode after it. + fn assert_custom_call_rereads_the_steps>() { + let (expected, full) = + run_custom_call::(ConfigInspector::new(OpcodeSet::ALL), CUSTOM_CALL, &[]); + + // The child message of the custom opcode switches the steps to `GAS`, and no steps before. + let (result, inspector) = run_custom_call::( + ConfigInspector::new(OpcodeSet::EMPTY).switching_to(opcode_set([op::GAS])), + CUSTOM_CALL, + &[], + ); + assert_eq!(result, expected); + let gas = full.steps.iter().position(|record| record.0 == op::GAS).unwrap(); + assert_eq!(inspector.steps, [full.steps[gas].clone()]); + } + + #[test] + fn custom_call_opcode_rereads_the_steps() { + assert_custom_call_rereads_the_steps::(); + } + + #[test] + fn hooked_opcode_that_runs_a_message_through_the_host_rereads_the_steps() { + assert_custom_call_rereads_the_steps::(); + } + + #[test] + fn custom_call_opcode_with_immediates_resumes_after_them() { + // The immediates are invalid opcodes that fail the frame if the reload resumes inside them. + let immediates = [op::INVALID; 30]; + let (expected, _) = run_custom_call::( + ConfigInspector::new(OpcodeSet::ALL), + op::PUSH30, + &immediates, + ); + assert_eq!(expected.stop, InstrStop::Stop); + + let (result, inspector) = run_custom_call::( + ConfigInspector::new(OpcodeSet::EMPTY).switching_to(opcode_set([op::GAS])), + op::PUSH30, + &immediates, + ); + assert_eq!(result, expected); + assert_eq!(inspector.steps.len(), 1); + } + + #[test] + fn empty_steps_skip_step_hooks_but_keep_message_and_log_hooks() { + let (db, code) = config_test_code(); + let (expected, expected_logs) = run_evm_uninspected(db.clone(), code.clone(), 1_000_000); + let (result, inspector, _) = run_evm_with_inspector_db( + db, + code, + &MessageExt::default(), + 1_000_000, + ConfigInspector::new(OpcodeSet::EMPTY), + ); + + assert_eq!(result, expected); + assert!(inspector.steps.is_empty()); + assert!(inspector.step_ends.is_empty()); + // The top-level message and the child call. + assert_eq!(inspector.calls, 2); + assert_eq!(inspector.creates, 1); + assert_eq!(inspector.logs, expected_logs); + assert_eq!(inspector.logs.len(), 1); + } + + /// Runs a `CALL` followed by `GAS`, `POP` and `STOP` through inspectors that switch their steps + /// when the child message starts, with the call answered by `answer` in the hook if given. + /// Asserts that the caller steps the new set from the opcode after the `CALL`, and returns the + /// result and the inspector of the run that steps every opcode. + fn assert_steps_follow_the_child( + answer: Option>, + ) -> (MessageResult, ConfigInspector) { + let mut code = call_code(Address::repeat_byte(0x66)); + code.extend([op::CALL, op::GAS, op::POP, op::STOP]); + let run = |inspector: ConfigInspector| { + let inspector = match &answer { + Some(answer) => inspector.answering_calls(answer.clone()), + None => inspector, + }; + run_evm_with_inspector(code.clone(), &MessageExt::default(), 100_000, inspector) + }; + let (expected, full, _) = run(ConfigInspector::new(OpcodeSet::ALL)); + let call = full.steps.iter().position(|record| record.0 == op::CALL).unwrap(); + let end = full.steps.len(); + + for (before, after, range, kept) in [ + // Starts stepping after the CALL. + (OpcodeSet::EMPTY, OpcodeSet::ALL, call + 1..end, OpcodeSet::ALL), + // Stops stepping after the CALL. + (OpcodeSet::ALL, OpcodeSet::EMPTY, 0..call + 1, OpcodeSet::ALL), + ( + OpcodeSet::EMPTY, + opcode_set([op::GAS, op::STOP]), + call + 1..end, + opcode_set([op::GAS, op::STOP]), + ), + ( + opcode_set([op::CALL]), + opcode_set([op::POP]), + call..end, + opcode_set([op::CALL, op::POP]), + ), + ] { + let (result, inspector, _) = run(ConfigInspector::new(before).switching_to(after)); + assert_eq!(result, expected, "{before:?} {after:?}"); + let keep = |records: &[(u8, u64, Vec)]| { + let records = records[range.clone()].iter(); + records.filter(|record| kept.contains(record.0)).cloned().collect::>() + }; + assert_eq!(inspector.steps, keep(&full.steps), "{before:?} {after:?}"); + assert_eq!(inspector.step_ends, keep(&full.step_ends), "{before:?} {after:?}"); + } + // The first step after the CALL sees its success flag on the stack. + let (_, inspector, _) = + run(ConfigInspector::new(OpcodeSet::EMPTY).switching_to(OpcodeSet::ALL)); + assert_eq!(inspector.steps[0], (op::GAS, full.steps[call + 1].1, vec![Word::ONE])); + (expected, *full) + } + + #[test] + fn child_message_can_change_the_parent_steps() { + let mut code = call_code(Address::repeat_byte(0x66)); + code.extend([op::CALL, op::GAS, op::POP, op::STOP]); + let (uninspected, _) = run_evm_uninspected(InMemoryDB::default(), code, 100_000); + let (result, _) = assert_steps_follow_the_child(None); + assert_eq!(result, uninspected); + } + + #[test] + fn answered_call_can_change_the_parent_steps() { + let answer = MessageResultExt { + stop: InstrStop::Return, + output: Bytes::from_static(&[0xaa]), + ..Default::default() + }; + let (_, full) = assert_steps_follow_the_child(Some(answer)); + // The hook answers the call, so every step belongs to the caller. + assert_eq!(full.calls, 2); + } + + #[test] + fn code_after_call_with_runs_when_the_steps_change() { + let (expected, full) = run_custom_call::( + ConfigInspector::new(OpcodeSet::ALL), + CUSTOM_CALL, + &[], + ); + let gas = full.steps.iter().find(|record| record.0 == op::GAS).unwrap(); + // The instruction pushes a word after `call_with`, below the one that `GAS` pushes. + assert_eq!(gas.2, [Word::ONE, Word::from(0x77)]); + + let (result, inspector) = run_custom_call::( + ConfigInspector::new(OpcodeSet::EMPTY).switching_to(opcode_set([op::GAS])), + CUSTOM_CALL, + &[], + ); + assert_eq!(result, expected); + assert_eq!(inspector.steps, core::slice::from_ref(gas)); + } + + #[test] + fn inspector_gas_edits_survive_dispatch() { + #[derive(Default)] + struct GasInspector { + steps: usize, + ends: usize, + } + + impl Inspector for GasInspector { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + let mut gas = interp.gas(); + gas.set_remaining(100); + gas.set_refund(7); + interp.set_gas(gas); + } + + fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + let expected = [100, 128, 156, 184, 212, 236][self.steps]; + assert_eq!(interp.gas().remaining(), expected); + assert_eq!(interp.gas().refunded(), 7); + interp.gas_mut().set_remaining(expected + 10); + self.steps += 1; + } + + fn step_end(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + let expected = [108, 136, 164, 192, 216, 246][self.ends]; + assert_eq!(interp.gas().remaining(), expected); + if self.ends == 1 { + assert_eq!(interp.stack().last(), Some(&Word::from(136))); + } + interp.gas_mut().set_remaining(expected + 20); + self.ends += 1; + } + } + + let (result, inspector, _) = run_evm_with_inspector( + Vec::from([op::PUSH0, op::GAS, op::POP, op::PUSH0, op::MSTORE, op::STOP]), + &MessageExt::default(), + 1000, + GasInspector::default(), + ); + assert_eq!(result.stop, InstrStop::Stop); + assert_eq!(result.gas.remaining(), 266); + assert_eq!(result.gas.refunded(), 7); + assert_eq!((inspector.steps, inspector.ends), (6, 6)); } #[test] @@ -574,7 +1475,7 @@ mod tests { Some(MessageResultExt { stop: InstrStop::Return, gas: GasTracker::new(100), - created_address: Some(Address::from([0x77; 20])), + created_address: Some(Address::repeat_byte(0x77)), ..Default::default() }) } @@ -611,7 +1512,7 @@ mod tests { } for (mut code, opcode) in - [(call_code(Address::from([0x66; 20])), op::CALL), (create_code(), op::CREATE)] + [(call_code(Address::repeat_byte(0x66)), op::CALL), (create_code(), op::CREATE)] { code.extend([opcode, op::GAS, op::STOP]); let (result, inspector, _) = run_evm_with_inspector( @@ -698,7 +1599,7 @@ mod tests { ); assert_eq!(result.stop, InstrStop::Revert); - assert_eq!(inspector.stack, [Word::from(1), Word::from(2)]); + assert_eq!(inspector.stack, [Word::ONE, Word::from(2)]); assert_eq!(inspector.steps, 3); assert_eq!(inspector.step_ends, 2); assert_eq!(result.gas.remaining(), 123); @@ -739,7 +1640,7 @@ mod tests { ); assert_eq!(result.stop, InstrStop::Revert); - assert_eq!(inspector.stack, [Word::from(1)]); + assert_eq!(inspector.stack, [Word::ONE]); assert_eq!(inspector.steps, 1); assert_eq!(inspector.step_ends, 1); assert_eq!(result.gas.remaining(), 123); @@ -747,21 +1648,19 @@ mod tests { #[test] fn call_too_deep_is_inspected_without_executing() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut code = call_code(target); code.extend([op::CALL, op::STOP]); for initial_call_depth in [0, 7] { - let mut version = Version::new(SpecId::OSAKA); - version.initial_call_depth = initial_call_depth; - let mut evm = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(SpecId::OSAKA, version), + let mut evm = Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), TxRegistry::new(), InMemoryDB::default(), Precompiles::base(SpecId::OSAKA), ); + evm.set_initial_call_depth(initial_call_depth); evm.set_inspector(HookInspector::default()); let depth = initial_call_depth + CALL_DEPTH_LIMIT; let mut message = MessageExt { @@ -770,8 +1669,7 @@ mod tests { code: legacy_bytecode(code.clone()), ..Default::default() }; - let result = - Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); let inspector = evm.clear_inspector_as::().unwrap(); assert_matches!(result.stop, InstrStop::Stop); assert_eq!(inspector.call_depths, [depth, depth + 1]); @@ -781,7 +1679,7 @@ mod tests { #[test] fn call_inspector_override_skips_execution_and_still_calls_end() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let inspector = OverrideCallInspector { result: MessageResultExt { stop: InstrStop::Return, @@ -808,7 +1706,7 @@ mod tests { #[test] fn call_inspector_override_wins_at_max_depth() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let inspector = OverrideCallInspector { result: MessageResultExt { stop: InstrStop::Return, ..Default::default() }, min_depth: CALL_DEPTH_LIMIT + 1, @@ -826,124 +1724,342 @@ mod tests { inspector, ); - assert_matches!(result.stop, InstrStop::Return); - // The override wins over the call depth check: the call succeeds. - assert_eq!(Word::from_be_slice(&result.output), Word::from(1)); - assert_eq!(inspector.call_depth, Some(CALL_DEPTH_LIMIT + 1)); - assert_eq!(inspector.call_end_stop, Some(InstrStop::Return)); + assert_matches!(result.stop, InstrStop::Return); + // The override wins over the call depth check: the call succeeds. + assert_eq!(Word::from_be_slice(&result.output), Word::ONE); + assert_eq!(inspector.call_depth, Some(CALL_DEPTH_LIMIT + 1)); + assert_eq!(inspector.call_end_stop, Some(InstrStop::Return)); + } + + #[test] + fn call_inspector_can_mutate_message_before_execution() { + struct MutateCallInspector { + destination: Address, + } + + impl Inspector for MutateCallInspector { + fn call( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.depth > 0 { + message.destination = self.destination; + message.code_address = self.destination; + } + None + } + } + + let target = Address::repeat_byte(0x22); + let replacement = Address::repeat_byte(0x33); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &Address::ZERO, + AccountInfo::default().with_balance(U256::from(100)), + ); + let mut code = Vec::new(); + push_all( + &mut code, + [ + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::from(7), + address_to_word(&target), + Word::from(50_000), + ], + ); + code.extend([op::CALL, op::STOP]); + + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + db, + Precompiles::base(SpecId::OSAKA), + ); + evm.set_inspector(MutateCallInspector { destination: replacement }); + let bytecode = legacy_bytecode(code); + let mut message = MessageExt { gas_limit: 100_000, code: bytecode, ..Default::default() }; + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + + assert_matches!(result.stop, InstrStop::Stop); + // The redirected call transferred the value to the replacement, not the target. + let mut balance = |address| { + evm.state_mut() + .account_info_untracked(&address) + .unwrap() + .map_or(U256::ZERO, |info| info.balance) + }; + assert_eq!(balance(replacement), U256::from(7)); + assert_eq!(balance(target), U256::ZERO); + } + + #[test] + fn call_end_can_mutate_result_before_caller_observes_it() { + struct CallEndInspector; + + impl Inspector for CallEndInspector { + fn call( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.depth == 0 { + return None; + } + Some(MessageResultExt { + stop: InstrStop::Revert, + gas: GasTracker::new(message.gas_limit), + ..Default::default() + }) + } + + fn call_end( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &Message, + result: &mut MessageResult, + ) { + if message.depth > 0 { + result.stop = InstrStop::Return; + result.output = Bytes::from_static(&[0xaa, 0xbb]); + } + } + } + + let target = Address::repeat_byte(0x22); + let mut code = call_code(target); + code.extend([op::CALL, op::POP, op::RETURNDATASIZE]); + return_top_word(&mut code); + + let (result, _, _) = + run_evm_with_inspector(code, &MessageExt::default(), 50_000, CallEndInspector); + + assert_matches!(result.stop, InstrStop::Return); + // `call_end` upgraded the override from a revert to a 2-byte return. + assert_eq!(Word::from_be_slice(&result.output), Word::from(2)); + } + + /// An account that returns its call data in [`echo_db`]. + const ECHO: Address = Address::repeat_byte(0x22); + + /// Returns a database with an account at [`ECHO`] that returns its call data. + fn echo_db() -> InMemoryDB { + let mut db = InMemoryDB::default(); + db.insert_account_info( + &ECHO, + AccountInfo::default().with_code(legacy_bytecode(vec![ + op::CALLDATASIZE, + op::PUSH0, + op::PUSH0, + op::CALLDATACOPY, + op::CALLDATASIZE, + op::PUSH0, + op::RETURN, + ])), + ); + db + } + + #[test] + fn call_hook_overwriting_caller_memory_changes_child_input() { + struct OverwriteCallerMemory; + + impl Inspector for OverwriteCallerMemory { + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.depth > 0 { + // The input is a range of the caller's memory, read through the caller. + assert_eq!(message.input.as_owned(), None); + assert_eq!(message.input.len(), 32); + assert_eq!(message.input.as_bytes(interp.memory()), [0xaa; 32]); + interp.memory_mut().set(0, &[0xbb; 32]); + } + None + } + + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &Message, + _result: &mut MessageResult, + ) { + if message.depth > 0 { + assert_eq!(message.input.to_bytes(interp.memory())[..], [0xbb; 32]); + } + } + } + + for target in [ECHO, Address::with_last_byte(4)] { + let (result, _, _) = run_evm_with_inspector_db( + echo_db(), + memory_input_call_code(target, op::CALL), + &MessageExt::default(), + 100_000, + OverwriteCallerMemory, + ); + + assert_matches!(result.stop, InstrStop::Return); + assert_eq!(result.output[..], [0xbb; 32], "{target}"); + } } #[test] - fn call_inspector_can_mutate_message_before_execution() { - struct MutateCallInspector { - destination: Address, - } + #[should_panic = "a call input must be resolved against the frame that made it"] + fn forwarding_a_borrowed_call_input_from_another_frame_panics() { + // Forwards the borrowed input of the child, a range of the caller's memory, from the + // child's own frame, where the range would name the child's memory. + struct ForwardInput; - impl Inspector for MutateCallInspector { - fn call( - &mut self, - _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, - message: &mut Message, - ) -> Option> { - if message.depth > 0 { - message.destination = self.destination; - message.code_address = self.destination; + impl Inspector for ForwardInput { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + if interp.message().depth == 1 { + let mut message = MessageExt { + depth: 2, + gas_limit: 1_000, + input: interp.message().input.clone(), + code: legacy_bytecode([op::STOP]), + ..Default::default() + }; + let _ = Host::execute_message(interp.host(), &mut message); } - None } } - let target = Address::from([0x22; 20]); - let replacement = Address::from([0x33; 20]); - let mut db = InMemoryDB::default(); - db.insert_account_info( - &Address::ZERO, - AccountInfo::default().with_balance(U256::from(100)), + run_evm_with_inspector_db( + echo_db(), + memory_input_call_code(ECHO, op::CALL), + &MessageExt::default(), + 100_000, + ForwardInput, ); - let mut code = Vec::new(); + } + + #[test] + #[should_panic = "a call input must be resolved against the frame that made it"] + fn forwarding_a_borrowed_call_input_from_a_sibling_frame_panics() { + // Keeps the borrowed input of the first child and forwards it from the second child, a + // frame at the same depth whose memory the range would name. + #[derive(Default)] + struct ForwardSiblingInput(Option); + + impl Inspector for ForwardSiblingInput { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + if interp.message().depth != 1 { + return; + } + match self.0.take() { + None => self.0 = Some(interp.message().input.clone()), + Some(input) => { + let mut message = MessageExt { + depth: 2, + gas_limit: 1_000, + input, + code: legacy_bytecode([op::STOP]), + ..Default::default() + }; + let _ = Host::execute_message(interp.host(), &mut message); + } + } + } + } + + // Calls `ECHO` twice with memory 0..32 as input. + let mut code = memory_input_call_code(ECHO, op::CALL); + code.truncate(code.len() - 5); push_all( &mut code, [ + Word::from(32), + Word::from(32), + Word::from(32), Word::ZERO, Word::ZERO, - Word::ZERO, - Word::ZERO, - Word::from(7), - address_to_word(&target), + address_to_word(&ECHO), Word::from(50_000), ], ); - code.extend([op::CALL, op::STOP]); + code.extend([op::CALL, op::POP, op::STOP]); - let mut evm = Evm::::new( - SpecId::OSAKA, - BlockEnvExt::default(), - TxRegistry::new(), - db, - Precompiles::base(SpecId::OSAKA), + run_evm_with_inspector_db( + echo_db(), + code, + &MessageExt::default(), + 100_000, + ForwardSiblingInput::default(), ); - evm.set_inspector(MutateCallInspector { destination: replacement }); - let tx_env = TxEnvExt::default(); - let bytecode = legacy_bytecode(code); - let mut message = MessageExt { gas_limit: 100_000, code: bytecode, ..Default::default() }; - let result = Host::execute_message(&mut evm, &tx_env, &mut message).unwrap(); - - assert_matches!(result.stop, InstrStop::Stop); - // The redirected call transferred the value to the replacement, not the target. - let mut balance = |address| { - evm.state_mut() - .account_info_untracked(&address) - .unwrap() - .map_or(U256::ZERO, |info| info.balance) - }; - assert_eq!(balance(replacement), U256::from(7)); - assert_eq!(balance(target), U256::ZERO); } #[test] - fn call_end_can_mutate_result_before_caller_observes_it() { - struct CallEndInspector; + fn call_hook_replacing_input_keeps_caller_memory() { + struct ReplaceInput; - impl Inspector for CallEndInspector { + impl Inspector for ReplaceInput { fn call( &mut self, - _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, message: &mut Message, ) -> Option> { - if message.depth == 0 { - return None; + if message.depth > 0 { + let mut input = message.input.to_bytes(interp.memory()).to_vec(); + input[..16].fill(0xcc); + message.input = Bytes::from(input).into(); } - Some(MessageResultExt { - stop: InstrStop::Revert, - gas: GasTracker::new(message.gas_limit), - ..Default::default() - }) + None } fn call_end( &mut self, - _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, message: &Message, - result: &mut MessageResult, + _result: &mut MessageResult, ) { if message.depth > 0 { - result.stop = InstrStop::Return; - result.output = Bytes::from_static(&[0xaa, 0xbb]); + assert_eq!(interp.memory().slice(0, 32), [0xaa; 32]); + assert_eq!(message.input.as_owned().unwrap()[..16], [0xcc; 16]); } } } - let target = Address::from([0x22; 20]); - let mut code = call_code(target); - code.extend([op::CALL, op::POP, op::RETURNDATASIZE]); - return_top_word(&mut code); + for target in [ECHO, Address::with_last_byte(4)] { + let (result, _, _) = run_evm_with_inspector_db( + echo_db(), + memory_input_call_code(target, op::CALL), + &MessageExt::default(), + 100_000, + ReplaceInput, + ); - let (result, _, _) = - run_evm_with_inspector(code, &MessageExt::default(), 50_000, CallEndInspector); + assert_matches!(result.stop, InstrStop::Return); + assert_eq!(result.output[..16], [0xcc; 16], "{target}"); + assert_eq!(result.output[16..], [0xaa; 16], "{target}"); + } + } + + #[test] + fn call_with_hook_overwriting_caller_memory_changes_child_input() { + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_config::(), + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + echo_db(), + Precompiles::base(SpecId::OSAKA), + ); + let code = memory_input_call_code(ECHO, CUSTOM_CALL); + let mut message = + MessageExt { gas_limit: 100_000, code: legacy_bytecode(code), ..Default::default() }; + + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_matches!(result.stop, InstrStop::Return); - // `call_end` upgraded the override from a revert to a 2-byte return. - assert_eq!(Word::from_be_slice(&result.output), Word::from(2)); + assert_eq!(result.output[..], [0xbb; 32]); } #[test] @@ -965,7 +2081,7 @@ mod tests { #[test] fn create_inspector_override_skips_execution_and_still_calls_end() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let inspector = OverrideCreateInspector { created, create_depth: None, create_end_stop: None }; let mut code = create_code(); @@ -983,7 +2099,7 @@ mod tests { #[test] fn create_inspector_sees_derived_destination() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let expected = contract.create(0); let mut code = create_code(); code.extend([op::CREATE, op::STOP]); @@ -1001,10 +2117,10 @@ mod tests { #[test] fn amsterdam_create_preaccess_failure_does_not_fire_create_hook() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); for is_create2 in [false, true] { for (balance, nonce, value, depth) in [ - (Word::from(1), 0, Word::from(2), 0), + (Word::ONE, 0, Word::from(2), 0), (Word::MAX, u64::MAX, Word::ZERO, 0), (Word::MAX, 0, Word::ZERO, CALL_DEPTH_LIMIT), ] { @@ -1041,7 +2157,7 @@ mod tests { #[test] fn create_inspector_override_wins_at_max_depth() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let inspector = OverrideCreateInspector { created, create_depth: None, create_end_stop: None }; let mut code = create_code(); @@ -1091,7 +2207,7 @@ mod tests { } } - let created = Address::from([0x88; 20]); + let created = Address::repeat_byte(0x88); let mut code = create_code(); code.extend([op::CREATE]); return_top_word(&mut code); @@ -1109,7 +2225,7 @@ mod tests { #[test] fn log_opcode_is_inspected_and_emitted_to_host() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let code = Vec::from([op::PUSH1, 0, op::PUSH1, 0, op::LOG0, op::STOP]); let (result, inspector, evm) = run_evm_with_inspector( @@ -1170,8 +2286,8 @@ mod tests { #[test] fn selfdestruct_is_inspected_from_opcode() { - let contract = Address::from([0x11; 20]); - let target = Address::from([0x99; 20]); + let contract = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x99); let value = Word::from(0xbeef); let mut db = InMemoryDB::default(); db.insert_account_info(&contract, AccountInfo::default().with_balance(value)); @@ -1193,8 +2309,8 @@ mod tests { #[test] fn selfdestruct_dynamic_gas_oog_is_not_inspected() { - let contract = Address::from([0x11; 20]); - let target = Address::from([0x99; 20]); + let contract = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x99); let mut db = InMemoryDB::default(); db.insert_account_info(&contract, AccountInfo::default()); let mut code = Vec::new(); @@ -1216,7 +2332,7 @@ mod tests { #[test] fn selfdestruct_host_error_is_not_inspected() { // Host failures are injected through the mock host; this intentionally uses [`TestHost`]. - let target = Address::from([0x99; 20]); + let target = Address::repeat_byte(0x99); let mut host = TestHost { selfdestruct_error: Some( DatabaseError::new(crate::AnyError::from("selfdestruct failed"), true).into(), @@ -1228,13 +2344,12 @@ mod tests { push(&mut code, address_to_word(&target)); code.push(op::SELFDESTRUCT); - let tx_env = TxEnvExt::default(); let message = Message:: { gas_limit: 10_000, code: legacy_bytecode(code), ..Default::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); let stop = interp.run_inspect(&mut host, &mut inspector); assert_matches!(stop, Err(crate::ExecutionError::Database(error)) if error.is_fatal()); @@ -1243,8 +2358,8 @@ mod tests { #[test] fn evm_transaction_inspects_interpreter_steps_and_logs() { - let caller = Address::from([0xaa; 20]); - let contract = Address::from([0xbb; 20]); + let caller = Address::repeat_byte(0xaa); + let contract = Address::repeat_byte(0xbb); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::PUSH1, 0, @@ -1280,7 +2395,7 @@ mod tests { let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); let state = &inspector.state; - assert!(result.status); + assert!(result.is_success()); assert_eq!(state.initialized, 1); assert_eq!(state.steps, 4); assert_eq!(state.step_ends, 4); @@ -1292,8 +2407,8 @@ mod tests { #[test] fn evm_transaction_inspects_eip7708_transfer_log() { - let caller = Address::from([0xaa; 20]); - let target = Address::from([0xbb; 20]); + let caller = Address::repeat_byte(0xaa); + let target = Address::repeat_byte(0xbb); let mut database = InMemoryDB::default(); database.insert_account_info( &caller, @@ -1321,7 +2436,7 @@ mod tests { let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); let state = &inspector.state; - assert!(result.result.status); + assert!(result.result.is_success()); assert_eq!(result.result.logs.len(), 1); assert_eq!(state.logs, result.result.logs); assert_eq!(state.logs[0].address, SYSTEM_ADDRESS); @@ -1329,7 +2444,7 @@ mod tests { #[test] fn evm_create_transaction_initializes_interpreter_with_create_hook() { - let caller = Address::from([0xaa; 20]); + let caller = Address::repeat_byte(0xaa); let mut database = InMemoryDB::default(); database.insert_account_info( &caller, @@ -1357,7 +2472,7 @@ mod tests { let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); let state = &inspector.state; - assert!(result.status); + assert!(result.is_success()); assert_eq!(state.initialized, 1); assert_eq!(state.steps, 1); assert_eq!(state.step_ends, 1); @@ -1402,8 +2517,8 @@ mod tests { } } - let caller = Address::from([0xaa; 20]); - let target = Address::from([0xbb; 20]); + let caller = Address::repeat_byte(0xaa); + let target = Address::repeat_byte(0xbb); // CALL(gas, 0x44, 0, 0, 0, 0, 0), then STOP. let code = vec![ op::PUSH0, @@ -1449,7 +2564,7 @@ mod tests { ); let result = evm.transact(&tx).expect("transaction should execute").discard(); - assert!(result.status); + assert!(result.is_success()); let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); let (before_hook, caller_balance, target_exists) = inspector.observed.unwrap(); // The value transfer, and for a create the account creation, follow the checkpoint. @@ -1499,10 +2614,20 @@ mod tests { } } - let caller = Address::from([0xaa; 20]); - let target = Address::from([0xbb; 20]); - // CREATE(0, 0, 0), then STOP. - let code = vec![op::PUSH0, op::PUSH0, op::PUSH0, op::CREATE, op::STOP]; + let caller = Address::repeat_byte(0xaa); + let target = Address::repeat_byte(0xbb); + // MSTORE8(0, 0), CREATE(0, 0, 1) with a one byte STOP initcode, then STOP. + let code = vec![ + op::PUSH0, + op::PUSH0, + op::MSTORE8, + op::PUSH1, + 1, + op::PUSH0, + op::PUSH0, + op::CREATE, + op::STOP, + ]; let mut database = InMemoryDB::default(); database.insert_account_info( &caller, @@ -1530,7 +2655,7 @@ mod tests { ); let result = evm.transact(&tx).expect("transaction should execute").discard(); - assert!(result.status); + assert!(result.is_success()); let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); // The create frame's checkpoint follows the creator's nonce bump and precedes the // account creation. @@ -1539,6 +2664,88 @@ mod tests { assert_eq!(inspector.restored, Some(true)); } + #[test] + fn runtime_oog_first_frame_fires_call_hooks() { + #[derive(Default)] + struct HaltedFrameInspector { + calls: Vec<(u16, Address, u64)>, + ends: Vec<(InstrStop, u64)>, + } + impl Inspector for HaltedFrameInspector { + fn call( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + assert!(message.code.is_empty()); + self.calls.push((message.depth, message.destination, message.gas_limit)); + // A returned result must not replace the halt. + Some(MessageResultExt::default()) + } + + fn call_end( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + _message: &Message, + result: &mut MessageResult, + ) { + self.ends.push((result.stop, result.gas.spent())); + } + } + + let key = k256::ecdsa::SigningKey::from_slice(&[0x11; 32]).unwrap(); + let authorization = alloy_eips::eip7702::Authorization { + chain_id: U256::from(1), + address: Address::ZERO, + nonce: 0, + }; + let (signature, recovery_id) = + key.sign_prehash_recoverable(authorization.signature_hash().as_slice()); + let authorization = alloy_eips::eip7702::SignedAuthorization::new_unchecked( + authorization, + recovery_id.to_byte(), + U256::from_be_slice(&signature.r().to_bytes()), + U256::from_be_slice(&signature.s().to_bytes()), + ); + let caller = Address::from([0xaa; 20]); + let target = Address::from([0xbb; 20]); + let mut database = InMemoryDB::default(); + database.insert_account_info( + &caller, + AccountInfo::default().with_balance(U256::from(1_000_000_000_u64)), + ); + let mut evm = Evm::::new( + SpecId::AMSTERDAM, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::AMSTERDAM), + database, + Precompiles::base(SpecId::AMSTERDAM), + ); + evm.set_inspector(HaltedFrameInspector::default()); + // The gas covers the intrinsic cost but not the new authority's state gas. + let tx = Recovered::new_unchecked( + TxEnvelope::from(alloy_consensus::TxEip7702 { + chain_id: 1, + gas_limit: 60_000, + to: target, + authorization_list: vec![authorization], + ..Default::default() + }), + caller, + ); + + let result = evm.transact(&tx).expect("transaction should execute").discard(); + assert!(!result.status); + assert_eq!(result.stop, InstrStop::OutOfGas); + let inspector = evm.inspector().unwrap().downcast_ref::().unwrap(); + let [(0, destination, gas_limit)] = inspector.calls[..] else { + panic!("expected one depth-0 call hook, got {:?}", inspector.calls) + }; + assert_eq!(destination, target); + // The halt burns the whole budget the frame was given. + assert_eq!(inspector.ends, [(InstrStop::OutOfGas, gas_limit)]); + } + #[test] fn cooling_loaded_slot_respects_access_list() { struct CoolSlotInspector { @@ -1617,8 +2824,7 @@ mod tests { if marker == Address::with_last_byte(0x44) { self.snapshot = Some(interp.host().state().clone_with(EmptyDB::default())); } else if marker == Address::with_last_byte(0x55) { - *interp.host().state_mut() = - self.snapshot.as_ref().unwrap().clone_with(EmptyDB::default()); + *interp.host().state_mut() = self.snapshot.as_ref().unwrap().clone(); self.restored = true; } else { return None; @@ -1773,68 +2979,6 @@ mod tests { ); } - #[test] - fn isolated_transaction_resets_transient_state_and_merges_storage() { - let target = Address::with_last_byte(0xbb); - let caller = Address::with_last_byte(0x99); - for reverts in [false, true] { - let mut parent = State::new(EmptyDB::default()); - // Return the initial persistent and transient values, then write persistent storage. - let code = vec![ - op::PUSH0, - op::SLOAD, - op::PUSH0, - op::MSTORE, - op::PUSH0, - op::TLOAD, - op::PUSH1, - 32, - op::MSTORE, - op::PUSH1, - 8, - op::PUSH0, - op::SSTORE, - op::PUSH1, - 64, - op::PUSH0, - if reverts { op::REVERT } else { op::RETURN }, - ]; - parent.account(&target).unwrap().set_code_slow(legacy_bytecode(code)); - parent.account(&caller).unwrap().set_balance(Word::from(1_000_000_000)); - parent.storage_slot(&target, Word::ZERO).unwrap().set(Word::from(7)); - parent.storage_slot(&target, Word::ZERO).unwrap().warm(); - parent.tstore(&target, &Word::ZERO, &Word::from(9)); - let checkpoint = parent.checkpoint(); - let mut child = Evm::::new( - SpecId::CANCUN, - BlockEnvExt::default(), - ethereum_tx_registry(SpecId::CANCUN), - EmptyDB::default(), - Precompiles::base(SpecId::CANCUN), - ); - child.state_mut().set_pending_state(parent.prepare_isolated_state()); - let tx = Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { - to: TxKind::Call(target), - gas_limit: 100_000, - ..Default::default() - }), - caller, - ); - let output = child.transact(&tx).unwrap().detach(); - assert_eq!(output.result.status, !reverts); - assert_eq!(Word::from_be_slice(&output.result.output[..32]), Word::from(7)); - assert_eq!(Word::from_be_slice(&output.result.output[32..]), Word::ZERO); - parent.merge_isolated_state(output.pending_state); - assert_eq!(parent.checkpoint(), checkpoint); - let slot = parent.storage_slot(&target, Word::ZERO).unwrap(); - assert_eq!(slot.original(), Word::ZERO); - assert_eq!(slot.current(), Word::from(if reverts { 7 } else { 8 })); - assert!(slot.is_warm()); - assert_eq!(parent.tload(&target, &Word::ZERO), Word::from(9)); - } - } - #[test] fn inspector_gas_changes_survive_dispatch_errors() { struct GasEdit; @@ -1848,17 +2992,136 @@ mod tests { (Vec::from([op::PUSH1, 0]), 2, InstrStop::OutOfGas), (Vec::from([op::INVALID]), 10_000, InstrStop::InvalidFEOpcode), ] { - let tx_env = TxEnvExt::default(); let message = Message:: { gas_limit, code: legacy_bytecode(code), ..Default::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); let stop = interp.run_inspect(&mut TestHost::default(), &mut GasEdit); assert_eq!(stop, Ok(expected)); assert_eq!(interp.gas().remaining(), 1000); } } + + #[test] + fn tuple_inspector_forwards_hooks_and_prefers_first_override() { + let target = Address::repeat_byte(0x22); + let mut code = call_code(target); + code.extend([op::CALL, op::POP, op::RETURNDATASIZE]); + return_top_word(&mut code); + let override_inspector = |output: &'static [u8]| OverrideCallInspector { + result: MessageResultExt { + stop: InstrStop::Return, + output: Bytes::from_static(output), + ..Default::default() + }, + min_depth: 1, + call_depth: None, + call_end_stop: None, + }; + + let (result, inspector, _) = run_evm_with_inspector( + code.clone(), + &MessageExt::default(), + 50_000, + (HookInspector::default(), override_inspector(&[0xaa, 0xbb, 0xcc])), + ); + assert_matches!(result.stop, InstrStop::Return); + assert_eq!(Word::from_be_slice(&result.output), Word::from(3)); + assert_eq!(inspector.0.call_depths, [0, 1]); + assert_eq!(inspector.0.call_end_stops, [InstrStop::Return, InstrStop::Return]); + assert_eq!(inspector.1.call_depth, Some(1)); + assert_eq!(inspector.1.call_end_stop, Some(InstrStop::Return)); + + let (result, ..) = run_evm_with_inspector( + code, + &MessageExt::default(), + 50_000, + (override_inspector(&[0xaa]), override_inspector(&[0xaa, 0xbb, 0xcc])), + ); + assert_eq!(Word::from_be_slice(&result.output), Word::ONE); + } + + #[test] + fn option_inspector_forwards_hooks_only_when_present() { + let target = Address::repeat_byte(0x22); + let mut code = call_code(target); + code.extend([op::CALL, op::STOP]); + + let (_, inspector, _) = run_evm_with_inspector( + code.clone(), + &MessageExt::default(), + 50_000, + Some(HookInspector::default()), + ); + let inspector = inspector.unwrap(); + assert_eq!(inspector.call_depths, [0, 1]); + assert_eq!(inspector.call_end_stops, [InstrStop::Stop, InstrStop::Stop]); + + let (result, inspector, _) = + run_evm_with_inspector(code, &MessageExt::default(), 50_000, None::); + assert_matches!(result.stop, InstrStop::Stop); + assert!(inspector.is_none()); + } + + #[test] + fn tuple_and_option_inspectors_step_the_union_of_their_sets() { + let (db, code) = config_test_code(); + let (expected, expected_logs) = run_evm_uninspected(db.clone(), code.clone(), 1_000_000); + let config = |inspector: &dyn Inspector| inspector.inspector_config().steps(); + let stepped = |inspector: &ConfigInspector| { + let mut opcodes = inspector.steps.iter().map(|step| step.0).collect::>(); + opcodes.sort_unstable(); + opcodes.dedup(); + opcodes + }; + let sload = || ConfigInspector::new(opcode_set([op::SLOAD])); + let call = || ConfigInspector::new(opcode_set([op::CALL])); + + assert_eq!(config(&(sload(), call())), opcode_set([op::SLOAD, op::CALL])); + assert_eq!( + config(&(sload(), ConfigInspector::new(OpcodeSet::EMPTY))), + opcode_set([op::SLOAD]) + ); + assert_eq!(config(&(sload(), NoopInspector::default())), opcode_set([op::SLOAD])); + assert_eq!(config(&(sload(), HookInspector::default())), OpcodeSet::ALL); + assert_eq!(config(&Some(sload())), opcode_set([op::SLOAD])); + assert_eq!(config(&None::), OpcodeSet::EMPTY); + + let (result, inspector, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + (sload(), call()), + ); + assert_eq!(result, expected); + assert_eq!(inspector.0.logs, expected_logs); + // Each member also sees the steps of the other, and both see every message. + assert_eq!(stepped(&inspector.0), [op::SLOAD, op::CALL]); + assert_eq!(inspector.0.steps, inspector.1.steps); + assert_eq!((inspector.0.calls, inspector.1.calls), (2, 2)); + + let (result, inspector, _) = run_evm_with_inspector_db( + db.clone(), + code.clone(), + &MessageExt::default(), + 1_000_000, + Some(sload()), + ); + assert_eq!(result, expected); + assert_eq!(stepped(&inspector.unwrap()), [op::SLOAD]); + + let (result, inspector, _) = run_evm_with_inspector_db( + db, + code, + &MessageExt::default(), + 1_000_000, + None::, + ); + assert_eq!(result, expected); + assert!(inspector.is_none()); + } } diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 416ef289b..b41e8f9d6 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -34,9 +34,9 @@ //! 1. **Accepted overlay**: transaction-boundary state accepted by prior commits. It shadows the //! wrapped database and is visible to later transactions executed by the same [`Evm`]. //! 2. **Transaction scratch**: writes, warm-access state, transient storage, journal entries, -//! touched accounts, selfdestruct markers, and logs for the currently executing transaction. It -//! is cleared after `commit`, `commit_to`, `commit_with`, `discard`, `discard_with`, or `detach` -//! while retaining capacity where possible. +//! touched and selfdestructed account flags, and logs for the currently executing transaction. +//! It is cleared after `commit`, `commit_to`, `commit_with`, `discard`, `discard_with`, or +//! `detach` while retaining capacity where possible. //! 3. **Block accumulator**: optional block-level state output. It coalesces committed transaction //! writes and keeps block-boundary originals. //! @@ -120,11 +120,11 @@ use crate::{ DatabaseError, EvmConfigSelector, EvmTypes, EvmTypesHost, ExecutionConfig, ExecutionError, HostError, LoadError, PrecompileError, PrecompileHalt, SpecId, bytecode::Bytecode, - constants::EIP7708_TRANSFER_TOPIC, + constants::{CALL_DEPTH_LIMIT, EIP7708_TRANSFER_TOPIC}, env::{BlockEnv, TxEnv}, interpreter::{ - Gas, GasTracker, Host, InstrStop, Interpreter, InterpreterPool, Message, MessageKind, - MessageResult, MessageResultExt, Word, + Gas, GasTracker, Host, InstrStop, Interpreter, InterpreterPool, Memory, Message, + MessageKind, MessageResult, MessageResultExt, Word, create_preaccess_failure, }, registry::{HandlerResult, TxRegistry}, trustme, @@ -133,10 +133,10 @@ use crate::{ use alloc::{borrow::Cow, boxed::Box, sync::Arc, vec, vec::Vec}; use alloy_consensus::transaction::Recovered; use alloy_eips::eip2718::Typed2718; -use alloy_primitives::{Address, B256, Bytes, Log, LogData}; -#[cfg(feature = "async")] -use core::future::Future; +use alloy_primitives::{Address, B256, Bytes, Log}; use core::ptr::NonNull; +#[cfg(feature = "async")] +use core::{any::TypeId, future::Future}; use derive_where::derive_where; #[cfg(feature = "async")] @@ -195,11 +195,22 @@ pub trait InterpreterRunner: core::fmt::Debug + Send + Sync + ' /// /// Record execution errors with [`Interpreter::fail`]; the EVM extracts them after this /// method returns. + /// + /// The EVM calls this only while [`Self::supports`] holds for the active execution config; + /// other callers must check it first. fn run<'frame, 'host>( &self, interpreter: &mut Interpreter<'frame, 'host, T>, host: &mut T::Host<'host>, ) -> Option; + + /// Returns whether this runner can execute frames under `config`. + /// + /// The EVM calls this when the runner or the execution config changes, not per frame, and + /// skips the runner while it returns `false`. + fn supports(&self, _config: &ExecutionConfig) -> bool { + true + } } /// EVM host and transaction dispatcher. @@ -211,6 +222,7 @@ pub struct Evm<'a, T: EvmTypesHost> { execution_config: ExecutionConfig, features: EvmFeatures, pub(crate) block: BlockEnv, + tx_env: TxEnv, registry: TxRegistry>, #[derive_where(skip)] pub(crate) state: State<'a>, @@ -223,13 +235,22 @@ pub struct Evm<'a, T: EvmTypesHost> { #[derive_where(skip)] inspector: Option>, inspect_system_calls: bool, + initial_call_depth: u16, #[derive_where(skip)] interpreter_runner: Option>>, + /// Whether `interpreter_runner` is set and supports `execution_config`. + /// + /// Refreshed whenever either changes. + #[derive_where(skip)] + runner_supported: bool, /// The currently running interpreter frame, if any. /// /// This is passed to the inspector call and create hooks as the parent frame. #[derive_where(skip)] current_frame: Option>>, + /// The number of frames this EVM has entered, which numbers the frame it enters next. + #[derive_where(skip)] + frames_entered: u64, /// The checkpoint the currently running interpreter frame rolls back to if it fails. #[derive_where(skip)] frame_checkpoint: Option, @@ -238,7 +259,11 @@ pub struct Evm<'a, T: EvmTypesHost> { #[cfg(feature = "async")] #[derive_where(skip)] async_stack: r#async::FiberStack, - evm_send: bool, + /// The erased value types last verified as `Send` by [`Evm::evm_is_send`] or + /// [`Evm::evm_is_send_with_inspector`]. + #[cfg(feature = "async")] + #[derive_where(skip)] + send_types: Option, } impl<'a, T: EvmTypes> Evm<'a, T> { @@ -345,6 +370,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { features: execution_config.version().features, execution_config, block, + tx_env: TxEnv::::default(), registry, state: State::new_mono(database), ext, @@ -352,13 +378,17 @@ impl<'a, T: EvmTypes> Evm<'a, T> { interpreter_pool: InterpreterPool::new(), inspector: None, inspect_system_calls: false, + initial_call_depth: 0, interpreter_runner: None, + runner_supported: false, current_frame: None, + frames_entered: 0, frame_checkpoint: None, running: false, #[cfg(feature = "async")] async_stack: r#async::FiberStack::default(), - evm_send: false, + #[cfg(feature = "async")] + send_types: None, } } @@ -412,6 +442,14 @@ impl<'a, T: EvmTypes> Evm<'a, T> { assert!(!self.running, "execution config cannot be modified during EVM execution"); } + #[inline] + fn refresh_runner_support(&mut self) { + self.runner_supported = self + .interpreter_runner + .as_ref() + .is_some_and(|runner| runner.supports(&self.execution_config)); + } + #[inline] const fn enter_execution(&mut self) -> ExecutionGuard<'_, 'a, T> { let was_running = self.running; @@ -480,6 +518,27 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.block = block; } + /// Returns the active transaction environment. + /// + /// Transaction handlers and system calls replace it before executing their initial frame, and + /// it keeps the last transaction's values afterwards. + #[inline] + pub const fn tx_env(&self) -> &TxEnv { + &self.tx_env + } + + /// Returns the active transaction environment mutably, including during execution. + /// + /// The current frame and nested frames observe changes on subsequent reads, such as `ORIGIN` + /// or `GASPRICE`. Validation, fees, and nonces do not read it, so an inspector can override, + /// for example, the origin of a running transaction from its top-level call or create hook + /// without affecting how the transaction is charged. Handlers overwrite it when the next + /// transaction starts executing. + #[inline] + pub const fn tx_env_mut(&mut self) -> &mut TxEnv { + &mut self.tx_env + } + /// Replaces the active execution specification and its fork-dependent dispatch state. /// /// `execution_config` must have the same base specification as `spec_id`. The transaction @@ -539,7 +598,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { "execution config spec mismatch" ); let precompiles = boxed_precompile_provider(precompiles); - self.block = block; + self.set_block(block); self.replace_execution_config(execution_config, spec_id, registry, precompiles); } @@ -568,9 +627,10 @@ impl<'a, T: EvmTypes> Evm<'a, T> { execution_config.base_spec_id(), "execution config spec mismatch" ); - self.block = block; + self.set_block(block); self.features = execution_config.version().features; self.execution_config = execution_config; + self.refresh_runner_support(); } #[inline] @@ -584,9 +644,11 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.spec_id = spec_id; self.features = execution_config.version().features; self.execution_config = execution_config; + // Refresh before the old registry and precompiles drop, so an unwinding destructor + // cannot leave a stale verdict. + self.refresh_runner_support(); self.registry = registry; self.precompiles = precompiles; - self.evm_send = false; } /// Returns the accepted-state overlay database. @@ -606,7 +668,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// available through [`Self::database_mut`]. #[inline] pub fn overlay_db_mut(&mut self) -> &mut CacheDB> { - self.evm_send = false; self.state.overlay_db_mut() } @@ -638,20 +699,19 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline] pub fn set_database(&mut self, database: impl DynDatabase + 'a) { self.state.set_initial(database); - self.evm_send = false; } #[cfg(feature = "async")] #[inline] - fn async_stack(&mut self) -> core::ptr::NonNull { - core::ptr::NonNull::from(&mut self.async_stack) + fn async_stack(&mut self) -> NonNull { + NonNull::from(&mut self.async_stack) } #[cfg(feature = "async")] #[inline] fn assert_erased_send(&self) { assert!( - self.evm_send, + self.send_types == Some(self.erased_type_ids()), "async EVM execution requires EVM erased fields to be verified as Send with \ Evm::evm_is_send" ); @@ -661,10 +721,24 @@ impl<'a, T: EvmTypes> Evm<'a, T> { ); } + #[cfg(feature = "async")] + #[inline] + fn erased_type_ids(&self) -> ErasedTypeIds { + ErasedTypeIds { + database: self.database().type_id(), + precompiles: self.precompiles().type_id(), + inspector: self.inspector().map(|inspector| inspector.type_id()), + } + } + /// Marks this EVM as thread-sendable after checking the current erased field types. /// /// This requires no active inspector. Use [`Self::evm_is_send_with_inspector`] when an /// inspector is installed. + /// + /// Async execution re-checks that the erased types are still the verified ones, so replacing + /// one with a different type makes it panic until the new types are verified. + #[cfg(feature = "async")] #[inline] pub fn evm_is_send(&mut self) -> &mut Self where @@ -675,11 +749,12 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.assert_database_type::(); self.assert_precompiles_type::

(); assert!(self.inspector.is_none(), "inspector type mismatch"); - self.evm_send = true; + self.send_types = Some(self.erased_type_ids()); self } /// Marks this EVM as thread-sendable after checking the current erased field types. + #[cfg(feature = "async")] #[inline] pub fn evm_is_send_with_inspector(&mut self) -> &mut Self where @@ -691,10 +766,11 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.assert_database_type::(); self.assert_precompiles_type::

(); self.assert_inspector_type::(); - self.evm_send = true; + self.send_types = Some(self.erased_type_ids()); self } + #[cfg(feature = "async")] #[inline] fn assert_database_type(&self) where @@ -703,6 +779,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { assert_eq!(self.database().type_id(), typeid::of::(), "database type mismatch"); } + #[cfg(feature = "async")] #[inline] fn assert_precompiles_type + 'static>(&self) where @@ -715,6 +792,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { ); } + #[cfg(feature = "async")] #[inline] fn assert_inspector_type + 'static>(&self) where @@ -775,7 +853,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// Returns the mutable EVM state. #[inline] pub const fn state_mut(&mut self) -> &mut State<'a> { - self.evm_send = false; &mut self.state } @@ -786,9 +863,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { } /// Returns the logs emitted by the current in-flight transaction mutably. - /// - /// Unlike [`Self::state_mut`], this keeps the EVM verified as `Send`, since logs are not - /// erased. #[inline] pub const fn logs_mut(&mut self) -> &mut Vec { self.state.logs_mut() @@ -820,9 +894,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// Replaces the precompile provider. #[inline] pub fn set_precompiles(&mut self, precompiles: impl PrecompileProvider + 'a) { - self.assert_precompiles_mutable(); - self.precompiles = boxed_precompile_provider(precompiles); - self.evm_send = false; + self.set_boxed_precompiles(boxed_precompile_provider(precompiles)); } /// Replaces the precompile provider with an already boxed one. @@ -830,7 +902,6 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn set_boxed_precompiles(&mut self, precompiles: Box + 'a>) { self.assert_precompiles_mutable(); self.precompiles = precompiles; - self.evm_send = false; } /// Returns the precompile provider as `P` if it has that concrete type. @@ -936,9 +1007,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// Sets the active execution inspector. #[inline] pub fn set_inspector + 'a>(&mut self, inspector: I) { - self.assert_inspector_mutable(); - self.inspector = Some(InspectorSlot::Owned(boxed_inspector(inspector))); - self.evm_send = false; + self.set_boxed_inspector(boxed_inspector(inspector)); } /// Sets the active boxed execution inspector. @@ -946,14 +1015,12 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn set_boxed_inspector(&mut self, inspector: Box + 'a>) { self.assert_inspector_mutable(); self.inspector = Some(InspectorSlot::Owned(inspector)); - self.evm_send = false; } /// Removes the active execution inspector. #[inline] pub fn clear_inspector(&mut self) -> Option + 'a>> { self.assert_inspector_mutable(); - self.evm_send = false; match self.inspector.take()? { InspectorSlot::Owned(inspector) => Some(inspector), InspectorSlot::Borrowed(_) => unreachable!("borrowed inspector outlived its call"), @@ -989,11 +1056,58 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.inspect_system_calls = inspect; } + /// Returns the inspector-visible depth of top-level messages. Defaults to zero. + /// + /// The call depth limit and the nested-create nonce check are measured from this depth, so a + /// top-level message started at a non-zero depth still gets the full [`CALL_DEPTH_LIMIT`] + /// budget below it. Inspectors compare against it to find the top-level message. + #[inline] + pub const fn initial_call_depth(&self) -> u16 { + self.initial_call_depth + } + + /// Sets [`Self::initial_call_depth`] for top-level messages started afterwards. + /// + /// A caller can set it before running a nested transaction, such as an isolated call of a + /// test framework that keeps its outer depth, and a transaction handler can set it around the + /// messages it runs below an implicit parent, such as the calls of a batch transaction. The + /// value stays until it is set again, so a handler that changes it should restore it before + /// returning. + /// + /// It must not be called while a message is executing, such as from an inspector hook or a + /// precompile: the running message would then be checked against the new depth. As with + /// [`Self::set_execution_config`], only a running interpreter frame is detected. + /// + /// # Panics + /// + /// Panics if called from inside a running interpreter frame, or if `depth` does not leave room + /// in `u16` for the call depth budget and the rejected too-deep message: it must be at most + /// `u16::MAX - CALL_DEPTH_LIMIT - 1` (64510). + #[inline] + pub fn set_initial_call_depth(&mut self, depth: u16) { + assert!( + self.current_frame.is_none(), + "initial call depth cannot be modified while a frame is running" + ); + assert!( + depth < u16::MAX - CALL_DEPTH_LIMIT, + "initial call depth must leave room for 1024 descendants and the rejected attempt" + ); + self.initial_call_depth = depth; + } + + /// Returns the deepest permitted inspector-visible call depth. + /// + /// [`Self::set_initial_call_depth`] keeps this from overflowing. + #[inline] + const fn max_call_depth(&self) -> u16 { + self.initial_call_depth + CALL_DEPTH_LIMIT + } + /// Sets the optional external interpreter runner. #[inline] pub fn set_interpreter_runner>(&mut self, runner: R) { - self.assert_interpreter_runner_mutable(); - self.interpreter_runner = Some(Arc::new(runner)); + self.set_shared_interpreter_runner(Arc::new(runner)); } /// Sets the optional shared external interpreter runner. @@ -1001,13 +1115,16 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn set_shared_interpreter_runner(&mut self, runner: Arc>) { self.assert_interpreter_runner_mutable(); self.interpreter_runner = Some(runner); + self.refresh_runner_support(); } /// Removes the optional external interpreter runner. #[inline] pub fn clear_interpreter_runner(&mut self) -> Option>> { self.assert_interpreter_runner_mutable(); - self.interpreter_runner.take() + let runner = self.interpreter_runner.take(); + self.refresh_runner_support(); + runner } /// Returns the active EVM version. @@ -1108,14 +1225,24 @@ impl<'guard, 'evm, T: EvmTypesHost> Drop for ExecutionGuard<'guard, 'evm, T> { } } +/// The concrete types of the erased database, precompile provider, and inspector. +#[cfg(feature = "async")] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct ErasedTypeIds { + database: TypeId, + precompiles: TypeId, + inspector: Option, +} + #[cfg(feature = "async")] struct SendEvmRef<'a, 'evm, T: EvmTypesHost> { evm: &'a mut Evm<'evm, T>, } #[cfg(feature = "async")] -// SAFETY: `SendEvmRef` is only constructed by async entrypoints after `Evm::evm_is_send` has -// verified the concrete erased field types as `Send`. +// SAFETY: `SendEvmRef` is only constructed by async entrypoints for an `Evm<'static, T>` whose +// erased field types `Evm::evm_is_send` verified as `Send`. The `'static` bound keeps a verified +// type from being replaced by a shorter-lived instance of itself, which `TypeId` cannot tell apart. unsafe impl Send for SendEvmRef<'_, '_, T> where T: EvmTypesHost, @@ -1246,6 +1373,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { + 'fut where T::Tx: Sync, + 'a: 'static, { self.assert_erased_send(); let stack = self.async_stack(); @@ -1283,17 +1411,14 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline] fn execute_message_impl( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError> { let mut result = match message.kind { - MessageKind::Create | MessageKind::Create2 => { - self.execute_create_message(tx_env, message) - } + MessageKind::Create | MessageKind::Create2 => self.execute_create_message(message), MessageKind::Call | MessageKind::CallCode | MessageKind::DelegateCall - | MessageKind::StaticCall => self.execute_call_message(tx_env, message), + | MessageKind::StaticCall => self.execute_call_message(message), }?; // Settle the returning frame's gas for its stop reason at this single exit, // rather than in each result builder, so every consumer (parent @@ -1306,23 +1431,28 @@ impl<'a, T: EvmTypes> Evm<'a, T> { /// /// This is invoked for every message when an inspector is installed; hook overrides skip /// execution entirely, including the call depth check. + /// + /// A `halted` result stands in for the execution: the hooks fire around it, and a hook + /// override does not replace it. #[inline(never)] fn execute_message_inspected<'frame>( &mut self, - tx_env: &'frame TxEnv, message: &'frame mut Message, + halted: Option>, ) -> Result, ExecutionError> { let guard = self.enter_execution(); let Some(inspector) = guard.evm.active_inspector() else { - return guard.evm.execute_message_impl(tx_env, message); + return match halted { + Some(result) => Ok(result), + None => guard.evm.execute_message_impl(message), + }; }; // SAFETY: The inspector is stored in `self`; the execution guard prevents inspector // replacement while the hooks are running. let inspector = unsafe { trustme::decouple_lt_mut(inspector) }; - // `destination` already holds the create's contract address (set when the message was // constructed), so the create hook observes it directly. - let is_create = matches!(message.kind, MessageKind::Create | MessageKind::Create2); + let is_create = message.kind.is_create(); // Keeps the top-level frame's bytecode alive when a hook replaces `message.code`. let top_code; @@ -1339,13 +1469,15 @@ impl<'a, T: EvmTypes> Evm<'a, T> { None => { // SAFETY: The message outlives the frame, which is returned to the pool below. let frame_message = unsafe { trustme::decouple_lt(&*message) }; - let frame = top_frame.insert(guard.evm.interpreter_pool.pop(tx_env, frame_message)); + let frame = top_frame.insert(guard.evm.interpreter_pool.pop(frame_message)); top_code = message.code.clone(); // SAFETY: `top_code` owns a clone of the code, so the frame's pointers into it // stay valid even if a hook replaces `message.code`. It is declared before // `top_frame` and dropped after the frame is returned to the pool, which resets // its bytecode borrow. frame.set_bytecode(unsafe { trustme::decouple_lt(&top_code) }); + // The frame only runs hooks, which can replace the message's input. + frame.set_input(&[]); frame.prepare_run(guard.evm); frame } @@ -1361,8 +1493,10 @@ impl<'a, T: EvmTypes> Evm<'a, T> { let mut result = if let Some(error) = frame.take_error() { Err(error) + } else if let Some(halted) = halted { + Ok(halted) } else { - inspected.map(Ok).unwrap_or_else(|| guard.evm.execute_message_impl(tx_env, message)) + inspected.map(Ok).unwrap_or_else(|| guard.evm.execute_message_impl(message)) }; if let Ok(result) = &mut result { @@ -1387,16 +1521,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn execute_create_message( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError> { - if message.depth > self.version().max_call_depth() { - return Ok(Self::error_message_result( - InstrStop::CallTooDeep, - message.gas_limit, - message.reservoir, - )); - } if let Err(error) = self.prepare_create_message(message) { return match error { HostError::Halt(stop) => { @@ -1415,31 +1541,42 @@ impl<'a, T: EvmTypes> Evm<'a, T> { HostError::Execution(error) => Err(error), }; } - message.code_address = message.destination; - let input = core::mem::take(&mut message.input); - let result = self.run_frame(tx_env, message, checkpoint.clone()); - message.input = input; - let stop = result?; - self.finish_create_message_run(checkpoint, &message.destination, stop) + // Empty initcode deploys an empty contract without executing the bytecode's padding STOP. + let (stop, gas, output) = if message.code.is_empty() { + ( + InstrStop::Stop, + Gas::new_with_execution_gas_and_reservoir(message.gas_limit, message.reservoir), + Bytes::new(), + ) + } else { + let input = core::mem::take(&mut message.input); + let result = self.run_frame(message, checkpoint.clone()); + message.input = input; + let stop = result?; + let interp = self.interpreter_pool.last_mut().unwrap(); + (stop, interp.gas(), Bytes::copy_from_slice(interp.output())) + }; + self.finish_create_message_run(checkpoint, &message.destination, stop, gas, output) } #[inline(never)] fn prepare_create_message(&mut self, message: &mut Message) -> Result<(), HostError> { - let nested = message.depth > self.version().initial_call_depth; - let info = if message.value > 0 || nested { - self.state.account_info_untracked(&message.caller).map_err(HostError::from)? + let nested = message.depth > self.initial_call_depth; + // A too-deep create fails on depth alone, without reading the creator. + let info = if message.depth <= self.max_call_depth() && (message.value > 0 || nested) { + self.state.account_info_untracked(&message.caller)? } else { None }; - - if message.value > 0 && info.as_ref().is_none_or(|info| info.balance < message.value) { - return Err(InstrStop::OutOfFunds.into()); - } - - // EIP-2681 caps account nonces at u64::MAX; CREATE/CREATE2 return zero instead of - // wrapping or saturating the creator nonce. - if nested && info.as_ref().is_some_and(|info| info.nonce == u64::MAX) { - return Err(InstrStop::Return.into()); + let (balance, nonce) = info.as_ref().map_or((Word::ZERO, 0), |i| (i.balance, i.nonce)); + if let Some(stop) = create_preaccess_failure( + balance, + nonce, + message.value, + message.depth, + self.initial_call_depth, + ) { + return Err(stop.into()); } // `destination` already holds the contract address (derived when the message was @@ -1456,9 +1593,12 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn create_message_account(&mut self, message: &Message) -> Result<(), HostError> { - self.state - .create_account(&message.caller, &message.destination, &message.value, self.features) - .map_err(HostError::from)??; + self.state.create_account( + &message.caller, + &message.destination, + &message.value, + self.features, + )??; self.log_eip7708_transfer(&message.caller, &message.destination, &message.value); Ok(()) @@ -1470,11 +1610,10 @@ impl<'a, T: EvmTypes> Evm<'a, T> { checkpoint: StateCheckpoint, address: &Address, stop: InstrStop, + mut gas: Gas, + mut output: Bytes, ) -> Result, ExecutionError> { - let interp = self.interpreter_pool.last_mut().unwrap(); - let mut gas = interp.gas(); - let output = if stop.is_success() { - let mut output = Bytes::copy_from_slice(interp.output()); + if stop.is_success() { if let Err(stop) = self.validate_create_output(&mut gas, &mut output) { self.state.rollback(checkpoint, self.features); return Ok(MessageResultExt { @@ -1495,12 +1634,9 @@ impl<'a, T: EvmTypes> Evm<'a, T> { self.state.rollback(checkpoint, self.features); return Err(code.into()); } - - output } else { self.state.rollback(checkpoint, self.features); - Bytes::copy_from_slice(interp.output()) - }; + } Ok(MessageResultExt { stop, @@ -1554,10 +1690,9 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn execute_call_message( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError> { - if message.depth > self.version().max_call_depth() { + if message.depth > self.max_call_depth() { return Ok(Self::error_message_result( InstrStop::CallTooDeep, message.gas_limit, @@ -1593,7 +1728,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { ..MessageResultExt::default() }); } - let stop = self.run_frame(tx_env, message, checkpoint.clone())?; + let stop = self.run_frame(message, checkpoint.clone())?; Ok(self.finish_call_message_run(checkpoint, stop)) } @@ -1678,12 +1813,11 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline] fn run_frame( &mut self, - tx_env: &TxEnv, message: &Message, checkpoint: StateCheckpoint, ) -> Result { let previous = self.frame_checkpoint.replace(checkpoint); - let stop = self.run_interpreter(tx_env, message); + let stop = self.run_interpreter(message); self.frame_checkpoint = previous; stop } @@ -1691,27 +1825,38 @@ impl<'a, T: EvmTypes> Evm<'a, T> { #[inline(never)] fn run_interpreter<'frame>( &mut self, - tx_env: &'frame TxEnv, message: &'frame Message, ) -> Result { let guard = self.enter_execution(); - let mut interp: Box> = - guard.evm.interpreter_pool.pop(tx_env, message); + let mut interp: Box> = guard.evm.interpreter_pool.pop(message); let interp_ref = interp.as_mut(); + if let Some((range, frame)) = message.input.borrowed_range() { + let memory = guard.evm.caller_memory(frame); + // SAFETY: The caller frame is suspended until this frame returns, and `current_frame` + // becomes this one below, so nothing can reach the caller's memory in between. + interp_ref + .set_input(unsafe { trustme::decouple_lt(&memory.as_slice()[range.clone()]) }); + } + interp_ref.lend_memory(); + guard.evm.frames_entered += 1; + interp_ref.set_frame(guard.evm.frames_entered); + // The initialize hook can send messages, whose hooks must get this frame and not the + // caller, whose memory the input slice points into. + let prev_frame = guard + .evm + .current_frame + .replace(NonNull::from(&mut *interp_ref).cast::>()); guard.evm.inspect_initialize_interp(interp_ref); let inspector = guard.evm.active_inspector().map(|inspector| { // SAFETY: The inspector is stored in `self` and remains alive for the duration of the // interpreter run. unsafe { trustme::decouple_lt_mut(inspector) } }); - let prev_frame = guard - .evm - .current_frame - .replace(NonNull::from(&mut *interp_ref).cast::>()); - let interpreter_runner = guard.evm.interpreter_runner.clone(); + let runner = + guard.evm.interpreter_runner.as_ref().filter(|_| guard.evm.runner_supported).cloned(); let stop = if let Some(inspector) = inspector { interp_ref.run_inspect(guard.evm, inspector) - } else if let Some(runner) = interpreter_runner + } else if let Some(runner) = runner && let Some(stop) = runner.run(interp_ref, guard.evm) { interp_ref.finish_run(stop) @@ -1734,6 +1879,61 @@ impl<'a, T: EvmTypes> Evm<'a, T> { inspector.initialize_interp(interp); } } + + /// Reports a message that halts without executing, such as a transaction's first frame whose + /// runtime gas phase ran out of gas. + /// + /// With an inspector installed, the call or create hook fires and the matching end hook + /// receives `result`, as for an executed frame; the hook cannot replace `result`. `result` + /// must already have its gas settled for its stop reason. + pub fn execute_halted_message( + &mut self, + message: &mut Message, + result: MessageResult, + ) -> Result, ExecutionError> { + if self.inspector.is_some() { + return self.execute_message_inspected(message, Some(result)); + } + Ok(result) + } +} + +impl Evm<'_, T> { + /// Returns the bytes of `message`'s input, reading a range of the caller's memory from the + /// running caller frame. + /// + /// The bytes borrow the EVM, so a precompile cannot send a message, whose hooks could change + /// the caller's memory, while it holds them. + /// + /// # Panics + /// + /// Panics if the input is a range of memory and no frame runs, the running frame is not the + /// frame that made the input, or the range is out of bounds of its memory. Each means that the + /// message did not come from a call instruction of the running frame: copy a borrowed input + /// with [`MessageInput::to_bytes`](crate::interpreter::MessageInput::to_bytes) before + /// forwarding it. + #[inline] + pub fn message_input<'m>(&'m self, message: &'m Message) -> &'m [u8] { + match message.input.borrowed_range() { + None => message.input.as_owned().map_or(&[][..], |input| &input[..]), + Some((range, frame)) => &self.caller_memory(frame).as_slice()[range.clone()], + } + } + + /// Returns the memory of the running frame, which must be the frame with the entry number + /// `frame`. + #[inline] + fn caller_memory(&self, frame: u64) -> &Memory { + let caller = self.current_frame.expect("a call input must come from a running frame"); + // SAFETY: The running frame is suspended on this call stack. + let caller = unsafe { caller.as_ref() }; + assert_eq!( + caller.frame(), + frame, + "a call input must be resolved against the frame that made it", + ); + caller.memory() + } } impl<'a, T: EvmTypes> Host for Evm<'a, T> { @@ -1749,6 +1949,14 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { &self.block } + fn tx_env(&mut self) -> &TxEnv { + &self.tx_env + } + + fn initial_call_depth(&self) -> u16 { + self.initial_call_depth + } + fn load_account( &mut self, address: &Address, @@ -1762,37 +1970,26 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { }; let is_cold = account.warm(); - let (balance, nonce, code_hash, exists, is_empty) = match account.get() { - Some(info) => (info.balance, info.nonce, info.code_hash, true, info.is_empty()), - None => (Word::ZERO, 0, B256::ZERO, false, true), + let is_empty_for_new_account_gas = account.is_empty_for_new_account_gas(self.features); + let (balance, nonce, code_hash, is_empty) = match account.get() { + Some(info) => (info.balance, info.nonce, info.code_hash, info.is_empty()), + None => (Word::ZERO, 0, B256::ZERO, true), }; // load code - let code = if load_code { - account.load_code().map_err(HostError::from)? - } else { - Bytecode::default() - }; + let code = if load_code { account.load_code()? } else { Bytecode::default() }; Ok(AccountLoad { balance, nonce, code_hash, code, - exists, is_empty, + is_empty_for_new_account_gas, is_cold, _non_exhaustive: (), }) } - fn target_is_empty_for_new_account_gas( - &mut self, - address: &Address, - features: EvmFeatures, - ) -> Result { - Ok(self.state.account(address)?.is_empty_for_new_account_gas(features)) - } - fn block_hash(&mut self, number: &Word) -> Result { self.state.block_hash(number) } @@ -1874,14 +2071,13 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { #[inline] fn execute_message( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError> { let checkpoint = self.state.checkpoint(); let result = if self.inspector.is_some() { - self.execute_message_inspected(tx_env, message) + self.execute_message_inspected(message, None) } else { - self.execute_message_impl(tx_env, message) + self.execute_message_impl(message) }; if result.is_err() { self.state.rollback(checkpoint, self.features); @@ -1896,19 +2092,12 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { skip_cold_load: bool, ) -> Result { let features = self.features; - let eip2929 = features.contains(EvmFeatures::EIP2929); let (is_cold, target_is_empty_for_new_account_gas) = - match self.state.account_with_skip(target, eip2929 && skip_cold_load) { - Ok(mut account) => { - let is_cold = account.warm() && eip2929; - (is_cold, account.is_empty_for_new_account_gas(features)) - } + match self.state.account_with_skip(target, skip_cold_load) { + Ok(mut account) => (account.warm(), account.is_empty_for_new_account_gas(features)), Err(LoadError::ColdLoadSkipped) => return Err(InstrStop::OutOfGas.into()), Err(code) => return Err(code.into()), }; - if skip_cold_load && is_cold { - return Err(InstrStop::OutOfGas.into()); - } let (previously_destroyed, balance, should_destroy) = { let account = self.state.account(contract)?; let should_destroy = !features.contains(EvmFeatures::EIP6780) || account.is_created(); @@ -1916,8 +2105,7 @@ impl<'a, T: EvmTypes> Host for Evm<'a, T> { }; if contract != target { - let transferred = - self.state.transfer(contract, target, &balance).map_err(HostError::from)?; + let transferred = self.state.transfer(contract, target, &balance)?; if transferred { self.log_eip7708_transfer(contract, target, &balance); } @@ -1953,10 +2141,11 @@ pub struct AccountLoad { pub code_hash: B256, /// Account bytecode. pub code: Bytecode, - /// Whether the account exists in state. - pub exists: bool, /// Whether the account is empty. pub is_empty: bool, + /// Whether the account is empty for new-account gas: absent or empty under EIP-161, absent + /// and untouched before it. + pub is_empty_for_new_account_gas: bool, /// Whether the account access was cold. pub is_cold: bool, #[doc(hidden)] // Not public API. Please use an existing constructor. @@ -2055,15 +2244,11 @@ fn eip7708_transfer_log(from: &Address, to: &Address, value: &Word) -> Option())), - }) + Some(Log::new_unchecked( + SYSTEM_ADDRESS, + vec![EIP7708_TRANSFER_TOPIC, from.into_word(), to.into_word()], + Bytes::from(value.to_be_bytes::<32>()), + )) } #[cfg(test)] @@ -2072,12 +2257,16 @@ mod tests { use crate::{ BaseEvmConfigSelector, BaseEvmTypes, NoopInspector, Precompiles, SpecId, Version, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, - ethereum::{RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, + constants::CALL_DEPTH_LIMIT, + env::BlockEnvExt, + ethereum::{TxEnvelope, ethereum_tx_registry}, interpreter::{GasTracker, Interpreter, Message, MessageExt, MessageKind, op}, precompiles::{Precompile, PrecompileError, PrecompileId, PrecompileMap}, registry::{HandlerError, TxRequest, handler}, - test_utils::{legacy_bytecode, push_address}, + test_utils::{ + handle_test_tx, legacy_bytecode, memory_input_call_code, push_address, test_tx, + }, + utils::address_to_word, }; use alloc::{borrow::Cow, string::ToString, sync::Arc, vec, vec::Vec}; use alloy_consensus::{TxLegacy, transaction::Recovered}; @@ -2124,22 +2313,6 @@ mod tests { assert_eq!(*evm.ext(), 42); } - fn test_tx(value: u64) -> RecoveredTxEnvelope { - Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { nonce: value, ..TxLegacy::default() }), - Address::ZERO, - ) - } - - fn handle_test_tx(req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>) -> HandlerResult { - let _ = req.host.spec_id(); - Ok(TxResultExt { - status: true, - total_gas_spent: req.tx.nonce + 1, - ..TxResultExt::default() - }) - } - #[derive(Debug)] struct TestInterpreterRunner { stop: Option, @@ -2167,14 +2340,13 @@ mod tests { Precompiles::base(SpecId::OSAKA), ); evm.set_interpreter_runner(TestInterpreterRunner { stop, calls: Arc::clone(&calls) }); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 30_000, code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), ..Default::default() }; - let stop = evm.run_interpreter(&tx_env, &message).unwrap(); + let stop = evm.run_interpreter(&message).unwrap(); assert_eq!(calls.load(Ordering::Relaxed), 1); stop @@ -2188,10 +2360,7 @@ mod tests { ) -> HandlerResult { let value = Word::from(req.tx.nonce); req.host.state.storage_slot(&LIFECYCLE_ACCOUNT, LIFECYCLE_STORAGE_KEY)?.write(value); - req.host.state.log(Log { - address: LIFECYCLE_ACCOUNT, - data: LogData::new_unchecked(vec![], Bytes::new()), - }); + req.host.state.log(Log::new_unchecked(LIFECYCLE_ACCOUNT, vec![], Bytes::new())); Ok(TxResultExt { status: true, total_gas_spent: req.tx.nonce, ..TxResultExt::default() }) } @@ -2199,11 +2368,11 @@ mod tests { req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>, ) -> HandlerResult { let account = req.host.state.account(&LIFECYCLE_ACCOUNT)?; - assert_eq!(account.balance(), Word::from(1)); + assert_eq!(account.balance(), Word::ONE); drop(account); let slot = req.host.state.storage_slot(&LIFECYCLE_ACCOUNT, LIFECYCLE_STORAGE_KEY)?; - assert_eq!(slot.current(), Word::from(1)); + assert_eq!(slot.current(), Word::ONE); Ok(TxResultExt { status: true, ..TxResultExt::default() }) } @@ -2238,7 +2407,7 @@ mod tests { destination: address, call_target: address, caller: Address::ZERO, - input: Bytes::new(), + input: Default::default(), value: U256::ZERO, code: Bytecode::default(), code_address: address, @@ -2261,10 +2430,11 @@ mod tests { } let precompiles = precompiles_with([test_precompile(TEST_PRECOMPILE, |evm, _, _| { - evm.log(Log { - address: TEST_PRECOMPILE, - data: LogData::new_unchecked(Vec::new(), Bytes::from_static(b"precompile")), - }); + evm.log(Log::new_unchecked( + TEST_PRECOMPILE, + Vec::new(), + Bytes::from_static(b"precompile"), + )); Ok(PrecompileOutput::new(Bytes::new())) })]); let mut evm = Evm::::new( @@ -2277,12 +2447,12 @@ mod tests { evm.set_inspector(LogInspector::default()); let mut message = precompile_message(TEST_PRECOMPILE); - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Return); let inspector = evm.clear_inspector_as::().unwrap(); assert_eq!(inspector.0.len(), 1); - assert_eq!(inspector.0.as_slice(), evm.logs()); + assert_eq!(inspector.0, evm.logs()); } #[test] @@ -2301,7 +2471,7 @@ mod tests { let mut child = precompile_message(INNER_TEST_PRECOMPILE); child.depth = message.depth + 1; child.input = message.input.clone(); - Host::execute_message(evm, &TxEnvExt::default(), &mut child)?; + Host::execute_message(evm, &mut child)?; evm.log(Log { address: TEST_PRECOMPILE, data: LogData::default() }); Ok(PrecompileOutput::new(Bytes::new())) }), @@ -2328,10 +2498,9 @@ mod tests { evm.set_inspector(LogInspector::default()); let mut message = precompile_message(TEST_PRECOMPILE); if revert { - message.input = Bytes::from_static(b"revert"); + message.input = Bytes::from_static(b"revert").into(); } - let result = - Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Return); let inspector = evm.clear_inspector_as::().unwrap(); @@ -2377,8 +2546,7 @@ mod tests { for address in [TEST_PRECOMPILE, INNER_TEST_PRECOMPILE] { let mut message = precompile_message(address); - let result = - Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::PrecompileOOG); assert_eq!(result.gas.remaining(), 0); @@ -2394,9 +2562,9 @@ mod tests { let mut database = InMemoryDB::default(); database.insert_account_info( &LIFECYCLE_ACCOUNT, - AccountInfo::default().with_balance(Word::from(1)), + AccountInfo::default().with_balance(Word::ONE), ); - database.insert_account_storage(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY, &Word::from(1)); + database.insert_account_storage(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY, &Word::ONE); Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -2415,9 +2583,9 @@ mod tests { let mut database = InMemoryDB::default(); database.insert_account_info( &LIFECYCLE_ACCOUNT, - AccountInfo::default().with_balance(Word::from(1)), + AccountInfo::default().with_balance(Word::ONE), ); - database.insert_account_storage(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY, &Word::from(1)); + database.insert_account_storage(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY, &Word::ONE); Evm::::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -2429,12 +2597,11 @@ mod tests { #[test] fn owned_database_error_survives_nested_precompile_calls() { - use crate::{DatabaseError, evm::bal::BalError}; let precompiles = precompiles_with([ test_precompile(TEST_PRECOMPILE, |evm, _, _| { let mut child = precompile_message(INNER_TEST_PRECOMPILE); child.depth = 1; - Host::execute_message(evm, &TxEnvExt::default(), &mut child)?; + Host::execute_message(evm, &mut child)?; panic!("execution must abort on a database error"); }), test_precompile(INNER_TEST_PRECOMPILE, |_, _, _| { @@ -2452,11 +2619,7 @@ mod tests { InMemoryDB::default(), precompiles, ); - let result = Host::execute_message( - &mut evm, - &TxEnvExt::default(), - &mut precompile_message(TEST_PRECOMPILE), - ); + let result = Host::execute_message(&mut evm, &mut precompile_message(TEST_PRECOMPILE)); let ExecutionError::Database(error) = result.unwrap_err() else { panic!("expected database error") }; @@ -2481,7 +2644,7 @@ mod tests { Host::load_account(&mut evm, &address, true, true), Err(HostError::Halt(InstrStop::OutOfGas)) )); - let stats = evm.database().downcast_ref::>().unwrap().counts(); + let stats = evm.database_as::>().unwrap().counts(); assert_eq!(stats.get_account, 0); assert_eq!(stats.get_code_by_hash, 0); @@ -2491,7 +2654,7 @@ mod tests { Host::sload(&mut evm, &address, &Word::ZERO, true), Err(HostError::Halt(InstrStop::OutOfGas)) )); - let stats = evm.database().downcast_ref::>().unwrap().counts(); + let stats = evm.database_as::>().unwrap().counts(); assert_eq!(stats.get_account, 1); assert_eq!(stats.get_storage, 0); assert_eq!(stats.get_code_by_hash, 0); @@ -2512,7 +2675,7 @@ mod tests { impl Error for TestPrecompileError {} - let contract = Address::from([0xbb; 20]); + let contract = Address::repeat_byte(0xbb); let mut code = vec![op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0]; push_address(&mut code, &FATAL_PRECOMPILE_ADDRESS); code.extend([ @@ -2550,7 +2713,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message); + let result = Host::execute_message(&mut evm, &mut message); let ExecutionError::Fatal(error) = result.unwrap_err() else { panic!("expected fatal precompile error") @@ -2562,7 +2725,7 @@ mod tests { !pending .accounts .get(&contract) - .is_some_and(|account| account.storage.slots.contains_key(&Word::from(1))) + .is_some_and(|account| account.storage.slots.contains_key(&Word::ONE)) ); } @@ -2570,7 +2733,7 @@ mod tests { fn precompile_oog_halt_remains_recoverable_by_parent_call() { const OOG_PRECOMPILE_ADDRESS: Address = Address::with_last_byte(0x44); - let contract = Address::from([0xbc; 20]); + let contract = Address::repeat_byte(0xbc); let mut code = vec![op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0]; push_address(&mut code, &OOG_PRECOMPILE_ADDRESS); code.extend([ @@ -2608,7 +2771,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::Stop); evm.state.finalize_transaction_(Version::base(SpecId::OSAKA)); @@ -2617,7 +2780,7 @@ mod tests { pending .accounts .get(&contract) - .is_some_and(|account| account.storage.slots.contains_key(&Word::from(1))) + .is_some_and(|account| account.storage.slots.contains_key(&Word::ONE)) ); } @@ -2636,7 +2799,7 @@ mod tests { impl Error for TestPrecompileError {} - let caller = Address::from([0xaa; 20]); + let caller = Address::repeat_byte(0xaa); let tx = Recovered::new_unchecked( TxEnvelope::Legacy(TxLegacy { gas_limit: 50_000, @@ -2659,13 +2822,13 @@ mod tests { precompiles, ); - let error = evm.transact(&tx).map(ExecutedTx::discard).unwrap_err(); + let error = evm.call_tx(&tx).unwrap_err(); let HandlerError::Fatal(error) = error else { panic!("expected fatal precompile error") }; assert!(error.downcast_ref::().is_some()); assert_eq!(error.to_string(), "test precompile error"); evm.set_precompiles(Precompiles::base(SpecId::OSAKA)); let next = evm.transact(&tx).unwrap().discard(); - assert!(next.status); + assert!(next.is_success()); assert!(error.downcast_ref::().is_some()); } @@ -2915,8 +3078,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let _ = evm.run_interpreter(&tx_env, &message); + let _ = evm.run_interpreter(&message); } #[test] @@ -2941,9 +3103,8 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let _ = evm.run_interpreter(&tx_env, &message); + let _ = evm.run_interpreter(&message); } #[test] @@ -2999,12 +3160,70 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let _ = evm.run_interpreter(&tx_env, &message); + let _ = evm.run_interpreter(&message); assert_eq!(evm.block().number, Word::from(123)); assert_eq!(evm.block().timestamp, Word::from(124)); } + #[test] + fn tx_env_mut_during_transaction_overrides_origin() { + struct OriginInspector(Address); + + impl Inspector for OriginInspector { + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.depth == 0 { + interp.host().tx_env_mut().origin = self.0; + } + None + } + } + + let caller = Address::repeat_byte(0xaa); + let origin = Address::repeat_byte(0xbb); + let target = Address::repeat_byte(0x11); + let inner = Address::repeat_byte(0x22); + // SSTORE(0, ORIGIN), then CALL(inner). + let mut code = vec![op::ORIGIN, op::PUSH0, op::SSTORE]; + code.extend([op::PUSH0, op::PUSH0, op::PUSH0, op::PUSH0, op::PUSH0, op::PUSH20]); + code.extend(inner.0); + code.extend([op::GAS, op::CALL, op::STOP]); + // SSTORE(1, ORIGIN) + let inner_code = vec![op::ORIGIN, op::PUSH1, 1, op::SSTORE, op::STOP]; + let mut database = InMemoryDB::default(); + for (address, code) in [(target, code), (inner, inner_code)] { + database.insert_account_info( + &address, + AccountInfo::default().with_nonce(1).with_code(legacy_bytecode(code)), + ); + } + let mut evm = Evm::::new( + SpecId::CANCUN, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::CANCUN), + database, + Precompiles::base(SpecId::CANCUN), + ); + let tx = Recovered::new_unchecked( + TxEnvelope::Legacy(TxLegacy { + to: TxKind::Call(target), + gas_limit: 500_000, + ..Default::default() + }), + caller, + ); + let executed = evm.transact_with_inspector(&tx, &mut OriginInspector(origin)).unwrap(); + assert!(executed.commit().is_success()); + + let origin_word = address_to_word(&origin); + assert_eq!(evm.state.storage_slot_untracked(&target, &Word::ZERO).unwrap(), origin_word); + assert_eq!(evm.state.storage_slot_untracked(&inner, &Word::ONE).unwrap(), origin_word); + assert_eq!(evm.read_account_info(&caller).unwrap().unwrap().nonce, 1); + } + #[test] #[should_panic(expected = "execution config cannot be modified during EVM execution")] fn set_execution_config_panics_during_execution() { @@ -3034,8 +3253,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let _ = evm.run_interpreter(&tx_env, &message); + let _ = evm.run_interpreter(&message); } #[derive(Clone, Copy)] @@ -3087,11 +3305,11 @@ mod tests { } TopLevelInspectorHook::Call => { let mut message = MessageExt { kind: MessageKind::Call, ..Default::default() }; - let _ = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message); + let _ = Host::execute_message(&mut evm, &mut message); } TopLevelInspectorHook::Create => { let mut message = MessageExt { kind: MessageKind::Create, ..Default::default() }; - let _ = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message); + let _ = Host::execute_message(&mut evm, &mut message); } } } @@ -3123,10 +3341,10 @@ mod tests { assert_eq!(message.depth, 67); assert_eq!(message.destination, TEST_PRECOMPILE); assert_eq!(message.caller, Address::with_last_byte(0x7a)); - assert_eq!(message.input, Bytes::from_static(b"message input")); + assert_eq!(message.input.as_owned().unwrap()[..], *b"message input"); assert_eq!(message.value, U256::from(99)); assert_eq!(message.code_address, TEST_PRECOMPILE); - Ok(PrecompileOutput::new(Bytes::copy_from_slice(&evm.block.number.to_be_bytes::<32>()))) + Ok(PrecompileOutput::new(Bytes::from(evm.block.number.to_be_bytes::<32>()))) })]); let mut evm = Evm::::new( SpecId::OSAKA, @@ -3143,7 +3361,7 @@ mod tests { destination: address, call_target: address, caller: Address::with_last_byte(0x7a), - input: Bytes::from_static(b"message input"), + input: Bytes::from_static(b"message input").into(), value: U256::from(99), code: Bytecode::default(), code_address: address, @@ -3238,6 +3456,93 @@ mod tests { assert_eq!(run_with_test_interpreter_runner(None, &[op::STOP]), InstrStop::Stop); } + #[derive(Debug)] + struct SpecBoundRunner { + spec_id: SpecId, + chain_id: u64, + calls: Arc, + } + + impl InterpreterRunner for SpecBoundRunner { + fn run<'frame, 'host>( + &self, + _interpreter: &mut Interpreter<'frame, 'host, BaseEvmTypes>, + _host: &mut Evm<'host, BaseEvmTypes>, + ) -> Option { + self.calls.fetch_add(1, Ordering::Relaxed); + Some(InstrStop::Return) + } + + fn supports(&self, config: &ExecutionConfig) -> bool { + config.base_spec_id() == self.spec_id && config.version().chain_id == self.chain_id + } + } + + #[test] + fn interpreter_runner_support_follows_runner_and_execution_config() { + let calls = Arc::new(AtomicUsize::new(0)); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + let message = MessageExt { + gas_limit: 30_000, + code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), + ..Default::default() + }; + let run = |evm: &mut Evm<'_, BaseEvmTypes>| { + let stop = evm.run_interpreter(&message).unwrap(); + (stop, calls.load(Ordering::Relaxed)) + }; + let set_spec = |evm: &mut Evm<'_, BaseEvmTypes>, spec_id: SpecId| { + evm.set_execution_config( + ExecutionConfig::for_base_spec::(spec_id), + spec_id, + TxRegistry::new(), + Precompiles::base(spec_id), + ); + }; + + evm.set_shared_interpreter_runner(Arc::new(SpecBoundRunner { + spec_id: SpecId::PRAGUE, + chain_id: Version::new(SpecId::PRAGUE).chain_id, + calls: Arc::clone(&calls), + })); + assert_eq!(run(&mut evm), (InstrStop::Stop, 0)); + + set_spec(&mut evm, SpecId::PRAGUE); + assert_eq!(run(&mut evm), (InstrStop::Return, 1)); + + set_spec(&mut evm, SpecId::OSAKA); + assert_eq!(run(&mut evm), (InstrStop::Stop, 1)); + + set_spec(&mut evm, SpecId::PRAGUE); + assert_eq!(run(&mut evm), (InstrStop::Return, 2)); + + let mut version = Version::new(SpecId::PRAGUE); + version.chain_id += 1; + evm.set_block_and_execution_config_within_spec( + BlockEnvExt::default(), + ExecutionConfig::for_spec_and_version(SpecId::PRAGUE, version), + ); + assert_eq!(run(&mut evm), (InstrStop::Stop, 2)); + + evm.set_block_and_execution_config_within_spec( + BlockEnvExt::default(), + ExecutionConfig::for_base_spec::(SpecId::PRAGUE), + ); + assert_eq!(run(&mut evm), (InstrStop::Return, 3)); + + let runner = evm.clear_interpreter_runner().unwrap(); + assert_eq!(run(&mut evm), (InstrStop::Stop, 3)); + + evm.set_shared_interpreter_runner(runner); + assert_eq!(run(&mut evm), (InstrStop::Return, 4)); + } + #[test] fn dispatches_transaction_with_dynamic_version() { fn handle_test_tx_version( @@ -3306,20 +3611,20 @@ mod tests { TxEnvelope::as_legacy, handler(|_| Ok(()), |req, ()| fail(req)), ); - let error = evm.transact(&test_tx(7)).map(ExecutedTx::discard).unwrap_err(); + let error = evm.call_tx(&test_tx(7)).unwrap_err(); let HandlerError::Fatal(error) = error else { panic!("expected fatal handler error") }; assert_eq!(error.to_string(), "handler failed"); assert!(evm.logs().is_empty()); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); evm.registry.register( TEST_TX_TYPE, TxEnvelope::as_legacy, handler(|_| Ok(()), |req, ()| handle_lifecycle_tx(req)), ); - assert!(evm.transact(&test_tx(9)).unwrap().commit().status); + assert!(evm.transact(&test_tx(9)).unwrap().commit().is_success()); } #[test] @@ -3332,7 +3637,7 @@ mod tests { assert_eq!(outcome.logs.len(), 1); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3351,11 +3656,11 @@ mod tests { assert_eq!(outcome.logs.len(), 1); let storage = sink.storage_sorted(); assert_eq!(storage.len(), 1); - assert_eq!(storage[0].1.original, Word::from(1)); + assert_eq!(storage[0].1.original, Word::ONE); assert_eq!(storage[0].1.current, Word::from(7)); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3400,11 +3705,11 @@ mod tests { .storage; let slot = overlay.slots.get(&LIFECYCLE_STORAGE_KEY).expect("storage slot should be present"); - assert_eq!(slot.value.original, Word::from(1)); + assert_eq!(slot.value.original, Word::ONE); assert_eq!(slot.value.current, Word::from(7)); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3429,7 +3734,7 @@ mod tests { // Detaching accepted nothing into this EVM's overlay. assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3468,7 +3773,7 @@ mod tests { let storage = block_state.storage_sorted(); assert_eq!(storage.len(), 1); - assert_eq!(storage[0].1.original, Word::from(1)); + assert_eq!(storage[0].1.original, Word::ONE); assert_eq!(storage[0].1.current, Word::from(9)); assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), @@ -3513,10 +3818,10 @@ mod tests { // Taking the BAL yields a canonical EIP-7928 list and resets the index. let alloy = BlockAccessList::from(evm.state.take_bal_builder().expect("bal is present")); assert_eq!(alloy.len(), 1); - assert_eq!(alloy[0].address, LIFECYCLE_ACCOUNT); + assert_eq!(alloy[0].address(), LIFECYCLE_ACCOUNT); assert_eq!(alloy[0].storage_changes.len(), 1); assert_eq!(alloy[0].storage_changes[0].slot, LIFECYCLE_STORAGE_KEY); - assert_eq!(alloy[0].storage_changes[0].changes.len(), 2); + assert_eq!(alloy[0].storage_changes[0].len(), 2); assert_eq!(evm.state.bal_index(), BlockAccessIndex::PRE_EXECUTION); assert!(evm.state.bal_builder().is_none()); } @@ -3566,7 +3871,7 @@ mod tests { let slots = changes.storage.changed_slots().collect::>(); assert_eq!(slots.len(), 1); assert_eq!(*slots[0].0, LIFECYCLE_STORAGE_KEY); - assert_eq!(slots[0].1.original, Word::from(1)); + assert_eq!(slots[0].1.original, Word::ONE); assert_eq!(slots[0].1.current, Word::from(7)); let _ = executed.commit(); @@ -3583,7 +3888,7 @@ mod tests { assert_eq!( evm.state.storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY).unwrap(), - Word::from(1) + Word::ONE ); } @@ -3596,7 +3901,7 @@ mod tests { InMemoryDB::default(), Precompiles::base(SpecId::OSAKA), ); - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let bytecode = Bytecode::new_legacy(Bytes::from_static(&[op::ADDRESS, op::STOP])); let mut message = MessageExt { kind: MessageKind::Call, @@ -3607,8 +3912,8 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); - assert!(result.stop.is_success()); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + assert!(result.is_success()); } #[test] @@ -3661,7 +3966,7 @@ mod tests { Db::new(FailingStorageDb), Precompiles::base(SpecId::OSAKA), ); - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let bytecode = Bytecode::new_legacy(Bytes::from_static(&[op::PUSH0, op::SLOAD])); let mut message = MessageExt { kind: MessageKind::Call, @@ -3672,7 +3977,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message); + let result = Host::execute_message(&mut evm, &mut message); let ExecutionError::Database(error) = result.unwrap_err() else { panic!("expected database error") @@ -3681,10 +3986,7 @@ mod tests { assert!(error.downcast_ref::().is_some()); assert_eq!(error.to_string(), "storage read failed"); message.code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); - assert_eq!( - Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap().stop, - InstrStop::Stop - ); + assert_eq!(Host::execute_message(&mut evm, &mut message).unwrap().stop, InstrStop::Stop); } #[test] @@ -3696,7 +3998,7 @@ mod tests { InMemoryDB::default(), Precompiles::base(SpecId::OSAKA), ); - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let key = Word::ZERO; let bytecode = Bytecode::new_legacy(Bytes::from_static(&[op::PUSH1, 0, op::SLOAD, op::STOP])); @@ -3709,7 +4011,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::OutOfGas); assert!(!evm.state.storage(&contract).unwrap().is_warm(&key)); @@ -3745,7 +4047,7 @@ mod tests { self.target_reads.fetch_add(1, Ordering::SeqCst); return Err(SelfdestructTargetLoadError); } - Ok(Some(AccountInfo::default().with_balance(Word::from(1)))) + Ok(Some(AccountInfo::default().with_balance(Word::ONE))) } fn get_code_by_hash(&mut self, _code_hash: &B256) -> Result { @@ -3772,8 +4074,8 @@ mod tests { legacy_bytecode(code) } - let contract = Address::from([0xbb; 20]); - let target = Address::from([0xcc; 20]); + let contract = Address::repeat_byte(0xbb); + let target = Address::repeat_byte(0xcc); let target_reads = Arc::new(AtomicUsize::new(0)); let database = SelfdestructTargetLoadDb { target, target_reads: Arc::clone(&target_reads) }; let mut evm = Evm::::new( @@ -3791,7 +4093,7 @@ mod tests { code: selfdestruct_to_code(&target), ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::OutOfGas); assert_eq!(target_reads.load(Ordering::SeqCst), 0); @@ -3799,10 +4101,10 @@ mod tests { #[test] fn frontier_code_deposit_oog_creates_empty_contract() { - let caller = Address::from([0x11; 20]); - let created = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let created = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::ONE)); let mut evm = Evm::::new( SpecId::FRONTIER, BlockEnvExt::default(), @@ -3821,19 +4123,18 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); - assert!(result.stop.is_success()); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + assert!(result.is_success()); evm.state.finalize_transaction_(Version::base(SpecId::FRONTIER)); let pending = evm.state.take_pending_state(); - let account = - pending.accounts.get(&created).and_then(|entry| entry.present.as_ref()).unwrap(); + let account = pending.account_info(&created).unwrap(); assert!(account.is_empty_code_hash()); } #[test] fn frontier_materialized_empty_account_is_created() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut evm = Evm::::new( SpecId::FRONTIER, BlockEnvExt::default(), @@ -3851,10 +4152,10 @@ mod tests { #[test] fn homestead_code_deposit_oog_fails_create() { - let caller = Address::from([0x11; 20]); - let created = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let created = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::ONE)); let mut evm = Evm::::new( SpecId::HOMESTEAD, BlockEnvExt::default(), @@ -3873,7 +4174,7 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::OutOfGas); assert!(result.output.is_empty()); @@ -3884,10 +4185,10 @@ mod tests { #[test] fn create_validation_failure_clears_output() { - let caller = Address::from([0x11; 20]); - let created = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let created = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::ONE)); let mut evm = Evm::::new( SpecId::LONDON, BlockEnvExt::default(), @@ -3915,7 +4216,7 @@ mod tests { code, ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::CreateContractStartingWithEF); assert!(result.output.is_empty()); @@ -3923,10 +4224,10 @@ mod tests { #[test] fn create_size_limit_failure_clears_output() { - let caller = Address::from([0x11; 20]); - let created = Address::from([0x22; 20]); + let caller = Address::repeat_byte(0x11); + let created = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&caller, AccountInfo::default().with_balance(Word::ONE)); let mut evm = Evm::::new( SpecId::SPURIOUS_DRAGON, BlockEnvExt::default(), @@ -3950,7 +4251,7 @@ mod tests { code, ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::CreateContractSizeLimit); assert!(result.output.is_empty()); @@ -3958,10 +4259,10 @@ mod tests { #[test] fn staticcall_touches_empty_existing_destination() { - let target = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x11); let mut database = InMemoryDB::default(); database.insert_account_info(&target, AccountInfo::default()); - database.insert_account_storage(&target, &Word::ZERO, &Word::from(1)); + database.insert_account_storage(&target, &Word::ZERO, &Word::ONE); let mut evm = Evm::::new( SpecId::SPURIOUS_DRAGON, BlockEnvExt::default(), @@ -3977,8 +4278,8 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); - assert!(result.stop.is_success()); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + assert!(result.is_success()); evm.state.finalize_transaction_(Version::base(SpecId::SPURIOUS_DRAGON)); let pending = evm.state.take_pending_state(); @@ -3990,13 +4291,12 @@ mod tests { #[test] fn delegatecall_does_not_touch_empty_code_address() { - let destination = Address::from([0x11; 20]); - let code_address = Address::from([0x22; 20]); + let destination = Address::repeat_byte(0x11); + let code_address = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); - database - .insert_account_info(&destination, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&destination, AccountInfo::default().with_balance(Word::ONE)); database.insert_account_info(&code_address, AccountInfo::default()); - database.insert_account_storage(&code_address, &Word::ZERO, &Word::from(1)); + database.insert_account_storage(&code_address, &Word::ZERO, &Word::ONE); let mut evm = Evm::::new( SpecId::SPURIOUS_DRAGON, BlockEnvExt::default(), @@ -4012,8 +4312,8 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); - assert!(result.stop.is_success()); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + assert!(result.is_success()); evm.state.finalize_transaction_(Version::base(SpecId::SPURIOUS_DRAGON)); let pending = evm.state.take_pending_state(); @@ -4030,8 +4330,8 @@ mod tests { #[test] fn transfer_moves_value() { - let from = Address::from([0x01; 20]); - let to = Address::from([0x02; 20]); + let from = Address::repeat_byte(0x01); + let to = Address::repeat_byte(0x02); let mut state = State::new(InMemoryDB::default()); state.account(&from).unwrap().add_balance(U256::from(10)); @@ -4056,8 +4356,8 @@ mod tests { #[test] fn amsterdam_call_value_emits_eip7708_log() { - let caller = Address::from([0x01; 20]); - let target = Address::from([0x02; 20]); + let caller = Address::repeat_byte(0x01); + let target = Address::repeat_byte(0x02); let mut database = InMemoryDB::default(); database.insert_account_info(&caller, AccountInfo::default().with_balance(U256::from(10))); let mut evm = Evm::::new( @@ -4078,8 +4378,8 @@ mod tests { ..MessageExt::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); - assert!(result.stop.is_success()); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + assert!(result.is_success()); let version = *evm.version(); evm.state.finalize_transaction_(&version); @@ -4087,20 +4387,13 @@ mod tests { assert_eq!(logs.len(), 1); let log = &logs[0]; assert_eq!(log.address, SYSTEM_ADDRESS); - assert_eq!( - log.topics(), - &[ - EIP7708_TRANSFER_TOPIC, - B256::left_padding_from(caller.as_slice()), - B256::left_padding_from(target.as_slice()), - ] - ); - assert_eq!(log.data.data, Bytes::copy_from_slice(&U256::from(7).to_be_bytes::<32>())); + assert_eq!(log.topics(), &[EIP7708_TRANSFER_TOPIC, caller.into_word(), target.into_word()]); + assert_eq!(log.data.data, Bytes::from(B256::with_last_byte(7))); } #[test] fn eip8246_selfdestruct_preserves_balance_at_finalization() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info( @@ -4124,13 +4417,12 @@ mod tests { assert!(info.is_empty_code_hash()); let pending = state.take_pending_state(); - assert!(pending.accounts.contains_key(&contract), "account change recorded"); - assert!(pending.selfdestructs.contains(&contract)); + assert!(pending.accounts[&contract].flags.contains(AccountFlags::DESTROYED)); } #[test] - fn commit_transaction_clears_detached_selfdestructs() { - let contract = Address::from([0x44; 20]); + fn commit_transaction_clears_detached_destroyed_flag() { + let contract = Address::repeat_byte(0x44); let code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info(&contract, AccountInfo::default().with_code(code)); @@ -4148,7 +4440,7 @@ mod tests { #[test] fn eip8246_selfdestruct_zero_balance_is_deleted() { - let contract = Address::from([0x22; 20]); + let contract = Address::repeat_byte(0x22); let code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info(&contract, AccountInfo::default().with_code(code)); @@ -4163,7 +4455,7 @@ mod tests { #[test] fn pre_eip8246_selfdestruct_burns_balance_by_deleting_account() { - let contract = Address::from([0x33; 20]); + let contract = Address::repeat_byte(0x33); let code = Bytecode::new_legacy(Bytes::from_static(&[op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info( @@ -4347,7 +4639,7 @@ mod tests { { assert_eq!(steps.load(Ordering::Relaxed), 0); } - assert!(evm.call_tx(&tx).unwrap().status); + assert!(evm.call_tx(&tx).unwrap().is_success()); } #[test] @@ -4370,10 +4662,7 @@ mod tests { .storage_slot(&LIFECYCLE_ACCOUNT, LIFECYCLE_STORAGE_KEY)? .write(Word::from(99)); req.host.state.account(&LIFECYCLE_ACCOUNT)?.bump_nonce(); - req.host.state.log(Log { - address: LIFECYCLE_ACCOUNT, - data: LogData::new_unchecked(vec![], Bytes::new()), - }); + req.host.state.log(Log::new_unchecked(LIFECYCLE_ACCOUNT, vec![], Bytes::new())); if let Some(error) = errors.get(req.tx.nonce as usize) { return Err(error.clone()); } @@ -4394,7 +4683,7 @@ mod tests { evm.state .storage_slot_untracked(&LIFECYCLE_ACCOUNT, &LIFECYCLE_STORAGE_KEY) .unwrap(), - Word::from(1) + Word::ONE ); } } @@ -4529,16 +4818,14 @@ mod tests { let created = caller.create(nonce); let mut database = InMemoryDB::default(); database.insert_account_info(&caller, AccountInfo::default().with_nonce(nonce)); - let mut version = Version::new(spec); - version.initial_call_depth = initial_call_depth; - let mut evm = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(spec, version), + let mut evm = Evm::::new( spec, BlockEnvExt::default(), ethereum_tx_registry(spec), database, Precompiles::base(spec), ); + evm.set_initial_call_depth(initial_call_depth); // The nested transaction root also creates a child, which must still bump its creator. let tx = Recovered::new_unchecked( TxEnvelope::Legacy(TxLegacy { @@ -4551,7 +4838,7 @@ mod tests { caller, ); let executed = evm.transact(&tx).unwrap(); - assert!(executed.result().status); + assert!(executed.result().is_success()); let pending = executed.detach().pending_state; assert_eq!(pending.account_info(&caller).unwrap().nonce, nonce + 1); assert_eq!(pending.account_info(&created).unwrap().nonce, 2); @@ -4607,7 +4894,7 @@ mod tests { caller, ); let executed = evm.transact(&tx).unwrap(); - assert!(executed.result().status); + assert!(executed.result().is_success()); for (address, touched) in [(target, true), (read, false), (child, false), (leaf, false)] { let changes = executed.account_changes().find(|c| c.address == address).unwrap(); assert_eq!(changes.touched, touched, "{address}"); @@ -4632,7 +4919,7 @@ mod tests { caller, ); let next = evm.transact(&tx).unwrap(); - assert!(next.result().status); + assert!(next.result().is_success()); assert!(!next.account_changes().find(|c| c.address == target).unwrap().touched); } @@ -4664,23 +4951,22 @@ mod tests { ); let executed = evm.transact(&tx).unwrap(); - assert!(executed.result().status); + assert!(executed.result().is_success()); let changes = executed.changes(); assert!(!changes.is_empty()); assert_eq!(changes.account_info(&caller).unwrap().nonce, 1); let slot = changes.account_changes_for(&target).unwrap().storage.slots[&Word::ZERO].value; - assert_eq!(slot.current, Word::from(1)); - let mut in_place = changes.account_changes().map(|c| c.address).collect::>(); + assert_eq!(slot.current, Word::ONE); + let mut in_place = executed.account_changes().map(|c| c.address).collect::>(); in_place.sort(); let _ = executed.discard(); // Discarding after inspection leaves the accepted state unchanged, so the same transaction // executes again, and its detached state reports the same accounts. let executed = evm.transact(&tx).unwrap(); - assert!(executed.result().status); + assert!(executed.result().is_success()); let pending = executed.detach().pending_state; - let mut detached = - pending.changes().account_changes().map(|c| c.address).collect::>(); + let mut detached = pending.account_changes().map(|c| c.address).collect::>(); detached.sort(); assert_eq!(in_place, detached); assert_eq!(pending.changes().account_info(&caller).unwrap().nonce, 1); @@ -4723,7 +5009,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - Host::execute_message(req.host, &TxEnvExt::default(), &mut message)?; + Host::execute_message(req.host, &mut message)?; Ok(TxResultExt::default()) }, ), @@ -4841,7 +5127,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - Host::execute_message(req.host, &TxEnvExt::default(), &mut message)?; + Host::execute_message(req.host, &mut message)?; Ok(TxResultExt::default()) }, ), @@ -4856,10 +5142,9 @@ mod tests { assert!(evm.logs().is_empty()); } - #[cfg(feature = "async")] #[test] - #[should_panic(expected = "async EVM execution cannot run with a borrowed inspector")] - fn transact_with_inspector_rejects_nested_async_send() { + #[should_panic(expected = "inspector cannot be modified during EVM execution")] + fn nested_transact_with_inspector_panics() { let mut evm = scoped_inspector_evm(); evm.registry.register( TEST_TX_TYPE, @@ -4867,34 +5152,264 @@ mod tests { handler( |_| Ok(()), |req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>, ()| { - drop(req.host.transact_async_send(&test_tx(1))); + let _ = req + .host + .transact_with_inspector(&test_tx(1), &mut CallCounter::default())?; Ok(TxResultExt::default()) }, ), ); - evm.evm_is_send::>(); let _ = evm.transact_with_inspector(&test_tx(0), &mut CallCounter::default()); } #[test] - #[should_panic(expected = "inspector cannot be modified during EVM execution")] - fn nested_transact_with_inspector_panics() { - let mut evm = scoped_inspector_evm(); - evm.registry.register( + fn set_initial_call_depth_offsets_message_depths_and_limit() { + #[derive(Default)] + struct DepthInspector(Vec); + + impl Inspector for DepthInspector { + fn call( + &mut self, + _interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + self.0.push(message.depth); + None + } + } + + let target = Address::repeat_byte(0x11); + let child = Address::repeat_byte(0x22); + let mut code = vec![op::PUSH0; 5]; + push_address(&mut code, &child); + code.extend([op::GAS, op::CALL, op::POP, op::STOP]); + let mut database = InMemoryDB::default(); + database + .insert_account_info(&target, AccountInfo::default().with_code(legacy_bytecode(code))); + let mut evm = Evm::::new( + SpecId::CANCUN, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::CANCUN), + database, + Precompiles::base(SpecId::CANCUN), + ); + evm.set_initial_call_depth(7); + assert_eq!(evm.initial_call_depth(), 7); + + let tx = Recovered::new_unchecked( + TxEnvelope::Legacy(TxLegacy { + to: TxKind::Call(target), + gas_limit: 100_000, + ..Default::default() + }), + Address::repeat_byte(0xaa), + ); + let mut inspector = DepthInspector::default(); + let executed = evm.transact_with_inspector(&tx, &mut inspector).unwrap(); + assert!(executed.result().is_success()); + drop(executed); + assert_eq!(inspector.0, [7, 8]); + + // The full call depth budget remains below the new initial depth. + for (depth, stop) in [ + (7 + CALL_DEPTH_LIMIT, InstrStop::Stop), + (7 + CALL_DEPTH_LIMIT + 1, InstrStop::CallTooDeep), + ] { + let mut message = MessageExt { + depth, + gas_limit: 50_000, + code: legacy_bytecode(vec![op::STOP]), + ..MessageExt::default() + }; + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + assert_eq!(result.stop, stop); + } + } + + #[test] + fn set_initial_call_depth_from_transaction_handler() { + let registry = TxRegistry::new().with_handler( TEST_TX_TYPE, TxEnvelope::as_legacy, handler( |_| Ok(()), |req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>, ()| { - let _ = req - .host - .transact_with_inspector(&test_tx(1), &mut CallCounter::default())?; - Ok(TxResultExt::default()) + req.host.set_initial_call_depth(1); + let depth = req.host.initial_call_depth(); + req.host.set_initial_call_depth(0); + Ok(TxResultExt { + status: true, + total_gas_spent: depth.into(), + ..TxResultExt::default() + }) }, ), ); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + registry, + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); - let _ = evm.transact_with_inspector(&test_tx(0), &mut CallCounter::default()); + let executed = evm.transact(&test_tx(0)).unwrap(); + assert_eq!(executed.discard().tx_gas_used(), 1); + assert_eq!(evm.initial_call_depth(), 0); + } + + #[test] + #[should_panic(expected = "initial call depth cannot be modified while a frame is running")] + fn set_initial_call_depth_panics_while_frame_is_running() { + struct DepthSettingInspector; + + impl Inspector for DepthSettingInspector { + fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + interp.host().set_initial_call_depth(1); + } + } + + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + evm.set_inspector(DepthSettingInspector); + let message = MessageExt { + code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), + ..MessageExt::default() + }; + let _ = evm.run_interpreter(&message); + } + + #[test] + #[should_panic] + fn set_initial_call_depth_rejects_invalid_depth() { + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + Precompiles::base(SpecId::OSAKA), + ); + evm.set_initial_call_depth(u16::MAX - CALL_DEPTH_LIMIT); + } + + #[test] + fn hook_resizing_caller_memory_keeps_precompile_input_readable() { + // Grows and overwrites the caller's memory in the hook of a message that a precompile + // sends, after the precompile read its input from that memory. + struct OverwriteCallerMemory; + + impl Inspector for OverwriteCallerMemory { + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.destination == INNER_TEST_PRECOMPILE { + let memory = interp.memory_mut(); + memory.resize(0, 1 << 20).unwrap(); + memory.set(0, &[0xbb; 32]); + } + None + } + } + + let precompiles = precompiles_with([ + test_precompile(TEST_PRECOMPILE, |evm, message, _| { + let input = Bytes::copy_from_slice(evm.message_input(message)); + let mut child = precompile_message(INNER_TEST_PRECOMPILE); + child.depth = message.depth + 1; + Host::execute_message(evm, &mut child)?; + // The input is a range of the caller's memory, which the hook changed. + assert_eq!(evm.message_input(message), [0xbb; 32]); + Ok(PrecompileOutput::new(input)) + }), + test_precompile(INNER_TEST_PRECOMPILE, |_, _, _| { + Ok(PrecompileOutput::new(Bytes::new())) + }), + ]); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + InMemoryDB::default(), + precompiles, + ); + evm.set_inspector(OverwriteCallerMemory); + let code = memory_input_call_code(TEST_PRECOMPILE, op::CALL); + let mut message = + MessageExt { gas_limit: 100_000, code: legacy_bytecode(code), ..Default::default() }; + + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + + assert_eq!(result.stop, InstrStop::Return); + assert_eq!(result.output[..], [0xaa; 32]); + } + + #[test] + fn initialize_hook_message_keeps_caller_memory() { + const CALLEE: Address = Address::with_last_byte(0xca); + + struct SendOnInitialize; + + impl Inspector for SendOnInitialize { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + if interp.message().destination == CALLEE { + let mut message = precompile_message(INNER_TEST_PRECOMPILE); + message.depth = interp.message().depth + 1; + Host::execute_message(interp.host(), &mut message).unwrap(); + } + } + + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, BaseEvmTypes>, + message: &mut Message, + ) -> Option> { + if message.destination == INNER_TEST_PRECOMPILE { + let memory = interp.memory_mut(); + memory.resize(0, 1 << 20).unwrap(); + memory.set(0, &[0xbb; 64]); + } + None + } + } + + let precompiles = precompiles_with([test_precompile(INNER_TEST_PRECOMPILE, |_, _, _| { + Ok(PrecompileOutput::new(Bytes::new())) + })]); + let mut database = InMemoryDB::default(); + let echo = [ + op::CALLDATASIZE, + op::PUSH0, + op::PUSH0, + op::CALLDATACOPY, + op::CALLDATASIZE, + op::PUSH0, + op::RETURN, + ]; + database + .insert_account_info(&CALLEE, AccountInfo::default().with_code(legacy_bytecode(echo))); + let mut evm = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + database, + precompiles, + ); + evm.set_inspector(SendOnInitialize); + let code = memory_input_call_code(CALLEE, op::CALL); + let mut message = + MessageExt { gas_limit: 100_000, code: legacy_bytecode(code), ..Default::default() }; + + let result = Host::execute_message(&mut evm, &mut message).unwrap(); + + assert_eq!(result.stop, InstrStop::Return); + assert_eq!(result.output[..], [0xaa; 32]); } } diff --git a/crates/evm2/src/evm/prewarm_set.rs b/crates/evm2/src/evm/prewarm_set.rs index be74165b0..3ec99d72f 100644 --- a/crates/evm2/src/evm/prewarm_set.rs +++ b/crates/evm2/src/evm/prewarm_set.rs @@ -20,7 +20,7 @@ use crate::{interpreter::Word, utils::BitArray}; use alloy_primitives::{ Address, - map::{AddressMap, HashSet}, + map::{AddressMap, U256Set}, }; /// Short-address optimization cap. Addresses with 18 leading zero bytes whose last two bytes are @@ -68,7 +68,7 @@ pub struct PrewarmSet { /// [`Self::warm`] and [`Self::warm_storage`]: they are warmed /// before or alongside execution and survive [`crate::evm::State::rollback`]. An address /// present with an empty slot set is warm with no warm storage slots. - access_list: AddressMap>, + access_list: AddressMap, /// Bit vector of warm short addresses. An address shorter than [`SHORT_ADDRESS_CAP`] sets /// its bit here for faster access; precompiles and other low-numbered addresses land here. short_addresses: ShortAddresses, @@ -262,7 +262,7 @@ mod tests { assert!(prewarm_set.is_warm(&short_addr1)); assert!(prewarm_set.is_warm(&short_addr2)); assert!(prewarm_set.is_warm(&last_short_addr)); - assert!(!prewarm_set.is_warm(&Address::with_last_byte(20))); + assert!(prewarm_set.is_cold(&Address::with_last_byte(20))); assert!(prewarm_set.access_list.is_empty()); } @@ -284,7 +284,7 @@ mod tests { assert!(prewarm_set.is_warm(®ular_addr)); assert!(prewarm_set.is_warm(&boundary_addr)); - assert!(!prewarm_set.is_warm(&address!("0987654321098765432109876543210987654321"))); + assert!(prewarm_set.is_cold(&address!("0987654321098765432109876543210987654321"))); } #[test] @@ -303,7 +303,7 @@ mod tests { prewarm_set.clear(); assert_eq!(prewarm_set, PrewarmSet::new()); - assert!(addresses.iter().all(|address| !prewarm_set.is_warm(address))); + assert!(addresses.iter().all(|address| prewarm_set.is_cold(address))); } #[test] @@ -318,8 +318,8 @@ mod tests { assert!(prewarm_set.is_warm(®ular_addr)); prewarm_set.clear(); - assert!(!prewarm_set.is_warm(&short_addr)); - assert!(!prewarm_set.is_warm(®ular_addr)); + assert!(prewarm_set.is_cold(&short_addr)); + assert!(prewarm_set.is_cold(®ular_addr)); } #[test] @@ -336,9 +336,9 @@ mod tests { prewarm_set.remove(&short); prewarm_set.remove(&inline[0]); prewarm_set.remove(&mapped); - assert!(!prewarm_set.is_warm(&short)); - assert!(!prewarm_set.is_warm(&inline[0])); - assert!(!prewarm_set.is_warm(&mapped)); + assert!(prewarm_set.is_cold(&short)); + assert!(prewarm_set.is_cold(&inline[0])); + assert!(prewarm_set.is_cold(&mapped)); assert!(!prewarm_set.is_storage_warm(&mapped, &key)); assert!(prewarm_set.is_warm(&inline[1]) && prewarm_set.is_warm(&inline[2])); @@ -355,7 +355,7 @@ mod tests { prewarm_set.remove(&inline[0]); prewarm_set.remove(&short); for address in [inline[0], short] { - assert!(!prewarm_set.is_warm(&address)); + assert!(prewarm_set.is_cold(&address)); assert!(!prewarm_set.is_storage_warm(&address, &key)); } } diff --git a/crates/evm2/src/evm/registry.rs b/crates/evm2/src/evm/registry.rs index 196e20c56..990662018 100644 --- a/crates/evm2/src/evm/registry.rs +++ b/crates/evm2/src/evm/registry.rs @@ -65,6 +65,14 @@ pub enum HandlerError { /// Transaction gas limit. got: u64, }, + /// Transaction gas limit is lower than the calldata floor gas (EIP-7623). + #[error("gas limit below floor gas: required {required}, got {got}")] + GasLimitBelowFloor { + /// Required floor gas. + required: u64, + /// Transaction gas limit. + got: u64, + }, /// Sender cannot pay value plus maximum gas cost. #[error("lack of funds ({balance}) for max fee ({fee})")] InsufficientFunds { @@ -95,6 +103,19 @@ pub enum HandlerError { /// Active transaction gas limit cap. cap: u64, }, + /// Intrinsic execution gas, or the calldata floor gas if larger, exceeds the active execution + /// gas cap (EIP-8037). + /// + /// Unlike [`TxGasLimitGreaterThanCap`](Self::TxGasLimitGreaterThanCap), the transaction's own + /// gas limit is not what exceeds a cap: it may exceed the execution cap, but the gas it must + /// spend before execution cannot. + #[error("intrinsic gas exceeds cap: required {required}, cap {cap}")] + IntrinsicGasGreaterThanCap { + /// Required execution gas: the larger of the intrinsic and floor gas. + required: u64, + /// Active execution gas cap. + cap: u64, + }, /// Create transaction initcode exceeds the active size limit. #[error("create initcode size limit exceeded: limit {limit}, got {got}")] CreateInitCodeSizeLimit { @@ -422,7 +443,7 @@ impl TxRegistry { mod tests { use super::*; use crate::{ - BaseEvmConfigSelector, EvmFeatures, EvmTypesHost, ExecutionConfig, SpecId, + BaseEvmConfigSelector, EvmTypesHost, ExecutionConfig, SpecId, env::{BlockEnv, BlockEnvExt, TxEnv}, evm::{AccountLoad, SLoad, SStore, SelfDestructResult}, interpreter::{Host, Message, MessageResult, Word}, @@ -489,6 +510,14 @@ mod tests { &self.block } + fn tx_env(&mut self) -> &TxEnv { + unimplemented!() + } + + fn initial_call_depth(&self) -> u16 { + unimplemented!() + } + fn load_account( &mut self, _address: &Address, @@ -498,14 +527,6 @@ mod tests { unimplemented!() } - fn target_is_empty_for_new_account_gas( - &mut self, - _address: &Address, - _features: EvmFeatures, - ) -> Result { - unimplemented!() - } - fn block_hash(&mut self, _number: &Word) -> Result { unimplemented!() } @@ -543,7 +564,6 @@ mod tests { fn execute_message( &mut self, - _tx_env: &TxEnv, _message: &mut Message, ) -> Result, crate::ExecutionError> { unimplemented!() diff --git a/crates/evm2/src/evm/state/account.rs b/crates/evm2/src/evm/state/account.rs index 968b8b326..90e8c6d04 100644 --- a/crates/evm2/src/evm/state/account.rs +++ b/crates/evm2/src/evm/state/account.rs @@ -71,15 +71,7 @@ impl core::hash::Hash for AccountInfo { impl Default for AccountInfo { #[inline] fn default() -> Self { - Self { - balance: U256::ZERO, - nonce: 0, - code_hash: KECCAK256_EMPTY, - code: Some(Bytecode::default()), - #[cfg(feature = "account-ext")] - extension: super::AccountExtension::new(), - _non_exhaustive: (), - } + Self::new(U256::ZERO, 0, KECCAK256_EMPTY, Some(Bytecode::default())) } } @@ -172,20 +164,17 @@ bitflags! { pub struct AccountFlags: u8 { /// The account is warm in the current transaction. const WARM = 1 << 0; - /// The account is touched for transaction-finalization account-lifetime rules. + /// The account is touched for transaction-finalization account-lifetime rules. Kept + /// through finalization. const TOUCHED = 1 << 1; - /// Touch metadata retained after finalization consumes [`Self::TOUCHED`]. - const FINALIZED_TOUCHED = 1 << 2; - /// The account has been self-destructed in the current transaction. + /// The account has been self-destructed in the current transaction. Kept through + /// finalization, also on an EIP-8246 survivor. const DESTROYED = 1 << 3; /// The present overlay account was created in the current transaction. const CREATED = 1 << 4; - /// A created account keeps its storage baseline, see - /// [`AccountHandle::mark_created_keeping_storage`]. Meaningful only while - /// [`Self::CREATED`] is set. - const KEEPS_STORAGE = 1 << 5; - /// The present overlay account's code has been modified. - const CODE_CHANGED = 1 << 6; + /// The present overlay account's creation discarded its old storage, so unloaded slots + /// read as zero. + const DISCARDS_STORAGE = 1 << 5; } } @@ -197,9 +186,10 @@ bitflags! { /// [`State`](super::State) emit the transaction's account transition without re-reading the backing /// database. `present` is `None` for an account that was loaded as non-existent or deleted. /// -/// [`AccountFlags::CREATED`] and [`AccountFlags::CODE_CHANGED`] track creation and -/// code-modification state of the present overlay account, driving transaction-finalization and -/// change-emission rules. They are meaningful only while `present` is `Some`. +/// [`AccountFlags::CREATED`] tracks creation in the current transaction and drives +/// transaction-finalization rules. It survives selfdestruct finalization, so it can stay set while +/// `present` is `None`. [`AccountFlags::DISCARDS_STORAGE`] marks a creation that discarded the +/// account's old storage. /// /// Detached from the EVM as part of a [`PendingState`](super::PendingState), the entry describes /// the account's transaction transition: `original` against `present`, with @@ -210,8 +200,7 @@ pub(crate) struct Account { pub(crate) original: Option, /// Present account overlay after mutations. `None` means the account is absent/deleted. pub(crate) present: Option, - /// Lifecycle flags: warm, touched, finalized-touched, destroyed, created, keeps-storage, and - /// code changed. + /// Lifecycle flags: warm, touched, destroyed, created, and discards-storage. pub(crate) flags: AccountFlags, /// Persistent transaction storage owned by this account. pub(crate) storage: StorageOverlay, @@ -224,24 +213,17 @@ impl Account { Self { original, present, ..Self::default() } } - /// Applies isolated execution changes, retaining parent originals and account warmth. - #[cfg(test)] - pub(crate) fn merge_isolated(&mut self, child: Self) { - self.present = child.present; - self.flags |= child.flags - AccountFlags::WARM - AccountFlags::FINALIZED_TOUCHED; - self.storage.merge_isolated(child.storage); + /// Marks the account as created during the transaction. + #[inline] + pub(crate) const fn mark_created(&mut self) { + self.flags = self.flags.union(AccountFlags::CREATED); } - /// Marks the account as created during the transaction, which also flags its code as changed. - /// - /// The first creation mark decides whether the account keeps its storage baseline. + /// Marks the account as created and discards its old storage. #[inline] - pub(crate) const fn mark_created(&mut self) { - let mut flags = self.flags; - if !flags.contains(AccountFlags::CREATED) { - flags = flags.difference(AccountFlags::KEEPS_STORAGE); - } - self.flags = flags.union(AccountFlags::CREATED.union(AccountFlags::CODE_CHANGED)); + pub(crate) const fn mark_created_discarding_storage(&mut self) { + self.mark_created(); + self.flags = self.flags.union(AccountFlags::DISCARDS_STORAGE); } /// Returns whether the account was created during the transaction. @@ -257,8 +239,7 @@ impl Account { /// slots read as zero and committing it drops cached slots. #[inline] pub(crate) const fn discards_storage(&self) -> bool { - self.flags.contains(AccountFlags::CREATED) - && !self.flags.contains(AccountFlags::KEEPS_STORAGE) + self.flags.contains(AccountFlags::DISCARDS_STORAGE) } /// Returns whether the account's info changed during the transaction. @@ -273,7 +254,6 @@ impl Account { #[inline] pub(crate) fn is_existing_dead(&self) -> bool { self.present.as_ref().is_some_and(AccountInfo::is_empty) - || (self.present.is_none() && self.original.is_some()) } /// Returns the account's new bytecode keyed by code hash when its code changed during the @@ -283,23 +263,20 @@ impl Account { let account = self.present.as_ref()?; let code = account.code.as_ref()?; let code_hash = account.code_hash; - ((self.flags.contains(AccountFlags::CODE_CHANGED) - || self.original.as_ref().map(|original| original.code_hash) != Some(code_hash)) - && !code.is_empty() - && !code_hash.is_zero() - && !account.is_empty_code_hash()) + (self.original.as_ref().map(|original| original.code_hash) != Some(code_hash) + && !code.is_empty()) .then_some((code_hash, code)) } /// Returns this account's transaction changes. - pub(super) fn changes(&self, address: Address, selfdestructed: bool) -> AccountChanges<'_> { + pub(super) fn changes(&self, address: Address) -> AccountChanges<'_> { AccountChanges { address, original: self.original.as_ref(), current: self.present.as_ref(), created: self.is_created(), - selfdestructed, - touched: self.flags.contains(AccountFlags::FINALIZED_TOUCHED), + selfdestructed: self.flags.contains(AccountFlags::DESTROYED), + touched: self.flags.contains(AccountFlags::TOUCHED), code: self.changed_code(), storage: &self.storage, } @@ -314,7 +291,7 @@ impl Account { /// cannot drift apart, mirroring revm's `AccountHandle`. /// /// The first mutation made through the handle captures a snapshot of the overlay entry (present -/// value plus the warm/touched/destroyed/created/code-changed flags); on drop the handle flushes +/// value plus the warm/touched/destroyed/created flags); on drop the handle flushes /// that snapshot as a single [`JournalEntry::AccountChange`], which /// [`State::rollback`](super::State::rollback) replays to restore the whole entry at once. A handle /// used only for reads records nothing, so it emits no journal entry. @@ -405,6 +382,17 @@ impl<'a, 'db> AccountHandle<'a, 'db> { self.tracked.present.as_ref() } + /// Returns the account information at the transaction boundary, before any frame mutated it. + /// + /// `None` means the account did not exist at the start of the transaction. Loading bytecode + /// into the present overlay does not change this baseline; committing and starting another + /// transaction does. Use [`Self::original_code`] to load the account's start-of-transaction + /// bytecode. + #[inline] + pub const fn original(&self) -> Option<&AccountInfo> { + self.tracked.original.as_ref() + } + /// Returns whether the account currently exists in the overlay. #[inline] pub const fn exists(&self) -> bool { @@ -447,8 +435,8 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// Returns whether the account has been marked self-destructed in the current transaction. #[inline] - pub fn is_destructed(&self) -> bool { - self.inner.selfdestructs.contains(&self.address) + pub const fn is_destructed(&self) -> bool { + self.tracked.flags.contains(AccountFlags::DESTROYED) } /// Returns whether the account is touched for transaction-finalization account-lifetime rules. @@ -460,14 +448,13 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// Returns whether the account was created in the current transaction. #[inline] pub const fn is_created(&self) -> bool { - self.tracked.present.is_some() && self.tracked.is_created() + self.exists() && self.tracked.is_created() } /// Returns whether the account is dead by the EIP-161 definition while existing in the overlay. /// - /// An account is existing-dead when it has zero nonce, zero balance, and empty code, or when it - /// was deleted in this transaction but existed at the transaction boundary. Spurious Dragon - /// deletes such touched accounts during transaction finalization. + /// An account is existing-dead when it has zero nonce, zero balance, and empty code. Spurious + /// Dragon deletes such touched accounts during transaction finalization. #[inline] pub fn is_existing_dead(&self) -> bool { self.tracked.is_existing_dead() @@ -482,7 +469,7 @@ impl<'a, 'db> AccountHandle<'a, 'db> { if features.contains(EvmFeatures::EIP161) { return self.tracked.present.as_ref().is_none_or(AccountInfo::is_empty); } - self.tracked.present.is_none() && !self.tracked.flags.contains(AccountFlags::TOUCHED) + self.tracked.present.is_none() && !self.is_touched() } /// Loads the account's bytecode, reading it from the backing database by code hash when it is @@ -497,13 +484,13 @@ impl<'a, 'db> AccountHandle<'a, 'db> { if account.is_empty_code_hash() { return Ok(Bytecode::default()); } - if let Some(code) = account.code.as_ref() - && !code.is_empty() - { - return Ok(code.clone()); - } let code_hash = account.code_hash; - self.inner.database.get_code_by_hash(&code_hash) + let code = match account.code.as_ref() { + Some(code) if !code.is_empty() => code.clone(), + _ => self.inner.database.get_code_by_hash(&code_hash)?, + }; + code.seed_hash(code_hash); + Ok(code) } /// Loads the account's bytecode as it stood at the start of the transaction. @@ -520,13 +507,13 @@ impl<'a, 'db> AccountHandle<'a, 'db> { if account.is_empty_code_hash() { return Ok(Bytecode::default()); } - if let Some(code) = account.code.as_ref() - && !code.is_empty() - { - return Ok(code.clone()); - } let code_hash = account.code_hash; - self.inner.database.get_code_by_hash(&code_hash) + let code = match account.code.as_ref() { + Some(code) if !code.is_empty() => code.clone(), + _ => self.inner.database.get_code_by_hash(&code_hash)?, + }; + code.seed_hash(code_hash); + Ok(code) } /// Touches the account, recording a revert snapshot the first time it is mutated. @@ -535,7 +522,7 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// finalization even when no field changes. #[inline] pub fn touch(&mut self) { - if !self.tracked.flags.contains(AccountFlags::TOUCHED) { + if !self.is_touched() { self.record_change(); self.tracked.flags.insert(AccountFlags::TOUCHED); } @@ -544,14 +531,13 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// Marks the account self-destructed in the current transaction, recording a revert snapshot /// the first time and touching the account. /// - /// Touching makes the account participate in EIP-158/161 cleanup, and the self-destruct set - /// membership is undone by [`State::rollback`](super::State::rollback). + /// Touching makes the account participate in EIP-158/161 cleanup, and + /// [`State::rollback`](super::State::rollback) restores the destroyed flag. #[inline] pub fn mark_destructed(&mut self) { if !self.tracked.flags.contains(AccountFlags::DESTROYED) { self.record_change(); self.tracked.flags.insert(AccountFlags::DESTROYED); - self.inner.selfdestructs.insert(self.address); } self.touch(); } @@ -563,9 +549,7 @@ impl<'a, 'db> AccountHandle<'a, 'db> { /// base warm set stay warm across rollback, so warming them again records nothing. #[inline] pub fn warm(&mut self) -> bool { - if self.tracked.flags.contains(AccountFlags::WARM) - || self.inner.prewarm_set.is_warm(&self.address) - { + if self.is_warm() { return false; } self.record_change(); @@ -623,7 +607,6 @@ impl<'a, 'db> AccountHandle<'a, 'db> { let account = self.present_mut(); account.code_hash = code_hash; account.code = Some(code); - self.tracked.flags.insert(AccountFlags::CODE_CHANGED); } /// Sets the account code, computing its hash, touching the account and recording a revert @@ -657,12 +640,18 @@ impl<'a, 'db> AccountHandle<'a, 'db> { self.tracked.present.get_or_insert_with(empty_account) } - /// Marks the present overlay account as created in the current transaction, also flagging its - /// code as changed. The creation/code-change status is reverted together with the rest of the - /// overlay by the [`JournalEntry::AccountChange`] snapshot recorded for this handle. + /// Marks the present overlay account as created in the current transaction. The creation + /// status is reverted together with the rest of the overlay by the + /// [`JournalEntry::AccountChange`] snapshot recorded for this handle. /// /// Created accounts follow the EIP-6780 rules for `SELFDESTRUCT` and are emitted as created by - /// [`PendingState`](super::PendingState). + /// [`PendingState`](super::PendingState). The mark does not discard the account's old storage: + /// unloaded slots still read from the database and committing the account keeps its cached + /// slots. This suits an embedder that continues a transaction on a fresh overlay whose database + /// already holds the account's creation and storage writes, such as after suspending the + /// transaction. Slots then load with the database values as their transaction originals, so + /// `SSTORE` gas and refunds can differ from an uninterrupted run, where a created account's + /// originals are zero. /// /// The account must be present, for example through [`Self::get_or_insert`]. #[inline] @@ -671,25 +660,12 @@ impl<'a, 'db> AccountHandle<'a, 'db> { self.tracked.mark_created(); } - /// Marks the present overlay account as created in the current transaction, like - /// [`Self::mark_created`], but keeps its storage baseline: unloaded slots read from the - /// database and committing the account does not drop its cached slots. - /// - /// For embedders that continue a transaction on a fresh overlay whose database already holds - /// the account's creation and storage writes, such as after suspending the transaction. The - /// account stays eligible for EIP-6780 `SELFDESTRUCT`. An account already created in this - /// overlay keeps reading its old storage as zero. - /// - /// Slots load with the database values as their transaction originals, so `SSTORE` gas and - /// refunds can differ from an uninterrupted run, where a created account's originals are zero. - /// This matches resuming any other account on a fresh overlay. + /// Marks the account as created like [`Self::mark_created`] and discards its old storage, so + /// unloaded slots read as zero. #[inline] - pub fn mark_created_keeping_storage(&mut self) { - let first = !self.tracked.is_created(); - self.mark_created(); - if first { - self.tracked.flags.insert(AccountFlags::KEEPS_STORAGE); - } + pub(super) fn mark_created_discarding_storage(&mut self) { + self.record_change(); + self.tracked.mark_created_discarding_storage(); } /// Records a revert snapshot and returns the live account, materializing an empty one when it @@ -727,8 +703,7 @@ impl<'a, 'db> AccountHandle<'a, 'db> { #[inline] pub(crate) fn reset_selfdestructed_for_finalization(&mut self) { let balance = self.balance(); - self.tracked.flags.remove(AccountFlags::DESTROYED); - self.tracked.present = Some(AccountInfo::default().with_balance(balance)); + self.tracked.present = Some(empty_account().with_balance(balance)); } /// Overrides the live balance and the balance restored by this handle's revert snapshot, @@ -759,14 +734,17 @@ impl<'a, 'db> AccountHandle<'a, 'db> { #[cfg(test)] mod tests { use super::*; - use crate::evm::{CacheDB, state::State}; - use alloy_primitives::Address; + use crate::{ + LoadError, SpecId, Version, + evm::{CacheDB, state::State}, + }; + use alloy_primitives::{Address, Bytes}; #[test] fn transfer_to_self_requires_balance() { - let address = Address::from([0x77; 20]); + let address = Address::repeat_byte(0x77); let mut database = CacheDB::default(); - database.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(3))); + database.insert_account_info(&address, empty_account().with_balance(Word::from(3))); let mut state = State::new(database); assert!(!state.transfer(&address, &address, &Word::from(4)).unwrap()); @@ -775,9 +753,7 @@ mod tests { #[test] fn journaled_account_mutations_journal_and_roll_back() { - use crate::{SpecId, Version, bytecode::Bytecode}; - - let address = Address::from([0x88; 20]); + let address = Address::repeat_byte(0x88); let mut state = State::new(CacheDB::default()); let checkpoint = state.checkpoint(); @@ -811,9 +787,7 @@ mod tests { #[test] fn account_info_set_code_is_published_on_commit() { - use alloy_primitives::Bytes; - - let address = Address::from([0x8c; 20]); + let address = Address::repeat_byte(0x8c); let code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); let mut state = State::new(CacheDB::default()); @@ -825,20 +799,103 @@ mod tests { } #[test] - fn journaled_existing_account_field_changes_roll_back_granularly() { - use crate::{SpecId, Version, bytecode::Bytecode}; - use alloy_primitives::Bytes; + fn changed_code_requires_new_non_empty_code_hash() { + let code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + let with_code = empty_account().with_code(code.clone()); + let other = empty_account().with_code(Bytecode::new_raw(Bytes::from_static(&[0]))); + + let created = Account::new(None, Some(with_code.clone())); + assert_eq!(created.changed_code(), Some((with_code.code_hash, &code))); + + let replaced = Account::new(Some(other), Some(with_code.clone())); + assert_eq!(replaced.changed_code(), Some((with_code.code_hash, &code))); - let address = Address::from([0x8b; 20]); + assert_eq!( + Account::new(Some(with_code.clone()), Some(with_code.clone())).changed_code(), + None + ); + assert_eq!(Account::new(Some(with_code), Some(empty_account())).changed_code(), None); + assert_eq!(Account::new(None, Some(empty_account())).changed_code(), None); + } + + #[test] + fn loaded_code_is_seeded_with_account_code_hash() { + // A hash that is not the keccak of the code proves `hash_slow` returns the seeded value + // instead of hashing. + let seed = B256::repeat_byte(0x5e); + let address = Address::repeat_byte(0x8d); + let fresh = || Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + assert_ne!(fresh().hash_slow(), seed); + + let mut database = CacheDB::default(); + database + .insert_account_info(&address, AccountInfo::new(Word::ZERO, 1, seed, Some(fresh()))); + let mut state = State::new(database); + + assert_eq!(state.account(&address).unwrap().load_code().unwrap().hash_slow(), seed); + assert_eq!(state.account(&address).unwrap().original_code().unwrap().hash_slow(), seed); + + let overlay_address = Address::repeat_byte(0x8e); + state.account(&overlay_address).unwrap().set_code(seed, fresh()); + assert_eq!(state.account(&overlay_address).unwrap().load_code().unwrap().hash_slow(), seed); + } + + #[test] + fn account_original_reports_transaction_baseline() { + let address = Address::repeat_byte(0x93); + let code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); + let original = + empty_account().with_balance(Word::from(10)).with_nonce(3).with_code(code.clone()); + let mut database = CacheDB::default(); + database.insert_account_info(&address, original.clone()); + let mut state = State::new(database); + + // An unchanged account reports its start-of-transaction info. + { + let account = state.account(&address).unwrap(); + assert_eq!(account.original(), Some(&original)); + assert_eq!(account.get(), Some(&original)); + } + + // Mutations move the present overlay but leave the baseline alone. + { + let mut account = state.account(&address).unwrap(); + account.set_balance(Word::from(99)); + account.set_nonce(4); + assert_eq!(account.original(), Some(&original)); + assert_eq!(account.balance(), Word::from(99)); + assert_eq!(account.nonce(), 4); + } + + // Loading bytecode into the present cache does not move the baseline either. + { + let mut account = state.account(&address).unwrap(); + assert_eq!(account.load_code().unwrap(), code); + assert_eq!(account.original(), Some(&original)); + } + + // An account absent at the transaction boundary keeps no baseline, including after the + // transaction creates it. + let created = Address::repeat_byte(0x94); + { + let mut account = state.account(&created).unwrap(); + assert_eq!(account.original(), None); + account.get_or_insert().balance = Word::ONE; + account.mark_created(); + assert_eq!(account.original(), None); + assert_eq!(account.balance(), Word::ONE); + } + } + + #[test] + fn journaled_existing_account_field_changes_roll_back_granularly() { + let address = Address::repeat_byte(0x8b); let original_code = Bytecode::new_raw(Bytes::from_static(&[0x60, 0x01])); let original_code_hash = original_code.hash_slow(); let mut database = CacheDB::default(); database.insert_account_info( &address, - AccountInfo::default() - .with_balance(Word::from(10)) - .with_nonce(2) - .with_code(original_code), + empty_account().with_balance(Word::from(10)).with_nonce(2).with_code(original_code), ); let mut state = State::new(database); @@ -868,9 +925,9 @@ mod tests { #[test] fn journaled_account_read_only_handle_journals_nothing() { - let address = Address::from([0x89; 20]); + let address = Address::repeat_byte(0x89); let mut database = CacheDB::default(); - database.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(5))); + database.insert_account_info(&address, empty_account().with_balance(Word::from(5))); let mut state = State::new(database); let checkpoint = state.checkpoint(); @@ -886,11 +943,9 @@ mod tests { #[test] fn journaled_account_skip_cold_load_signals_skip() { - use crate::LoadError; - - let address = Address::from([0x8a; 20]); + let address = Address::repeat_byte(0x8a); let mut database = CacheDB::default(); - database.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(5))); + database.insert_account_info(&address, empty_account().with_balance(Word::from(5))); let mut state = State::new(database); // A cold, not-yet-loaded account signals the skip instead of reading the database. @@ -908,7 +963,7 @@ mod tests { #[test] fn account_overrides_update_only_selected_snapshot_fields() { let address = Address::with_last_byte(0x90); - let original = AccountInfo::default().with_balance(Word::from(100)).with_nonce(7); + let original = empty_account().with_balance(Word::from(100)).with_nonce(7); for (override_balance, override_nonce) in [(true, false), (false, true), (true, true)] { let mut db = CacheDB::default(); db.insert_account_info(&address, original.clone()); @@ -951,7 +1006,6 @@ mod tests { assert!(!account.is_warm()); assert!(!account.is_created()); assert!(!account.is_destructed()); - assert!(!account.tracked.flags.contains(AccountFlags::CODE_CHANGED)); } } @@ -974,7 +1028,7 @@ mod tests { assert_eq!((account.balance(), account.nonce()), (balance, nonce)); assert!(!account.is_touched()); assert!(!account.is_warm()); - account.set_balance(Word::from(1)); + account.set_balance(Word::ONE); account.set_nonce(1); } assert_eq!(state.checkpoint().journal_len(), checkpoint.journal_len() + 1); @@ -992,7 +1046,7 @@ mod tests { #[test] fn account_overrides_preserve_earlier_handles_snapshots() { let address = Address::with_last_byte(0x92); - let original = AccountInfo::default().with_balance(Word::from(100)).with_nonce(7); + let original = empty_account().with_balance(Word::from(100)).with_nonce(7); let mut db = CacheDB::default(); db.insert_account_info(&address, original.clone()); let mut state = State::new(db); diff --git a/crates/evm2/src/evm/state/block.rs b/crates/evm2/src/evm/state/block.rs index 75ce25ab3..e0c79b6f8 100644 --- a/crates/evm2/src/evm/state/block.rs +++ b/crates/evm2/src/evm/state/block.rs @@ -46,9 +46,13 @@ impl BlockStateAccumulator { } /// Returns storage-wipe addresses in arbitrary set order. + /// + /// An account deleted by the block is not listed: its deletion already wipes its storage. #[inline] pub fn storage_wipes(&self) -> impl Iterator + '_ { - self.storage_wipes.iter().copied() + self.storage_wipes.iter().copied().filter(|address| { + self.accounts.get(address).is_none_or(|delta| delta.current.is_some()) + }) } /// Returns storage deltas with storage keys in arbitrary map order. @@ -120,13 +124,6 @@ impl StateChangeSink for BlockStateAccumulator { match self.accounts.entry(change.address) { hash_map::Entry::Occupied(mut entry) => { let delta = entry.get_mut(); - // Reviving an account deleted earlier in the block re-records the deletion's - // implied storage wipe: the deletion dropped the wipe marker because applying - // `current == None` wipes storage at the sink, but applying a live account does - // not, so pre-block storage would otherwise leak through the revival. - if delta.current.is_none() && current.is_some() && delta.original.is_some() { - self.storage_wipes.insert(change.address); - } delta.set_current(current); if !delta.is_changed() { entry.remove(); @@ -141,7 +138,13 @@ impl StateChangeSink for BlockStateAccumulator { } if deletes_account { - self.storage_wipes.remove(&change.address); + // A delta left after a deletion means the account existed before the block, so its + // pre-block storage is destroyed. The marker stays so a later revival still wipes it. + if self.accounts.contains_key(&change.address) { + self.storage_wipes.insert(change.address); + } else { + self.storage_wipes.remove(&change.address); + } self.storage.remove(&change.address); } else if created { // Only a newly created delta can have slots recorded against pre-block storage: while @@ -203,13 +206,19 @@ impl StateChangeSink for BlockStateAccumulator { impl StateChangeSource for BlockStateAccumulator { #[inline] fn visit(&self, sink: &mut S) -> Result<(), S::Error> { - visit_block_changes(&self.accounts, &self.storage_wipes, &self.storage, &self.code, sink) + visit_block_changes( + &self.accounts, + &self.storage_wipes_sorted(), + &self.storage, + &self.code, + sink, + ) } } fn visit_block_changes( accounts: &AddressMap>>, - storage_wipes: &AddressSet, + storage_wipes: &[Address], storage: &AddressMap>>, code: &B256Map, sink: &mut S, @@ -220,10 +229,8 @@ fn visit_block_changes( sink.bytecode(code_hash, code)?; } - let mut storage_wipes = storage_wipes.iter().copied().collect::>(); - storage_wipes.sort_unstable(); - for address in &storage_wipes { - sink.storage_wipe(*address)?; + for &address in storage_wipes { + sink.storage_wipe(address)?; } let mut storage_accounts = storage.iter().collect::>(); @@ -261,26 +268,22 @@ fn visit_block_changes( mod tests { use super::{ super::{ - Account, AccountInfo, PendingState, StateChangeSource, StorageOverlay, StorageSlot, - Tracked, + Account, AccountChangeRef, AccountInfo, PendingState, StateChangeSource, StorageChange, + StorageOverlay, StorageSlot, Tracked, }, BlockStateAccumulator, }; - use crate::interpreter::Word; + use crate::{interpreter::Word, test_utils::storage_slot}; use alloy_primitives::{Address, map::U256Map}; + use core::convert::Infallible; use super::super::StateChangeSink; - #[cfg(feature = "serde")] use crate::bytecode::Bytecode; use alloc::{vec, vec::Vec}; - #[cfg(feature = "serde")] - use alloy_primitives::B256; + use alloy_primitives::{B256, Bytes}; #[test] fn code_sorted_orders_by_hash() { - use crate::bytecode::Bytecode; - use alloy_primitives::{B256, Bytes}; - let mut block = BlockStateAccumulator::new(); // Inserted descending, so map order cannot be mistaken for sorted order. for byte in [0xcc_u8, 0x11, 0x77] { @@ -318,14 +321,7 @@ mod tests { } fn slot(key: Word, original: Word, current: Word) -> U256Map { - U256Map::from_iter([( - key, - StorageSlot { - value: Tracked::from_parts(original, current), - is_warm: false, - _non_exhaustive: (), - }, - )]) + U256Map::from_iter([(key, storage_slot(original, current))]) } #[cfg(feature = "serde")] @@ -344,8 +340,8 @@ mod tests { #[test] fn block_accumulator_collapses_create_then_delete() { - let address = Address::from([0x50; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x50); + let key = Word::ONE; let created = AccountInfo::default().with_nonce(1); let mut accumulator = BlockStateAccumulator::new(); @@ -368,8 +364,8 @@ mod tests { #[test] fn block_accumulator_preserves_original_for_delete_then_recreate() { - let address = Address::from([0x51; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x51); + let key = Word::ONE; let original = AccountInfo::default().with_balance(Word::from(3)); let recreated = AccountInfo::default().with_nonce(1); let mut accumulator = BlockStateAccumulator::new(); @@ -398,28 +394,99 @@ mod tests { assert_eq!(storage[0].1.current, Word::from(7)); } + /// A change a sink observed, in arrival order. + #[derive(Debug, Eq, PartialEq)] + enum Event { + Wipe(Address), + Storage { address: Address, key: Word, original: Word, current: Word }, + Account { address: Address, original: Option, current: Option }, + } + + #[derive(Default)] + struct Recorder(Vec); + + impl StateChangeSink for Recorder { + type Error = Infallible; + + fn account(&mut self, change: AccountChangeRef<'_>) -> Result<(), Self::Error> { + self.0.push(Event::Account { + address: change.address, + original: change.original.cloned(), + current: change.current.cloned(), + }); + Ok(()) + } + + fn storage_wipe(&mut self, address: Address) -> Result<(), Self::Error> { + self.0.push(Event::Wipe(address)); + Ok(()) + } + + fn storage(&mut self, change: StorageChange) -> Result<(), Self::Error> { + self.0.push(Event::Storage { + address: change.address, + key: change.key, + original: change.original, + current: change.current, + }); + Ok(()) + } + } + + fn emitted(accumulator: &BlockStateAccumulator) -> Vec { + let mut recorder = Recorder::default(); + accumulator.visit(&mut recorder).expect("recorder is infallible"); + recorder.0 + } + #[test] - fn block_accumulator_restores_wipe_when_deleted_account_is_revived() { - // Selfdestruct followed by a revival in a later transaction of the same block (a plain - // transfer or a re-create) must keep the deletion's storage wipe: the revived account is - // applied as a live account, which does not wipe storage at the sink by itself. - let address = Address::from([0x55; 20]); + fn block_accumulator_emits_only_the_deletion_for_deleted_account() { + // The deletion wipes the account's storage at a sink by itself, so a sink sees no + // separate wipe even though the accumulator keeps the marker for a later revival. + let address = Address::from([0x58; 20]); let original = AccountInfo::default().with_balance(Word::from(3)); - let revived = AccountInfo::default().with_balance(Word::from(1)); let mut accumulator = BlockStateAccumulator::new(); - let delete = changes(address, Some(original.clone()), None, true, U256Map::default()); - delete.visit(&mut accumulator).expect("block accumulator is infallible"); - assert!(accumulator.storage_wipes_sorted().is_empty(), "deletion subsumes the wipe"); + changes(address, Some(original.clone()), None, true, U256Map::default()) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + + assert!(accumulator.storage_wipes_sorted().is_empty()); + assert_eq!( + emitted(&accumulator), + [Event::Account { address, original: Some(original), current: None }] + ); + } - let revive = changes(address, None, Some(revived.clone()), false, U256Map::default()); - revive.visit(&mut accumulator).expect("block accumulator is infallible"); + #[test] + fn block_accumulator_keeps_wipe_when_deleted_account_is_revived() { + // Selfdestruct followed by a revival in a later transaction of the same block (a plain + // transfer or a re-create): the revived account is a live account, which does not wipe + // storage at a sink by itself, so the sink must still see the wipe. + let address = Address::repeat_byte(0x55); + let original = AccountInfo::default().with_balance(Word::from(3)); + let revived = AccountInfo::default().with_balance(Word::ONE); + let mut accumulator = BlockStateAccumulator::new(); + + changes(address, Some(original.clone()), None, true, U256Map::default()) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + changes(address, None, Some(revived.clone()), false, U256Map::default()) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); - let accounts = accumulator.accounts_sorted(); - assert_eq!(accounts.len(), 1); - assert_eq!(accounts[0].1.original.as_ref(), Some(&original)); - assert_eq!(accounts[0].1.current.as_ref(), Some(&revived)); assert_eq!(accumulator.storage_wipes_sorted(), [address]); + assert_eq!( + emitted(&accumulator), + [ + Event::Wipe(address), + Event::Account { + address, + original: Some(original.clone()), + current: Some(revived) + }, + ] + ); // Reviving back to a value equal to the original collapses the account delta, but the // wipe still stands: the account's pre-block storage was destroyed. @@ -430,14 +497,59 @@ mod tests { changes(address, None, Some(original), false, U256Map::default()) .visit(&mut collapsing) .expect("block accumulator is infallible"); - assert!(collapsing.accounts_sorted().is_empty()); - assert_eq!(collapsing.storage_wipes_sorted(), [address]); + assert_eq!(emitted(&collapsing), [Event::Wipe(address)]); + } + + #[test] + fn block_accumulator_emits_wipe_and_new_slots_when_deleted_account_is_recreated() { + let address = Address::from([0x59; 20]); + let (kept, rewritten) = (Word::from(1), Word::from(2)); + let original = AccountInfo::default().with_balance(Word::from(3)); + let recreated = AccountInfo::default().with_nonce(1); + let mut accumulator = BlockStateAccumulator::new(); + + // A pre-block slot changes, then the account is deleted, then re-created with a new slot. + changes( + address, + Some(original.clone()), + Some(original.clone()), + false, + slot(kept, Word::from(5), Word::from(6)), + ) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + changes(address, Some(original.clone()), None, true, U256Map::default()) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + changes( + address, + None, + Some(recreated.clone()), + true, + slot(rewritten, Word::ZERO, Word::from(7)), + ) + .visit(&mut accumulator) + .expect("block accumulator is infallible"); + + assert_eq!( + emitted(&accumulator), + [ + Event::Wipe(address), + Event::Storage { + address, + key: rewritten, + original: Word::ZERO, + current: Word::from(7), + }, + Event::Account { address, original: Some(original), current: Some(recreated) }, + ] + ); } #[test] fn block_accumulator_keeps_nonzero_write_after_storage_wipe() { - let address = Address::from([0x52; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x52); + let key = Word::ONE; let original = AccountInfo::default().with_balance(Word::from(3)); let mut accumulator = BlockStateAccumulator::new(); @@ -461,8 +573,8 @@ mod tests { #[test] fn block_accumulator_reinserts_unchanged_slot_after_wiping_created_account() { - let address = Address::from([0x53; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x53); + let key = Word::ONE; let account = AccountInfo::default().with_nonce(1); let mut accumulator = BlockStateAccumulator::new(); @@ -493,8 +605,8 @@ mod tests { #[test] fn block_accumulator_normalizes_storage_when_creation_arrives_after_slots() { - let address = Address::from([0x54; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x54); + let key = Word::ONE; let account = AccountInfo::default().with_nonce(1); let mut accumulator = BlockStateAccumulator::new(); @@ -514,8 +626,8 @@ mod tests { #[test] fn block_accumulator_deletion_subsumes_storage_writes() { - let address = Address::from([0x56; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x56); + let key = Word::ONE; let original = AccountInfo::default().with_balance(Word::from(3)); let mut accumulator = BlockStateAccumulator::new(); @@ -533,8 +645,8 @@ mod tests { #[test] fn block_accumulator_collapses_storage_wipe_write_wipe() { - let address = Address::from([0x52; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x52); + let key = Word::ONE; let mut accumulator = BlockStateAccumulator::new(); let first = changes(address, None, None, true, slot(key, Word::from(5), Word::from(7))); @@ -550,8 +662,8 @@ mod tests { #[test] fn block_accumulator_drops_storage_reverted_to_original() { - let address = Address::from([0x57; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x57); + let key = Word::ONE; let mut accumulator = BlockStateAccumulator::new(); changes(address, None, None, false, slot(key, Word::from(3), Word::from(4))) @@ -566,8 +678,8 @@ mod tests { #[test] fn block_accumulator_isolates_storage_between_accounts() { - let [a, b, c, d, e] = [0x61, 0x62, 0x63, 0x64, 0x65].map(|byte| Address::from([byte; 20])); - let (k1, k2) = (Word::from(1), Word::from(2)); + let [a, b, c, d, e] = [0x61, 0x62, 0x63, 0x64, 0x65].map(Address::repeat_byte); + let (k1, k2) = (Word::ONE, Word::from(2)); let info = AccountInfo::default().with_nonce(1); let w = Word::from; let mut accumulator = BlockStateAccumulator::new(); @@ -630,10 +742,10 @@ mod tests { #[test] fn block_accumulator_keeps_account_only_and_storage_only_changes_separate() { - let account_address = Address::from([0x53; 20]); - let storage_address = Address::from([0x54; 20]); - let key = Word::from(1); - let original = AccountInfo::default().with_balance(Word::from(1)); + let account_address = Address::repeat_byte(0x53); + let storage_address = Address::repeat_byte(0x54); + let key = Word::ONE; + let original = AccountInfo::default().with_balance(Word::ONE); let current = AccountInfo::default().with_balance(Word::from(2)); let mut accumulator = BlockStateAccumulator::new(); diff --git a/crates/evm2/src/evm/state/journal.rs b/crates/evm2/src/evm/state/journal.rs index 4c4742889..64a3b767d 100644 --- a/crates/evm2/src/evm/state/journal.rs +++ b/crates/evm2/src/evm/state/journal.rs @@ -87,11 +87,11 @@ mod tests { }, }; use alloc::vec::Vec; - use alloy_primitives::Log; + use alloy_primitives::{Bytes, Log}; #[test] fn destruct_change_rolls_back_to_checkpoint() { - let address = Address::from([0x33; 20]); + let address = Address::repeat_byte(0x33); let mut state = State::new(CacheDB::default()); let checkpoint = state.checkpoint(); @@ -104,17 +104,11 @@ mod tests { #[test] fn log_rolls_back_to_checkpoint() { - use alloy_primitives::{Bytes, LogData}; - let mut state = State::new(CacheDB::default()); - let kept = Log { - address: Address::from([0x44; 20]), - data: LogData::new_unchecked(Vec::new(), Bytes::from_static(&[0x01])), - }; - let reverted = Log { - address: Address::from([0x55; 20]), - data: LogData::new_unchecked(Vec::new(), Bytes::from_static(&[0x02])), - }; + let kept = + Log::new_unchecked(Address::repeat_byte(0x44), Vec::new(), Bytes::from_static(&[0x01])); + let reverted = + Log::new_unchecked(Address::repeat_byte(0x55), Vec::new(), Bytes::from_static(&[0x02])); state.log(kept.clone()); let checkpoint = state.checkpoint(); @@ -124,10 +118,11 @@ mod tests { state.logs(), &[ kept.clone(), - Log { - address: Address::from([0x55; 20]), - data: LogData::new_unchecked(Vec::new(), Bytes::from_static(&[0x02])), - } + Log::new_unchecked( + Address::repeat_byte(0x55), + Vec::new(), + Bytes::from_static(&[0x02]) + ) ] ); state.rollback(checkpoint, Version::base(SpecId::FRONTIER).features); @@ -156,8 +151,7 @@ mod tests { assert_eq!(state.account(&precompile3).unwrap().is_touched(), touched); assert!(!state.account(&other).unwrap().is_touched()); - // Finalization consumes the touch but keeps it in the reported changes, also when run - // twice. + // Finalization keeps the touch in the reported changes, also when run twice. state.finalize_transaction(Version::base(spec)).unwrap(); state.finalize_transaction(Version::base(spec)).unwrap(); let pending = state.take_pending_state(); @@ -173,7 +167,7 @@ mod tests { let frame_account = Address::with_last_byte(0x11); let base_storage = Address::with_last_byte(0x12); let frame_storage = Address::with_last_byte(0x13); - let key = Word::from(1); + let key = Word::ONE; let mut state = State::new(CacheDB::default()); state.prewarm(&base_account); @@ -199,7 +193,7 @@ mod tests { fn warm_only_entries_do_not_emit_state_changes() { let account = Address::with_last_byte(0x14); let storage_account = Address::with_last_byte(0x15); - let key = Word::from(1); + let key = Word::ONE; let mut state = State::new(CacheDB::default()); state.prewarm(&account); @@ -219,7 +213,7 @@ mod tests { fn rollback_preserves_non_revertible_account_warmth_after_load() { let account = Address::with_last_byte(0x16); let mut database = CacheDB::default(); - database.insert_account_info(&account, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&account, AccountInfo::default().with_balance(Word::ONE)); let mut state = State::new(database); state.prewarm(&account); @@ -238,9 +232,9 @@ mod tests { #[test] fn rollback_preserves_non_revertible_storage_warmth_after_write() { let account = Address::with_last_byte(0x17); - let key = Word::from(1); + let key = Word::ONE; let mut database = CacheDB::default(); - database.insert_account_info(&account, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&account, AccountInfo::default().with_balance(Word::ONE)); let mut state = State::new(database); state.prewarm_storage_slot(&account, key); @@ -256,10 +250,10 @@ mod tests { #[test] fn rollback_reverts_storage_warmth_without_discarding_cached_value() { let account = Address::with_last_byte(0x18); - let key = Word::from(1); + let key = Word::ONE; let value = Word::from(9); let mut database = CacheDB::default(); - database.insert_account_info(&account, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&account, AccountInfo::default().with_balance(Word::ONE)); database.insert_account_storage(&account, &key, &value); let mut state = State::new(database); diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index b1eaff64d..738e7de3e 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -39,7 +39,7 @@ use crate::{ use alloc::{boxed::Box, sync::Arc, vec::Vec}; use alloy_primitives::{ Address, B256, KECCAK256_EMPTY, Log, - map::{AddressMap, AddressSet, hash_map}, + map::{AddressMap, hash_map}, }; use core::{ mem, @@ -74,7 +74,6 @@ pub struct StateSnapshot { prewarm_set: PrewarmSet, journal: Vec, logs: Vec, - selfdestructs: AddressSet, } impl StateSnapshot { @@ -94,7 +93,6 @@ impl StateSnapshot { prewarm_set: self.prewarm_set, journal: self.journal, logs: self.logs, - selfdestructs: self.selfdestructs, }, } } @@ -119,7 +117,6 @@ impl State<'_> { prewarm_set: self.prewarm_set.clone(), journal: self.journal.clone(), logs: self.logs.clone(), - selfdestructs: self.selfdestructs.clone(), } } @@ -151,7 +148,6 @@ impl State<'_> { database, journal: Vec::new(), logs: Vec::new(), - selfdestructs: AddressSet::default(), prewarm_set: PrewarmSet::new(), }, } @@ -189,8 +185,6 @@ pub struct StateInner<'a> { journal: Vec, /// Logs emitted by the current transaction. logs: Vec, - /// Accounts self-destructed in the current transaction. - selfdestructs: AddressSet, /// Pre-warmed set: precompiles, coinbase, and the EIP-2930 access list. prewarm_set: PrewarmSet, } @@ -211,6 +205,25 @@ impl StateInner<'_> { self.journal.set_len(len + 1); } } + + /// Reads a slot absent from the overlay. + /// + /// A slot of a wiped account, or of an account whose creation discarded its old storage, reads + /// zero without a database read, though a strict BAL must still cover it. + #[inline] + fn read_unloaded_slot( + &mut self, + address: &Address, + key: &Word, + reads_zero: bool, + ) -> DbResult { + if reads_zero { + self.database.check_bal_storage(address, key)?; + Ok(Word::ZERO) + } else { + self.database.get_storage(address, key) + } + } } impl<'a> State<'a> { @@ -228,7 +241,6 @@ impl<'a> State<'a> { database: CacheDB::new(initial), journal: Vec::new(), logs: Vec::new(), - selfdestructs: AddressSet::default(), prewarm_set: PrewarmSet::new(), }, } @@ -513,7 +525,7 @@ impl<'a> State<'a> { accounts, storage_pool, transient_storage, - inner: StateInner { prewarm_set, journal, selfdestructs, logs, database: _ }, + inner: StateInner { prewarm_set, journal, logs, database: _ }, .. } = self; for account in accounts.values_mut() { @@ -523,7 +535,6 @@ impl<'a> State<'a> { transient_storage.clear(); prewarm_set.clear(); journal.clear(); - selfdestructs.clear(); logs.clear(); } @@ -568,7 +579,7 @@ impl<'a> State<'a> { let account = entry.into_mut(); if skip_cold && !account.flags.contains(AccountFlags::WARM) - && !inner.prewarm_set.is_warm(address) + && inner.prewarm_set.is_cold(address) { return Err(LoadError::ColdLoadSkipped); } @@ -665,19 +676,41 @@ impl<'a> State<'a> { /// This is the storage-side mirror of [`Self::account_info_untracked`]: a non-loading peek that /// returns the overlay slot value when one has been loaded or written this transaction, /// otherwise it reads the backing database directly without caching the result in the overlay. - /// A slot of a wiped or created account that has not been rewritten reads as zero. Use + /// A slot of a wiped account, or of one whose creation discarded its old storage, that has not + /// been loaded or written reads as zero, though an attached strict BAL must still cover it. Use /// [`Self::storage`] when the slot should be loaded and preserved. #[inline(never)] pub fn storage_slot_untracked(&mut self, address: &Address, key: &Word) -> DbResult { - if let Some(account) = self.accounts.get(address) { - if let Some(slot) = account.storage.slots.get(key) { - return Ok(slot.value.current); - } - if account.storage.wiped || account.discards_storage() { - return Ok(Word::ZERO); - } + let account = self.accounts.get(address); + if let Some(slot) = account.and_then(|account| account.storage.slots.get(key)) { + return Ok(slot.value.current); } - self.database.get_storage(address, key) + let reads_zero = + account.is_some_and(|account| account.storage.wiped || account.discards_storage()); + self.inner.read_unloaded_slot(address, key, reads_zero) + } + + /// Returns an account's bytecode from the overlay or the backing database. + /// + /// This is the code-side mirror of [`Self::account_info_untracked`]: a non-loading peek that + /// resolves the account without caching it in the overlay, then returns the code held by the + /// account info or reads it from the database by code hash. It returns empty bytecode for an + /// absent account or the empty code hash. Use [`Self::account`] and + /// [`AccountHandle::load_code`] when the account should be loaded and preserved. + #[inline(never)] + pub fn code_untracked(&mut self, address: &Address) -> DbResult { + let Some(info) = self.account_info_untracked(address)? else { + return Ok(Bytecode::default()); + }; + if info.is_empty_code_hash() { + return Ok(Bytecode::default()); + } + let code = match info.code { + Some(code) if !code.is_empty() => code, + _ => self.database.get_code_by_hash(&info.code_hash)?, + }; + code.seed_hash(info.code_hash); + Ok(code) } /// Transfers value between accounts. @@ -758,7 +791,7 @@ impl<'a> State<'a> { #[cfg(feature = "account-ext")] extension, }; - target.mark_created(); + target.mark_created_discarding_storage(); target.touch(); Ok(Ok(())) } @@ -829,36 +862,20 @@ impl<'a> State<'a> { pub fn rollback(&mut self, checkpoint: StateCheckpoint, features: EvmFeatures) { // Restoring an older snapshot can leave active cursors past the current lengths. Preserve // shorter prefixes and revert only entries beyond each cursor. - self.logs.truncate(checkpoint.logs_len); - while self.journal.len() > checkpoint.journal_len { + self.logs.truncate(checkpoint.logs_len()); + while self.journal.len() > checkpoint.journal_len() { let Some(entry) = self.journal.pop() else { unreachable!("journal length is checked above") }; match entry { JournalEntry::AccountChange { address, previous, previous_flags } => { - // Reconcile the self-destruct set with the restored destroyed flag. - let was_destroyed = self - .accounts - .get(&address) - .is_some_and(|entry| entry.flags.contains(AccountFlags::DESTROYED)); - let previous_destroyed = previous_flags.contains(AccountFlags::DESTROYED); - if was_destroyed && !previous_destroyed { - self.selfdestructs.remove(&address); - } else if !was_destroyed && previous_destroyed { - self.selfdestructs.insert(address); - } if let Some(entry) = self.accounts.get_mut(&address) { entry.present = previous; // EIP-161 preserves the historical Yellow Paper K.1 precompile-3 touch. let keep_touch = features.contains(EvmFeatures::EIP161) && address == Address::with_last_byte(3); - // Neither the finalized touch nor the storage baseline choice is journaled, - // so rollback never restores them. - let mut kept = - AccountFlags::FINALIZED_TOUCHED | AccountFlags::KEEPS_STORAGE; - if keep_touch { - kept |= AccountFlags::TOUCHED; - } + let kept = + if keep_touch { AccountFlags::TOUCHED } else { AccountFlags::empty() }; entry.flags = (previous_flags - kept) | (entry.flags & kept); } } @@ -868,7 +885,7 @@ impl<'a> State<'a> { .get_mut(&address) .and_then(|account| account.storage.slots.get_mut(&key)) { - slot.value.current = previous; + slot.value.set_current(previous); } } JournalEntry::TransientStorageChange { address, key, previous } => match previous { @@ -880,13 +897,7 @@ impl<'a> State<'a> { } }, JournalEntry::StorageWarmed { address, key } => { - if let Some(slot) = self - .accounts - .get_mut(&address) - .and_then(|account| account.storage.slots.get_mut(&key)) - { - slot.is_warm = false; - } + self.set_storage_warm(&address, key, false); } } } @@ -900,7 +911,7 @@ impl<'a> State<'a> { // Finalization runs after the last revertible scope, so this is not journaled: the // entry would never be replayed before `clear_transaction_state` clears it. entry.present = Some(AccountInfo::default()); - entry.mark_created(); + entry.mark_created_discarding_storage(); } Ok(()) } @@ -918,10 +929,26 @@ impl<'a> State<'a> { /// turns that substate into account deletions, storage wipes, balance-only /// selfdestruct resets (EIP-8246), or pre-EIP-161 empty-account materialization. pub(crate) fn finalize_transaction(&mut self, version: &Version) -> DbResult<()> { - let selfdestructs = mem::take(&mut self.selfdestructs); + // Finalization neither touches nor destroys accounts, so one pass over the overlay selects + // both sets. Selfdestructed accounts are always touched, so they are not selected twice. + // Only the selected accounts need another lookup, and they are rare. EIP-161 deletes + // touched dead accounts at transaction finalization. Before EIP-161, touching a + // non-existent account materializes it as empty. + let eip161 = version.feature(EvmFeatures::EIP161); + let mut destroyed = Vec::new(); + let mut finalize = Vec::new(); + for (&address, entry) in &self.accounts { + if entry.flags.contains(AccountFlags::DESTROYED) { + destroyed.push(address); + } else if entry.flags.contains(AccountFlags::TOUCHED) + && if eip161 { entry.is_existing_dead() } else { entry.present.is_none() } + { + finalize.push(address); + } + } let eip8246 = version.feature(EvmFeatures::EIP8246); - for address in &selfdestructs { + for address in &destroyed { // EIP-8246: a self-destructed account that still holds balance is preserved as a // balance-only account instead of being burned. One with no balance is removed. The // handle is scoped so its `AccountChange` flushes on drop before the storage wipe. @@ -935,36 +962,14 @@ impl<'a> State<'a> { } survives }; - // Like revm, a created survivor keeps its unloaded old storage, which later - // transactions read as zero. One that keeps its storage baseline has no such reads, - // so its storage is wiped. + // A created survivor that discarded its old storage keeps it unloaded, and later + // transactions read it as zero. One that did not discard has no such reads, so its + // storage is wiped. let zero_loaded_only = survives && self.accounts[address].discards_storage(); let mut storage = self.storage(address)?; if zero_loaded_only { storage.zero_loaded() } else { storage.wipe() } } - // Finalization never touches accounts, so clearing touches in the same pass that selects - // the touched accounts to finalize sees the same set. Only those accounts need another - // lookup, and they are rare. - let eip161 = version.feature(EvmFeatures::EIP161); - let mut finalize = Vec::new(); - for (&address, entry) in &mut self.accounts { - if !entry.flags.contains(AccountFlags::TOUCHED) { - continue; - } - entry.flags.remove(AccountFlags::TOUCHED); - entry.flags.insert(AccountFlags::FINALIZED_TOUCHED); - // EIP-161 deletes touched dead accounts at transaction finalization. Before EIP-161, - // touching a non-existent account materializes it as empty. - let needs_finalization = if eip161 { - entry.is_existing_dead() - } else { - entry.present.is_none() && !selfdestructs.contains(&address) - }; - if needs_finalization { - finalize.push(address); - } - } for address in &finalize { if eip161 { self.account(address)?.delete_for_finalization(); @@ -973,18 +978,13 @@ impl<'a> State<'a> { self.materialize_empty_account_for_finalization(address)?; } } - - // Restore the selfdestruct set without clearing it: `take_pending_state` moves it into the - // detached [`PendingState`] to flag selfdestructed accounts, and `clear_transaction_state` - // clears it at the end of the transaction lifecycle. - self.selfdestructs = selfdestructs; Ok(()) } /// Returns a borrowed view of the transaction layer's changes, without detaching it. #[inline] - pub(crate) fn transaction_changes(&self) -> TxStateChanges<'_> { - TxStateChanges::new(&self.accounts, &self.selfdestructs) + pub(crate) const fn transaction_changes(&self) -> TxStateChanges<'_> { + TxStateChanges::new(&self.accounts) } /// Detaches the transaction overlay into an owned [`PendingState`]. @@ -992,81 +992,30 @@ impl<'a> State<'a> { /// The remaining transaction scratch (journal, logs, warm sets, transient storage) is left for /// [`Self::clear_transaction_state`]. pub(crate) fn take_pending_state(&mut self) -> PendingState { - PendingState { - accounts: mem::take(&mut self.accounts), - selfdestructs: mem::take(&mut self.inner.selfdestructs), - } - } - - /// Copies loaded state for an isolated transaction, without transaction scratch. - /// - /// Account and slot originals become their current values, runtime slot warmth is cleared, and - /// account warmth is retained only for pre-warmed addresses. Other account metadata is - /// preserved. - #[cfg(test)] - pub(crate) fn prepare_isolated_state(&self) -> PendingState { - let mut accounts = self.accounts.clone(); - for (address, account) in &mut accounts { - account.original = account.present.clone(); - account.flags.set(AccountFlags::WARM, self.inner.prewarm_set.is_warm(address)); - for slot in account.storage.slots.values_mut() { - slot.value = Tracked::new(slot.value.current); - slot.is_warm = false; - } - } - PendingState { accounts, selfdestructs: self.inner.selfdestructs.clone() } - } - - /// Merges an isolated transaction's returned state without adding journal entries. - /// - /// Existing account and slot originals are retained, as is existing account warmth. Slot - /// warmth is combined, current values are replaced, and account lifecycle flags are combined. - /// Newly loaded accounts and slots retain their child metadata. Journals, logs, pre-warmed - /// sets, transient storage and backing databases are unchanged. - #[cfg(test)] - pub(crate) fn merge_isolated_state(&mut self, child: PendingState) { - for (address, account) in child.accounts { - match self.accounts.entry(address) { - hash_map::Entry::Vacant(entry) => { - entry.insert(account); - } - hash_map::Entry::Occupied(mut entry) => { - let parent = entry.get_mut(); - parent.merge_isolated(account); - } - } - } - for address in child.selfdestructs { - let still_pending = self - .accounts - .get(&address) - .is_some_and(|account| account.flags.contains(AccountFlags::DESTROYED)); - if still_pending { - self.inner.selfdestructs.insert(address); - } - } + PendingState { accounts: mem::take(&mut self.accounts) } } /// Reattaches a detached [`PendingState`] as the current transaction overlay, replacing it. /// /// This is the inverse of the detach performed by - /// [`ExecutedTx::detach`](crate::ExecutedTx::detach): the pending accounts, storage, and - /// selfdestruct set become the transaction layer again, as if the transaction had just been - /// finalized. Other transaction scratch (journal, logs, warm sets, transient storage) is not + /// [`ExecutedTx::detach`](crate::ExecutedTx::detach): the pending accounts and storage become + /// the transaction layer again, as if the transaction had just been finalized. Touch and + /// selfdestruct flags are kept, so finalizing again would reset EIP-8246 survivors a second + /// time: call [`Self::commit_transaction`] before executing another + /// transaction. Other transaction scratch (journal, logs, warm sets, transient storage) is not /// affected. pub fn set_pending_state(&mut self, pending: PendingState) { for account in self.accounts.values_mut() { self.storage_pool.clear_overlay(&mut account.storage); } self.accounts = pending.accounts; - self.inner.selfdestructs = pending.selfdestructs; } /// Accepts the current transaction's state transition into the accepted overlay. /// /// This advances the in-memory accepted overlay by the transaction's write-set and clears the - /// transaction account/storage layers and selfdestruct markers. It does not take logs or write - /// to the wrapped backing database. + /// transaction account/storage layers. It does not take logs or write to the wrapped backing + /// database. pub fn commit_transaction(&mut self) { // The transaction overlay is folded into the accepted-overlay database directly, without // detaching it. @@ -1075,7 +1024,6 @@ impl<'a> State<'a> { self.storage_pool.clear_overlay(&mut account.storage); } self.accounts.clear(); - self.inner.selfdestructs.clear(); } } @@ -1102,7 +1050,7 @@ mod tests { state.tstore(&written, &Word::ZERO, &Word::from(9)); state.log(Log::default()); - let mut expected = state.clone_with(EmptyDB::default()); + let mut expected = state.clone(); expected.commit_transaction(); expected.clear_transaction_state(); let mut copy = state.committed_copy(EmptyDB::default()); @@ -1148,18 +1096,19 @@ mod tests { fn created_account_reads_old_storage_as_zero() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (loaded, unloaded) = (Word::from(1), Word::from(2)); + let (loaded, unloaded, fresh) = (Word::ONE, Word::from(2), Word::from(3)); let features = EvmFeatures::empty(); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &loaded, &Word::from(5)); db.insert_account_storage(&address, &unloaded, &Word::from(6)); + db.insert_account_storage(&address, &fresh, &Word::from(7)); let mut state = State::new(db); assert_eq!(state.storage_slot(&address, loaded).unwrap().current(), Word::from(5)); let checkpoint = state.checkpoint(); state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); - // A slot loaded before the creation keeps its value, as in revm. + // A slot loaded before the creation keeps its value. assert_eq!(state.storage_slot(&address, loaded).unwrap().current(), Word::from(5)); assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::ZERO); assert_eq!(state.storage_slot(&address, unloaded).unwrap().original(), Word::ZERO); @@ -1168,22 +1117,22 @@ mod tests { state.storage_slot(&address, unloaded).unwrap().write(Word::from(9)); state.rollback(checkpoint, features); - // Like revm, rolling back the creation keeps the zero it loaded, so a retried creation - // still reads zero. + // Rolling back the creation keeps the zero it loaded. assert_eq!(state.storage_slot(&address, loaded).unwrap().current(), Word::from(5)); assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::ZERO); let checkpoint = state.checkpoint(); state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); + assert_eq!(state.storage_slot(&address, fresh).unwrap().current(), Word::ZERO); assert_eq!(state.storage_slot(&address, unloaded).unwrap().current(), Word::ZERO); state.rollback(checkpoint, features); assert!(!state.take_pending_state().is_changed()); } #[test] - fn created_account_keeping_storage_reads_and_commits_its_baseline() { + fn marked_created_account_reads_and_commits_its_baseline() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (written, unwritten) = (Word::from(1), Word::from(2)); + let (written, unwritten) = (Word::ONE, Word::from(2)); let features = EvmFeatures::empty(); let mut db = CacheDB::default(); db.insert_account_info(&address, AccountInfo::default().with_nonce(1)); @@ -1192,7 +1141,7 @@ mod tests { let mut state = State::new(db); let mut account = state.account(&address).unwrap(); - account.mark_created_keeping_storage(); + account.mark_created(); // Still created for EIP-6780 `SELFDESTRUCT`. assert!(account.is_created()); drop(account); @@ -1206,19 +1155,15 @@ mod tests { assert_eq!(state.storage_slot(&address, written).unwrap().current(), Word::from(9)); assert_eq!(state.storage_slot(&address, unwritten).unwrap().current(), Word::from(6)); - // Rolling back the mark forgets the kept baseline, and an account already created in the - // overlay keeps reading its old storage as zero. + // Marking an account created in the overlay does not undo its discarded storage. let other = Address::with_last_byte(3); state .overlay_db_mut() - .insert_account_info(&other, AccountInfo::default().with_balance(Word::from(1))); + .insert_account_info(&other, AccountInfo::default().with_balance(Word::ONE)); state.overlay_db_mut().insert_account_storage(&other, &unwritten, &Word::from(6)); - let checkpoint = state.checkpoint(); - state.account(&other).unwrap().mark_created_keeping_storage(); - state.rollback(checkpoint, features); state.create_account(&caller, &other, &Word::ZERO, features).unwrap().unwrap(); + state.account(&other).unwrap().mark_created(); assert_eq!(state.storage_slot_untracked(&other, &unwritten).unwrap(), Word::ZERO); - state.account(&other).unwrap().mark_created_keeping_storage(); assert_eq!(state.storage_slot(&other, unwritten).unwrap().current(), Word::ZERO); } @@ -1229,7 +1174,7 @@ mod tests { let written = Word::from(2); let version = Version::base(crate::SpecId::CANCUN); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); let mut state = State::new(db); state.create_account(&caller, &address, &Word::ZERO, version.features).unwrap().unwrap(); @@ -1246,10 +1191,10 @@ mod tests { fn eip8246_created_survivor_keeps_old_storage() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (written, old) = (Word::from(1), Word::from(2)); + let (written, old) = (Word::ONE, Word::from(2)); let version = Version::base(crate::SpecId::AMSTERDAM); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &old, &Word::from(6)); let mut state = State::new(db); @@ -1268,17 +1213,17 @@ mod tests { } #[test] - fn eip8246_survivor_keeping_storage_wipes_it() { + fn eip8246_marked_created_survivor_wipes_storage() { let address = Address::with_last_byte(2); - let (written, earlier) = (Word::from(1), Word::from(2)); + let (written, earlier) = (Word::ONE, Word::from(2)); let version = Version::base(crate::SpecId::AMSTERDAM); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); // A slot the account's creation wrote before the transaction was suspended. db.insert_account_storage(&address, &earlier, &Word::from(6)); let mut state = State::new(db); - state.account(&address).unwrap().mark_created_keeping_storage(); + state.account(&address).unwrap().mark_created(); state.storage_slot(&address, written).unwrap().write(Word::from(9)); state.account(&address).unwrap().mark_destructed(); state.finalize_transaction_(version); @@ -1291,32 +1236,171 @@ mod tests { } #[test] - fn rollback_of_later_creation_mark_keeps_storage_baseline() { + fn rollback_of_storage_discard_restores_storage_baseline() { let address = Address::with_last_byte(2); - let key = Word::from(1); + let key = Word::ONE; let features = Version::base(crate::SpecId::CANCUN).features; let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &key, &Word::from(6)); let mut state = State::new(db); - state.account(&address).unwrap().mark_created_keeping_storage(); - let checkpoint = state.checkpoint(); state.account(&address).unwrap().mark_created(); + let checkpoint = state.checkpoint(); + state.account(&address).unwrap().mark_created_discarding_storage(); + assert!(state.accounts[&address].discards_storage()); state.rollback(checkpoint, features); + assert!(state.accounts[&address].is_created()); assert!(!state.accounts[&address].discards_storage()); assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::from(6)); } + #[test] + fn rollback_of_creation_keeps_slots_read_as_zero() { + let caller = Address::with_last_byte(1); + let address = Address::with_last_byte(2); + let (read, written, empty) = (Word::ONE, Word::from(2), Word::from(3)); + let version = Version::base(crate::SpecId::CANCUN); + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); + db.insert_account_storage(&address, &read, &Word::from(6)); + db.insert_account_storage(&address, &written, &Word::from(7)); + let mut state = State::new(db); + + let checkpoint = state.checkpoint(); + state.create_account(&caller, &address, &Word::ZERO, version.features).unwrap().unwrap(); + assert_eq!(state.storage_slot(&address, read).unwrap().current(), Word::ZERO); + state.storage_slot(&address, written).unwrap().write(Word::from(9)); + assert_eq!(state.storage_slot(&address, empty).unwrap().current(), Word::ZERO); + state.rollback(checkpoint, version.features); + + assert!(!state.accounts[&address].discards_storage()); + for key in [read, written, empty] { + assert_eq!(state.storage_slot_untracked(&address, &key).unwrap(), Word::ZERO); + let slot = state.storage_slot(&address, key).unwrap(); + assert_eq!((slot.original(), slot.current()), (Word::ZERO, Word::ZERO)); + } + assert!(!state.take_pending_state().is_changed()); + } + + #[test] + fn rollback_keeps_slot_read_as_zero_under_the_creation() { + let caller = Address::with_last_byte(1); + let address = Address::with_last_byte(2); + let key = Word::ONE; + let features = Version::base(crate::SpecId::CANCUN).features; + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); + db.insert_account_storage(&address, &key, &Word::from(6)); + let mut state = State::new(db); + + let outer = state.checkpoint(); + state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); + let inner = state.checkpoint(); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + state.rollback(inner, features); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + state.rollback(outer, features); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + } + + #[test] + fn rollback_of_creation_mark_leaves_slots_alone() { + let address = Address::with_last_byte(2); + let (read, zero) = (Word::ONE, Word::from(2)); + let features = Version::base(crate::SpecId::CANCUN).features; + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); + db.insert_account_storage(&address, &read, &Word::from(6)); + let mut state = State::new(db); + + let checkpoint = state.checkpoint(); + state.account(&address).unwrap().mark_created(); + assert_eq!(state.storage_slot(&address, read).unwrap().current(), Word::from(6)); + assert_eq!(state.storage_slot(&address, zero).unwrap().current(), Word::ZERO); + state.rollback(checkpoint, features); + + assert_eq!(state.storage_slot(&address, read).unwrap().current(), Word::from(6)); + assert_eq!(state.storage_slot(&address, zero).unwrap().current(), Word::ZERO); + } + + #[test] + fn finalization_wipe_after_rolled_back_creation_reads_zero() { + let caller = Address::with_last_byte(1); + let address = Address::with_last_byte(2); + let key = Word::ONE; + let version = Version::base(crate::SpecId::LONDON); + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); + db.insert_account_storage(&address, &key, &Word::from(6)); + let mut state = State::new(db); + + let checkpoint = state.checkpoint(); + state.create_account(&caller, &address, &Word::ZERO, version.features).unwrap().unwrap(); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + state.rollback(checkpoint, version.features); + + state.account(&address).unwrap().mark_destructed(); + state.finalize_transaction_(version); + + assert_eq!(state.storage_slot_untracked(&address, &key).unwrap(), Word::ZERO); + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + } + + #[test] + fn rollback_of_creation_keeps_slots_loaded_under_it() { + let caller = Address::with_last_byte(1); + let address = Address::with_last_byte(2); + let (before, under) = (Word::ONE, Word::from(2)); + let features = Version::base(crate::SpecId::CANCUN).features; + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); + let mut state = State::new(db); + assert_eq!(state.storage_slot(&address, before).unwrap().current(), Word::ZERO); + // Only a reread can observe these values, and rollback does not reread. + for key in [before, under] { + state.overlay_db_mut().insert_account_storage(&address, &key, &Word::from(5)); + } + + let checkpoint = state.checkpoint(); + state.create_account(&caller, &address, &Word::ZERO, features).unwrap().unwrap(); + assert_eq!(state.storage_slot(&address, under).unwrap().current(), Word::ZERO); + state.rollback(checkpoint, features); + + assert_eq!(state.storage_slot(&address, before).unwrap().current(), Word::ZERO); + assert_eq!(state.storage_slot(&address, under).unwrap().current(), Word::ZERO); + } + + #[test] + fn rollback_keeps_zero_of_slot_written_through_the_creating_handle() { + let address = Address::with_last_byte(2); + let key = Word::ONE; + let features = Version::base(crate::SpecId::CANCUN).features; + let mut db = CacheDB::default(); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); + db.insert_account_storage(&address, &key, &Word::from(6)); + let mut state = State::new(db); + + let checkpoint = state.checkpoint(); + let mut account = state.account(&address).unwrap(); + account.mark_created_discarding_storage(); + // The slot write is journaled before the handle flushes its creation entry on drop. + account.storage().into_slot(key).unwrap().write(Word::from(9)); + drop(account); + state.rollback(checkpoint, features); + + assert_eq!(state.storage_slot(&address, key).unwrap().current(), Word::ZERO); + } + #[test] fn later_transactions_read_old_storage_of_created_account_as_zero() { let caller = Address::with_last_byte(1); let address = Address::with_last_byte(2); - let (cached, unloaded, written) = (Word::from(1), Word::from(2), Word::from(3)); + let (cached, unloaded, written) = (Word::ONE, Word::from(2), Word::from(3)); let version = Version::base(crate::SpecId::CANCUN); let mut db = CacheDB::default(); - db.insert_account_info(&address, AccountInfo::default().with_balance(Word::from(1))); + db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); db.insert_account_storage(&address, &cached, &Word::from(5)); db.insert_account_storage(&address, &unloaded, &Word::from(6)); let mut state = State::new(db); @@ -1338,7 +1422,7 @@ mod tests { fn account_storage_handle_reuses_pooled_slot_map() { let address = Address::with_last_byte(1); let mut state = State::new(EmptyDB::default()); - let key = Word::from(1); + let key = Word::ONE; state.storage_slot(&address, key).unwrap(); let expected_capacity = state.accounts[&address].storage.slots.capacity(); assert!(expected_capacity > 0); @@ -1354,7 +1438,7 @@ mod tests { #[test] fn clone_preserves_state_and_detaches_database() { let address = Address::with_last_byte(42); - let key = Word::from(1); + let key = Word::ONE; let mut db = CacheDB::default(); db.insert_account_info(&address, AccountInfo::default()); db.insert_account_storage(&address, &key, &Word::from(10)); @@ -1380,13 +1464,13 @@ mod tests { fn clone_with_uses_database_and_preserves_state() { let address = Address::with_last_byte(42); let mut state = State::new(EmptyDB::default()); - state.tstore(&address, &Word::ZERO, &Word::from(1)); + state.tstore(&address, &Word::ZERO, &Word::ONE); let mut db = CacheDB::default(); db.insert_account_info(&address, AccountInfo::default()); let mut cloned = state.clone_with(db); - assert_eq!(cloned.tload(&address, &Word::ZERO), Word::from(1)); + assert_eq!(cloned.tload(&address, &Word::ZERO), Word::ONE); assert!(cloned.account_info_untracked(&address).unwrap().is_some()); } @@ -1408,7 +1492,7 @@ mod tests { #[test] fn set_storage_warm_preserves_loaded_values() { let address = Address::with_last_byte(42); - let key = Word::from(1); + let key = Word::ONE; for prewarmed in [false, true] { let mut state = State::new(EmptyDB::default()); state.set_storage_warm(&address, key, true); @@ -1436,7 +1520,7 @@ mod tests { #[test] fn set_account_warm_preserves_loaded_values() { let address = Address::with_last_byte(42); - let key = Word::from(1); + let key = Word::ONE; for prewarmed in [false, true] { let mut state = State::new(EmptyDB::default()); state.set_account_warm(&address, true); @@ -1506,129 +1590,40 @@ mod tests { } #[test] - fn isolated_state_resets_originals_and_preserves_parent_metadata() { - let address = Address::with_last_byte(42); - let prewarmed = Address::with_last_byte(43); - let created = Address::with_last_byte(44); - let destroyed = Address::with_last_byte(45); - let wiped = Address::with_last_byte(46); - let child_only = Address::with_last_byte(47); - let mut parent = State::new(EmptyDB::default()); - parent.account(&address).unwrap().warm(); - parent.prewarm(&prewarmed); - assert!(parent.account(&prewarmed).unwrap().is_warm()); - { - let mut created_account = parent.account(&created).unwrap(); - created_account.set_balance(Word::ONE); - created_account.mark_created(); + fn wiped_slot_is_reported_as_a_read_of_its_original_value() { + #[derive(Default)] + struct Reads(Vec<(Word, Word)>); + + impl StateChangeSink for Reads { + type Error = core::convert::Infallible; + + fn storage_read( + &mut self, + _address: Address, + key: Word, + value: Word, + ) -> Result<(), Self::Error> { + self.0.push((key, value)); + Ok(()) + } } - parent.account(&destroyed).unwrap().mark_destructed(); - parent.storage(&wiped).unwrap().wipe(); - let mut slot = parent.storage_slot(&address, Word::ZERO).unwrap(); - slot.set(Word::from(7)); - slot.warm(); - parent.tstore(&address, &Word::ZERO, &Word::from(9)); - let checkpoint = parent.checkpoint(); - let mut child = State::new(EmptyDB::default()); - child.set_pending_state(parent.prepare_isolated_state()); - assert!(!child.account(&address).unwrap().is_warm()); - assert!(child.account(&prewarmed).unwrap().is_warm()); - assert!(child.account(&created).unwrap().is_created()); - assert!(child.account(&destroyed).unwrap().is_destructed()); - assert!(child.storage(&wiped).unwrap().is_wiped()); - assert_eq!(child.tload(&address, &Word::ZERO), Word::ZERO); - let mut slot = child.storage_slot(&address, Word::ZERO).unwrap(); - assert_eq!((slot.original(), slot.current()), (Word::from(7), Word::from(7))); - assert!(!slot.is_warm()); - slot.set(Word::from(8)); - child.storage_slot(&address, Word::ONE).unwrap().set(Word::from(10)); - child.account(&child_only).unwrap().set_balance(Word::ONE); - child.storage_slot(&child_only, Word::ZERO).unwrap().set(Word::ONE); - parent.merge_isolated_state(child.take_pending_state()); - assert_eq!(parent.checkpoint(), checkpoint); - let slot = parent.storage_slot(&address, Word::ZERO).unwrap(); - assert_eq!((slot.original(), slot.current()), (Word::ZERO, Word::from(8))); - assert!(slot.is_warm()); - assert_eq!(parent.get_storage(&address, &Word::ONE), Some(Word::from(10))); - assert_eq!(parent.tload(&address, &Word::ZERO), Word::from(9)); - assert!(parent.accounts[&created].is_created()); - assert!(parent.inner.selfdestructs.contains(&destroyed)); - assert!(parent.accounts[&wiped].storage.wiped); - assert_eq!(parent.accounts[&child_only].present.as_ref().unwrap().balance, Word::ONE); - assert_eq!(parent.get_storage(&child_only, &Word::ZERO), Some(Word::ONE)); - } - - #[test] - fn isolated_state_rebases_account_original_and_preserves_parent_warmth() { - let address = Address::with_last_byte(42); - let cold_address = Address::with_last_byte(43); - let mut parent = State::new(EmptyDB::default()); - parent.account(&address).unwrap().set_balance(Word::from(7)); - parent.account(&cold_address).unwrap().set_balance(Word::ONE); - assert!(!parent.account(&address).unwrap().is_warm()); - assert!(!parent.account(&cold_address).unwrap().is_warm()); - parent.prewarm(&address); - - let mut child = State::new(EmptyDB::default()); - child.set_pending_state(parent.prepare_isolated_state()); - assert_eq!(child.accounts[&address].original, child.accounts[&address].present); - assert!(child.account(&address).unwrap().is_warm()); - assert!(!child.account(&cold_address).unwrap().is_warm()); - child.account(&cold_address).unwrap().warm(); - child.account(&address).unwrap().set_balance(Word::from(8)); - parent.merge_isolated_state(child.take_pending_state()); - - assert!(parent.accounts[&address].original.is_none()); - assert_eq!(parent.account(&address).unwrap().balance(), Word::from(8)); - assert!(parent.account(&address).unwrap().is_warm()); - assert!(!parent.account(&cold_address).unwrap().is_warm()); - } - #[test] - fn isolated_state_reinserts_restored_slot_after_wipe() { let address = Address::with_last_byte(42); - let key = Word::ONE; + let (read_key, written_key) = (Word::ONE, Word::from(2)); let mut db = CacheDB::default(); db.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); - db.insert_account_storage(&address, &key, &Word::from(5)); - let mut parent = State::new(db); - parent.account(&address).unwrap(); - parent.storage_slot(&address, key).unwrap(); - parent.storage(&address).unwrap().wipe(); - - let mut child = State::new(EmptyDB::default()); - child.set_pending_state(parent.prepare_isolated_state()); - child.storage_slot(&address, key).unwrap().set(Word::from(5)); - parent.merge_isolated_state(child.take_pending_state()); - assert_eq!(parent.get_storage(&address, &key), Some(Word::from(5))); - parent.commit_transaction(); - - assert_eq!(parent.storage_slot_untracked(&address, &key).unwrap(), Word::from(5)); - } - - #[test] - fn isolated_state_does_not_replay_finalized_eip8246_selfdestruct() { - let address = Address::with_last_byte(42); - let key = Word::ONE; - let mut child = State::new(EmptyDB::default()); - { - let mut account = child.account(&address).unwrap(); - account.set_balance(Word::from(5)); - account.mark_destructed(); - } - child.finalize_transaction_(Version::base(crate::SpecId::AMSTERDAM)); - assert!(!child.accounts[&address].flags.contains(AccountFlags::DESTROYED)); - assert!(child.inner.selfdestructs.contains(&address)); - - let mut parent = State::new(EmptyDB::default()); - parent.merge_isolated_state(child.take_pending_state()); - assert!(!parent.inner.selfdestructs.contains(&address)); - parent.account(&address).unwrap().set_nonce(1); - parent.storage_slot(&address, key).unwrap().set(Word::from(9)); - parent.finalize_transaction_(Version::base(crate::SpecId::AMSTERDAM)); + db.insert_account_storage(&address, &read_key, &Word::from(5)); + db.insert_account_storage(&address, &written_key, &Word::from(6)); + let mut state = State::new(db); + state.account(&address).unwrap(); + state.storage_slot(&address, read_key).unwrap(); + state.storage_slot(&address, written_key).unwrap().set(Word::from(7)); + state.storage(&address).unwrap().wipe(); - assert_eq!(parent.account(&address).unwrap().nonce(), 1); - assert_eq!(parent.storage_slot_untracked(&address, &key).unwrap(), Word::from(9)); + let mut reads = Reads::default(); + state.take_pending_state().visit(&mut reads).unwrap(); + reads.0.sort(); + assert_eq!(reads.0, [(read_key, Word::from(5)), (written_key, Word::from(6))]); } #[test] @@ -1672,23 +1667,23 @@ mod tests { #[test] fn loaded_accounts_include_reads_and_absent_accounts() { - let read = Address::from([0x74; 20]); - let absent = Address::from([0x75; 20]); - let accepted = Address::from([0x76; 20]); + let read = Address::repeat_byte(0x74); + let absent = Address::repeat_byte(0x75); + let accepted = Address::repeat_byte(0x76); let mut database = CacheDB::default(); - database.insert_account_info(&read, AccountInfo::default().with_balance(Word::from(1))); - database.insert_account_storage(&read, &Word::from(1), &Word::from(10)); + database.insert_account_info(&read, AccountInfo::default().with_balance(Word::ONE)); + database.insert_account_storage(&read, &Word::ONE, &Word::from(10)); let mut state = State::new(database); - state.account(&accepted).unwrap().set_balance(Word::from(1)); + state.account(&accepted).unwrap().set_balance(Word::ONE); state.commit_transaction(); - state.storage_slot(&read, Word::from(1)).unwrap(); + state.storage_slot(&read, Word::ONE).unwrap(); state.storage_slot(&read, Word::from(2)).unwrap(); state.account(&absent).unwrap(); assert_eq!( sorted_loaded_accounts(&state), - [(read, vec![Word::from(1), Word::from(2)]), (absent, vec![])] + [(read, vec![Word::ONE, Word::from(2)]), (absent, vec![])] ); } @@ -1696,7 +1691,7 @@ mod tests { fn loaded_accounts_include_reads_in_reverted_frames() { let read = Address::with_last_byte(0x77); let mut database = CacheDB::default(); - database.insert_account_info(&read, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&read, AccountInfo::default().with_balance(Word::ONE)); let mut state = State::new(database); let checkpoint = state.checkpoint(); diff --git a/crates/evm2/src/evm/state/pending.rs b/crates/evm2/src/evm/state/pending.rs index 42bc64fc7..84c6db14d 100644 --- a/crates/evm2/src/evm/state/pending.rs +++ b/crates/evm2/src/evm/state/pending.rs @@ -5,10 +5,7 @@ use super::{ StorageSlot, Tracked, }; use crate::interpreter::Word; -use alloy_primitives::{ - Address, - map::{AddressMap, AddressSet}, -}; +use alloy_primitives::{Address, map::AddressMap}; /// A transaction's finalized-but-uncommitted state, moved out of the EVM. /// @@ -32,8 +29,6 @@ pub struct PendingState { /// Accounts loaded by the transaction: transaction-boundary original info, present info, and /// account-lifetime flags. pub(crate) accounts: AddressMap, - /// Accounts selfdestructed by the transaction. - pub(crate) selfdestructs: AddressSet, } impl PendingState { @@ -56,14 +51,11 @@ impl PendingState { original: Option, current: Option, ) { - let code_changed = original.as_ref().map(|account| account.code_hash) - != current.as_ref().map(|account| account.code_hash); - let flags = if code_changed { AccountFlags::CODE_CHANGED } else { AccountFlags::empty() }; let account = self.accounts.entry(address).or_default(); *account = Account { original, present: current, - flags, + flags: AccountFlags::empty(), storage: core::mem::take(&mut account.storage), }; } @@ -100,7 +92,7 @@ impl PendingState { /// Returns a borrowed view of the transaction's changes. #[inline] pub const fn changes(&self) -> TxStateChanges<'_> { - TxStateChanges::new(&self.accounts, &self.selfdestructs) + TxStateChanges::new(&self.accounts) } /// Returns each loaded account's changes, in an unspecified account order. @@ -140,44 +132,39 @@ impl StateChangeSource for PendingState { /// transaction wrote before deciding to commit or discard it, without detaching the state. #[derive(Clone, Copy, Debug, Default)] pub struct TxStateChanges<'a> { - inner: Option<(&'a AddressMap, &'a AddressSet)>, + accounts: Option<&'a AddressMap>, } impl<'a> TxStateChanges<'a> { #[inline] - pub(crate) const fn new( - accounts: &'a AddressMap, - selfdestructs: &'a AddressSet, - ) -> Self { - Self { inner: Some((accounts, selfdestructs)) } + pub(crate) const fn new(accounts: &'a AddressMap) -> Self { + Self { accounts: Some(accounts) } } /// Returns a view with no changes. #[inline] pub const fn empty() -> Self { - Self { inner: None } + Self { accounts: None } } /// Returns whether the transaction loaded no accounts and no storage. #[inline] pub fn is_empty(&self) -> bool { - self.inner.is_none_or(|(accounts, _)| accounts.is_empty()) + self.accounts.is_none_or(AddressMap::is_empty) } /// Returns the current account information when the transaction loaded the account and it /// exists after the transaction. #[inline] pub fn account_info(&self, address: &Address) -> Option<&'a AccountInfo> { - self.inner?.0.get(address).and_then(|account| account.present.as_ref()) + self.accounts?.get(address).and_then(|account| account.present.as_ref()) } /// Returns each loaded account's changes, in an unspecified account order. #[inline] pub fn account_changes(self) -> impl Iterator> { - self.inner.into_iter().flat_map(|(accounts, selfdestructs)| { - accounts.iter().map(|(&address, account)| { - account.changes(address, selfdestructs.contains(&address)) - }) + self.accounts.into_iter().flat_map(|accounts| { + accounts.iter().map(|(&address, account)| account.changes(address)) }) } @@ -185,10 +172,7 @@ impl<'a> TxStateChanges<'a> { /// it. #[inline] pub fn account_changes_for(&self, address: &Address) -> Option> { - let (accounts, selfdestructs) = self.inner?; - accounts - .get(address) - .map(|account| account.changes(*address, selfdestructs.contains(address))) + self.accounts?.get(address).map(|account| account.changes(*address)) } } @@ -209,7 +193,7 @@ mod tests { #[test] fn inserts_account_and_storage() { let address = Address::with_last_byte(0xaa); - let key = Word::from(1); + let key = Word::ONE; let original = AccountInfo { nonce: 1, ..AccountInfo::default() }; let current = AccountInfo { nonce: 2, ..AccountInfo::default() }; let mut state = PendingState::default(); @@ -240,7 +224,7 @@ mod tests { state.insert_account(unchanged, Some(info.clone()), Some(info.clone())); let destroyed = Address::with_last_byte(0xdd); state.insert_account(destroyed, Some(info.clone()), Some(info)); - state.selfdestructs.insert(destroyed); + state.accounts.get_mut(&destroyed).unwrap().flags.insert(AccountFlags::DESTROYED); let changes = state.account_changes_for(&changed).unwrap(); assert_eq!(changes.address, changed); diff --git a/crates/evm2/src/evm/state/storage.rs b/crates/evm2/src/evm/state/storage.rs index e43242f2b..f566ba122 100644 --- a/crates/evm2/src/evm/state/storage.rs +++ b/crates/evm2/src/evm/state/storage.rs @@ -1,6 +1,6 @@ //! Transaction-scoped persistent storage overlay. -use super::{DbResult, DynDatabase, JournalEntry, StateInner, Tracked}; +use super::{DbResult, JournalEntry, StateInner, Tracked}; use crate::{LoadError, interpreter::Word}; use alloy_primitives::{ Address, @@ -23,24 +23,6 @@ pub struct StorageOverlay { } impl StorageOverlay { - /// Applies an isolated execution's slots, retaining parent originals and combining warmth. - #[cfg(test)] - pub(crate) fn merge_isolated(&mut self, child: Self) { - self.wiped |= child.wiped; - for (key, slot) in child.slots { - match self.slots.entry(key) { - hash_map::Entry::Vacant(entry) => { - entry.insert(slot); - } - hash_map::Entry::Occupied(mut entry) => { - let parent = entry.get_mut(); - parent.value.current = slot.value.current; - parent.is_warm |= slot.is_warm; - } - } - } - } - /// Returns the changed storage slots. /// /// A slot is changed when its current value differs from its transaction-boundary original, @@ -107,8 +89,9 @@ pub struct StorageHandle<'a, 'db> { address: &'a Address, /// Transaction overlay entry: the per-account storage slots plus the wipe flag. storage: &'a mut StorageOverlay, - /// Whether the account was created in this transaction, so its unloaded slots read as zero. - created: bool, + /// Whether the account's creation discarded its old storage, so its unloaded slots read as + /// zero. + discards_storage: bool, /// Shared inner state: backing database, revert journal, and base warm set. #[derive_where(skip)] inner: &'a mut StateInner<'db>, @@ -121,10 +104,10 @@ impl<'a, 'db> StorageHandle<'a, 'db> { pub(crate) const fn new( address: &'a Address, storage: &'a mut StorageOverlay, - created: bool, + discards_storage: bool, inner: &'a mut StateInner<'db>, ) -> Self { - Self { address, storage, created, inner } + Self { address, storage, discards_storage, inner } } /// Returns the account address. @@ -216,7 +199,7 @@ impl<'a, 'db> StorageHandle<'a, 'db> { key: Word, skip_cold_load: bool, ) -> Result<(StorageSlotHandle<'a, 'db>, bool), LoadError> { - let Self { address, storage, created, inner } = self; + let Self { address, storage, discards_storage, inner } = self; let (slot, is_cold) = match storage.slots.entry(key) { hash_map::Entry::Occupied(entry) => { // An already-loaded slot has no cold database read to skip, so the skip only @@ -239,13 +222,8 @@ impl<'a, 'db> StorageHandle<'a, 'db> { if skip_cold_load && !is_warm { return Err(LoadError::ColdLoadSkipped); } - // Like revm, an unloaded slot of a created account reads zero without a database - // read, since the creation discards the address's old storage. - let value = if storage.wiped || created { - Word::ZERO - } else { - inner.database.get_storage(address, &key)? - }; + let value = + inner.read_unloaded_slot(address, &key, storage.wiped || discards_storage)?; (entry.insert(StorageSlot::loaded(value, is_warm)), !is_warm) } }; @@ -260,10 +238,11 @@ impl<'a, 'db> StorageHandle<'a, 'db> { /// /// Only called during transaction finalization (selfdestruct and EIP-161 dead-account /// deletion), after the last revertible scope, so the wipe is not journaled. Loaded slot - /// entries are kept with their values reset to zero: wiped slots resolve to zero on re-load, - /// and resetting `original` alongside `current` turns the transaction's prior writes into - /// unchanged reads, which keeps a destroyed account's storage accesses visible to the EIP-7928 - /// block access list (execution-specs `destroy_storage` converts writes to reads). + /// entries are kept with their `current` value reset to zero and their `original` value kept: + /// wiped slots resolve to zero on re-load, and the reset turns the transaction's prior writes + /// into unchanged reads of the slot's original value, which keeps a destroyed account's storage + /// accesses visible to the EIP-7928 block access list (execution-specs `destroy_storage` + /// converts writes to reads). #[inline] pub fn wipe(&mut self) { self.storage.wiped = true; @@ -369,7 +348,7 @@ impl StorageSlotHandle<'_, '_> { if previous == value { return; } - self.slot.value.current = value; + self.slot.value.set_current(value); self.inner.push_journal(|| JournalEntry::StorageChange { address: *self.address, key: self.key, @@ -397,67 +376,69 @@ mod tests { SpecId, Version, evm::{ CacheDB, + bal::{AccountBal, Bal, BalChanges, BalError, BlockAccessIndex}, state::{AccountInfo, State}, }, }; - use alloc::vec; + use alloc::{sync::Arc, vec}; + use alloy_eip7928::StorageChange; use alloy_primitives::Address; #[test] fn storage_change_rolls_back_to_checkpoint() { - let address = Address::from([0x11; 20]); + let address = Address::repeat_byte(0x11); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); - database.insert_account_storage(&address, &Word::from(1), &Word::from(10)); + database.insert_account_storage(&address, &Word::ONE, &Word::from(10)); let mut state = State::new(database); let checkpoint = state.checkpoint(); - state.storage_slot(&address, Word::from(1)).unwrap().write(Word::from(20)); - state.storage_slot(&address, Word::from(1)).unwrap().write(Word::from(30)); + state.storage_slot(&address, Word::ONE).unwrap().write(Word::from(20)); + state.storage_slot(&address, Word::ONE).unwrap().write(Word::from(30)); - assert_eq!(state.storage_slot(&address, Word::from(1)).unwrap().current(), Word::from(30)); + assert_eq!(state.storage_slot(&address, Word::ONE).unwrap().current(), Word::from(30)); state.rollback(checkpoint, Version::base(SpecId::FRONTIER).features); - assert_eq!(state.storage_slot(&address, Word::from(1)).unwrap().current(), Word::from(10)); + assert_eq!(state.storage_slot(&address, Word::ONE).unwrap().current(), Word::from(10)); } #[test] fn transient_storage_change_rolls_back_to_checkpoint() { - let address = Address::from([0x22; 20]); + let address = Address::repeat_byte(0x22); let mut state = State::new(CacheDB::default()); - state.tstore(&address, &Word::from(1), &Word::from(10)); + state.tstore(&address, &Word::ONE, &Word::from(10)); let checkpoint = state.checkpoint(); - state.tstore(&address, &Word::from(1), &Word::from(20)); + state.tstore(&address, &Word::ONE, &Word::from(20)); - assert_eq!(state.tload(&address, &Word::from(1)), Word::from(20)); + assert_eq!(state.tload(&address, &Word::ONE), Word::from(20)); state.rollback(checkpoint, Version::base(SpecId::FRONTIER).features); - assert_eq!(state.tload(&address, &Word::from(1)), Word::from(10)); + assert_eq!(state.tload(&address, &Word::ONE), Word::from(10)); } #[test] fn take_transient_storage_only_drains_requested_account() { - let address = Address::from([0x22; 20]); - let other = Address::from([0x23; 20]); + let address = Address::repeat_byte(0x22); + let other = Address::repeat_byte(0x23); let mut state = State::new(CacheDB::default()); - state.tstore(&address, &Word::from(1), &Word::from(10)); + state.tstore(&address, &Word::ONE, &Word::from(10)); state.tstore(&address, &Word::from(2), &Word::from(20)); - state.tstore(&other, &Word::from(1), &Word::from(30)); + state.tstore(&other, &Word::ONE, &Word::from(30)); let mut slots = state.take_transient_storage(&address); slots.sort_unstable_by_key(|(key, _)| *key); - assert_eq!(slots, vec![(Word::from(1), Word::from(10)), (Word::from(2), Word::from(20))]); - assert_eq!(state.tload(&address, &Word::from(1)), Word::ZERO); - assert_eq!(state.tload(&other, &Word::from(1)), Word::from(30)); + assert_eq!(slots, vec![(Word::ONE, Word::from(10)), (Word::from(2), Word::from(20))]); + assert_eq!(state.tload(&address, &Word::ONE), Word::ZERO); + assert_eq!(state.tload(&other, &Word::ONE), Word::from(30)); } #[test] fn storage_wipe_preserves_warm_slots_in_merged_storage_map() { let account = Address::with_last_byte(0x19); - let warm_key = Word::from(1); + let warm_key = Word::ONE; let cold_key = Word::from(2); let mut database = CacheDB::default(); - database.insert_account_info(&account, AccountInfo::default().with_balance(Word::from(1))); + database.insert_account_info(&account, AccountInfo::default().with_balance(Word::ONE)); database.insert_account_storage(&account, &warm_key, &Word::from(3)); database.insert_account_storage(&account, &cold_key, &Word::from(4)); let mut state = State::new(database); @@ -483,8 +464,8 @@ mod tests { #[test] fn journaled_storage_mutations_journal_and_roll_back() { - let address = Address::from([0x33; 20]); - let key = Word::from(1); + let address = Address::repeat_byte(0x33); + let key = Word::ONE; let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); database.insert_account_storage(&address, &key, &Word::from(10)); @@ -512,7 +493,7 @@ mod tests { #[test] fn journaled_storage_read_only_handle_journals_nothing() { - let address = Address::from([0x34; 20]); + let address = Address::repeat_byte(0x34); let key = Word::from(7); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); @@ -531,7 +512,7 @@ mod tests { #[test] fn fused_slot_load_warms_once_and_reverts() { - let address = Address::from([0x35; 20]); + let address = Address::repeat_byte(0x35); let key = Word::from(8); let mut state = State::new(CacheDB::default()); let checkpoint = state.checkpoint(); @@ -570,7 +551,7 @@ mod tests { #[test] fn storage_handle_reports_warmth_before_loading() { - let address = Address::from([0x35; 20]); + let address = Address::repeat_byte(0x35); let key = Word::from(9); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); @@ -595,7 +576,7 @@ mod tests { #[test] fn storage_slot_warm_honors_prewarm_after_load() { - let address = Address::from([0x36; 20]); + let address = Address::repeat_byte(0x36); let key = Word::from(11); let mut database = CacheDB::default(); database.insert_account_info(&address, AccountInfo::default()); @@ -611,4 +592,38 @@ mod tests { assert!(slot.is_warm()); assert!(!slot.warm(), "base-prewarmed slot must not report a cold transition"); } + + #[test] + fn created_account_slot_reads_keep_strict_bal_coverage() { + let creator = Address::with_last_byte(0x50); + let address = Address::with_last_byte(0x51); + let (covered, uncovered) = (Word::from(7), Word::from(9)); + let mut account = AccountBal::default(); + account.storage.storage.insert( + covered, + BalChanges::new(vec![StorageChange::new(BlockAccessIndex::new(1), Word::from(42))]), + ); + let mut database = CacheDB::default(); + database.insert_account_info(&address, AccountInfo::default().with_balance(Word::ONE)); + database.insert_account_storage(&address, &uncovered, &Word::from(20)); + let mut state = State::new(database); + state.set_bal(Arc::new(Bal::from_iter([(address, account)]))); + state.set_bal_index(BlockAccessIndex::new(2)); + let version = Version::base(SpecId::LONDON); + state.create_account(&creator, &address, &Word::ZERO, version.features).unwrap().unwrap(); + + // The created account reads zero, but a slot missing from the BAL is still invalid. + let error = state.storage_slot(&address, uncovered).unwrap_err(); + assert!(error.downcast_ref::().is_some()); + let error = state.storage_slot_untracked(&address, &uncovered).unwrap_err(); + assert!(error.downcast_ref::().is_some()); + assert!(!state.accounts[&address].storage.slots.contains_key(&uncovered)); + + assert_eq!(state.storage_slot(&address, covered).unwrap().current(), Word::ZERO); + assert_eq!(state.storage_slot_untracked(&address, &covered).unwrap(), Word::ZERO); + + // With database fallback enabled, an uncovered slot is allowed and still reads zero. + state.set_allow_bal_db_fallback(true); + assert_eq!(state.storage_slot(&address, uncovered).unwrap().current(), Word::ZERO); + } } diff --git a/crates/evm2/src/evm/state/stream.rs b/crates/evm2/src/evm/state/stream.rs index 1593136a8..7856987ce 100644 --- a/crates/evm2/src/evm/state/stream.rs +++ b/crates/evm2/src/evm/state/stream.rs @@ -90,7 +90,7 @@ impl<'a> AccountChanges<'a> { current: value.current, })?; } else { - sink.storage_read(address, key, value.current)?; + sink.storage_read(address, key, value.original)?; } } if self.is_info_changed() { @@ -168,6 +168,9 @@ pub trait StateChangeSink { /// Observes a storage slot the transaction loaded but left unchanged. /// + /// `value` is the slot's value at the start of the transaction. After a storage wipe the + /// slot's value after the transaction is zero. + /// /// Only transaction-level sources report reads; sinks that persist changes can ignore them. #[inline] fn storage_read( diff --git a/crates/evm2/src/evm/system.rs b/crates/evm2/src/evm/system.rs index f3afbd83f..5167e5b33 100644 --- a/crates/evm2/src/evm/system.rs +++ b/crates/evm2/src/evm/system.rs @@ -14,7 +14,7 @@ use crate::registry::HandlerError; use crate::{ EvmTypes, env::TxEnvExt, - ethereum::{execute_initial_frame, prepare_initial_frame}, + ethereum::{execute_initial_frame, gas_spent_breakdown, prepare_initial_frame}, interpreter::GasTracker, registry::HandlerResult, version::{EvmFeatures, GasId}, @@ -111,9 +111,10 @@ impl SystemTx { impl<'a, T: EvmTypes> Evm<'a, T> { /// Executes a system call. /// - /// System calls bypass normal transaction validation, nonce updates, fee charging, gas refunds, - /// and beneficiary rewards. They execute a top-level `CALL` with zero value and - /// [`SystemTx::gas_limit`] gas, then finalize and return an executed transaction handle. + /// System calls bypass normal transaction validation, nonce updates, fee charging, and + /// beneficiary rewards. They execute a top-level `CALL` with zero value and + /// [`SystemTx::gas_limit`] gas, then finalize and return an executed transaction handle. A + /// successful call reports its raw gas refund, without the EIP-3529 cap. /// /// The target system contract bytecode must already be present in state. This method does not /// deploy protocol system contracts or synthesize their bytecode. @@ -145,7 +146,7 @@ impl<'a, T: EvmTypes> Evm<'a, T> { pub fn execute_system_call(&mut self, tx: SystemTx) -> HandlerResult> { let SystemTx { caller, system_contract_address, data, gas_limit, .. } = tx; self.state.prewarm(&system_contract_address); - let tx_env = TxEnvExt { + self.tx_env = TxEnvExt { origin: caller, gas_price: U256::ZERO, chain_id: U256::from(self.version().chain_id), @@ -161,6 +162,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { } else { 0 }; + // The call's total budget is `gas_limit + reservoir` and must fit in a `u64`. + let gas_limit = gas_limit.min(u64::MAX - reservoir); let mut tx_gas = GasTracker::new_with_execution_gas_and_reservoir(gas_limit, reservoir); let frame = prepare_initial_frame( self, @@ -171,18 +174,19 @@ impl<'a, T: EvmTypes> Evm<'a, T> { U256::ZERO, &mut tx_gas, )?; - let result = - execute_initial_frame(self, &tx_env, frame, &mut tx_gas, gas_limit, reservoir)?; - let gas_spent = gas_limit.saturating_sub(result.gas.remaining()); - let gas_refunded = if result.stop.is_success() && result.gas.refunded() > 0 { + let result = execute_initial_frame(self, frame, &mut tx_gas, gas_limit, reservoir)?; + // The reservoir is funded on top of `gas_limit`, so the transaction gas limit includes it. + let (total_gas_spent, state_gas_spent) = + gas_spent_breakdown(gas_limit + reservoir, &result.gas, 0); + let gas_refunded = if result.is_success() && result.gas.refunded() > 0 { result.gas.refunded() as u64 } else { 0 }; Ok(TxResultExt { - status: result.stop.is_success(), - total_gas_spent: gas_spent, - state_gas_spent: result.gas.state_gas_spent().max(0) as u64, + status: result.is_success(), + total_gas_spent, + state_gas_spent, refunded: gas_refunded, floor_gas: 0, stop: result.stop, @@ -223,6 +227,8 @@ impl<'a, T: EvmTypes> Evm<'a, T> { &mut self, tx: SystemTx, ) -> impl Future, HandlerError>> + Send + '_ + where + 'a: 'static, { self.assert_erased_send(); let stack = self.async_stack(); @@ -250,14 +256,15 @@ mod tests { interpreter::{GasTracker, InstrStop, Interpreter, Message, MessageResult, op}, precompiles::{Precompile, PrecompileError, PrecompileId, PrecompileResult}, registry::{HandlerError, TxRegistry}, + utils::address_to_word, }; type TestEvm = Evm<'static, BaseEvmTypes>; #[test] fn system_call_uses_system_sender_without_fee_accounting() { - let contract = Address::from([0x42; 20]); - let beneficiary = Address::from([0x99; 20]); + let contract = Address::repeat_byte(0x42); + let beneficiary = Address::repeat_byte(0x99); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::CALLER, op::PUSH1, @@ -282,7 +289,7 @@ mod tests { let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().detach(); - assert!(result.result.status); + assert!(result.result.is_success()); assert!(result.result.tx_gas_used() < SYSTEM_CALL_GAS_LIMIT); let unchanged = |address| { result.pending_state.accounts.get(address).is_none_or(|entry| !entry.is_changed()) @@ -291,15 +298,15 @@ mod tests { assert!(unchanged(&beneficiary)); let storage = &result.pending_state.accounts.get(&contract).expect("storage changed").storage.slots; - let system_address = U256::from_be_slice(SYSTEM_ADDRESS.as_slice()); + let system_address = address_to_word(&SYSTEM_ADDRESS); assert_eq!(storage.get(&U256::ZERO).map(|slot| slot.value.current), Some(system_address)); assert_eq!(storage.get(&U256::ONE).map(|slot| slot.value.current), Some(system_address)); } #[test] fn system_call_uses_custom_caller() { - let caller = Address::from([0x11; 20]); - let contract = Address::from([0x42; 20]); + let caller = Address::repeat_byte(0x11); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::CALLER, op::PUSH1, @@ -326,17 +333,17 @@ mod tests { .unwrap() .detach(); - assert!(result.result.status); + assert!(result.result.is_success()); let storage = &result.pending_state.accounts.get(&contract).expect("storage changed").storage.slots; - let caller = U256::from_be_slice(caller.as_slice()); + let caller = address_to_word(&caller); assert_eq!(storage.get(&U256::ZERO).map(|slot| slot.value.current), Some(caller)); assert_eq!(storage.get(&U256::ONE).map(|slot| slot.value.current), Some(caller)); } #[test] fn system_call_starts_with_warm_system_contract() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[op::ADDRESS, op::EXTCODESIZE, op::STOP])); let mut database = InMemoryDB::default(); @@ -351,7 +358,7 @@ mod tests { let outcome = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().discard(); - assert!(outcome.status); + assert!(outcome.is_success()); assert!( outcome.tx_gas_used() < 1_000, "system contract should be warm before execution, got {} gas used", @@ -361,7 +368,7 @@ mod tests { #[test] fn system_call_to_missing_code_is_noop() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let mut evm = TestEvm::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -372,14 +379,14 @@ mod tests { let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().detach(); - assert!(result.result.status); + assert!(result.result.is_success()); assert_eq!(result.result.tx_gas_used(), 0); assert!(!result.pending_state.is_changed()); } #[test] fn system_call_reverts_state_changes() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::PUSH1, 1, @@ -404,7 +411,7 @@ mod tests { let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().detach(); - assert!(!result.result.status); + assert!(!result.result.is_success()); assert_eq!(result.result.stop, InstrStop::Revert); assert!(!result.pending_state.is_changed()); } @@ -463,7 +470,7 @@ mod tests { #[case] gas_limit: Option, #[case] expected_limit: u64, ) { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); // Return the gas available at the start of execution (after GAS charges its own cost). let code = Bytecode::new_legacy(Bytes::from_static(&[ op::GAS, @@ -490,15 +497,15 @@ mod tests { let result = evm.system_call(tx).unwrap().discard(); - assert!(result.status); + assert!(result.is_success()); assert_eq!(U256::from_be_slice(&result.output), U256::from(expected_limit - 2)); assert_eq!(result.total_gas_spent, 15); - assert_eq!(result.state_gas_spent, 0); + assert_eq!(result.state_gas_spent(), 0); } #[test] fn system_call_exhausts_configured_gas_limit() { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[ op::PUSH1, 1, @@ -521,12 +528,72 @@ mod tests { .unwrap() .detach(); - assert!(!result.result.status); + assert!(!result.result.is_success()); assert_eq!(result.result.stop, InstrStop::OutOfGas); assert_eq!(result.result.total_gas_spent, 3_000); assert!(!result.pending_state.is_changed()); } + #[test] + fn system_call_total_includes_reservoir_funded_state_gas() { + let contract = Address::repeat_byte(0x42); + let code = Bytecode::new_legacy(Bytes::from_static(&[ + op::PUSH1, + 1, + op::PUSH0, + op::SSTORE, + op::STOP, + ])); + let mut database = InMemoryDB::default(); + database.insert_account_info(&contract, AccountInfo::default().with_code(code)); + let mut evm = TestEvm::new( + SpecId::AMSTERDAM, + BlockEnvExt::default(), + TxRegistry::new(), + database, + Precompiles::base(SpecId::AMSTERDAM), + ); + + let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().discard(); + + // One new slot: its state gas comes from the system call's reservoir, the regular gas + // (PUSH1, PUSH0, cold SSTORE) from `gas_limit`. + let state_gas = evm.version().gas_params.get(GasId::SstoreSetState); + let regular_gas = 3 + 2 + 2_100 + 10_000; + assert!(result.is_success()); + assert_eq!(result.state_gas_spent(), state_gas); + assert_eq!(result.total_gas_spent, regular_gas + state_gas); + assert_eq!(result.execution_gas_spent(), regular_gas); + } + + #[test] + fn system_call_total_covers_state_gas_with_unbounded_gas_limit() { + let contract = Address::repeat_byte(0x42); + let code = Bytecode::new_legacy(Bytes::from_static(&[ + op::PUSH1, + 1, + op::PUSH0, + op::SSTORE, + op::STOP, + ])); + let mut database = InMemoryDB::default(); + database.insert_account_info(&contract, AccountInfo::default().with_code(code)); + let mut evm = TestEvm::new( + SpecId::AMSTERDAM, + BlockEnvExt::default(), + TxRegistry::new(), + database, + Precompiles::base(SpecId::AMSTERDAM), + ); + + let tx = SystemTx::new(contract, Bytes::new()).with_gas_limit(u64::MAX); + let result = evm.system_call(tx).unwrap().discard(); + + assert!(result.is_success()); + assert!(result.total_gas_spent >= result.state_gas_spent()); + assert_eq!(result.execution_gas_spent(), 3 + 2 + 2_100 + 10_000); + } + #[derive(Debug, Default)] struct CountingInspector { calls: usize, @@ -566,7 +633,7 @@ mod tests { #[case] expected_calls: usize, #[case] expected_steps: usize, ) { - let contract = Address::from([0x42; 20]); + let contract = Address::repeat_byte(0x42); let code = Bytecode::new_legacy(Bytes::from_static(&[op::PUSH1, 1, op::PUSH0, op::STOP])); let mut database = InMemoryDB::default(); database.insert_account_info(&contract, AccountInfo::default().with_code(code)); @@ -581,7 +648,7 @@ mod tests { evm.set_inspect_system_calls(inspect); let result = evm.system_call(SystemTx::new(contract, Bytes::new())).unwrap().detach(); - assert!(result.result.status); + assert!(result.result.is_success()); let inspector = evm.clear_inspector_as::().expect("inspector kept"); assert_eq!(inspector.calls, expected_calls); diff --git a/crates/evm2/src/evm/tx.rs b/crates/evm2/src/evm/tx.rs index c304d5664..eeac119ef 100644 --- a/crates/evm2/src/evm/tx.rs +++ b/crates/evm2/src/evm/tx.rs @@ -98,12 +98,12 @@ impl TxResultExt { /// Returns whether execution ended with the `REVERT` opcode. /// - /// Unlike [`InstrStop::is_revert`], this does not count [`InstrStop::CallTooDeep`] or - /// [`InstrStop::OutOfFunds`]. Those are call-frame failures that the interpreter treats as - /// reverts, but a transaction result reports them as halts. + /// Unlike [`InstrStop::is_frame_revert`], this does not count [`InstrStop::CallTooDeep`], + /// [`InstrStop::OutOfFunds`] or [`InstrStop::NonceOverflow`]. Those are call-frame failures + /// that the interpreter treats as reverts, but a transaction result reports them as halts. #[inline] pub const fn is_revert(&self) -> bool { - !self.status && matches!(self.stop, InstrStop::Revert) + !self.status && self.stop.is_revert() } /// Returns whether execution failed for a reason other than the `REVERT` opcode. @@ -216,11 +216,8 @@ impl<'evm, 'host, T: EvmTypesHost> ExecutedTx<'evm, 'host, T> { } #[inline] - fn take_result(&mut self) -> TxResult { - match self.result.take() { - Some(result) => result, - None => unreachable!("executed transaction result was already taken"), - } + const fn take_result(&mut self) -> TxResult { + self.result.take().expect("executed transaction result was already taken") } #[inline] @@ -428,8 +425,13 @@ mod tests { let revert = with_stop(false, InstrStop::Revert); assert!(!revert.is_success() && revert.is_revert() && !revert.is_halt()); - // Counted by `InstrStop::is_revert`, but not a top-level `REVERT`. - for stop in [InstrStop::OutOfFunds, InstrStop::CallTooDeep, InstrStop::OutOfGas] { + // Counted by `InstrStop::is_frame_revert`, but not a top-level `REVERT`. + for stop in [ + InstrStop::OutOfFunds, + InstrStop::CallTooDeep, + InstrStop::NonceOverflow, + InstrStop::OutOfGas, + ] { let halt = with_stop(false, stop); assert!(!halt.is_success() && !halt.is_revert() && halt.is_halt(), "{stop:?}"); } diff --git a/crates/evm2/src/interpreter/dispatch/mod.rs b/crates/evm2/src/interpreter/dispatch/mod.rs index d011f3990..921e3db94 100644 --- a/crates/evm2/src/interpreter/dispatch/mod.rs +++ b/crates/evm2/src/interpreter/dispatch/mod.rs @@ -42,7 +42,7 @@ const fn inc_pc(pc: &mut Pc, op: u8) { } #[inline(always)] -const fn instruction_len(op: u8) -> usize { +pub(crate) const fn instruction_len(op: u8) -> usize { match op { op::JUMP | op::JUMPI => 0, // Set inside. op::PUSH1..=op::PUSH32 => (op - op::PUSH1 + 2) as usize, @@ -217,8 +217,15 @@ const fn instruction_changed( } trait InspectMode { + /// Whether the dispatch calls the inspector's step hooks: from the handlers on the tail-call + /// backend, and from the loop on the others, where a hook must not run while a handler holds + /// the stack borrow. const INSPECT: bool; + /// Whether the inspector steps every opcode, so the dispatch loop need not look at the set. + #[cfg(not(tco))] + const ALL_STEPS: bool = false; + fn step(state: &mut InterpreterState<'_, '_, T>, pc: Pc, stack_len: usize); fn step_end(state: &mut InterpreterState<'_, '_, T>, pc: Pc, stack_len: usize); @@ -236,10 +243,14 @@ impl InspectMode for NoInspector { fn step_end(_state: &mut InterpreterState<'_, '_, T>, _pc: Pc, _stack_len: usize) {} } -struct DynInspector; +/// Mode of the inspect table's handlers. `ALL` is the loop mode of an inspector that steps every +/// opcode. +struct DynInspector; -impl InspectMode for DynInspector { +impl InspectMode for DynInspector { const INSPECT: bool = true; + #[cfg(not(tco))] + const ALL_STEPS: bool = ALL; #[inline(always)] fn step(state: &mut InterpreterState<'_, '_, T>, pc: Pc, stack_len: usize) { diff --git a/crates/evm2/src/interpreter/dispatch/table/mod.rs b/crates/evm2/src/interpreter/dispatch/table/mod.rs index 033929309..fcc14d459 100644 --- a/crates/evm2/src/interpreter/dispatch/table/mod.rs +++ b/crates/evm2/src/interpreter/dispatch/table/mod.rs @@ -1,7 +1,10 @@ use super::{DynInspector, InspectMode, NoInspector, inc_pc, run_state}; use crate::{ - EvmConfig, EvmTypesHost, - interpreter::{InstrStop, Interpreter, InterpreterState, Pc, RawStack, Result, StackMut}, + EvmConfig, EvmTypesHost, OpcodeSet, + interpreter::{ + InstrStop, Interpreter, InterpreterState, Pc, RawStack, Result, StackMut, + private::InstructionFlags, + }, }; use core::hint::cold_path; @@ -10,10 +13,6 @@ core::cfg_select! { mod packed; use packed as imp; } - dispatch_single_return => { - mod single_return; - use single_return as imp; - } _ => { mod unpacked; use unpacked as imp; @@ -109,14 +108,21 @@ fn dispatch_inner, M: InspectMode, G: Dispat ) -> (Pc, G) { let instruction = C::OPCODE_CONFIG.instruction(op); let instr = instruction.instr; - let dynamic_gas = instruction.dynamic_gas; + let dynamic_gas = instruction.flags.contains(InstructionFlags::DYNAMIC_GAS); + let hooked = instruction.flags.contains(InstructionFlags::HOOKED); let r; match gas.pre_step::(state, op) { Ok(()) => { gas.sync_before_exec(state, dynamic_gas); + if hooked { + state.set_pc_stack_len(pc.as_ptr(), *stack.len); + } r = instr(&mut pc, stack.reborrow(), state); if r.is_ok() { inc_pc(&mut pc, op); + if hooked { + state.note_inspector_config_change(); + } } gas.sync_after_exec(state, dynamic_gas); } @@ -131,6 +137,12 @@ fn dispatch_inner, M: InspectMode, G: Dispat state.set_result(Err(e)); cold_path(); return (Pc::new(core::ptr::null()), gas); + } else if hooked && state.reload_requested() { + // The frame continues after the instruction on the table for the new config. + cold_path(); + state.set_result(Err(InstrStop::Reload)); + state.set_pc_stack_len(pc.as_ptr(), *stack.len); + return (Pc::new(core::ptr::null()), gas); } (pc, gas) } @@ -141,8 +153,12 @@ pub(in crate::interpreter) fn run( instructions: &RawInstrTable, ) -> InstrStop { let (state, pc, stack) = run_state(interpreter); - if state.is_inspecting() { - return run_inner::(state, pc, stack, instructions); + // An inspector that steps nothing runs the plain loop. + if state.is_inspecting() && state.steps() != OpcodeSet::EMPTY { + if state.steps() == OpcodeSet::ALL { + return run_inner::>(state, pc, stack, instructions); + } + return run_inner::>(state, pc, stack, instructions); } run_inner::(state, pc, stack, instructions) } @@ -156,37 +172,100 @@ fn run_inner>( ) -> InstrStop { let mut loop_state = imp::loop_state(state.gas_mut()); let mut cursor = imp::Cursor::new(pc, stack.len()); + let steps = if M::INSPECT { state.steps() } else { OpcodeSet::EMPTY }; + // Without this, the loop's offset in its 64-byte line depends on unrelated code size, and a + // loop that crosses a line costs about 20%. + #[cfg(all(not(miri), any(target_arch = "x86_64", target_arch = "aarch64")))] + // SAFETY: The directive only pads with nops. It reads and writes no memory or registers. + unsafe { + core::arch::asm!(".p2align 6", options(nomem, nostack, preserves_flags)); + } loop { - if M::INSPECT { - imp::sync_loop_state(state, loop_state); - M::step(state, cursor.pc(), cursor.stack_len()); - loop_state = imp::loop_state(state.gas_mut()); - if state.result().is_err() { - return finish_run(state, cursor, loop_state); - } - } - let op = cursor.pc().op(); - let instr = instructions[op as usize]; - cursor = { - // SAFETY: This is the only stack borrow outside inspector callbacks. - let stack_view = unsafe { stack.borrow() }; - imp::dispatch_loop_call(instr, cursor, stack_view.stack, state, &mut loop_state) - }; - - if M::INSPECT { - imp::sync_loop_state(state, loop_state); - M::step_end(state, cursor.pc(), cursor.stack_len()); - loop_state = imp::loop_state(state.gas_mut()); - if state.result().is_err() { + // A sparse set's table holds inspect handlers at the stepped opcodes and plain handlers + // elsewhere, so each path calls the handlers it matches. + if M::INSPECT && (M::ALL_STEPS || steps.contains(op)) { + let stepped = if M::ALL_STEPS { + run_stepped::(state, cursor, stack, instructions[op as usize], loop_state) + } else { + run_stepped_out_of_line::( + state, + cursor, + stack, + instructions[op as usize], + loop_state, + ) + }; + match stepped { + Ok(next) => (cursor, loop_state) = next, + Err(stop) => return stop, + } + } else { + cursor = { + // SAFETY: This is the only stack borrow outside inspector callbacks. + let stack_view = unsafe { stack.borrow() }; + imp::dispatch_loop_call( + instructions[op as usize], + cursor, + stack_view.stack, + state, + &mut loop_state, + ) + }; + if cursor.pc().as_ptr().is_null() { return finish_run(state, cursor, loop_state); } - } else if cursor.pc().as_ptr().is_null() { - return finish_run(state, cursor, loop_state); } } } +/// Runs one instruction between its step hooks, or returns the stop that ended the frame. +/// +/// The hooks run here, outside the stack borrow, because a hook may access the stack through the +/// interpreter and no handler may hold the borrow meanwhile. +#[inline(always)] +fn run_stepped>( + state: &mut InterpreterState<'_, '_, T>, + mut cursor: imp::Cursor, + mut stack: RawStack<'_>, + instr: imp::RawInstrFn, + mut loop_state: imp::LoopState, +) -> core::result::Result<(imp::Cursor, imp::LoopState), InstrStop> { + imp::sync_loop_state(state, loop_state); + M::step(state, cursor.pc(), cursor.stack_len()); + loop_state = imp::loop_state(state.gas_mut()); + if state.result().is_err() { + return Err(finish_run(state, cursor, loop_state)); + } + + cursor = { + // SAFETY: This is the only stack borrow outside inspector callbacks. + let stack_view = unsafe { stack.borrow() }; + imp::dispatch_loop_call(instr, cursor, stack_view.stack, state, &mut loop_state) + }; + + imp::sync_loop_state(state, loop_state); + M::step_end(state, cursor.pc(), cursor.stack_len()); + loop_state = imp::loop_state(state.gas_mut()); + if state.result().is_err() { + return Err(finish_run(state, cursor, loop_state)); + } + Ok((cursor, loop_state)) +} + +/// [`run_stepped`] behind a call, so that the registers the loop holds for the unstepped +/// instructions of a sparse set are not shared with the hook calls. +#[inline(never)] +fn run_stepped_out_of_line>( + state: &mut InterpreterState<'_, '_, T>, + cursor: imp::Cursor, + stack: RawStack<'_>, + instr: imp::RawInstrFn, + loop_state: imp::LoopState, +) -> core::result::Result<(imp::Cursor, imp::LoopState), InstrStop> { + run_stepped::(state, cursor, stack, instr, loop_state) +} + #[inline(always)] fn finish_run( state: &mut InterpreterState<'_, '_, T>, @@ -196,7 +275,7 @@ fn finish_run( cold_path(); let (pc, stack_len) = (cursor.pc(), cursor.stack_len()); // The NULL `pc` is only a loop-exit sentinel; never publish it. - let pc = if pc.as_ptr().is_null() { state.bytecode().as_ptr() } else { pc.as_ptr() }; + let pc = if pc.as_ptr().is_null() { state.0.pc } else { pc.as_ptr() }; state.set_pc_stack_len(pc, stack_len); imp::finish_loop(state.gas_mut(), loop_state); state.result().unwrap_err() diff --git a/crates/evm2/src/interpreter/dispatch/table/packed.rs b/crates/evm2/src/interpreter/dispatch/table/packed.rs index 4753e6d89..2d2fbd2f6 100644 --- a/crates/evm2/src/interpreter/dispatch/table/packed.rs +++ b/crates/evm2/src/interpreter/dispatch/table/packed.rs @@ -73,7 +73,7 @@ impl super::DispatchGas for RemainingGas { dynamic_gas: bool, ) { if dynamic_gas { - state.gas_mut().set_remaining(self.get()); + sync_loop_state(state, *self); } } diff --git a/crates/evm2/src/interpreter/dispatch/table/single_return.rs b/crates/evm2/src/interpreter/dispatch/table/single_return.rs deleted file mode 100644 index ada74a726..000000000 --- a/crates/evm2/src/interpreter/dispatch/table/single_return.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::{ - EvmConfig, EvmTypesHost, - interpreter::{InterpreterState, Pc, Stack, StackBacking, StackMut, gas::Gas}, -}; - -pub(super) type LoopState = (); - -pub(super) type Cursor = (Pc, usize); - -/// Single-return instruction function pointer. -pub(in crate::interpreter::dispatch) type RawInstrFn = - extern_table!(fn(pc: Pc, stack: StackMut<'_>, state: &mut InterpreterState<'_, '_, T>) -> Pc); - -#[inline(always)] -pub(super) fn dispatch_loop_call( - instr: RawInstrFn, - (pc, stack_len): Cursor, - stack: &mut StackBacking, - state: &mut InterpreterState<'_, '_, T>, - _loop_state: &mut LoopState, -) -> Cursor { - let mut stack = Stack::new(stack, stack_len); - let next_pc = instr(pc, stack.as_mut(), state); - (next_pc, stack.len) -} - -#[inline(always)] -pub(super) const fn loop_state(_gas: &Gas) -> LoopState {} - -#[inline(always)] -pub(super) const fn finish_loop(_gas: &mut Gas, _loop_state: LoopState) {} - -#[inline(always)] -pub(super) const fn sync_loop_state( - _state: &mut InterpreterState<'_, '_, T>, - _loop_state: LoopState, -) { -} - -extern_table! { - pub(in crate::interpreter::dispatch) fn dispatch< - T: EvmTypesHost, - C: EvmConfig, - M: super::InspectMode, - const OP: u8, - >( - pc: Pc, - stack: StackMut<'_>, - state: &mut InterpreterState<'_, '_, T>, - ) -> Pc { - let (pc, ()) = super::dispatch_inner::(pc, stack, (), state, OP); - pc - } -} diff --git a/crates/evm2/src/interpreter/dispatch/table/unpacked.rs b/crates/evm2/src/interpreter/dispatch/table/unpacked.rs index 7399fe647..b0de375a4 100644 --- a/crates/evm2/src/interpreter/dispatch/table/unpacked.rs +++ b/crates/evm2/src/interpreter/dispatch/table/unpacked.rs @@ -1,3 +1,4 @@ +use super::Cursor as _; use crate::{ EvmConfig, EvmTypesHost, interpreter::{InterpreterState, Pc, Stack, StackBacking, gas::Gas}, @@ -5,10 +6,43 @@ use crate::{ pub(super) type LoopState = (); -pub(super) type Cursor = (Pc, usize); +core::cfg_select! { + all(target_family = "wasm", target_pointer_width = "32") => { + pub(super) type Cursor = WasmCursor; + + /// Program counter in the high 32 bits and stack length in the low 32 bits. + /// + /// Rust's wasm ABI returns a `(Pc, usize)` pair through a result pointer in linear memory: + /// every handler takes an extra pointer argument and stores both values, and the dispatch + /// loop loads them back. A single `u64` is returned as one wasm `i64` result instead. + #[derive(Clone, Copy)] + #[repr(transparent)] + pub(crate) struct WasmCursor(u64); + + impl super::Cursor for WasmCursor { + #[inline(always)] + fn new(pc: Pc, stack_len: usize) -> Self { + Self(((pc.as_ptr() as usize as u64) << 32) | stack_len as u64) + } + + #[inline(always)] + fn pc(self) -> Pc { + Pc::new((self.0 >> 32) as usize as *const u8) + } + + #[inline(always)] + fn stack_len(self) -> usize { + self.0 as u32 as usize + } + } + } + _ => { + pub(super) type Cursor = (Pc, usize); + } +} /// Unpacked instruction return value. -type InstrFnRet = (Pc, usize); +type InstrFnRet = Cursor; /// Unpacked instruction function pointer. pub(in crate::interpreter::dispatch) type RawInstrFn = extern_table!( @@ -18,12 +52,12 @@ pub(in crate::interpreter::dispatch) type RawInstrFn = extern_table!( #[inline(always)] pub(super) fn dispatch_loop_call( instr: RawInstrFn, - (pc, stack_len): Cursor, + cursor: Cursor, stack: &mut StackBacking, state: &mut InterpreterState<'_, '_, T>, _loop_state: &mut LoopState, ) -> Cursor { - instr(pc, Stack::new(stack, stack_len), state) + instr(cursor.pc(), Stack::new(stack, cursor.stack_len()), state) } #[inline(always)] @@ -51,6 +85,6 @@ extern_table! { state: &mut InterpreterState<'_, '_, T>, ) -> InstrFnRet { let (pc, ()) = super::dispatch_inner::(pc, stack.as_mut(), (), state, OP); - (pc, stack.len) + Cursor::new(pc, stack.len) } } diff --git a/crates/evm2/src/interpreter/dispatch/tco.rs b/crates/evm2/src/interpreter/dispatch/tco.rs index 47251a224..868b89eec 100644 --- a/crates/evm2/src/interpreter/dispatch/tco.rs +++ b/crates/evm2/src/interpreter/dispatch/tco.rs @@ -2,8 +2,9 @@ use super::{InspectMode, run_state}; use crate::{ EvmConfig, EvmTypesHost, interpreter::{ - InstrStop, Interpreter, InterpreterState, Pc, RawStack, Result, gas::RemainingGas, - private::InstructionImplFn, + InstrStop, Interpreter, InterpreterState, Pc, RawStack, Result, + gas::RemainingGas, + private::{InstructionFlags, InstructionImplFn}, }, }; use core::hint::cold_path; @@ -40,6 +41,9 @@ pub(in crate::interpreter) fn run( } extern_table! { + // Without alignment, each handler's offset inside its 64-byte line depends on unrelated code + // size. Aligned handlers run faster and vary less across layouts. + #[cfg_attr(target_arch = "x86_64", rustc_align(64))] pub(super) fn dispatch< T: EvmTypesHost, C: EvmConfig, @@ -54,8 +58,11 @@ extern_table! { ) { let instruction = C::OPCODE_CONFIG.instruction(OP); let instr: InstructionImplFn = instruction.instr; - let dynamic_gas = instruction.dynamic_gas; + let dynamic_gas = instruction.flags.contains(InstructionFlags::DYNAMIC_GAS); + let hooked = instruction.flags.contains(InstructionFlags::HOOKED); if M::INSPECT { + // Handlers that skip the hooks keep the gas in a register. + state.gas_mut().set_remaining(remaining_gas.get()); M::step(state, pc, stack.len()); remaining_gas.set(state.gas_mut().remaining()); if state.result().is_err() { @@ -76,6 +83,9 @@ extern_table! { if M::INSPECT || dynamic_gas { state.gas_mut().set_remaining(remaining_gas.get()); } + if hooked { + state.set_pc_stack_len(pc.as_ptr(), stack.len()); + } let (r, stack_len) = { // SAFETY: This is the only stack borrow outside inspector callbacks. let mut stack_view = unsafe { stack.borrow() }; @@ -96,6 +106,15 @@ extern_table! { tail_return!(tail_call_restore(pc, stack, remaining_gas, state, instructions)); } super::inc_pc(&mut pc, OP); + if hooked { + state.note_inspector_config_change(); + if !M::INSPECT && state.reload_requested() { + // The frame continues after the instruction on the table for the new config. + cold_path(); + state.set_result(Err(InstrStop::Reload)); + tail_return!(tail_call_restore(pc, stack, remaining_gas, state, instructions)); + } + } if M::INSPECT { M::step_end(state, pc, stack.len()); remaining_gas.set(state.gas_mut().remaining()); diff --git a/crates/evm2/src/interpreter/gas.rs b/crates/evm2/src/interpreter/gas.rs index 6689d3d0e..88e975360 100644 --- a/crates/evm2/src/interpreter/gas.rs +++ b/crates/evm2/src/interpreter/gas.rs @@ -160,7 +160,7 @@ impl GasTracker { /// Creates a gas tracker with `limit` execution gas. #[inline] pub const fn new(limit: u64) -> Self { - Self::from_parts(limit, limit, 0) + Self::new_with_execution_gas_and_reservoir(limit, 0) } /// Creates a gas tracker with execution gas and a state gas reservoir. @@ -265,6 +265,16 @@ impl GasTracker { self.state_gas_spilled } + /// Returns the remaining gas plus the state gas drawn from it, which is the remaining gas had + /// the reservoir covered every state charge. + /// + /// The difference between two readings is the execution gas charged in between: a spill of + /// state gas into the remaining gas cancels out, as does a refill of it. + #[inline] + pub const fn remaining_excluding_state_spill(&self) -> u64 { + self.remaining.saturating_add(self.state_gas_spilled) + } + /// Adds `delta` to the spilled state gas, saturating. /// /// Used to merge a successful child frame's spilled state gas into this @@ -339,7 +349,7 @@ impl GasTracker { #[inline] pub fn spend_state(&mut self, cost: u64) -> Result { if self.reservoir >= cost { - self.state_gas_spent = self.state_gas_spent.saturating_add(cost as i64); + self.add_state_gas_spent(cost as i64); self.reservoir -= cost; return Ok(()); } @@ -347,8 +357,8 @@ impl GasTracker { let spill = cost - self.reservoir; self.spend(spill)?; - self.state_gas_spent = self.state_gas_spent.saturating_add(cost as i64); - self.state_gas_spilled = self.state_gas_spilled.saturating_add(spill); + self.add_state_gas_spent(cost as i64); + self.add_state_gas_spilled(spill); self.reservoir = 0; Ok(()) } @@ -384,7 +394,7 @@ impl GasTracker { .reservoir .saturating_add_signed(self.state_gas_spent) .saturating_sub(self.state_gas_spilled); - self.remaining = self.remaining.saturating_add(self.state_gas_spilled); + self.remaining = self.remaining_excluding_state_spill(); self.state_gas_spent = 0; self.state_gas_spilled = 0; } @@ -459,7 +469,7 @@ impl GasTracker { /// revert/halt the child's state changes roll back, so it contributes none. #[inline] pub const fn merge_child_gas(&mut self, child: Self, stop: InstrStop) { - if stop.is_success() || stop.is_revert() { + if stop.is_success() || stop.is_frame_revert() { self.erase_cost(child.remaining); } if stop.is_success() { @@ -629,6 +639,13 @@ impl Gas { self.tracker.state_gas_spilled() } + /// Returns the remaining gas plus the state gas drawn from it. + /// See [`GasTracker::remaining_excluding_state_spill`]. + #[inline] + pub const fn remaining_excluding_state_spill(&self) -> u64 { + self.tracker.remaining_excluding_state_spill() + } + /// Adds `delta` to the spilled state gas, saturating. /// See [`GasTracker::add_state_gas_spilled`]. #[inline] @@ -832,6 +849,24 @@ mod tests { assert_eq!((gas.state_gas_spilled(), gas.remaining()), (400, 600)); } + #[test] + fn test_remaining_excluding_state_spill() { + let mut gas = Gas::new_with_execution_gas_and_reservoir(1000, 500); + assert_eq!(gas.remaining_excluding_state_spill(), 1000); + + // A charge the reservoir covers and a spill into the remaining gas do not move it. + assert!(gas.spend_state(300).is_ok()); + assert!(gas.spend_state(500).is_ok()); + assert_eq!((gas.remaining(), gas.state_gas_spilled()), (700, 300)); + assert_eq!(gas.remaining_excluding_state_spill(), 1000); + + // Execution gas does, and a refill of the spill does not. + assert!(gas.spend(100).is_ok()); + assert_eq!(gas.remaining_excluding_state_spill(), 900); + gas.refill_reservoir(300); + assert_eq!(gas.remaining_excluding_state_spill(), 900); + } + #[test] fn test_rollback_state_gas_no_spill() { // Pure-reservoir spend: unwind restores the reservoir, leaving execution gas alone. @@ -924,11 +959,11 @@ mod tests { fn sibling_refill_restores_parent_regular_gas() { const STATE_GAS: u64 = 200; const CHILD_GAS: u64 = 500; - let mut parent = GasTracker::from_parts(1_000, 1_000, 0); + let mut parent = GasTracker::new(1_000); // A's state creation spills into execution gas and is merged into P. parent.spend(CHILD_GAS).unwrap(); - let mut child_a = GasTracker::from_parts(CHILD_GAS, CHILD_GAS, 0); + let mut child_a = GasTracker::new(CHILD_GAS); child_a.spend_state(STATE_GAS).unwrap(); child_a.settle_gas(InstrStop::Stop); parent.merge_child_gas(child_a, InstrStop::Stop); @@ -939,7 +974,8 @@ mod tests { // B clears A's slot, with no local spill counter to restore. parent.spend(CHILD_GAS).unwrap(); - let mut child_b = GasTracker::from_parts(CHILD_GAS, CHILD_GAS, parent.reservoir()); + let mut child_b = + GasTracker::new_with_execution_gas_and_reservoir(CHILD_GAS, parent.reservoir()); child_b.refill_reservoir(STATE_GAS); assert_eq!(child_b.reservoir(), STATE_GAS); assert_eq!(child_b.state_gas_spilled(), 0); diff --git a/crates/evm2/src/interpreter/host.rs b/crates/evm2/src/interpreter/host.rs index c7722002d..e44eb6079 100644 --- a/crates/evm2/src/interpreter/host.rs +++ b/crates/evm2/src/interpreter/host.rs @@ -1,9 +1,9 @@ use super::{GasTracker, InstrStop, Message, Result, Word}; use crate::{ - BaseEvmTypes, DatabaseError, EvmFeatures, EvmTypesHost, ExecutionConfig, ExecutionError, - HostError, SpecId, + BaseEvmTypes, DatabaseError, EvmTypesHost, ExecutionConfig, ExecutionError, HostError, SpecId, env::{BlockEnv, TxEnv}, evm::{AccountLoad, SLoad, SStore, SelfDestructResult}, + utils::address_to_word, }; use alloc::borrow::Cow; use alloy_primitives::{Address, B256, Bytes}; @@ -61,7 +61,7 @@ impl MessageResultExt { pub fn created_address_for_parent(&self) -> Word { self.created_address .filter(|_| self.stop.is_success()) - .map(|address| Word::from_be_slice(address.as_slice())) + .map(|address| address_to_word(&address)) .unwrap_or_default() } @@ -71,7 +71,6 @@ impl MessageResultExt { if self.gas.refunded() <= 0 { return 0; } - let max_refund_quotient = if max_refund_quotient == 0 { 1 } else { max_refund_quotient }; // EIP-8037: the unused state-gas reservoir is reimbursed to the caller, so it // is not part of the gas actually spent. The EIP-3529 refund cap is a fraction // of the gas the transaction truly consumed, so the reservoir must be excluded @@ -118,7 +117,8 @@ pub trait Host { /// Returns the active execution configuration. /// /// The interpreter keeps a reference to the returned version for the whole run, so the host - /// must not replace or mutate it while the interpreter runs. + /// must not replace or mutate it while the interpreter runs. An inspected run reads the config + /// again when its inspector changes the opcodes it steps. fn execution_config(&self) -> &ExecutionConfig; /// Returns the block environment. @@ -127,6 +127,19 @@ pub trait Host { /// must keep it at a stable address and unchanged while the interpreter runs. fn block_env(&mut self) -> &BlockEnv; + /// Returns the transaction environment. + /// + /// Compiled code keeps a pointer to the returned environment for the whole run, so the host + /// must keep it at a stable address while the interpreter runs. + fn tx_env(&mut self) -> &TxEnv; + + /// Returns the inspector-visible depth of top-level messages. + /// + /// The call depth limit and the nested-create nonce check are measured from this depth, and + /// inspectors compare against it to find the top-level message. It should be at most + /// `u16::MAX - CALL_DEPTH_LIMIT - 1` so the deepest message stays representable. + fn initial_call_depth(&self) -> u16; + /// Loads account information. fn load_account( &mut self, @@ -135,13 +148,6 @@ pub trait Host { skip_cold_load: bool, ) -> Result; - /// Returns whether an account is empty/non-existent for new-account gas checks. - fn target_is_empty_for_new_account_gas( - &mut self, - address: &Address, - features: EvmFeatures, - ) -> Result; - /// Returns a historical block hash. fn block_hash(&mut self, number: &Word) -> Result; @@ -177,7 +183,6 @@ pub trait Host { /// Executes a message inside this host. fn execute_message( &mut self, - tx_env: &TxEnv, message: &mut Message, ) -> Result, ExecutionError>; @@ -205,6 +210,5 @@ mod tests { assert_eq!(result.final_refund(100, 2), 40); assert_eq!(result.final_refund(100, 5), 16); assert_eq!(result.final_refund(100, 1), 60); - assert_eq!(result.final_refund(100, 0), 60); } } diff --git a/crates/evm2/src/interpreter/instructions/arithmetic.rs b/crates/evm2/src/interpreter/instructions/arithmetic.rs index 2b34db1b4..d84c91c81 100644 --- a/crates/evm2/src/interpreter/instructions/arithmetic.rs +++ b/crates/evm2/src/interpreter/instructions/arithmetic.rs @@ -56,7 +56,7 @@ pub fn exp(cx: _, [a, b]: [Word]) -> Result { #[instruction] pub fn signextend([ext, value]: [Word]) -> out { if *ext < 31 { - let bit_index = (8 * ext.as_limbs()[0] + 7) as usize; + let bit_index = (8 * ext.wrapping_to::() + 7) as usize; let mask = (Word::ONE << bit_index) - Word::ONE; out.write(if value.bit(bit_index) { *value | !mask } else { *value & mask }); } diff --git a/crates/evm2/src/interpreter/instructions/bitwise.rs b/crates/evm2/src/interpreter/instructions/bitwise.rs index 20e92074a..f95193088 100644 --- a/crates/evm2/src/interpreter/instructions/bitwise.rs +++ b/crates/evm2/src/interpreter/instructions/bitwise.rs @@ -192,7 +192,7 @@ mod tests { #[test] fn sar_opcode() { - assert_stack!(SAR(1, neg(1) - Word::from(1)), neg(1)); + assert_stack!(SAR(1, neg(1) - Word::ONE), neg(1)); assert_stack!(SAR(1, 4), 2); assert_stack!(SAR(256, neg(1)), neg(1)); } diff --git a/crates/evm2/src/interpreter/instructions/block.rs b/crates/evm2/src/interpreter/instructions/block.rs index fd27fbc6d..d760e239b 100644 --- a/crates/evm2/src/interpreter/instructions/block.rs +++ b/crates/evm2/src/interpreter/instructions/block.rs @@ -2,7 +2,7 @@ use crate::{ EvmFeatures, constants::BLOCK_HASH_HISTORY, interpreter::{Host, Word}, - utils::{WordExt, address_to_word, b256_to_word}, + utils::WordExt, }; use evm2_macros::instruction; @@ -12,7 +12,7 @@ pub fn blockhash(cx: _, [number]: [Word]) -> Result { if diff == 0 || diff > BLOCK_HASH_HISTORY { Word::ZERO } else { - b256_to_word(cx.state.host().block_hash(number).map_err(|error| cx.state.fail(error))?) + cx.state.host().block_hash(number).map_err(|error| cx.state.fail(error))?.into() } } else { Word::ZERO @@ -22,7 +22,7 @@ pub fn blockhash(cx: _, [number]: [Word]) -> Result { #[instruction] pub fn coinbase(cx: _) -> out { - out.write(address_to_word(&cx.state.host().block_env().beneficiary)); + out.write(cx.state.host().block_env().beneficiary.into_word().into()); } #[instruction] @@ -51,7 +51,7 @@ pub fn gaslimit(cx: _) -> out { #[instruction] pub fn chainid(cx: _) -> Result { - *out = cx.state.tx().chain_id; + *out = cx.state.host().tx_env().chain_id; } #[instruction] @@ -73,7 +73,7 @@ pub fn basefee(cx: _) -> Result { #[instruction] pub fn blobhash(cx: _, [index]: [Word]) -> Result { let index = index.to_usize_saturated(); - *out = cx.state.tx().blob_hashes.get(index).copied().unwrap_or_default(); + *out = cx.state.host().tx_env().blob_hashes.get(index).copied().unwrap_or_default(); } #[instruction] @@ -90,19 +90,15 @@ pub fn slotnum(cx: _) -> Result { mod tests { use crate::{ SpecId, - env::{BlockEnv, BlockEnvExt, TxEnvExt}, + env::{BlockEnvExt, TxEnvExt}, interpreter::{InstrStop, MessageExt, Word, op}, - test_utils::{RunConfig, TestHost, TestTypes, push, run}, + test_utils::{RunConfig, TestHost, push, run, test_host}, utils::{address_to_word, b256_to_word}, }; use alloc::vec::Vec; use alloy_primitives::{Address, B256}; use core::assert_matches; - fn test_host(block: BlockEnv) -> TestHost { - TestHost { block, ..TestHost::default() } - } - #[test] fn blockhash_opcode() { let mut host = test_host(BlockEnvExt { number: Word::from(10), ..BlockEnvExt::default() }); @@ -139,7 +135,7 @@ mod tests { #[test] fn coinbase_opcode() { - let beneficiary = Address::from([0x44; 20]); + let beneficiary = Address::repeat_byte(0x44); let mut host = test_host(BlockEnvExt { beneficiary, ..BlockEnvExt::default() }); let interp = run(RunConfig::new([op::COINBASE, op::STOP]).host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); @@ -168,7 +164,7 @@ mod tests { let randao = B256::with_last_byte(0x55); let mut host = test_host(BlockEnvExt { difficulty: Word::from(14), - prevrandao: b256_to_word(randao), + prevrandao: randao.into(), ..BlockEnvExt::default() }); let interp = @@ -194,18 +190,18 @@ mod tests { #[test] fn chainid_opcode() { let mut host = TestHost::default(); - let tx_env = TxEnvExt { chain_id: Word::from(1), ..TxEnvExt::default() }; + let tx_env = TxEnvExt { chain_id: Word::ONE, ..TxEnvExt::default() }; let interp = run(RunConfig::new([op::CHAINID, op::STOP]) .host(&mut host) .tx_env(tx_env) .spec(SpecId::ISTANBUL)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); } #[test] fn selfbalance_opcode() { - let address = Address::from([0x66; 20]); + let address = Address::repeat_byte(0x66); let mut host = TestHost::default(); let message = MessageExt { destination: address, gas_limit: 10_000, ..Default::default() }; let interp = @@ -227,8 +223,7 @@ mod tests { fn blobhash_opcode() { let hash = B256::with_last_byte(0x42); let mut host = TestHost::default(); - let tx_env = - TxEnvExt { blob_hashes: Vec::from([b256_to_word(hash)]), ..TxEnvExt::default() }; + let tx_env = TxEnvExt { blob_hashes: Vec::from([hash.into()]), ..TxEnvExt::default() }; let interp = run(RunConfig::new([op::PUSH0, op::BLOBHASH, op::STOP]) .host(&mut host) diff --git a/crates/evm2/src/interpreter/instructions/control.rs b/crates/evm2/src/interpreter/instructions/control.rs index e90b6d339..24b800b9e 100644 --- a/crates/evm2/src/interpreter/instructions/control.rs +++ b/crates/evm2/src/interpreter/instructions/control.rs @@ -77,9 +77,6 @@ fn return_inner( let Some(end) = offset.checked_add(len) else { return Err(InstrStop::MemoryOOG); }; - if end > u32::MAX as usize { - return Err(InstrStop::MemoryLimitOOG); - } cx.state.resize_memory(cx.gas, offset, len)?; offset as u32..end as u32 } else { @@ -211,7 +208,7 @@ mod tests { let interp = run(RunConfig::new([op::JUMPDEST, op::PC, op::STOP])); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); } #[test] @@ -260,10 +257,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Return); assert_eq!(interp.output(), [0xab, 0, 0]); - let interp = run_stack([Word::from(0), Word::MAX], op::RETURN); + let interp = run_stack([Word::ZERO, Word::MAX], op::RETURN); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); - let interp = run_stack([Word::from(u32::MAX), Word::from(1)], op::RETURN); + let interp = run_stack([Word::from(u32::MAX), Word::ONE], op::RETURN); assert_matches!(interp.err, InstrStop::MemoryLimitOOG); } @@ -290,10 +287,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Revert); assert_eq!(interp.output(), [0xcd, 0]); - let interp = run_stack([Word::from(0), Word::MAX], op::REVERT); + let interp = run_stack([Word::ZERO, Word::MAX], op::REVERT); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); - let interp = run_stack([Word::from(u32::MAX), Word::from(1)], op::REVERT); + let interp = run_stack([Word::from(u32::MAX), Word::ONE], op::REVERT); assert_matches!(interp.err, InstrStop::MemoryLimitOOG); } } diff --git a/crates/evm2/src/interpreter/instructions/crypto.rs b/crates/evm2/src/interpreter/instructions/crypto.rs index 46145357c..200cbebbb 100644 --- a/crates/evm2/src/interpreter/instructions/crypto.rs +++ b/crates/evm2/src/interpreter/instructions/crypto.rs @@ -1,4 +1,4 @@ -use crate::utils::{WordExt, b256_to_word}; +use crate::utils::WordExt; use alloy_primitives::{KECCAK256_EMPTY, keccak256 as keccak256_hash}; use evm2_macros::instruction; @@ -13,7 +13,7 @@ pub fn keccak256(cx: _, [offset, len]: [Word]) -> Result { cx.state.resize_memory(cx.gas, offset, len)?; keccak256_hash(cx.state.memory().slice(offset, len)) }; - *out = b256_to_word(hash); + *out = hash.into(); } #[cfg(test)] @@ -42,7 +42,7 @@ mod tests { push(&mut code, Word::from(0x80)); push(&mut code, 0); code.push(op::MSTORE8); - push(&mut code, Word::from(1)); + push(&mut code, Word::ONE); push(&mut code, 0); code.push(op::KECCAK256); code.push(op::STOP); @@ -50,11 +50,11 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [b256_to_word(keccak256([0x80]))]); - let interp = run_stack([Word::MAX, Word::from(0)], op::KECCAK256); + let interp = run_stack([Word::MAX, Word::ZERO], op::KECCAK256); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [b256_to_word(keccak256([]))]); - let interp = run_stack([Word::MAX, Word::from(1)], op::KECCAK256); + let interp = run_stack([Word::MAX, Word::ONE], op::KECCAK256); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } } diff --git a/crates/evm2/src/interpreter/instructions/env.rs b/crates/evm2/src/interpreter/instructions/env.rs index d88807e6b..deb064caf 100644 --- a/crates/evm2/src/interpreter/instructions/env.rs +++ b/crates/evm2/src/interpreter/instructions/env.rs @@ -2,7 +2,7 @@ use crate::{ EvmTypesHost, evm::AccountLoad, interpreter::{Gas, Host, InstrStop, Memory, Result, Word, private::GasInstructionCx}, - utils::{WordExt, address_to_word, b256_to_word, read_word_padded}, + utils::{WordExt, read_word_padded}, version::GasParams, }; use evm2_macros::instruction; @@ -46,7 +46,7 @@ fn copy_data( #[instruction] pub fn address(cx: _) -> out { - out.write(address_to_word(&cx.state.message().destination)); + out.write(cx.state.message().destination.into_word().into()); } #[instruction(dynamic_gas)] @@ -56,12 +56,12 @@ pub fn balance(cx: _, [addr]: [Word]) -> Result { #[instruction] pub fn origin(cx: _) -> out { - out.write(address_to_word(&cx.state.tx().origin)); + out.write(cx.state.host().tx_env().origin.into_word().into()); } #[instruction] pub fn caller(cx: _) -> out { - out.write(address_to_word(&cx.state.message().caller)); + out.write(cx.state.message().caller.into_word().into()); } #[instruction] @@ -72,12 +72,12 @@ pub fn callvalue(cx: _) -> out { #[instruction] pub fn calldataload(cx: _, [offset]: [Word]) -> out { let offset = offset.to_usize_saturated(); - *out = b256_to_word(read_word_padded(&cx.state.message().input, offset)); + *out = read_word_padded(cx.state.input(), offset).into(); } #[instruction] pub fn calldatasize(cx: _) -> out { - out.write_u64(cx.state.message().input.len() as u64); + out.write_u64(cx.state.input().len() as u64); } #[instruction(dynamic_gas)] @@ -86,7 +86,7 @@ pub fn calldatacopy(cx: _, [memory_offset, data_offset, len]: [Word]) -> Result // SAFETY: Charging and copying data make no host calls. let gas_params = unsafe { cx.state.gas_params_detached() }; cx.gas.spend(gas_params.copy_cost(len))?; - let input = cx.state.message().input.as_ref(); + let input = cx.state.0.input; copy_data(cx.gas, &mut cx.state.0.memory, gas_params, memory_offset, data_offset, len, input) } @@ -107,7 +107,7 @@ pub fn codecopy(cx: _, [memory_offset, code_offset, len]: [Word]) -> Result { #[instruction] pub fn gasprice(cx: _) -> out { - *out = cx.state.tx().gas_price; + *out = cx.state.host().tx_env().gas_price; } #[instruction(dynamic_gas)] @@ -118,7 +118,7 @@ pub fn extcodesize(cx: _, [addr]: [Word]) -> Result { #[instruction(dynamic_gas)] pub fn extcodehash(cx: _, [addr]: [Word]) -> Result { let account = load_account(&mut cx, *addr, false)?; - *out = if account.is_empty { Word::ZERO } else { b256_to_word(account.code_hash) }; + *out = if account.is_empty { Word::ZERO } else { account.code_hash.into() }; } #[instruction(dynamic_gas)] @@ -168,9 +168,9 @@ mod tests { use crate::{ SpecId, env::TxEnvExt, - interpreter::{InstrStop, Message, MessageExt, Word, op}, + interpreter::{InstrStop, MessageExt, Word, op}, test_utils::{ - RunConfig, TestHost, TestTypes, assert_stack, neg, push, run, run_stack, stack_code, + RunConfig, TestHost, assert_stack, neg, push, run, run_stack, stack_code, test_message, }, utils::{address_to_word, b256_to_word}, }; @@ -178,13 +178,9 @@ mod tests { use alloy_primitives::{Address, B256, Bytes}; use core::assert_matches; - fn test_message() -> Message { - MessageExt { gas_limit: 10_000, ..MessageExt::default() } - } - #[test] fn address_opcode() { - let address = Address::from([0x11; 20]); + let address = Address::repeat_byte(0x11); let mut host = TestHost::default(); let message = MessageExt { destination: address, ..test_message() }; let interp = run(RunConfig::new([op::ADDRESS, op::STOP]).host(&mut host).message(message)); @@ -196,7 +192,7 @@ mod tests { fn balance_opcode() { assert_stack!(BALANCE(0xbeef), 0xbeef); assert_stack!(BALANCE(0), 0); - assert_stack!(BALANCE(neg(1)), address_to_word(&Address::from([0xff; 20]))); + assert_stack!(BALANCE(neg(1)), address_to_word(&Address::repeat_byte(0xff))); } #[test] @@ -222,7 +218,7 @@ mod tests { #[test] fn origin_opcode() { - let origin = Address::from([0x22; 20]); + let origin = Address::repeat_byte(0x22); let mut host = TestHost::default(); let tx_env = TxEnvExt { origin, ..TxEnvExt::default() }; let interp = run(RunConfig::new([op::ORIGIN, op::STOP]).host(&mut host).tx_env(tx_env)); @@ -232,7 +228,7 @@ mod tests { #[test] fn caller_opcode() { - let caller = Address::from([0x33; 20]); + let caller = Address::repeat_byte(0x33); let mut host = TestHost::default(); let message = MessageExt { caller, ..test_message() }; let interp = run(RunConfig::new([op::CALLER, op::STOP]).host(&mut host).message(message)); @@ -254,7 +250,7 @@ mod tests { fn calldataload_opcode() { let input = Bytes::from(Vec::from([1_u8, 2, 3])); let mut host = TestHost::default(); - let message = MessageExt { input, ..test_message() }; + let message = MessageExt { input: input.into(), ..test_message() }; let interp = run(RunConfig::new([op::PUSH0, op::CALLDATALOAD, op::STOP]) .host(&mut host) @@ -275,7 +271,7 @@ mod tests { fn calldatasize_opcode() { let input = Bytes::from(Vec::from([1_u8, 2, 3, 4])); let mut host = TestHost::default(); - let message = MessageExt { input, ..test_message() }; + let message = MessageExt { input: input.into(), ..test_message() }; let interp = run(RunConfig::new([op::CALLDATASIZE, op::STOP]).host(&mut host).message(message)); assert_matches!(interp.err, InstrStop::Stop); @@ -286,7 +282,7 @@ mod tests { fn calldatacopy_opcode() { let input = Bytes::from(Vec::from([0xaa_u8, 0xbb, 0xcc])); let mut host = TestHost::default(); - let message = MessageExt { input, ..test_message() }; + let message = MessageExt { input: input.into(), ..test_message() }; let mut code = Vec::new(); push(&mut code, 2); push(&mut code, 1); @@ -302,12 +298,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [Word::from_be_bytes(expected)]); - let interp = run(RunConfig::new(stack_code( - [Word::MAX, Word::MAX, Word::from(0)], - op::CALLDATACOPY, - )) - .host(&mut host) - .message(message)); + let interp = + run(RunConfig::new(stack_code([Word::MAX, Word::MAX, Word::ZERO], op::CALLDATACOPY)) + .host(&mut host) + .message(message)); assert_matches!(interp.err, InstrStop::Stop); } @@ -319,7 +313,7 @@ mod tests { let interp = run(RunConfig::new([op::PUSH1, 0x00, op::CODESIZE, op::STOP])); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(0), Word::from(4)]); + assert_eq!(interp.stack(), [Word::ZERO, Word::from(4)]); } #[test] @@ -340,7 +334,7 @@ mod tests { assert_eq!(interp.stack(), [Word::from_be_bytes(expected)]); let mut code = Vec::new(); - push(&mut code, Word::from(1)); + push(&mut code, Word::ONE); push(&mut code, Word::from(usize::MAX)); push(&mut code, 0); code.push(op::CODECOPY); @@ -351,10 +345,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [0]); - let interp = run_stack([Word::MAX, Word::MAX, Word::from(0)], op::CODECOPY); + let interp = run_stack([Word::MAX, Word::MAX, Word::ZERO], op::CODECOPY); assert_matches!(interp.err, InstrStop::Stop); - let interp = run_stack([Word::MAX, Word::from(0), Word::from(1)], op::CODECOPY); + let interp = run_stack([Word::MAX, Word::ZERO, Word::ONE], op::CODECOPY); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } @@ -412,14 +406,14 @@ mod tests { assert_eq!(interp.stack(), [Word::from_be_bytes(expected)]); let interp = run(RunConfig::new(stack_code( - [Word::from(0xbeef), Word::MAX, Word::MAX, Word::from(0)], + [Word::from(0xbeef), Word::MAX, Word::MAX, Word::ZERO], op::EXTCODECOPY, )) .host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); let interp = run(RunConfig::new(stack_code( - [Word::from(0xbeef), Word::MAX, Word::from(0), Word::from(1)], + [Word::from(0xbeef), Word::MAX, Word::ZERO, Word::ONE], op::EXTCODECOPY, )) .host(&mut host)); @@ -456,7 +450,7 @@ mod tests { assert_eq!(interp.stack(), [Word::from_be_bytes(expected)]); let interp = run(RunConfig::new(stack_code( - [Word::from(0), Word::from(3), Word::from(0)], + [Word::ZERO, Word::from(3), Word::ZERO], op::RETURNDATACOPY, )) .spec(SpecId::BYZANTIUM) @@ -464,23 +458,21 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); let interp = run(RunConfig::new(stack_code( - [Word::from(0), Word::from(4), Word::from(0)], + [Word::ZERO, Word::from(4), Word::ZERO], op::RETURNDATACOPY, )) .spec(SpecId::BYZANTIUM) .return_data(Bytes::from_static(&[0xaa, 0xbb, 0xcc]))); assert_matches!(interp.err, InstrStop::OutOfOffset); - let interp = run(RunConfig::new(stack_code( - [Word::MAX, Word::from(0), Word::from(1)], - op::RETURNDATACOPY, - )) - .spec(SpecId::BYZANTIUM) - .return_data(Bytes::from_static(&[0xaa]))); + let interp = + run(RunConfig::new(stack_code([Word::MAX, Word::ZERO, Word::ONE], op::RETURNDATACOPY)) + .spec(SpecId::BYZANTIUM) + .return_data(Bytes::from_static(&[0xaa]))); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); let interp = run(RunConfig::new(stack_code( - [Word::from(0), Word::from(0), Word::from(0)], + [Word::ZERO, Word::ZERO, Word::ZERO], op::RETURNDATACOPY, )) .spec(SpecId::FRONTIER)); diff --git a/crates/evm2/src/interpreter/instructions/host.rs b/crates/evm2/src/interpreter/instructions/host.rs index 2b7c37976..506ab3218 100644 --- a/crates/evm2/src/interpreter/instructions/host.rs +++ b/crates/evm2/src/interpreter/instructions/host.rs @@ -1,3 +1,4 @@ +use super::require_non_staticcall; use crate::{ EvmFeatures, EvmTypesHost, evm::SStore, @@ -10,14 +11,6 @@ use crate::{ use alloy_primitives::{Address, B256}; use evm2_macros::instruction; -#[inline] -const fn require_non_staticcall(state: &InterpreterState<'_, '_, T>) -> Result { - if state.is_static() { - return Err(InstrStop::StateChangeDuringStaticCall); - } - Ok(()) -} - #[instruction(dynamic_gas)] pub fn sload(cx: _, [key]: [Word]) -> Result { // EIP-2929: SLOAD pays the warm read cost as static opcode gas, then only @@ -150,7 +143,7 @@ fn log_common( let mut topics = [B256::ZERO; 4]; for (topic, word) in topics.iter_mut().zip(stack.popn_dyn(n)?) { - *topic = B256::from(word.to_be_bytes::<32>()); + *topic = B256::from(word); } let destination = &cx.state.message().destination; let (memory, host) = cx.state.0.memory_host(); @@ -178,7 +171,7 @@ mod tests { #[test] fn sload_opcode() { let mut host = TestHost::default(); - host.storage.insert(StorageKey::new(Address::ZERO, Word::from(1)), Word::from(0xbeef)); + host.storage.insert(StorageKey::new(Address::ZERO, Word::ONE), Word::from(0xbeef)); let mut code = Vec::new(); push(&mut code, 1); @@ -209,7 +202,7 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [Word::from(0xbeef)]); assert_eq!( - host.storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), + host.storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), Some(&Word::from(0xbeef)) ); } @@ -225,7 +218,7 @@ mod tests { let interp = run(RunConfig::new(code).host(&mut host).staticcall()); assert_matches!(interp.err, InstrStop::StateChangeDuringStaticCall); assert!(interp.stack().is_empty()); - assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), None); + assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), None); } #[test] @@ -242,7 +235,7 @@ mod tests { assert_matches!(interp.err, InstrStop::StateChangeDuringStaticCall); assert!(interp.stack().is_empty()); - assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), None); + assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), None); } #[test] @@ -256,7 +249,7 @@ mod tests { let interp = run(RunConfig::new(code).host(&mut host).spec(SpecId::ISTANBUL).gas_limit(2306)); assert_matches!(interp.err, InstrStop::ReentrancySentryOOG); - assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), None); + assert_eq!(host.storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), None); } #[test] @@ -304,7 +297,7 @@ mod tests { #[test] fn sstore_reset_to_original_records_refund() { let mut host = TestHost::default(); - host.storage.insert(StorageKey::new(Address::ZERO, Word::from(0)), Word::from(5)); + host.storage.insert(StorageKey::new(Address::ZERO, Word::ZERO), Word::from(5)); let mut code = Vec::new(); push(&mut code, 7); push(&mut code, 0); @@ -341,7 +334,7 @@ mod tests { fn tload_opcode() { let mut host = TestHost::default(); host.transient_storage - .insert(StorageKey::new(Address::ZERO, Word::from(1)), Word::from(0xcafe)); + .insert(StorageKey::new(Address::ZERO, Word::ONE), Word::from(0xcafe)); let mut code = Vec::new(); push(&mut code, 1); @@ -371,7 +364,7 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [Word::from(0xcafe)]); assert_eq!( - host.transient_storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), + host.transient_storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), Some(&Word::from(0xcafe)) ); @@ -393,10 +386,7 @@ mod tests { let interp = run(RunConfig::new(code).host(&mut host).spec(SpecId::CANCUN).staticcall()); assert_matches!(interp.err, InstrStop::StateChangeDuringStaticCall); assert!(interp.stack().is_empty()); - assert_eq!( - host.transient_storage.get(&StorageKey::new(Address::ZERO, Word::from(1))), - None - ); + assert_eq!(host.transient_storage.get(&StorageKey::new(Address::ZERO, Word::ONE)), None); } fn log_code(offset: usize, len: usize, topics: [Word; N]) -> Vec { @@ -417,7 +407,7 @@ mod tests { #[test] fn log0_opcode() { let mut host = TestHost::default(); - let address = Address::from([0x11; 20]); + let address = Address::repeat_byte(0x11); let message = MessageExt { destination: address, gas_limit: 10_000, ..Default::default() }; let interp = run(RunConfig::new(log_code(30, 2, [])).host(&mut host).message(message)); assert_matches!(interp.err, InstrStop::Stop); @@ -431,9 +421,9 @@ mod tests { #[test] fn log1_opcode() { let mut host = TestHost::default(); - let interp = run(RunConfig::new(log_code(30, 2, [Word::from(1)])).host(&mut host)); + let interp = run(RunConfig::new(log_code(30, 2, [Word::ONE])).host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(host.logs[0].topics(), &[B256::from(Word::from(1).to_be_bytes::<32>())]); + assert_eq!(host.logs[0].topics(), &[B256::with_last_byte(1)]); assert_eq!(host.logs[0].data.data, Bytes::from_static(&[0xbe, 0xef])); } @@ -441,15 +431,9 @@ mod tests { fn log2_opcode() { let mut host = TestHost::default(); let interp = - run(RunConfig::new(log_code(30, 0, [Word::from(1), Word::from(2)])).host(&mut host)); + run(RunConfig::new(log_code(30, 0, [Word::ONE, Word::from(2)])).host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!( - host.logs[0].topics(), - &[ - B256::from(Word::from(1).to_be_bytes::<32>()), - B256::from(Word::from(2).to_be_bytes::<32>()), - ] - ); + assert_eq!(host.logs[0].topics(), &[B256::with_last_byte(1), B256::with_last_byte(2),]); assert!(host.logs[0].data.data.is_empty()); } @@ -457,7 +441,7 @@ mod tests { fn log3_opcode() { let mut host = TestHost::default(); let interp = - run(RunConfig::new(log_code(30, 1, [Word::from(1), Word::from(2), Word::from(3)])) + run(RunConfig::new(log_code(30, 1, [Word::ONE, Word::from(2), Word::from(3)])) .host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(host.logs[0].topics().len(), 3); @@ -470,7 +454,7 @@ mod tests { let interp = run(RunConfig::new(log_code( 30, 1, - [Word::from(1), Word::from(2), Word::from(3), Word::from(4)], + [Word::ONE, Word::from(2), Word::from(3), Word::from(4)], )) .host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); diff --git a/crates/evm2/src/interpreter/instructions/macro_tests.rs b/crates/evm2/src/interpreter/instructions/macro_tests.rs index a1a466956..aff8d1c53 100644 --- a/crates/evm2/src/interpreter/instructions/macro_tests.rs +++ b/crates/evm2/src/interpreter/instructions/macro_tests.rs @@ -2,7 +2,9 @@ use crate::{ BaseEvmConfig, EvmConfig, ExecutionConfig, SpecId, env::BlockEnvExt, interpreter::{Host, InstrStop, Interpreter, Message, Word, op}, - test_utils::{RunConfig, TestHost, TestInterpreter, TestTypes, legacy_bytecode, push}, + test_utils::{ + RunConfig, TestHost, TestInterpreter, TestTypes, legacy_bytecode, push, test_host, + }, version::OpcodeConfig, }; use alloc::vec::Vec; @@ -80,10 +82,11 @@ fn run(config: RunConfig<'_>) -> TestInterpreter { let execution_config = ExecutionConfig::::for_config::(); let RunConfig { code, host, spec_id, tx_env, message, gas_limit, return_data } = config; let message = Message:: { code: legacy_bytecode(code), gas_limit, ..message }; - let mut inner = Interpreter::::new(&tx_env, &message); + let mut inner = Interpreter::::new(&message); *inner.return_data_mut() = return_data; let mut default_host = TestHost::default(); let host = host.unwrap_or(&mut default_host); + host.tx_env = tx_env; host.spec_id = spec_id; host.execution_config = execution_config; let err = inner.run(host).unwrap(); @@ -123,10 +126,7 @@ fn instruction_macro_no_stack_preamble_attribute() { #[test] fn instruction_macro_concrete_evm_types_equals_attribute() { - let mut host = TestHost { - block: BlockEnvExt { number: Word::from(42), ..BlockEnvExt::default() }, - ..TestHost::default() - }; + let mut host = test_host(BlockEnvExt { number: Word::from(42), ..BlockEnvExt::default() }); let interp = run(RunConfig::new([CONCRETE_EQ_OPCODE, op::STOP]).host(&mut host)); assert_eq!(interp.err, InstrStop::Stop); @@ -135,10 +135,7 @@ fn instruction_macro_concrete_evm_types_equals_attribute() { #[test] fn instruction_macro_evm_types_colon_bound_attribute() { - let mut host = TestHost { - block: BlockEnvExt { number: Word::from(31337), ..BlockEnvExt::default() }, - ..TestHost::default() - }; + let mut host = test_host(BlockEnvExt { number: Word::from(31337), ..BlockEnvExt::default() }); let interp = run(RunConfig::new([TYPE_BOUND_OPCODE, op::STOP]).host(&mut host)); assert_eq!(interp.err, InstrStop::Stop); @@ -147,10 +144,7 @@ fn instruction_macro_evm_types_colon_bound_attribute() { #[test] fn instruction_macro_evm_types_assoc_colon_bound_attribute() { - let mut host = TestHost { - block: BlockEnvExt { number: Word::from(31337), ..BlockEnvExt::default() }, - ..TestHost::default() - }; + let mut host = test_host(BlockEnvExt { number: Word::from(31337), ..BlockEnvExt::default() }); let interp = run(RunConfig::new([ASSOC_BOUND_OPCODE, op::STOP]).host(&mut host)); assert_eq!(interp.err, InstrStop::Stop); diff --git a/crates/evm2/src/interpreter/instructions/memory.rs b/crates/evm2/src/interpreter/instructions/memory.rs index 12c2c2c23..379bdbe13 100644 --- a/crates/evm2/src/interpreter/instructions/memory.rs +++ b/crates/evm2/src/interpreter/instructions/memory.rs @@ -43,7 +43,6 @@ pub fn mcopy(cx: _, [dst, src, len]: [Word]) -> Result { mod tests { use crate::{ ExecutionConfig, SpecId, Version, - env::TxEnvExt, interpreter::{InstrStop, Interpreter, MessageExt, Word, op}, test_utils::{RunConfig, TestHost, TestTypes, legacy_bytecode, push, run, run_stack}, version::GasId, @@ -86,7 +85,7 @@ mod tests { assert_eq!(interp.stack(), [Word::from(64)]); assert_eq!(interp.memory(38, 2), [0xfe, 0xed]); - let interp = run_stack([Word::MAX, Word::from(0)], op::MSTORE); + let interp = run_stack([Word::MAX, Word::ZERO], op::MSTORE); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } @@ -101,10 +100,9 @@ mod tests { code.push(op::MSTORE); code.push(op::STOP); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 10_000, code: legacy_bytecode(code), ..MessageExt::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); let mut host = TestHost { execution_config: config, ..TestHost::default() }; let err = interp.run(&mut host).unwrap(); @@ -123,10 +121,9 @@ mod tests { code.push(op::MSTORE); code.push(op::STOP); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 17, code: legacy_bytecode(code), ..MessageExt::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); let mut host = TestHost { execution_config: config, ..TestHost::default() }; let err = interp.run(&mut host).unwrap(); @@ -149,7 +146,7 @@ mod tests { assert_eq!(interp.memory(4, 1), [0xab]); assert_eq!(interp.stack()[0] >> 248, Word::from(0xab)); - let interp = run_stack([Word::MAX, Word::from(0)], op::MSTORE8); + let interp = run_stack([Word::MAX, Word::ZERO], op::MSTORE8); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } @@ -191,7 +188,7 @@ mod tests { let mut code = Vec::new(); push(&mut code, 0); - push(&mut code, Word::from(1)); + push(&mut code, Word::ONE); push(&mut code, 0); code.push(op::MCOPY); code.push(op::MSIZE); @@ -200,10 +197,10 @@ mod tests { assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [0]); - let interp = run_stack([Word::MAX, Word::MAX, Word::from(0)], op::MCOPY); + let interp = run_stack([Word::MAX, Word::MAX, Word::ZERO], op::MCOPY); assert_matches!(interp.err, InstrStop::Stop); - let interp = run_stack([Word::MAX, Word::from(0), Word::from(1)], op::MCOPY); + let interp = run_stack([Word::MAX, Word::ZERO, Word::ONE], op::MCOPY); assert_matches!(interp.err, InstrStop::InvalidOperandOOG); } diff --git a/crates/evm2/src/interpreter/instructions/mod.rs b/crates/evm2/src/interpreter/instructions/mod.rs index 9b34882c9..a03362eea 100644 --- a/crates/evm2/src/interpreter/instructions/mod.rs +++ b/crates/evm2/src/interpreter/instructions/mod.rs @@ -1,3 +1,8 @@ +use crate::{ + EvmTypesHost, + interpreter::{InstrStop, InterpreterState, Result}, +}; + mod arithmetic; pub use arithmetic::*; @@ -30,5 +35,13 @@ pub use system::*; pub mod i256; +#[inline] +const fn require_non_staticcall(state: &InterpreterState<'_, '_, T>) -> Result { + if state.is_static() { + return Err(InstrStop::StateChangeDuringStaticCall); + } + Ok(()) +} + #[cfg(test)] mod macro_tests; diff --git a/crates/evm2/src/interpreter/instructions/stack.rs b/crates/evm2/src/interpreter/instructions/stack.rs index bee41ea37..65bdc01d5 100644 --- a/crates/evm2/src/interpreter/instructions/stack.rs +++ b/crates/evm2/src/interpreter/instructions/stack.rs @@ -27,39 +27,80 @@ pub fn swap() -> Result { pub fn dupn(cx: _) -> Result { let n = decode_single(unsafe { cx.pc.read_bytes_offset_unchecked(1, 1)[0] }) .ok_or(InstrStop::InvalidImmediateEncoding)?; - stack.dup(n) + stack.dup(n as usize) } #[instruction(no_stack_preamble)] pub fn swapn(cx: _) -> Result { let n = decode_single(unsafe { cx.pc.read_bytes_offset_unchecked(1, 1)[0] }) .ok_or(InstrStop::InvalidImmediateEncoding)?; - stack.exchange(0, n) + stack.swap(n as usize) } #[instruction(no_stack_preamble)] pub fn exchange(cx: _) -> Result { let (n, m) = decode_pair(unsafe { cx.pc.read_bytes_offset_unchecked(1, 1)[0] }) .ok_or(InstrStop::InvalidImmediateEncoding)?; - stack.exchange(n, m) + stack.exchange(n as usize, m as usize) } -const fn decode_single(x: u8) -> Option { - if x <= 90 || x >= 128 { Some(x.wrapping_add(145) as usize) } else { None } +/// Decodes a `DUPN`/`SWAPN` immediate byte into a stack index. +/// +/// Returns `None` if the immediate is in the invalid range `[91, 127]`. +pub const fn decode_single(x: u8) -> Option { + if x <= 90 || x >= 128 { Some(x.wrapping_add(145)) } else { None } } -const fn decode_pair(x: u8) -> Option<(usize, usize)> { +/// Decodes an `EXCHANGE` immediate byte into a pair of stack indices `(n, m)`. +/// +/// Returns `None` if the immediate is in the invalid range `[82, 127]`. +pub const fn decode_pair(x: u8) -> Option<(u8, u8)> { if x > 81 && x < 128 { return None; } - let k = (x ^ 143) as usize; - let q = k / 16; - let r = k % 16; + let k = (x ^ 143) as u16; + let q = (k / 16) as u8; + let r = (k % 16) as u8; if q < r { Some((q + 1, r + 1)) } else { Some((r + 1, 29 - q)) } } +/// Encodes a stack index into a DUPN/SWAPN immediate byte. +/// +/// Returns `None` if the value is outside the valid range. +pub fn encode_single(n: u8) -> Option { + let x = n.wrapping_sub(145); + if decode_single(x) == Some(n) { Some(x) } else { None } +} + +/// Encodes a pair of stack indices into an EXCHANGE immediate byte. +/// +/// Returns `None` if the pair cannot be encoded. +pub fn encode_pair(n: u8, m: u8) -> Option { + if n == 0 || m == 0 { + return None; + } + // Try both (q,r) orderings and check round-trip. + let try_k = |q: u8, r: u8| -> Option { + if q >= 16 || r >= 16 { + return None; + } + let k = q as u16 * 16 + r as u16; + let x = (k as u8) ^ 143; + if decode_pair(x) == Some((n, m)) { Some(x) } else { None } + }; + + // Case 1: q < r → n = q+1, m = r+1. + if n < m { + try_k(n - 1, m - 1) + } else { + // Case 2: q >= r → n = r+1, m = 29-q. + 29u8.checked_sub(m).and_then(|q| try_k(q, n - 1)) + } +} + #[cfg(test)] mod tests { + use super::*; use crate::{ SpecId, interpreter::{InstrStop, StackMut, Word, op}, @@ -76,7 +117,7 @@ mod tests { let interp = run(RunConfig::new([op::PUSH1, 0x01, op::PUSH1, 0x02, op::POP, op::STOP])); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); } #[test] @@ -133,10 +174,7 @@ mod tests { fn assert_push_opcode(opcode: u8, n: usize) { for bytes in [vec![0; n], vec![n as u8; n], (1..=n as u8).collect::>()] { - let mut code = Vec::new(); - code.push(opcode); - code.extend_from_slice(&bytes); - code.push(op::STOP); + let code = [&[opcode][..], &bytes[..], &[op::STOP][..]].concat(); let interp = run(RunConfig::new(code)); assert_matches!(interp.err, InstrStop::Stop); @@ -291,8 +329,8 @@ mod tests { let interp = run(RunConfig::new(code).spec(SpecId::AMSTERDAM)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack().len(), 18); - assert_eq!(interp.stack()[17], Word::from(1)); - assert_eq!(interp.stack()[0], Word::from(1)); + assert_eq!(interp.stack()[17], Word::ONE); + assert_eq!(interp.stack()[0], Word::ONE); for i in 1..17 { assert_eq!(interp.stack()[i], 0); } @@ -305,7 +343,7 @@ mod tests { let interp = run(RunConfig::new(code).spec(SpecId::AMSTERDAM)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack().len(), 146); - assert_eq!(interp.stack()[145], Word::from(1)); + assert_eq!(interp.stack()[145], Word::ONE); } #[test] @@ -316,7 +354,7 @@ mod tests { let interp = run(RunConfig::new(code).spec(SpecId::AMSTERDAM)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack().len(), 18); - assert_eq!(interp.stack()[17], Word::from(1)); + assert_eq!(interp.stack()[17], Word::ONE); assert_eq!(interp.stack()[0], Word::from(2)); for i in 1..17 { assert_eq!(interp.stack()[i], 0); @@ -348,7 +386,7 @@ mod tests { ]) .spec(SpecId::AMSTERDAM)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1), Word::from(0), Word::from(2)]); + assert_eq!(interp.stack(), [Word::ONE, Word::ZERO, Word::from(2)]); let mut code = Vec::new(); for value in 0..23 { @@ -373,4 +411,64 @@ mod tests { let interp = run(RunConfig::new([op::EXCHANGE, 0x8e]).spec(SpecId::OSAKA)); assert_matches!(interp.err, InstrStop::NotActivated); } + + #[test] + fn encode_decode_single_roundtrip() { + for x in 0..=255u8 { + if let Some(n) = decode_single(x) { + assert_eq!(encode_single(n), Some(x), "roundtrip failed for x={x}, n={n}"); + } + } + for n in 0..=255u8 { + if let Some(x) = encode_single(n) { + assert_eq!(decode_single(x), Some(n), "roundtrip failed for n={n}, x={x}"); + } + } + // Out-of-range: values in [236,255] ∪ [0,16] have no valid encoding. + assert_eq!(encode_single(0), None); + assert_eq!(encode_single(16), None); + assert_eq!(encode_single(236), None); + // Invalid raw bytes. + assert_eq!(decode_single(91), None); + assert_eq!(decode_single(127), None); + } + + #[test] + fn encode_decode_pair_roundtrip() { + // encode → decode is always exact. + for n in 1..=30u8 { + for m in 1..=30u8 { + if let Some(x) = encode_pair(n, m) { + assert_eq!( + decode_pair(x), + Some((n, m)), + "roundtrip failed for (n,m)=({n},{m}), x={x}" + ); + } + } + } + // decode → encode: some pairs have multiple encodings, so the raw byte may differ, + // but decoding the result must produce the same pair. + // Some decoded pairs may have values too large to re-encode. + for x in 0..=255u8 { + if let Some((n, m)) = decode_pair(x) + && let Some(re) = encode_pair(n, m) + { + assert_eq!( + decode_pair(re), + Some((n, m)), + "decode roundtrip failed for x={x}, re={re}, (n,m)=({n},{m})" + ); + } + } + // Zero indices are invalid. + assert_eq!(encode_pair(0, 1), None); + assert_eq!(encode_pair(1, 0), None); + // Invalid raw bytes: [82, 127]. + assert_eq!(decode_pair(82), None); + assert_eq!(decode_pair(127), None); + // Edge: 80 and 81 are now valid. + assert!(decode_pair(80).is_some()); + assert!(decode_pair(81).is_some()); + } } diff --git a/crates/evm2/src/interpreter/instructions/system.rs b/crates/evm2/src/interpreter/instructions/system.rs index 4abf1dabe..b7914e112 100644 --- a/crates/evm2/src/interpreter/instructions/system.rs +++ b/crates/evm2/src/interpreter/instructions/system.rs @@ -1,11 +1,12 @@ //! System opcode implementations. +use super::require_non_staticcall; use crate::{ EvmFeatures, EvmTypesHost, bytecode::Bytecode, interpreter::{ - Gas, Host, InstrStop, InterpreterState, Message, MessageExt, MessageKind, Result, StackMut, - Word, derive_create_destination, + Gas, Host, InstrStop, InterpreterState, Message, MessageExt, MessageKind, MessageResult, + Result, StackMut, Word, create_preaccess_failure, derive_create_destination, }, utils::WordExt, version::GasId, @@ -14,14 +15,6 @@ use alloy_primitives::{Address, B256, Bytes}; use core::{cmp::min, ops::Range}; use evm2_macros::instruction; -#[inline] -const fn require_non_staticcall(state: &InterpreterState<'_, '_, T>) -> Result { - if state.is_static() { - return Err(InstrStop::StateChangeDuringStaticCall); - } - Ok(()) -} - #[inline] const fn should_charge_new_account_gas( eip161: bool, @@ -113,21 +106,18 @@ fn load_acc_and_calc_gas( code = delegated_account.code; code_address = delegated_address; } - let features = state.version().features; let mut new_account_state_gas = 0; - // Check emptiness only when new-account gas can apply: after EIP-161 a zero-value call never - // pays it, so it skips the extra account lookup. if create_empty_account - && (!features.contains(EvmFeatures::EIP161) || transfers_value) - && state - .host() - .target_is_empty_for_new_account_gas(&to, features) - .map_err(|error| state.fail(error))? + && should_charge_new_account_gas( + state.feature(EvmFeatures::EIP161), + transfers_value, + account.is_empty_for_new_account_gas, + ) { cost += state.gas_params().get(GasId::NewAccountCost); // EIP-8037: value transfer to a new account also charges state gas from // the reservoir. Charged before the 63/64 child gas split below. - if features.contains(EvmFeatures::EIP8037) && transfers_value { + if state.feature(EvmFeatures::EIP8037) && transfers_value { new_account_state_gas = state.gas_params().new_account_state_gas(); } } @@ -171,7 +161,7 @@ fn prepare_call( return Err(InstrStop::CallNotAllowedInsideStatic); } - let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX); + let local_gas_limit = local_gas_limit.saturating_to::(); let (input_range, prepared_return_memory_range) = get_memory_input_and_out_ranges( gas, state, @@ -189,7 +179,7 @@ fn prepare_call( kind == MessageKind::Call, local_gas_limit, )?; - let input = memory_range_bytes(state, input_range)?; + let input = state.0.call_input(input_range.start, input_range.len()); let current = state.message(); let (destination, caller, call_value, code_address) = match kind { @@ -225,21 +215,82 @@ fn prepare_call( Ok(new_account_state_gas) } +#[instruction(no_stack_preamble, hooked)] +pub fn call(cx: _) -> Result { + call_inner(stack, cx.gas, cx.state, MessageKind::Call) +} + +#[instruction(no_stack_preamble, hooked)] +pub fn callcode(cx: _) -> Result { + call_inner(stack, cx.gas, cx.state, MessageKind::CallCode) +} + +#[instruction(no_stack_preamble, hooked)] +pub fn delegatecall(cx: _) -> Result { + call_inner(stack, cx.gas, cx.state, MessageKind::DelegateCall) +} + +#[instruction(no_stack_preamble, hooked)] +pub fn staticcall(cx: _) -> Result { + call_inner(stack, cx.gas, cx.state, MessageKind::StaticCall) +} + +#[instruction(no_stack_preamble, hooked)] +pub fn create(cx: _) -> Result { + create_inner(stack, cx.gas, cx.state, IS_CREATE2) +} + #[inline(never)] fn call_inner( + stack: StackMut<'_>, + gas: &mut Gas, + state: &mut InterpreterState<'_, '_, T>, + kind: MessageKind, +) -> Result { + call_with(stack, gas, state, kind, |_, _, _| Ok(()), |_, _, _, _| Ok(())) +} + +/// Executes a call opcode of `kind` with hooks around its child message. +/// +/// This is the body of [`call`], [`callcode`], [`delegatecall`] and [`staticcall`], for custom +/// call instructions that need extra behavior. Use it from an +/// `#[instruction(no_stack_preamble, hooked)]` function with `stack`, `cx.gas` and +/// `cx.state`. The instruction must be `hooked`, so that the hooks and the host see its pc, opcode, +/// stack and gas. +/// +/// `before` runs after every charge the opcode makes, the EIP-8037 new-account state gas +/// included, and after the forwarded gas is taken from `gas`, right before the child message +/// executes. It may charge or credit `gas`, edit the child message (for example its `gas_limit`, +/// `reservoir` or `ext`), or fail the instruction. +/// +/// `after` runs once the child's gas is merged into `gas` and the upfront new-account state gas of +/// a failed child is refunded, before the return data is copied and the success word is pushed. +/// It may charge or credit `gas` or fail the instruction. A fatal child result returns before +/// `after` runs. +/// +/// An `Err` from either hook stops the instruction with that error. +#[inline] +pub fn call_with( mut stack: StackMut<'_>, gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, kind: MessageKind, + before: impl FnOnce(&mut Gas, &mut InterpreterState<'_, '_, T>, &mut Message) -> Result, + after: impl FnOnce( + &mut Gas, + &mut InterpreterState<'_, '_, T>, + &Message, + &MessageResult, + ) -> Result, ) -> Result { let mut message = Message::::default(); let mut return_memory_range = 0..0; let new_account_state_gas = prepare_call(stack.reborrow(), gas, state, kind, &mut message, &mut return_memory_range)?; + before(gas, state, &mut message)?; - let tx_env = state.tx(); let mut result = - state.host().execute_message(tx_env, &mut message).map_err(|error| state.fail(error))?; + state.host().execute_message(&mut message).map_err(|error| state.fail(error))?; if result.stop.is_fatal() { return Err(result.stop); } @@ -248,49 +299,59 @@ fn call_inner( // gas upfront on this frame. If the call does not succeed (depth/balance failure, // child revert or halt) the target is not created, so refund the upfront charge to // the reservoir (execution-specs `credit_state_gas_refund`), mirroring CREATE. - if new_account_state_gas != 0 && !result.stop.is_success() { + if new_account_state_gas != 0 && !result.is_success() { gas.refill_reservoir(new_account_state_gas); } + after(gas, state, &message, &result)?; let copy_len = min(return_memory_range.len(), result.output.len()); unsafe { let output = result.output.get_unchecked(..copy_len); state.memory().set_unchecked(return_memory_range.start, output); } state.swap_return_data(&mut result.output); - stack.push_u64(result.stop.is_success() as u64) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn call(cx: _) -> Result { - call_inner(stack, cx.gas, cx.state, MessageKind::Call) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn callcode(cx: _) -> Result { - call_inner(stack, cx.gas, cx.state, MessageKind::CallCode) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn delegatecall(cx: _) -> Result { - call_inner(stack, cx.gas, cx.state, MessageKind::DelegateCall) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn staticcall(cx: _) -> Result { - call_inner(stack, cx.gas, cx.state, MessageKind::StaticCall) -} - -#[instruction(no_stack_preamble, dynamic_gas)] -pub fn create(cx: _) -> Result { - create_inner(stack, cx.gas, cx.state, IS_CREATE2) + stack.push_u64(result.is_success() as u64)?; + // A stop a hook set. + state.result() } #[inline(never)] fn create_inner( + stack: StackMut<'_>, + gas: &mut Gas, + state: &mut InterpreterState<'_, '_, T>, + is_create2: bool, +) -> Result { + create_with(stack, gas, state, is_create2, |_, _, _| Ok(()), |_, _, _, _| Ok(())) +} + +/// Executes `CREATE` or `CREATE2` with hooks around its child message. +/// +/// This is the body of [`create`], for custom creation instructions that need extra behavior, in +/// the same shape as [`call_with`]. +/// +/// `before` runs after every charge the opcode makes, the EIP-8037 account-creation state gas +/// included, and after the forwarded gas is taken from `gas`, right before the child message +/// executes. It does not run when the opcode refuses the creation before forwarding gas (the +/// EIP-8037 balance, nonce and depth checks). +/// +/// `after` runs once the child's gas is merged into `gas` and the account-creation state gas of a +/// failed creation is refunded, before the return data is set and the created address is pushed. +/// A fatal child result returns before `after` runs. +/// +/// Both hooks may charge or credit `gas` or fail the instruction, as in [`call_with`]. +#[inline] +pub fn create_with( mut stack: StackMut<'_>, gas: &mut Gas, state: &mut InterpreterState<'_, '_, T>, is_create2: bool, + before: impl FnOnce(&mut Gas, &mut InterpreterState<'_, '_, T>, &mut Message) -> Result, + after: impl FnOnce( + &mut Gas, + &mut InterpreterState<'_, '_, T>, + &Message, + &MessageResult, + ) -> Result, ) -> Result { require_non_staticcall(state)?; @@ -317,7 +378,7 @@ fn create_inner( let current = state.message(); let caller = current.destination; let depth = current.depth.saturating_add(1); - let salt = salt.map(|salt| B256::from(salt.to_be_bytes())).unwrap_or_default(); + let salt = salt.map(B256::from).unwrap_or_default(); // Derive the created contract address once. CREATE needs the creator's (pre-bump) nonce; the // caller is the currently-executing contract, already warm and in the access list, so this read @@ -346,9 +407,14 @@ fn create_inner( if state.feature(EvmFeatures::EIP8037) { // `caller_info` is always `Some` when EIP-8037 is enabled. if let Some(caller_info) = caller_info.as_ref() - && (caller_info.balance < value - || caller_info.nonce == u64::MAX - || depth > state.version().max_call_depth()) + && create_preaccess_failure( + caller_info.balance, + caller_info.nonce, + value, + depth, + state.host().initial_call_depth(), + ) + .is_some() { state.clear_return_data(); stack.push_u64(0)?; @@ -357,11 +423,11 @@ fn create_inner( // The destination is loaded here and made warm. Balance, nonce, and depth pre-access // failures return above before the destination is accessed. - let features = state.version().features; if state .host() - .target_is_empty_for_new_account_gas(&destination, features) + .load_account(&destination, false, false) .map_err(|error| state.fail(error))? + .is_empty_for_new_account_gas { gas.spend_state(state.gas_params().create_state_gas())?; charged_create_state_gas = true; @@ -374,7 +440,6 @@ fn create_inner( }; gas.spend(gas_limit)?; - let tx_env = state.tx(); let mut message = MessageExt { kind, depth, @@ -384,17 +449,18 @@ fn create_inner( call_target: destination, caller, code: Bytecode::new_legacy(input.clone()), - input, + input: input.into(), value, - code_address: caller, + code_address: destination, // CREATE is rejected in a static context (see `require_non_staticcall`). caller_is_static: false, salt, ext: T::MessageExt::default(), _non_exhaustive: (), }; + before(gas, state, &mut message)?; let mut result = - state.host().execute_message(tx_env, &mut message).map_err(|error| state.fail(error))?; + state.host().execute_message(&mut message).map_err(|error| state.fail(error))?; if result.stop.is_fatal() { return Err(result.stop); } @@ -405,17 +471,20 @@ fn create_inner( // `created_address == None` — depth, out-of-funds, nonce overflow), no new account leaf is // created, so refund the charge to the reservoir via `refill_reservoir` (matching 0→x→0 storage // restoration). A successful deployment keeps the charge; an alive target was never charged. - let create_failed = result.created_address.is_none() || !result.stop.is_success(); + let create_failed = result.created_address.is_none() || !result.is_success(); if charged_create_state_gas && create_failed { gas.refill_reservoir(state.gas_params().create_state_gas()); } + after(gas, state, &message, &result)?; // EIP-211 exposes CREATE failure data only for REVERT; other failures clear returndata. - if result.stop == InstrStop::Revert { + if result.stop.is_revert() { state.swap_return_data(&mut result.output); } else { state.clear_return_data(); } - stack.push(result.created_address_for_parent()) + stack.push(result.created_address_for_parent())?; + // A stop a hook set. + state.result() } #[instruction(dynamic_gas)] @@ -452,30 +521,34 @@ pub fn selfdestruct(cx: _, [target]: [Word]) -> Result { mod tests { use super::*; use crate::{ - ExecutionError, SpecId, + BaseEvmConfig, EvmConfig, ExecutionConfig, ExecutionError, SpecId, constants::{CALL_DEPTH_LIMIT, MAX_INITCODE_SIZE}, - interpreter::{MessageResultExt, op}, - test_utils::{RunConfig, TestHost, push, push_all, run}, + interpreter::{Interpreter, MessageResultExt, op}, + test_utils::{ + RunConfig, TestHost, TestInterpreter, TestTypes, create_code, legacy_bytecode, push, + push_all, run, + }, utils::address_to_word, + version::OpcodeConfig, }; use alloc::vec::Vec; use core::assert_matches; #[test] fn call_opcode() { - let target = Address::from([0x22; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let caller = Address::repeat_byte(0x11); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), - address_to_word(&target), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + target.into_word().into(), Word::from(1000), ], ); @@ -487,7 +560,7 @@ mod tests { ..Default::default() })); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); assert_eq!(host.calls.len(), 1); assert_eq!(host.calls[0].kind, MessageKind::Call); assert_eq!(host.calls[0].destination, target); @@ -496,22 +569,10 @@ mod tests { #[test] fn call_propagates_fatal_child_result() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let error = ExecutionError::Fatal("child call failed".into()); let mut host = TestHost { execute_error: Some(error.clone()), ..Default::default() }; - let mut code = Vec::new(); - push_all( - &mut code, - [ - Word::ZERO, - Word::ZERO, - Word::ZERO, - Word::ZERO, - Word::ZERO, - address_to_word(&target), - Word::from(1000), - ], - ); + let mut code = crate::test_utils::call_code(target); code.extend([op::CALL, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host)); @@ -523,18 +584,18 @@ mod tests { #[test] fn call_propagates_static_flag() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), Word::ZERO, - address_to_word(&target), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + target.into_word().into(), Word::from(1000), ], ); @@ -549,7 +610,7 @@ mod tests { #[test] fn call_too_deep_charges_dynamic_gas() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut host = TestHost { is_cold: true, is_empty: true, ..Default::default() }; let mut code = Vec::new(); push_all( @@ -559,8 +620,8 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - Word::from(1), - address_to_word(&target), + Word::ONE, + target.into_word().into(), Word::from(1000), ], ); @@ -612,7 +673,7 @@ mod tests { #[test] fn call_too_deep_charges_pre_spurious_empty_account() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut host = TestHost { exists: false, is_empty: true, ..Default::default() }; let mut code = Vec::new(); push_all( @@ -623,7 +684,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - address_to_word(&target), + target.into_word().into(), Word::ZERO, ], ); @@ -642,7 +703,7 @@ mod tests { #[test] fn call_too_deep_skips_pre_spurious_touched_empty_account_cost() { - let target = Address::from([0x22; 20]); + let target = Address::repeat_byte(0x22); let mut host = TestHost { exists: false, is_empty: true, is_touched: true, ..Default::default() }; let mut code = Vec::new(); @@ -654,7 +715,7 @@ mod tests { Word::ZERO, Word::ZERO, Word::ZERO, - address_to_word(&target), + target.into_word().into(), Word::ZERO, ], ); @@ -673,18 +734,18 @@ mod tests { #[test] fn callcode_propagates_static_flag_and_allows_apparent_value() { - let code_address = Address::from([0x33; 20]); + let code_address = Address::repeat_byte(0x33); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, Word::from(7), - address_to_word(&code_address), + code_address.into_word().into(), Word::from(1000), ], ); @@ -700,19 +761,19 @@ mod tests { #[test] fn callcode_opcode() { - let code_address = Address::from([0x33; 20]); - let destination = Address::from([0x11; 20]); + let code_address = Address::repeat_byte(0x33); + let destination = Address::repeat_byte(0x11); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, Word::from(7), - address_to_word(&code_address), + code_address.into_word().into(), Word::from(1000), ], ); @@ -723,7 +784,7 @@ mod tests { .message(MessageExt { destination, ..Default::default() }) .gas_limit(20_000)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); assert_eq!(host.calls[0].kind, MessageKind::CallCode); assert_eq!(host.calls[0].destination, destination); assert_eq!(host.calls[0].code_address, code_address); @@ -732,18 +793,18 @@ mod tests { #[test] fn delegatecall_opcode() { - let code_address = Address::from([0x44; 20]); - let caller = Address::from([0x55; 20]); + let code_address = Address::repeat_byte(0x44); + let caller = Address::repeat_byte(0x55); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), - address_to_word(&code_address), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + code_address.into_word().into(), Word::from(1000), ], ); @@ -756,7 +817,7 @@ mod tests { ..Default::default() })); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); assert_eq!(host.calls[0].kind, MessageKind::DelegateCall); assert_eq!(host.calls[0].caller, caller); assert_eq!(host.calls[0].value, Word::from(9)); @@ -783,17 +844,17 @@ mod tests { #[test] fn staticcall_opcode() { - let target = Address::from([0x66; 20]); + let target = Address::repeat_byte(0x66); let mut host = TestHost::default(); let mut code = Vec::new(); push_all( &mut code, [ - Word::from(0), - Word::from(0), - Word::from(0), - Word::from(0), - address_to_word(&target), + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + target.into_word().into(), Word::from(1000), ], ); @@ -801,7 +862,7 @@ mod tests { let interp = run(RunConfig::new(code).host(&mut host)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [Word::from(1)]); + assert_eq!(interp.stack(), [Word::ONE]); assert_eq!(host.calls[0].kind, MessageKind::StaticCall); assert!(host.call_static_flags[0]); @@ -826,7 +887,7 @@ mod tests { #[test] fn create_opcode() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let mut host = TestHost { execute_result: MessageResultExt { stop: InstrStop::Return, @@ -836,7 +897,7 @@ mod tests { ..Default::default() }; let mut code = Vec::new(); - push_all(&mut code, [Word::from(0), Word::from(0), Word::from(0)]); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); code.extend([op::CREATE, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); @@ -850,8 +911,7 @@ mod tests { fn create_propagates_fatal_child_result() { let error = ExecutionError::Fatal("child create failed".into()); let mut host = TestHost { execute_error: Some(error.clone()), ..Default::default() }; - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut code = create_code(); code.extend([op::CREATE, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); @@ -863,7 +923,7 @@ mod tests { #[test] fn create_clears_return_data_on_success() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let mut host = TestHost { execute_result: MessageResultExt { stop: InstrStop::Return, @@ -874,12 +934,12 @@ mod tests { ..Default::default() }; let mut code = Vec::new(); - push_all(&mut code, [Word::from(0), Word::from(0), Word::from(0)]); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); code.extend([op::CREATE, op::RETURNDATASIZE, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); assert_matches!(interp.err, InstrStop::Stop); - assert_eq!(interp.stack(), [address_to_word(&created), Word::ZERO]); + assert_eq!(interp.stack(), [created.into_word().into(), Word::ZERO]); } #[test] @@ -893,7 +953,7 @@ mod tests { ..Default::default() }; let mut code = Vec::new(); - push_all(&mut code, [Word::from(0), Word::from(0), Word::from(0)]); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); code.extend([op::CREATE, op::RETURNDATASIZE, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); @@ -904,8 +964,7 @@ mod tests { #[test] fn create_too_deep_charges_create_gas() { let mut host = TestHost::default(); - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut code = create_code(); code.extend([op::CREATE, op::STOP]); let interp = run(RunConfig::new(code) @@ -923,20 +982,24 @@ mod tests { fn create_too_deep_clears_return_data_and_skips_destination_read_eip8037() { // EIP-8037: a create failing the depth pre-check must not access the destination — // the read would leak the address into the EIP-7928 block access list. + let caller = Address::repeat_byte(0x11); let mut host = TestHost { exists: false, ..Default::default() }; - let mut code = Vec::new(); - push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + let mut code = create_code(); code.extend([op::CREATE, op::RETURNDATASIZE, op::STOP]); let interp = run(RunConfig::new(code) .host(&mut host) .spec(SpecId::AMSTERDAM) - .message(MessageExt { depth: CALL_DEPTH_LIMIT, ..Default::default() }) + .message(MessageExt { + depth: CALL_DEPTH_LIMIT, + destination: caller, + ..Default::default() + }) .return_data(Bytes::from_static(b"stale")) .gas_limit(50_000)); assert_matches!(interp.err, InstrStop::Stop); assert_eq!(interp.stack(), [Word::ZERO, Word::ZERO]); - assert!(host.new_account_checks.is_empty()); + assert_eq!(host.account_loads, [caller]); } #[test] @@ -954,7 +1017,7 @@ mod tests { #[test] fn create2_opcode() { - let created = Address::from([0x88; 20]); + let created = Address::repeat_byte(0x88); let mut host = TestHost { execute_result: MessageResultExt { stop: InstrStop::Return, @@ -964,7 +1027,7 @@ mod tests { ..Default::default() }; let mut code = Vec::new(); - push_all(&mut code, [Word::from(0), Word::from(0), Word::from(0), Word::from(0)]); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO, Word::ZERO]); code.extend([op::CREATE2, op::STOP]); let interp = run(RunConfig::new(code).host(&mut host).gas_limit(50_000)); @@ -989,8 +1052,8 @@ mod tests { #[test] fn selfdestruct_opcode() { - let contract = Address::from([0x11; 20]); - let target = Address::from([0x99; 20]); + let contract = Address::repeat_byte(0x11); + let target = Address::repeat_byte(0x99); let mut host = TestHost::default(); let mut code = Vec::new(); push(&mut code, address_to_word(&target)); @@ -1004,4 +1067,197 @@ mod tests { assert_matches!(interp.err, InstrStop::SelfDestruct); assert_eq!(host.selfdestructs, [(contract, target, false)]); } + + const HOOKED_CALL: u8 = 0x0c; + const HOOKED_CREATE: u8 = 0x0d; + const KEPT_GAS: u64 = 1_000; + const AFTER_FEE: u64 = 7; + + // Keeps `KEPT_GAS` of the forwarded gas in the caller and charges `AFTER_FEE` after a + // successful child; a failed child fails the instruction. + #[instruction(no_stack_preamble, hooked)] + fn hooked_call(cx: _) -> Result { + call_with( + stack, + cx.gas, + cx.state, + MessageKind::Call, + |gas, _, message| { + message.gas_limit -= KEPT_GAS; + gas.erase_cost(KEPT_GAS); + Ok(()) + }, + |gas, _, _, result| { + if result.is_success() { gas.spend(AFTER_FEE) } else { Err(InstrStop::Revert) } + }, + ) + } + + #[instruction(no_stack_preamble, hooked)] + fn hooked_create(cx: _) -> Result { + create_with( + stack, + cx.gas, + cx.state, + false, + |gas, _, message| { + message.gas_limit -= KEPT_GAS; + gas.erase_cost(KEPT_GAS); + Ok(()) + }, + |gas, _, _, _| gas.spend(AFTER_FEE), + ) + } + + struct HookedConfig; + + impl EvmConfig for HookedConfig { + const BASE_SPEC_ID: SpecId = SpecId::AMSTERDAM; + const OPCODE_CONFIG: &'static OpcodeConfig = &hooked_opcode_config(); + } + + const fn hooked_opcode_config() -> OpcodeConfig { + let mut config = + OpcodeConfig::::base::>(); + config.set_instruction::>(HOOKED_CALL, config.static_gas(op::CALL)); + config.set_instruction::>( + HOOKED_CREATE, + config.static_gas(op::CREATE), + ); + config + } + + fn run_hooked(config: RunConfig<'_>) -> TestInterpreter { + let RunConfig { code, host, tx_env, message, gas_limit, return_data, .. } = config; + let message = Message:: { code: legacy_bytecode(code), gas_limit, ..message }; + let mut inner = Interpreter::::new(&message); + *inner.return_data_mut() = return_data; + let mut default_host = TestHost::default(); + let host = host.unwrap_or(&mut default_host); + host.tx_env = tx_env; + host.spec_id = SpecId::AMSTERDAM; + host.execution_config = ExecutionConfig::::for_config::(); + let err = inner.run(host).unwrap(); + let (stack, stack_len, gas, memory, output) = inner.into_parts(); + TestInterpreter { stack, stack_len, gas, memory, output, err, execution_error: None } + } + + fn call_code(opcode: u8, value: u64) -> Vec { + let mut code = Vec::new(); + push_all( + &mut code, + [ + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::from(value), + Address::repeat_byte(0x22).into_word().into(), + Word::from(5_000), + ], + ); + code.extend([opcode, op::STOP]); + code + } + + #[test] + fn call_with_hooks_adjust_child_and_caller_gas() { + let mut base_host = TestHost::default(); + let base = run_hooked( + RunConfig::new(call_code(op::CALL, 0)).host(&mut base_host).gas_limit(30_000), + ); + let mut host = TestHost::default(); + let hooked = + run_hooked(RunConfig::new(call_code(HOOKED_CALL, 0)).host(&mut host).gas_limit(30_000)); + + assert_matches!(base.err, InstrStop::Stop); + assert_matches!(hooked.err, InstrStop::Stop); + assert_eq!(hooked.stack(), [Word::ONE]); + assert_eq!(host.calls.len(), 1); + assert_eq!(host.calls[0].gas_limit, base_host.calls[0].gas_limit - KEPT_GAS); + assert_eq!(hooked.gas_remaining(), base.gas_remaining() + KEPT_GAS - AFTER_FEE); + } + + #[test] + fn call_with_before_runs_after_new_account_state_gas() { + // A value call to an empty account charges EIP-8037 new-account state gas before the + // forward; `before` sees the child message built after that charge. + let mut base_host = TestHost { is_empty: true, ..Default::default() }; + let base = run_hooked( + RunConfig::new(call_code(op::CALL, 1)).host(&mut base_host).gas_limit(200_000), + ); + let mut host = TestHost { is_empty: true, ..Default::default() }; + let hooked = run_hooked( + RunConfig::new(call_code(HOOKED_CALL, 1)).host(&mut host).gas_limit(200_000), + ); + + assert_matches!(hooked.err, InstrStop::Stop); + assert_eq!(host.calls[0].gas_limit, base_host.calls[0].gas_limit - KEPT_GAS); + assert_eq!(hooked.gas.state_gas_spent(), base.gas.state_gas_spent()); + assert_ne!(hooked.gas.state_gas_spent(), 0); + assert_eq!(hooked.gas_remaining(), base.gas_remaining() + KEPT_GAS - AFTER_FEE); + } + + #[test] + fn call_with_after_can_fail_the_instruction() { + let mut host = TestHost { + execute_result: MessageResultExt { stop: InstrStop::Revert, ..Default::default() }, + ..Default::default() + }; + let interp = + run_hooked(RunConfig::new(call_code(HOOKED_CALL, 0)).host(&mut host).gas_limit(30_000)); + + assert_matches!(interp.err, InstrStop::Revert); + assert!(interp.stack().is_empty()); + assert_eq!(host.calls.len(), 1); + } + + #[test] + fn create_with_hooks_adjust_child_and_caller_gas() { + let created = Address::repeat_byte(0x77); + let execute_result = MessageResultExt { + stop: InstrStop::Return, + created_address: Some(created), + ..MessageResultExt::default() + }; + let mut code = create_code(); + let mut base_code = code.clone(); + base_code.extend([op::CREATE, op::STOP]); + code.extend([HOOKED_CREATE, op::STOP]); + + let mut base_host = TestHost { + execute_result: execute_result.clone(), + is_empty: true, + ..Default::default() + }; + let base = run_hooked(RunConfig::new(base_code).host(&mut base_host).gas_limit(500_000)); + let mut host = TestHost { execute_result, is_empty: true, ..Default::default() }; + let hooked = run_hooked(RunConfig::new(code).host(&mut host).gas_limit(500_000)); + + assert_matches!(hooked.err, InstrStop::Stop); + assert_eq!(hooked.stack(), [address_to_word(&created)]); + assert_eq!(host.calls[0].gas_limit, base_host.calls[0].gas_limit - KEPT_GAS); + assert_eq!(hooked.gas.state_gas_spent(), base.gas.state_gas_spent()); + assert_eq!(hooked.gas_remaining(), base.gas_remaining() + KEPT_GAS - AFTER_FEE); + } + + #[test] + fn create_with_skips_hooks_when_creation_is_refused_before_forward() { + let mut code = create_code(); + let mut base_code = code.clone(); + base_code.extend([op::CREATE, op::STOP]); + code.extend([HOOKED_CREATE, op::STOP]); + let message = MessageExt { depth: CALL_DEPTH_LIMIT, ..Default::default() }; + + let base = + run_hooked(RunConfig::new(base_code).message(message.clone()).gas_limit(500_000)); + let mut host = TestHost::default(); + let hooked = + run_hooked(RunConfig::new(code).host(&mut host).message(message).gas_limit(500_000)); + + assert_matches!(hooked.err, InstrStop::Stop); + assert_eq!(hooked.stack(), [Word::ZERO]); + assert!(host.calls.is_empty()); + assert_eq!(hooked.gas_remaining(), base.gas_remaining()); + } } diff --git a/crates/evm2/src/interpreter/message.rs b/crates/evm2/src/interpreter/message.rs index 274a9e96f..97689096b 100644 --- a/crates/evm2/src/interpreter/message.rs +++ b/crates/evm2/src/interpreter/message.rs @@ -1,5 +1,11 @@ -use crate::{BaseEvmTypes, EvmTypesHost, bytecode::Bytecode}; +use crate::{ + BaseEvmTypes, EvmTypesHost, + bytecode::Bytecode, + constants::CALL_DEPTH_LIMIT, + interpreter::{InstrStop, Memory, Word}, +}; use alloy_primitives::{Address, B256, Bytes, U256, keccak256}; +use core::ops::Range; /// EVM message kind. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] @@ -37,7 +43,7 @@ pub struct MessageExt { /// Message kind. pub kind: MessageKind, /// Inspector-visible call depth, starting at - /// [`Version::initial_call_depth`](crate::Version::initial_call_depth). + /// [`Host::initial_call_depth`](crate::interpreter::Host::initial_call_depth). pub depth: u16, /// Gas available to this message. pub gas_limit: u64, @@ -64,7 +70,7 @@ pub struct MessageExt { /// Immediate caller. pub caller: Address, /// Call input data, or initcode for create messages. - pub input: Bytes, + pub input: MessageInput, /// Value transferred with the message. pub value: U256, /// Bytecode resolved for this frame: the code at [`MessageExt::code_address`] (the resolved @@ -172,10 +178,178 @@ pub fn derive_create_destination( } } +/// Returns the reason a create message fails before its destination is accessed, if any. +/// +/// `balance` and `nonce` are the creator's, and are zero for an absent account. Failures are +/// checked in order: call depth (more than [`CALL_DEPTH_LIMIT`] below `initial_call_depth`), +/// insufficient balance for `value`, then, for a nested create (`depth` above +/// `initial_call_depth`), a creator nonce at `u64::MAX` (EIP-2681). The transaction itself bumps +/// the nonce of a top-level create, so that one skips the nonce check. +#[inline] +pub fn create_preaccess_failure( + balance: Word, + nonce: u64, + value: Word, + depth: u16, + initial_call_depth: u16, +) -> Option { + if depth.saturating_sub(initial_call_depth) > CALL_DEPTH_LIMIT { + Some(InstrStop::CallTooDeep) + } else if balance < value { + Some(InstrStop::OutOfFunds) + } else if depth > initial_call_depth && nonce == u64::MAX { + Some(InstrStop::NonceOverflow) + } else { + None + } +} + +/// The input of a message: call data, or initcode for create messages. +/// +/// The input of a call instruction's message is a range of the caller frame's memory instead of a +/// copy, since the caller waits for the call to return. The child frame reads it from there, and +/// the inspector hooks that hold the message read it with [`as_bytes`](Self::as_bytes) and the +/// caller's memory, which is the `interp.memory()` of the hook, and a precompile reads it with +/// [`Evm::message_input`](crate::Evm::message_input). To change an input, copy it out and assign +/// an owned one, as in `message.input = bytes.into()`. A clone of such an input is only the range, +/// and does not record whose memory it names: copy the bytes out with +/// [`to_bytes`](Self::to_bytes) to keep or forward them. +/// +/// A running frame reads its own input with +/// [`Interpreter::input`](crate::interpreter::Interpreter::input), and only [`Evm`](crate::Evm) +/// resolves a borrowed input when it enters a frame. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct MessageInput(Repr); + +#[derive(Clone, Debug, PartialEq, Eq)] +enum Repr { + /// The input's bytes. + Owned(Bytes), + /// The input's range in the memory of the caller frame with the entry number `frame`. + Borrowed { range: Range, frame: u64 }, +} + +impl Default for Repr { + #[inline] + fn default() -> Self { + Self::Owned(Bytes::new()) + } +} + +impl MessageInput { + /// Creates an input that is `range` of the memory of the caller frame with the entry number + /// `frame`. + #[inline] + pub(crate) const fn borrowed(range: Range, frame: u64) -> Self { + Self(Repr::Borrowed { range, frame }) + } + + /// Returns the range and the caller frame's entry number of a borrowed input. + #[inline] + pub(crate) const fn borrowed_range(&self) -> Option<(&Range, u64)> { + match &self.0 { + Repr::Owned(_) => None, + Repr::Borrowed { range, frame } => Some((range, *frame)), + } + } + + /// Returns the length of the input. + #[inline] + pub fn len(&self) -> usize { + match &self.0 { + Repr::Owned(bytes) => bytes.len(), + Repr::Borrowed { range, .. } => range.len(), + } + } + + /// Returns whether the input is empty. + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Returns the bytes of an input that owns them, and `None` for a range of the caller's + /// memory. + #[inline] + pub const fn as_owned(&self) -> Option<&Bytes> { + match &self.0 { + Repr::Owned(bytes) => Some(bytes), + Repr::Borrowed { .. } => None, + } + } + + /// Returns the input bytes, reading them from `memory`, the caller frame's memory, unless the + /// input owns them. + /// + /// # Panics + /// + /// Panics if the input's range is out of bounds of `memory`. + #[inline] + pub fn as_bytes<'a>(&'a self, memory: &'a Memory) -> &'a [u8] { + match &self.0 { + Repr::Owned(bytes) => bytes, + Repr::Borrowed { range, .. } => &memory.as_slice()[range.clone()], + } + } + + /// Returns the input as owned bytes, copying them from `memory`, the caller frame's memory, + /// unless the input owns them. + /// + /// # Panics + /// + /// Panics if the input's range is out of bounds of `memory`. + #[inline] + pub fn to_bytes(&self, memory: &Memory) -> Bytes { + match &self.0 { + Repr::Owned(bytes) => bytes.clone(), + Repr::Borrowed { .. } => Bytes::copy_from_slice(self.as_bytes(memory)), + } + } +} + +impl From for MessageInput { + #[inline] + fn from(bytes: Bytes) -> Self { + Self(Repr::Owned(bytes)) + } +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn create_preaccess_failure_order_and_nesting() { + let initial_call_depth = 5; + let max = initial_call_depth + CALL_DEPTH_LIMIT; + let check = |balance: u64, nonce: u64, value: u64, depth: u16| { + create_preaccess_failure( + Word::from(balance), + nonce, + Word::from(value), + depth, + initial_call_depth, + ) + }; + + assert_eq!(check(0, u64::MAX, 1, max + 1), Some(InstrStop::CallTooDeep)); + assert_eq!(check(0, u64::MAX, 1, max), Some(InstrStop::OutOfFunds)); + assert_eq!(check(1, u64::MAX, 1, max), Some(InstrStop::NonceOverflow)); + assert_eq!(check(1, u64::MAX, 1, initial_call_depth), None); + assert_eq!(check(0, 0, 0, max), None); + + // A host-supplied initial depth near `u16::MAX` must not overflow the limit. + let check_at = |initial_call_depth: u16, depth: u16| { + create_preaccess_failure(Word::ZERO, 0, Word::ZERO, depth, initial_call_depth) + }; + assert_eq!(check_at(u16::MAX, u16::MAX), None); + assert_eq!(check_at(u16::MAX - CALL_DEPTH_LIMIT, u16::MAX), None); + assert_eq!( + check_at(u16::MAX - CALL_DEPTH_LIMIT - 1, u16::MAX), + Some(InstrStop::CallTooDeep) + ); + } + #[test] fn is_static_follows_caller_or_staticcall_kind() { let message = |kind, caller_is_static| MessageExt::<()> { @@ -214,4 +388,34 @@ mod tests { // Address-based: a delegate call to the executing account's own address is direct. assert!(delegated(MessageKind::DelegateCall, target).is_direct_call()); } + + #[test] + fn caller_memory_input() { + let mut memory = Memory::new(); + memory.resize(0, 3).unwrap(); + memory.set(0, &[1, 2, 3]); + let input = MessageInput::borrowed(1..3, 0); + let owned = MessageInput::from(Bytes::from_static(&[9])); + let kept = input.clone(); + memory.set(1, &[7]); + + assert_eq!((input.len(), owned.len()), (2, 1)); + assert_eq!(input.as_owned(), None); + assert_eq!(owned.as_owned(), Some(&Bytes::from_static(&[9]))); + // A borrowed input, and a clone of it, read the caller's memory. + assert_eq!(input.as_bytes(&memory), [7, 3]); + assert_eq!(kept.as_bytes(&memory), [7, 3]); + assert_eq!(input.to_bytes(&memory)[..], [7, 3]); + assert_eq!(owned.as_bytes(&memory), [9]); + assert_eq!(MessageInput::default().len(), 0); + } + + #[test] + #[should_panic = "range end index 3 out of range for slice of length 2"] + fn caller_memory_input_out_of_bounds_panics() { + let mut memory = Memory::new(); + memory.resize(0, 2).unwrap(); + + MessageInput::borrowed(1..3, 0).as_bytes(&memory); + } } diff --git a/crates/evm2/src/interpreter/mod.rs b/crates/evm2/src/interpreter/mod.rs index 1f13453a8..7b880aa96 100644 --- a/crates/evm2/src/interpreter/mod.rs +++ b/crates/evm2/src/interpreter/mod.rs @@ -32,7 +32,10 @@ mod memory; pub use memory::Memory; mod message; -pub use message::{Message, MessageExt, MessageKind, derive_create_destination}; +pub use message::{ + Message, MessageExt, MessageInput, MessageKind, create_preaccess_failure, + derive_create_destination, +}; mod host; pub use host::{Host, MessageResult, MessageResultExt}; @@ -65,6 +68,8 @@ pub enum InstrStop { CallTooDeep, /// Insufficient funds for transfer. OutOfFunds, + /// Nonce overflow. + NonceOverflow, // Halt Codes /// Out of gas error. @@ -101,8 +106,6 @@ pub enum InstrStop { CreateCollision, /// Error in precompiled contract execution. PrecompileError, - /// Nonce overflow. - NonceOverflow, /// Exceeded contract size limit during creation. CreateContractSizeLimit, /// Created contract starts with invalid bytes (`0xEF`). @@ -113,6 +116,11 @@ pub enum InstrStop { FatalExternalError, /// Invalid encoding of an instruction's immediate operand. InvalidImmediateEncoding, + /// The inspector's config changed while a `hooked` instruction ran. The dispatch exits with + /// this stop after the instruction, and the frame continues on the table for the new config. No + /// instruction returns it, and a frame never reports it. + #[doc(hidden)] + Reload, } impl InstrStop { @@ -123,16 +131,26 @@ impl InstrStop { matches!(self, Self::Stop | Self::Return | Self::SelfDestruct) } - /// Returns whether execution reverted without an exceptional halt. + /// Returns whether execution ended with the `REVERT` opcode. #[inline] pub const fn is_revert(self) -> bool { - matches!(self, Self::Revert | Self::CallTooDeep | Self::OutOfFunds) + matches!(self, Self::Revert) + } + + /// Returns whether the frame failed without an exceptional halt, so its unused gas returns to + /// the caller. + /// + /// This is the `REVERT` opcode and the failures of a call or create before its frame ran: + /// [`Self::CallTooDeep`], [`Self::OutOfFunds`] and [`Self::NonceOverflow`]. + #[inline] + pub const fn is_frame_revert(self) -> bool { + matches!(self, Self::Revert | Self::CallTooDeep | Self::OutOfFunds | Self::NonceOverflow) } /// Returns whether execution halted exceptionally. #[inline] pub const fn is_halt(self) -> bool { - !self.is_success() && !self.is_revert() + !self.is_success() && !self.is_frame_revert() } /// Returns whether execution ran out of gas or exceeded the memory limit. diff --git a/crates/evm2/src/interpreter/opcode.rs b/crates/evm2/src/interpreter/opcode.rs index 30a17e406..a5ad0fae5 100644 --- a/crates/evm2/src/interpreter/opcode.rs +++ b/crates/evm2/src/interpreter/opcode.rs @@ -200,7 +200,7 @@ impl fmt::Display for OpCode { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let n = self.get(); - if let Some(val) = OPCODE_INFO[n as usize] { + if let Some(val) = OPCODE_INFO[self.as_usize()] { f.write_str(val.name()) } else { write!(f, "UNKNOWN(0x{n:02X})") @@ -650,6 +650,6 @@ mod tests { assert_eq!(OpCode::SWAP16.input_output(), (17, 17)); assert!(OpCode::RETURN.info().is_terminating()); assert!(!OpCode::ADD.info().is_terminating()); - assert_eq!(OpCode::new_or_unknown(0x0C).info().name(), "UNKNOWN"); + assert_eq!(OpCode::new_or_unknown(0x0C).as_str(), "UNKNOWN"); } } diff --git a/crates/evm2/src/interpreter/private.rs b/crates/evm2/src/interpreter/private.rs index 5bbbea2b6..d660decd2 100644 --- a/crates/evm2/src/interpreter/private.rs +++ b/crates/evm2/src/interpreter/private.rs @@ -1,14 +1,28 @@ use super::{Gas, InterpreterState, Pc, Result, StackMut, Word}; use crate::EvmTypesHost; +use bitflags::bitflags; /// Function signature of an `#[instruction]`. pub(crate) type InstructionImplFn = fn(pc: &mut Pc, stack: StackMut<'_>, state: &mut InterpreterState<'_, '_, T>) -> Result; +bitflags! { + /// Properties of an [`Instruction`] that the dispatch acts on. + #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] + pub struct InstructionFlags: u64 { + /// The instruction needs mutable gas state. + const DYNAMIC_GAS = 1 << 0; + /// The instruction may run inspector hooks or host code that read the interpreter, so the + /// dispatch stores the latest pc, stack length and gas into it first. Implies + /// [`Self::DYNAMIC_GAS`]. + const HOOKED = 1 << 1; + } +} + /// EVM instruction implementation. pub trait Instruction { - /// Whether this instruction needs mutable gas state. - const DYNAMIC_GAS: bool = true; + /// The properties of this instruction. + const FLAGS: InstructionFlags = InstructionFlags::DYNAMIC_GAS; /// Executes this instruction. fn execute(pc: &mut Pc, stack: StackMut<'_>, state: &mut InterpreterState<'_, '_, T>) diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index 258138f66..966318baf 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -1,21 +1,32 @@ use super::{ - BytecodeRef, Gas, Host, InstrStop, Memory, Message, Pc, Result, StackBacking, StackMut, - StackRef, Word, + BytecodeRef, Gas, Host, InstrStop, Memory, Message, MessageInput, Pc, Result, StackBacking, + StackMut, StackRef, Word, }; use crate::{ EvmTypesHost, ExecutionConfig, ExecutionError, HostError, SpecId, Version, bytecode::Bytecode, - env::TxEnv, - evm::inspector::Inspector, - interpreter::dispatch, + evm::inspector::{Inspector, OpcodeSet}, + interpreter::{ + dispatch::{self, InstrTable}, + opcode::OpCode, + }, trustme, version::{EvmFeatures, GasParams}, }; -use alloc::{boxed::Box, vec::Vec}; +use alloc::{boxed::Box, format, vec::Vec}; use alloy_primitives::{Address, B256, Bytes}; use core::{fmt, hint::cold_path, ops::Range, ptr::NonNull}; use derive_where::derive_where; +/// Instruction table for a sparse set of stepped opcodes. +struct SparseTable { + /// The stepped opcodes. + steps: OpcodeSet, + /// The config's inspect table the entries at `steps` came from. + source: *const InstrTable, + instructions: InstrTable, +} + /// EVM interpreter. #[derive_where(Debug)] pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { @@ -32,11 +43,25 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { pub(in crate::interpreter) pc: *const u8, output: Range, #[derive_where(skip)] - tx_env: Option<&'frame TxEnv>, - #[derive_where(skip)] message: Option<&'frame Message>, + /// The bytes of the message's input: the message's own bytes, or the caller's memory for an + /// input that is a range of it, with the lifetime erased. The suspended caller does not change + /// its memory until this frame returns. + pub(in crate::interpreter) input: &'frame [u8], + /// The number [`Evm`](crate::Evm) gave this frame when it entered it, which tells a range of + /// this frame's memory from one of another frame. Zero for a frame that `Evm` does not run. + frame: u64, host: Option>>, inspector: Option + 'host>>, + /// Opcodes the inspector steps on the table this frame runs on. + steps: OpcodeSet, + /// Whether the inspector's config changed while the last `hooked` instruction ran. The frame + /// leaves the dispatch after the instruction and continues on the table for the new config. + reload: bool, + /// The last sparse inspected run's step set, the config table it was built from, and the + /// table. + #[derive_where(skip)] + inspect_table: Option>>, pub(in crate::interpreter) stack_len: usize, #[derive_where(skip)] pub(in crate::interpreter) stack: Box, @@ -47,6 +72,9 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { spec: SpecId, features: EvmFeatures, is_static: bool, + /// Whether [`Self::call_input`] returns a range of memory. Set only for frames that + /// [`Evm`](crate::Evm) runs, which resolves the range against this frame's memory. + lends_memory: bool, } // SAFETY: The interpreter's internal pointers are always valid. `pc` and `bytecode_ref` point into @@ -58,17 +86,15 @@ unsafe impl Send for Interpreter<'_, '_, T> where T: EvmTypesHost, T::MessageExt: Sync, - T::TxEnvExt: Sync, { } impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { - /// Creates an interpreter from a transaction-global environment and a frame-local message, - /// which carries the bytecode to run. - pub fn new(tx_env: &'frame TxEnv, message: &'frame Message) -> Self { + /// Creates an interpreter from a frame-local message, which carries the bytecode to run. + pub fn new(message: &'frame Message) -> Self { // SAFETY: Calling init right after. let mut interp = unsafe { Self::uninit() }; - interp.init(tx_env, message); + interp.init(message); interp } @@ -87,12 +113,17 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { result: Ok(()), error: None, output: 0..0, - tx_env: None, message: None, + input: &[], + frame: 0, is_static: false, + lends_memory: false, return_data: Bytes::new(), host: None, inspector: None, + steps: OpcodeSet::EMPTY, + reload: false, + inspect_table: None, spec: SpecId::DEFAULT, features: EvmFeatures::empty(), // SAFETY: `MaybeUninit` does not need initialization. @@ -101,7 +132,7 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { } /// Initializes this interpreter for a new frame, retaining reusable allocations. - fn init(&mut self, tx_env: &'frame TxEnv, message: &'frame Message) { + fn init(&mut self, message: &'frame Message) { let gas_limit = message.gas_limit; let is_static = message.is_static(); self.set_bytecode(&message.code); @@ -111,9 +142,11 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.result = Ok(()); self.error = None; self.output = 0..0; - self.tx_env = Some(tx_env); self.message = Some(message); + self.input = message.input.as_owned().map_or(&[][..], |input| &input[..]); self.is_static = is_static; + self.lends_memory = false; + self.frame = 0; self.return_data = Bytes::new(); } @@ -127,8 +160,8 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { pub(crate) fn clear_frame_refs(&mut self) { self.bytecode = Default::default(); self.bytecode_ref = None; - self.tx_env = None; self.message = None; + self.input = &[]; self.host = None; self.inspector = None; } @@ -249,6 +282,54 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { &mut self.memory } + /// Returns the range of memory at `offset` of `len` bytes as the input of a call message: a + /// range of this frame's memory when this frame is run by [`Evm`](crate::Evm), and a copy + /// otherwise. + /// + /// The range is only meaningful for the host that runs this frame, which reads it from this + /// frame's memory when it executes the message, so it must be in bounds of memory. + #[inline] + #[doc(hidden)] + pub fn call_input(&self, offset: usize, len: usize) -> MessageInput { + if len == 0 { + return MessageInput::default(); + } + if !self.lends_memory { + return Bytes::copy_from_slice(self.memory.slice(offset, len)).into(); + } + MessageInput::borrowed(offset..offset + len, self.frame) + } + + /// Returns the bytes of the active message's input. + #[inline] + pub const fn input(&self) -> &[u8] { + self.input + } + + /// Returns the number [`Evm`](crate::Evm) gave this frame when it entered it. + #[inline] + pub(crate) const fn frame(&self) -> u64 { + self.frame + } + + /// Sets the number [`Evm`](crate::Evm) gave this frame when it entered it. + #[inline] + pub(crate) const fn set_frame(&mut self, frame: u64) { + self.frame = frame; + } + + /// Sets the bytes of the active message's input. + #[inline] + pub(crate) const fn set_input(&mut self, input: &'frame [u8]) { + self.input = input; + } + + /// Lets [`Self::call_input`] return ranges of this frame's memory for the rest of this frame. + #[inline] + pub(crate) const fn lend_memory(&mut self) { + self.lends_memory = true; + } + /// Returns the current interpreter result. #[inline] pub const fn result(&self) -> Result { @@ -280,13 +361,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { core::mem::offset_of!(Self, message) } - /// Returns the cached transaction-global environment. - #[inline] - pub const fn tx_env(&self) -> &'frame TxEnv { - // SAFETY: `tx_env` is initialized before execution starts. - unsafe { self.tx_env.unwrap_unchecked() } - } - /// Returns return data from the last call-like operation. #[inline] pub const fn return_data(&self) -> &Bytes { @@ -434,6 +508,7 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.features = version.features; self.host = Some(NonNull::from(host)); self.inspector = None; + self.reload = false; } /// Runs the host's instruction implementation for the opcode at `pc` on the interpreter @@ -462,7 +537,8 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { // SAFETY: `prepare_run` installed the host, which outlives the run. let config = unsafe { host.as_ref() }.execution_config(); // SAFETY: `pc` is in bounds. - let mut pc = Pc::new(unsafe { bytecode.as_ptr().add(pc) }); + let offset = pc; + let mut pc = Pc::new(unsafe { bytecode.as_ptr().add(offset) }); let instr = config.opcode_config().instruction(pc.op()).instr; self.stack_len = stack_len; let this: *mut Self = self; @@ -478,11 +554,33 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { instr(&mut pc, stack.reborrow(), state)?; // Compiled code continues at the next instruction. if pc.as_ptr() != start { - return Err(InstrStop::FatalExternalError); + return Err(self.fail_host_instruction( + offset, + format_args!("moved the program counter, which compiled code cannot follow"), + )); } Ok(self.stack_len) } + /// Records a fatal error for the host instruction at `pc` that compiled code cannot continue + /// after. + /// + /// # Panics + /// + /// Panics if `pc` is out of bounds. + #[doc(hidden)] + pub fn fail_host_instruction(&mut self, pc: usize, detail: fmt::Arguments<'_>) -> InstrStop { + let opcode = OpCode::new_or_unknown(self.original_bytecode_slice()[pc]); + self.fail(ExecutionError::Fatal( + format!( + "host instruction {} (0x{:02x}) at pc {pc} {detail}", + opcode.as_str(), + opcode.get() + ) + .into(), + )) + } + #[inline(never)] fn run_inner( &mut self, @@ -497,15 +595,96 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { } else { // Load the table only after setup: passing it in keeps it in a callee-saved register // across setup, which makes the dispatch loop spill the stack pointer. - // SAFETY: `prepare_run` initialized the host, and the config is only read here. - let config = unsafe { self.execution_config_detached() }; - let instructions = - if inspector.is_some() { config.inspect_instructions } else { config.instructions }; - dispatch::run(self, instructions) + match inspector { + Some(_) => self.run_inspected(), + None => { + // SAFETY: `prepare_run` initialized the host, and the config is only read + // here. + let config = unsafe { self.execution_config_detached() }; + dispatch::run(self, config.instructions) + } + } }; self.finish_run(stop) } + /// Runs the frame on the table for its inspector's config, and again on the table for the new + /// config each time a child message changes it. + /// + /// Only `self` is live across the dispatch call, which recurses once per nested frame. + #[inline(always)] + fn run_inspected(&mut self) -> InstrStop { + loop { + // A hook before the run, such as `initialize_interp`, may have stopped the frame. + if let Err(stop) = self.result { + cold_path(); + return stop; + } + let instructions = self.select_inspect_table(); + // SAFETY: The table is a static one, or the one `self` holds until the next selection. + let instructions = unsafe { &*instructions }; + let stop = if self.steps == OpcodeSet::ALL { + dispatch::run(self, instructions) + } else { + self.run_inspected_table(instructions) + }; + if stop != InstrStop::Reload { + return stop; + } + cold_path(); + self.result = Ok(()); + self.reload = false; + } + } + + /// Runs a frame that steps some opcodes, or none. Behind a call, so that its loops are not + /// copies inlined into `run_inner`, where they compile worse than the loop for no inspector. + #[inline(never)] + fn run_inspected_table(&mut self, instructions: &InstrTable) -> InstrStop { + dispatch::run(self, instructions) + } + + /// Records the opcodes the inspector steps now and returns the table for them. Not inlined, so + /// that its temporaries stay out of the frame of `run_inspected`. + #[inline(never)] + fn select_inspect_table(&mut self) -> *const InstrTable { + // SAFETY: `prepare_run` initialized the host. The config is read again on each reload and + // only its static tables are used, so nothing outlives a replaced config. + let config = unsafe { self.execution_config_detached() }; + // SAFETY: `run_inner` installed the inspector, which is valid for this run. + let inspector = unsafe { self.inspector.unwrap_unchecked().as_ref() }; + self.steps = inspector.inspector_config().steps(); + if self.steps == OpcodeSet::EMPTY { + return config.instructions; + } + if self.steps == OpcodeSet::ALL { + return config.inspect_instructions; + } + // A sparse set runs the plain handlers, with the inspect table's at the opcodes in the + // set. The table lives in this interpreter and is rebuilt when the set or the config + // differs from the previous run's, so the pooled interpreters of a deep call stack mostly + // reuse theirs. + let source = core::ptr::from_ref(config.inspect_instructions); + let table = self.inspect_table.get_or_insert_with(|| { + Box::new(SparseTable { + steps: OpcodeSet::EMPTY, + source: core::ptr::null(), + instructions: *config.instructions, + }) + }); + if table.steps != self.steps || table.source != source { + table.steps = self.steps; + table.source = source; + table.instructions = *config.instructions; + for op in 0..=u8::MAX { + if self.steps.contains(op) { + table.instructions[op as usize] = config.inspect_instructions[op as usize]; + } + } + } + &raw const table.instructions + } + /// Takes an owned error recorded by an instruction or inspector hook. pub(crate) const fn take_error(&mut self) -> Option { self.error.take() @@ -567,18 +746,42 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { self.0.inspector.is_some() } + /// Returns the opcodes the inspector steps in this frame. #[inline] - pub(crate) const fn set_pc_stack_len(&mut self, pc: *const u8, stack_len: usize) { - self.0.pc = pc; - self.0.stack_len = stack_len; + #[cfg(not(tco))] + pub(crate) const fn steps(&self) -> OpcodeSet { + self.0.steps } - /// Returns the cached transaction-global environment. + /// Requests a reload if the inspector's config no longer matches the table this frame runs on. + /// The dispatch calls it after each `hooked` instruction returns, and leaves for the reload. + #[inline(always)] + pub(crate) fn note_inspector_config_change(&mut self) { + if self.0.inspector.is_some() { + self.reload_if_config_changed(); + } + } + + #[inline(never)] + fn reload_if_config_changed(&mut self) { + if let Some(inspector) = self.0.inspector + // SAFETY: The inspector pointer is valid while the frame executes. + && unsafe { inspector.as_ref() }.inspector_config().steps() != self.0.steps + { + self.0.reload = true; + } + } + + /// Returns whether the running instruction requested a reload. #[inline] - pub const fn tx(&self) -> &'frame TxEnv { - // SAFETY: `tx_env` is initialized at the beginning of `run` and remains set for - // instruction execution. - unsafe { self.0.tx_env.unwrap_unchecked() } + pub(crate) const fn reload_requested(&self) -> bool { + self.0.reload + } + + #[inline] + pub(crate) const fn set_pc_stack_len(&mut self, pc: *const u8, stack_len: usize) { + self.0.pc = pc; + self.0.stack_len = stack_len; } /// Returns the active base specification ID. @@ -598,10 +801,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { /// Returns the host implementation. #[inline] pub const fn host(&mut self) -> &mut T::Host<'host> { - // SAFETY: `host` is initialized at the beginning of `run` and cleared before the - // method returns. Instruction execution is synchronous, so the pointer cannot outlive the - // `run` host borrow. - unsafe { self.0.host.unwrap_unchecked().as_mut() } + self.0.host() } /// Returns the active runtime version data. @@ -618,6 +818,12 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { unsafe { self.0.message.unwrap_unchecked() } } + /// Returns the bytes of the active message's input. + #[inline] + pub const fn input(&self) -> &[u8] { + self.0.input() + } + /// Returns whether the active frame forbids state-changing operations. #[inline] pub const fn is_static(&self) -> bool { @@ -698,8 +904,7 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { #[inline] pub(crate) fn inspect_step(&mut self, pc: Pc, stack_len: usize) { - self.0.pc = pc.as_ptr(); - self.0.stack_len = stack_len; + self.set_pc_stack_len(pc.as_ptr(), stack_len); unsafe { let mut inspector = self.0.inspector.unwrap_unchecked(); inspector.as_mut().step(&mut self.0); @@ -708,17 +913,23 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { #[inline] pub(crate) fn inspect_step_end(&mut self, pc: Pc, stack_len: usize) { - self.0.pc = pc.as_ptr(); - self.0.stack_len = stack_len; - if self.0.result.is_err_and(InstrStop::is_out_of_gas) { + self.set_pc_stack_len(pc.as_ptr(), stack_len); + if self.0.result.is_err() { cold_path(); - // Failed charges may leave a wrapped counter until frame settlement. - self.0.gas.set_remaining(0); + if self.0.result.is_err_and(InstrStop::is_out_of_gas) { + // An out-of-gas frame has burned all its gas, so 0 is the real remaining gas. + self.0.gas.set_remaining(0); + } } unsafe { let mut inspector = self.0.inspector.unwrap_unchecked(); inspector.as_mut().step_end(&mut self.0); } + // A stop the hook set wins over the reload. + if self.0.reload && self.0.result.is_ok() { + cold_path(); + self.set_result(Err(InstrStop::Reload)); + } } #[inline] @@ -749,15 +960,14 @@ impl InterpreterPool { pub(crate) fn pop<'frame, 'host>( &mut self, - tx_env: &'frame TxEnv, message: &'frame Message, ) -> Box> { let frame = match self.frames.pop() { Some(mut frame) => { - frame.init(tx_env, message); + frame.init(message); frame } - None => Box::new(Interpreter::new(tx_env, message)), + None => Box::new(Interpreter::new(message)), }; // SAFETY: Frames stored in the pool have their frame-local references cleared before they // are erased to `'static`. Rebinding the lifetime is only used to initialize the next @@ -790,13 +1000,12 @@ impl InterpreterPool { #[cfg(test)] mod owned_error_tests { use super::*; - use crate::{DatabaseError, env::TxEnvExt, interpreter::MessageExt, test_utils::TestTypes}; + use crate::{DatabaseError, interpreter::MessageExt, test_utils::TestTypes}; #[test] fn owned_error_is_taken_on_exit() { - let tx = TxEnvExt::default(); let message = MessageExt::default(); - let mut interpreter = Interpreter::::new(&tx, &message); + let mut interpreter = Interpreter::::new(&message); let error = DatabaseError::new(core::fmt::Error, false); let stop = interpreter.fail(error.clone()); let result = interpreter.finish_run(stop); diff --git a/crates/evm2/src/interpreter/stack.rs b/crates/evm2/src/interpreter/stack.rs index 83279501a..4c39d1eb4 100644 --- a/crates/evm2/src/interpreter/stack.rs +++ b/crates/evm2/src/interpreter/stack.rs @@ -11,6 +11,12 @@ use core::{ /// EVM stack word. pub type Word = U256; +/// Units of [`RawStack`]'s stored length per stack word: the word size in bytes for the tail-call +/// dispatch on x86-64, where an address scale stops at 8 and a word index costs a shift, and 1 +/// elsewhere, where the scale is free or the table dispatch only reads the length at its entry. +const RAW_LEN_SCALE: usize = + if cfg!(all(tco, target_arch = "x86_64")) { size_of::() } else { 1 }; + /// Operand stack storage. /// /// Aligned to the word size so that no word crosses a cache line. Loads and stores that cross a @@ -35,6 +41,7 @@ pub struct Stack<'a> { #[derive(Clone, Copy)] pub(crate) struct RawStack<'a> { stack: *mut StackBacking, + /// Length in units of [`RAW_LEN_SCALE`] per word. len: usize, marker: PhantomData<&'a mut StackBacking>, } @@ -203,19 +210,31 @@ impl<'a> RawStack<'a> { #[inline(always)] pub(crate) const fn new(stack: *mut StackBacking, len: usize) -> Self { debug_assert!(len <= Stack::CAPACITY); - Self { stack, len, marker: PhantomData } + Self { stack, len: len * RAW_LEN_SCALE, marker: PhantomData } } #[inline(always)] pub(crate) const fn len(self) -> usize { - self.len + let raw = self.len; + // Only the scaled length gets these facts, so that the table backends compile unchanged. + if RAW_LEN_SCALE > 1 { + // SAFETY: Type invariant. + unsafe { + core::hint::assert_unchecked(raw.is_multiple_of(RAW_LEN_SCALE)); + core::hint::assert_unchecked(raw <= Stack::CAPACITY * RAW_LEN_SCALE); + } + } + raw / RAW_LEN_SCALE } #[inline(always)] #[cfg(tco)] pub(crate) const fn set_len(&mut self, len: usize) { debug_assert!(len <= Stack::CAPACITY); - self.len = len; + // Add the word delta to the stored length instead of scaling `len`, so that a push or pop + // compiles to `stored ± RAW_LEN_SCALE` rather than `(stored / RAW_LEN_SCALE ± 1) * + // RAW_LEN_SCALE`. + self.len = self.len.wrapping_add(len.wrapping_sub(self.len()).wrapping_mul(RAW_LEN_SCALE)); } /// Borrows the stack backing. @@ -226,7 +245,7 @@ impl<'a> RawStack<'a> { #[inline(always)] pub(crate) unsafe fn borrow(&mut self) -> Stack<'_> { // SAFETY: The caller upholds exclusive access to the stack backing. - Stack::new(unsafe { &mut *self.stack }, self.len) + Stack::new(unsafe { &mut *self.stack }, self.len()) } } @@ -252,8 +271,8 @@ impl<'a> StackMut<'a> { /// The caller must keep the length at or below [`STACK_LIMIT`] and ensure the first `len` /// words are initialized before creating another stack view. #[inline] - pub const unsafe fn into_raw_parts(self) -> (*mut Word, &'a mut usize) { - (self.stack.0.as_mut_ptr().cast(), self.len) + pub const unsafe fn into_raw_parts(mut self) -> (*mut Word, &'a mut usize) { + (self.as_word_mut_ptr(), self.len) } #[inline] @@ -645,11 +664,11 @@ mod tests { #[test] fn push_and_pop() { run(|stack| { - stack.push(Word::from(1)).unwrap(); + stack.push(Word::ONE).unwrap(); stack.push(Word::from(2)).unwrap(); - assert_eq!(stack.as_slice(), [Word::from(1), Word::from(2)]); + assert_eq!(stack.as_slice(), [Word::ONE, Word::from(2)]); assert_eq!(stack.pop().unwrap(), Word::from(2)); - assert_eq!(stack.popn::<1>().unwrap(), [Word::from(1)]); + assert_eq!(stack.popn::<1>().unwrap(), [Word::ONE]); assert_matches!(stack.pop(), Err(InstrStop::StackUnderflow)); }); @@ -662,8 +681,8 @@ mod tests { fn popn_top() { run_with_len(3, |stack| { let (popped, top) = stack.popn_top::<2>().unwrap(); - assert_eq!(popped, [Word::from(2), Word::from(1)]); - assert_eq!(*top, Word::from(0)); + assert_eq!(popped, [Word::from(2), Word::ONE]); + assert_eq!(*top, Word::ZERO); *top = Word::from(9); assert_eq!(stack.as_slice(), [Word::from(9)]); }); @@ -677,13 +696,13 @@ mod tests { fn popn_dyn() { run_with_len(4, |stack| { let popped = stack.popn_dyn(3).unwrap().collect::>(); - assert_eq!(popped, [Word::from(3), Word::from(2), Word::from(1)]); - assert_eq!(stack.as_slice(), [Word::from(0)]); + assert_eq!(popped, [Word::from(3), Word::from(2), Word::ONE]); + assert_eq!(stack.as_slice(), [Word::ZERO]); }); run_with_len(2, |stack| { assert_matches!(stack.popn_dyn(3).map(|_| ()), Err(InstrStop::StackUnderflow)); - assert_eq!(stack.as_slice(), [Word::from(0), Word::from(1)]); + assert_eq!(stack.as_slice(), [Word::ZERO, Word::ONE]); }); } @@ -691,10 +710,10 @@ mod tests { fn peekn() { run_with_len(3, |stack| { assert_eq!(stack.peek(0), Some(Word::from(2))); - assert_eq!(stack.peek(1), Some(Word::from(1))); - assert_eq!(stack.peekn::<2>(), Some([Word::from(2), Word::from(1)])); + assert_eq!(stack.peek(1), Some(Word::ONE)); + assert_eq!(stack.peekn::<2>(), Some([Word::from(2), Word::ONE])); assert_eq!(stack.peekn::<4>(), None); - assert_eq!(stack.as_slice(), [Word::from(0), Word::from(1), Word::from(2)]); + assert_eq!(stack.as_slice(), [Word::ZERO, Word::ONE, Word::from(2)]); }); let mut backing = StackBacking([MaybeUninit::new(Word::ZERO); StackRef::CAPACITY]); @@ -702,7 +721,7 @@ mod tests { word.write(Word::from(i)); } let stack = StackRef::new(&backing, 3); - assert_eq!(stack.peekn::<3>(), Some([Word::from(2), Word::from(1), Word::from(0)])); + assert_eq!(stack.peekn::<3>(), Some([Word::from(2), Word::ONE, Word::ZERO])); assert_eq!(stack.peekn::<4>(), None); } @@ -728,19 +747,19 @@ mod tests { stack.dup(2).unwrap(); assert_eq!( stack.as_slice(), - [Word::from(0), Word::from(1), Word::from(2), Word::from(3), Word::from(2)] + [Word::ZERO, Word::ONE, Word::from(2), Word::from(3), Word::from(2)] ); stack.swap(3).unwrap(); assert_eq!( stack.as_slice(), - [Word::from(0), Word::from(2), Word::from(2), Word::from(3), Word::from(1)] + [Word::ZERO, Word::from(2), Word::from(2), Word::from(3), Word::ONE] ); stack.exchange(1, 4).unwrap(); assert_eq!( stack.as_slice(), - [Word::from(3), Word::from(2), Word::from(2), Word::from(0), Word::from(1)] + [Word::from(3), Word::from(2), Word::from(2), Word::ZERO, Word::ONE] ); }); diff --git a/crates/evm2/src/lib.rs b/crates/evm2/src/lib.rs index f32e35036..2918f26a7 100644 --- a/crates/evm2/src/lib.rs +++ b/crates/evm2/src/lib.rs @@ -1,5 +1,6 @@ #![doc = include_str!("../README.md")] #![cfg_attr(tco, feature(explicit_tail_calls), allow(incomplete_features))] +#![cfg_attr(all(tco, target_arch = "x86_64"), feature(fn_align))] #![cfg_attr(all(tco, not(cranelift)), feature(rust_preserve_none_cc))] #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(test), warn(unused_crate_dependencies))] @@ -11,6 +12,8 @@ extern crate alloc; #[cfg(feature = "nightly")] use ruint as _; +#[cfg(feature = "block")] +pub mod block; pub mod bytecode; pub mod constants; mod error; @@ -31,7 +34,7 @@ pub use evm::{ }, env, handler, inspector, precompile, registry, }; -pub use inspector::{Inspector, NoopInspector}; +pub use inspector::{Inspector, InspectorConfig, NoopInspector, OpcodeSet}; pub mod precompiles; pub use precompiles::{ @@ -47,7 +50,7 @@ pub mod version; pub use version::{EvmFeatures, OpcodeChange, OpcodeConfig, OpcodeView, Version}; mod spec_id; -pub use spec_id::SpecId; +pub use spec_id::{ParseSpecIdError, SpecId}; mod once_lock; mod storage_key; diff --git a/crates/evm2/src/precompiles/bls12_381/g1_msm.rs b/crates/evm2/src/precompiles/bls12_381/g1_msm.rs index b59b145aa..ad5905314 100644 --- a/crates/evm2/src/precompiles/bls12_381/g1_msm.rs +++ b/crates/evm2/src/precompiles/bls12_381/g1_msm.rs @@ -57,13 +57,13 @@ mod test { use super::*; use crate::precompiles::kzg_point_evaluation::RETURN_VALUE; use alloc::vec::Vec; - use alloy_primitives::{Bytes, U256, hex}; + use alloy_primitives::{U256, bytes, hex}; #[test] fn bls_g1multiexp_g1_not_on_curve_but_in_subgroup() { - let input = Bytes::from(hex!( + let input = bytes!( "000000000000000000000000000000000a2833e497b38ee3ca5c62828bf4887a9f940c9e426c7890a759c20f248c23a7210d2432f4c98a514e524b5184a0ddac00000000000000000000000000000000150772d56bf9509469f9ebcd6e47570429fd31b0e262b66d512e245c38ec37255529f2271fd70066473e393a8bead0c30000000000000000000000000000000000000000000000000000000000000000" - )); + ); let fail = run(&input, &mut GasTracker::new(G1_MSM_BASE_GAS_FEE)); assert_eq!( fail.err().and_then(|e| e.as_halt().cloned()), @@ -100,14 +100,14 @@ mod test { let scalars = [ U256::ZERO, - U256::from(1), + U256::ONE, U256::from(2), - r - U256::from(1), + r - U256::ONE, r, - r + U256::from(1), + r + U256::ONE, r * U256::from(2), - r * U256::from(2) + U256::from(1), - U256::from(1) << 255, + r * U256::from(2) + U256::ONE, + U256::ONE << 255, U256::MAX, ]; for scalar in scalars { @@ -127,21 +127,15 @@ mod test { (&non_subgroup, PrecompileHalt::Bls12381G1NotInSubgroup), ] { assert_eq!(msm(&[(bad, scalar)]), Err(Some(err.clone()))); - assert_eq!(msm(&[(&point, U256::from(1)), (bad, scalar)]), Err(Some(err.clone()))); - assert_eq!(msm(&[(&point, scalar), (bad, U256::from(1))]), Err(Some(err))); + assert_eq!(msm(&[(&point, U256::ONE), (bad, scalar)]), Err(Some(err.clone()))); + assert_eq!(msm(&[(&point, scalar), (bad, U256::ONE)]), Err(Some(err))); } } - assert_eq!(msm(&[(&point, U256::from(1))]), Ok(point.to_vec())); + assert_eq!(msm(&[(&point, U256::ONE)]), Ok(point.to_vec())); assert_eq!(msm(&[(&point, r), (&point, r * U256::from(2))]), Ok(infinity.to_vec())); - assert_eq!( - msm(&[(&point, r - U256::from(1)), (&point, U256::from(1))]), - Ok(infinity.to_vec()) - ); - assert_eq!( - msm(&[(&point, r - U256::from(1)), (&point, U256::from(2))]), - Ok(point.to_vec()) - ); + assert_eq!(msm(&[(&point, r - U256::ONE), (&point, U256::ONE)]), Ok(infinity.to_vec())); + assert_eq!(msm(&[(&point, r - U256::ONE), (&point, U256::from(2))]), Ok(point.to_vec())); // Many pairs with scalars just below `r` (bit 254 set) sum to `-n(n+1)/2 * P`. let pairs = (0..64u64).map(|i| (&point, r - U256::from(1 + i))).collect::>(); diff --git a/crates/evm2/src/precompiles/bls12_381/g2_msm.rs b/crates/evm2/src/precompiles/bls12_381/g2_msm.rs index a219bcc4b..1e1d8514a 100644 --- a/crates/evm2/src/precompiles/bls12_381/g2_msm.rs +++ b/crates/evm2/src/precompiles/bls12_381/g2_msm.rs @@ -84,14 +84,14 @@ mod test { let scalars = [ U256::ZERO, - U256::from(1), + U256::ONE, U256::from(2), - r - U256::from(1), + r - U256::ONE, r, - r + U256::from(1), + r + U256::ONE, r * U256::from(2), - r * U256::from(2) + U256::from(1), - U256::from(1) << 255, + r * U256::from(2) + U256::ONE, + U256::ONE << 255, U256::MAX, ]; for scalar in scalars { @@ -111,21 +111,15 @@ mod test { (&non_subgroup, PrecompileHalt::Bls12381G2NotInSubgroup), ] { assert_eq!(msm(&[(bad, scalar)]), Err(Some(err.clone()))); - assert_eq!(msm(&[(&point, U256::from(1)), (bad, scalar)]), Err(Some(err.clone()))); - assert_eq!(msm(&[(&point, scalar), (bad, U256::from(1))]), Err(Some(err))); + assert_eq!(msm(&[(&point, U256::ONE), (bad, scalar)]), Err(Some(err.clone()))); + assert_eq!(msm(&[(&point, scalar), (bad, U256::ONE)]), Err(Some(err))); } } - assert_eq!(msm(&[(&point, U256::from(1))]), Ok(point.to_vec())); + assert_eq!(msm(&[(&point, U256::ONE)]), Ok(point.to_vec())); assert_eq!(msm(&[(&point, r), (&point, r * U256::from(2))]), Ok(infinity.to_vec())); - assert_eq!( - msm(&[(&point, r - U256::from(1)), (&point, U256::from(1))]), - Ok(infinity.to_vec()) - ); - assert_eq!( - msm(&[(&point, r - U256::from(1)), (&point, U256::from(2))]), - Ok(point.to_vec()) - ); + assert_eq!(msm(&[(&point, r - U256::ONE), (&point, U256::ONE)]), Ok(infinity.to_vec())); + assert_eq!(msm(&[(&point, r - U256::ONE), (&point, U256::from(2))]), Ok(point.to_vec())); // Many pairs with scalars just below `r` (bit 254 set) sum to `-n(n+1)/2 * P`. let pairs = (0..64u64).map(|i| (&point, r - U256::from(1 + i))).collect::>(); diff --git a/crates/evm2/src/precompiles/bls12_381/map_fp_to_g1.rs b/crates/evm2/src/precompiles/bls12_381/map_fp_to_g1.rs index 53c0ca150..d1435bf35 100644 --- a/crates/evm2/src/precompiles/bls12_381/map_fp_to_g1.rs +++ b/crates/evm2/src/precompiles/bls12_381/map_fp_to_g1.rs @@ -32,13 +32,13 @@ pub fn run(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { #[cfg(test)] mod test { use super::*; - use alloy_primitives::{Bytes, hex}; + use alloy_primitives::bytes; #[test] fn sanity_test() { - let input = Bytes::from(hex!( + let input = bytes!( "000000000000000000000000000000006900000000000000636f6e7472616374595a603f343061cd305a03f40239f5ffff31818185c136bc2595f2aa18e08f17" - )); + ); let fail = run(&input, &mut GasTracker::new(MAP_FP_TO_G1_BASE_GAS_FEE)); assert_eq!( fail.err().and_then(|e| e.as_halt().cloned()), diff --git a/crates/evm2/src/precompiles/bn254/mod.rs b/crates/evm2/src/precompiles/bn254/mod.rs index cebe3c8b2..330166489 100644 --- a/crates/evm2/src/precompiles/bn254/mod.rs +++ b/crates/evm2/src/precompiles/bn254/mod.rs @@ -262,99 +262,92 @@ mod tests { pair::{BYZANTIUM_PAIR_BASE, BYZANTIUM_PAIR_PER_POINT}, }, }; - use alloy_primitives::hex; + use alloy_primitives::{B256, hex}; use core::assert_matches; use super::*; #[test] fn test_bn254_add() { - let input = hex::decode( + let input = hex!( "\ 18b18acfb4c2c30276db5411368e7185b311dd124691610c5d3b74034e093dc9\ 063c909c4720840cb5134cb9f59fa749755796819658d32efc0d288198f37266\ 07c2b7f58a84bd6145f00c9c2bc0bb1a187f20ff2c92963a88019e7c6a014eed\ - 06614e20c147e940f2d70da3f74c9a17df361706a4485c742bd6788478fa17d7", - ) - .unwrap(); - let expected = hex::decode( + 06614e20c147e940f2d70da3f74c9a17df361706a4485c742bd6788478fa17d7" + ); + let expected = hex!( "\ 2243525c5efd4b9c3d3c45ac0ca3fe4dd85e830a4ce6b65fa1eeaee202839703\ - 301d1d33be6da8e509df21cc35964723180eed7532537db9ae5e7d48f195c915", - ) - .unwrap(); + 301d1d33be6da8e509df21cc35964723180eed7532537db9ae5e7d48f195c915" + ); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), expected); // Zero sum test - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); - let expected = hex::decode( + 0000000000000000000000000000000000000000000000000000000000000000" + ); + let expected = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), expected); // Out of gas test - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); - let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(499)); + let res = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(499)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::OutOfGas))); // No input test let input = [0u8; 0]; - let expected = hex::decode( + let expected = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), expected); // Point not on curve fail - let input = hex::decode( + let input = hex!( "\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ - 1111111111111111111111111111111111111111111111111111111111111111", - ) - .unwrap(); + 1111111111111111111111111111111111111111111111111111111111111111" + ); - let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); + let res = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); assert_matches!( res, Err(PrecompileError::Halt(PrecompileHalt::Bn254AffineGFailedToCreate)), ); // Short input is right-padded. This makes the first field element non-canonical. - let res = run_add(&[0x40], BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); + let res = add::run(&[0x40], BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::Bn254FieldPointNotAMember)),); // A coordinate equal to the field modulus is non-canonical. let input = hex!("30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47"); - let res = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); + let res = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::Bn254FieldPointNotAMember)),); // The largest canonical coordinate, p - 1, is accepted: (p - 1, sqrt(2)) is on the curve. @@ -365,133 +358,122 @@ mod tests { input[32..64].copy_from_slice(&hex!( "279d7bc4e184e3a57f5fa684690c6df6b484a7f1daa1de608d266a2a4be6593f" )); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), &input[..64]); - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000001\ 30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd45\ 0000000000000000000000000000000000000000000000000000000000000001\ - 30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd45", - ) - .unwrap(); - let expected = hex::decode( + 30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd45" + ); + let expected = hex!( "\ 030644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd3\ - 1a76dae6d3272396d0cbe61fced2bc532edac647851e3ac53ce1cc9c7e645a83", - ) - .unwrap(); + 1a76dae6d3272396d0cbe61fced2bc532edac647851e3ac53ce1cc9c7e645a83" + ); - let outcome = run_add(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); + let outcome = add::run(&input, BYZANTIUM_ADD_GAS_COST, &mut GasTracker::new(500)).unwrap(); assert_eq!(outcome.bytes(), expected); } #[test] fn test_bn254_mul() { - let input = hex::decode( + let input = hex!( "\ 2bd3e6d0f3b142924f5ca7b49ce5b9d54c4703d7ae5648e61d02268b1a0a9fb7\ 21611ce0a6af85915e2f1d70300909ce2e49dfad4a4619c8390cae66cefdb204\ - 00000000000000000000000000000000000000000000000011138ce750fa15c2", - ) - .unwrap(); - let expected = hex::decode( + 00000000000000000000000000000000000000000000000011138ce750fa15c2" + ); + let expected = hex!( "\ 070a8d6a982153cae4be29d434e8faef8a47b274a053f5a4ee2a6c9c13c31e5c\ - 031b8ce914eba3a9ffb989f9cdd5b0f01943074bf4f0f315690ec3cec6981afc", - ) - .unwrap(); + 031b8ce914eba3a9ffb989f9cdd5b0f01943074bf4f0f315690ec3cec6981afc" + ); let outcome = - run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); + mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); assert_eq!(outcome.bytes(), expected); // Out of gas test - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0200000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0200000000000000000000000000000000000000000000000000000000000000" + ); - let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(39_999)); + let res = mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(39_999)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::OutOfGas))); // Zero multiplication test - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0200000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); - let expected = hex::decode( + 0200000000000000000000000000000000000000000000000000000000000000" + ); + let expected = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); let outcome = - run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); + mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); assert_eq!(outcome.bytes(), expected); // No input test let input = [0u8; 0]; - let expected = hex::decode( + let expected = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); let outcome = - run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); + mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); assert_eq!(outcome.bytes(), expected); // Point not on curve fail - let input = hex::decode( + let input = hex!( "\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ - 0f00000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0f00000000000000000000000000000000000000000000000000000000000000" + ); - let res = run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)); + let res = mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)); assert_matches!( res, Err(PrecompileError::Halt(PrecompileHalt::Bn254AffineGFailedToCreate)), ); // Short input is right-padded. This makes the point x-coordinate non-canonical. - let res = run_mul(&[0x40], BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)); + let res = mul::run(&[0x40], BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)); assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::Bn254FieldPointNotAMember)),); - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000001\ 30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd45\ - 0000000000000000000000000000000000000000000000000000000000000002", - ) - .unwrap(); - let expected = hex::decode( + 0000000000000000000000000000000000000000000000000000000000000002" + ); + let expected = hex!( "\ 030644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd3\ - 1a76dae6d3272396d0cbe61fced2bc532edac647851e3ac53ce1cc9c7e645a83", - ) - .unwrap(); + 1a76dae6d3272396d0cbe61fced2bc532edac647851e3ac53ce1cc9c7e645a83" + ); let outcome = - run_mul(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); + mul::run(&input, BYZANTIUM_MUL_GAS_COST, &mut GasTracker::new(40_000)).unwrap(); assert_eq!(outcome.bytes(), expected); } #[test] fn test_bn254_pair() { - let input = hex::decode( + let input = hex!( "\ 1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59\ 3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41\ @@ -504,14 +486,11 @@ mod tests { 198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2\ 1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed\ 090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b\ - 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa", - ) - .unwrap(); - let expected = - hex::decode("0000000000000000000000000000000000000000000000000000000000000001") - .unwrap(); + 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa" + ); + let expected = hex!("0000000000000000000000000000000000000000000000000000000000000001"); - let outcome = run_pair( + let outcome = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -521,7 +500,7 @@ mod tests { assert_eq!(outcome.bytes(), expected); // Out of gas test - let input = hex::decode( + let input = hex!( "\ 1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59\ 3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41\ @@ -534,11 +513,10 @@ mod tests { 198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2\ 1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed\ 090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b\ - 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa", - ) - .unwrap(); + 12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa" + ); - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -548,11 +526,9 @@ mod tests { // No input test let input = [0u8; 0]; - let expected = - hex::decode("0000000000000000000000000000000000000000000000000000000000000001") - .unwrap(); + let expected = hex!("0000000000000000000000000000000000000000000000000000000000000001"); - let outcome = run_pair( + let outcome = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -562,18 +538,17 @@ mod tests { assert_eq!(outcome.bytes(), expected); // Point not on curve fail - let input = hex::decode( + let input = hex!( "\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ - 1111111111111111111111111111111111111111111111111111111111111111", - ) - .unwrap(); + 1111111111111111111111111111111111111111111111111111111111111111" + ); - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -585,18 +560,17 @@ mod tests { ); // G2 point on the curve but not in the subgroup fail - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000002\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000001\ 0d1271953ed9ea0836846e70a1934187998c7f790cb4d7511b7f8da82de048a4\ - 2869111d5381f072f8e2728fdb825a51aadd70e52c9830e9ab4b871c0531f1bb", - ) - .unwrap(); + 2869111d5381f072f8e2728fdb825a51aadd70e52c9830e9ab4b871c0531f1bb" + ); - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -623,15 +597,14 @@ mod tests { let pair_infinity_with = |g2: &str| { let mut input = [0u8; PAIR_ELEMENT_LEN]; hex::decode_to_slice(g2, &mut input[G1_LEN..]).unwrap(); - run_pair( + pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, &mut GasTracker::new(260_000), ) }; - let mut one = [0u8; 32]; - one[31] = 1; + let one: [u8; 32] = B256::with_last_byte(1).into(); assert_eq!(pair_infinity_with(SUBGROUP_G2).unwrap().bytes(), &one); for g2 in TORSION_G2 { assert_matches!( @@ -642,7 +615,7 @@ mod tests { let mut input = [0u8; PAIR_ELEMENT_LEN]; input[0] = 0x40; - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -651,16 +624,15 @@ mod tests { assert_matches!(res, Err(PrecompileError::Halt(PrecompileHalt::Bn254FieldPointNotAMember))); // Invalid input length - let input = hex::decode( + let input = hex!( "\ 1111111111111111111111111111111111111111111111111111111111111111\ 1111111111111111111111111111111111111111111111111111111111111111\ 111111111111111111111111111111\ - ", - ) - .unwrap(); + " + ); - let res = run_pair( + let res = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -670,21 +642,18 @@ mod tests { // Test with point at infinity - should return true (identity element) // G1 point at infinity (0,0) followed by a valid G2 point - let input = hex::decode( + let input = hex!( "\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ 209dd15ebff5d46c4bd888e51a93cf99a7329636c63514396b4a452003a35bf7\ 04bf11ca01483bfa8b34b43561848d28905960114c8ac04049af4b6315a41678\ 2bb8324af6cfc93537a2ad1a445cfd0ca2a71acd7ac41fadbf933c2a51be344d\ - 120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550", - ) - .unwrap(); - let expected = - hex::decode("0000000000000000000000000000000000000000000000000000000000000001") - .unwrap(); + 120a2a4cf30c1bf9845f20c6fe39e07ea2cce61f0c9bb048165fe5e4de877550" + ); + let expected = hex!("0000000000000000000000000000000000000000000000000000000000000001"); - let outcome = run_pair( + let outcome = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, @@ -695,18 +664,17 @@ mod tests { // Test with G2 point at infinity - should also return true // Valid G1 point followed by G2 point at infinity (0,0,0,0) - let input = hex::decode( + let input = hex!( "\ 1c76476f4def4bb94541d57ebba1193381ffa7aa76ada664dd31c16024c43f59\ 3034dd2920f673e204fee2811c678745fc819b55d3e9d294e45c9b03a76aef41\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000000" + ); - let outcome = run_pair( + let outcome = pair::run( &input, BYZANTIUM_PAIR_PER_POINT, BYZANTIUM_PAIR_BASE, diff --git a/crates/evm2/src/precompiles/crypto.rs b/crates/evm2/src/precompiles/crypto.rs index 9c7dc94f5..aab72f023 100644 --- a/crates/evm2/src/precompiles/crypto.rs +++ b/crates/evm2/src/precompiles/crypto.rs @@ -7,13 +7,13 @@ use crate::{ }; use alloc::{boxed::Box, vec::Vec}; use core::fmt::Debug; +use sha2::Digest; /// Crypto operations trait for precompiles. pub trait Crypto: Send + Sync + Debug { /// Compute SHA-256 hash #[inline] fn sha256(&self, input: &[u8]) -> [u8; 32] { - use sha2::Digest; let output = sha2::Sha256::digest(input); output.into() } @@ -21,7 +21,6 @@ pub trait Crypto: Send + Sync + Debug { /// Compute RIPEMD-160 hash #[inline] fn ripemd160(&self, input: &[u8]) -> [u8; 32] { - use ripemd::Digest; let mut hasher = ripemd::Ripemd160::new(); hasher.update(input); diff --git a/crates/evm2/src/precompiles/hash.rs b/crates/evm2/src/precompiles/hash.rs index 9bcb229cc..58160da07 100644 --- a/crates/evm2/src/precompiles/hash.rs +++ b/crates/evm2/src/precompiles/hash.rs @@ -17,7 +17,7 @@ pub fn run_sha256(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { let cost = calc_linear_cost(input.len(), 60, 12); gas.spend(cost)?; let output = crate::precompiles::crypto().sha256(input); - Ok(PrecompileOutput::new(output.to_vec().into())) + Ok(PrecompileOutput::new(output.into())) } /// Computes the RIPEMD-160 hash of the input data @@ -30,5 +30,5 @@ pub fn run_ripemd160(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { let gas_used = calc_linear_cost(input.len(), 600, 120); gas.spend(gas_used)?; let output = crate::precompiles::crypto().ripemd160(input); - Ok(PrecompileOutput::new(output.to_vec().into())) + Ok(PrecompileOutput::new(output.into())) } diff --git a/crates/evm2/src/precompiles/kzg_point_evaluation/arkworks.rs b/crates/evm2/src/precompiles/kzg_point_evaluation/arkworks.rs index 19397da7f..e085f4d93 100644 --- a/crates/evm2/src/precompiles/kzg_point_evaluation/arkworks.rs +++ b/crates/evm2/src/precompiles/kzg_point_evaluation/arkworks.rs @@ -72,7 +72,7 @@ fn read_scalar_canonical(bytes: &[u8; 32]) -> Result { // Check if the field element is canonical by serializing back and comparing let bytes_roundtrip = fr.into_bigint().to_bytes_be(); - if bytes_roundtrip.as_slice() != bytes { + if bytes_roundtrip != bytes { return Err(PrecompileHalt::NonCanonicalFp); } diff --git a/crates/evm2/src/precompiles/kzg_point_evaluation/mod.rs b/crates/evm2/src/precompiles/kzg_point_evaluation/mod.rs index e709dd5a3..5fc62a04f 100644 --- a/crates/evm2/src/precompiles/kzg_point_evaluation/mod.rs +++ b/crates/evm2/src/precompiles/kzg_point_evaluation/mod.rs @@ -109,6 +109,7 @@ pub(crate) fn verify_kzg_proof( #[cfg(test)] mod tests { use super::*; + use alloy_primitives::B256; #[test] fn basic_test() { @@ -239,11 +240,7 @@ mod tests { "a62ad71d14c5719385c0686f1871430475bf3a00f0aa3f7b8dd99a9abc2160744faf0070725e00b60ad9a026a15b1a8c" ); let wrong_y = hex!("1522a4a7f34e1ea350ae07c29c96c7e79655aa926122e95fe69fcbd932ca49ea"); - let scalar = |n: u8| { - let mut bytes = [0u8; 32]; - bytes[31] = n; - bytes - }; + let scalar = |n: u8| B256::with_last_byte(n).0; let cases = [ ("c-kzg valid", valid_commitment, r_minus_one, valid_y, valid_proof, true), diff --git a/crates/evm2/src/precompiles/modexp.rs b/crates/evm2/src/precompiles/modexp.rs index 1c91f8b72..330242c53 100644 --- a/crates/evm2/src/precompiles/modexp.rs +++ b/crates/evm2/src/precompiles/modexp.rs @@ -4,7 +4,7 @@ use crate::{ interpreter::GasTracker, precompiles::{PrecompileHalt, PrecompileOutput, PrecompileResult, eip7823}, - utils::{left_pad, left_pad_vec_be, read_word_padded, right_pad_vec}, + utils::{b256_to_word, left_pad, left_pad_vec_be, read_word_padded, right_pad_vec}, }; #[cfg(feature = "gmp")] use alloc::vec; @@ -154,19 +154,19 @@ fn modexp_big(base: &[u8], exponent: &[u8], modulus: &[u8]) -> Vec { /// See: /// See: pub fn run_byzantium(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { - run_inner(input, gas, 0, false, byzantium_gas_calc) + run_inner(input, gas, BYZANTIUM_PARAMS, false, byzantium_gas_calc) } /// See: /// Gas cost of berlin is modified from byzantium. pub fn run_berlin(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { - run_inner(input, gas, 200, false, berlin_gas_calc) + run_inner(input, gas, BERLIN_PARAMS, false, berlin_gas_calc) } /// See: -/// Gas cost of berlin is modified from byzantium. +/// See: pub fn run_osaka(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { - run_inner(input, gas, 500, true, osaka_gas_calc) + run_inner(input, gas, OSAKA_PARAMS, true, osaka_gas_calc) } /// Calculate the iteration count for the modexp precompile. @@ -186,14 +186,15 @@ pub(crate) fn calculate_iteration_count(exp_length: u64, exp_highp: &U256, multi } /// Run the modexp precompile. -pub fn run_inner( +fn run_inner( input: &[u8], gas: &mut GasTracker, - min_gas: u64, + params: GasCalcParams, osaka: bool, calc_gas: fn(u64, u64, u64, &U256) -> u64, ) -> PrecompileResult { - gas.spend(min_gas)?; + // Fail before parsing the lengths when the minimum price is unaffordable. + gas.spend(params.min_price)?; // The format of input is: // @@ -202,24 +203,15 @@ pub fn run_inner( const HEADER_LENGTH: usize = 96; // Extract the header - let base_len = U256::from_be_bytes(read_word_padded(input, 0).0); - let exp_len = U256::from_be_bytes(read_word_padded(input, 32).0); - let mod_len = U256::from_be_bytes(read_word_padded(input, 64).0); + let base_len = b256_to_word(read_word_padded(input, 0)); + let exp_len = b256_to_word(read_word_padded(input, 32)); + let mod_len = b256_to_word(read_word_padded(input, 64)); - // Cast base and modulus to usize, it does not make sense to handle larger values - let base_len = usize::try_from(base_len).map_err(|_| PrecompileHalt::ModexpEip7823LimitSize)?; - let mod_len = usize::try_from(mod_len).map_err(|_| PrecompileHalt::ModexpEip7823LimitSize)?; - // cast exp len to the max size, it will fail later in gas calculation if it is too large. - let exp_len = usize::try_from(exp_len).unwrap_or(usize::MAX); - - // for EIP-7823 we need to check size of inputs - if osaka - && (base_len > eip7823::INPUT_SIZE_LIMIT - || mod_len > eip7823::INPUT_SIZE_LIMIT - || exp_len > eip7823::INPUT_SIZE_LIMIT) - { - return Err(PrecompileHalt::ModexpEip7823LimitSize.into()); - } + // Lengths that do not fit in 64 bits cost more gas than any call has. + let [base_len64, exp_len64, mod_len64] = + [base_len, exp_len, mod_len].map(|len| len.saturating_to::()); + let [base_len, exp_len, mod_len] = + [base_len64, exp_len64, mod_len64].map(|len| usize::try_from(len).unwrap_or(usize::MAX)); // Used to extract ADJUSTED_EXPONENT_LENGTH. let exp_highp_len = min(exp_len, 32); @@ -237,8 +229,17 @@ pub fn run_inner( }; // Check if we have enough gas. - let gas_cost = calc_gas(base_len as u64, exp_len as u64, mod_len as u64, &exp_highp); - gas.spend(gas_cost.saturating_sub(min_gas))?; + let gas_cost = calc_gas(base_len64, exp_len64, mod_len64, &exp_highp); + gas.spend(gas_cost - params.min_price)?; + + // EIP-7823 limits the size of the inputs, after the gas is charged. + if osaka + && [base_len64, exp_len64, mod_len64] + .into_iter() + .any(|len| len > eip7823::INPUT_SIZE_LIMIT as u64) + { + return Err(PrecompileHalt::ModexpEip7823LimitSize.into()); + } if base_len == 0 && mod_len == 0 { return Ok(PrecompileOutput::new(Bytes::new())); @@ -269,7 +270,7 @@ pub(crate) fn byzantium_gas_calc( exp_len, mod_len, exp_highp, - GasCalcParams { min_price: 0, multiplier: 8, divisor: 20 }, + BYZANTIUM_PARAMS, byzantium_multiplication_complexity, ) } @@ -277,14 +278,7 @@ pub(crate) fn byzantium_gas_calc( /// Calculate gas cost according to EIP 2565: /// pub(crate) fn berlin_gas_calc(base_len: u64, exp_len: u64, mod_len: u64, exp_highp: &U256) -> u64 { - gas_calc( - base_len, - exp_len, - mod_len, - exp_highp, - GasCalcParams { min_price: 200, multiplier: 8, divisor: 3 }, - berlin_multiplication_complexity, - ) + gas_calc(base_len, exp_len, mod_len, exp_highp, BERLIN_PARAMS, berlin_multiplication_complexity) } /// Calculate gas cost according to EIP-7883: @@ -295,14 +289,7 @@ pub(crate) fn berlin_gas_calc(base_len: u64, exp_len: u64, mod_len: u64, exp_hig /// 2. Increase cost when exponent is larger than 32 bytes /// 3. Increase cost when base or modulus is larger than 32 bytes pub(crate) fn osaka_gas_calc(base_len: u64, exp_len: u64, mod_len: u64, exp_highp: &U256) -> u64 { - gas_calc( - base_len, - exp_len, - mod_len, - exp_highp, - GasCalcParams { min_price: 500, multiplier: 16, divisor: 1 }, - osaka_multiplication_complexity, - ) + gas_calc(base_len, exp_len, mod_len, exp_highp, OSAKA_PARAMS, osaka_multiplication_complexity) } #[derive(Clone, Copy, Debug)] @@ -312,6 +299,10 @@ pub(crate) struct GasCalcParams { divisor: u64, } +const BYZANTIUM_PARAMS: GasCalcParams = GasCalcParams { min_price: 0, multiplier: 8, divisor: 20 }; +const BERLIN_PARAMS: GasCalcParams = GasCalcParams { min_price: 200, multiplier: 8, divisor: 3 }; +const OSAKA_PARAMS: GasCalcParams = GasCalcParams { min_price: 500, multiplier: 16, divisor: 1 }; + /// Calculate gas cost. #[inline] pub(crate) fn gas_calc( @@ -353,8 +344,7 @@ fn osaka_multiplication_complexity(max_len: u64) -> U256 { return U256::from(16); } - let words = U256::from(max_len.div_ceil(8)); - words * words * U256::from(2) + berlin_multiplication_complexity(max_len) * U256::from(2) } #[cfg(test)] @@ -599,6 +589,7 @@ mod tests { base_len: U256, exp_len: U256, mod_len: U256, + gas: u64, expected: Option, } @@ -617,42 +608,69 @@ mod tests { base_len: U256::from(1025), exp_len: U256::from(1024), mod_len: U256::from(1024), + gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { base_len: U256::from(1024), exp_len: U256::from(1025), mod_len: U256::from(1024), + gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { base_len: U256::from(1024), exp_len: U256::from(1024), mod_len: U256::from(1025), + gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { - base_len: U256::from(0), - exp_len: U256::from(0), + base_len: U256::ZERO, + exp_len: U256::ZERO, mod_len: U256::from(1025), + gas: u64::MAX, expected: Some(PrecompileHalt::ModexpEip7823LimitSize), }, TestInput { base_len: U256::from(1024), exp_len: U256::from(1024), mod_len: U256::from(1024), + gas: 100_000_000, expected: Some(PrecompileHalt::OutOfGas), }, TestInput { - base_len: U256::from(0), - exp_len: U256::from(0), - mod_len: U256::from(0), + base_len: U256::ONE << 64, + exp_len: U256::ONE, + mod_len: U256::ONE, + gas: u64::MAX, + expected: Some(PrecompileHalt::ModexpEip7823LimitSize), + }, + TestInput { + base_len: U256::ONE, + exp_len: U256::ONE << 64, + mod_len: U256::ONE, + gas: u64::MAX, + expected: Some(PrecompileHalt::ModexpEip7823LimitSize), + }, + TestInput { + base_len: U256::ONE, + exp_len: U256::ONE, + mod_len: U256::ONE << 64, + gas: u64::MAX, + expected: Some(PrecompileHalt::ModexpEip7823LimitSize), + }, + TestInput { + base_len: U256::ZERO, + exp_len: U256::ZERO, + mod_len: U256::ZERO, + gas: 100_000_000, expected: None, }, ]; for test in test_inputs { let input = test.input(); - let res = run_osaka(&input, &mut GasTracker::new(100_000_000)).err(); + let res = run_osaka(&input, &mut GasTracker::new(test.gas)).err(); let halt = match &res { Some(PrecompileError::Halt(halt)) => Some(halt), None => None, @@ -664,57 +682,78 @@ mod tests { } } + #[test] + fn oversized_lengths_are_charged_before_they_are_rejected() { + let input = |lengths: [U256; 3]| { + lengths.iter().flat_map(|len| len.to_be_bytes::<32>()).collect::>() + }; + let halt = |input: &[u8], run: fn(&[u8], &mut GasTracker) -> PrecompileResult| match run( + input, + &mut GasTracker::new(100_000), + ) { + Err(PrecompileError::Halt(halt)) => halt, + result => panic!("expected a halt, got {result:?}"), + }; + // A length that does not fit in 64 bits costs more than any call can pay, on every fork. + for position in 0..3 { + let mut lengths = [U256::ONE; 3]; + lengths[position] = U256::ONE << 64; + for run in [run_byzantium, run_berlin, run_osaka] { + assert_eq!(halt(&input(lengths), run), PrecompileHalt::OutOfGas, "{position}"); + } + } + // An Osaka length over the limit is out of gas when the gas does not cover its price. + let lengths = [U256::from(65_536), U256::ONE, U256::ONE]; + assert_eq!(halt(&input(lengths), run_osaka), PrecompileHalt::OutOfGas); + } + #[test] fn test_modexp_edge_cases() { // Test case 1: Zero base with non-zero exponent and modulus - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 00\ 05\ - 07", - ) - .unwrap(); + 07" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes(), vec![0x00], "0^5 mod 7 should be 0"); // Test case 2: Base equals modulus - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 07\ 03\ - 07", - ) - .unwrap(); + 07" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes(), vec![0x00], "7^3 mod 7 should be 0"); // Test case 3: Exponent is zero (result should always be 1) - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000001\ 05\ - 07", - ) - .unwrap(); + 07" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes(), vec![0x01], "5^0 mod 7 should be 1"); // Test case 4: Large base with small modulus // Actually, (2^256 - 1) mod 3 = 0, so 0^2 = 0 - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000020\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\ 02\ - 03", - ) - .unwrap(); + 03" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); // (2^256 - 1) = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff // This is divisible by 3, so (2^256 - 1) mod 3 = 0 @@ -737,15 +776,14 @@ mod tests { assert_eq!(gas.spent(), 200, "Empty input should use minimum gas 200 for Berlin"); // Test gas consumption with very small inputs - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 01\ 01\ - 01", - ) - .unwrap(); + 01" + ); // For Byzantium, check that it computes correctly let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes(), vec![0x00], "1^1 mod 1 = 0"); @@ -759,15 +797,14 @@ mod tests { #[test] fn test_modexp_padding() { // Test that results are properly padded to modulus length - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000020\ 02\ 03\ - 0000000000000000000000000000000000000000000000000000000000000101", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000101" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); assert_eq!(res.bytes().len(), 32, "Result should be padded to modulus length"); assert_eq!(res.bytes()[31], 8, "2^3 mod 257 = 8"); @@ -776,15 +813,14 @@ mod tests { #[test] fn test_modexp_berlin_vs_byzantium_gas() { // Test that Berlin gas costs are lower than Byzantium for the same input - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000020\ 0000000000000000000000000000000000000000000000000000000000000020\ 0000000000000000000000000000000000000000000000000000000000000020\ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\ - fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd", - ) - .unwrap(); + fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd" + ); let gas_limit = 10_000_000u64; let mut byzantium_gas = GasTracker::new(gas_limit); @@ -804,30 +840,28 @@ mod tests { #[test] fn test_modexp_osaka_size_limits() { // Test Osaka's 1024-byte limit per parameter - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000400\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 00\ 01\ - 01", - ) - .unwrap(); + 01" + ); // Should succeed with exactly 1024 bytes let res = run_osaka(&input, &mut GasTracker::new(100_000_000)); assert!(res.is_ok(), "1024-byte base should be allowed"); // Test with 1025 bytes - should fail - let input_fail = hex::decode( + let input_fail = hex!( "0000000000000000000000000000000000000000000000000000000000000401\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000001\ 00\ 01\ - 01", - ) - .unwrap(); + 01" + ); let res = run_osaka(&input_fail, &mut GasTracker::new(100_000_000)); assert_matches!( @@ -840,13 +874,13 @@ mod tests { #[test] fn test_modexp_input_truncation() { // Test behavior when input is shorter than expected - let input = hex::decode( + // Only 1 byte provided for 2-byte base + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000002\ 0000000000000000000000000000000000000000000000000000000000000002\ 0000000000000000000000000000000000000000000000000000000000000002\ - ff", // Only 1 byte provided for 2-byte base - ) - .unwrap(); + ff" + ); let res = run_byzantium(&input, &mut GasTracker::new(100_000)).unwrap(); // Should pad with zeros and compute ff00^ff00 mod ff00 @@ -856,15 +890,14 @@ mod tests { #[test] fn test_modexp_maximum_values() { // Test with maximum U256 values - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000020\ 0000000000000000000000000000000000000000000000000000000000000001\ 0000000000000000000000000000000000000000000000000000000000000020\ ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\ 01\ - fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe", - ) - .unwrap(); + fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe" + ); let res = run_byzantium(&input, &mut GasTracker::new(10_000_000)).unwrap(); assert_eq!(res.bytes().len(), 32, "Result should be 32 bytes"); @@ -915,12 +948,11 @@ mod tests { #[test] fn test_modexp_out_of_gas() { // Test that large inputs properly return out of gas error - let input = hex::decode( + let input = hex!( "0000000000000000000000000000000000000000000000000000000000000100\ 0000000000000000000000000000000000000000000000000000000000000100\ - 0000000000000000000000000000000000000000000000000000000000000100", - ) - .unwrap(); + 0000000000000000000000000000000000000000000000000000000000000100" + ); // Provide insufficient gas let res = run_byzantium(&input, &mut GasTracker::new(1000)); diff --git a/crates/evm2/src/precompiles/secp256k1/mod.rs b/crates/evm2/src/precompiles/secp256k1/mod.rs index 7504fcac8..1cf4a4f87 100644 --- a/crates/evm2/src/precompiles/secp256k1/mod.rs +++ b/crates/evm2/src/precompiles/secp256k1/mod.rs @@ -46,7 +46,7 @@ pub fn run(input: &[u8], gas: &mut GasTracker) -> PrecompileResult { let sig = input.last_chunk().unwrap(); let res = crate::precompiles::crypto().secp256k1_ecrecover(sig, recid, msg).ok(); - let out = res.map(|o| o.to_vec().into()).unwrap_or_default(); + let out = res.map(|o| o.into()).unwrap_or_default(); Ok(PrecompileOutput::new(out)) } diff --git a/crates/evm2/src/precompiles/table.rs b/crates/evm2/src/precompiles/table.rs index 37d491d81..4bed41a25 100644 --- a/crates/evm2/src/precompiles/table.rs +++ b/crates/evm2/src/precompiles/table.rs @@ -16,6 +16,9 @@ use core::fmt::{self, Display}; use derive_where::derive_where; /// Precompile implementation function. +/// +/// It reads the message's input with [`Evm::message_input`], since the input of a call +/// instruction's message is a range of the caller's memory. pub type PrecompileFn = fn(&mut Evm<'_, T>, &Message, &mut GasTracker) -> PrecompileResult; @@ -163,7 +166,7 @@ impl PrecompileMap { pub fn with_capacity(capacity: usize) -> Self { Self { inner: AddressMap::with_capacity_and_hasher(capacity, Default::default()), - ..Self::default() + ..Self::new() } } @@ -449,11 +452,11 @@ macro_rules! define_precompiles { #[allow(non_snake_case)] $vis const fn $name() -> $crate::precompiles::Precompile { fn run( - _evm: &mut $crate::Evm<'_, T>, + evm: &mut $crate::Evm<'_, T>, message: &$crate::interpreter::Message, gas: &mut $crate::interpreter::GasTracker, ) -> $crate::precompiles::PrecompileResult { - $f(message.input.as_ref(), gas) + $f(evm.message_input(message), gas) } $crate::precompiles::Precompile::new( @@ -522,7 +525,7 @@ define_precompiles! { #[cfg(test)] mod tests { use super::*; - use crate::evm::precompile::PrecompileOutput; + use crate::{evm::precompile::PrecompileOutput, precompiles::Precompiles}; use alloy_primitives::{Bytes, address}; use core::assert_matches; @@ -530,7 +533,7 @@ mod tests { fn map_precompile_updates_data_at_target_address() { let identity = IDENTITY::(); let address = identity.address(); - let mut map = PrecompileMap::from_precompiles([identity]); + let mut map = Precompiles::map([identity]); map.map_precompile(&address, |precompile| { precompile @@ -568,7 +571,7 @@ mod tests { fn map_precompiles_preserves_existing_addresses() { let identity = IDENTITY::(); let sha256 = SHA256::(); - let mut map = PrecompileMap::from_precompiles([identity.clone(), sha256.clone()]); + let mut map = Precompiles::map([identity.clone(), sha256.clone()]); map.map_precompiles(|_, precompile| { assert_matches!(precompile.id(), PrecompileId::Identity | PrecompileId::Sha256); @@ -588,7 +591,7 @@ mod tests { let source = identity.address(); let missing = address!("0x0000000000000000000000000000000000000999"); let dest = address!("0x0000000000000000000000000000000000001000"); - let mut map = PrecompileMap::from_precompiles([identity]); + let mut map = Precompiles::map([identity]); let err = map.move_precompiles([(source, dest), (missing, sha256.address())]); @@ -605,7 +608,7 @@ mod tests { let sha256_address = sha256.address(); let new_identity = address!("0x0000000000000000000000000000000000001001"); let new_sha256 = address!("0x0000000000000000000000000000000000001002"); - let mut map = PrecompileMap::from_precompiles([identity, sha256]); + let mut map = Precompiles::map([identity, sha256]); map.move_precompiles([(identity_address, new_identity), (sha256_address, new_sha256)]) .unwrap(); @@ -622,7 +625,7 @@ mod tests { let identity_address = identity.address(); let first = address!("0x0000000000000000000000000000000000001001"); let second = address!("0x0000000000000000000000000000000000001002"); - let mut map = PrecompileMap::from_precompiles([identity]); + let mut map = Precompiles::map([identity]); map.move_precompiles([(identity_address, first), (identity_address, second)]).unwrap(); @@ -637,7 +640,7 @@ mod tests { let short_miss = address!("0x0000000000000000000000000000000000000999"); let long = address!("0x1000000000000000000000000000000000000004"); let long_miss = address!("0x1000000000000000000000000000000000000005"); - let mut map = PrecompileMap::from_precompiles([identity]); + let mut map = Precompiles::map([identity]); assert_eq!(map.get_data(&identity_address).unwrap().id(), &PrecompileId::Identity); assert!(map.get_data(&short_miss).is_none()); @@ -661,14 +664,14 @@ mod tests { let identity_address = identity.address(); let sha256_address = sha256.address(); let long = address!("0x1000000000000000000000000000000000000004"); - let mut map = PrecompileMap::from_precompiles([identity, sha256]); + let mut map = Precompiles::map([identity, sha256]); map.move_precompiles([(sha256_address, long)]).unwrap(); let mut warm = PrewarmSet::new(); map.warm_addresses(&mut warm); assert!(warm.is_warm(&identity_address)); assert!(warm.is_warm(&long)); - assert!(!warm.is_warm(&sha256_address)); + assert!(warm.is_cold(&sha256_address)); map.move_precompiles([(long, sha256_address)]).unwrap(); assert!(!map.contains(&long)); @@ -677,6 +680,6 @@ mod tests { let mut warm = PrewarmSet::new(); map.warm_addresses(&mut warm); assert!(warm.is_warm(&sha256_address)); - assert!(!warm.is_warm(&long)); + assert!(warm.is_cold(&long)); } } diff --git a/crates/evm2/src/spec_id.rs b/crates/evm2/src/spec_id.rs index 02fa4731a..f4e6f7846 100644 --- a/crates/evm2/src/spec_id.rs +++ b/crates/evm2/src/spec_id.rs @@ -1,3 +1,5 @@ +use core::{fmt, str::FromStr}; + /// Specification IDs and their activation points. /// /// Information was obtained from the [Ethereum Execution Specifications](https://github.com/ethereum/execution-specs). @@ -119,6 +121,47 @@ impl SpecId { } } +macro_rules! spec_names { + ([] $($spec:ident $lower:ident,)*) => { + impl SpecId { + /// Returns the upper-case variant name, such as `"SPURIOUS_DRAGON"`. + pub const fn name(self) -> &'static str { + match self { + $(Self::$spec => stringify!($spec),)* + } + } + } + + impl FromStr for SpecId { + type Err = ParseSpecIdError; + + /// Parses a variant name, ignoring ASCII case. + fn from_str(s: &str) -> Result { + $( + if s.eq_ignore_ascii_case(stringify!($spec)) { + return Ok(Self::$spec); + } + )* + Err(ParseSpecIdError) + } + } + }; +} + +crate::for_each_spec!([] spec_names); + +/// Error returned when parsing a [`SpecId`] from an unknown name. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ParseSpecIdError; + +impl fmt::Display for ParseSpecIdError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("unknown spec id") + } +} + +impl core::error::Error for ParseSpecIdError {} + impl From for u32 { #[inline] fn from(spec_id: SpecId) -> Self { @@ -193,4 +236,15 @@ mod tests { fn defaults() { assert_eq!(SpecId::DEFAULT, SpecId::default()); } + + #[test] + fn names_round_trip() { + for value in 0..SpecId::COUNT as u32 { + let spec = SpecId::try_from_u32(value).unwrap(); + assert_eq!(spec.name().parse(), Ok(spec)); + assert_eq!(spec.name().to_ascii_lowercase().parse(), Ok(spec)); + } + assert_eq!("SPURIOUS_DRAGON".parse(), Ok(SpecId::SPURIOUS_DRAGON)); + assert_eq!("CONSTANTINOPLE".parse::(), Err(ParseSpecIdError)); + } } diff --git a/crates/evm2/src/test_utils.rs b/crates/evm2/src/test_utils.rs index 9f5cac91f..e51d3d43c 100644 --- a/crates/evm2/src/test_utils.rs +++ b/crates/evm2/src/test_utils.rs @@ -1,16 +1,21 @@ use crate::{ - BaseEvmConfigSelector, DatabaseError, EvmFeatures, EvmTypesHost, ExecutionConfig, SpecId, + BaseEvmConfigSelector, BaseEvmTypes, DatabaseError, EvmFeatures, EvmTypesHost, ExecutionConfig, + SpecId, TxResult, TxResultExt, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, env::{BlockEnv, BlockEnvExt, TxEnv, TxEnvExt}, - evm::{AccountLoad, SLoad, SStore, SelfDestructResult}, + ethereum::{RecoveredTxEnvelope, TxEnvelope}, + evm::{AccountLoad, SLoad, SStore, SelfDestructResult, StorageSlot, Tracked}, interpreter::{ Gas, GasTracker, Host, InstrStop, Interpreter, Memory, Message, MessageExt, MessageKind, MessageResult, MessageResultExt, StackBacking, Word, op, }, + registry::{HandlerResult, TxRequest}, storage_key::{StorageKey, StorageKeyMap}, + utils::address_to_word, }; use alloc::{borrow::Cow, boxed::Box, vec::Vec}; +use alloy_consensus::{TxLegacy, transaction::Recovered}; use alloy_primitives::{Address, B256, Bytes, Log}; use core::{assert_matches, ops::Range}; @@ -35,6 +40,7 @@ pub(crate) struct TestHost { pub(crate) spec_id: SpecId, pub(crate) execution_config: ExecutionConfig, pub(crate) block: BlockEnv, + pub(crate) tx_env: TxEnv, pub(crate) code_hash: B256, pub(crate) code: Bytes, pub(crate) exists: bool, @@ -53,7 +59,7 @@ pub(crate) struct TestHost { pub(crate) calls: Vec>, pub(crate) call_static_flags: Vec, pub(crate) selfdestructs: Vec<(Address, Address, bool)>, - pub(crate) new_account_checks: Vec

, + pub(crate) account_loads: Vec
, } impl Default for TestHost { @@ -64,6 +70,7 @@ impl Default for TestHost { SpecId::OSAKA, ), block: BlockEnvExt::default(), + tx_env: TxEnvExt::default(), code_hash: B256::ZERO, code: Bytes::new(), exists: true, @@ -85,7 +92,7 @@ impl Default for TestHost { calls: Vec::new(), call_static_flags: Vec::new(), selfdestructs: Vec::new(), - new_account_checks: Vec::new(), + account_loads: Vec::new(), } } } @@ -103,6 +110,14 @@ impl Host for TestHost { &self.block } + fn tx_env(&mut self) -> &TxEnv { + &self.tx_env + } + + fn initial_call_depth(&self) -> u16 { + 0 + } + fn load_account( &mut self, address: &Address, @@ -112,6 +127,7 @@ impl Host for TestHost { if skip_cold_load && self.is_cold { return Err(InstrStop::OutOfGas.into()); } + self.account_loads.push(*address); Ok(AccountLoad { balance: address.into_word().into(), nonce: 0, @@ -121,25 +137,21 @@ impl Host for TestHost { } else { Bytecode::default() }, - exists: self.exists, is_empty: self.is_empty, + is_empty_for_new_account_gas: if self + .execution_config + .version() + .feature(EvmFeatures::EIP161) + { + !self.exists || self.is_empty + } else { + !self.exists && !self.is_touched + }, is_cold: self.is_cold, _non_exhaustive: (), }) } - fn target_is_empty_for_new_account_gas( - &mut self, - address: &Address, - features: EvmFeatures, - ) -> Result { - self.new_account_checks.push(*address); - if features.contains(EvmFeatures::EIP161) { - return Ok(!self.exists || self.is_empty); - } - Ok(!self.exists && !self.is_touched) - } - fn block_hash(&mut self, number: &Word) -> Result { if self.missing_block_hash { return Err(DatabaseError::new(crate::AnyError::from("missing block hash"), true)); @@ -204,7 +216,6 @@ impl Host for TestHost { fn execute_message( &mut self, - _tx_env: &TxEnv, message: &mut Message, ) -> Result, crate::ExecutionError> { // Mimics the depth limit enforced by the real host. @@ -331,20 +342,29 @@ impl Default for RunConfig<'_> { host: None, spec_id: SpecId::OSAKA, tx_env: TxEnvExt::default(), - message: MessageExt { gas_limit: 10_000, ..MessageExt::default() }, + message: test_message(), gas_limit: 10_000, return_data: Bytes::new(), } } } +pub(crate) fn test_message() -> Message { + MessageExt { gas_limit: 10_000, ..MessageExt::default() } +} + +pub(crate) fn test_host(block: BlockEnv) -> TestHost { + TestHost { block, ..TestHost::default() } +} + pub(crate) fn run(config: RunConfig<'_>) -> TestInterpreter { let RunConfig { code, host, spec_id, tx_env, message, gas_limit, return_data } = config; let message = Message:: { code: legacy_bytecode(code), gas_limit, ..message }; - let mut inner = Interpreter::::new(&tx_env, &message); + let mut inner = Interpreter::::new(&message); *inner.return_data_mut() = return_data; let mut default_host = TestHost::default(); let host = host.unwrap_or(&mut default_host); + host.tx_env = tx_env; host.spec_id = spec_id; host.execution_config = ExecutionConfig::for_base_spec::(spec_id); let (err, execution_error) = match inner.run(host) { @@ -441,13 +461,82 @@ pub(crate) fn push_all(code: &mut Vec, values: [T } pub(crate) fn push_address(code: &mut Vec, address: &Address) { - push(code, Word::from_be_slice(address.as_slice())); + push(code, address_to_word(address)); } pub(crate) fn neg(value: u64) -> Word { Word::ZERO.wrapping_sub(Word::from(value)) } +/// Calls `target` through `call_op` with memory 0..32 as input and returns its 32-byte output. +pub(crate) fn memory_input_call_code(target: Address, call_op: u8) -> Vec { + let mut code = Vec::new(); + push_all(&mut code, [Word::from_be_bytes([0xaa; 32]), Word::ZERO]); + code.push(op::MSTORE); + push_all( + &mut code, + [ + Word::from(32), + Word::from(32), + Word::from(32), + Word::ZERO, + Word::ZERO, + address_to_word(&target), + Word::from(50_000), + ], + ); + code.extend([call_op, op::POP]); + push_all(&mut code, [Word::from(32), Word::from(32)]); + code.push(op::RETURN); + code +} + pub(crate) fn legacy_bytecode(code: impl Into>) -> Bytecode { Bytecode::new_legacy(Bytes::from(code.into())) } + +pub(crate) fn test_tx(value: u64) -> RecoveredTxEnvelope { + Recovered::new_unchecked( + TxEnvelope::Legacy(TxLegacy { nonce: value, ..TxLegacy::default() }), + Address::ZERO, + ) +} + +pub(crate) fn handle_test_tx( + req: TxRequest<'_, '_, BaseEvmTypes, TxLegacy>, +) -> HandlerResult { + let _ = req.host.spec_id(); + Ok(TxResultExt { status: true, total_gas_spent: req.tx.nonce + 1, ..TxResultExt::default() }) +} + +/// Appends code that returns the word at the top of the stack as the frame output. +pub(crate) fn return_top_word(code: &mut Vec) { + code.extend([op::PUSH0, op::MSTORE, op::PUSH1, 32, op::PUSH0, op::RETURN]); +} + +pub(crate) fn storage_slot(original: Word, current: Word) -> StorageSlot { + StorageSlot { value: Tracked::from_parts(original, current), ..Default::default() } +} + +pub(crate) fn call_code(target: Address) -> Vec { + let mut code = Vec::new(); + push_all( + &mut code, + [ + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + Word::ZERO, + address_to_word(&target), + Word::from(1000), + ], + ); + code +} + +pub(crate) fn create_code() -> Vec { + let mut code = Vec::new(); + push_all(&mut code, [Word::ZERO, Word::ZERO, Word::ZERO]); + code +} diff --git a/crates/evm2/src/tests.rs b/crates/evm2/src/tests.rs index 3a431aa6f..8d829d940 100644 --- a/crates/evm2/src/tests.rs +++ b/crates/evm2/src/tests.rs @@ -1,22 +1,22 @@ use crate::{ BaseEvmTypes, Evm, EvmFeatures, ExecutionConfig, Inspector, Precompiles, SpecId, Version, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, ethereum::{ - LazyAuthorization, LazyTxEip7702, RecoveredTxEnvelope, TxEnvelope, + LazyTxEip7702, RecoveredTxEnvelope, TxEnvelope, eip7702::{AuthRefunds, Eip7702Authorization, RuntimeAuthCharges, apply_auth_list}, ethereum_tx_registry, prepare_initial_frame, }, evm::{AccountInfo, InMemoryDB, SYSTEM_ADDRESS}, interpreter::{GasTracker, Host, InstrStop, MessageExt, Word, op}, registry::TxRegistry, - test_utils::{legacy_bytecode, push, push_address, push_all}, + test_utils::{legacy_bytecode, push, push_address, push_all, return_top_word}, version::GasId, }; use alloc::{vec, vec::Vec}; -use alloy_consensus::{TxEip7702, TxLegacy, transaction::Recovered}; -use alloy_eips::eip7702::{Authorization, RecoveredAuthority, RecoveredAuthorization}; -use alloy_primitives::{Address, Log, TxKind, U256, hex}; +use alloy_consensus::{TxEip1559, TxEip2930, TxEip7702, TxLegacy, transaction::Recovered}; +use alloy_eips::eip7702::{Authorization, SignedAuthorization}; +use alloy_primitives::{Address, Log, TxKind, U256, bytes, hex}; type TestEvm = Evm<'static, BaseEvmTypes>; @@ -28,13 +28,13 @@ fn run_tx(evm: &mut TestEvm, destination: Address, code: impl Into>) { code: legacy_bytecode(code), ..Default::default() }; - let result = Host::execute_message(evm, &TxEnvExt::default(), &mut message).unwrap(); - assert!(result.stop.is_success()); + let result = Host::execute_message(evm, &mut message).unwrap(); + assert!(result.is_success()); } #[test] fn evm_executes_storage_transaction() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); let mut evm = TestEvm::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -52,18 +52,15 @@ fn evm_executes_storage_transaction() { run_tx(&mut evm, contract, [op::PUSH1, 0x2a, op::PUSH1, 0x01, op::SSTORE, op::STOP]); - assert_eq!( - evm.state.storage_slot(&contract, Word::from(1)).unwrap().current(), - Word::from(0x2a) - ); + assert_eq!(evm.state.storage_slot(&contract, Word::ONE).unwrap().current(), Word::from(0x2a)); } #[test] fn evm_runs_transactions_against_initial_state() { - let contract = Address::from([0x22; 20]); + let contract = Address::repeat_byte(0x22); let mut database = InMemoryDB::default(); database.insert_account_info(&contract, AccountInfo { nonce: 1, ..Default::default() }); - database.insert_account_storage(&contract, &Word::from(1), &Word::from(40)); + database.insert_account_storage(&contract, &Word::ONE, &Word::from(40)); let mut evm = TestEvm::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -90,7 +87,7 @@ fn evm_runs_transactions_against_initial_state() { ); run_tx(&mut evm, contract, [op::PUSH1, 0x07, op::PUSH1, 0x01, op::SSTORE, op::STOP]); - assert_eq!(evm.state.storage_slot(&contract, Word::from(1)).unwrap().current(), Word::from(7)); + assert_eq!(evm.state.storage_slot(&contract, Word::ONE).unwrap().current(), Word::from(7)); assert_eq!(evm.state.storage_slot(&contract, Word::from(2)).unwrap().current(), Word::from(42)); } @@ -107,7 +104,7 @@ fn evm_propagates_child_sstore_negative_refund() { &contract, AccountInfo { nonce: 1, code: Some(legacy_bytecode(child_code)), ..Default::default() }, ); - database.insert_account_storage(&contract, &Word::from(0), &Word::from(5)); + database.insert_account_storage(&contract, &Word::ZERO, &Word::from(5)); let mut evm = TestEvm::new( SpecId::LONDON, BlockEnvExt::default(), @@ -136,15 +133,15 @@ fn evm_propagates_child_sstore_negative_refund() { code: legacy_bytecode(parent_code), ..Default::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); - assert!(result.stop.is_success()); + assert!(result.is_success()); assert_eq!(result.gas.refunded(), 0); } #[test] fn evm_reports_invalid_transaction_execution() { - let contract = Address::from([0x33; 20]); + let contract = Address::repeat_byte(0x33); let mut evm = TestEvm::new( SpecId::OSAKA, BlockEnvExt::default(), @@ -159,25 +156,17 @@ fn evm_reports_invalid_transaction_execution() { code: legacy_bytecode([op::PUSH1, 0x01, op::SSTORE]), ..Default::default() }; - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut message).unwrap(); + let result = Host::execute_message(&mut evm, &mut message).unwrap(); assert_eq!(result.stop, InstrStop::StackUnderflow); } #[test] fn eip8037_sibling_refill_restores_parent_gas_left() { - use crate::{ - EvmFeatures, ExecutionConfig, Version, - ethereum::{TxEnvelope, ethereum_tx_registry}, - version::GasId, - }; - use alloy_consensus::{TxLegacy, transaction::Recovered}; - use alloy_primitives::{TxKind, U256}; - - let parent = Address::from([0x11; 20]); - let setter = Address::from([0xa1; 20]); - let clearer = Address::from([0xb2; 20]); - let caller = Address::from([0xcc; 20]); + let parent = Address::repeat_byte(0x11); + let setter = Address::repeat_byte(0xa1); + let clearer = Address::repeat_byte(0xb2); + let caller = Address::repeat_byte(0xcc); let mut parent_code = Vec::new(); for target in [setter, clearer] { // DELEGATECALL(gas, target, 0, 0, 0, 0). @@ -240,19 +229,19 @@ fn eip8037_sibling_refill_restores_parent_gas_left() { let baseline = run(false); let result = run(true); - assert!(baseline.status); - assert!(result.status); - assert_eq!(result.state_gas_spent, 0); + assert!(baseline.is_success()); + assert!(result.is_success()); + assert_eq!(result.state_gas_spent(), 0); assert_eq!(result.output.len(), 32); assert_eq!(result.output, baseline.output); } #[test] fn eip2780_authorization_charges_no_state_gas_without_eip8037() { - let caller = Address::from([0xbb; 20]); - let recipient = Address::from([0xaa; 20]); - let authority = Address::from([0xcc; 20]); - let delegate = Address::from([0xdd; 20]); + let caller = Address::repeat_byte(0xbb); + let recipient = Address::repeat_byte(0xaa); + let authority = Address::repeat_byte(0xcc); + let delegate = Address::repeat_byte(0xdd); let mut database = InMemoryDB::default(); database.insert_account_info( @@ -274,23 +263,35 @@ fn eip2780_authorization_charges_no_state_gas_without_eip8037() { let gas_limit = 12_000 + 3_000 + 7_816 + 9_000; let tx = Recovered::new_unchecked( TxEnvelope::Eip7702(LazyTxEip7702::from_cached_recovered_authorizations( - TxEip7702 { chain_id, to: recipient, gas_limit, ..Default::default() }, - vec![RecoveredAuthorization::new_unchecked( - Authorization { chain_id: U256::ZERO, address: delegate, nonce: 0 }, - RecoveredAuthority::Valid(authority), - )], + TxEip7702 { + chain_id, + to: recipient, + gas_limit, + authorization_list: vec![unsigned(Authorization { + chain_id: U256::ZERO, + address: delegate, + nonce: 0, + })], + ..Default::default() + }, + vec![Some(authority)], )), caller, ); let result = evm.transact(&tx).unwrap().detach(); - assert!(result.result.status); - assert_eq!(result.result.state_gas_spent, 0); + assert!(result.result.is_success()); + assert_eq!(result.result.state_gas_spent(), 0); let authority = result.pending_state.account_info(&authority).unwrap(); assert_eq!(authority.nonce, 1); assert_eq!(authority.code_hash, Bytecode::new_eip7702(delegate).hash_slow()); } +/// Wraps an authorization with a placeholder signature, for use with a cached authority. +fn unsigned(authorization: Authorization) -> SignedAuthorization { + SignedAuthorization::new_unchecked(authorization, 0, U256::ZERO, U256::ZERO) +} + fn absent_authority_evm(spec: SpecId, touched: Address) -> TestEvm { let mut evm = TestEvm::new( spec, @@ -303,12 +304,12 @@ fn absent_authority_evm(spec: SpecId, touched: Address) -> TestEvm { evm } -fn auth_for(authority: Address) -> LazyAuthorization { - RecoveredAuthorization::new_unchecked( - Authorization { chain_id: U256::ZERO, address: Address::with_last_byte(0xdd), nonce: 0 }, - RecoveredAuthority::Valid(authority), - ) - .into() +fn auth_for(authority: Address) -> CustomAuthorization { + CustomAuthorization { + nonce: 0, + address: Address::with_last_byte(0xdd), + authority: Some(authority), + } } #[test] @@ -318,13 +319,100 @@ fn apply_auth_list_refunds_touched_absent_authority() { let mut evm = absent_authority_evm(SpecId::PRAGUE, touched); let mut refunds = AuthRefunds::new(evm.version()); - apply_auth_list(&mut evm, 1, &[auth_for(touched), auth_for(untouched)], &mut refunds).unwrap(); + apply_auth_list(&mut evm, 1, [auth_for(touched), auth_for(untouched)], &mut refunds).unwrap(); // An authority is refunded unless it is absent and untouched. let per_auth = evm.version().gas_params.get(GasId::TxEip7702AuthRefund); assert_eq!(refunds.execution_refund, per_auth); } +#[rstest::rstest] +#[case::legacy(0)] +#[case::eip2930(1)] +#[case::eip1559(2)] +fn top_level_create_uses_sender_state_nonce( + #[case] tx_type: u8, + #[values(false, true)] occupied_first_address: bool, + #[values((false, 0), (false, 9), (true, 5))] nonce_check_and_tx_nonce: (bool, u64), +) { + let (nonce_check, nonce) = nonce_check_and_tx_nonce; + let caller = Address::from([0xbb; 20]); + let destination = caller.create(5); + let first_address = caller.create(0); + let code = legacy_bytecode([op::STOP]); + let mut database = InMemoryDB::default(); + database.insert_account_info( + &caller, + AccountInfo { nonce: 5, balance: U256::from(u64::MAX), ..Default::default() }, + ); + if occupied_first_address { + database.insert_account_info( + &first_address, + AccountInfo::default().with_nonce(1).with_code(code.clone()), + ); + } + let mut version = Version::new(SpecId::OSAKA); + version.features.set(EvmFeatures::NONCE_CHECK, nonce_check); + let chain_id = version.chain_id; + let mut evm = TestEvm::new_with_execution_config( + ExecutionConfig::for_spec_and_version(SpecId::OSAKA, version), + SpecId::OSAKA, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::OSAKA), + database, + Precompiles::base(SpecId::OSAKA), + ); + // Return one zero byte as deployed STOP code. + let input = [op::PUSH1, 1, op::PUSH1, 0, op::RETURN].into(); + let gas_limit = 100_000; + let value = U256::from(7); + let tx = match tx_type { + 0 => TxEnvelope::Legacy(TxLegacy { + nonce, + gas_limit, + value, + input, + to: TxKind::Create, + ..Default::default() + }), + 1 => TxEnvelope::Eip2930(TxEip2930 { + chain_id, + nonce, + gas_limit, + value, + input, + to: TxKind::Create, + ..Default::default() + }), + 2 => TxEnvelope::Eip1559(TxEip1559 { + chain_id, + nonce, + gas_limit, + value, + input, + to: TxKind::Create, + ..Default::default() + }), + _ => unreachable!(), + }; + let result = evm.transact(&Recovered::new_unchecked(tx, caller)).unwrap().commit(); + assert!(result.status, "{result:?}"); + assert_eq!(result.created_address, Some(destination)); + let created = evm.state.account_info_untracked(&destination).unwrap().unwrap(); + assert_eq!(created.nonce, 1); + assert_eq!(created.balance, value); + assert_eq!(created.code_hash, code.hash_slow()); + assert_eq!(evm.state.account(&caller).unwrap().nonce(), 6); + let first = evm.state.account(&first_address).unwrap(); + if occupied_first_address { + assert_eq!(first.nonce(), 1); + assert_eq!(first.code_hash(), code.hash_slow()); + assert_eq!(first.balance(), U256::ZERO); + } else { + assert!(first.get().is_none()); + } +} + #[test] fn eip2780_touched_absent_authority_pays_no_new_account_state_gas() { let authority = Address::with_last_byte(0xa1); @@ -338,7 +426,7 @@ fn eip2780_touched_absent_authority_pays_no_new_account_state_gas() { Address::with_last_byte(0xc0), U256::ZERO, ); - assert!(!apply_auth_list(&mut evm, 1, &[auth_for(authority)], &mut charges).unwrap()); + assert!(!apply_auth_list(&mut evm, 1, [auth_for(authority)], &mut charges).unwrap()); (gas.state_gas_spent(), evm.version().gas_params.new_account_state_gas()) }; @@ -391,11 +479,6 @@ fn log_collection_tx() -> RecoveredTxEnvelope { ) } -/// Appends code that returns the word at the top of the stack. -fn return_top_word(code: &mut Vec) { - code.extend([op::PUSH0, op::MSTORE, op::PUSH1, 32, op::PUSH0, op::RETURN]); -} - #[test] fn log_collection_off_keeps_log_semantics() { // LOG4 over memory that is already allocated, then LOG0 past it, which grows memory to four @@ -410,7 +493,7 @@ fn log_collection_off_keeps_log_semantics() { return_top_word(&mut logs); let mut memory_oog = Vec::new(); - push_all(&mut memory_oog, [Word::from(1), Word::MAX]); + push_all(&mut memory_oog, [Word::ONE, Word::MAX]); memory_oog.push(op::LOG0); let mut underflow = Vec::new(); @@ -439,7 +522,7 @@ fn log_collection_off_keeps_log_semantics() { let mut on = run(true); let off = run(false); - assert_eq!(on.status, status, "{name}"); + assert_eq!(on.is_success(), status, "{name}"); if let Some(output) = output { assert_eq!(on.output[..], output.to_be_bytes::<32>(), "{name}"); } @@ -469,7 +552,7 @@ fn log_collection_off_still_inspects_logs() { let inspected = evm.transact(&log_collection_tx()).unwrap().discard(); let inspector = evm.clear_inspector_as::().unwrap(); - assert!(inspected.status); + assert!(inspected.is_success()); assert!(inspected.logs.is_empty()); assert_eq!( inspector.0.iter().map(|log| log.address).collect::>(), @@ -557,15 +640,14 @@ fn precompile_dispatch_uses_original_callee( caller, 0, TxKind::Call(target), - &hex!("01020304").into(), + &bytes!("01020304"), U256::ZERO, &mut gas, ) - .unwrap() .unwrap(); - let result = Host::execute_message(&mut evm, &TxEnvExt::default(), &mut frame.message).unwrap(); + let result = Host::execute_message(&mut evm, &mut frame.message).unwrap(); - assert!(result.stop.is_success(), "{:?}", result.stop); + assert!(result.is_success(), "{:?}", result.stop); assert_eq!(result.output.as_ref(), expected_output); // Geth's Prague gas usage includes delegation resolution even for precompile callees. let expected_gas = match opcode { @@ -634,7 +716,7 @@ fn apply_auth_list_accepts_custom_filtered_authorizations() { let oog = apply_auth_list( &mut evm, 1, - list.iter().filter(|auth| auth.authority != Some(skipped)), + list.into_iter().filter(|auth| auth.authority != Some(skipped)), &mut refunds, ) .unwrap(); diff --git a/crates/evm2/src/utils.rs b/crates/evm2/src/utils.rs index 6af0afab9..6e2b936b4 100644 --- a/crates/evm2/src/utils.rs +++ b/crates/evm2/src/utils.rs @@ -2,7 +2,7 @@ use crate::interpreter::{InstrStop, Result, Word}; use alloc::{borrow::Cow, vec}; -use alloy_primitives::{Address, B256, Bytes, b256}; +use alloy_primitives::{Address, B256, Bytes}; use core::hint::cold_path; /// Returns the number of EVM words needed for `len` bytes. @@ -14,7 +14,7 @@ pub const fn num_words(len: usize) -> usize { /// Converts an address to an EVM word. #[inline] pub fn address_to_word(address: &Address) -> Word { - Word::from_be_slice(address.as_slice()) + address.into_word().into() } /// Converts a 256-bit hash to an EVM word. @@ -27,7 +27,7 @@ pub const fn b256_to_word(value: B256) -> Word { /// /// This trait is sealed and only implemented for [`Word`]. pub trait WordExt: sealed::Sealed { - /// Writes `value` with a single 32-byte store. + /// Writes `value` with one 32-byte store, or two 16-byte stores without AVX. /// /// With `*self = value`, LLVM writes a word held in general-purpose registers as four 8-byte /// stores. The next instruction often reads the word back with one 32-byte load, for example @@ -39,17 +39,20 @@ pub trait WordExt: sealed::Sealed { /// copied from memory already move with one 32-byte load and store, so plain assignment is /// better for them; this method would route them through general-purpose registers. /// - /// Without AVX this is the same as `*self = value`. + /// Without AVX on x86_64 this is two 16-byte stores, which a pair of 16-byte loads can still + /// forward from. On other targets it is the same as `*self = value`. fn write(&mut self, value: Word); - /// Writes `value`, zero-extended to a word, with a single 32-byte store. + /// Writes `value`, zero-extended to a word, with one 32-byte store, or two 16-byte stores + /// without AVX. /// /// With `*self = Word::from(value)`, LLVM stores the value and the three zero limbs separately, /// so a later 32-byte load of the word waits for those stores to reach the cache, as described /// in [`write`](Self::write). Use this for results that fit in a `u64`, such as comparison /// flags, sizes and offsets. /// - /// Without AVX this is the same as `*self = Word::from(value)`. + /// Without AVX on x86_64 this is two 16-byte stores. On other targets it is the same as + /// `*self = Word::from(value)`. fn write_u64(&mut self, value: u64); /// Converts the word to an address, keeping its low 20 bytes. @@ -132,6 +135,14 @@ pub(crate) unsafe fn write_word(dst: *mut Word, value: Word) { options(nostack, preserves_flags), ); }, + // LLVM splits the word store into 8- and 16-byte stores, which the 16-byte loads that read + // it back cannot forward from, so store two 16-byte halves. + all(target_arch = "x86_64", not(miri)) => unsafe { + use core::arch::x86_64::{__m128i, _mm_set_epi64x, _mm_storeu_si128}; + let [l0, l1, l2, l3] = value.into_limbs().map(|l| l as i64); + _mm_storeu_si128(dst.cast(), _mm_set_epi64x(l1, l0)); + _mm_storeu_si128(dst.cast::<__m128i>().add(1), _mm_set_epi64x(l3, l2)); + }, _ => unsafe { dst.write(value) }, } } @@ -158,6 +169,15 @@ pub(crate) unsafe fn write_u64(dst: *mut Word, value: u64) { options(nostack, preserves_flags), ); }, + // LLVM splits the word store into 8- and 16-byte stores, which the 16-byte loads that read + // it back cannot forward from, so store two 16-byte halves. + all(target_arch = "x86_64", not(miri)) => unsafe { + use core::arch::x86_64::{ + __m128i, _mm_cvtsi64_si128, _mm_setzero_si128, _mm_storeu_si128, + }; + _mm_storeu_si128(dst.cast(), _mm_cvtsi64_si128(value as i64)); + _mm_storeu_si128(dst.cast::<__m128i>().add(1), _mm_setzero_si128()); + }, _ => unsafe { dst.write(Word::from(value)) }, } } @@ -259,7 +279,7 @@ pub fn left_pad_vec_be(data: &[u8], len: usize) -> Cow<'_, [u8]> { /// This is optimized to not allocate at runtime by using 2 static arrays. #[inline] pub const fn bool_to_bytes32(value: bool) -> Bytes { - Bytes::from_static(&bool_to_b256(value).0) + Bytes::from_static(bool_to_b256(value).as_slice()) } /// Converts a boolean to a left-padded [`B256`] value. @@ -267,10 +287,8 @@ pub const fn bool_to_bytes32(value: bool) -> Bytes { /// This is optimized to not allocate at runtime by using 2 static arrays. #[inline] const fn bool_to_b256(value: bool) -> &'static B256 { - const TRUE: &B256 = - &b256!("0x0000000000000000000000000000000000000000000000000000000000000001"); - const FALSE: &B256 = - &b256!("0x0000000000000000000000000000000000000000000000000000000000000000"); + const TRUE: &B256 = &B256::with_last_byte(1); + const FALSE: &B256 = &B256::ZERO; if value { TRUE } else { FALSE } } diff --git a/crates/evm2/src/version/gas_params.rs b/crates/evm2/src/version/gas_params.rs index 495800403..b33c20a93 100644 --- a/crates/evm2/src/version/gas_params.rs +++ b/crates/evm2/src/version/gas_params.rs @@ -211,7 +211,7 @@ impl Index for GasParams { impl IndexMut for GasParams { #[inline] fn index_mut(&mut self, id: GasId) -> &mut Self::Output { - &mut self.table[id.as_usize()] + self.get_mut(id) } } @@ -258,28 +258,34 @@ impl GasParams { self.get(GasId::ExpByte).saturating_mul(power.bit_len().div_ceil(8) as u64) } + /// Calculates the per-word gas `id` for `len` bytes. + #[inline] + const fn per_word_cost(&self, id: GasId, len: usize) -> u64 { + self.get(id).saturating_mul(num_words(len) as u64) + } + /// Calculates copy gas for `len` bytes. #[inline] pub const fn copy_cost(&self, len: usize) -> u64 { - self.get(GasId::CopyPerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::CopyPerWord, len) } /// Calculates `EXTCODECOPY` copy gas for `len` bytes. #[inline] pub const fn extcodecopy_cost(&self, len: usize) -> u64 { - self.get(GasId::ExtcodecopyPerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::ExtcodecopyPerWord, len) } /// Calculates `MCOPY` copy gas for `len` bytes. #[inline] pub const fn mcopy_cost(&self, len: usize) -> u64 { - self.get(GasId::McopyPerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::McopyPerWord, len) } /// Calculates `KECCAK256` word gas for `len` bytes. #[inline] pub const fn keccak256_word_cost(&self, len: usize) -> u64 { - self.get(GasId::Keccak256PerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::Keccak256PerWord, len) } /// Calculates dynamic `LOG` gas. @@ -293,14 +299,13 @@ impl GasParams { /// Calculates initcode word gas for `len` bytes. #[inline] pub const fn initcode_cost(&self, len: usize) -> u64 { - self.get(GasId::InitcodePerWord).saturating_mul(num_words(len) as u64) + self.per_word_cost(GasId::InitcodePerWord, len) } /// Calculates dynamic `CREATE2` gas for `len` bytes. #[inline] pub const fn create2_cost(&self, len: usize) -> u64 { - self.get(GasId::Create) - .saturating_add(self.get(GasId::Keccak256PerWord).saturating_mul(num_words(len) as u64)) + self.get(GasId::Create).saturating_add(self.keccak256_word_cost(len)) } /// Returns `CALL` stipend reduction. @@ -427,8 +432,7 @@ impl GasParams { /// Returns `SELFDESTRUCT` cold account cost. #[inline] pub const fn selfdestruct_cold_cost(&self) -> u64 { - self.get(GasId::ColdAccountAdditionalCost) - .saturating_add(self.get(GasId::WarmStorageReadCost)) + self.cold_account_additional_cost().saturating_add(self.get(GasId::WarmStorageReadCost)) } /// Calculates `SELFDESTRUCT` dynamic gas. @@ -543,7 +547,7 @@ mod tests { fn gas_id_roundtrips_values() { assert_eq!(GasId::from_usize(0), Some(GasId::ExpByte)); assert_eq!(GasId::ExpByte.as_usize(), 0); - assert_eq!(GasId::from_usize(GasId::MAX as usize), Some(GasId::Custom7)); + assert_eq!(GasId::from_usize(GasId::MAX.as_usize()), Some(GasId::Custom7)); assert_eq!(GasId::from_usize(GasId::COUNT), None); } @@ -579,7 +583,7 @@ mod tests { let berlin = gas_params(SpecId::BERLIN); assert_eq!(berlin.get(GasId::SstoreStatic), 100); - assert_eq!(berlin.get(GasId::ColdAccountAdditionalCost), 2500); + assert_eq!(berlin.cold_account_additional_cost(), 2500); assert_eq!(berlin.get(GasId::ColdStorageCost), 2100); assert_eq!(berlin.get(GasId::MaxRefundQuotient), 2); @@ -607,7 +611,7 @@ mod tests { assert_eq!(amsterdam.get(GasId::Create), 12_000); assert_eq!(amsterdam.get(GasId::WarmStorageReadCost), 100); // unchanged assert_eq!(amsterdam.get(GasId::ColdStorageCost), 2000); - assert_eq!(amsterdam.get(GasId::ColdAccountAdditionalCost), 2900); + assert_eq!(amsterdam.cold_account_additional_cost(), 2900); assert_eq!(amsterdam.get(GasId::TransferValueCost), 11_300); // CALL folds the account-write surcharge into CALL_VALUE, so a new target // adds no extra execution gas (only NEW_ACCOUNT state gas). diff --git a/crates/evm2/src/version/mod.rs b/crates/evm2/src/version/mod.rs index f194210af..dbd1156fe 100644 --- a/crates/evm2/src/version/mod.rs +++ b/crates/evm2/src/version/mod.rs @@ -3,8 +3,8 @@ use crate::{ EvmConfig, EvmTypesHost, SpecId, constants::{ - CALL_DEPTH_LIMIT, MAX_CODE_SIZE, MAX_CODE_SIZE_AMSTERDAM, MAX_INITCODE_SIZE, - MAX_INITCODE_SIZE_AMSTERDAM, + MAX_CODE_SIZE, MAX_CODE_SIZE_AMSTERDAM, MAX_INITCODE_SIZE, MAX_INITCODE_SIZE_AMSTERDAM, + MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, }, interpreter::{instructions as instr, op}, }; @@ -37,7 +37,17 @@ pub struct Version { pub chain_id: u64, /// Transaction gas limit cap. pub tx_gas_limit_cap: u64, + /// EIP-8037 total transaction gas limit cap. + /// + /// Bounds `tx.gas` when [`EvmFeatures::EIP8037`] is active. `u64::MAX` disables the cap. The + /// default is [`MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM`] (`2^32 - 1`). Without EIP-8037 this field + /// is unused and [`tx_gas_limit_cap`](Self::tx_gas_limit_cap) bounds `tx.gas` instead. + pub tx_total_gas_cap: u64, /// Hard memory limit in bytes. + /// + /// Frame output ranges are `u32`, so + /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) clamps a larger + /// limit to [`u32::MAX`], and memory stops growing there. pub memory_limit: u64, /// Maximum deployed contract bytecode size. pub max_code_size: usize, @@ -45,14 +55,6 @@ pub struct Version { pub max_initcode_size: usize, /// Maximum blobs allowed in a single blob transaction. pub max_blobs_per_tx: usize, - /// Inspector-visible depth of the transaction root. Defaults to zero. - /// - /// Nested transaction roots can retain their outer call depth while receiving a fresh - /// [`CALL_DEPTH_LIMIT`]-descendant budget. This must leave room in `u16` for that budget - /// and the attempted-too-deep message: at most `u16::MAX - CALL_DEPTH_LIMIT - 1` (64510). - /// Installing a version with a larger value through - /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) panics. - pub initial_call_depth: u16, #[doc(hidden)] // Not public API. Please use an existing constructor. pub _non_exhaustive: (), @@ -86,7 +88,9 @@ impl Version { /// /// Without EIP-8037, [`tx_gas_limit_cap`](Self::tx_gas_limit_cap) (EIP-7825) bounds `tx.gas` /// itself, which also bounds the execution gas. With EIP-8037, `tx.gas` may exceed it because - /// the excess funds the state-gas reservoir, so it only bounds the required execution gas. + /// the excess funds the state-gas reservoir, so it only bounds the required execution gas, and + /// `tx.gas` is bounded by [`tx_total_gas_cap`](Self::tx_total_gas_cap) instead. `u64::MAX` + /// disables that total cap; the default is [`MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM`]. /// /// The Ethereum transaction handlers enforce these caps through /// [`validate_tx_gas_limit_cap`](crate::ethereum::validate_tx_gas_limit_cap) and @@ -96,19 +100,24 @@ impl Version { #[inline] pub const fn tx_gas_caps(&self) -> (u64, u64) { if self.feature(EvmFeatures::EIP8037) { - (u64::MAX, self.tx_gas_limit_cap) + (self.tx_total_gas_cap, self.tx_gas_limit_cap) } else { (self.tx_gas_limit_cap, u64::MAX) } } - /// Returns the deepest permitted inspector-visible call depth. + /// Panics if a field holds a value the interpreter cannot run with. /// - /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) validates - /// [`Self::initial_call_depth`], so this cannot overflow for an installed version. + /// [`ExecutionConfig::with_version`](crate::ExecutionConfig::with_version) calls this when it + /// installs a version, so instructions can rely on these invariants: + /// + /// - [`GasId::MaxRefundQuotient`] is non-zero. #[inline] - pub const fn max_call_depth(&self) -> u16 { - self.initial_call_depth + CALL_DEPTH_LIMIT + pub const fn assert_valid(&self) { + assert!( + self.gas_params.get(GasId::MaxRefundQuotient) != 0, + "max refund quotient must be non-zero" + ); } } @@ -144,11 +153,11 @@ static BASE_VERSIONS: [Version; SpecId::COUNT] = { features: EvmFeatures::empty(), chain_id: DEFAULT_CHAIN_ID, tx_gas_limit_cap: u64::MAX, + tx_total_gas_cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, memory_limit: DEFAULT_MEMORY_LIMIT, max_code_size: MAX_CODE_SIZE, max_initcode_size: MAX_INITCODE_SIZE, max_blobs_per_tx: MAX_BLOBS_PER_BLOCK_DENCUN, - initial_call_depth: 0, _non_exhaustive: (), } }; SpecId::COUNT]; @@ -160,11 +169,11 @@ static BASE_VERSIONS: [Version; SpecId::COUNT] = { features: base_features(spec_id), chain_id: DEFAULT_CHAIN_ID, tx_gas_limit_cap: base_tx_gas_limit_cap(spec_id), + tx_total_gas_cap: MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, memory_limit: DEFAULT_MEMORY_LIMIT, max_code_size: base_max_code_size(spec_id), max_initcode_size: base_max_initcode_size(spec_id), max_blobs_per_tx: base_max_blobs_per_tx(spec_id), - initial_call_depth: 0, _non_exhaustive: (), }; i += 1; @@ -320,6 +329,7 @@ mod tests { assert!(amsterdam.feature(EvmFeatures::EIP8246)); assert_eq!(amsterdam.chain_id, DEFAULT_CHAIN_ID); assert_eq!(amsterdam.tx_gas_limit_cap, MAX_TX_GAS_LIMIT_OSAKA); + assert_eq!(amsterdam.tx_total_gas_cap, MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM); assert_eq!(amsterdam.memory_limit, DEFAULT_MEMORY_LIMIT); assert_eq!(amsterdam.max_code_size, MAX_CODE_SIZE_AMSTERDAM); assert_eq!(amsterdam.max_initcode_size, MAX_INITCODE_SIZE_AMSTERDAM); @@ -389,7 +399,10 @@ mod tests { assert_eq!(osaka.tx_gas_caps(), (MAX_TX_GAS_LIMIT_OSAKA, u64::MAX)); let amsterdam = Version::base(SpecId::AMSTERDAM); - assert_eq!(amsterdam.tx_gas_caps(), (u64::MAX, MAX_TX_GAS_LIMIT_OSAKA)); + assert_eq!( + amsterdam.tx_gas_caps(), + (MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, MAX_TX_GAS_LIMIT_OSAKA) + ); let mut amsterdam_without_eip8037 = Version::new(SpecId::AMSTERDAM); amsterdam_without_eip8037.features.remove(EvmFeatures::EIP8037); @@ -397,10 +410,39 @@ mod tests { } #[test] - #[should_panic(expected = "initial call depth must leave room")] - fn execution_config_rejects_unrepresentable_call_budget() { + fn tx_total_gas_cap_is_configurable() { + // Without EIP-8037 the total cap is unused, so a disabled value changes nothing. + let mut osaka = Version::new(SpecId::OSAKA); + osaka.tx_total_gas_cap = u64::MAX; + assert_eq!(osaka.tx_gas_caps(), (MAX_TX_GAS_LIMIT_OSAKA, u64::MAX)); + + let mut amsterdam = Version::new(SpecId::AMSTERDAM); + assert_eq!( + amsterdam.tx_gas_caps(), + (MAX_TX_TOTAL_GAS_LIMIT_AMSTERDAM, MAX_TX_GAS_LIMIT_OSAKA) + ); + + amsterdam.tx_total_gas_cap = 10_000_000_000; + assert_eq!(amsterdam.tx_gas_caps(), (10_000_000_000, MAX_TX_GAS_LIMIT_OSAKA)); + + amsterdam.tx_total_gas_cap = u64::MAX; + assert_eq!(amsterdam.tx_gas_caps(), (u64::MAX, MAX_TX_GAS_LIMIT_OSAKA)); + } + + #[test] + fn execution_config_clamps_memory_limit_to_u32() { + let mut version = Version::new(SpecId::CANCUN); + version.memory_limit = u64::MAX; + let config = + crate::ExecutionConfig::::for_spec_and_version(SpecId::CANCUN, version); + assert_eq!(config.version().memory_limit, u32::MAX as u64); + } + + #[test] + #[should_panic(expected = "max refund quotient must be non-zero")] + fn execution_config_rejects_zero_max_refund_quotient() { let mut version = Version::new(SpecId::CANCUN); - version.initial_call_depth = u16::MAX - CALL_DEPTH_LIMIT; + version.gas_params.set(GasId::MaxRefundQuotient, 0); crate::ExecutionConfig::::for_spec_and_version(SpecId::CANCUN, version); } } diff --git a/crates/evm2/src/version/opcode_config.rs b/crates/evm2/src/version/opcode_config.rs index 4b10b7697..26eebaff7 100644 --- a/crates/evm2/src/version/opcode_config.rs +++ b/crates/evm2/src/version/opcode_config.rs @@ -2,7 +2,7 @@ use crate::{ BaseEvmConfig, EvmConfig, EvmTypesHost, SpecId, interpreter::{ instructions as instr, op, - private::{Instruction, InstructionImplFn}, + private::{Instruction, InstructionFlags, InstructionImplFn}, }, }; use core::fmt; @@ -26,8 +26,7 @@ pub struct OpcodeConfig { pub(crate) struct InstructionInfo { pub(crate) instr: InstructionImplFn, - #[allow(dead_code)] - pub(crate) dynamic_gas: bool, + pub(crate) flags: InstructionFlags, } impl fmt::Debug for OpcodeConfig { @@ -162,7 +161,7 @@ impl OpcodeConfig { assert!(opcode != op::INVALID, "INVALID opcode cannot be overridden"); self.set_static_gas(opcode, gas); self.instruction_sets[opcode as usize] += 1; - if self.instruction_impls.set(opcode, I::execute, I::DYNAMIC_GAS) { + if self.instruction_impls.set(opcode, I::execute, I::FLAGS) { self.bump_revision(opcode); } } @@ -242,24 +241,25 @@ impl StaticGasTable { struct InstructionImplTable { instrs: [Option>; 256], - dynamic_gas: [bool; 256], + flags: [InstructionFlags; 256], } impl InstructionImplTable { /// Creates an empty instruction implementation table. #[inline] const fn empty() -> Self { - Self { instrs: [None; 256], dynamic_gas: [false; 256] } + Self { instrs: [None; 256], flags: [InstructionFlags::empty(); 256] } } /// Returns the instruction implementation for `opcode`. #[inline(always)] const fn get(&self, opcode: u8) -> InstructionInfo { match self.instrs[opcode as usize] { - Some(instr) => { - InstructionInfo { instr, dynamic_gas: self.dynamic_gas[opcode as usize] } - } - None => InstructionInfo { instr: instr::invalid::::execute, dynamic_gas: false }, + Some(instr) => InstructionInfo { instr, flags: self.flags[opcode as usize] }, + None => InstructionInfo { + instr: instr::invalid::::execute, + flags: InstructionFlags::empty(), + }, } } @@ -271,10 +271,15 @@ impl InstructionImplTable { /// Sets the instruction implementation for `opcode`. #[inline(always)] - const fn set(&mut self, opcode: u8, instr: InstructionImplFn, dynamic_gas: bool) -> bool { + const fn set( + &mut self, + opcode: u8, + instr: InstructionImplFn, + flags: InstructionFlags, + ) -> bool { let index = opcode as usize; self.instrs[index] = Some(instr); - self.dynamic_gas[index] = dynamic_gas; + self.flags[index] = flags; true } } diff --git a/crates/evm2/tests/eip7702_recovery_crypto.rs b/crates/evm2/tests/eip7702_recovery_crypto.rs index 066820f7e..778eb8a87 100644 --- a/crates/evm2/tests/eip7702_recovery_crypto.rs +++ b/crates/evm2/tests/eip7702_recovery_crypto.rs @@ -1,4 +1,4 @@ -//! Authority recovery must not go through, or initialize, the global crypto provider. +//! Authority recovery must go through the installed crypto provider. //! //! The provider is a process-wide `OnceLock`, so this lives in its own test binary. @@ -27,24 +27,19 @@ impl Crypto for BogusCrypto { } #[test] -fn recovery_ignores_crypto_provider() { +fn recovery_uses_installed_crypto_provider() { let key = SigningKey::from_slice(&[0x11; 32]).unwrap(); - let signer = - Address::from_raw_public_key(&key.verifying_key().to_sec1_point(false).as_bytes()[1..]); - let inner = Authorization { chain_id: U256::from(1), address: Address::ZERO, nonce: 7 }; + let inner = Authorization { chain_id: U256::ONE, address: Address::ZERO, nonce: 7 }; let (sig, recid) = key.sign_prehash_recoverable(inner.signature_hash().as_slice()); let r = U256::from_be_slice(&sig.r().to_bytes()); let s = U256::from_be_slice(&sig.s().to_bytes()); let authorization = SignedAuthorization::new_unchecked(inner, recid.to_byte(), r, s); let tx = TxEip7702 { authorization_list: vec![authorization.clone()], ..Default::default() }; - assert_eq!(LazyAuthorization::from(authorization.clone()).authority(), Some(signer)); - let recovered = LazyTxEip7702::from_recovered_authorizations(tx.clone()); - assert_eq!(recovered.authorization_list[0].authority(), Some(signer)); + assert!(install_crypto(BogusCrypto)); - assert!(install_crypto(BogusCrypto), "recovery initialized the crypto provider"); - - assert_eq!(LazyAuthorization::from(authorization).authority(), Some(signer)); + let bogus = Address::repeat_byte(0xff); + assert_eq!(LazyAuthorization::Signed(&authorization).authority(), Some(bogus)); let recovered = LazyTxEip7702::from_recovered_authorizations(tx); - assert_eq!(recovered.authorization_list[0].authority(), Some(signer)); + assert_eq!(recovered.authorizations().next().unwrap().authority(), Some(bogus)); } diff --git a/crates/inspectors/Cargo.toml b/crates/inspectors/Cargo.toml index d51592fd2..ce68cba50 100644 --- a/crates/inspectors/Cargo.toml +++ b/crates/inspectors/Cargo.toml @@ -39,6 +39,7 @@ num-bigint = { workspace = true, optional = true } [dev-dependencies] alloy-consensus = { workspace = true, features = ["k256"] } alloy-hardforks.workspace = true +evm2 = { workspace = true, features = ["block"] } snapbox = { workspace = true, features = ["term-svg"] } [features] diff --git a/crates/inspectors/src/access_list.rs b/crates/inspectors/src/access_list.rs index 5b7672198..5298e5cd8 100644 --- a/crates/inspectors/src/access_list.rs +++ b/crates/inspectors/src/access_list.rs @@ -8,6 +8,7 @@ use alloy_primitives::{ use evm2::{ Evm, EvmTypes, Inspector, interpreter::{Interpreter, Message, MessageResult, op}, + utils::WordExt, }; /// An [Inspector] that collects touched accounts and storage slots. @@ -19,6 +20,9 @@ pub struct AccessListInspector { excluded: HashSet
, /// All addresses and touched slots touched_slots: HashMap>, + /// Whether the seeded entries of excluded addresses were removed, which happens once, before + /// the first top-level call. + seeds_pruned: bool, } impl From for AccessListInspector { @@ -36,7 +40,7 @@ impl AccessListInspector { for item in access_list.0 { touched_slots.entry(item.address).or_default().extend(item.storage_keys); } - Self { excluded: Default::default(), touched_slots } + Self { excluded: Default::default(), touched_slots, seeds_pruned: false } } /// Excludes additional addresses from the final access list. @@ -108,16 +112,22 @@ impl AccessListInspector { access_list } - /// Collects addresses which should be excluded from the access list. Must be called before the - /// top-level call. + /// Collects addresses which should be excluded from the access list. Must be called before + /// each top-level call. /// - /// Those include caller, callee and precompiles. + /// Those include caller, callee and precompiles. In a transaction with several top-level + /// calls, such as an account-abstraction batch, entries touched by earlier calls are kept even + /// if a later call targets their address. fn collect_excluded_addresses(&mut self, message: &Message, host: &Evm<'_, T>) { self.excluded.extend( [message.caller, message.destination].into_iter().chain(host.precompiles().addresses()), ); - // Remove seeded excluded entries; SLOAD/SSTORE can re-add slots accessed during execution. - self.touched_slots.retain(|address, _| !self.excluded.contains(address)); + if !self.seeds_pruned { + self.seeds_pruned = true; + // Remove seeded excluded entries; SLOAD/SSTORE can re-add slots accessed during + // execution. + self.touched_slots.retain(|address, _| !self.excluded.contains(address)); + } } } @@ -127,10 +137,7 @@ impl Inspector for AccessListInspector { op::SLOAD | op::SSTORE => { if let Some([slot]) = interp.stack().peekn() { let cur_contract = interp.message().destination; - self.touched_slots - .entry(cur_contract) - .or_default() - .insert(B256::from(slot.to_be_bytes())); + self.touched_slots.entry(cur_contract).or_default().insert(B256::from(slot)); } } op::EXTCODECOPY @@ -139,15 +146,17 @@ impl Inspector for AccessListInspector { | op::BALANCE | op::SELFDESTRUCT => { if let Some([slot]) = interp.stack().peekn() { - let addr = Address::from_word(B256::from(slot.to_be_bytes())); + let addr = slot.to_address(); if !self.excluded.contains(&addr) { self.touched_slots.entry(addr).or_default(); } } } op::DELEGATECALL | op::CALL | op::STATICCALL | op::CALLCODE => { - if let Some([_, slot]) = interp.stack().peekn() { - let addr = Address::from_word(B256::from(slot.to_be_bytes())); + // `go-ethereum` reads the target once five items are on the stack, even if the call + // then underflows. + if let Some([_, slot, _, _, _]) = interp.stack().peekn() { + let addr = slot.to_address(); if !self.excluded.contains(&addr) { self.touched_slots.entry(addr).or_default(); } @@ -163,7 +172,7 @@ impl Inspector for AccessListInspector { message: &mut Message, ) -> Option> { // At the top-level frame, fill the excluded addresses. - if message.depth == interp.version().initial_call_depth { + if message.depth == 0 { self.collect_excluded_addresses(message, interp.host()); } None @@ -175,7 +184,7 @@ impl Inspector for AccessListInspector { message: &mut Message, ) -> Option> { // At the top-level frame, fill the excluded addresses. - if message.depth == interp.version().initial_call_depth { + if message.depth == 0 { self.collect_excluded_addresses(message, interp.host()); } None diff --git a/crates/inspectors/src/opcode.rs b/crates/inspectors/src/opcode.rs index 9b58632c0..b23f7784c 100644 --- a/crates/inspectors/src/opcode.rs +++ b/crates/inspectors/src/opcode.rs @@ -1,17 +1,18 @@ +use crate::tracing::utils::step_cost; use alloc::{string::ToString, vec::Vec}; use alloy_primitives::map::HashMap; use alloy_rpc_types_trace::opcode::OpcodeGas; use evm2::{ EvmTypesHost, Inspector, interpreter::{ - InstrStop, Interpreter, Message, MessageKind, MessageResult, + Interpreter, Message, MessageKind, MessageResult, opcode::{OpCode, op}, }, }; #[derive(Clone, Copy, Debug)] struct PendingOpcodeGas { - opcode: OpCode, + opcode: Option, gas_remaining: u64, child_gas_used: u64, } @@ -71,7 +72,7 @@ impl OpcodeGasInspector { return; }; if let Some(pending) = self.pending_steps.last_mut() - && pending.opcode == opcode + && pending.opcode == Some(opcode) { pending.child_gas_used = pending.child_gas_used.saturating_add(child_gas_used); } @@ -81,44 +82,42 @@ impl OpcodeGasInspector { impl Inspector for OpcodeGasInspector { fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { let opcode_value = interp.opcode(); - if let Some(opcode) = OpCode::new(opcode_value) { + let opcode = OpCode::new(opcode_value); + if let Some(opcode) = opcode { // keep track of opcode counts *self.opcode_counts.entry(opcode).or_default() += 1; - - // keep track of the last opcode executed - self.pending_steps.push(PendingOpcodeGas { - opcode, - gas_remaining: interp.gas().remaining(), - child_gas_used: 0, - }); } + + // Undefined opcodes also receive step_end and must not pop a parent's step. + self.pending_steps.push(PendingOpcodeGas { + opcode, + gas_remaining: interp.gas().remaining(), + child_gas_used: 0, + }); } fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { // update gas usage for the last opcode - if let Some(pending) = self.pending_steps.pop() { - // The sentry rejects SSTORE before charging gas, but out-of-gas hooks already - // see the zeroed frame remainder. - let gas_cost = if interp.result() == Err(InstrStop::ReentrancySentryOOG) { - 0 - } else { - pending - .gas_remaining - .saturating_sub(interp.gas().remaining()) - .saturating_sub(pending.child_gas_used) - }; - let opcode = pending.opcode; + if let Some(pending) = self.pending_steps.pop() + && let Some(opcode) = pending.opcode + { + let gas_cost = step_cost( + interp.result(), + pending.gas_remaining, + pending.gas_remaining.saturating_sub(interp.gas().remaining()), + ) + .saturating_sub(pending.child_gas_used); *self.opcode_gas.entry(opcode).or_default() += gas_cost; } } fn call_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, message: &Message, result: &mut MessageResult, ) { - if message.depth == interp.version().initial_call_depth { + if message.depth == 0 { // skip the root call return; } @@ -139,11 +138,11 @@ impl Inspector for OpcodeGasInspector { fn create_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, message: &Message, result: &mut MessageResult, ) { - if message.depth == interp.version().initial_call_depth { + if message.depth == 0 { // skip the root create return; } @@ -169,7 +168,7 @@ pub fn immediate_size(opcode: u8) -> u8 { mod tests { use super::*; use alloy_primitives::Bytes; - use evm2::{BaseEvmTypes, bytecode::Bytecode, env::TxEnv}; + use evm2::{BaseEvmTypes, bytecode::Bytecode}; #[test] fn test_opcode_counter_inspector() { @@ -177,12 +176,11 @@ mod tests { let opcodes = [op::ADD, op::ADD, op::ADD, op::BYTE]; - let tx_env = TxEnv::::default(); let message = Message:: { code: Bytecode::new_legacy(Bytes::from(opcodes)), ..Message::::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); for _ in &opcodes { opcode_counter.step(&mut interp); @@ -195,12 +193,11 @@ mod tests { let opcodes = [op::PUSH1, op::PUSH1, op::ADD, op::PUSH1, op::SSTORE, op::STOP]; - let tx_env = TxEnv::::default(); let message = Message:: { code: Bytecode::new_legacy(Bytes::from(opcodes)), ..Message::::default() }; - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); for _ in &opcodes { opcode_counter.step(&mut interp); diff --git a/crates/inspectors/src/otterscan.rs b/crates/inspectors/src/otterscan.rs index 5ef56340a..202e1878c 100644 --- a/crates/inspectors/src/otterscan.rs +++ b/crates/inspectors/src/otterscan.rs @@ -4,7 +4,7 @@ use alloc::vec::Vec; use alloy_primitives::{Address, U256}; use alloy_rpc_types_trace::otterscan::{InternalOperation, OperationType}; use evm2::{ - EvmTypesHost, Inspector, + EvmTypesHost, Inspector, InspectorConfig, interpreter::{Interpreter, Message, MessageKind, MessageResult}, }; @@ -32,8 +32,8 @@ impl InternalOperationsInspector { self.operations } - fn on_operation(&mut self, message: &Message, initial_call_depth: u16) { - if message.depth == initial_call_depth { + fn on_operation(&mut self, message: &Message) { + if message.depth == 0 { return; } let r#type = match message.kind { @@ -52,21 +52,25 @@ impl InternalOperationsInspector { } impl Inspector for InternalOperationsInspector { + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::without_steps() + } + fn call( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - self.on_operation::(message, interp.version().initial_call_depth); + self.on_operation::(message); None } fn create( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - self.on_operation::(message, interp.version().initial_call_depth); + self.on_operation::(message); None } diff --git a/crates/inspectors/src/storage.rs b/crates/inspectors/src/storage.rs index c30fdcdd6..2d391610f 100644 --- a/crates/inspectors/src/storage.rs +++ b/crates/inspectors/src/storage.rs @@ -52,7 +52,7 @@ impl Inspector for StorageInspector { && let Some([slot]) = interp.stack().peekn() { let address = interp.message().destination; - let slot = B256::from(slot.to_be_bytes()); + let slot = B256::from(slot); let slot_access_count = self.accessed_slots.entry(address).or_default().entry(slot).or_default(); diff --git a/crates/inspectors/src/tracing/arena.rs b/crates/inspectors/src/tracing/arena.rs index acfb75166..57bbaa543 100644 --- a/crates/inspectors/src/tracing/arena.rs +++ b/crates/inspectors/src/tracing/arena.rs @@ -56,28 +56,30 @@ impl CallTraceArena { /// Pushes a new trace into the arena as a child of `parent`, returning the trace ID /// /// This appends a new trace to the arena, and also inserts a new entry in the node's parent - /// node children set if `attach_to_parent` is `true`. E.g. if calls to precompiles should - /// not be included in the call graph this should be called with [PushTraceKind::PushOnly]. + /// node children set. /// - /// If there is no parent, the trace replaces the root node, regardless of its visible depth. - pub(crate) fn push_trace( - &mut self, - parent: Option, - kind: PushTraceKind, - new_trace: CallTrace, - ) -> usize { - // The entry node, just update it. - let Some(parent) = parent else { - self.arena[0].trace = new_trace; - return 0; + /// A trace without a parent at depth 0 replaces the root node. A trace without a parent at a + /// greater depth is attached to the root node instead, which stays an empty placeholder, as in + /// revm-inspectors: top-level messages that start below an implicit parent, such as the calls + /// of a batch transaction, then share one root. + pub(crate) fn push_trace(&mut self, parent: Option, new_trace: CallTrace) -> usize { + let parent = match parent { + Some(parent) => { + debug_assert_eq!( + self.arena[parent].trace.depth + 1, + new_trace.depth, + "parent must be exactly one level above the new trace" + ); + parent + } + // The entry node, just update it. + None if new_trace.depth == 0 => { + self.arena[0].trace = new_trace; + return 0; + } + None => 0, }; - debug_assert_eq!( - self.arena[parent].trace.depth + 1, - new_trace.depth, - "parent must be exactly one level above the new trace" - ); - let idx = self.arena.len(); self.arena.push(CallTraceNode { parent: Some(parent), @@ -87,30 +89,39 @@ impl CallTraceArena { }); // Also track the child in the parent node. - if kind.is_attach_to_parent() { - let parent = &mut self.arena[parent]; - let trace_location = parent.children.len(); - parent.ordering.push(TraceMemberOrder::Call(trace_location)); - parent.children.push(idx); - } + let parent = &mut self.arena[parent]; + let trace_location = parent.children.len(); + parent.ordering.push(TraceMemberOrder::Call(trace_location)); + parent.children.push(idx); idx } } -/// How to push a trace into the arena -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum PushTraceKind { - /// This will _only_ push the trace into the arena. - PushOnly, - /// This will push the trace into the arena, and also insert a new entry in the node's parent - /// node children set. - PushAndAttachToParent, -} +#[cfg(test)] +mod tests { + use super::*; -impl PushTraceKind { - #[inline] - const fn is_attach_to_parent(&self) -> bool { - matches!(self, Self::PushAndAttachToParent) + #[test] + fn parentless_traces_above_depth_zero_share_an_empty_root() { + let mut arena = CallTraceArena::default(); + let first = arena.push_trace(None, CallTrace { depth: 1, ..Default::default() }); + arena.push_trace(Some(first), CallTrace { depth: 2, ..Default::default() }); + let second = arena.push_trace(None, CallTrace { depth: 1, ..Default::default() }); + + let nodes = arena.nodes(); + assert_eq!(nodes[0].trace, CallTrace::default()); + assert_eq!(nodes[0].children, [first, second]); + assert_eq!(nodes[first].parent, Some(0)); + assert_eq!(nodes[second].parent, Some(0)); + } + + #[test] + fn parentless_trace_at_depth_zero_replaces_the_root() { + let mut arena = CallTraceArena::default(); + let address = Address::repeat_byte(0x11); + assert_eq!(arena.push_trace(None, CallTrace { address, ..Default::default() }), 0); + assert_eq!(arena.nodes().len(), 1); + assert_eq!(arena.nodes()[0].trace.address, address); } } diff --git a/crates/inspectors/src/tracing/builder/geth.rs b/crates/inspectors/src/tracing/builder/geth.rs index 2484fd650..4fdeaf80e 100644 --- a/crates/inspectors/src/tracing/builder/geth.rs +++ b/crates/inspectors/src/tracing/builder/geth.rs @@ -1,32 +1,89 @@ //! Geth trace builder use crate::tracing::{ - final_refunded, - tx_state::TxState, - types::{CallKind, CallTraceNode, CallTraceStepStackItem}, + types::{CallKind, CallTraceNode, CallTraceStepStackItem, StorageChange}, utils::load_account_code, }; -use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, format, vec, vec::Vec}; +use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, vec, vec::Vec}; use alloy_primitives::{ Address, B256, Bytes, U256, - map::{Entry, HashMap}, + map::{AddressMap, AddressSet, HashMap}, }; use alloy_rpc_types_trace::geth::{ - AccountChangeKind, AccountState, CallConfig, CallFrame, DefaultFrame, DiffMode, - GethDefaultTracingOptions, PreStateConfig, PreStateFrame, PreStateMode, StructLog, - erc7562::{AccessedSlots, CallFrameType, ContractSize, Erc7562Config, Erc7562Frame}, + AccountState, CallConfig, CallFrame, DefaultFrame, DiffMode, GethDefaultTracingOptions, + PreStateConfig, PreStateFrame, PreStateMode, StructLog, erc7562::CallFrameType, }; use evm2::{ EvmFeatures, EvmTypesHost, TxResultExt, TxResultWithState, - evm::{DbResult, DynDatabase}, - interpreter::{InstrStop, op}, + evm::{DbResult, DynDatabase, PendingState}, }; +/// The smallest size in bytes of the JSON of a struct log, +/// `{"pc":0,"op":"OR","gas":0,"gasCost":0,"depth":1}`. +pub(crate) const MIN_STRUCT_LOG_JSON_LEN: u64 = 48; + +/// Returns the size in bytes of the JSON that geth's struct logger writes for the log. +/// +/// geth counts these bytes against the `limit` option. The JSON is compact, orders the fields as +/// `pc`, `op`, `gas`, `gasCost`, `depth`, `error`, `stack`, `returnData`, `memory`, `storage` and +/// `refund`, and omits the optional ones that are unset, empty or zero. It also counts the +/// `stateGasCost` and `stateGasReservoir` fields that evm2 adds after EIP-8037. It escapes `<`, `>` +/// and `&` in strings as `\u00xx`. +/// +/// See +fn struct_log_json_len(log: &StructLog) -> usize { + const fn digits(value: u64) -> usize { + match value.checked_ilog10() { + Some(log) => log as usize + 1, + None => 1, + } + } + // The quoted `0x`-prefixed minimal hex of a word. + fn word(value: &U256) -> usize { + 2 + 2 + value.bit_len().div_ceil(4).max(1) + } + // The quoted `0x`-prefixed hex of a 32-byte hash. + const HASH: usize = 2 + 2 + 64; + + // `{"pc":`, `,"op":"`, `","gas":`, `,"gasCost":`, `,"depth":` and `}`. + let mut len = 6 + digits(log.pc) + 7 + log.op.len() + 1 + 7 + digits(log.gas); + len += 11 + digits(log.gas_cost) + 9 + digits(log.depth) + 1; + // The fields after EIP-8037 that evm2 adds to the struct log: `,"stateGasCost":` and + // `,"stateGasReservoir":`. + if let Some(cost) = log.state_gas_cost { + len += 16 + usize::from(cost < 0) + digits(cost.unsigned_abs()); + } + if let Some(reservoir) = log.state_gas_reservoir { + len += 21 + digits(reservoir); + } + if let Some(error) = log.error.as_ref().filter(|error| !error.is_empty()) { + let escaped = error.bytes().filter(|byte| matches!(byte, b'<' | b'>' | b'&')).count(); + len += 10 + error.len() + 5 * escaped + 1; + } + if let Some(stack) = &log.stack { + len += 10 + stack.iter().map(word).sum::() + stack.len().saturating_sub(1) + 1; + } + if let Some(return_data) = log.return_data.as_ref().filter(|data| !data.is_empty()) { + len += 15 + 2 + 2 * return_data.len() + 1; + } + if let Some(memory) = log.memory.as_ref().filter(|memory| !memory.is_empty()) { + len += 11 + memory.iter().map(|chunk| chunk.len() + 2).sum::(); + len += memory.len().saturating_sub(1) + 1; + } + if let Some(storage) = log.storage.as_ref().filter(|storage| !storage.is_empty()) { + len += 12 + storage.len() * (HASH + 1 + HASH) + storage.len().saturating_sub(1) + 1; + } + if let Some(refund) = log.refund_counter.filter(|refund| *refund != 0) { + len += 10 + digits(refund); + } + len +} + /// A frame awaiting tree assembly, with visibility inherited by children in the forward pass. -struct PendingCallFrame { +struct PendingCallFrame { /// Whether this call and its ancestors permit descendant logs. Independent of whether /// the frame itself contains logs. logs_visible: bool, - frame: T, + frame: CallFrame, } #[cfg(test)] @@ -84,7 +141,7 @@ impl<'a> GethTraceBuilder<'a> { &self, main_trace_node: &CallTraceNode, opts: &GethDefaultTracingOptions, - storage: &mut HashMap>, + storage: &mut AddressMap>, struct_logs: &mut Vec, mut stacks: Option<&mut [StackSnapshots]>, ) { @@ -92,9 +149,11 @@ impl<'a> GethTraceBuilder<'a> { // call depth rather than step count, and a trace without subcalls needs no allocation. let mut steps = main_trace_node.steps_with_children(); let mut parents = Vec::new(); + let mut json_len = 0u64; loop { - if opts.limit.is_some_and(|limit| struct_logs.len() as u64 >= limit) { + // geth stops logging once the JSON written so far is over the limit. + if opts.limit.is_some_and(|limit| json_len > limit) { break; } let Some(CallTraceStepStackItem { trace_node, step, call_child_id }) = steps.next() @@ -117,26 +176,26 @@ impl<'a> GethTraceBuilder<'a> { let mut log = step.convert_to_geth_struct_log(opts, trace_node.trace.depth as u64 + 1, stack); - // Only touch the storage cache when updating it or emitting a storage snapshot. + // geth snapshots the storage of the contract, with the slots of all its earlier + // SLOADs and SSTOREs, unless the opcode fails before it runs. if opts.is_storage_enabled() - && (step.storage_change.is_some() - || matches!(step.op.get(), op::SLOAD | op::SSTORE)) + && !step.is_error() + && let Some(StorageChange { key, value, .. }) = + step.storage_change.as_deref().copied() { let contract_storage = storage.entry(trace_node.execution_address()).or_default(); - if let Some(change) = &step.storage_change { - contract_storage.insert(change.key.into(), change.value.into()); - } - - if matches!(step.op.get(), op::SLOAD | op::SSTORE) { - log.storage = Some(contract_storage.clone()); - } + contract_storage.insert(key.into(), value.into()); + log.storage = Some(contract_storage.clone()); } - if opts.is_return_data_enabled() { + if opts.is_return_data_enabled() && !step.returndata.is_empty() { log.return_data = Some(step.returndata.clone()); } // Add step to geth trace + if opts.limit.is_some() { + json_len = json_len.saturating_add(struct_log_json_len(&log) as u64); + } struct_logs.push(log); // Visit the child immediately after its call opcode, then resume the parent. @@ -168,7 +227,7 @@ impl<'a> GethTraceBuilder<'a> { ) -> DefaultFrame { self.geth_traces_inner( gas.tx_gas_used(), - Some((gas.execution_gas_spent(), gas.state_gas_spent(), final_refunded(gas))), + Some((gas.execution_gas_spent(), gas.state_gas_spent(), gas.final_refunded())), return_value, opts, None, @@ -198,7 +257,10 @@ impl<'a> GethTraceBuilder<'a> { GethDefaultTracingOptions { limit: opts.limit.filter(|limit| *limit != 0), ..opts }; let capacity = opts.limit.map_or_else( || self.trace_step_count(), - |limit| self.trace_step_count().min(usize::try_from(limit).unwrap_or(usize::MAX)), + |limit| { + let steps = limit / MIN_STRUCT_LOG_JSON_LEN + 1; + self.trace_step_count().min(usize::try_from(steps).unwrap_or(usize::MAX)) + }, ); let mut struct_logs = Vec::with_capacity(capacity); let mut storage = HashMap::default(); @@ -236,7 +298,7 @@ impl<'a> GethTraceBuilder<'a> { self.geth_call_traces_inner( opts, gas.tx_gas_used(), - Some((gas.execution_gas_spent(), gas.state_gas_spent(), final_refunded(gas))), + Some((gas.execution_gas_spent(), gas.state_gas_spent(), gas.final_refunded())), ) } @@ -338,19 +400,18 @@ impl<'a> GethTraceBuilder<'a> { prestate_config: &PreStateConfig, db: &mut dyn DynDatabase, ) -> DbResult { - let state = TxState::from_pending(pending_state); let code_enabled = prestate_config.code_enabled(); let storage_enabled = prestate_config.storage_enabled(); if prestate_config.is_diff_mode() { - self.geth_prestate_diff_traces(&state, db, code_enabled, storage_enabled) + self.geth_prestate_diff_traces(pending_state, db, code_enabled, storage_enabled) } else { - self.geth_prestate_pre_traces(&state, db, code_enabled, storage_enabled) + self.geth_prestate_pre_traces(pending_state, db, code_enabled, storage_enabled) } } fn geth_prestate_pre_traces( &self, - state: &TxState, + state: &PendingState, db: &mut dyn DynDatabase, code_enabled: bool, storage_enabled: bool, @@ -358,8 +419,8 @@ impl<'a> GethTraceBuilder<'a> { let mut prestate = PreStateMode::default(); // we only want changed accounts for things like balance changes etc - for (addr, loaded_acc) in state.accounts.iter() { - let addr = *addr; + for loaded_acc in state.account_changes() { + let addr = loaded_acc.address; let db_acc = db.get_account(&addr)?.unwrap_or_default(); // Geth omits accounts that were empty before execution, even if fees fund them. // Check the original code hash before applying the disableCode output option. @@ -371,8 +432,8 @@ impl<'a> GethTraceBuilder<'a> { // insert the original value of all loaded storage slots if storage_enabled { - for (key, slot) in loaded_acc.storage.iter() { - acc_state.storage.insert((*key).into(), slot.original.into()); + for (key, slot) in &loaded_acc.storage.slots { + acc_state.storage.insert((*key).into(), slot.value.original.into()); } } @@ -384,22 +445,21 @@ impl<'a> GethTraceBuilder<'a> { fn geth_prestate_diff_traces( &self, - state: &TxState, + state: &PendingState, db: &mut dyn DynDatabase, code_enabled: bool, storage_enabled: bool, ) -> DbResult { let mut state_diff = DiffMode::default(); - let mut account_change_kinds = - HashMap::with_capacity_and_hasher(state.accounts.len(), Default::default()); - for (addr, changed_acc) in state.accounts.iter() { - let addr = *addr; + let mut empty_before = AddressSet::default(); + for changed_acc in state.account_changes() { + let addr = changed_acc.address; let db_acc = db.get_account(&addr)?.unwrap_or_default(); let pre_code = if code_enabled { load_account_code(db, &db_acc)? } else { None }; // deleted accounts are treated as drained: the post info is empty - let info = changed_acc.current.clone().unwrap_or_default(); + let info = changed_acc.current.cloned().unwrap_or_default(); let mut post_state = AccountState::from_account_info( info.nonce, @@ -419,33 +479,40 @@ impl<'a> GethTraceBuilder<'a> { let mut pre_state = AccountState::from_account_info(db_acc.nonce, db_acc.balance, pre_code); + // Geth reports `"code":"0x"` when the transaction clears the code the account had. + if pre_state.code.is_some() && post_state.code.is_none() { + post_state.code = Some(Bytes::new()); + } + // handle storage changes if storage_enabled { - for (key, slot) in changed_acc.changed_storage() { - pre_state.storage.insert((*key).into(), slot.original.into()); - post_state.storage.insert((*key).into(), slot.current.into()); + // Geth keeps the pre state of a selfdestructed account as it looked it up, with + // every slot the transaction touched, unchanged and zero ones included. + if changed_acc.selfdestructed { + for (key, slot) in &changed_acc.storage.slots { + pre_state.storage.insert((*key).into(), slot.value.original.into()); + } + } else { + for (key, slot) in changed_acc.storage.changed_slots() { + if !slot.original.is_zero() { + pre_state.storage.insert((*key).into(), slot.original.into()); + } + if !slot.current.is_zero() { + post_state.storage.insert((*key).into(), slot.current.into()); + } + } } } state_diff.pre.insert(addr, pre_state); - // determine the change type - // if the account was created _and_ not empty we need to treat it as modified, - // so that it is retained later in `diff_traces` + // Geth drops every account that was empty before the transaction from the pre state, + // whether the transaction created it or only funded it. // See // - let pre_change = if changed_acc.created && db_acc.is_empty() { - AccountChangeKind::Create - } else { - AccountChangeKind::Modify - }; - let post_change = if changed_acc.selfdestructed { - AccountChangeKind::SelfDestruct - } else { - AccountChangeKind::Modify - }; - - account_change_kinds.insert(addr, (pre_change, post_change)); + if db_acc.is_empty() { + empty_before.insert(addr); + } // Don't insert selfdestructed accounts into post state if !changed_acc.selfdestructed { @@ -454,212 +521,30 @@ impl<'a> GethTraceBuilder<'a> { } // ensure we're only keeping changed entries - state_diff.retain_changed().remove_zero_storage_values(); + state_diff.retain_changed(); - self.diff_traces(&mut state_diff.pre, &mut state_diff.post, account_change_kinds); + self.diff_traces(&mut state_diff.pre, &mut state_diff.post, &empty_before); Ok(PreStateFrame::Diff(state_diff)) } - /// Returns the difference between the pre and post state of the transaction depending on the - /// kind of changes of that account (pre,post) + /// Returns the difference between the pre and post state of the transaction. + /// + /// `empty_before` holds the accounts that were empty before the transaction. They are removed + /// from `pre` after `post` is diffed against them. fn diff_traces( &self, pre: &mut BTreeMap, post: &mut BTreeMap, - change_type: HashMap, + empty_before: &AddressSet, ) { - post.retain(|addr, post_state| { - // Don't keep destroyed accounts in the post state - if change_type.get(addr).map(|ty| ty.1.is_selfdestruct()).unwrap_or(false) { - return false; - } + for (addr, post_state) in post.iter_mut() { if let Some(pre_state) = pre.get(addr) { // remove any unchanged account info post_state.remove_matching_account_info(pre_state); } - - true - }); - - // Don't keep created accounts the pre state - pre.retain(|addr, _pre_state| { - // only keep accounts that are not created - change_type.get(addr).map(|ty| !ty.0.is_created()).unwrap_or(true) - }); - } - - /// Traces ERC-7562 calls using the call tracer. - pub fn geth_erc7562_traces( - &self, - opts: Erc7562Config, - gas_used: u64, - db: &mut dyn DynDatabase, - ) -> DbResult { - if self.nodes.is_empty() { - return Ok(Default::default()); } - let include_logs = opts.with_log.unwrap_or_default(); - // only the root frame's own fields are needed, its children are assembled below - let mut top_call = { - let root_logs_visible = include_logs && !self.nodes[0].trace.is_error(); - let mut root = self.nodes[0].geth_empty_call_frame(root_logs_visible); - root.gas_used = U256::from(gas_used); - Some(root) - }; - - let mut frames: Vec> = Vec::with_capacity(self.nodes.len()); - - for (idx, node) in self.nodes.iter().enumerate() { - let trace = &node.trace; - - let mut accessed_slots = AccessedSlots::default(); - let mut used_opcodes = HashMap::default(); - let mut contract_size = HashMap::default(); - let mut ext_code_access_info = Vec::new(); - let mut keccak = Vec::new(); - let mut out_of_gas = trace.status.is_some_and(InstrStop::is_out_of_gas); - - for step in &trace.steps { - let op = step.op.get(); - out_of_gas |= step.status.is_some_and(InstrStop::is_out_of_gas); - - // Skip if opcode is ignored - if opts.ignored_opcodes.contains(&op) { - continue; - } - - // Count used opcodes - *used_opcodes.entry(op).or_insert(0) += 1; - - // Accessed storage slots - match op { - op::SLOAD => { - if let Some(stack) = &step.stack - && let Some(slot) = stack.get(stack.len().saturating_sub(1)) - { - let slot: B256 = (*slot).into(); - let already_read = accessed_slots.reads.contains_key(&slot); - let already_written = accessed_slots.writes.contains_key(&slot); - if !already_read - && !already_written - && let Some(change) = &step.storage_change - { - let value: B256 = change.value.into(); - accessed_slots.reads.entry(slot).or_default().push(value); - } - } - } - op::SSTORE => { - if let Some(stack) = &step.stack - && let Some(slot) = stack.get(stack.len().saturating_sub(1)) - { - let slot: B256 = (*slot).into(); - *accessed_slots.writes.entry(slot).or_insert(0) += 1; - } - } - op::TLOAD => { - if let Some(stack) = &step.stack - && let Some(slot) = stack.get(stack.len().saturating_sub(1)) - { - let slot: B256 = (*slot).into(); - *accessed_slots.transient_reads.entry(slot).or_insert(0) += 1; - } - } - op::TSTORE => { - if let Some(stack) = &step.stack - && let Some(slot) = stack.get(stack.len().saturating_sub(1)) - { - let slot: B256 = (*slot).into(); - *accessed_slots.transient_writes.entry(slot).or_insert(0) += 1; - } - } - _ => {} - } - - if matches!(op, op::EXTCODESIZE | op::EXTCODECOPY | op::EXTCODEHASH) - && let Some(stack) = &step.stack - && let Some(item) = stack.get(stack.len().saturating_sub(1)) - { - let address = Address::from_word((*item).into()); - ext_code_access_info.push(format!("{address:?}")); - if let Entry::Vacant(e) = contract_size.entry(address) - && let Some(account) = db.get_account(&address)? - && let Some(code) = load_account_code(db, &account)? - { - e.insert(ContractSize { contract_size: code.len() as u64, opcode: op }); - } - } - - // KECCAK preimages from returndata - if op == op::KECCAK256 - && step.status.is_none() - && let (Some(stack), Some(memory)) = (&step.stack, &step.memory) - && stack.len() >= 2 - { - let offset = stack[stack.len() - 1]; - let len = stack[stack.len() - 2]; - if let (Ok(offset), Ok(len)) = (usize::try_from(offset), usize::try_from(len)) { - let mut data = vec![0; len]; - if offset < memory.0.len() { - let end = (offset + len).min(memory.0.len()); - let copy_len = end - offset; - data[..copy_len].copy_from_slice(&memory.0[offset..end]); - } - keccak.push(Bytes::from(data)); - } - } - } - - let include_logs = include_logs - && !trace.is_error() - && node.parent.is_none_or(|parent| frames[parent].logs_visible); - let call_frame = if idx == 0 { - top_call.take().unwrap() - } else { - self.nodes[idx].geth_empty_call_frame(include_logs) - }; - - let call_frame_type = Self::convert_call_kind(node.kind()); - - frames.push(PendingCallFrame { - logs_visible: include_logs, - frame: Erc7562Frame { - call_frame_type, - from: call_frame.from, - gas: call_frame.gas.to(), - gas_used: call_frame.gas_used.to(), - to: call_frame.to, - input: call_frame.input, - output: call_frame.output, - error: call_frame.error, - revert_reason: call_frame.revert_reason, - logs: call_frame.logs, - value: call_frame.value, - accessed_slots, - ext_code_access_info, - used_opcodes, - contract_size, - out_of_gas, - keccak, - calls: vec![], - }, - }); - } - - // Assemble tree, see `geth_call_traces_inner` for the ordering logic - loop { - let PendingCallFrame { mut frame, .. } = frames.pop().expect("call frames not empty"); - let idx = frames.len(); - frame.calls.reverse(); - let node = &self.nodes[idx]; - if let Some(parent) = node.parent { - frames[parent].frame.calls.push(frame); - } else { - debug_assert!(frames.is_empty(), "only one root node has no parent"); - return Ok(frame); - } - } + pre.retain(|addr, _pre_state| !empty_before.contains(addr)); } /// Converts a CallKind to a CallFrameType. @@ -679,16 +564,13 @@ impl<'a> GethTraceBuilder<'a> { #[cfg(test)] mod tests { use super::*; - use crate::tracing::{ - tx_state::TxAccount, - types::{CallLog, CallTrace}, - }; + use crate::tracing::types::{CallLog, CallTrace}; use alloy_primitives::{B256, U256, address}; use evm2::{ AccountInfo, DatabaseError, bytecode::Bytecode, - evm::{CacheDB, DbResult, DynDatabase, EmptyDB}, - interpreter::{InstrStop, Word}, + evm::{DbResult, DynDatabase, InMemoryDB}, + interpreter::{InstrStop, Word, op}, }; struct FailingDb { @@ -775,20 +657,11 @@ mod tests { CallConfig { with_log: Some(with_log), ..Default::default() }, 0, ); - let erc = builder - .geth_erc7562_traces( - Erc7562Config { with_log: Some(with_log), ..Default::default() }, - 0, - &mut EmptyDB::default(), - ) - .unwrap(); - let mut pending = vec![(&call, &erc)]; + let mut pending = vec![&call]; let mut visited = Vec::new(); - while let Some((call, erc)) = pending.pop() { + while let Some(call) = pending.pop() { let idx = call.to.unwrap().as_slice()[19] as usize; visited.push(idx); - assert_eq!(call.to, erc.to); - assert_eq!(call.logs, erc.logs); let visible = with_log && !root_failed && (idx == 0 || idx >= 5); assert_eq!(call.logs.len(), if visible { 2 } else { 0 }, "node {idx}"); if visible { @@ -798,8 +671,7 @@ mod tests { assert_eq!(log.data, Some(Bytes::from(vec![idx as u8, log_idx as u8]))); } } - assert_eq!(call.calls.len(), erc.calls.len()); - pending.extend(call.calls.iter().zip(&erc.calls).rev()); + pending.extend(call.calls.iter().rev()); } assert_eq!(visited, [0, 1, 2, 3, 4, 5, 6]); } @@ -807,30 +679,19 @@ mod tests { } #[test] - fn prestate_diff_keeps_prefunded_created_accounts() { - let mut state = TxState::default(); + fn prestate_diff_keeps_prefunded_accounts() { + let mut state = PendingState::default(); let prefunded_addr = address!("1000000000000000000000000000000000000001"); let empty_addr = address!("2000000000000000000000000000000000000002"); - state.accounts.insert( + state.insert_account( prefunded_addr, - TxAccount { - original: Some(AccountInfo::default().with_balance(U256::from(10))), - current: Some(AccountInfo::default().with_balance(U256::from(1)).with_nonce(1)), - created: true, - ..TxAccount::default() - }, - ); - state.accounts.insert( - empty_addr, - TxAccount { - current: Some(AccountInfo::default().with_nonce(1)), - created: true, - ..TxAccount::default() - }, + Some(AccountInfo::default().with_balance(U256::from(10))), + Some(AccountInfo::default().with_balance(U256::ONE).with_nonce(1)), ); + state.insert_account(empty_addr, None, Some(AccountInfo::default().with_nonce(1))); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &prefunded_addr, AccountInfo::default().with_balance(U256::from(10)), @@ -856,18 +717,15 @@ mod tests { #[test] fn prestate_diff_loads_post_code_from_state_changes() { - let mut state = TxState::default(); + let mut state = PendingState::default(); let address = address!("1000000000000000000000000000000000000001"); let code = Bytecode::new_legacy(vec![op::PUSH1, 0x01, op::STOP].into()); // the changed account from the state output always holds the code let account = AccountInfo::default().with_nonce(1).with_code(code.clone()); - state.accounts.insert( - address, - TxAccount { current: Some(account), created: true, ..TxAccount::default() }, - ); + state.insert_account(address, None, Some(account)); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); let builder = GethTraceBuilder::new(Vec::new()); let frame = builder.geth_prestate_diff_traces(&state, &mut db, true, false).unwrap(); @@ -885,16 +743,9 @@ mod tests { #[test] fn prestate_propagates_db_account_errors() { - let mut state = TxState::default(); + let mut state = PendingState::default(); let address = address!("1000000000000000000000000000000000000001"); - state.accounts.insert( - address, - TxAccount { - current: Some(AccountInfo::default().with_nonce(1)), - created: true, - ..TxAccount::default() - }, - ); + state.insert_account(address, None, Some(AccountInfo::default().with_nonce(1))); let error = DatabaseError::new(evm2::AnyError::from("test database error"), true); let mut db = FailingDb::new(error.clone()); @@ -914,7 +765,7 @@ mod tests { let nonce_account = Address::with_last_byte(4); let contract = Address::with_last_byte(5); let code = Bytecode::new_legacy(Bytes::from_static(&[0x00])); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info(&empty_beneficiary, AccountInfo::default()); db.insert_account_info( &funded_beneficiary, @@ -923,19 +774,17 @@ mod tests { db.insert_account_info(&nonce_account, AccountInfo::default().with_nonce(1)); db.insert_account_info(&contract, AccountInfo::default().with_code(code.clone())); - let mut state = TxState::default(); + let mut state = PendingState::default(); for address in [absent_beneficiary, empty_beneficiary] { - state.accounts.insert( + state.insert_account( address, - TxAccount { - current: Some(AccountInfo::default().with_balance(U256::from(1))), - ..Default::default() - }, + None, + Some(AccountInfo::default().with_balance(U256::ONE)), ); } // An account emptied by execution must retain its original prestate. for address in [funded_beneficiary, nonce_account, contract] { - state.accounts.insert(address, TxAccount::default()); + state.insert_account(address, None, None); } let builder = GethTraceBuilder::new(Vec::new()); diff --git a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs index a4ca15a60..e0fbf0738 100644 --- a/crates/inspectors/src/tracing/builder/geth/steps_tests.rs +++ b/crates/inspectors/src/tracing/builder/geth/steps_tests.rs @@ -1,11 +1,9 @@ use super::*; use crate::tracing::types::{ - CallTrace, CallTraceStep, RecordedMemory, StorageChange, StorageChangeReason, -}; -use evm2::{ - evm::EmptyDB, - interpreter::{InstrStop, opcode::OpCode}, + CallTrace, CallTraceStep, RecordedMemory, StorageChange, StorageChangeReason, TraceMemberOrder, }; +use alloc::string::String; +use evm2::interpreter::{InstrStop, op, opcode::OpCode}; fn test_step(pc: usize, op: u8) -> CallTraceStep { CallTraceStep { @@ -17,7 +15,6 @@ fn test_step(pc: usize, op: u8) -> CallTraceStep { returndata: Bytes::from(vec![pc as u8]), gas_remaining: 1000 - pc as u64, gas_refund_counter: 0, - gas_used: pc as u64, gas_cost: 1, state_gas_cost: None, state_gas_reservoir: None, @@ -26,7 +23,7 @@ fn test_step(pc: usize, op: u8) -> CallTraceStep { alloc::boxed::Box::new(StorageChange { key: U256::ZERO, value: U256::from(pc), - had_value: None, + had_value: Some(U256::ZERO), reason: StorageChangeReason::SSTORE, }) }), @@ -57,97 +54,36 @@ fn struct_log_errors_exclude_reverts() { } #[test] -fn erc7562_keccak_preimages_require_successful_steps() { - for status in [ - None, - Some(InstrStop::OutOfGas), - Some(InstrStop::MemoryOOG), - Some(InstrStop::MemoryLimitOOG), - Some(InstrStop::PrecompileOOG), - Some(InstrStop::InvalidOperandOOG), - Some(InstrStop::ReentrancySentryOOG), - Some(InstrStop::StackUnderflow), - Some(InstrStop::FatalExternalError), - ] { - for (offset, len, expected) in - [(0, 0, vec![]), (1, 2, vec![2, 3]), (1, 4, vec![2, 3, 0, 0]), (4, 2, vec![0, 0])] - { - let mut step = test_step(0, op::KECCAK256); - step.stack = Some(vec![U256::from(len), U256::from(offset)].into_boxed_slice()); - step.memory = Some(RecordedMemory::new(&[1, 2, 3])); - step.status = status; - let builder = GethTraceBuilder::new(vec![CallTraceNode { - trace: CallTrace { - steps: vec![step, test_step(1, op::REVERT)], - status: Some(InstrStop::Revert), - ..Default::default() - }, - ..Default::default() - }]); - let frame = builder - .geth_erc7562_traces(Erc7562Config::default(), 0, &mut EmptyDB::default()) - .unwrap(); - let expected = if status.is_none() { vec![Bytes::from(expected)] } else { vec![] }; - assert_eq!(frame.keccak, expected, "status {status:?}, offset {offset}, len {len}"); - } - } -} +fn struct_log_omits_zero_refund_and_empty_memory() { + let opts = GethDefaultTracingOptions::default().enable_memory(); + let mut step = test_step(0, op::PUSH0); + step.memory = Some(RecordedMemory::new(&[])); + let log = step.convert_to_geth_struct_log(&opts, 1, None); + assert_eq!((log.refund_counter, log.memory), (None, None)); -#[test] -fn erc7562_classifies_all_out_of_gas_statuses() { - for (status, expected) in [ - (None, false), - (Some(InstrStop::Stop), false), - (Some(InstrStop::Return), false), - (Some(InstrStop::Revert), false), - (Some(InstrStop::OutOfFunds), false), - (Some(InstrStop::OpcodeNotFound), false), - (Some(InstrStop::OutOfGas), true), - (Some(InstrStop::MemoryOOG), true), - (Some(InstrStop::MemoryLimitOOG), true), - (Some(InstrStop::PrecompileOOG), true), - (Some(InstrStop::InvalidOperandOOG), true), - (Some(InstrStop::ReentrancySentryOOG), true), - ] { - assert_eq!(status.is_some_and(InstrStop::is_out_of_gas), expected); - for frame_status in [false, true] { - let mut step = test_step(0, op::KECCAK256); - step.status = status; - let builder = GethTraceBuilder::new(vec![CallTraceNode { - trace: CallTrace { - status: if frame_status { status } else { None }, - steps: if frame_status { vec![] } else { vec![step] }, - ..Default::default() - }, - ..Default::default() - }]); - for ignored in [false, true] { - let mut config = Erc7562Config::default(); - if ignored { - config.ignored_opcodes.push(op::KECCAK256); - } - let frame = - builder.geth_erc7562_traces(config, 0, &mut EmptyDB::default()).unwrap(); - assert_eq!( - frame.out_of_gas, expected, - "status {status:?}, frame_status {frame_status}, ignored {ignored}" - ); - } - } - } + step.gas_refund_counter = 4800; + step.memory = Some(RecordedMemory::new(&[1])); + let log = step.convert_to_geth_struct_log(&opts, 1, None); + assert_eq!(log.refund_counter, Some(4800)); + assert_eq!(log.memory.map(|memory| memory.len()), Some(1)); } #[test] fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { // Cover all call-like opcodes, a precompile with no steps, an empty creation, // a reverting grandchild, and a final call opcode that failed before recording a child. - type Fixture = (Option, usize, &'static [usize], &'static [u8]); - let fixtures: &[Fixture] = &[ - ( - None, - 0, - &[1, 2, 4, 5, 6], - &[ + struct Fixture { + parent: Option, + depth: usize, + children: &'static [usize], + ops: &'static [u8], + } + let fixtures = [ + Fixture { + parent: None, + depth: 0, + children: &[1, 2, 4, 5, 6], + ops: &[ op::SSTORE, op::CALL, op::CALLCODE, @@ -156,18 +92,23 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { op::CREATE2, op::CALL, ], - ), - (Some(0), 1, &[], &[]), - (Some(0), 1, &[3], &[op::SSTORE, op::DELEGATECALL, op::SLOAD, op::STOP]), - (Some(2), 2, &[], &[op::SSTORE, op::REVERT]), - (Some(0), 1, &[], &[op::RETURN]), - (Some(0), 1, &[], &[]), - (Some(0), 1, &[], &[op::STOP]), + }, + Fixture { parent: Some(0), depth: 1, children: &[], ops: &[] }, + Fixture { + parent: Some(0), + depth: 1, + children: &[3], + ops: &[op::SSTORE, op::DELEGATECALL, op::SLOAD, op::STOP], + }, + Fixture { parent: Some(2), depth: 2, children: &[], ops: &[op::SSTORE, op::REVERT] }, + Fixture { parent: Some(0), depth: 1, children: &[], ops: &[op::RETURN] }, + Fixture { parent: Some(0), depth: 1, children: &[], ops: &[] }, + Fixture { parent: Some(0), depth: 1, children: &[], ops: &[op::STOP] }, ]; let mut nodes: Vec<_> = fixtures .iter() .enumerate() - .map(|(idx, &(parent, depth, children, ops))| { + .map(|(idx, &Fixture { parent, depth, children, ops })| { CallTraceNode { idx, parent, @@ -190,7 +131,26 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { } }) .collect(); + // Each call opcode enters the next child, as the inspector records them. + for node in &mut nodes { + let mut children = 0..node.children.len(); + for (step_idx, step) in node.trace.steps.iter().enumerate() { + node.ordering.push(TraceMemberOrder::Step(step_idx)); + if step.is_call_like_op() + && let Some(child) = children.next() + { + node.ordering.push(TraceMemberOrder::Call(child)); + } + } + } nodes[0].trace.steps.last_mut().unwrap().status = Some(InstrStop::OutOfGas); + // The SLOAD reads the value its delegatecall child wrote. + nodes[2].trace.steps[2].storage_change = Some(Box::new(StorageChange { + key: U256::ZERO, + value: U256::from(30), + had_value: None, + reason: StorageChangeReason::SLOAD, + })); let builder = GethTraceBuilder::new(nodes); let expected = [ (0, 1), @@ -238,20 +198,35 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { .map(|value| { BTreeMap::from([(B256::ZERO, U256::from(value).into())]) }) ); } - for limit in [0, 1, 3, 6, 9, 99] { + let sizes: Vec<_> = frame.struct_logs.iter().map(struct_log_json_len).collect(); + let total: usize = sizes.iter().sum(); + for limit in + [0, 1, sizes[0], sizes[0] + sizes[1], sizes[..5].iter().sum(), total, 2 * total] + { let limited = builder.geth_traces( 123, Bytes::new(), GethDefaultTracingOptions { - limit: Some(limit), + limit: Some(limit as u64), disable_storage: Some(disable_storage), enable_return_data: Some(true), ..Default::default() }, ); - let len = - if limit == 0 { expected.len() } else { (limit as usize).min(expected.len()) }; - assert_eq!(limited.struct_logs, frame.struct_logs[..len]); + let mut written = 0; + let len = if limit == 0 { + expected.len() + } else { + sizes + .iter() + .take_while(|size| { + let logged = written <= limit; + written += **size; + logged + }) + .count() + }; + assert_eq!(limited.struct_logs, frame.struct_logs[..len], "limit {limit}"); } } assert!( @@ -269,18 +244,22 @@ fn opcode_trace_resumes_parents_after_nested_and_empty_calls() { } #[test] -fn storage_snapshots_keep_empty_reads_and_custom_changes() { - let mut custom_change = test_step(2, op::SSTORE); - // Public trace nodes can carry a storage change on a different opcode. - custom_change.op = OpCode::new_or_unknown(op::ADD); +fn storage_snapshots_accumulate_and_skip_failed_steps() { + let mut failed = test_step(4, op::SSTORE); + failed.status = Some(InstrStop::OutOfGas); + let mut sload = test_step(1, op::SLOAD); + sload.storage_change = Some(Box::new(StorageChange { + key: U256::ZERO, + value: U256::ONE, + had_value: None, + reason: StorageChangeReason::SLOAD, + })); + // A step that is not an SLOAD or SSTORE has no slot. + let mut add = test_step(2, op::ADD); + add.storage_change = None; let builder = GethTraceBuilder::new(vec![CallTraceNode { trace: CallTrace { - steps: vec![ - test_step(0, op::PUSH0), - test_step(1, op::SLOAD), - custom_change, - test_step(3, op::SLOAD), - ], + steps: vec![test_step(0, op::PUSH0), sload, add, failed], ..Default::default() }, ..Default::default() @@ -296,11 +275,104 @@ fn storage_snapshots_keep_empty_reads_and_custom_changes() { ); assert_eq!(frame.struct_logs.len(), 4); assert!(frame.struct_logs[0].storage.is_none()); - assert_eq!(frame.struct_logs[1].storage, (!disable_storage).then(BTreeMap::new)); + assert_eq!( + frame.struct_logs[1].storage, + (!disable_storage).then(|| BTreeMap::from([(B256::ZERO, B256::with_last_byte(1))])) + ); assert!(frame.struct_logs[2].storage.is_none()); + assert!(frame.struct_logs[3].storage.is_none()); + } +} + +/// The entries are what go-ethereum master's struct logger writes (`debug_traceCall`). +#[test] +fn struct_log_json_len_matches_geth() { + for json in [ + r#"{"pc":0,"op":"ADD","gas":979000,"gasCost":3,"depth":1,"error":"stack underflow (0 \u003c=\u003e 2)","stack":[]}"#, + r#"{"pc":4,"op":"SSTORE","gas":978994,"gasCost":12100,"depth":1,"stack":["0x1","0x0"],"storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000001"}}"#, + r#"{"pc":7,"op":"SLOAD","gas":868971,"gasCost":100,"depth":1,"stack":["0x0"],"storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000001"}}"#, + r#"{"pc":11,"op":"PUSH1","gas":868862,"gasCost":3,"depth":1,"stack":[],"memory":["0x0000000000000000000000000000000000000000000000000000000000000001"]}"#, + r#"{"pc":13,"op":"MLOAD","gas":868859,"gasCost":3,"depth":1,"stack":["0x0"],"memory":["0x0000000000000000000000000000000000000000000000000000000000000001"]}"#, + r#"{"pc":14,"op":"PUSH1","gas":868856,"gasCost":3,"depth":1,"stack":["0x1"],"memory":["0x0000000000000000000000000000000000000000000000000000000000000001"]}"#, + r#"{"pc":0,"op":"PUSH1","gas":979000,"gasCost":3,"depth":1,"stack":[]}"#, + r#"{"pc":4,"op":"SSTORE","gas":978994,"gasCost":12100,"depth":1,"stack":["0x0","0x5"],"storage":{"0x0000000000000000000000000000000000000000000000000000000000000005":"0x0000000000000000000000000000000000000000000000000000000000000000"},"refund":11616}"#, + r#"{"pc":33,"op":"STOP","gas":978997,"gasCost":0,"depth":1,"stack":["0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff60"]}"#, + r#"{"pc":1,"op":"STOP","gas":5,"gasCost":0,"depth":1,"returnData":"0xdeadbeef","refund":4800}"#, + r#"{"pc":20,"op":"opcode 0xc not defined","gas":5,"gasCost":0,"depth":12,"error":"gas uint64 overflow \u0026 more"}"#, + ] { + let log: StructLog = serde_json::from_str(json).unwrap(); + assert_eq!(struct_log_json_len(&log), json.len(), "{json}"); + } +} + +#[test] +fn struct_log_json_len_skips_the_fields_geth_omits() { + let log = StructLog { + pc: 1, + op: "STOP".into(), + gas: 5, + depth: 1, + error: Some(String::new()), + return_data: Some(Bytes::new()), + memory: Some(vec![]), + storage: Some(BTreeMap::new()), + refund_counter: Some(0), + ..Default::default() + }; + let json = r#"{"pc":1,"op":"STOP","gas":5,"gasCost":0,"depth":1}"#; + assert_eq!(struct_log_json_len(&log), json.len()); +} + +#[test] +fn struct_log_json_len_counts_the_state_gas_fields() { + for (cost, reservoir) in [(None, None), (Some(0), Some(0)), (Some(-1234), Some(99_999))] { + let log = StructLog { + op: "SSTORE".into(), + gas: 100, + depth: 2, + state_gas_cost: cost, + state_gas_reservoir: reservoir, + ..Default::default() + }; assert_eq!( - frame.struct_logs[3].storage, - (!disable_storage).then(|| { BTreeMap::from([(B256::ZERO, U256::from(2).into())]) }) + struct_log_json_len(&log), + serde_json::to_string(&log).unwrap().len(), + "{cost:?} {reservoir:?}" + ); + } +} + +#[test] +fn struct_log_limit_counts_json_bytes() { + let steps = [op::PUSH1, op::PUSH1, op::SSTORE, op::STOP] + .iter() + .enumerate() + .map(|(pc, &op)| test_step(pc, op)) + .collect(); + let builder = GethTraceBuilder::new(vec![CallTraceNode { + trace: CallTrace { steps, ..Default::default() }, + ..Default::default() + }]); + let unlimited = builder.geth_traces(0, Bytes::new(), Default::default()).struct_logs; + let sizes: Vec<_> = unlimited.iter().map(struct_log_json_len).collect(); + assert_eq!(unlimited.len(), 4); + // geth logs a step while the bytes written so far do not exceed the limit, so the step that + // crosses the limit is the last one. + for limit in [1, sizes[0], sizes[0] + 1, sizes[0] + sizes[1], sizes[..3].iter().sum(), 10_000] { + let mut written = 0; + let expected = sizes + .iter() + .take_while(|size| { + let logged = written <= limit; + written += **size; + logged + }) + .count(); + let frame = builder.geth_traces( + 0, + Bytes::new(), + GethDefaultTracingOptions { limit: Some(limit as u64), ..Default::default() }, ); + assert_eq!(frame.struct_logs.len(), expected, "limit {limit}"); } } diff --git a/crates/inspectors/src/tracing/builder/parity.rs b/crates/inspectors/src/tracing/builder/parity.rs index cca1d3980..642b7d47c 100644 --- a/crates/inspectors/src/tracing/builder/parity.rs +++ b/crates/inspectors/src/tracing/builder/parity.rs @@ -1,7 +1,5 @@ use crate::tracing::{ - TracingInspectorConfig, - tx_state::TxState, - types::{CallTraceNode, CallTraceStep, StepDelta, TraceMemberOrder}, + types::{CallKind, CallTraceNode, CallTraceStep, StepDelta, TraceMemberOrder}, utils::load_account_code, }; use alloc::{string::ToString, vec, vec::Vec}; @@ -13,28 +11,53 @@ use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::parity::*; use core::iter::Peekable; use evm2::{ - EvmTypesHost, SpecId, TxResultExt, TxResultWithState, + AccountInfo, EvmTypesHost, TxResultExt, TxResultWithState, evm::{DbResult, DynDatabase, PendingState}, interpreter::InstrStop, }; /// A type for creating parity style traces /// -/// Note: Parity style traces always ignore calls to precompiles. +/// Note: Parity style traces ignore calls to precompiles without value, see +/// [`PrecompileExclusion::ZeroValue`]. #[derive(Clone, Debug)] pub struct ParityTraceBuilder { /// Recorded trace nodes nodes: Vec, + /// Marks the calls to precompiles left out of the traces, and everything they called; their + /// parents no longer list them as children. + excluded: Vec, } impl ParityTraceBuilder { - /// Returns a new instance of the builder - pub const fn new( - nodes: Vec, - _spec_id: Option, - _config: TracingInspectorConfig, - ) -> Self { - Self { nodes } + /// Returns a new instance of the builder that leaves out the calls to precompiles selected by + /// `exclusion`. + pub fn new(mut nodes: Vec, exclusion: PrecompileExclusion) -> Self { + // Parents always precede their children in the arena. + let mut excluded = Vec::::with_capacity(nodes.len()); + for node in &nodes { + excluded.push(exclusion.excludes(node) || node.parent.is_some_and(|p| excluded[p])); + } + if !excluded.contains(&true) { + return Self { nodes, excluded }; + } + + for node in &mut nodes { + // Calls appear in `ordering` in the order of `children`. + let mut kept = 0; + node.ordering.retain_mut(|member| { + let TraceMemberOrder::Call(position) = member else { return true }; + let keep = !excluded[node.children[*position]]; + if keep { + *position = kept; + kept += 1; + } + keep + }); + node.children.retain(|&child| !excluded[child]); + } + + Self { nodes, excluded } } /// Returns a list of all addresses that appeared as callers. @@ -70,8 +93,8 @@ impl ParityTraceBuilder { /// Each entry in the returned vector represents the trace address of the corresponding node in /// the nodes set. /// - /// CAUTION: This also includes precompiles, which have an empty trace address, as they are not - /// tracked as children of their parent. + /// CAUTION: This also includes excluded precompile calls, which have an empty trace address, as + /// they are not tracked as children of their parent. fn trace_addresses(&self) -> Vec> { let mut all_addresses = vec![Vec::new(); self.nodes.len()]; // Parents always precede their children in the arena, so a single forward pass can derive @@ -90,9 +113,12 @@ impl ParityTraceBuilder { /// Returns an iterator over all nodes to trace /// - /// This excludes nodes that represent calls to precompiles. + /// This excludes the calls to precompiles selected by the builder's [`PrecompileExclusion`]. fn iter_traceable_nodes(&self) -> impl Iterator { - self.nodes.iter().filter(|node| !node.is_precompile()) + self.nodes + .iter() + .zip(&self.excluded) + .filter_map(|(node, &excluded)| (!excluded).then_some(node)) } /// Returns an iterator over all recorded traces for `trace_transaction` @@ -126,7 +152,7 @@ impl ParityTraceBuilder { /// Warning: If `trace_types` contains [TraceType::StateDiff] the returned [StateDiff] will not /// be filled. Use [ParityTraceBuilder::into_trace_results_with_state] or /// [populate_state_diff] to populate the balance and nonce changes for the [StateDiff] - /// using the database. + /// using the pending state and the database for bytecode. pub fn into_trace_results( self, res: &TxResultExt, @@ -142,7 +168,8 @@ impl ParityTraceBuilder { /// Consumes the inspector and returns the trace results according to the configured trace /// types. /// - /// This also takes the database to populate the balance and nonce changes for the [StateDiff]. + /// This also takes the pending state and a database (used for bytecode) to populate the + /// [StateDiff]. /// /// Note: this is considered a convenience method that takes the state changes after inspecting /// a transaction with the [TracingInspector](crate::tracing::TracingInspector). @@ -162,8 +189,7 @@ impl ParityTraceBuilder { /// state, without cloning the state into a result container. /// /// Populates state diffs when requested by `trace_types`. VM bytecode comes from the recorded - /// frames. The database must represent the state before the transaction's changes are - /// committed. + /// frames. The database is only used to load bytecode by hash. pub fn into_trace_results_with_state_parts( self, result: &TxResultExt, @@ -262,8 +288,11 @@ impl ParityTraceBuilder { .nodes .into_iter() .zip(trace_addresses) - .filter(|(node, _)| !node.is_precompile()) - .map(|(node, trace_address)| (node.parity_transaction_trace(trace_address), node)) + .zip(self.excluded) + .filter(|(_, excluded)| !excluded) + .map(|((node, trace_address), _)| { + (node.parity_transaction_trace(trace_address), node) + }) .peekable(), } } @@ -287,11 +316,12 @@ impl ParityTraceBuilder { let mut child_idx_stack = Vec::with_capacity(self.nodes.len()); let mut sub_stack = Vec::with_capacity(self.nodes.len()); - // Excluded precompile calls are recorded without being attached to their parent, in call - // order. + // Excluded precompile calls are not attached to their parent; collect them in call order. let mut precompiles = HashMap::>::default(); - for node in self.nodes.iter().filter(|node| node.is_precompile()) { - if let Some(parent) = node.parent { + for (node, _) in self.nodes.iter().zip(&self.excluded).filter(|(_, excluded)| **excluded) { + if let Some(parent) = node.parent + && !self.excluded[parent] + { precompiles.entry(parent).or_default().push(node); } } @@ -398,6 +428,35 @@ impl ParityTraceBuilder { } } +/// Which calls to precompiles a [`ParityTraceBuilder`] leaves out of its traces. +/// +/// A call is only left out if the inspector recorded it as a call to a precompile, see +/// [`TracingInspectorConfig::record_precompile_calls`](crate::tracing::TracingInspectorConfig::record_precompile_calls), and it is not the root call. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PrecompileExclusion { + /// Keeps every call to a precompile. + None, + /// Leaves out calls of any kind that transfer no value, as Parity does. + ZeroValue, + /// Leaves out `CALL` and `STATICCALL` regardless of value and keeps `DELEGATECALL` and + /// `CALLCODE`, as geth's flatCallTracer does. + CallAndStaticCall, +} + +impl PrecompileExclusion { + fn excludes(self, node: &CallTraceNode) -> bool { + node.parent.is_some() + && node.is_precompile() + && match self { + Self::None => false, + Self::ZeroValue => node.trace.value.is_zero(), + Self::CallAndStaticCall => { + matches!(node.kind(), CallKind::Call | CallKind::StaticCall) + } + } + } +} + /// Returns whether a step began executing, excluding operations rejected before execution and /// the synthetic `STOP` past the end of the recorded bytecode. fn began_executing(step: &CallTraceStep, bytecode: Option<&Bytes>) -> bool { @@ -477,38 +536,38 @@ where /// Populates [StateDiff] given the [PendingState] of a transaction and a database. /// /// Loops over all state accounts in the accounts diff that contains all accounts that are included -/// in the [PendingState] and compares the balance and nonce against what's in the `db`, which -/// should point to the beginning of the transaction. +/// in the [PendingState] and compares the balance and nonce against the account as it was at the +/// start of the transaction, which the pending state records. The `db` is only used to load +/// bytecode by hash. pub fn populate_state_diff( state_diff: &mut StateDiff, db: &mut dyn DynDatabase, pending: &PendingState, ) -> DbResult<()> { - let state = TxState::from_pending(pending); - for (addr, changed_acc) in state.accounts.iter() { - let db_acc = db.get_account(addr)?; - + let absent = AccountInfo::default(); + for changed_acc in pending.account_changes() { + let addr = &changed_acc.address; // An account created and destroyed in this transaction has no net change unless it // already existed, for example with a prefunded balance. - if changed_acc.current.is_none() && db_acc.is_none() { + if changed_acc.current.is_none() && changed_acc.original.is_none() { continue; } - let existed = db_acc.is_some(); - let db_acc = db_acc.unwrap_or_default(); + let existed = changed_acc.original.is_some(); + let original = changed_acc.original.unwrap_or(&absent); let entry = state_diff.entry(*addr).or_default(); // Use the finalized account state: selfdestruct can preserve a balance-only account. if changed_acc.current.is_none() { - entry.balance = Delta::Removed(db_acc.balance); - entry.nonce = Delta::Removed(U64::from(db_acc.nonce)); - entry.code = Delta::Removed(load_account_code(db, &db_acc)?.unwrap_or_default()); + entry.balance = Delta::Removed(original.balance); + entry.nonce = Delta::Removed(U64::from(original.nonce)); + entry.code = Delta::Removed(load_account_code(db, original)?.unwrap_or_default()); // Deletion wipes all storage, which the removed account implies. PendingState // holds only accessed slots, so listing them would produce a partial diff. continue; } - let info = changed_acc.current.as_ref().expect("deleted accounts handled above"); + let info = changed_acc.current.expect("deleted accounts handled above"); // An absent prestate account is added if it has nonempty final state or // the EVM marked it created (which can preserve an empty account). @@ -521,19 +580,19 @@ pub fn populate_state_diff( entry.code = Delta::Added(account_code); // Only changed slots are added; unchanged zero-valued slots are omitted. - for (key, slot) in changed_acc.changed_storage() { + for (key, slot) in changed_acc.storage.changed_slots() { entry.storage.insert((*key).into(), Delta::Added(slot.current.into())); } } else { // EIP-7702 can change code without creating the account, even if execution reverts. - if db_acc.code_hash != info.code_hash { - let original_account_code = load_account_code(db, &db_acc)?.unwrap_or_default(); + if original.code_hash != info.code_hash { + let original_account_code = load_account_code(db, original)?.unwrap_or_default(); let present_account_code = load_account_code(db, info)?.unwrap_or_default(); entry.code = Delta::changed(original_account_code, present_account_code); } // update _changed_ storage values - for (key, slot) in changed_acc.changed_storage() { + for (key, slot) in changed_acc.storage.changed_slots() { entry.storage.insert( (*key).into(), Delta::changed(slot.original.into(), slot.current.into()), @@ -541,26 +600,23 @@ pub fn populate_state_diff( } // check if the account was changed at all - if entry.storage.is_empty() && &db_acc == info && !changed_acc.selfdestructed { + if entry.storage.is_empty() && original == info && !changed_acc.selfdestructed { // clear the entry if the account was not changed state_diff.remove(addr); continue; } - entry.balance = if db_acc.balance == info.balance { + entry.balance = if original.balance == info.balance { Delta::Unchanged } else { - Delta::Changed(ChangedType { from: db_acc.balance, to: info.balance }) + Delta::changed(original.balance, info.balance) }; // this is relevant for the caller and contracts - entry.nonce = if db_acc.nonce == info.nonce { + entry.nonce = if original.nonce == info.nonce { Delta::Unchanged } else { - Delta::Changed(ChangedType { - from: U64::from(db_acc.nonce), - to: U64::from(info.nonce), - }) + Delta::changed(U64::from(original.nonce), U64::from(info.nonce)) }; } } @@ -571,7 +627,8 @@ pub fn populate_state_diff( #[cfg(test)] mod tests { use super::*; - use crate::tracing::types::{CallKind, CallTrace}; + use crate::tracing::types::CallTrace; + use alloy_primitives::U256; #[test] fn test_parity_suicide_simple_call() { @@ -584,7 +641,7 @@ mod tests { ..Default::default() }]; - let traces = ParityTraceBuilder::new(nodes, None, TracingInspectorConfig::default_parity()) + let traces = ParityTraceBuilder::new(nodes, PrecompileExclusion::ZeroValue) .into_transaction_traces(); assert_eq!(traces.len(), 2); @@ -648,7 +705,7 @@ mod tests { }, ]; - let traces = ParityTraceBuilder::new(nodes, None, TracingInspectorConfig::default_parity()) + let traces = ParityTraceBuilder::new(nodes, PrecompileExclusion::ZeroValue) .into_transaction_traces(); assert_eq!(traces.len(), 4); @@ -671,4 +728,51 @@ mod tests { assert_eq!(traces[3].trace_address, vec![0, 1]); assert!(traces[3].action.is_selfdestruct()); } + + #[test] + fn excluded_precompile_calls_take_their_nested_calls_along() { + let precompile = CallTrace { + depth: 1, + kind: CallKind::Call, + value: U256::from(1), + maybe_precompile: Some(true), + ..Default::default() + }; + let nodes = vec![ + CallTraceNode { + children: vec![1, 3], + idx: 0, + trace: CallTrace { depth: 0, ..Default::default() }, + ordering: vec![TraceMemberOrder::Call(0), TraceMemberOrder::Call(1)], + ..Default::default() + }, + CallTraceNode { + parent: Some(0), + children: vec![2], + idx: 1, + trace: precompile, + ordering: vec![TraceMemberOrder::Call(0)], + ..Default::default() + }, + CallTraceNode { + parent: Some(1), + idx: 2, + trace: CallTrace { depth: 2, ..Default::default() }, + ..Default::default() + }, + CallTraceNode { + parent: Some(0), + idx: 3, + trace: CallTrace { depth: 1, ..Default::default() }, + ..Default::default() + }, + ]; + + let traces = ParityTraceBuilder::new(nodes, PrecompileExclusion::CallAndStaticCall) + .into_transaction_traces(); + + let addresses: Vec<_> = traces.iter().map(|trace| trace.trace_address.clone()).collect(); + assert_eq!(addresses, [vec![], vec![0]]); + assert_eq!(traces[0].subtraces, 1); + } } diff --git a/crates/inspectors/src/tracing/config.rs b/crates/inspectors/src/tracing/config.rs index 89b262597..5cf6382a1 100644 --- a/crates/inspectors/src/tracing/config.rs +++ b/crates/inspectors/src/tracing/config.rs @@ -1,56 +1,11 @@ -use alloy_primitives::{U256, map::HashSet}; +use crate::tracing::builder::geth::MIN_STRUCT_LOG_JSON_LEN; +use alloy_primitives::map::HashSet; use alloy_rpc_types_trace::{ - geth::{ - CallConfig, FlatCallConfig, GethDefaultTracingOptions, PreStateConfig, - erc7562::Erc7562Config, - }, + geth::{CallConfig, GethDefaultTracingOptions, PreStateConfig}, parity::TraceType, }; use core::num::NonZeroU64; -use evm2::interpreter::opcode::OpCode; - -/// 256 bits each marking whether an opcode should be included into steps trace or not. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -#[must_use] -pub struct OpcodeFilter(U256); - -impl Default for OpcodeFilter { - fn default() -> Self { - Self::new() - } -} - -impl OpcodeFilter { - /// Returns a new [OpcodeFilter] that does not trace any opcodes. - #[inline] - pub const fn new() -> Self { - Self(U256::ZERO) - } - - /// Returns whether steps with given [OpCode] should be traced. - #[inline] - pub const fn is_enabled(&self, op: OpCode) -> bool { - self.0.bit(op.get() as usize) - } - - /// Enables tracing of given [OpCode]. - #[inline] - pub const fn enable(&mut self, op: OpCode) -> &mut Self { - self.0.set_bit(op.get() as usize, true); - self - } - - /// Enables tracing of given [OpCode]. - #[inline] - pub const fn enabled(mut self, op: OpCode) -> Self { - let index = op.get() as usize; - let mut limbs = self.0.into_limbs(); - let (limb, bit) = (index / 64, index % 64); - limbs[limb] |= 1 << bit; - self.0 = U256::from_limbs(limbs); - self - } -} +use evm2::{OpcodeSet, interpreter::opcode::OpCode}; /// Gives guidance to the [TracingInspector](crate::tracing::TracingInspector). /// @@ -80,18 +35,19 @@ pub struct TracingInspectorConfig { pub record_returndata_snapshots: bool, /// Optional filter for opcodes to record. If provided, only steps with opcode in this set will /// be recorded. - pub record_opcodes_filter: Option, - /// Whether to ignore precompile calls. - pub exclude_precompile_calls: bool, + pub record_opcodes_filter: Option, + /// Whether to record which calls target a precompile, so that trace builders can leave them + /// out. + pub record_precompile_calls: bool, /// Whether to record logs pub record_logs: bool, /// Whether to record immediate bytes for opcodes. pub record_immediate_bytes: bool, - /// Whether to record the deltas of each step: the memory it writes or an `MLOAD` reads, the - /// storage an `SSTORE` writes, and the remaining gas after returning from a call or gaining - /// gas. + /// Whether to record the memory each step writes or an `MLOAD` reads, and the storage an + /// `SSTORE` writes. /// - /// Required for parity `vmTrace`. + /// Required for parity `vmTrace` memory and storage. The remaining gas after returning from a + /// call or gaining gas is recorded whenever steps are enabled, independently of this option. pub record_step_deltas: bool, } @@ -108,7 +64,7 @@ impl TracingInspectorConfig { record_state_diff: true, record_returndata_snapshots: true, record_opcodes_filter: None, - exclude_precompile_calls: false, + record_precompile_calls: false, record_logs: true, record_immediate_bytes: true, record_step_deltas: true, @@ -126,7 +82,7 @@ impl TracingInspectorConfig { record_stack_snapshots: StackSnapshotType::None, record_state_diff: false, record_returndata_snapshots: false, - exclude_precompile_calls: false, + record_precompile_calls: false, record_logs: false, record_opcodes_filter: None, record_immediate_bytes: false, @@ -147,7 +103,7 @@ impl TracingInspectorConfig { record_stack_snapshots: StackSnapshotType::None, record_state_diff: false, record_returndata_snapshots: false, - exclude_precompile_calls: true, + record_precompile_calls: true, record_logs: false, record_opcodes_filter: None, record_immediate_bytes: false, @@ -167,7 +123,7 @@ impl TracingInspectorConfig { pub const fn parity_vm_trace() -> Self { Self::default_parity() .set_record_inputs(false) - .set_steps(true) + .steps() .set_bytecode(true) .set_stack_snapshots(StackSnapshotType::Pushes) .set_step_deltas(true) @@ -175,7 +131,8 @@ impl TracingInspectorConfig { /// Returns a config for geth style traces. /// - /// This config does _not_ record opcode level traces and is suited for `debug_traceTransaction` + /// This config records opcode-level steps with full stack snapshots and is suited for + /// `debug_traceTransaction` with the default struct logger. /// /// This will configure the default output of geth's default /// [StructLogTracer](alloy_rpc_types_trace::geth::DefaultFrame). @@ -189,7 +146,7 @@ impl TracingInspectorConfig { record_stack_snapshots: StackSnapshotType::Full, record_state_diff: true, record_returndata_snapshots: false, - exclude_precompile_calls: false, + record_precompile_calls: false, record_logs: false, record_opcodes_filter: None, record_immediate_bytes: false, @@ -221,7 +178,12 @@ impl TracingInspectorConfig { #[inline] pub fn from_geth_config(config: &GethDefaultTracingOptions) -> Self { Self { - step_limit: config.limit.and_then(NonZeroU64::new), + // The limit counts bytes of JSON, and a struct log takes at least that many, so the + // trace never needs more steps. + step_limit: config + .limit + .filter(|limit| *limit != 0) + .and_then(|limit| NonZeroU64::new(limit / MIN_STRUCT_LOG_JSON_LEN + 1)), record_memory_snapshots: config.is_memory_enabled(), record_stack_snapshots: if config.disable_stack.unwrap_or_default() { StackSnapshotType::None @@ -246,36 +208,6 @@ impl TracingInspectorConfig { .set_record_logs(config.with_log.unwrap_or_default()) } - /// Returns a config for geth's - /// [Erc7562Frame](alloy_rpc_types_trace::geth::erc7562::Erc7562Frame). - #[inline] - pub fn from_geth_erc7562_config(config: &Erc7562Config) -> Self { - Self::none() - .set_record_inputs(true) - // call tracer is similar parity tracer with optional support for logs - .set_record_logs(config.with_log.unwrap_or_default()) - // need memory snapshots for keccak preimages - .set_memory_snapshots(true) - // need stack snapshots for keccak preimages - .set_stack_snapshots(StackSnapshotType::Full) - // need state diffs to capture SLOAD values in accessedSlots.reads - .set_state_diffs(true) - .steps() - } - - /// Returns a config for geth's - /// [FlatCallTracer](alloy_rpc_types_trace::geth::call::FlatCallFrame). - /// - /// This returns [Self::default_parity] and sets - /// [TracingInspectorConfig::exclude_precompile_calls] if configured in the given - /// [FlatCallConfig] - #[inline] - pub fn from_flat_call_config(config: &FlatCallConfig) -> Self { - Self::default_parity() - // call tracer is similar parity tracer with optional support for logs - .set_exclude_precompile_calls(!config.include_precompiles.unwrap_or_default()) - } - /// Returns a config for geth's [PrestateTracer](alloy_rpc_types_trace::geth::PreStateFrame). /// /// Note: This currently returns [Self::none] because the prestate tracer result currently @@ -308,7 +240,7 @@ impl TracingInspectorConfig { self.record_stack_snapshots = other.record_stack_snapshots; self.record_state_diff |= other.record_state_diff; self.record_returndata_snapshots |= other.record_returndata_snapshots; - self.exclude_precompile_calls |= other.exclude_precompile_calls; + self.record_precompile_calls |= other.record_precompile_calls; self.record_logs |= other.record_logs; self.record_opcodes_filter = self.record_opcodes_filter.or(other.record_opcodes_filter); self.record_immediate_bytes |= other.record_immediate_bytes; @@ -322,14 +254,6 @@ impl TracingInspectorConfig { self } - /// Configure whether calls to precompiles should be ignored. - /// - /// If set to `true`, calls to precompiles without value transfers will be ignored. - pub const fn set_exclude_precompile_calls(mut self, exclude_precompile_calls: bool) -> Self { - self.exclude_precompile_calls = exclude_precompile_calls; - self - } - /// Disable recording of individual opcode level steps pub const fn disable_steps(self) -> Self { self.set_steps(false) @@ -446,11 +370,11 @@ impl TracingInspectorConfig { self.set_immediate_bytes(true) } - /// If [OpcodeFilter] is configured, returns whether the given opcode should be recorded. + /// If an opcode filter is configured, returns whether the given opcode should be recorded. /// Otherwise, always returns true. #[inline] pub fn should_record_opcode(&self, op: OpCode) -> bool { - self.record_opcodes_filter.as_ref().is_none_or(|filter| filter.is_enabled(op)) + self.record_opcodes_filter.as_ref().is_none_or(|filter| filter.contains(op.get())) } } @@ -537,8 +461,6 @@ mod tests { TracingInspectorConfig::all(), TracingInspectorConfig::default_parity(), TracingInspectorConfig::from_geth_call_config(&CallConfig::default()), - TracingInspectorConfig::from_geth_erc7562_config(&Erc7562Config::default()), - TracingInspectorConfig::from_flat_call_config(&FlatCallConfig::default()), ] { assert!(config.record_inputs); } @@ -569,7 +491,7 @@ mod tests { #[test] fn merge_step_limits_preserves_each_consumers_capture() { let limited = TracingInspectorConfig::from_geth_config(&GethDefaultTracingOptions { - limit: Some(2), + limit: Some(2 * MIN_STRUCT_LOG_JSON_LEN - 1), ..Default::default() }); let larger = TracingInspectorConfig { step_limit: NonZeroU64::new(4), ..limited }; @@ -622,17 +544,6 @@ mod tests { assert!(!config.record_state_diff); } - #[test] - fn test_flat_call_config() { - let config = FlatCallConfig { include_precompiles: Some(true), ..Default::default() }; - let config = TracingInspectorConfig::from_flat_call_config(&config); - assert!(!config.exclude_precompile_calls); - - let config = FlatCallConfig { include_precompiles: Some(false), ..Default::default() }; - let config = TracingInspectorConfig::from_flat_call_config(&config); - assert!(config.exclude_precompile_calls); - } - #[test] fn test_all_records_all_stack_snapshots() { // `all()` enables everything, so the stack snapshot type should be the most diff --git a/crates/inspectors/src/tracing/debug.rs b/crates/inspectors/src/tracing/debug.rs index da5707b39..7fee5d01a 100644 --- a/crates/inspectors/src/tracing/debug.rs +++ b/crates/inspectors/src/tracing/debug.rs @@ -1,18 +1,19 @@ use crate::tracing::{ - FourByteInspector, MuxInspector, TracingInspector, TracingInspectorConfig, TransactionContext, - final_refunded, + Erc7562Inspector, FourByteInspector, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, + MuxInspector, TracingInspector, TracingInspectorConfig, TransactionContext, + utils::transaction_info, }; use alloc::vec::Vec; use alloy_consensus::{Transaction, transaction::Recovered}; use alloy_primitives::{Address, Log, U256}; -use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::geth::{ - CallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerType, + CallConfig, FlatCallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, NoopFrame, PreStateConfig, StateGasTrace, erc7562::Erc7562Config, mux::MuxConfig, }; use evm2::{ - DatabaseError, EvmTypes, EvmTypesHost, Inspector, NoopInspector, TxResultWithState, + DatabaseError, EvmTypes, EvmTypesHost, Inspector, InspectorConfig, NoopInspector, + TxResultWithState, env::BlockEnv, evm::DynDatabase, interpreter::{Interpreter, Message, MessageResult}, @@ -45,9 +46,11 @@ pub enum DebugInspector { /// Mux tracer Mux(MuxInspector, MuxConfig), /// FlatCallTracer - FlatCallTracer(TracingInspector), + FlatCallTracer(TracingInspector, FlatCallConfig), /// Erc7562Tracer - Erc7562Tracer(TracingInspector, Erc7562Config), + Erc7562Tracer(Erc7562Inspector), + /// Keccak256PreimageTracer + Keccak256Preimage(Keccak256PreimageInspector), /// Default tracer Default(TracingInspector, GethDefaultTracingOptions), /// JS tracer @@ -66,11 +69,12 @@ impl DebugInspector { } Self::Noop(inspector) => Self::Noop(inspector.clone()), Self::StateGasTracer(inspector) => Self::StateGasTracer(inspector.clone()), - Self::Mux(inspector, config) => Self::Mux(inspector.clone(), config.clone()), - Self::FlatCallTracer(inspector) => Self::FlatCallTracer(inspector.clone()), - Self::Erc7562Tracer(inspector, config) => { - Self::Erc7562Tracer(inspector.clone(), config.clone()) + Self::Mux(inspector, config) => Self::Mux(inspector.try_clone()?, config.clone()), + Self::FlatCallTracer(inspector, config) => { + Self::FlatCallTracer(inspector.clone(), *config) } + Self::Erc7562Tracer(inspector) => Self::Erc7562Tracer(inspector.clone()), + Self::Keccak256Preimage(inspector) => Self::Keccak256Preimage(inspector.clone()), Self::Default(inspector, config) => Self::Default(inspector.clone(), *config), #[cfg(feature = "js-tracer")] Self::Js(inspector) => Self::Js(inspector.try_clone()?.into()), @@ -94,9 +98,7 @@ impl DebugInspector { Self::FourByte(FourByteInspector::default()) } GethDebugBuiltInTracerType::CallTracer => { - let config = tracer_config - .into_call_config() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)?; + let config = tracer_config.into_call_config()?; Self::CallTracer( TracingInspector::new(TracingInspectorConfig::from_geth_call_config( @@ -106,9 +108,7 @@ impl DebugInspector { ) } GethDebugBuiltInTracerType::PreStateTracer => { - let config = tracer_config - .into_pre_state_config() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)?; + let config = tracer_config.into_pre_state_config()?; Self::PreStateTracer( TracingInspector::new( @@ -122,36 +122,26 @@ impl DebugInspector { Self::StateGasTracer(NoopInspector::default()) } GethDebugBuiltInTracerType::MuxTracer => { - let config = tracer_config - .into_mux_config() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)?; + let config = tracer_config.into_mux_config()?; Self::Mux(MuxInspector::try_from_config(config.clone())?, config) } GethDebugBuiltInTracerType::FlatCallTracer => { - let flat_call_config = tracer_config - .into_flat_call_config() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)?; + let flat_call_config = tracer_config.into_flat_call_config()?; - Self::FlatCallTracer(TracingInspector::new( - TracingInspectorConfig::from_flat_call_config(&flat_call_config), - )) + Self::FlatCallTracer( + TracingInspector::new(TracingInspectorConfig::default_parity()), + flat_call_config, + ) } GethDebugBuiltInTracerType::Erc7562Tracer => { let config = if tracer_config.is_null() { Erc7562Config::default() } else { - tracer_config - .from_value() - .map_err(|_| DebugInspectorError::InvalidTracerConfig)? + tracer_config.from_value()? }; - Self::Erc7562Tracer( - TracingInspector::new( - TracingInspectorConfig::from_geth_erc7562_config(&config), - ), - config, - ) + Self::Erc7562Tracer(Erc7562Inspector::new(config)) } _ => { // Note: this match is non-exhaustive in case we need to add support for @@ -159,6 +149,10 @@ impl DebugInspector { return Err(DebugInspectorError::UnsupportedTracer); } }, + // geth looks a tracer up by name before it evaluates the string as JavaScript. + GethDebugTracerType::JsTracer(name) if name == KECCAK256_PREIMAGE_TRACER => { + Self::Keccak256Preimage(Keccak256PreimageInspector::default()) + } #[cfg(not(feature = "js-tracer"))] GethDebugTracerType::JsTracer(_) => { return Err(DebugInspectorError::JsTracerNotEnabled); @@ -193,13 +187,12 @@ impl DebugInspector { } Self::CallTracer(inspector, _) | Self::PreStateTracer(inspector, _) - | Self::FlatCallTracer(inspector) - | Self::Erc7562Tracer(inspector, _) + | Self::FlatCallTracer(inspector, _) | Self::Default(inspector, _) => inspector.fuse(), + Self::Erc7562Tracer(inspector) => inspector.fuse(), + Self::Keccak256Preimage(inspector) => inspector.clear(), Self::Noop(_) | Self::StateGasTracer(_) => {} - Self::Mux(inspector, config) => { - *inspector = MuxInspector::try_from_config(config.clone())?; - } + Self::Mux(inspector, _) => inspector.fuse()?, #[cfg(feature = "js-tracer")] Self::Js(inspector) => inspector.fuse()?, } @@ -207,15 +200,26 @@ impl DebugInspector { Ok(()) } + /// Sets the handle that interrupts the JavaScript tracers of this inspector, including those + /// in a mux tracer, see [`JsInspector::with_interrupt`](crate::tracing::js::JsInspector). + #[cfg(feature = "js-tracer")] + pub fn set_js_interrupt(&mut self, interrupt: &crate::tracing::js::JsInspectorInterrupt) { + match self { + Self::Js(inspector) => inspector.set_interrupt(interrupt.clone()), + Self::Mux(inspector, _) => inspector.set_js_interrupt(interrupt), + _ => {} + } + } + /// Sets the index the next recorded log receives, see /// [`TracingInspector::set_next_log_index`]. - pub const fn set_next_log_index(&mut self, index: usize) { + pub fn set_next_log_index(&mut self, index: usize) { match self { Self::CallTracer(inspector, _) | Self::PreStateTracer(inspector, _) - | Self::FlatCallTracer(inspector) - | Self::Erc7562Tracer(inspector, _) + | Self::FlatCallTracer(inspector, _) | Self::Default(inspector, _) => inspector.set_next_log_index(index), + Self::Erc7562Tracer(inspector) => inspector.set_next_log_index(index), Self::Mux(inspector, _) => inspector.set_next_log_index(index), _ => {} } @@ -237,20 +241,11 @@ impl DebugInspector { res: &TxResultWithState, db: &mut dyn DynDatabase, ) -> Result { - #[allow(clippy::needless_update)] - let tx_info = TransactionInfo { - hash: tx_context.as_ref().and_then(|c| c.tx_hash), - index: tx_context.as_ref().and_then(|c| c.tx_index.map(|i| i as u64)), - block_hash: tx_context.as_ref().and_then(|c| c.block_hash), - block_number: Some(block_env.number.saturating_to()), - base_fee: Some(block_env.basefee.saturating_to()), - ..Default::default() - }; + let tx_info = transaction_info::(tx_context.as_ref(), block_env); let res = match self { Self::FourByte(inspector) => FourByteFrame::from(&*inspector).into(), Self::CallTracer(inspector, config) => { - inspector.recover_runtime_oog_root(tx, &res.result); inspector.set_transaction_gas_limit(tx.gas_limit()); inspector.set_transaction_caller(tx.signer()); inspector @@ -271,36 +266,24 @@ impl DebugInspector { gas_used: res.result.tx_gas_used(), execution_gas_used: res.result.execution_gas_spent(), state_gas_used: res.result.state_gas_spent(), - gas_refund: final_refunded(&res.result), + gas_refund: res.result.final_refunded(), } .into(), Self::Mux(inspector, _) => { - inspector.finalize_transaction(tx, &res.result); - inspector - .try_into_mux_frame(res, db, tx_info) - .map_err(DebugInspectorError::Database)? - .into() + inspector.try_into_mux_frame(res, tx, block_env, tx_context, db)?.into() } - Self::FlatCallTracer(inspector) => { - inspector.recover_runtime_oog_root(tx, &res.result); + Self::FlatCallTracer(inspector, config) => { inspector.set_transaction_gas_limit(tx.gas_limit()); inspector.set_transaction_caller(tx.signer()); inspector .clone() - .into_parity_builder() + .into_flat_call_builder(config) .with_transaction_gas_used(res.result.tx_gas_used()) .into_localized_transaction_traces(tx_info) .into() } - Self::Erc7562Tracer(inspector, config) => { - inspector.set_transaction_gas_limit(tx.gas_limit()); - inspector.set_transaction_caller(tx.signer()); - inspector - .geth_builder() - .geth_erc7562_traces(config.clone(), res.result.tx_gas_used(), db) - .map_err(DebugInspectorError::Database)? - .into() - } + Self::Erc7562Tracer(inspector) => inspector.frame(tx, &res.result)?.into(), + Self::Keccak256Preimage(inspector) => GethTrace::JS(inspector.json_result()?), Self::Default(inspector, config) => { inspector.set_transaction_gas_limit(tx.gas_limit()); inspector.set_transaction_caller(tx.signer()); @@ -324,7 +307,7 @@ impl DebugInspector { Some(( gas.execution_gas_spent(), gas.state_gas_spent(), - final_refunded(gas), + gas.final_refunded(), )), res.result.output.clone(), *config, @@ -349,14 +332,15 @@ macro_rules! delegate { Self::FourByte($insp) => Inspector::::$method($insp, $($arg),*), Self::CallTracer($insp, _) => Inspector::::$method($insp, $($arg),*), Self::PreStateTracer($insp, _) => Inspector::::$method($insp, $($arg),*), - Self::FlatCallTracer($insp) => Inspector::::$method($insp, $($arg),*), - Self::Erc7562Tracer($insp, _) => Inspector::::$method($insp, $($arg),*), + Self::FlatCallTracer($insp, _) => Inspector::::$method($insp, $($arg),*), + Self::Erc7562Tracer($insp) => Inspector::::$method($insp, $($arg),*), Self::Default($insp, _) => Inspector::::$method($insp, $($arg),*), + Self::Keccak256Preimage($insp) => Inspector::::$method($insp, $($arg),*), Self::Noop($insp) => Inspector::::$method($insp, $($arg),*), Self::StateGasTracer($insp) => Inspector::::$method($insp, $($arg),*), Self::Mux($insp, _) => Inspector::::$method($insp, $($arg),*), #[cfg(feature = "js-tracer")] - Self::Js($insp) => Inspector::::$method(&mut **$insp, $($arg),*), + Self::Js($insp) => Inspector::::$method($insp, $($arg),*), } }; } @@ -366,6 +350,10 @@ impl Inspector for DebugInspector { delegate!(self => inspector.initialize_interp(interp)) } + fn inspector_config(&self) -> InspectorConfig { + delegate!(self => inspector.inspector_config()) + } + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { delegate!(self => inspector.step(interp)) } @@ -427,8 +415,8 @@ impl Inspector for DebugInspector { #[derive(Debug, Error)] pub enum DebugInspectorError { /// Invalid tracer configuration - #[error("invalid tracer config")] - InvalidTracerConfig, + #[error("invalid tracer config: {0}")] + InvalidTracerConfig(#[from] serde_json::Error), /// Unsupported tracer #[error("unsupported tracer")] UnsupportedTracer, @@ -438,6 +426,12 @@ pub enum DebugInspectorError { /// Error from MuxInspector #[error(transparent)] MuxInspector(#[from] crate::tracing::MuxError), + /// The keccak256 preimage tracer reached its limit of recorded bytes + #[error(transparent)] + Keccak256Preimage(#[from] crate::tracing::Keccak256PreimageLimitExceeded), + /// The erc7562 tracer reached its limit of recorded preimage bytes or failed to read the state + #[error(transparent)] + Erc7562(#[from] crate::tracing::Erc7562Error), /// Error from JS inspector #[cfg(feature = "js-tracer")] #[error(transparent)] diff --git a/crates/inspectors/src/tracing/erc7562.rs b/crates/inspectors/src/tracing/erc7562.rs new file mode 100644 index 000000000..e3937d249 --- /dev/null +++ b/crates/inspectors/src/tracing/erc7562.rs @@ -0,0 +1,433 @@ +//! ERC-7562 tracing inspector +//! +//! The erc7562Tracer reports a call tree in which every frame also lists what its code did: the +//! opcodes it ran, the storage slots it accessed and the code of other accounts it inspected. The +//! root frame lists the preimages of every `KECCAK256` of the transaction. +//! +//! This inspector follows geth's erc7562Tracer. The call tree, with its inputs, outputs and logs, +//! comes from a [`TracingInspector`] that records calls only. Each instruction updates the +//! counters of the frame that runs it as the instruction is about to execute, reading the stack, +//! the memory and the state like geth does. A step that fails counts like any other. +//! +//! A frame that fails at a `KECCAK256` still leaves its preimage, and a preimage can reach 1 MiB of +//! zero padding for a few hundred gas, so the [`TraceLimits`] of the inspector bound the bytes of +//! the preimages it retains, which geth does not. [`DEFAULT_MAX_RECORDED_BYTES`] is the default. +//! +//! See also + +use crate::tracing::{ + DEFAULT_MAX_RECORDED_BYTES, GethTraceBuilder, TraceLimits, TracingInspector, + TracingInspectorConfig, keccak::memory_copy_padded, limits::TraceBudget, +}; +use alloc::{borrow::Cow, collections::BTreeSet, format, vec::Vec}; +use alloy_consensus::{Transaction, transaction::Recovered}; +use alloy_primitives::{ + Address, B256, Bytes, Log, U256, + map::{Entry, HashMap}, +}; +use alloy_rpc_types_trace::geth::{ + CallConfig, + erc7562::{AccessedSlots, ContractSize, Erc7562Config, Erc7562Frame}, +}; +use evm2::{ + DatabaseError, EvmTypes, Inspector, TxResultExt, + evm::DbResult, + interpreter::{InstrStop, Interpreter, Message, MessageResult, opcode::op}, + utils::WordExt, +}; +use thiserror::Error; + +/// The result of an [`Erc7562Inspector`] would be wrong. +#[derive(Clone, Debug, Error)] +pub enum Error { + /// The inspector reached its byte limit, so the result would miss preimages. + #[error("erc7562Tracer reached its limit of recorded preimage bytes")] + LimitExceeded, + /// The inspector could not read the state that an instruction addressed, so a code size or a + /// slot value would be wrong. + #[error("database error {0:?}")] + Database(DatabaseError), +} + +/// Inspector that collects what geth's `erc7562Tracer` reports for a transaction. +#[derive(Clone, Debug)] +pub struct Erc7562Inspector { + /// Whether the result lists logs. + with_log: bool, + /// The opcodes that the used opcode counts leave out, one bit per opcode. + ignored: U256, + /// Records the call tree. + calls: TracingInspector, + /// What each call did, indexed like the nodes of `calls`. + frames: Vec, + /// The preimages of the `KECCAK256` operations of the transaction, sorted. + keccak: BTreeSet, + /// The bytes of the preimages. + budget: TraceBudget, + /// The first error of a read of the state, which the result reports. + error: Option, + /// The instruction that ran last, in any frame. + last: Option, +} + +/// What a call did. +#[derive(Clone, Debug, Default)] +struct FrameCounters { + /// The storage slots the call accessed. + accessed_slots: AccessedSlots, + /// The accounts whose code an `EXTCODE*` instruction inspected, in order. + ext_code_access_info: Vec
, + /// How often the call ran each opcode that is not ignored. + used_opcodes: HashMap, + /// The code size of the accounts that `EXTCODE*` and call instructions addressed. + contract_size: HashMap, +} + +/// The instruction that ran last. +#[derive(Clone, Copy, Debug)] +struct LastStep { + /// The opcode. + op: u8, + /// The top of the stack before the instruction, if the stack had an item. + top: Option, +} + +impl Erc7562Inspector { + /// Returns an inspector that collects the result of the given tracer config. + pub fn new(config: Erc7562Config) -> Self { + let with_log = config.with_log.unwrap_or_default(); + let ignored_opcodes = if config.ignored_opcodes.is_empty() { + (op::PUSH0..=op::SWAP16).chain(DEFAULT_IGNORED_OPCODES).collect() + } else { + config.ignored_opcodes + }; + let mut ignored = U256::ZERO; + for op in ignored_opcodes { + ignored.set_bit(usize::from(op), true); + } + let calls = + TracingInspector::new(TracingInspectorConfig::from_geth_call_config(&CallConfig { + with_log: Some(with_log), + ..Default::default() + })); + let limits = + TraceLimits::default().set_max_recorded_bytes(Some(DEFAULT_MAX_RECORDED_BYTES)); + Self { + with_log, + ignored, + calls, + frames: Vec::new(), + keccak: BTreeSet::new(), + budget: TraceBudget { limits, ..Default::default() }, + error: None, + last: None, + } + } + + /// Bounds the bytes of the preimages that the inspector retains by the given limits, which + /// default to [`DEFAULT_MAX_RECORDED_BYTES`]. + pub const fn with_limits(mut self, limits: TraceLimits) -> Self { + self.budget.limits = limits; + self + } + + /// Returns whether a preimage was omitted after reaching the byte limit. + pub const fn limit_exceeded(&self) -> bool { + self.budget.exceeded() + } + + /// Clears what the inspector collected, to trace the next transaction. + pub fn fuse(&mut self) { + self.calls.fuse(); + self.frames.clear(); + self.keccak.clear(); + self.budget.recorded = 0; + self.budget.exceeded = false; + self.error = None; + self.last = None; + } + + /// Sets the index the next recorded log receives, see + /// [`TracingInspector::set_next_log_index`]. + pub const fn set_next_log_index(&mut self, index: usize) { + self.calls.set_next_log_index(index); + } + + /// Returns the result of the tracer for a transaction that ran with this inspector. + /// + /// Fails if the byte limit was reached, because the result would miss preimages, or if a read + /// of the state failed, because a code size or a slot value would be wrong. + pub fn frame( + &mut self, + tx: &Recovered, + result: &TxResultExt, + ) -> Result { + if self.budget.exceeded() { + return Err(Error::LimitExceeded); + } + if let Some(error) = &self.error { + return Err(Error::Database(error.clone())); + } + self.calls.set_transaction_gas_limit(tx.gas_limit()); + self.calls.set_transaction_caller(tx.signer()); + let nodes = self.calls.traces.nodes(); + + // Parents precede their children, so a frame knows whether its parent shows logs. + let mut frames = Vec::<(bool, Erc7562Frame)>::with_capacity(nodes.len()); + for (idx, node) in nodes.iter().enumerate() { + let counters = self.frames.get(idx).cloned().unwrap_or_default(); + let logs_visible = self.with_log + && !node.trace.is_error() + && node.parent.is_none_or(|parent| frames[parent].0); + let call = node.geth_empty_call_frame(logs_visible); + let frame = Erc7562Frame { + call_frame_type: GethTraceBuilder::convert_call_kind(node.kind()), + from: call.from, + gas: call.gas.to(), + gas_used: if idx == 0 { result.tx_gas_used() } else { call.gas_used.to() }, + to: call.to, + input: call.input, + output: call.output, + error: call.error, + revert_reason: call.revert_reason, + logs: call.logs, + value: call.value, + accessed_slots: counters.accessed_slots, + ext_code_access_info: counters + .ext_code_access_info + .iter() + .map(|address| format!("{address:?}")) + .collect(), + used_opcodes: counters.used_opcodes, + contract_size: counters.contract_size, + // geth's OnExit checks ErrOutOfGas / ErrCodeStoreOutOfGas, not the bare + // ErrGasUintOverflow of an oversized memory operand. The root is excluded. + out_of_gas: idx != 0 + && node.status().is_some_and(|status| { + status.is_out_of_gas() && status != InstrStop::InvalidOperandOOG + }), + keccak: if idx == 0 { self.keccak.iter().cloned().collect() } else { Vec::new() }, + calls: Vec::new(), + }; + frames.push((logs_visible, frame)); + } + + // Children are attached from the back, which reverses their order. + loop { + let (_, mut frame) = frames.pop().expect("the root frame exists"); + frame.calls.reverse(); + match nodes[frames.len()].parent { + Some(parent) => frames[parent].1.calls.push(frame), + None => return Ok(frame), + } + } + } + + /// Adds the counters of the call a hook started. + fn push_frame(&mut self) { + let idx = self.calls.last_trace_idx(); + if self.frames.len() <= idx { + self.frames.resize_with(idx + 1, FrameCounters::default); + } + } + + /// Returns the data that a `KECCAK256` with these operands hashes, if geth records it. + fn preimage<'a, T: EvmTypes>( + interp: &'a Interpreter<'_, '_, T>, + offset: Option, + length: Option, + ) -> Option> { + // The operands are truncated to 64 bits, and geth fails on a negative one. + let (offset, length) = (offset?.wrapping_to::(), length?.wrapping_to::()); + i64::try_from(offset).ok()?; + i64::try_from(length).ok()?; + memory_copy_padded(interp.memory().as_slice(), offset, length) + } + + /// Returns the size of the code of the account, without loading the account. + fn code_size( + interp: &mut Interpreter<'_, '_, T>, + address: &Address, + ) -> DbResult { + Ok(interp.host().state_mut().code_untracked(address)?.len() as u64) + } +} + +/// The opcodes besides `PUSH0` to `SWAP16` that the used opcode counts leave out by default. +const DEFAULT_IGNORED_OPCODES: [u8; 16] = [ + op::POP, + op::ADD, + op::SUB, + op::MUL, + op::DIV, + op::EQ, + op::LT, + op::GT, + op::SLT, + op::SGT, + op::SHL, + op::SHR, + op::AND, + op::OR, + op::NOT, + op::ISZERO, +]; + +const fn is_ext(op: u8) -> bool { + matches!(op, op::EXTCODEHASH | op::EXTCODESIZE | op::EXTCODECOPY) +} + +const fn is_call(op: u8) -> bool { + matches!(op, op::CALL | op::CALLCODE | op::DELEGATECALL | op::STATICCALL) +} + +/// Keeps the first error of a read of the state, and returns the zero that the read reports. +fn record(first: &mut Option, error: DatabaseError) -> V { + first.get_or_insert(error); + V::default() +} + +impl Inspector for Erc7562Inspector { + fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { + self.calls.initialize_interp(interp); + } + + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { + let op = interp.opcode(); + // The stack can be too short if the instruction is about to fail. + let (top, second) = match *interp.stack().as_slice() { + [.., second, top] => (Some(top), Some(second)), + [top] => (Some(top), None), + [] => (None, None), + }; + // A return does not leave its frame's last instruction to the frame that resumes. + let last = if matches!(op, op::RETURN | op::REVERT) { None } else { self.last }; + let frame = &mut self.frames[self.calls.last_trace_idx()]; + + // Only the last `EXTCODE*` instruction per address would matter, but geth lists them all, + // except an `EXTCODESIZE` that `ISZERO` consumes. + if let Some(last) = last + && is_ext(last.op) + && let Some(top) = last.top + && !(last.op == op::EXTCODESIZE && op == op::ISZERO) + { + frame.ext_code_access_info.push(top.to_address()); + } + if last.is_some_and(|last| last.op == op::GAS) && !is_call(op) { + *frame.used_opcodes.entry(op::GAS).or_default() += 1; + } + if op != op::GAS && !self.ignored.bit(usize::from(op)) { + *frame.used_opcodes.entry(op).or_default() += 1; + } + + let slots = &mut frame.accessed_slots; + match op { + op if is_ext(op) || is_call(op) => { + // The code size of the account that the instruction addresses. + if let Some(word) = if is_ext(op) { top } else { second } { + let address = word.to_address(); + if let Entry::Vacant(entry) = frame.contract_size.entry(address) { + let contract_size = Self::code_size(interp, &address) + .unwrap_or_else(|error| record(&mut self.error, error)); + entry.insert(ContractSize { contract_size, opcode: op }); + } + } + } + op::SLOAD => { + // The value of a slot that the frame did not write or read yet. + if let Some(slot) = top.map(B256::from) + && !slots.reads.contains_key(&slot) + && !slots.writes.contains_key(&slot) + { + let address = interp.message().destination; + let value = interp + .host() + .state_mut() + .storage_slot_untracked(&address, &slot.into()) + .unwrap_or_else(|error| record(&mut self.error, error)); + slots.reads.entry(slot).or_default().push(value.into()); + } + } + op::SSTORE => { + if let Some(slot) = top { + *slots.writes.entry(slot.into()).or_default() += 1; + } + } + op::TLOAD => { + if let Some(slot) = top { + *slots.transient_reads.entry(slot.into()).or_default() += 1; + } + } + op::TSTORE => { + if let Some(slot) = top { + *slots.transient_writes.entry(slot.into()).or_default() += 1; + } + } + op::KECCAK256 => { + if !self.budget.exceeded() + && let Some(preimage) = Self::preimage(interp, top, second) + && !self.keccak.contains(&*preimage) + && self.budget.reserve(preimage.len()) + { + self.keccak.insert(Bytes::from(preimage.into_owned())); + } + } + _ => {} + } + // Only `EXTCODE*` and `GAS` look at the instruction that ran before them. + self.last = (is_ext(op) || op == op::GAS).then_some(LastStep { op, top }); + self.budget.halt(interp, true); + } + + fn log(&mut self, log: &Log, host: &mut T::Host<'_>) { + Inspector::::log(&mut self.calls, log, host); + } + + fn call( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + let result = self.calls.call(interp, message); + self.push_frame(); + result + } + + fn call_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + self.calls.call_end(interp, message, result); + } + + fn create( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &mut Message, + ) -> Option> { + let result = self.calls.create(interp, message); + self.push_frame(); + result + } + + fn create_end( + &mut self, + interp: &mut Interpreter<'_, '_, T>, + message: &Message, + result: &mut MessageResult, + ) { + self.calls.create_end(interp, message, result); + } + + fn selfdestruct( + &mut self, + contract: &Address, + target: &Address, + value: &U256, + host: &mut T::Host<'_>, + ) { + Inspector::::selfdestruct(&mut self.calls, contract, target, value, host); + } +} diff --git a/crates/inspectors/src/tracing/fourbyte.rs b/crates/inspectors/src/tracing/fourbyte.rs index 092d9dd56..3ce2c33d6 100644 --- a/crates/inspectors/src/tracing/fourbyte.rs +++ b/crates/inspectors/src/tracing/fourbyte.rs @@ -23,10 +23,11 @@ //! See also use alloc::format; -use alloy_primitives::{Selector, hex, map::HashMap}; +use alloy_primitives::{Selector, map::HashMap}; use alloy_rpc_types_trace::geth::FourByteFrame; use evm2::{ - EvmTypes, Inspector, + EvmTypes, Inspector, InspectorConfig, + evm::PrewarmSet, interpreter::{Interpreter, Message, MessageResult}, }; @@ -35,6 +36,11 @@ use evm2::{ pub struct FourByteInspector { /// The map of SELECTOR to number of occurrences entries inner: HashMap<(Selector, usize), u64>, + /// The precompiles the host warms, read on the first call. + /// + /// Addresses a provider only dispatches to on demand, such as Tempo's token precompiles, + /// count like contracts. + precompiles: Option, } impl FourByteInspector { @@ -45,14 +51,27 @@ impl FourByteInspector { } impl Inspector for FourByteInspector { + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::without_steps() + } + fn call( &mut self, interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { // Skip precompile invocations, matching geth's 4byteTracer. - if let Some((selector, calldata)) = message.input.split_first_chunk() - && !interp.host().precompiles().contains(&message.call_target) + if message.input.len() >= 4 + && !self + .precompiles + .get_or_insert_with(|| { + let mut precompiles = PrewarmSet::new(); + interp.host().precompiles().warm_addresses(&mut precompiles); + precompiles + }) + .is_warm(&message.call_target) + && let Some((selector, calldata)) = + message.input.as_bytes(interp.memory()).split_first_chunk() { let selector = Selector::from(*selector); let calldata_size = calldata.len(); @@ -75,7 +94,7 @@ impl From<&FourByteInspector> for FourByteFrame { .inner .iter() .map(|((selector, calldata_size), count)| { - let key = format!("0x{}-{}", hex::encode(selector), *calldata_size); + let key = format!("0x{selector:x}-{}", *calldata_size); (key, *count) }) .collect(), diff --git a/crates/inspectors/src/tracing/js/bindings.rs b/crates/inspectors/src/tracing/js/bindings.rs index 905c10ddc..c1e3ec46b 100644 --- a/crates/inspectors/src/tracing/js/bindings.rs +++ b/crates/inspectors/src/tracing/js/bindings.rs @@ -7,7 +7,6 @@ use crate::tracing::{ call_kind_js_string, to_bigint, to_uint8_array, to_uint8_array_value, uint8_array_from_block, }, - tx_state::TxState, types::CallKind, }; use alloc::{ @@ -34,9 +33,10 @@ use core::{ }; use evm2::{ bytecode::Bytecode, - evm::{AccountInfo, DbResult, DynDatabase, State}, + evm::{AccountInfo, DbResult, DynDatabase, PendingState, State}, interpreter::{ Word, + instructions::{decode_pair, decode_single}, opcode::{ OpCode, op as opcode, op::{PUSH0, PUSH32}, @@ -112,7 +112,7 @@ fn state_fn( .build() } -fn type_error(message: String) -> JsError { +pub(crate) fn type_error(message: String) -> JsError { JsError::from_native(JsNativeError::typ().with_message(message)) } @@ -241,6 +241,8 @@ pub(crate) struct PreStep<'a> { pub(crate) pc: u64, /// Opcode to be executed pub(crate) op: u8, + /// Byte after the opcode, or 0 if the bytecode ends there + pub(crate) immediate: u8, /// Remaining gas before step execution pub(crate) gas_remaining: u64, /// Stack before step execution @@ -272,6 +274,24 @@ pub(crate) struct StepInfo<'a> { pub(crate) call_id: u64, } +impl Default for StepInfo<'_> { + /// The log of a tracer whose step hook never ran: everything is zero. + fn default() -> Self { + static NO_INPUT: Bytes = Bytes::new(); + Self { + cost: 0, + refund: 0, + depth: 0, + error: None, + caller: Address::ZERO, + contract: Address::ZERO, + value: U256::ZERO, + input: &NO_INPUT, + call_id: 0, + } + } +} + /// Revokes the JS log object's access to the interpreter's stack and memory when dropped. #[derive(Debug)] #[must_use] @@ -312,8 +332,15 @@ impl ReusableCallFrame { let get_to = state_fn(ctx, state.clone(), 0, |state, _, ctx| { address_to_uint8_array_value(state.borrow().contract, ctx) }); - let get_value = - state_fn(ctx, state.clone(), 0, |state, _, _| Ok(to_bigint(state.borrow().value))); + // Geth passes no value for STATICCALL, which `getValue()` reports as undefined. + let get_value = state_fn(ctx, state.clone(), 0, |state, _, _| { + let state = state.borrow(); + Ok(if matches!(state.kind, FrameKind::Call(CallKind::StaticCall)) { + JsValue::undefined() + } else { + to_bigint(state.value) + }) + }); let get_input = state_fn(ctx, state.clone(), 0, |state, _, ctx| { to_uint8_array_value(state.borrow().input.clone(), ctx) }); @@ -456,10 +483,10 @@ impl ReusableEvmDb { self.with_scope(&mut StateDbReader { state }, f) } - /// Borrows a transaction's materialized changes and backing database during a callback. + /// Borrows a transaction's pending state and backing database during a callback. pub(crate) fn with_changes_scope( &self, - changes: &TxState, + changes: &PendingState, db: &mut dyn DynDatabase, f: impl FnOnce() -> R, ) -> R { @@ -527,7 +554,7 @@ impl EvmDbReader for StateDbReader<'_, '_> { } fn read_code(&mut self, address: &Address) -> DbResult { - self.state.account(address)?.load_code() + self.state.code_untracked(address) } fn read_state(&mut self, address: &Address, slot: &Word) -> DbResult { @@ -536,7 +563,7 @@ impl EvmDbReader for StateDbReader<'_, '_> { } struct ChangesDbReader<'a> { - changes: &'a TxState, + changes: &'a PendingState, db: &'a mut dyn DynDatabase, } @@ -557,13 +584,16 @@ impl ChangesDbReader<'_> { impl EvmDbReader for ChangesDbReader<'_> { fn read_basic(&mut self, address: &Address) -> DbResult> { - if let Some(account) = self.changes.accounts.get(address) { - return Ok(account.current.clone()); + if let Some(account) = self.changes.account_changes_for(address) { + return Ok(account.current.cloned()); } self.db.get_account(address) } fn read_code(&mut self, address: &Address) -> DbResult { + if let Some((_, code)) = self.changes.account_changes_for(address).and_then(|a| a.code) { + return Ok(code.clone()); + } let Some(info) = self.read_basic(address)? else { return Ok(Bytecode::default()); }; @@ -620,12 +650,12 @@ pub(crate) struct StepSnapshot { impl StepSnapshot { /// Records everything about a step that must be captured before the opcode executes. pub(crate) fn record(&mut self, step: PreStep<'_>) { - let PreStep { pc, op, gas_remaining, stack, memory } = step; + let PreStep { pc, op, immediate, gas_remaining, stack, memory } = step; let state = self; state.pc = pc; state.op = op; state.gas_remaining = gas_remaining; - state.stack.record(op, stack); + state.stack.record(op, immediate, stack); state.memory.record(op, stack, memory); } } @@ -702,6 +732,9 @@ impl OpcodeNames { /// An opcode only touches its top `inputs` items, so the pre-execution stack is the post-execution /// stack below those items followed by the saved inputs. This holds for halted opcodes too, which /// pop at most `inputs` items. +/// +/// SWAPN and EXCHANGE declare no inputs but swap items at depths decoded from their immediate, so +/// they save every item down to the deepest one they touch. #[derive(Debug, Default)] struct StackView { /// Pre-execution length. @@ -713,9 +746,17 @@ struct StackView { } impl StackView { - fn record(&mut self, op: u8, stack: &[U256]) { + fn record(&mut self, op: u8, immediate: u8, stack: &[U256]) { self.len = stack.len(); - let inputs = OpCode::new(op).map_or(0, |op| op.inputs() as usize).min(stack.len()); + let inputs = match op { + // An invalid immediate halts the opcode before it touches the stack. + opcode::SWAPN => decode_single(immediate).map_or(0, |n| usize::from(n) + 1), + opcode::EXCHANGE => { + decode_pair(immediate).map_or(0, |(n, m)| usize::from(n.max(m)) + 1) + } + _ => OpCode::new(op).map_or(0, |op| op.inputs() as usize), + } + .min(stack.len()); self.saved.clear(); self.saved.extend_from_slice(&stack[stack.len() - inputs..]); } @@ -726,16 +767,18 @@ impl StackView { } /// Returns the item `idx` positions from the top of the pre-execution stack. - fn peek(&self, idx: usize) -> JsResult { + fn peek(&self, idx: i64) -> JsResult { let Some(post) = self.post else { return Err(type_error("tracer accessed stack outside of a callback".to_string())); }; let saved = self.saved.len(); - let item = if idx < saved { - Some(self.saved[saved - 1 - idx]) - } else { - (idx < self.len).then(|| post.get(self.len - 1 - idx).copied()).flatten() - }; + let item = usize::try_from(idx).ok().and_then(|idx| { + if idx < saved { + Some(self.saved[saved - 1 - idx]) + } else { + (idx < self.len).then(|| post.get(self.len - 1 - idx).copied()).flatten() + } + }); item.ok_or_else(|| { type_error(format!( "tracer accessed out of bound stack: size {}, index {idx}", @@ -813,7 +856,7 @@ fn memory_write_range(op: u8, stack: &[U256]) -> Option<(usize, usize)> { let peek = |n: usize| stack.len().checked_sub(n + 1).map(|i| stack[i]); let (offset, size) = match op { opcode::MSTORE => (peek(0)?, U256::from(32)), - opcode::MSTORE8 => (peek(0)?, U256::from(1)), + opcode::MSTORE8 => (peek(0)?, U256::ONE), opcode::MCOPY | opcode::CALLDATACOPY | opcode::CODECOPY | opcode::RETURNDATACOPY => { (peek(0)?, peek(2)?) } @@ -865,8 +908,7 @@ fn build_step_stack_object(state: Shared, ctx: &mut Context) -> Js Ok(JsValue::from(state.borrow().stack.len())) }); let peek = state_fn(ctx, state, 1, |state, args, ctx| { - let len = state.borrow().stack.len(); - let idx = parse_stack_index(args.get_or_undefined(0), len, ctx)?; + let idx = to_int64(args.get_or_undefined(0), ctx)?; Ok(to_bigint(state.borrow().stack.peek(idx)?)) }); @@ -881,37 +923,35 @@ fn build_step_memory_object(state: Shared, ctx: &mut Context) -> J Ok(JsValue::from(state.borrow().memory.len() as u64)) }); let slice = state_fn(ctx, state.clone(), 2, |state, args, ctx| { - let len = state.borrow().memory.len(); - let start = parse_memory_offset(args.get_or_undefined(0), "start", ctx)?; - let end = parse_memory_offset(args.get_or_undefined(1), "end", ctx)?; - if end == start { + let begin = to_int64(args.get_or_undefined(0), ctx)?; + let end = to_int64(args.get_or_undefined(1), ctx)?; + if end == begin { return uint8_array_from_block(AlignedVec::from_iter(0, core::iter::empty()), ctx); } - if end < start { - return Err(memory_out_of_bounds_error(len, start, end.saturating_sub(start))); + if end < begin || begin < 0 { + return Err(type_error(format!( + "tracer accessed out of bound memory: offset {begin}, end {end}" + ))); } - if end > len { - let padding = end - len; - if padding > MEMORY_PAD_LIMIT { - return Err(type_error(format!( - "reached limit for padding memory slice: {padding}" - ))); - } + let len = state.borrow().memory.len(); + let padding = end - len as i64; + if padding > MEMORY_PAD_LIMIT { + return Err(type_error(format!("reached limit for padding memory slice: {padding}"))); } - let bytes = state.borrow().memory.bytes(start..end)?; + let bytes = state.borrow().memory.bytes(begin as usize..end as usize)?; uint8_array_from_block(bytes, ctx) }); let get_uint = state_fn(ctx, state, 1, |state, args, ctx| { let len = state.borrow().memory.len(); - let offset = parse_memory_index(args.get_or_undefined(0), "offset", len, ctx)?; - let Some(end) = offset.checked_add(32) else { - return Err(memory_out_of_bounds_error(len, offset, 32)); - }; - if end > len { - return Err(memory_out_of_bounds_error(len, offset, 32)); + let offset = to_int64(args.get_or_undefined(0), ctx)?; + if offset < 0 || offset.saturating_add(32) > len as i64 { + return Err(type_error(format!( + "tracer accessed out of bound memory: available {len}, offset {offset}, size 32" + ))); } - let bytes = state.borrow().memory.bytes(offset..end)?; - uint8_array_from_block(bytes, ctx) + let offset = offset as usize; + let bytes = state.borrow().memory.bytes(offset..offset + 32)?; + Ok(to_bigint(U256::from_be_slice(bytes.as_slice()))) }); obj.set(js_string!("slice"), slice, false, ctx)?; @@ -944,60 +984,28 @@ fn build_step_contract_object( Ok(obj) } -fn parse_stack_index(value: &JsValue, len: usize, ctx: &mut Context) -> JsResult { - let index = value.to_numeric_number(ctx)?; - if !index.is_finite() || index < 0. || index > usize::MAX as f64 { - return Err(type_error(format!( - "tracer accessed out of bound stack: size {len}, index {index}" - ))); - } - Ok(index as usize) -} - -fn invalid_memory_index_error(name: &str, index: impl core::fmt::Display) -> JsError { - type_error(format!("invalid memory {name}: {index}")) -} - -const MEMORY_PAD_LIMIT: usize = 1024 * 1024; - -fn memory_out_of_bounds_error(len: usize, offset: usize, size: usize) -> JsError { - type_error(format!( - "tracer accessed out of bound memory: available {len}, offset {offset}, size {size}" - )) -} - -fn parse_memory_index( - value: &JsValue, - name: &str, - len: usize, - ctx: &mut Context, -) -> JsResult { - let index = parse_memory_offset(value, name, ctx)?; - if index > len { - return Err(invalid_memory_index_error(name, index)); - } - Ok(index) -} - -fn parse_memory_offset(value: &JsValue, name: &str, ctx: &mut Context) -> JsResult { - if value.is_undefined() { - return Err(invalid_memory_index_error(name, "undefined")); - } - if let Some(index) = value.as_number() - && (!index.is_finite() || index < 0.) - { - return Err(invalid_memory_index_error(name, index)); - } - if let Some(index) = value.as_bigint() { - // Boa's `ToIndex` rejects BigInt, but stack-derived tracer values are BigInt. - let index = index.to_string(); - index.parse::().map_err(|_| invalid_memory_index_error(name, &index)) +/// Converts a JS argument to the `int64` goja passes to a Go callback. +/// +/// NaN and infinities become 0, fractions truncate toward zero and values outside the `int64` +/// range become `i64::MIN`, as Go's float conversion does on amd64. +fn to_int64(value: &JsValue, ctx: &mut Context) -> JsResult { + // Boa's `ToNumber` rejects BigInt, but stack-derived tracer values are BigInt. Geth's bigint + // objects reach goja through their `Number` value, so go through a float as well. + let number = match value.as_bigint() { + Some(value) => value.to_f64(), + None => value.to_number(ctx)?, + }; + Ok(if !number.is_finite() { + 0 + } else if (-(2f64.powi(63))..2f64.powi(63)).contains(&number) { + number as i64 } else { - let index = value.to_index(ctx)?; - usize::try_from(index).map_err(|_| invalid_memory_index_error(name, index)) - } + i64::MIN + }) } +const MEMORY_PAD_LIMIT: i64 = 1024 * 1024; + /// Represents the contract object #[derive(Clone, Debug, Default)] pub(crate) struct Contract { @@ -1047,10 +1055,8 @@ pub(crate) struct JsEvmContext { pub(crate) gas: u64, /// Number, amount of gas used in executing the transaction (excludes txdata costs) pub(crate) gas_used: u64, - /// Number, gas price configured in the transaction being executed - pub(crate) gas_price: u64, - /// Number, intrinsic gas for the transaction being executed - pub(crate) intrinsic_gas: u64, + /// big.int, effective gas tip of the transaction being executed + pub(crate) gas_price: U256, /// big.int Amount to be transferred in wei pub(crate) value: U256, /// Number, block number @@ -1058,8 +1064,6 @@ pub(crate) struct JsEvmContext { /// Address, miner of the block pub(crate) coinbase: Address, pub(crate) output: Bytes, - /// Number, block timestamp - pub(crate) time: String, pub(crate) transaction_ctx: TransactionContext, /// returns information about the error if one occurred, otherwise returns undefined pub(crate) error: Option, @@ -1075,12 +1079,10 @@ impl JsEvmContext { gas, gas_used, gas_price, - intrinsic_gas, value, block, coinbase, output, - time, transaction_ctx, error, } = self; @@ -1099,21 +1101,19 @@ impl JsEvmContext { obj.set(js_string!("input"), to_uint8_array(input, ctx)?, false, ctx)?; obj.set(js_string!("gas"), gas, false, ctx)?; obj.set(js_string!("gasUsed"), gas_used, false, ctx)?; - obj.set(js_string!("gasPrice"), gas_price, false, ctx)?; - obj.set(js_string!("intrinsicGas"), intrinsic_gas, false, ctx)?; + obj.set(js_string!("gasPrice"), to_bigint(gas_price), false, ctx)?; obj.set(js_string!("value"), to_bigint(value), false, ctx)?; obj.set(js_string!("block"), block, false, ctx)?; obj.set(js_string!("coinbase"), address_to_uint8_array(coinbase, ctx)?, false, ctx)?; obj.set(js_string!("output"), to_uint8_array(output, ctx)?, false, ctx)?; - obj.set(js_string!("time"), js_string!(time), false, ctx)?; - if let Some(block_hash) = transaction_ctx.block_hash { + // Like geth, the transaction fields are only set for a non-zero block and transaction hash. + if let Some(block_hash) = transaction_ctx.block_hash.filter(|hash| !hash.is_zero()) { obj.set(js_string!("blockHash"), to_uint8_array(block_hash, ctx)?, false, ctx)?; - } - if let Some(tx_index) = transaction_ctx.tx_index { - obj.set(js_string!("txIndex"), tx_index as u64, false, ctx)?; - } - if let Some(tx_hash) = transaction_ctx.tx_hash { - obj.set(js_string!("txHash"), to_uint8_array(tx_hash, ctx)?, false, ctx)?; + if let Some(tx_hash) = transaction_ctx.tx_hash.filter(|hash| !hash.is_zero()) { + let tx_index = transaction_ctx.tx_index.unwrap_or_default(); + obj.set(js_string!("txIndex"), tx_index as u64, false, ctx)?; + obj.set(js_string!("txHash"), to_uint8_array(tx_hash, ctx)?, false, ctx)?; + } } if let Some(error) = error { obj.set(js_string!("error"), js_string!(error), false, ctx)?; @@ -1132,7 +1132,8 @@ mod tests { use alloc::vec; use boa_engine::{Source, object::builtins::JsUint8Array}; use core::convert::Infallible; - use evm2::evm::{CacheDB, Database, Db, EmptyDB}; + use evm2::evm::{Database, Db, InMemoryDB}; + use std::panic::{AssertUnwindSafe, catch_unwind}; /// Records and enters a step with the given pre- and post-execution stack. fn with_step<'a, R>( @@ -1144,7 +1145,14 @@ mod tests { f: impl FnOnce() -> R, ) -> R { let mut snapshot = StepSnapshot::default(); - snapshot.record(PreStep { pc: 0, op, gas_remaining: 0, stack, memory: &memory.borrow() }); + snapshot.record(PreStep { + pc: 0, + op, + immediate: 0, + gas_remaining: 0, + stack, + memory: &memory.borrow(), + }); log.with_scope( &mut snapshot, stack, @@ -1251,8 +1259,8 @@ mod tests { let f = result.as_callable().unwrap(); - let mut db = CacheDB::new(EmptyDB::default()); - let state = TxState::default(); + let mut db = InMemoryDB::default(); + let state = PendingState::default(); let reusable_db = ReusableEvmDb::new(&mut context).unwrap(); { let addr = JsValue::from(js_string!(Address::default().to_string())); @@ -1304,8 +1312,8 @@ mod tests { let result_fn = obj.get(js_string!("result"), &mut context).unwrap().as_object().unwrap(); let setup_fn = obj.get(js_string!("setup"), &mut context).unwrap().as_object().unwrap(); - let mut db = CacheDB::new(EmptyDB::default()); - let state = TxState::default(); + let mut db = InMemoryDB::default(); + let state = PendingState::default(); { let reusable_db = ReusableEvmDb::new(&mut context).unwrap(); let addr = JsValue::from(js_string!(Address::default().to_string())); @@ -1373,7 +1381,7 @@ mod tests { .with_nonce(9) .with_code(code.clone()); let mut db = Db::new(BackingDb { address, account, slot, value }); - let changes = TxState::default(); + let changes = PendingState::default(); let reusable_db = ReusableEvmDb::new(&mut context).unwrap(); reusable_db.with_changes_scope(&changes, &mut db, || { let js_db = reusable_db.value().as_object().unwrap(); @@ -1539,8 +1547,6 @@ mod tests { #[test] fn test_scopes_revoke_access_on_return_error_and_unwind() { - use std::panic::{AssertUnwindSafe, catch_unwind}; - let mut ctx = Context::default(); register_builtins(&mut ctx).unwrap(); let log = ReusableStepLog::new(&mut ctx, OpcodeNames::new()).unwrap(); @@ -1552,13 +1558,13 @@ mod tests { let stack = vec![U256::from(42)]; let memory = RefCell::new(vec![7u8; 32]); let contract = Contract::default(); - let state = TxState::default(); - let mut database = CacheDB::new(EmptyDB::default()); + let state = PendingState::default(); + let mut database = InMemoryDB::default(); let result = catch_unwind(AssertUnwindSafe(|| { db.with_changes_scope(&state, &mut database, || { with_step(&log, opcode::STOP, &stack, &memory, &contract, || { assert!(ctx.eval(Source::from_bytes( - "savedLog.stack.peek(0) === 42n && savedLog.memory.getUint(0)[0] === 7 && savedDb.getBalance('00') === 0n" + "savedLog.stack.peek(0) === 42n && savedLog.memory.getUint(0) >> 248n === 7n && savedDb.getBalance('00') === 0n" )).unwrap().as_boolean().unwrap()); match outcome { 0 => Ok(JsValue::undefined()), @@ -1595,23 +1601,23 @@ mod tests { #[test] fn test_stack_view_reconstructs_pre_execution_stack() { // SWAP1 touches the top two items, DUP1 one item, ADD two items - let pre = [U256::from(1), U256::from(2), U256::from(3)]; + let pre = [U256::ONE, U256::from(2), U256::from(3)]; for (op, post) in [ - (opcode::SWAP1, vec![U256::from(1), U256::from(3), U256::from(2)]), - (opcode::DUP1, vec![U256::from(1), U256::from(2), U256::from(3), U256::from(3)]), - (opcode::ADD, vec![U256::from(1), U256::from(5)]), - (opcode::POP, vec![U256::from(1), U256::from(2)]), + (opcode::SWAP1, vec![U256::ONE, U256::from(3), U256::from(2)]), + (opcode::DUP1, vec![U256::ONE, U256::from(2), U256::from(3), U256::from(3)]), + (opcode::ADD, vec![U256::ONE, U256::from(5)]), + (opcode::POP, vec![U256::ONE, U256::from(2)]), // halted before pushing, e.g. out of gas after popping the inputs - (opcode::MSTORE, vec![U256::from(1)]), + (opcode::MSTORE, vec![U256::ONE]), (opcode::STOP, pre.to_vec()), ] { let mut view = StackView::default(); - view.record(op, &pre); + view.record(op, 0, &pre); let post: &'static [U256] = post.leak(); view.post = Some(post); assert_eq!(view.len(), 3); for (idx, expected) in pre.iter().rev().enumerate() { - assert_eq!(view.peek(idx).unwrap(), *expected, "op {op:#x} idx {idx}"); + assert_eq!(view.peek(idx as i64).unwrap(), *expected, "op {op:#x} idx {idx}"); } assert!(view.peek(3).is_err()); } @@ -1633,7 +1639,7 @@ mod tests { view.post = Some(post.leak()); assert_eq!(view.len(), 64); - assert_eq!(view.bytes(0..64).unwrap().as_slice(), pre.as_slice()); + assert_eq!(view.bytes(0..64).unwrap().as_slice(), pre); assert_eq!(view.bytes(10..20).unwrap().as_slice(), &pre[10..20]); // writes beyond the pre-execution length only expand memory @@ -1651,7 +1657,7 @@ mod tests { let mut view = MemoryView::default(); view.record( opcode::EXTCODECOPY, - &[U256::from(2), U256::ZERO, U256::from(4), U256::from(1)], + &[U256::from(2), U256::ZERO, U256::from(4), U256::ONE], &pre, ); assert_eq!(view.patch_offset, 4); diff --git a/crates/inspectors/src/tracing/js/builtins.rs b/crates/inspectors/src/tracing/js/builtins.rs index 16a96386d..96b77600e 100644 --- a/crates/inspectors/src/tracing/js/builtins.rs +++ b/crates/inspectors/src/tracing/js/builtins.rs @@ -1,8 +1,8 @@ //! Builtin functions -use crate::tracing::types::CallKind; -use alloc::{borrow::Cow, format, vec::Vec}; -use alloy_primitives::{Address, B256, FixedBytes, U256, hex, map::HashSet}; +use crate::tracing::{js::bindings::type_error, types::CallKind}; +use alloc::{borrow::Cow, format, rc::Rc, vec::Vec}; +use alloy_primitives::{Address, B256, FixedBytes, U256, hex, map::AddressSet}; use boa_engine::{ Context, JsArgs, JsBigInt, JsError, JsNativeError, JsResult, JsString, JsValue, NativeFunction, Source, @@ -15,7 +15,7 @@ use boa_engine::{ property::Attribute, }; use boa_gc::{Finalize, Trace, empty_trace}; -use core::borrow::Borrow; +use core::{borrow::Borrow, cell::RefCell}; /// Converts the given `JsValue` to a `serde_json::Value`. /// @@ -66,7 +66,7 @@ pub(crate) fn json_stringify(val: JsValue, ctx: &mut Context) -> JsResult JsResult<()> { let big_int = ctx.global_object().get(js_string!("BigInt"), ctx)?; // Add toJSON method and geth-compatible polyfill shims to BigInt prototype. @@ -188,9 +188,7 @@ pub(crate) fn bytes_from_value(val: JsValue, context: &mut Context) -> JsResult< return hex_decode_js_string(&js_string); } - Err(JsError::from_native( - JsNativeError::typ().with_message(format!("invalid buffer type: {}", val.type_of())), - )) + Err(type_error(format!("invalid buffer type: {}", val.type_of()))) } /// Copies the viewed bytes of a `Uint8Array` directly out of its buffer. @@ -227,7 +225,7 @@ pub(crate) fn address_to_uint8_array( addr: Address, context: &mut Context, ) -> JsResult { - JsUint8Array::from_iter(addr, context) + to_uint8_array(addr, context) } /// Create a new [JsUint8Array] array buffer from the address' bytes. @@ -447,27 +445,45 @@ pub(crate) fn slice(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResul } } -/// A container for all precompile addresses used for the `isPrecompiled` global callable. -#[derive(Clone, Debug)] -pub(crate) struct PrecompileList(pub(crate) HashSet
); +/// The active precompile addresses behind the `isPrecompiled` global callable. +/// +/// Unset until the transaction starts, like geth's list of active precompiles. +#[derive(Clone, Debug, Default)] +pub(crate) struct PrecompileList(Rc>>); impl PrecompileList { - /// Registers the global callable `isPrecompiled` - pub(crate) fn register_callable(self, ctx: &mut Context) -> JsResult<()> { + /// Registers the global callable `isPrecompiled`, which reads this list. + pub(crate) fn register_callable(&self, ctx: &mut Context) -> JsResult<()> { let is_precompiled = NativeFunction::from_copy_closure_with_captures( move |_this, args, precompiles, ctx| { let val = args.get_or_undefined(0).clone(); let buf = bytes_from_value(val, ctx)?; let addr = bytes_to_address(&buf); - Ok(precompiles.0.contains(&addr).into()) + let precompiles = RefCell::borrow(&precompiles.0); + Ok(precompiles.as_ref().is_some_and(|list| list.contains(&addr)).into()) }, - self, + self.clone(), ); ctx.register_global_callable(js_string!("isPrecompiled"), 1, is_precompiled)?; Ok(()) } + + /// Returns true if the active precompile addresses have been set. + pub(crate) fn is_set(&self) -> bool { + RefCell::borrow(&self.0).is_some() + } + + /// Sets the active precompile addresses. + pub(crate) fn set(&self, addresses: impl IntoIterator) { + *self.0.borrow_mut() = Some(addresses.into_iter().collect()); + } + + /// Unsets the active precompile addresses. + pub(crate) fn clear(&self) { + *self.0.borrow_mut() = None; + } } impl Finalize for PrecompileList {} @@ -479,6 +495,7 @@ unsafe impl Trace for PrecompileList { #[cfg(test)] mod tests { use super::*; + use alloy_primitives::uint; #[test] fn test_install_bigint() { @@ -504,13 +521,10 @@ mod tests { // Test various U256 values through to_bigint let test_cases = vec![ (U256::ZERO, "0"), - (U256::from(1u64), "1"), + (U256::ONE, "1"), (U256::from(42u64), "42"), (U256::from(u64::MAX), "18446744073709551615"), - ( - U256::from_str_radix("123456789012345678901234567890", 10).unwrap(), - "123456789012345678901234567890", - ), + (uint!(123456789012345678901234567890_U256), "123456789012345678901234567890"), ]; for (value, expected) in test_cases { @@ -522,9 +536,9 @@ mod tests { // Values above 64 and 128 bits take different construction paths for value in [ - U256::from(u64::MAX) + U256::from(1), + U256::from(u64::MAX) + U256::ONE, U256::from(u128::MAX), - U256::from(u128::MAX) + U256::from(1), + U256::from(u128::MAX) + U256::ONE, U256::from_limbs([ 0x0123456789abcdef, 0xfedcba9876543210, diff --git a/crates/inspectors/src/tracing/js/interrupt_tests.rs b/crates/inspectors/src/tracing/js/interrupt_tests.rs index dcf134087..727326bdb 100644 --- a/crates/inspectors/src/tracing/js/interrupt_tests.rs +++ b/crates/inspectors/src/tracing/js/interrupt_tests.rs @@ -7,10 +7,11 @@ use boa_engine::NativeFunction; use boa_gc::{Finalize, Trace}; use evm2::{ BaseEvmTypes, Precompiles, SpecId, + block::ethereum::execution::execute_transaction_without_commit, bytecode::Bytecode, - env::{BlockEnvExt, TxEnv}, - ethereum::{TxEnvelope, ethereum_tx_registry}, - evm::{AccountInfo, CacheDB, EmptyDB}, + env::BlockEnvExt, + ethereum::{RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, + evm::{AccountInfo, EmptyDB, InMemoryDB}, registry::HandlerError, }; @@ -19,14 +20,14 @@ const TARGET: Address = Address::with_last_byte(0x42); #[test] fn unset_interrupt_preserves_results() { - for interrupt in [None, Some(JsInspectorInterrupt::default())] { + for interrupt in [None, Some(JsInspectorInterrupt::new())] { let mut inspector = JsInspector::new(TRACER.into(), serde_json::Value::Null).unwrap(); if let Some(interrupt) = interrupt { inspector = inspector.with_interrupt(interrupt); } let (mut inspector, result) = run(inspector, &hex!("60015000"), TxKind::Call(TARGET)); let result = result.unwrap(); - assert!(result.result.status); + assert!(result.result.is_success()); assert_eq!( inspector .json_result( @@ -66,9 +67,8 @@ fn interrupt_stops_steps_without_js_step_callback() { let mut inspector = JsInspector::new(TRACER.into(), serde_json::Value::Null) .unwrap() .with_interrupt(interrupt.clone()); - let tx_env = TxEnv::::default(); let message = Message::::default(); - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); // Signal from another thread after the inspector has been created. std::thread::spawn(move || interrupt.interrupt()).join().unwrap(); inspector.step(&mut interp); @@ -106,8 +106,9 @@ fn interrupt_during_enter_and_exit_aborts() { // SELFDESTRUCT invokes enter/exit outside of step_end. &hex!("6043ff")[..], ] { + let other = if callback == "enter" { "exit" } else { "enter" }; let script = format!( - "{{ {callback}: function() {{ interrupt(); }}, + "{{ {callback}: function() {{ interrupt(); }}, {other}: function() {{}}, fault: function() {{}}, result: function() {{ return 42; }} }}" ); let (_, result) = run(interruptible(&script), code, TxKind::Call(TARGET)); @@ -127,9 +128,8 @@ fn interrupt_before_step_end_skips_callback_and_preserves_error() { ) .unwrap() .with_interrupt(interrupt.clone()); - let tx_env = TxEnv::::default(); let message = Message::::default(); - let mut interp = Interpreter::::new(&tx_env, &message); + let mut interp = Interpreter::::new(&message); inspector.step(&mut interp); assert!(inspector.pending_steps[0].active); let error = if preserve_error { @@ -248,7 +248,7 @@ fn run( code: &[u8], kind: TxKind, ) -> (JsInspector, Result) { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &TARGET, AccountInfo::default().with_code(Bytecode::new_raw(code.to_vec().into())), @@ -261,7 +261,7 @@ fn run( Precompiles::base(SpecId::CANCUN), ); evm.set_inspector(inspector); - let result = evm.transact(&transaction(kind)).map(|result| result.detach()); + let result = execute_transaction_without_commit(&mut evm, &transaction(kind)); (*evm.clear_inspector_as::().unwrap(), result) } @@ -272,7 +272,7 @@ fn assert_interrupted(result: Result) { )); } -fn transaction(kind: TxKind) -> Recovered { +pub(super) fn transaction(kind: TxKind) -> RecoveredTxEnvelope { Recovered::new_unchecked( TxEnvelope::Legacy(TxLegacy { gas_limit: 1_000_000, to: kind, ..Default::default() }), Address::ZERO, @@ -285,7 +285,7 @@ fn hook_error_takes_precedence_over_a_later_interrupt() { let (mut inspector, result) = run(interruptible(script), &hex!("60015000"), TxKind::Call(TARGET)); let result = result.unwrap(); - assert!(result.result.status); + assert!(result.result.is_success()); inspector.interrupt.as_ref().unwrap().interrupt(); let error = inspector .json_result( diff --git a/crates/inspectors/src/tracing/js/mod.rs b/crates/inspectors/src/tracing/js/mod.rs index 628dda8fd..4b5aa88bd 100644 --- a/crates/inspectors/src/tracing/js/mod.rs +++ b/crates/inspectors/src/tracing/js/mod.rs @@ -11,7 +11,6 @@ use crate::tracing::{ }, builtins::{PrecompileList, register_builtins, to_serde_value}, }, - tx_state::TxState, types::CallKind, utils, }; @@ -22,7 +21,7 @@ use alloc::{ vec::Vec, }; use alloy_consensus::{Transaction, transaction::Recovered}; -use alloy_primitives::{Address, Bytes, TxKind, U256, map::HashSet}; +use alloy_primitives::{Address, Bytes, TxKind, U256}; use boa_engine::{ Context, JsError, JsNativeError, JsObject, JsResult, JsValue, Script, Source, builtins::error::Error, error::EngineError, js_string, object::builtins::JsArray, @@ -118,8 +117,8 @@ pub struct JsInspector { call_stack: Vec, /// Monotonically increasing ID used to refresh callback contract input. next_call_id: u64, - /// Marker to track whether the precompiles have been registered. - precompiles_registered: bool, + /// The precompiles read by `isPrecompiled`, set on the first call hook of a transaction. + precompiles: PrecompileList, /// Pre-execution states captured in `step()` to be processed in `step_end()`. pending_steps: Vec, /// Depth of the root frame, which can be non-zero for a nested transaction root. @@ -152,9 +151,10 @@ impl JsInspector { /// - `fault`: a function that will be called when the transaction fails. /// /// Optional functions are invoked during inspection: - /// - `setup`: a function that will be called before the inspection starts. - /// - `enter`: a function that will be called when the execution enters a new call. - /// - `exit`: a function that will be called when the execution exits a call. + /// - `setup`: a function that will be called before the inspection starts, with the config as a + /// JSON string. + /// - `enter` and `exit`: functions that will be called when the execution enters and exits a + /// call. Both must be present or both absent. /// - `step`: a function that will be called when the execution steps to the next instruction. /// /// This also accepts a sender half of a channel to communicate with the database service so the @@ -181,6 +181,8 @@ impl JsInspector { ctx.runtime_limits_mut().set_recursion_limit(RECURSION_LIMIT); register_builtins(&mut ctx)?; + let precompiles = PrecompileList::default(); + precompiles.register_callable(&mut ctx)?; // parse the code let wrapped = format!("({code})"); @@ -219,7 +221,7 @@ impl JsInspector { reusable_db, call_stack: Default::default(), next_call_id: 1, - precompiles_registered: false, + precompiles, pending_steps: Vec::new(), root_depth: 0, hook_error: None, @@ -248,6 +250,8 @@ impl JsInspector { /// wrappers are recreated so their own properties do not carry over between transactions. pub fn fuse(&mut self) -> Result<(), JsInspectorError> { self.ensure_not_interrupted()?; + // `setup` of the fresh tracer must not see the previous transaction's precompiles. + self.precompiles.clear(); let JsTracerObject { obj, result_fn, fault_fn, enter_fn, exit_fn, step_fn } = JsTracerObject::evaluate(&self.script, &self.config, &mut self.ctx)?; // Callback objects are mutable JS objects: replacing their Rust state does not remove @@ -273,7 +277,6 @@ impl JsInspector { self.exit_fn = exit_fn; self.step_fn = step_fn; self.call_stack.clear(); - self.precompiles_registered = false; self.pending_steps.clear(); self.root_depth = 0; self.hook_error = None; @@ -328,14 +331,13 @@ impl JsInspector { } self.ensure_not_interrupted()?; let TxResultWithState { result, pending_state, .. } = res; - let state = TxState::from_pending(pending_state); let mut output_bytes = None; let mut error = None; - if result.status { + if result.is_success() { output_bytes = Some(result.output.clone()); - } else if result.stop.is_revert() { + } else if result.stop.is_frame_revert() { error = Some("execution reverted".to_string()); output_bytes = Some(result.output.clone()); } else { @@ -350,7 +352,8 @@ impl JsInspector { } }); - let base_fee = block.basefee.try_into().unwrap_or(u64::MAX); + let base_fee = block.basefee.saturating_to::(); + let gas_tip = tx.effective_tip_per_gas(base_fee).unwrap_or_default(); let ctx = JsEvmContext { r#type: match kind { @@ -363,18 +366,16 @@ impl JsInspector { input: tx.input().clone(), gas: tx.gas_limit(), gas_used: result.tx_gas_used(), - gas_price: tx.effective_gas_price(Some(base_fee)).try_into().unwrap_or(u64::MAX), + gas_price: U256::from(gas_tip), value: tx.value(), - block: block.number.try_into().unwrap_or(u64::MAX), + block: block.number.saturating_to::(), coinbase: block.beneficiary, output: output_bytes.unwrap_or_default(), - time: block.timestamp.to_string(), - intrinsic_gas: 0, transaction_ctx: self.transaction_context, error, }; let ctx = ctx.into_js_object(&mut self.ctx)?; - let result = self.reusable_db.with_changes_scope(&state, db, || { + let result = self.reusable_db.with_changes_scope(pending_state, db, || { self.result_fn.call(&self.this, &[ctx.into(), self.reusable_db.value()], &mut self.ctx) }); self.ensure_not_interrupted()?; @@ -449,16 +450,11 @@ impl JsInspector { self.active_call() } - /// Registers the precompiles in the JS context + /// Sets the active precompiles read by `isPrecompiled`. fn register_precompiles(&mut self, host: &Evm<'_, T>) { - if self.precompiles_registered { - return; + if !self.precompiles.is_set() { + self.precompiles.set(host.precompiles().addresses()); } - let precompiles = PrecompileList(HashSet::from_iter(host.precompiles().addresses())); - - let _ = precompiles.register_callable(&mut self.ctx); - - self.precompiles_registered = true } /// Sets a shared handle that cooperatively interrupts JavaScript tracing. @@ -491,10 +487,16 @@ impl JsInspector { /// ``` #[must_use] pub fn with_interrupt(mut self, interrupt: JsInspectorInterrupt) -> Self { - self.interrupt = Some(interrupt); + self.set_interrupt(interrupt); self } + /// Sets the shared handle that cooperatively interrupts JavaScript tracing, see + /// [`Self::with_interrupt`]. + pub fn set_interrupt(&mut self, interrupt: JsInspectorInterrupt) { + self.interrupt = Some(interrupt); + } + #[inline] fn is_interrupted(&self) -> bool { self.interrupt.as_ref().is_some_and(JsInspectorInterrupt::is_interrupted) @@ -565,8 +567,14 @@ impl JsInspector { }; let refund = call.enclosing_refund + frame_refund; debug_assert!(refund >= 0, "transaction-wide refund counter went negative: {refund}"); + // An out-of-gas step consumes the whole remainder it started with. + let cost = if interp.result().is_err_and(InstrStop::is_out_of_gas) { + pending.instruction.gas + } else { + interp.gas().spent().saturating_sub(pending.gas_spent_before) + }; let info = StepInfo { - cost: interp.gas().spent().saturating_sub(pending.gas_spent_before), + cost, refund: refund.max(0) as u64, // Geth reports the depth starting at 1 for the top-level call. depth: u64::from(depth) + 1, @@ -577,6 +585,13 @@ impl JsInspector { input: &call.contract.input, call_id: call.id, }; + // Geth fills the log in its step hook, which a tracer without `step` never runs, so + // `fault` sees an empty log with only the error set. + let info = if self.step_fn.is_none() { + StepInfo { error: info.error, ..Default::default() } + } else { + info + }; let (stack, memory, host) = interp.stack_memory_host(); let res = self.reusable_db.with_state_scope(host.state_mut(), || { self.reusable_step_log.with_scope( @@ -654,6 +669,11 @@ impl Inspector for JsInspector { pending.snapshot.record(PreStep { pc: pending.instruction.pc, op: pending.instruction.op, + immediate: interp + .original_bytecode_slice() + .get(interp.pc() + 1) + .copied() + .unwrap_or_default(), gas_remaining: pending.instruction.gas, stack: interp.stack().as_slice(), memory: interp.memory().as_slice(), @@ -675,19 +695,7 @@ impl Inspector for JsInspector { let result = interp.result().err(); let is_fault = result.is_some_and(|stop| should_fault(stop, pending.instruction.op)); let error = result.and_then(|stop| utils::fmt_error_msg(stop, TraceStyle::Geth)); - if self.step_fn.is_none() { - if !is_fault { - return; - } - // Fault-only tracers expose post-execution stack and memory. - pending.snapshot.record(PreStep { - pc: pending.instruction.pc, - op: pending.instruction.op, - gas_remaining: pending.instruction.gas, - stack: interp.stack().as_slice(), - memory: interp.memory().as_slice(), - }); - } else { + if self.step_fn.is_some() { self.invoke_step_hook(interp, false, if is_fault { None } else { error.clone() }); if self.check_and_halt(interp) || self.hook_errored() { return; @@ -720,7 +728,12 @@ impl Inspector for JsInspector { self.push_call( message.depth, - Contract { caller, contract, value: message.value, input: message.input.clone() }, + Contract { + caller, + contract, + value: message.value, + input: message.input.to_bytes(interp.memory()), + }, message.kind.into(), message.gas_limit, interp.gas().refunded(), @@ -782,7 +795,7 @@ impl Inspector for JsInspector { caller: message.caller, contract: message.destination, value: message.value, - input: message.input.clone(), + input: message.input.to_bytes(interp.memory()), }, message.kind.into(), message.gas_limit, @@ -969,17 +982,21 @@ impl JsTracerObject { let exit_fn = obj.get(js_string!("exit"), ctx)?.as_object().filter(|o| o.is_callable()); let step_fn = obj.get(js_string!("step"), ctx)?.as_object().filter(|o| o.is_callable()); - let js_config_value = - JsValue::from_json(config, ctx).map_err(JsInspectorError::InvalidJsonConfig)?; - - if let Some(setup_fn) = obj.get(js_string!("setup"), ctx)?.as_object() { - if !setup_fn.is_callable() { - return Err(JsInspectorError::SetupFunctionNotCallable); - } + if enter_fn.is_some() != exit_fn.is_some() { + return Err(JsInspectorError::EnterExitMismatch); + } - // call setup() + // Like geth, `setup` receives the config as a JSON string, `{}` if there is none, and a + // non-callable `setup` is ignored. + if let Some(setup_fn) = obj.get(js_string!("setup"), ctx)?.as_object() + && setup_fn.is_callable() + { + let config = match config { + serde_json::Value::Null => js_string!("{}"), + config => js_string!(config.to_string()), + }; setup_fn - .call(&(obj.clone().into()), core::slice::from_ref(&js_config_value), ctx) + .call(&(obj.clone().into()), &[config.into()], ctx) .map_err(JsInspectorError::SetupCallFailed)?; } @@ -1022,18 +1039,14 @@ pub enum JsInspectorError { #[error("trace object must expose a function fault()")] FaultFunctionMissing, - /// The setup object must be a callable function. - #[error("setup object must be a function")] - SetupFunctionNotCallable, + /// The trace object must expose either both or none of `enter()` and `exit()`. + #[error("trace object must expose either both or none of enter() and exit()")] + EnterExitMismatch, /// Failure during the invocation of the `setup()` function. #[error("failed to call setup(): {0}")] SetupCallFailed(JsError), - /// Invalid JSON configuration encountered. - #[error("invalid JSON config: {0}")] - InvalidJsonConfig(JsError), - /// Tracing was cancelled through the shared interrupt flag. #[error("JavaScript tracing interrupted")] Interrupted, @@ -1125,16 +1138,16 @@ fn with_message_suffix(err: JsError, suffix: &str, ctx: &mut Context) -> JsError #[cfg(test)] mod tests { - use super::*; + use super::{interrupt_tests::transaction, *}; use alloc::{string::ToString, vec, vec::Vec}; - use alloy_consensus::{TxLegacy, transaction::Recovered}; - use alloy_primitives::{Address, Bytes, TxKind, U256, bytes, hex}; + use alloy_consensus::{TxEip1559, TxLegacy, transaction::Recovered}; + use alloy_primitives::{Address, B256, Bytes, TxKind, U256, bytes, hex}; use boa_engine::error::{EngineError, RuntimeLimitError}; use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, Version, bytecode::Bytecode, ethereum::{TxEnvelope, ethereum_tx_registry, intrinsic_and_floor_gas}, - evm::{AccountInfo, CacheDB, EmptyDB}, + evm::{AccountInfo, InMemoryDB}, interpreter::{Host, op}, }; use serde_json::json; @@ -1166,6 +1179,108 @@ mod tests { assert!(matches!(result, Err(JsInspectorError::FaultFunctionMissing))); } + #[test] + fn test_setup_receives_config_json_string() { + let code = |expected: &str| { + format!( + r#"{{ + setup: function(config) {{ + if (typeof config !== "string" || config !== '{expected}') {{ + throw new Error(typeof config + ":" + config); + }} + }}, + fault: function() {{}}, + result: function() {{}} + }}"# + ) + }; + JsInspector::new(code(r#"{"a":1}"#), json!({"a": 1})).unwrap(); + JsInspector::new(code("{}"), serde_json::Value::Null).unwrap(); + let result = JsInspector::new(code("{}"), json!({"a": 1})); + assert!(matches!(result, Err(JsInspectorError::SetupCallFailed(_)))); + } + + #[test] + fn test_enter_exit_must_come_together() { + for functions in ["enter: function() {},", "exit: function() {},"] { + let code = format!("{{{functions} fault: function() {{}}, result: function() {{}}}}"); + let result = JsInspector::new(code, serde_json::Value::Null); + assert!(matches!(result, Err(JsInspectorError::EnterExitMismatch))); + } + let code = "{enter: function() {}, exit: function() {}, fault: function() {}, result: function() {}}"; + JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); + } + + #[test] + fn test_ctx_matches_geth() { + let code = r#"{ + fault: function() {}, + result: function(ctx) { + return [ctx.gasPrice.toString(), ctx.intrinsicGas, ctx.time]; + } + }"#; + // The tip is min(max_priority_fee, max_fee - base_fee). + for (max_fee, expected) in + [(10_000_000_000u128, "1000000000"), (5_500_000_000, "500000000")] + { + let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); + let tx = TxEnvelope::Eip1559(TxEip1559 { + chain_id: 1, + gas_limit: 1_000_000, + max_fee_per_gas: max_fee, + max_priority_fee_per_gas: 1_000_000_000, + to: TxKind::Call(Address::repeat_byte(0x01)), + ..Default::default() + }); + let res = try_run_tx(insp, None, true, U256::from(5_000_000_000u64), tx).unwrap(); + assert_eq!(res, json!([expected, null, null])); + } + } + + #[test] + fn test_ctx_transaction_fields_need_non_zero_hashes() { + let code = r#"{ + fault: function() {}, + result: function(ctx) { + return [ctx.blockHash !== undefined, ctx.txIndex, ctx.txHash !== undefined]; + } + }"#; + let hash = Some(B256::repeat_byte(1)); + let zero = Some(B256::ZERO); + for (block_hash, tx_hash, tx_index, expected) in [ + (None, None, None, json!([false, null, false])), + (hash, None, Some(3), json!([true, null, false])), + (zero, hash, Some(3), json!([false, null, false])), + (hash, zero, Some(3), json!([true, null, false])), + (hash, hash, Some(3), json!([true, 3, true])), + (hash, hash, None, json!([true, 0, true])), + ] { + let tx_context = TransactionContext { block_hash, tx_index, tx_hash }; + let insp = JsInspector::with_transaction_context( + code.to_string(), + serde_json::Value::Null, + tx_context, + ) + .unwrap(); + let res = try_run_tx(insp, None, true, U256::ZERO, legacy_tx(1024)).unwrap(); + assert_eq!(res, expected); + } + } + + #[test] + fn test_is_precompiled_is_false_in_setup() { + let code = r#"{ + before: false, + setup: function() { this.before = isPrecompiled("0x01"); }, + fault: function() {}, + result: function() { return this.before; } + }"#; + let mut insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); + insp.precompiles.set([Address::with_last_byte(1)]); + insp.fuse().unwrap(); + assert_eq!(try_run_tx(insp, None, true, U256::ZERO, legacy_tx(0)).unwrap(), json!(false)); + } + fn run_trace(code: &str, contract: Option, success: bool) -> serde_json::Value { try_run_trace(code, contract, success).unwrap() } @@ -1174,9 +1289,29 @@ mod tests { code: &str, contract: Option, success: bool, + ) -> Result { + let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); + try_run_tx(insp, contract, success, U256::ZERO, legacy_tx(1024)) + } + + fn legacy_tx(gas_price: u128) -> TxEnvelope { + TxEnvelope::Legacy(TxLegacy { + gas_price, + gas_limit: 1_000_000, + to: TxKind::Call(Address::repeat_byte(0x01)), + ..Default::default() + }) + } + + fn try_run_tx( + insp: JsInspector, + contract: Option, + success: bool, + basefee: U256, + tx: TxEnvelope, ) -> Result { let addr = Address::repeat_byte(0x01); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); // Insert the caller db.insert_account_info( @@ -1189,34 +1324,25 @@ mod tests { AccountInfo { code: Some(Bytecode::new_legacy( /* PUSH1 1, PUSH1 1, STOP */ - contract.unwrap_or_else(|| hex!("6001600100").into()), + contract.unwrap_or(bytes!("6001600100")), )), ..Default::default() }, ); - let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); let mut evm = Evm::::new( SpecId::CANCUN, - evm2::env::BlockEnvExt::default(), + evm2::env::BlockEnvExt { basefee, ..Default::default() }, ethereum_tx_registry(SpecId::CANCUN), db, Precompiles::base(SpecId::CANCUN), ); evm.set_inspector(insp); - let tx = Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { - gas_price: 1024, - gas_limit: 1_000_000, - to: TxKind::Call(addr), - ..Default::default() - }), - Address::ZERO, - ); + let tx = Recovered::new_unchecked(tx, Address::ZERO); let res = evm.transact(&tx).expect("pass without error").detach(); - assert_eq!(res.result.status, success); + assert_eq!(res.result.is_success(), success); let mut inspector = evm.clear_inspector_as::().unwrap(); let block = *evm.block_env(); inspector.json_result(&res, &tx, &block, evm.database_mut()) @@ -1247,27 +1373,20 @@ mod tests { } #[test] - fn test_memory_slice_rejects_non_finite_indexes() { + fn test_memory_slice_non_finite_indexes_are_zero() { let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.memory.slice(Infinity, NaN)); }, - fault: function() {}, - result: function() { return this.depths; } - }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); - } - - #[test] - fn test_memory_slice_rejects_non_finite_end() { - let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.memory.slice(0, Infinity)); }, + res: [], + step: function(log) { + if (log.op.toString() === 'STOP') { + this.res.push(log.memory.slice(Infinity, NaN).length, log.memory.slice(NaN, 2).length); + this.res.push(log.memory.slice(0, Infinity).length); + } + }, fault: function() {}, - result: function() { return this.depths; } + result: function() { return this.res; } }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + let res = run_trace(code, None, true); + assert_eq!(res, json!([0, 2, 0])); } #[test] @@ -1295,75 +1414,107 @@ mod tests { } #[test] - fn test_stack_peek() { - let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.stack.peek(-1)); }, - fault: function() {}, - result: function() { return this.depths; } - }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); - } - - #[test] - fn test_stack_peek_nan() { + fn test_memory_get_uint_returns_bigint() { let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.stack.peek(NaN)); }, - fault: function() {}, - result: function() { return this.depths; } - }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); - } - - #[test] - fn test_stack_peek_infinity() { - let code = r#"{ - depths: [], - step: function(log) { this.depths.push(log.stack.peek(Infinity)); }, + res: [], + step: function(log) { + if (log.op.toString() === 'STOP') { + var word = log.memory.getUint(0); + this.res.push(typeof word, word.toString(), log.memory.getUint(NaN).toString()); + this.res.push(log.memory.getUint(Infinity).toString()); + } + }, fault: function() {}, - result: function() { return this.depths; } + result: function() { return this.res; } }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + // PUSH1 42, PUSH1 0, MSTORE, STOP + let res = run_trace(code, Some(bytes!("0x602a60005200")), true); + assert_eq!(res, json!(["bigint", "42", "42", "42"])); } #[test] - fn test_memory_get_uint() { - let code = r#"{ - depths: [], - step: function(log, db) { this.depths.push(log.memory.getUint(-64)); }, - fault: function() {}, - result: function() { return this.depths; } - }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + fn test_memory_and_stack_errors_match_geth() { + // PUSH1 42, PUSH1 0, MSTORE, PUSH1 1, PUSH1 2, STOP + let contract = Some(bytes!("0x602a600052600160020000")); + for (expression, expected) in [ + ("log.memory.slice(5, 2)", "tracer accessed out of bound memory: offset 5, end 2"), + ("log.memory.slice(-1, 4)", "tracer accessed out of bound memory: offset -1, end 4"), + ( + "log.memory.slice(0, 1e30)", + "tracer accessed out of bound memory: offset 0, end -9223372036854775808", + ), + ( + "log.memory.slice(0, 1048577 + 32)", + "reached limit for padding memory slice: 1048577", + ), + ( + "log.memory.getUint(1)", + "tracer accessed out of bound memory: available 32, offset 1, size 32", + ), + ( + "log.memory.getUint(-1)", + "tracer accessed out of bound memory: available 32, offset -1, size 32", + ), + ("log.stack.peek(2)", "tracer accessed out of bound stack: size 2, index 2"), + ("log.stack.peek(-1)", "tracer accessed out of bound stack: size 2, index -1"), + ( + "log.stack.peek(9223372036854775807n)", + "tracer accessed out of bound stack: size 2, index -9223372036854775808", + ), + ] { + let code = format!( + "{{step: function(log) {{ if (log.op.toString() === 'STOP') {expression}; }}, fault: function() {{}}, result: function() {{}}}}" + ); + let JsInspectorError::JsError(error) = + try_run_trace(&code, contract.clone(), true).unwrap_err() + else { + panic!("{expression}: unexpected error"); + }; + assert_eq!( + error.as_native().unwrap().message(), + format!("{expected} in server-side tracer function 'step'"), + "{expression}" + ); + } } #[test] - fn test_memory_get_uint_rejects_non_finite_offset() { + fn test_stack_peek_index_conversion() { let code = r#"{ - depths: [], - step: function(log, db) { this.depths.push(log.memory.getUint(Infinity)); }, + res: [], + step: function(log) { + if (log.op.toString() === 'STOP') { + this.res.push( + log.stack.peek(NaN).toString(), + log.stack.peek(Infinity).toString(), + log.stack.peek(1.5).toString(), + log.stack.peek('1').toString(), + log.stack.peek(1n).toString() + ); + } + }, fault: function() {}, - result: function() { return this.depths; } + result: function() { return this.res; } }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + let res = run_trace(code, Some(bytes!("0x600160020000")), true); + assert_eq!(res, json!(["2", "2", "1", "1", "1"])); } #[test] - fn test_memory_get_uint_rejects_nan_offset() { + fn test_frame_value_is_undefined_for_staticcall() { let code = r#"{ - depths: [], - step: function(log, db) { this.depths.push(log.memory.getUint(NaN)); }, + res: [], + enter: function(frame) { this.res.push([frame.getType(), String(frame.getValue())]); }, + exit: function() {}, fault: function() {}, - result: function() { return this.depths; } + result: function() { return this.res; } }"#; - let err = try_run_trace(code, None, true).unwrap_err(); - assert!(matches!(err, JsInspectorError::JsError(_))); + // CALL and STATICCALL of the identity precompile. + let calls = bytes!("0x6000600060006000600060046101fff1600060006000600060046101fffa00"); + assert_eq!( + run_trace(code, Some(calls), true), + json!([["CALL", "0"], ["STATICCALL", "undefined"]]) + ); } #[test] @@ -1658,7 +1809,7 @@ mod tests { if (op === 'MSTORE8' || op === 'STOP') { this.res.push(log.memory.length()); if (log.memory.length() > 0) { - this.res.push(log.memory.getUint(0)[0]); + this.res.push(Number(log.memory.getUint(0) >> 248n)); this.res.push(log.memory.slice(0, 2)[0]); } } @@ -1681,7 +1832,7 @@ mod tests { result: function() { return this.count; } }"#; let addr = Address::repeat_byte(0x01); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &Address::ZERO, AccountInfo { balance: U256::from(1e18), ..Default::default() }, @@ -1689,7 +1840,7 @@ mod tests { db.insert_account_info( &addr, AccountInfo { - code: Some(Bytecode::new_legacy(hex!("6001600100").into())), + code: Some(Bytecode::new_legacy(bytes!("6001600100"))), ..Default::default() }, ); @@ -1702,18 +1853,11 @@ mod tests { db, Precompiles::base(SpecId::CANCUN), ); - let tx = Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { - gas_limit: 1_000_000, - to: TxKind::Call(addr), - ..Default::default() - }), - Address::ZERO, - ); + let tx = transaction(TxKind::Call(addr)); for _ in 0..3 { evm.set_inspector(inspector); let res = evm.transact(&tx).unwrap().detach(); - assert!(res.result.status); + assert!(res.result.is_success()); inspector = *evm.clear_inspector_as::().unwrap(); let block = *evm.block_env(); assert_eq!( @@ -1729,8 +1873,6 @@ mod tests { let code = r#"{ counts: { log: 0, db: 0, enter: 0, exit: 0 }, setup: function(config) { - if (config.used) throw new Error("config was reused"); - config.used = true; this.ready = true; }, mark: function(object, kind) { @@ -1761,7 +1903,7 @@ mod tests { bytecode.extend_from_slice(&hex!("61fffff150")); // PUSH2 gas, CALL, POP } bytecode.push(0x00); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &addr, AccountInfo { code: Some(Bytecode::new_legacy(bytecode.into())), ..Default::default() }, @@ -1769,7 +1911,7 @@ mod tests { db.insert_account_info( &child, AccountInfo { - code: Some(Bytecode::new_legacy(hex!("600100").into())), + code: Some(Bytecode::new_legacy(bytes!("600100"))), ..Default::default() }, ); @@ -1781,18 +1923,11 @@ mod tests { db, Precompiles::base(SpecId::CANCUN), ); - let tx = Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { - gas_limit: 1_000_000, - to: TxKind::Call(addr), - ..Default::default() - }), - Address::ZERO, - ); + let tx = transaction(TxKind::Call(addr)); for _ in 0..3 { evm.set_inspector(inspector); let res = evm.transact(&tx).unwrap().detach(); - assert!(res.result.status); + assert!(res.result.is_success()); inspector = *evm.clear_inspector_as::().unwrap(); let block = *evm.block_env(); assert_eq!( @@ -1882,7 +2017,7 @@ mod tests { ] { let caller = Address::repeat_byte(0xaa); let nonce = 7; - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info(&caller, AccountInfo { nonce, ..AccountInfo::default() }); let script = r#"{ fault: function() {}, @@ -1941,7 +2076,7 @@ mod tests { this.r=[a[0],a[32],capped.length,log.memory.slice(2**40,2**40).length,error]; },fault:function(){},result:function(){return this.r}}"#; assert_eq!( - run_trace(tracer, Some(hex!("60ff60005300").into()), true), + run_trace(tracer, Some(bytes!("60ff60005300")), true), json!([255, 0, 1024 * 1024, 0, "TypeError"]) ); } diff --git a/crates/inspectors/src/tracing/js/snapshot_tests.rs b/crates/inspectors/src/tracing/js/snapshot_tests.rs index 5ee86fca8..f2471e80b 100644 --- a/crates/inspectors/src/tracing/js/snapshot_tests.rs +++ b/crates/inspectors/src/tracing/js/snapshot_tests.rs @@ -1,14 +1,13 @@ //! Compare reconstructed JS step data with snapshots captured before interpreter execution. -use super::*; -use alloy_consensus::TxLegacy; +use super::{interrupt_tests::transaction, *}; use alloy_primitives::hex; use evm2::{ - BaseEvmTypes, ExecutionConfig, Precompiles, SpecId, Version, + BaseEvmTypes, Precompiles, SpecId, bytecode::Bytecode, constants::CALL_DEPTH_LIMIT, - ethereum::{TxEnvelope, ethereum_tx_registry}, - evm::{AccountInfo, CacheDB, EmptyDB}, + ethereum::ethereum_tx_registry, + evm::{AccountInfo, InMemoryDB}, interpreter::Host, }; use serde_json::{Value, json}; @@ -73,9 +72,9 @@ impl Inspector for SnapshotInspector { } } -fn compare(code: Bytes, child: Option, initial_call_depth: u16) -> Vec { +fn compare(code: Bytes, child: Option, initial_call_depth: u16, spec: SpecId) -> Vec { let address = Address::repeat_byte(0x11); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &address, AccountInfo { code: Some(Bytecode::new_legacy(code.clone())), ..Default::default() }, @@ -101,25 +100,16 @@ fn compare(code: Bytes, child: Option, initial_call_depth: u16) -> Vec::new_with_execution_config( - ExecutionConfig::for_spec_and_version(SpecId::CANCUN, version), - SpecId::CANCUN, + let mut evm = Evm::::new( + spec, evm2::env::BlockEnvExt::default(), - ethereum_tx_registry(SpecId::CANCUN), + ethereum_tx_registry(spec), db, - Precompiles::base(SpecId::CANCUN), + Precompiles::base(spec), ); + evm.set_initial_call_depth(initial_call_depth); evm.set_inspector(inspector); - let tx = Recovered::new_unchecked( - TxEnvelope::Legacy(TxLegacy { - gas_limit: 1_000_000, - to: TxKind::Call(address), - ..Default::default() - }), - Address::ZERO, - ); + let tx = transaction(TxKind::Call(address)); let res = evm.transact(&tx).unwrap().detach(); let mut inspector = evm.clear_inspector_as::().unwrap(); let block = *evm.block_env(); @@ -140,7 +130,7 @@ fn reconstructed_steps_match_all_opcode_bytes() { code.extend_from_slice(&[op::PUSH1, value]); } code.extend_from_slice(&[opcode, op::STOP]); - compare(code.into(), None, 0); + compare(code.into(), None, 0, SpecId::CANCUN); } } @@ -163,7 +153,7 @@ fn parent_snapshot_survives_calls_and_return_data() { parent.push(op::STOP); let mut child = hex!("60aa60005260206000").to_vec(); child.push(stop); - let steps = compare(parent.into(), Some(child.into()), 0); + let steps = compare(parent.into(), Some(child.into()), 0, SpecId::CANCUN); assert!(steps.iter().any(|step| step[3] == 2), "child must execute"); } } @@ -179,7 +169,7 @@ fn parent_snapshot_survives_create() { } code.extend_from_slice(&hex!("600a60166000")); code.extend_from_slice(&[opcode, op::POP, op::STOP]); - let steps = compare(code.into(), None, 0); + let steps = compare(code.into(), None, 0, SpecId::CANCUN); assert!(steps.iter().any(|step| step[3] == 2), "initcode must execute"); } } @@ -188,9 +178,9 @@ fn parent_snapshot_survives_create() { fn nested_root_steps_keep_visible_depth() { let parent = hex!("5f5f5f5f5f7322222222222222222222222222222222222222225af100"); let child = Bytes::from_static(&[op::STOP]); - let root_steps = compare(parent.into(), Some(child.clone()), 0); + let root_steps = compare(parent.into(), Some(child.clone()), 0, SpecId::CANCUN); for depth in [0, 7, u16::MAX - CALL_DEPTH_LIMIT - 1] { - let steps = compare(parent.into(), Some(child.clone()), depth); + let steps = compare(parent.into(), Some(child.clone()), depth, SpecId::CANCUN); let depths = steps.iter().map(|step| step[3].as_u64().unwrap()).collect::>(); let depth = u64::from(depth); let mut expected = vec![depth + 1; 8]; @@ -204,3 +194,31 @@ fn nested_root_steps_keep_visible_depth() { assert_eq!(steps, shifted); } } + +/// `DUPN`, `SWAPN` and `EXCHANGE` touch the stack items their immediate selects, not the ones their +/// opcode-table entry declares. +#[test] +fn reconstructed_steps_match_stack_immediates() { + let cases: &[&[u8]] = &[ + &[op::DUPN, 0x80], + &[op::SWAPN, 0x80], + &[op::SWAPN, 0x90], + // An invalid immediate, then one that underflows the stack. + &[op::SWAPN, 0x5b], + &[op::SWAPN, 0x00], + &[op::EXCHANGE, 0x8e], + &[op::EXCHANGE, 0x51], + &[op::EXCHANGE, 0xff], + &[op::EXCHANGE, 0x52], + // The interpreter reads the zero padding past the end of the code as the immediate. + &[op::EXCHANGE], + ]; + for case in cases { + let mut code = Vec::new(); + for value in 0..40u8 { + code.extend_from_slice(&[op::PUSH1, value]); + } + code.extend_from_slice(case); + compare(code.into(), None, 0, SpecId::AMSTERDAM); + } +} diff --git a/crates/inspectors/src/tracing/keccak.rs b/crates/inspectors/src/tracing/keccak.rs new file mode 100644 index 000000000..62b9bb3cf --- /dev/null +++ b/crates/inspectors/src/tracing/keccak.rs @@ -0,0 +1,132 @@ +//! Keccak256 preimage tracing inspector +//! +//! The keccak256PreimageTracer collects the preimage of every `KECCAK256` operation executed in +//! the lifetime of a transaction. The result is a map of hash to preimage. +//! +//! Like geth, it records an operation that goes on to fail. [`MEMORY_PAD_LIMIT`] bounds the work +//! per operation, and the [`TraceLimits`] of the inspector bound the bytes it retains, which geth +//! does not. +//! +//! See also + +use crate::tracing::{TraceLimits, limits::TraceBudget}; +use alloc::{borrow::Cow, vec}; +use alloy_primitives::{ + Bytes, hex, keccak256, + map::{B256Map, HashMap}, +}; +use evm2::{ + EvmTypes, Inspector, InspectorConfig, OpcodeSet, + interpreter::{Interpreter, opcode::op}, +}; +use serde_json::Value; +use thiserror::Error; + +/// The name of the tracer in the `tracer` option of `debug_trace*` and in a mux tracer config. +pub const KECCAK256_PREIMAGE_TRACER: &str = "keccak256PreimageTracer"; + +/// The most zero padding, in bytes, a preimage that reaches past the end of memory may need. geth +/// records nothing for a preimage that needs more. +pub const MEMORY_PAD_LIMIT: u64 = 1024 * 1024; + +/// The default limit on the bytes of all recorded preimages, 64 MiB. geth has no limit. +pub const DEFAULT_MAX_RECORDED_BYTES: usize = 64 * 1024 * 1024; + +/// The result of a [`Keccak256PreimageInspector`] would miss preimages: it reached its byte limit. +#[derive(Clone, Copy, Debug, Error)] +#[error("keccak256PreimageTracer reached its limit of recorded preimage bytes")] +pub struct LimitExceeded; + +/// Inspector that records the preimage of every `KECCAK256` operation. +#[derive(Clone, Debug)] +pub struct Keccak256PreimageInspector { + /// The map of hash to preimage. + inner: B256Map, + /// The bytes of the recorded preimages. + budget: TraceBudget, +} + +impl Default for Keccak256PreimageInspector { + fn default() -> Self { + Self::new(TraceLimits::default().set_max_recorded_bytes(Some(DEFAULT_MAX_RECORDED_BYTES))) + } +} + +impl Keccak256PreimageInspector { + /// Returns an inspector that records preimages up to the given limits. + /// [`Default`] caps recorded bytes at [`DEFAULT_MAX_RECORDED_BYTES`]. + pub fn new(limits: TraceLimits) -> Self { + Self { inner: HashMap::default(), budget: TraceBudget { limits, ..Default::default() } } + } + + /// Returns whether a preimage was omitted after reaching the byte limit. + pub const fn limit_exceeded(&self) -> bool { + self.budget.exceeded() + } + + /// Clears the recorded preimages. + pub fn clear(&mut self) { + self.inner.clear(); + self.budget.recorded = 0; + self.budget.exceeded = false; + } + + /// Returns the result of the tracer: a JSON object mapping each hash to its preimage. + /// + /// Fails if the byte limit was reached, because the object would miss preimages. + pub fn json_result(&self) -> Result { + if self.budget.exceeded() { + return Err(LimitExceeded); + } + Ok(Value::Object( + self.inner + .iter() + .map(|(hash, preimage)| { + (hex::encode_prefixed(hash), hex::encode_prefixed(preimage).into()) + }) + .collect(), + )) + } +} + +impl Inspector for Keccak256PreimageInspector { + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::new().with_steps([op::KECCAK256].into_iter().collect::()) + } + + fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { + if interp.opcode() != op::KECCAK256 || self.budget.exceeded() { + return; + } + // The stack can be too small if the operation is about to fail. + let &[.., length, offset] = interp.stack().as_slice() else { return }; + // The operands are truncated to 64 bits. + let (offset, length) = (offset.wrapping_to::(), length.wrapping_to::()); + let memory = interp.memory().as_slice(); + if let Some(preimage) = memory_copy_padded(memory, offset, length) { + let hash = keccak256(&preimage); + if !self.inner.contains_key(&hash) && self.budget.reserve(preimage.len()) { + self.inner.insert(hash, preimage.into_owned().into()); + } + } + self.budget.halt(interp, true); + } +} + +/// Returns the `size` bytes of `memory` at `offset`, zero padded past its end. +/// +/// Returns `None` if the range reaches more than [`MEMORY_PAD_LIMIT`] past the end of memory. +pub(crate) fn memory_copy_padded(memory: &[u8], offset: u64, size: u64) -> Option> { + let end = offset.saturating_add(size); + if end.saturating_sub(memory.len() as u64) > MEMORY_PAD_LIMIT { + return None; + } + if let Some(preimage) = memory.get(offset as usize..end as usize) { + return Some(Cow::Borrowed(preimage)); + } + let mut preimage = vec![0; size as usize]; + if let Some(overlap) = memory.get(offset as usize..) { + preimage[..overlap.len()].copy_from_slice(overlap); + } + Some(Cow::Owned(preimage)) +} diff --git a/crates/inspectors/src/tracing/limits.rs b/crates/inspectors/src/tracing/limits.rs index ce1219c64..64552da76 100644 --- a/crates/inspectors/src/tracing/limits.rs +++ b/crates/inspectors/src/tracing/limits.rs @@ -69,8 +69,18 @@ impl TraceBudget { self.exceeded } + /// Returns whether exceeding the budget can halt execution. + pub(crate) fn can_halt(&self) -> bool { + self.limits.max_recorded_bytes.is_some() && self.limits.behavior == TraceLimitBehavior::Halt + } + + /// Returns whether the budget was exceeded and the limits halt execution. + pub(crate) fn halts(&self) -> bool { + self.exceeded && self.limits.behavior == TraceLimitBehavior::Halt + } + pub(crate) fn halt(&self, interp: &mut Interpreter<'_, '_, T>, in_step: bool) { - if self.exceeded && self.limits.behavior == TraceLimitBehavior::Halt { + if self.halts() { let stop = interp.fail(ExecutionError::Fatal("trace recorded byte limit exceeded".into())); if in_step { diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index f5f545159..039960b34 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -3,22 +3,20 @@ use crate::{ opcode::immediate_size, tracing::{ - arena::PushTraceKind, types::{ CallKind, CallTraceNode, RecordedMemory, StepDelta, StorageChange, StorageChangeReason, TraceMemberOrder, }, - utils::gas_used, + utils::step_cost, }, }; use alloc::{boxed::Box, vec::Vec}; -use alloy_consensus::{Transaction, transaction::Recovered}; -use alloy_primitives::{Address, B256, Bytes, Log, TxKind, U256}; -use alloy_rpc_types_trace::parity::StorageDelta; +use alloy_primitives::{Address, B256, Bytes, Log, U256}; +use alloy_rpc_types_trace::{geth::FlatCallConfig, parity::StorageDelta}; use core::{mem, ops::Range}; use evm2::{ - Evm, EvmFeatures, EvmTypes, Inspector, SpecId, TxResultExt, - evm::JournalEntry, + EvmFeatures, EvmTypes, Inspector, InspectorConfig, OpcodeSet, SpecId, + evm::PrewarmSet, interpreter::{ InstrStop, Interpreter, Message, MessageKind, MessageResult, MessageResultExt, opcode::{OpCode, op}, @@ -31,15 +29,24 @@ pub use arena::CallTraceArena; mod builder; pub use builder::{ geth::{self, GethTraceBuilder}, - parity::{self, ParityTraceBuilder}, + parity::{self, ParityTraceBuilder, PrecompileExclusion}, }; mod config; -pub use config::{OpcodeFilter, StackSnapshotType, TracingInspectorConfig}; +pub use config::{StackSnapshotType, TracingInspectorConfig}; + +mod erc7562; +pub use erc7562::{Erc7562Inspector, Error as Erc7562Error}; mod fourbyte; pub use fourbyte::FourByteInspector; +mod keccak; +pub use keccak::{ + DEFAULT_MAX_RECORDED_BYTES, KECCAK256_PREIMAGE_TRACER, Keccak256PreimageInspector, + LimitExceeded as Keccak256PreimageLimitExceeded, MEMORY_PAD_LIMIT, +}; + mod limits; use limits::TraceBudget; pub use limits::{TraceLimitBehavior, TraceLimits}; @@ -50,9 +57,7 @@ pub use opcount::OpcodeCountInspector; pub mod types; use types::{CallLog, CallTrace, CallTraceStep}; -mod tx_state; - -mod utils; +pub(crate) mod utils; #[cfg(feature = "std")] mod writer; @@ -70,12 +75,6 @@ pub use mux::{Error as MuxError, MuxInspector}; mod debug; pub use debug::{DebugInspector, DebugInspectorError}; -/// Returns the effective gas refund after the calldata floor adjustment. -#[inline] -pub(crate) const fn final_refunded(gas: &TxResultExt) -> u64 { - gas.final_refunded() -} - /// An inspector that collects call traces. /// /// This [Inspector] can be hooked into evm2's EVM which then calls the inspector functions, such @@ -100,11 +99,9 @@ pub struct TracingInspector { /// /// This is a stack because nested calls are executed between the `step` and `step_end` of the /// call instruction itself. A `usize::MAX` step index marks a step that was not recorded. - step_stack: Vec<(usize, usize)>, + step_stack: Vec, /// Number of opcode steps captured across all calls since the last reset. recorded_steps: u64, - /// Tracks the journal len in the step, used in step_end to check if the journal has changed - last_journal_len: usize, /// The spec id of the EVM. /// /// This is filled during execution. @@ -117,6 +114,11 @@ pub struct TracingInspector { /// /// All `Vec` are always empty but may have capacity. reusable_step_vecs: Vec>, + /// The precompiles the host warms, read on the first call that records precompile calls. + /// + /// Addresses a provider only dispatches to on demand, such as Tempo's token precompiles, + /// trace like contracts, as with revm-inspectors' warm precompile set. + precompiles: Option, } impl TracingInspector { @@ -138,7 +140,7 @@ impl TracingInspector { /// Returns whether recording omitted a byte buffer after reaching the budget. pub const fn limit_exceeded(&self) -> bool { - self.budget.exceeded + self.budget.exceeded() } /// Resets the inspector to its initial state of [Self::new]. @@ -153,9 +155,9 @@ impl TracingInspector { log_count, step_stack, recorded_steps, - last_journal_len, spec_id, features, + precompiles, // kept config, budget, @@ -164,7 +166,7 @@ impl TracingInspector { // if we record steps we can reuse the individual calltracestep vecs if config.record_steps { - for node in &mut traces.arena { + for node in traces.nodes_mut() { // move out and store the reusable steps vec let mut steps = mem::take(&mut node.trace.steps); // ensure steps are cleared @@ -180,7 +182,7 @@ impl TracingInspector { *recorded_steps = 0; spec_id.take(); *features = EvmFeatures::empty(); - *last_journal_len = 0; + precompiles.take(); budget.recorded = 0; budget.exceeded = false; } @@ -287,10 +289,25 @@ impl TracingInspector { } } - /// Consumes the Inspector and returns a [ParityTraceBuilder]. + /// Consumes the Inspector and returns a [ParityTraceBuilder] that leaves out calls to + /// precompiles without value, see [PrecompileExclusion::ZeroValue]. #[inline] pub fn into_parity_builder(self) -> ParityTraceBuilder { - ParityTraceBuilder::new(self.traces.arena, self.spec_id, self.config) + ParityTraceBuilder::new(self.traces.arena, PrecompileExclusion::ZeroValue) + } + + /// Consumes the Inspector and returns a [ParityTraceBuilder] for geth's flatCallTracer. + /// + /// Unlike [Self::into_parity_builder], this keeps or leaves out calls to precompiles as the + /// tracer's `includePrecompiles` option says. + #[inline] + pub fn into_flat_call_builder(self, config: &FlatCallConfig) -> ParityTraceBuilder { + let exclusion = if config.include_precompiles.unwrap_or_default() { + PrecompileExclusion::None + } else { + PrecompileExclusion::CallAndStaticCall + }; + ParityTraceBuilder::new(self.traces.arena, exclusion) } /// Consumes the Inspector and returns a [GethTraceBuilder]. @@ -306,39 +323,7 @@ impl TracingInspector { /// starting a new transaction: [`Self::fuse`] #[inline] pub fn geth_builder(&self) -> GethTraceBuilder<'_> { - GethTraceBuilder::new_borrowed(&self.traces.arena).with_features(self.features) - } - - /// Returns true if we're no longer in the context of the root call. - const fn is_deep(&self) -> bool { - // the root call will always be the first entry in the trace stack - !self.trace_stack.is_empty() - } - - /// Returns true if this a call to a precompile contract. - /// - /// Returns true if the `to` address is a precompile contract and the value is zero. - #[inline] - fn is_precompile_call( - &self, - host: &Evm<'_, T>, - to: &Address, - value: &U256, - ) -> bool { - if host.precompiles().contains(to) { - // only if this is _not_ the root call - return self.is_deep() && value.is_zero(); - } - false - } - - /// Returns the currently active call trace. - /// - /// This will be the last call trace pushed to the stack: the call we entered most recently. - #[track_caller] - #[inline] - fn active_trace(&self) -> Option<&CallTraceNode> { - self.trace_stack.last().map(|entry| &self.traces.arena[entry.trace_idx]) + GethTraceBuilder::new_borrowed(self.traces.nodes()).with_features(self.features) } /// Returns the last trace [CallTrace] index from the stack. @@ -387,15 +372,6 @@ impl TracingInspector { gas_limit: u64, maybe_precompile: Option, ) { - // This will only be true if the inspector is configured to exclude precompiles and the call - // is to a precompile - let push_kind = if maybe_precompile.unwrap_or(false) { - // We don't want to track precompiles - PushTraceKind::PushOnly - } else { - PushTraceKind::PushAndAttachToParent - }; - // find an empty steps vec or create a new one let steps = self.reusable_step_vecs.pop().unwrap_or_default(); @@ -404,7 +380,6 @@ impl TracingInspector { let trace_idx = self.traces.push_trace( parent, - push_kind, CallTrace { depth, address, @@ -437,14 +412,15 @@ impl TracingInspector { trace.gas_refund_counter = result.gas.refunded().max(0) as u64; trace.status = Some(result.stop); - trace.success = result.stop.is_success(); + trace.success = result.is_success(); trace.output = result.output.clone(); if !trace.success { self.log_count = log_count; } + // The result is the frame's outcome as the parent sees it; another inspector may have + // overridden it, so it wins over the address recorded when the create started. if let Some(address) = result.created_address { - // A new contract was created via CREATE trace.address = address; } } @@ -470,7 +446,7 @@ impl TracingInspector { && self.config.step_limit.is_none_or(|limit| self.recorded_steps < limit.get()); if !record { // Push a sentinel so that the upcoming `step_end` stays paired with this step. - self.step_stack.push((trace_idx, usize::MAX)); + self.step_stack.push(PendingStep::new(trace_idx, usize::MAX, 0)); return; } @@ -513,9 +489,6 @@ impl TracingInspector { Bytes::new() }; - let gas_used = - gas_used(interp.spec(), interp.gas().spent(), interp.gas().refunded() as u64); - let mut immediate_bytes = None; if self.config.record_immediate_bytes { let size = usize::from(immediate_size(op.get())); @@ -528,7 +501,12 @@ impl TracingInspector { } } - self.last_journal_len = interp.host().state().journal().len(); + // A step that halts on the budget never runs, so it records no access. + let storage_change = if self.config.record_state_diff && !self.budget.halts() { + storage_change(interp, op.get()) + } else { + None + }; let step_idx = node.trace.steps.len(); node.trace.steps.push(CallTraceStep { @@ -539,7 +517,6 @@ impl TracingInspector { returndata, gas_remaining: interp.gas().remaining(), gas_refund_counter: interp.gas().refunded() as u64, - gas_used, immediate_bytes, // These fields will be populated in `step_end`. @@ -551,7 +528,7 @@ impl TracingInspector { .feature(EvmFeatures::EIP8037) .then_some(interp.gas().reservoir()), state_gas_spent: interp.gas().state_gas_spent(), - storage_change: None, + storage_change, status: None, // This is never populated in `TracingInspector`. @@ -559,11 +536,17 @@ impl TracingInspector { }); node.ordering.push(TraceMemberOrder::Step(step_idx)); - self.step_stack.push((trace_idx, step_idx)); + self.step_stack.push(PendingStep::new( + trace_idx, + step_idx, + interp.gas().remaining_excluding_state_spill(), + )); if self.config.record_step_deltas { let write_range = memory_write_range(op.get(), interp.stack().as_slice()); - let store = storage_write(op.get(), interp.stack().as_slice()); + let store = (!self.budget.halts()) + .then(|| storage_write(op.get(), interp.stack().as_slice())) + .flatten(); if write_range.is_some() || store.is_some() || node.trace.steps[step_idx].is_call_like_op() @@ -578,12 +561,30 @@ impl TracingInspector { } } + /// Records the gas a call or create step has charged, when its message starts. + /// + /// Invoked on [Inspector::call] and [Inspector::create], where the parent has charged the step, + /// including the gas forwarded to the child, and the child has not returned any gas yet. A + /// call step costs the forwarded gas too, a create step does not, so `forwarded` is the gas to + /// give back. + fn finish_step_charge(&mut self, interp: &Interpreter<'_, '_, T>, forwarded: u64) { + // The step's own message is the first one it starts; a precompile may start more. + if let Some(pending) = self.step_stack.last_mut() + && pending.gas_after_charge.is_none() + { + pending.gas_after_charge = + Some(interp.gas().remaining_excluding_state_spill().saturating_add(forwarded)); + } + } + /// Fills the current trace with the output of a step. /// /// Invoked on [Inspector::step_end]. #[cold] fn fill_step_on_step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { - let Some((trace_idx, step_idx)) = self.step_stack.pop() else { + let Some(PendingStep { trace_idx, step_idx, gas_before, gas_after_charge }) = + self.step_stack.pop() + else { return; }; let node = &mut self.traces.arena[trace_idx]; @@ -596,7 +597,6 @@ impl TracingInspector { debug_assert!( step.push_stack.is_none() && step.gas_cost == 0 - && step.storage_change.is_none() && step.status.is_none() && step.decoded.is_none(), "step in step_end is already filled: {trace_idx} -> {step:#?}", @@ -616,48 +616,19 @@ impl TracingInspector { ); } - let host = interp.host(); - let journal = host.state().journal(); - - // If journal has not changed, there is no state change to be recorded. - if self.config.record_state_diff && journal.len() != self.last_journal_len { - let op = step.op.get(); - - step.storage_change = if matches!(op, op::SLOAD | op::SSTORE) { - let reason = match op { - op::SLOAD => StorageChangeReason::SLOAD, - op::SSTORE => StorageChangeReason::SSTORE, - _ => unreachable!(), - }; - - match journal.last() { - Some(JournalEntry::StorageChange { address, key, previous }) => { - // SAFETY: (Address,key) exists if part if StorageChange - let value = host.state().get_storage(address, key).unwrap_or_default(); - let change = - StorageChange { key: *key, value, had_value: Some(*previous), reason }; - Some(Box::new(change)) - } - Some(JournalEntry::StorageWarmed { key, address }) => { - // SAFETY: (Address,key) exists if part if StorageChange - let value = host.state().get_storage(address, key).unwrap_or_default(); - let change = StorageChange { key: *key, value, had_value: None, reason }; - Some(Box::new(change)) - } - _ => None, - } - } else { - None - }; - } - - // The gas cost is the difference between the recorded gas remaining at the start of the - // step and the remaining gas here, at the end of the step. An out-of-gas step consumes - // the whole remainder, except the reentrancy sentry, which halts before any charge. - step.gas_cost = if interp.result() == Err(InstrStop::ReentrancySentryOOG) { - 0 - } else { - step.gas_remaining.saturating_sub(interp.gas().remaining()) + // A call or create step is charged once its message starts, which includes the gas + // forwarded by a call and excludes it for a create. Any other step is charged when it + // ends. State gas that spills into the remaining gas is not part of the cost. + let result = interp.result(); + step.gas_cost = match gas_after_charge { + Some(after) if result != Err(InstrStop::ReentrancySentryOOG) => { + gas_before.saturating_sub(after) + } + _ => step_cost( + result, + step.gas_remaining, + gas_before.saturating_sub(interp.gas().remaining_excluding_state_spill()), + ), }; let state_gas_delta = interp.gas().state_gas_spent().saturating_sub(step.state_gas_spent); if step.state_gas_reservoir.is_some() && state_gas_delta != 0 { @@ -667,24 +638,27 @@ impl TracingInspector { // set the status step.status = interp.result().err(); - if self.config.record_step_deltas { - if step.status.is_some_and(|status| status.is_halt()) { - return; - } - // Call results, returned memory and gas are already available in evm2's `step_end`. - // Gas credits cannot be recovered from the saturated unsigned gas cost. - let gas_remaining_after = (step.is_call_like_op() - || interp.gas().remaining() > step.gas_remaining) - .then_some(interp.gas().remaining()); - if gas_remaining_after.is_some() - && node.trace.step_deltas.last().is_none_or(|delta| delta.step != step_idx) - { - node.trace.step_deltas.push(StepDelta { step: step_idx, ..Default::default() }); - } - if let Some(delta) = - node.trace.step_deltas.last_mut().filter(|delta| delta.step == step_idx) - { - delta.gas_remaining_after = gas_remaining_after; + if step.status.is_some_and(|status| status.is_halt()) { + return; + } + // The gas a child returns, gas credits and state gas drawn from the remaining gas cannot be + // recovered from the step cost, so record the balance after the step even without memory + // and storage deltas. + let remaining = interp.gas().remaining(); + let gas_remaining_after = (step.is_call_like_op() + || remaining != step.gas_remaining.saturating_sub(step.gas_cost)) + .then_some(remaining); + if gas_remaining_after.is_some() + && node.trace.step_deltas.last().is_none_or(|delta| delta.step != step_idx) + { + node.trace.step_deltas.push(StepDelta { step: step_idx, ..Default::default() }); + } + if let Some(delta) = + node.trace.step_deltas.last_mut().filter(|delta| delta.step == step_idx) + { + delta.gas_remaining_after = gas_remaining_after; + // Call results and returned memory are already available in evm2's `step_end`. + if self.config.record_step_deltas { if step.is_call_like_op() && let Some(range) = &mut delta.write_range { @@ -697,37 +671,6 @@ impl TracingInspector { } } } - - /// Restores a root omitted when evm2 halts during runtime gas charging, before its hooks run. - pub(crate) fn recover_runtime_oog_root( - &mut self, - tx: &Recovered, - result: &TxResultExt, - ) { - // The arena starts with a default root. After execution, call_end/create_end have set - // its status if the root was recorded, even when it executed no opcodes. - if self.traces.arena[0].trace.status.is_some() || result.stop != InstrStop::OutOfGas { - return; - } - - let (kind, address) = match tx.kind() { - TxKind::Call(address) => (CallKind::Call, address), - TxKind::Create => (CallKind::Create, tx.signer().create(tx.nonce())), - }; - self.traces.arena[0].trace = CallTrace { - caller: tx.signer(), - address, - kind, - value: tx.value(), - data: if self.config.record_inputs { tx.input().clone() } else { Bytes::new() }, - gas_limit: tx.gas_limit(), - gas_used: result.tx_gas_used(), - status: Some(InstrStop::OutOfGas), - ..Default::default() - }; - // Do not guess the EVM features from the halt or its gas accounting. The normal hooks did - // not run, and the transaction result does not identify the active fork. - } } impl Inspector for TracingInspector { @@ -742,6 +685,20 @@ impl Inspector for TracingInspector { } } + #[inline] + fn inspector_config(&self) -> InspectorConfig { + // The step hooks record steps, and halt the execution once a halting byte budget is + // exceeded, which any hook can cause. + let steps = if self.budget.can_halt() { + OpcodeSet::ALL + } else if self.config.record_steps { + self.config.record_opcodes_filter.unwrap_or(OpcodeSet::ALL) + } else { + OpcodeSet::EMPTY + }; + InspectorConfig::new().with_steps(steps) + } + #[inline] fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { if self.config.record_steps { @@ -762,6 +719,11 @@ impl Inspector for TracingInspector { if self.config.record_logs { let log_count = self.log_count; self.log_count += 1; + // Embedders can emit logs outside any call, before the first or after the last. They + // still take a log index but have no trace to attach to. + if self.trace_stack.is_empty() { + return; + } let trace = self.last_trace(); trace.ordering.push(TraceMemberOrder::Log(trace.logs.len())); trace.logs.push( @@ -781,6 +743,7 @@ impl Inspector for TracingInspector { self.spec_id = Some(interp.spec()); } self.features = interp.version().features; + self.finish_step_charge(interp, 0); // determine correct `from` and `to` based on the call scheme let (from, to) = match message.kind { @@ -790,23 +753,21 @@ impl Inspector for TracingInspector { _ => (message.caller, message.destination), }; - let value = if matches!(message.kind, MessageKind::DelegateCall) { - // for delegate calls we need to use the value of the top trace - if let Some(parent) = self.active_trace() { parent.trace.value } else { message.value } - } else { - message.value - }; - - // if calls to precompiles should be excluded, check whether this is a call to a precompile - let maybe_precompile = self - .config - .exclude_precompile_calls - .then(|| self.is_precompile_call(interp.host(), &to, &value)); + // Builders decide whether to leave out calls to precompiles, so record which calls are. + let maybe_precompile = self.config.record_precompile_calls.then(|| { + self.precompiles + .get_or_insert_with(|| { + let mut precompiles = PrewarmSet::new(); + interp.host().precompiles().warm_addresses(&mut precompiles); + precompiles + }) + .is_warm(&to) + }); let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) { - message.input.clone() + message.input.to_bytes(interp.memory()) } else { Bytes::new() }; @@ -814,7 +775,7 @@ impl Inspector for TracingInspector { usize::from(message.depth), to, input, - value, + message.value, message.kind.into(), from, message.gas_limit, @@ -844,11 +805,12 @@ impl Inspector for TracingInspector { self.spec_id = Some(interp.spec()); } self.features = interp.version().features; + self.finish_step_charge(interp, message.gas_limit); let input = if self.config.record_inputs && (self.trace_stack.is_empty() || self.budget.reserve(message.input.len())) { - message.input.clone() + message.input.to_bytes(interp.memory()) } else { Bytes::new() }; @@ -868,22 +830,11 @@ impl Inspector for TracingInspector { fn create_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, - message: &Message, + _interp: &mut Interpreter<'_, '_, T>, + _message: &Message, result: &mut MessageResult, ) { - let trace_idx = self.last_trace_idx(); self.fill_trace_on_call_end(result); - // A nested CREATE whose creator nonce cannot be incremented returns successfully but - // without an address, leaving its gas unspent. - if message.depth > interp.version().initial_call_depth - && result.created_address.is_none() - && result.stop == InstrStop::Return - { - let trace = &mut self.traces.arena[trace_idx].trace; - trace.status = Some(InstrStop::NonceOverflow); - trace.success = false; - } } fn selfdestruct( @@ -893,6 +844,9 @@ impl Inspector for TracingInspector { value: &U256, _host: &mut T::Host<'_>, ) { + if self.trace_stack.is_empty() { + return; + } let node = self.last_trace(); node.trace.selfdestruct_address = Some(*contract); node.trace.selfdestruct_refund_target = Some(*target); @@ -948,6 +902,25 @@ impl From for TransactionContext { } } +/// Returns the storage access an SLOAD or SSTORE is about to make, read before the opcode runs. +fn storage_change( + interp: &mut Interpreter<'_, '_, T>, + op: u8, +) -> Option> { + let (key, value, reason) = match (op, interp.stack().as_slice()) { + (op::SLOAD, [.., key]) => (*key, None, StorageChangeReason::SLOAD), + (op::SSTORE, [.., value, key]) => (*key, Some(*value), StorageChangeReason::SSTORE), + _ => return None, + }; + let address = interp.message().destination; + // A database error leaves the record empty: an opcode that reads the slot fails on it too. + let current = interp.host().state_mut().storage_slot_untracked(&address, &key).ok()?; + Some(Box::new(match value { + Some(value) => StorageChange { key, value, had_value: Some(current), reason }, + None => StorageChange { key, value: current, had_value: None, reason }, + })) +} + /// Returns the memory range whose contents after execution the opcode reports, derived from its /// inputs on the stack. /// @@ -984,3 +957,56 @@ struct TraceStackEntry { trace_idx: usize, log_count: usize, } + +/// A step waiting for `step_end`. +#[derive(Clone, Copy, Debug)] +struct PendingStep { + trace_idx: usize, + step_idx: usize, + /// The remaining gas plus spilled state gas when the step started. + gas_before: u64, + /// The same measure once a call or create step charged everything it charges before its + /// message starts. + gas_after_charge: Option, +} + +impl PendingStep { + const fn new(trace_idx: usize, step_idx: usize, gas_before: u64) -> Self { + Self { trace_idx, step_idx, gas_before, gas_after_charge: None } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use evm2::{BaseEvmTypes, Evm, Precompiles, ethereum::ethereum_tx_registry, evm::InMemoryDB}; + + #[test] + fn log_and_selfdestruct_without_active_frame() { + let mut inspector = TracingInspector::new(TracingInspectorConfig::none().record_logs()); + let mut host = Evm::::new( + SpecId::CANCUN, + Default::default(), + ethereum_tx_registry(SpecId::CANCUN), + InMemoryDB::default(), + Precompiles::base(SpecId::CANCUN), + ); + let traces = inspector.traces.clone(); + + Inspector::::log(&mut inspector, &Log::default(), &mut host); + + assert_eq!(inspector.log_count, 1); + assert!(inspector.traces.nodes().iter().all(|node| node.logs.is_empty())); + assert_eq!(inspector.traces, traces); + + Inspector::::selfdestruct( + &mut inspector, + &Address::ZERO, + &Address::ZERO, + &U256::ZERO, + &mut host, + ); + + assert_eq!(inspector.traces, traces); + } +} diff --git a/crates/inspectors/src/tracing/mux.rs b/crates/inspectors/src/tracing/mux.rs index 66deafd1f..bf97e5d30 100644 --- a/crates/inspectors/src/tracing/mux.rs +++ b/crates/inspectors/src/tracing/mux.rs @@ -1,22 +1,25 @@ -use crate::tracing::{FourByteInspector, TracingInspector, TracingInspectorConfig, final_refunded}; -use alloc::vec::Vec; +use crate::tracing::{ + DebugInspector, DebugInspectorError, FourByteInspector, TracingInspector, + TracingInspectorConfig, TransactionContext, utils::transaction_info, +}; +use alloc::{boxed::Box, vec::Vec}; use alloy_consensus::{Transaction, transaction::Recovered}; use alloy_primitives::{Address, Log, U256, map::HashMap}; -use alloy_rpc_types_eth::TransactionInfo; use alloy_rpc_types_trace::geth::{ - CallConfig, FlatCallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerType, - NoopFrame, PreStateConfig, StateGasTrace, + CallConfig, FlatCallConfig, FourByteFrame, GethDebugBuiltInTracerType, GethDebugTracerConfig, + GethDebugTracerType, GethDebugTracingOptions, NoopFrame, PreStateConfig, StateGasTrace, mux::{MuxConfig, MuxFrame}, }; use evm2::{ - EvmTypes, EvmTypesHost, Inspector, TxResultExt, TxResultWithState, - evm::{DbResult, DynDatabase}, + DatabaseError, EvmTypes, EvmTypesHost, Inspector, InspectorConfig, TxResultWithState, + env::BlockEnv, + evm::DynDatabase, interpreter::{Interpreter, Message, MessageResult}, }; use thiserror::Error; /// Mux tracing inspector that runs and collects results of multiple inspectors at once. -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct MuxInspector { /// An instance of FourByteInspector that can be reused four_byte: Option, @@ -24,6 +27,8 @@ pub struct MuxInspector { tracing: Option, /// Configurations for different Geth trace types configs: Vec<(GethDebugBuiltInTracerType, TraceConfig)>, + /// Tracers that run their own inspector, keyed by their tracer type. + children: Vec<(GethDebugTracerType, DebugInspector)>, } /// Holds all Geth supported trace configurations @@ -42,12 +47,17 @@ impl MuxInspector { let mut four_byte = None; let mut inspector_config = TracingInspectorConfig::none(); let mut configs = Vec::new(); + let mut children = Vec::new(); // Process each tracer configuration for (tracer_type, tracer_config) in config.0 { - let builtin = match tracer_type { - GethDebugTracerType::BuiltInTracer(b) => b, - _ => return Err(Error::UnsupportedTracerType(tracer_type)), + let builtin = match &tracer_type { + GethDebugTracerType::BuiltInTracer(builtin) => *builtin, + // geth evaluates a name that is not a built-in tracer as JavaScript. + GethDebugTracerType::JsTracer(_) => { + children.push(Self::child(tracer_type, tracer_config)?); + continue; + } }; #[allow(unreachable_patterns)] match builtin { @@ -79,40 +89,107 @@ impl MuxInspector { let flatcall_config = tracer_config.unwrap_or_default().into_flat_call_config()?; - inspector_config - .merge(TracingInspectorConfig::from_flat_call_config(&flatcall_config)); + inspector_config.merge(TracingInspectorConfig::default_parity()); configs.push((builtin, TraceConfig::FlatCall(flatcall_config))); } - GethDebugBuiltInTracerType::MuxTracer => { - return Err(Error::UnexpectedConfig(builtin)); + GethDebugBuiltInTracerType::MuxTracer + | GethDebugBuiltInTracerType::Erc7562Tracer => { + children.push(Self::child(tracer_type, tracer_config)?); } _ => { // keep this so that new variants can be supported - return Err(Error::UnexpectedConfig(builtin)); + return Err(Error::UnsupportedTracerType(tracer_type)); } } } let tracing = (!configs.is_empty()).then(|| TracingInspector::new(inspector_config)); - Ok(Self { four_byte, tracing, configs }) + Ok(Self { four_byte, tracing, configs, children }) + } + + /// Creates the inspector of a tracer that does not share the recording of the mux. + fn child( + tracer_type: GethDebugTracerType, + tracer_config: Option, + ) -> Result<(GethDebugTracerType, DebugInspector), Error> { + let options = GethDebugTracingOptions { + tracer: Some(tracer_type.clone()), + tracer_config: tracer_config.unwrap_or_default(), + ..Default::default() + }; + Ok((tracer_type, DebugInspector::new(options).map_err(Box::new)?)) + } + + /// Creates a copy of this inspector, with copies of the inspectors of its nested tracers. + /// + /// A JavaScript tracer shares its interrupt handle with its copy. + pub fn try_clone(&self) -> Result { + let children = self + .children + .iter() + .map(|(tracer_type, child)| { + Ok((tracer_type.clone(), child.try_clone().map_err(Box::new)?)) + }) + .collect::>()?; + Ok(Self { + four_byte: self.four_byte.clone(), + tracing: self.tracing.clone(), + configs: self.configs.clone(), + children, + }) + } + + /// Prepares the inspector for executing the next transaction. This removes any state from the + /// previous transaction. + pub fn fuse(&mut self) -> Result<(), Error> { + if let Some(inspector) = &mut self.four_byte { + core::mem::take(inspector); + } + if let Some(inspector) = &mut self.tracing { + inspector.fuse(); + } + for (_, child) in &mut self.children { + child.fuse().map_err(Box::new)?; + } + Ok(()) + } + + /// Sets the handle that interrupts the JavaScript tracers of this mux, including those of + /// nested mux tracers, see [`JsInspector::with_interrupt`](crate::tracing::js::JsInspector). + #[cfg(feature = "js-tracer")] + pub fn set_js_interrupt(&mut self, interrupt: &crate::tracing::js::JsInspectorInterrupt) { + for (_, child) in &mut self.children { + child.set_js_interrupt(interrupt); + } } /// Sets the index the next recorded log receives, see /// [`TracingInspector::set_next_log_index`]. - pub const fn set_next_log_index(&mut self, index: usize) { + pub fn set_next_log_index(&mut self, index: usize) { if let Some(tracing) = &mut self.tracing { tracing.set_next_log_index(index); } + for (_, child) in &mut self.children { + child.set_next_log_index(index); + } } /// Try converting this [MuxInspector] into a [MuxFrame]. - pub fn try_into_mux_frame( - &self, + pub fn try_into_mux_frame>( + &mut self, result: &TxResultWithState, + tx: &Recovered, + block_env: &BlockEnv, + tx_context: Option, db: &mut dyn DynDatabase, - tx_info: TransactionInfo, - ) -> DbResult { + ) -> Result { + if let Some(inspector) = &mut self.tracing { + inspector.set_transaction_gas_limit(tx.gas_limit()); + inspector.set_transaction_caller(tx.signer()); + } + let tx_info = transaction_info::(tx_context.as_ref(), block_env); + let mut frame = HashMap::with_capacity_and_hasher(self.configs.len(), Default::default()); for (tracer_type, config) in &self.configs { @@ -137,11 +214,11 @@ impl MuxInspector { continue; } } - TraceConfig::FlatCall(_flatcall_config) => { + TraceConfig::FlatCall(flatcall_config) => { if let Some(inspector) = &self.tracing { inspector .clone() - .into_parity_builder() + .into_flat_call_builder(flatcall_config) .with_transaction_gas_used(result.result.tx_gas_used()) .into_localized_transaction_traces(tx_info) .into() @@ -154,7 +231,7 @@ impl MuxInspector { gas_used: result.result.tx_gas_used(), execution_gas_used: result.result.execution_gas_spent(), state_gas_used: result.result.state_gas_spent(), - gas_refund: final_refunded(&result.result), + gas_refund: result.result.final_refunded(), } .into(), }; @@ -170,62 +247,62 @@ impl MuxInspector { ); } + for (tracer_type, child) in &mut self.children { + let trace = + child.get_result(tx_context, tx, block_env, result, db).map_err(Box::new)?; + frame.insert(tracer_type.clone(), trace); + } + Ok(MuxFrame(frame)) } +} - /// Finalizes the transaction root for tracers sharing the tracing inspector. - pub(crate) fn finalize_transaction( - &mut self, - tx: &Recovered, - result: &TxResultExt, - ) { - if let Some(inspector) = &mut self.tracing { - inspector.recover_runtime_oog_root(tx, result); - inspector.set_transaction_gas_limit(tx.gas_limit()); - inspector.set_transaction_caller(tx.signer()); +/// Calls a hook on every inspector of the mux. +macro_rules! fan_out { + ($mux:ident.$hook:ident($($arg:expr),*)) => {{ + for (_, child) in &mut $mux.children { + Inspector::::$hook(child, $($arg),*); } - } + if let Some(inspector) = &mut $mux.four_byte { + Inspector::::$hook(inspector, $($arg),*); + } + if let Some(inspector) = &mut $mux.tracing { + Inspector::::$hook(inspector, $($arg),*); + } + }}; } impl Inspector for MuxInspector { #[inline] fn initialize_interp(&mut self, interp: &mut Interpreter<'_, '_, T>) { - if let Some(ref mut inspector) = self.four_byte { - inspector.initialize_interp(interp); - } - if let Some(ref mut inspector) = self.tracing { - inspector.initialize_interp(interp); - } + fan_out!(self.initialize_interp(interp)); + } + + #[inline] + fn inspector_config(&self) -> InspectorConfig { + let children = + self.children.iter().map(|(_, child)| Inspector::::inspector_config(child)); + let four_byte = self.four_byte.iter().map(Inspector::::inspector_config); + let tracing = self.tracing.iter().map(Inspector::::inspector_config); + children + .chain(four_byte) + .chain(tracing) + .fold(InspectorConfig::without_steps(), InspectorConfig::union) } #[inline] fn step(&mut self, interp: &mut Interpreter<'_, '_, T>) { - if let Some(ref mut inspector) = self.four_byte { - inspector.step(interp); - } - if let Some(ref mut inspector) = self.tracing { - inspector.step(interp); - } + fan_out!(self.step(interp)); } #[inline] fn step_end(&mut self, interp: &mut Interpreter<'_, '_, T>) { - if let Some(ref mut inspector) = self.four_byte { - inspector.step_end(interp); - } - if let Some(ref mut inspector) = self.tracing { - inspector.step_end(interp); - } + fan_out!(self.step_end(interp)); } #[inline] fn log(&mut self, log: &Log, host: &mut T::Host<'_>) { - if let Some(ref mut inspector) = self.four_byte { - >::log(inspector, log, host); - } - if let Some(ref mut inspector) = self.tracing { - >::log(inspector, log, host); - } + fan_out!(self.log(log, host)); } #[inline] @@ -234,13 +311,13 @@ impl Inspector for MuxInspector { interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - if let Some(ref mut inspector) = self.four_byte { - let _ = inspector.call(interp, message); + for (_, child) in &mut self.children { + let _ = child.call(interp, message); } - if let Some(ref mut inspector) = self.tracing { - return inspector.call(interp, message); + if let Some(inspector) = &mut self.four_byte { + let _ = inspector.call(interp, message); } - None + self.tracing.as_mut().and_then(|inspector| inspector.call(interp, message)) } #[inline] @@ -250,12 +327,7 @@ impl Inspector for MuxInspector { message: &Message, result: &mut MessageResult, ) { - if let Some(ref mut inspector) = self.four_byte { - inspector.call_end(interp, message, result); - } - if let Some(ref mut inspector) = self.tracing { - inspector.call_end(interp, message, result); - } + fan_out!(self.call_end(interp, message, result)); } #[inline] @@ -264,13 +336,13 @@ impl Inspector for MuxInspector { interp: &mut Interpreter<'_, '_, T>, message: &mut Message, ) -> Option> { - if let Some(ref mut inspector) = self.four_byte { - let _ = inspector.create(interp, message); + for (_, child) in &mut self.children { + let _ = child.create(interp, message); } - if let Some(ref mut inspector) = self.tracing { - return inspector.create(interp, message); + if let Some(inspector) = &mut self.four_byte { + let _ = inspector.create(interp, message); } - None + self.tracing.as_mut().and_then(|inspector| inspector.create(interp, message)) } #[inline] @@ -280,12 +352,7 @@ impl Inspector for MuxInspector { message: &Message, result: &mut MessageResult, ) { - if let Some(ref mut inspector) = self.four_byte { - inspector.create_end(interp, message, result); - } - if let Some(ref mut inspector) = self.tracing { - inspector.create_end(interp, message, result); - } + fan_out!(self.create_end(interp, message, result)); } #[inline] @@ -296,29 +363,23 @@ impl Inspector for MuxInspector { value: &U256, host: &mut T::Host<'_>, ) { - if let Some(ref mut inspector) = self.four_byte { - >::selfdestruct( - inspector, contract, target, value, host, - ); - } - if let Some(ref mut inspector) = self.tracing { - >::selfdestruct( - inspector, contract, target, value, host, - ); - } + fan_out!(self.selfdestruct(contract, target, value, host)); } } /// Error type for [MuxInspector] #[derive(Debug, Error)] pub enum Error { - /// Config was provided for a tracer that does not expect it - #[error("unexpected config for tracer '{0:?}'")] - UnexpectedConfig(GethDebugBuiltInTracerType), - /// Unsupported tracer type (e.g. JS tracer) + /// Unsupported tracer type #[error("unsupported tracer type: '{0:?}'")] UnsupportedTracerType(GethDebugTracerType), /// Error when deserializing the config #[error("error deserializing config: {0}")] InvalidConfig(#[from] serde_json::Error), + /// Error from the inspector of a nested tracer + #[error(transparent)] + Child(#[from] Box), + /// Database operation failed + #[error("database error {0:?}")] + Database(#[from] DatabaseError), } diff --git a/crates/inspectors/src/tracing/tx_state.rs b/crates/inspectors/src/tracing/tx_state.rs deleted file mode 100644 index 2cd97b391..000000000 --- a/crates/inspectors/src/tracing/tx_state.rs +++ /dev/null @@ -1,107 +0,0 @@ -//! Materialized per-transaction state collected from a pending-state stream. - -use alloy_primitives::{ - Address, - map::{AddressMap, U256Map}, -}; -use core::convert::Infallible; -use evm2::{ - evm::{ - AccountChangeRef, AccountInfo, PendingState, StateChangeSink, StateChangeSource, - StorageChange, Tracked, - }, - interpreter::Word, -}; - -/// A transaction's loaded accounts and storage, materialized from the [`PendingState`] change -/// stream for random access by trace builders. -/// -/// Contains every account and storage slot the transaction loaded: changed entries arrive through -/// the [`StateChangeSink`] change callbacks, loaded-but-unchanged ones through its read callbacks. -#[derive(Debug, Default)] -pub(crate) struct TxState { - /// Accounts loaded by the transaction, keyed by address. - pub(crate) accounts: AddressMap, -} - -impl TxState { - /// Collects a transaction's detached pending state into a materialized view. - pub(crate) fn from_pending(pending: &PendingState) -> Self { - let mut state = Self::default(); - let Ok(()) = pending.visit(&mut state); - state - } -} - -/// One account loaded by the transaction, with its loaded storage slots. -#[derive(Debug, Default)] -pub(crate) struct TxAccount { - /// Account info at the start of the transaction. `None` means the account did not exist. - pub(crate) original: Option, - /// Account info after the transaction. `None` is an explicit deletion. - pub(crate) current: Option, - /// Whether the account was created during the transaction. - pub(crate) created: bool, - /// Whether the account was selfdestructed during the transaction. - pub(crate) selfdestructed: bool, - /// Storage slots loaded during the transaction, changed and unchanged. - pub(crate) storage: U256Map>, -} - -impl TxAccount { - /// Returns the storage slots the transaction changed. - pub(crate) fn changed_storage(&self) -> impl Iterator)> { - self.storage.iter().filter(|(_, slot)| slot.is_changed()) - } -} - -fn clone_account_info(target: &mut Option, source: Option<&AccountInfo>) { - match (target, source) { - (Some(target), Some(source)) => target.clone_from(source), - (target, Some(source)) => *target = Some(source.clone()), - (target, None) => *target = None, - } -} - -impl StateChangeSink for TxState { - type Error = Infallible; - - fn account(&mut self, change: AccountChangeRef<'_>) -> Result<(), Self::Error> { - let entry = self.accounts.entry(change.address).or_default(); - clone_account_info(&mut entry.original, change.original); - clone_account_info(&mut entry.current, change.current); - entry.created = change.created; - entry.selfdestructed = change.selfdestructed; - Ok(()) - } - - fn storage(&mut self, change: StorageChange) -> Result<(), Self::Error> { - self.accounts - .entry(change.address) - .or_default() - .storage - .insert(change.key, Tracked::from_parts(change.original, change.current)); - Ok(()) - } - - fn account_read( - &mut self, - address: Address, - info: Option<&AccountInfo>, - ) -> Result<(), Self::Error> { - let entry = self.accounts.entry(address).or_default(); - clone_account_info(&mut entry.original, info); - entry.current.clone_from(&entry.original); - Ok(()) - } - - fn storage_read( - &mut self, - address: Address, - key: Word, - value: Word, - ) -> Result<(), Self::Error> { - self.accounts.entry(address).or_default().storage.insert(key, Tracked::new(value)); - Ok(()) - } -} diff --git a/crates/inspectors/src/tracing/types.rs b/crates/inspectors/src/tracing/types.rs index 9ae3645f7..e8e6b31c4 100644 --- a/crates/inspectors/src/tracing/types.rs +++ b/crates/inspectors/src/tracing/types.rs @@ -51,7 +51,8 @@ pub struct DecodedCallTrace { pub struct CallTrace { /// The inspector-visible depth of the call. /// - /// The root starts at [`evm2::Version::initial_call_depth`], normally zero. + /// Top-level calls start at [`evm2::Evm::initial_call_depth`], normally zero. Above zero, + /// they are children of an empty root at depth zero. /// /// The depth of a call's steps is the depth of the call plus one. pub depth: usize, @@ -67,7 +68,9 @@ pub struct CallTrace { pub address: Address, /// Whether this is a call to a precompile. /// - /// Note: This is optional because not all tracers make use of this. + /// Only recorded if + /// [`TracingInspectorConfig::record_precompile_calls`](crate::tracing::TracingInspectorConfig::record_precompile_calls) + /// is set, otherwise `None`. pub maybe_precompile: Option, /// The address of the selfdestructed contract. pub selfdestruct_address: Option
, @@ -116,8 +119,8 @@ pub struct CallTrace { pub steps: Vec, /// The deltas recorded for [`Self::steps`], in step order. /// - /// Only steps that report memory or storage, make a call or gain gas have an entry, and only - /// if [`record_step_deltas`] is enabled. + /// Only steps that report memory or storage, make a call or gain gas have an entry. Memory + /// and storage require [`record_step_deltas`]; gas balances are recorded independently. /// /// [`record_step_deltas`]: crate::tracing::TracingInspectorConfig::record_step_deltas #[cfg_attr(feature = "serde", serde(default))] @@ -139,7 +142,7 @@ impl CallTrace { /// Returns true if the status code is a revert. #[inline] pub fn is_revert(&self) -> bool { - self.status.is_some_and(|status| status == InstrStop::Revert) + self.status.is_some_and(InstrStop::is_revert) } /// Returns `true` if this trace was a selfdestruct. @@ -279,14 +282,23 @@ impl CallTraceNode { if self.trace.kind.is_delegate() { self.trace.caller } else { self.trace.address } } - /// Iterates over steps in execution order, matching call opcodes to recorded children. + /// Iterates over steps in execution order, pairing each step with the child it called. + /// + /// A child is recorded right after the step that called it, so the node's `ordering` locates + /// it. A call opcode without a following child either failed before entering a frame, e.g. due + /// to out of gas (), or called an excluded + /// precompile that was not attached to this node. pub(crate) fn steps_with_children(&self) -> impl Iterator> { - let mut children = self.children.iter(); - self.trace.steps.iter().map(move |step| { - // A call opcode can fail before a child is recorded, e.g. due to out of gas: - // . - let call_child_id = - if step.is_call_like_op() { children.next().copied() } else { None }; + let mut ordering = self.ordering.iter().peekable(); + self.trace.steps.iter().enumerate().map(move |(step_idx, step)| { + ordering + .find(|member| matches!(member, TraceMemberOrder::Step(idx) if *idx == step_idx)); + let call_child_id = match ordering.next_if(|member| { + step.is_call_like_op() && matches!(member, TraceMemberOrder::Call(_)) + }) { + Some(TraceMemberOrder::Call(child)) => self.children.get(*child).copied(), + _ => None, + }; CallTraceStepStackItem { trace_node: self, step, call_child_id } }) } @@ -445,14 +457,6 @@ impl CallTraceNode { call_frame.to = None; } - if !self - .status() - .is_some_and(|status| status.is_revert() || status == InstrStop::NonceOverflow) - { - call_frame.gas_used = U256::from(self.trace.gas_limit); - call_frame.output = None; - } - call_frame.revert_reason = utils::maybe_revert_reason(self.trace.output.as_ref()); // Note: regular calltracer uses geth errors, only flatCallTracer uses parity errors: @@ -664,8 +668,8 @@ pub struct CallTraceStep { pub gas_remaining: u64, /// Gas refund counter before step execution pub gas_refund_counter: u64, - /// Total gas used before step execution - pub gas_used: u64, + /// The storage access of an SLOAD/SSTORE. Only set if state diffs are recorded. + pub storage_change: Option>, // Fields filled in `step_end` /// Gas cost of step execution pub gas_cost: u64, @@ -676,8 +680,6 @@ pub struct CallTraceStep { /// State-gas spent before this step, used to calculate `state_gas_cost`. #[cfg_attr(feature = "serde", serde(skip))] pub state_gas_spent: i64, - /// Change of the contract state after step execution (effect of the SLOAD/SSTORE instructions) - pub storage_change: Option>, /// Final status of the step /// /// This is set after the step was executed. @@ -711,10 +713,14 @@ impl CallTraceStep { format!("opcode {:#x} not defined", self.op.get()).into() }, pc: self.pc as u64, - refund_counter: Some(self.gas_refund_counter), + // geth omits a zero refund and empty memory and return data. + refund_counter: (self.gas_refund_counter != 0).then_some(self.gas_refund_counter), stack, memory: if opts.is_memory_enabled() { - self.memory.as_ref().map(RecordedMemory::memory_chunks) + self.memory + .as_ref() + .map(RecordedMemory::memory_chunks) + .filter(|memory| !memory.is_empty()) } else { None }, @@ -763,7 +769,8 @@ impl CallTraceStep { /// The deltas a [`CallTraceStep`] produced, as reported by parity's `vmTrace`. /// /// Recorded in [`CallTrace::step_deltas`] for the steps that report memory or storage, make a -/// call or gain gas, when [`record_step_deltas`] is enabled. +/// call or gain gas. Memory and storage require [`record_step_deltas`]; gas balances are recorded +/// independently. /// /// [`record_step_deltas`]: crate::tracing::TracingInspectorConfig::record_step_deltas #[derive(Clone, Debug, Default, PartialEq, Eq)] @@ -776,7 +783,8 @@ pub struct StepDelta { /// The storage written by an `SSTORE`, taken from its operands, so it is also set when the /// value does not change. pub store: Option, - /// The remaining gas after a call-like step or an instruction that gained gas. + /// The remaining gas after a call-like step or a step whose remaining gas after execution is + /// not `gas_remaining - gas_cost`, such as one that gained gas or drew state gas from it. /// /// For all other steps the remaining gas after execution is `gas_remaining - gas_cost`. pub gas_remaining_after: Option, @@ -814,20 +822,18 @@ pub enum StorageChangeReason { SSTORE, } -/// Represents a storage change during execution. -/// -/// This maps to evm storage journal entries. +/// Represents the storage access of an SLOAD or SSTORE step, read before the opcode runs. /// -/// It is used to track both storage change and warm load of a storage slot. For warm load in regard -/// to EIP-2929 AccessList had_value will be None. +/// For an SLOAD, `value` is the value the slot holds and `had_value` is `None`. For an SSTORE, +/// `value` is the value written and `had_value` is the value the slot held before. #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct StorageChange { /// key of the storage slot pub key: U256, - /// Current value of the storage slot + /// The value read by an SLOAD, or written by an SSTORE pub value: U256, - /// The previous value of the storage slot, if any + /// The value an SSTORE overwrites, `None` for an SLOAD pub had_value: Option, /// How this storage was accessed pub reason: StorageChangeReason, diff --git a/crates/inspectors/src/tracing/utils.rs b/crates/inspectors/src/tracing/utils.rs index 7eab85038..57fd57b35 100644 --- a/crates/inspectors/src/tracing/utils.rs +++ b/crates/inspectors/src/tracing/utils.rs @@ -1,16 +1,18 @@ //! Utility functions for evm2 related ops. -use crate::tracing::config::TraceStyle; +use crate::tracing::{TransactionContext, config::TraceStyle}; use alloc::{ format, string::{String, ToString}, vec::Vec, }; use alloy_primitives::{Bytes, hex}; +use alloy_rpc_types_eth::TransactionInfo; use alloy_sol_types::{ContractError, GenericRevertReason}; use evm2::{ - AccountInfo, SpecId, + AccountInfo, EvmTypesHost, + env::BlockEnv, evm::{DbResult, DynDatabase}, - interpreter::InstrStop, + interpreter::{InstrStop, Result}, }; /// Converts a non successful [`InstrStop`] to an error message. @@ -127,13 +129,6 @@ pub(crate) fn convert_memory(data: &[u8]) -> Vec { memory } -/// Get the gas used, accounting for refunds -#[inline] -pub(crate) fn gas_used(spec: SpecId, spent: u64, refunded: u64) -> u64 { - let refund_quotient = if spec.enables(SpecId::LONDON) { 5 } else { 2 }; - spent - (refunded).min(spent / refund_quotient) -} - /// Loads the code for the given account from the account itself or the database /// /// Returns None if the code hash is the KECCAK_EMPTY hash @@ -176,6 +171,35 @@ pub(crate) fn maybe_revert_reason(output: &[u8]) -> Option { } } +/// Returns the [`TransactionInfo`] of a traced transaction. +pub(crate) fn transaction_info( + tx_context: Option<&TransactionContext>, + block_env: &BlockEnv, +) -> TransactionInfo { + #[allow(clippy::needless_update)] + TransactionInfo { + hash: tx_context.and_then(|c| c.tx_hash), + index: tx_context.and_then(|c| c.tx_index.map(|i| i as u64)), + block_hash: tx_context.and_then(|c| c.block_hash), + block_number: Some(block_env.number.saturating_to()), + base_fee: Some(block_env.basefee.saturating_to()), + ..Default::default() + } +} + +/// Returns the gas cost of a step that stopped with `result`. +/// +/// A step that ran out of gas costs the whole `gas_remaining` it started with, except the +/// reentrancy sentry, which rejects SSTORE before charging any gas and costs 0. Any other step +/// costs what it `charged`. +pub(crate) const fn step_cost(result: Result, gas_remaining: u64, charged: u64) -> u64 { + match result { + Err(InstrStop::ReentrancySentryOOG) => 0, + Err(stop) if stop.is_out_of_gas() => gas_remaining, + _ => charged, + } +} + #[cfg(test)] mod tests { use super::*; @@ -268,7 +292,7 @@ mod tests { "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000214661696c656420746f2076657269667920424950333232207369676e617475726500000000000000000000000000000000000000000000000000000000000000" ); let reason = maybe_revert_reason(&err[..]).unwrap(); - assert_eq!(reason, "Failed to verify BIP322 signature".to_string()); + assert_eq!(reason, "Failed to verify BIP322 signature"); } #[test] diff --git a/crates/inspectors/src/tracing/writer.rs b/crates/inspectors/src/tracing/writer.rs index 26c249f66..81edc4fd9 100644 --- a/crates/inspectors/src/tracing/writer.rs +++ b/crates/inspectors/src/tracing/writer.rs @@ -10,7 +10,7 @@ use alloy_primitives::{Address, B256, U256, address, hex, map::HashMap}; use anstyle::{AnsiColor, Color, Style}; use colorchoice::ColorChoice; use evm2::interpreter::InstrStop; -use std::io::{self, Write}; +use std::io::{self, IsTerminal, Write}; const CHEATCODE_ADDRESS: Address = address!("7109709ECfa91a80626fF3989D68f67F5b1DD12D"); @@ -376,7 +376,8 @@ impl TraceWriter { match &**decoded { DecodedTraceStep::InternalCall(call, end_idx) => { - let gas_used = node.trace.steps[*end_idx].gas_used.saturating_sub(step.gas_used); + let gas_used = + step.gas_remaining.saturating_sub(node.trace.steps[*end_idx].gas_remaining); self.write_branch()?; self.indentation_level += 1; @@ -505,15 +506,16 @@ impl TraceWriter { // of their last write. let mut change_id = 0; for step in &node.trace.steps { - if let Some(change) = &step.storage_change { - // Only handle storage changes, signalled by `had_value` being `Some`. - if change.had_value.is_some() { - let (_first, last, last_change_id) = - changes_map.entry(&change.key).or_insert((change, change, change_id)); - *last = change; - *last_change_id = change_id; - change_id += 1; - } + // Only handle storage changes, signalled by `had_value` differing from `value`. + if !step.is_error() + && let Some(change) = &step.storage_change + && change.had_value.is_some_and(|had| had != change.value) + { + let (_first, last, last_change_id) = + changes_map.entry(&change.key).or_insert((change, change, change_id)); + *last = change; + *last_change_id = change_id; + change_id += 1; } } @@ -552,7 +554,6 @@ impl TraceWriter { } fn use_colors(choice: ColorChoice) -> bool { - use io::IsTerminal; match choice { ColorChoice::Auto => io::stdout().is_terminal(), ColorChoice::AlwaysAnsi | ColorChoice::Always => true, diff --git a/crates/inspectors/src/transfer.rs b/crates/inspectors/src/transfer.rs index 006f939cf..9b73dd374 100644 --- a/crates/inspectors/src/transfer.rs +++ b/crates/inspectors/src/transfer.rs @@ -1,17 +1,16 @@ //! Internal transfer inspector. use alloc::{vec, vec::Vec}; -use alloy_primitives::{Address, B256, Log, U256, address, b256}; -use alloy_sol_types::SolValue; +use alloy_primitives::{Address, B256, Log, U256, b256}; use evm2::{ - Evm, EvmTypes, Inspector, + Evm, EvmFeatures, EvmTypes, Inspector, InspectorConfig, interpreter::{Interpreter, Message, MessageKind, MessageResult}, }; /// Sender of ETH transfer log per `eth_simulateV1` spec. /// /// -pub const TRANSFER_LOG_EMITTER: Address = address!("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); +pub const TRANSFER_LOG_EMITTER: Address = Address::repeat_byte(0xee); /// Topic of `Transfer(address,address,uint256)` event. pub const TRANSFER_EVENT_TOPIC: B256 = @@ -22,19 +21,31 @@ pub const TRANSFER_EVENT_TOPIC: B256 = /// Transfers made by a call or create that fails, including transfers in its subcalls, are /// discarded together with their logs. /// +/// As in `go-ethereum`, zero-value transfers, `CALLCODE` and `DELEGATECALL` are not transfers, and +/// a `SELFDESTRUCT` is one only if the contract holds a balance. +/// +/// Like `go-ethereum`'s `eth_simulateV1` tracer, the logs are kept by the inspector and are never +/// added to the EVM's own logs, so receipts and log blooms do not include them. +/// +/// The inspector has no view of transaction errors: if a transaction stops on an error, the +/// transfers and logs recorded so far are kept and its open frames are only dropped at the next +/// top-level call. Discard the inspector after such an error instead of reusing it. +/// /// This can be used to construct an `eth_simulateV1` response. For `ots_getInternalOperations`, /// use [InternalOperationsInspector](crate::otterscan::InternalOperationsInspector). #[derive(Debug, Default, Clone)] pub struct TransferInspector { internal_only: bool, transfers: Vec, - /// Checkpoints of the call and create frames currently executing. - checkpoints: Vec, - /// If enabled, will insert ERC20-style transfer logs emitted by [TRANSFER_LOG_EMITTER] for - /// each ETH transfer. + /// The call and create frames currently executing, innermost last. + frames: Vec, + /// If enabled, collects the emitted logs together with ERC20-style transfer logs emitted by + /// [TRANSFER_LOG_EMITTER] for each ETH transfer. /// /// Can be used for [eth_simulateV1](https://github.com/ethereum/execution-apis/pull/484) execution. - insert_logs: bool, + collect_logs: bool, + /// Logs of the top-level frames that succeeded, and logs emitted outside any frame. + logs: Vec, } impl TransferInspector { @@ -43,7 +54,13 @@ impl TransferInspector { /// If `internal_only` is set to `true`, only internal transfers are collected, in other words, /// the top level call is ignored. pub const fn new(internal_only: bool) -> Self { - Self { internal_only, transfers: Vec::new(), checkpoints: Vec::new(), insert_logs: false } + Self { + internal_only, + transfers: Vec::new(), + frames: Vec::new(), + collect_logs: false, + logs: Vec::new(), + } } /// Creates a new transfer inspector that only collects internal transfers. @@ -56,12 +73,27 @@ impl TransferInspector { self.transfers } - /// Sets whether to insert ERC20-style transfer logs. - pub const fn with_logs(mut self, insert_logs: bool) -> Self { - self.insert_logs = insert_logs; + /// Sets whether to collect the emitted logs and the ERC20-style transfer logs. + /// + /// Once EIP-7708 is active the EVM emits the transfer logs itself, so only those are + /// collected, as in `eth_simulateV1`. + pub const fn with_logs(mut self, collect_logs: bool) -> Self { + self.collect_logs = collect_logs; self } + /// Returns a reference to the logs of the succeeded top-level frames and the logs emitted + /// outside any frame, in emission order. + pub fn logs(&self) -> &[Log] { + &self.logs + } + + /// Takes the logs of the succeeded top-level frames and the logs emitted outside any frame, + /// in emission order. + pub fn take_logs(&mut self) -> Vec { + core::mem::take(&mut self.logs) + } + /// Returns a reference to the collected transfers. pub fn transfers(&self) -> &[TransferOperation] { &self.transfers @@ -73,54 +105,84 @@ impl TransferInspector { } fn on_transfer(&mut self, message: &Message, host: &mut Evm<'_, T>) { - self.checkpoints - .push(TransferCheckpoint { transfers: self.transfers.len(), logs: host.logs().len() }); + // A transaction that stopped on an execution error never ran its end hooks. + if message.depth == host.initial_call_depth() { + self.frames.clear(); + } + self.frames.push(TransferFrame { transfers: self.transfers.len(), logs: Vec::new() }); let kind = match message.kind { - MessageKind::Call | MessageKind::CallCode => TransferKind::Call, + MessageKind::Call => TransferKind::Call, MessageKind::Create => TransferKind::Create, MessageKind::Create2 => TransferKind::Create2, _ => return, }; // skip top level transfers - if self.internal_only && message.depth == host.version().initial_call_depth { + if self.internal_only && message.depth == 0 { return; } + + self.record_transfer(kind, message.caller, message.destination, message.value, host); + } + + fn on_transfer_end(&mut self, failed: bool) { + let Some(frame) = self.frames.pop() else { return }; + if failed { + self.transfers.truncate(frame.transfers); + } else if let Some(parent) = self.frames.last_mut() { + parent.logs.extend(frame.logs); + } else { + self.logs.extend(frame.logs); + } + } + + fn record_transfer( + &mut self, + kind: TransferKind, + from: Address, + to: Address, + value: U256, + host: &Evm<'_, T>, + ) { // skip zero transfers - let value = message.value; if value.is_zero() { return; } - let from = message.caller; - let to = message.destination; self.transfers.push(TransferOperation { kind, from, to, value }); - if self.insert_logs { - let from = B256::from_slice(&from.abi_encode()); - let to = B256::from_slice(&to.abi_encode()); - let data = value.abi_encode(); - - host.log(Log::new_unchecked( + // Once EIP-7708 is active the EVM emits the transfer logs itself. + if self.collect_logs + && !host.feature(EvmFeatures::EIP7708) + && let Some(frame) = self.frames.last_mut() + { + frame.logs.push(Log::new_unchecked( TRANSFER_LOG_EMITTER, - vec![TRANSFER_EVENT_TOPIC, from, to], - data.into(), + vec![TRANSFER_EVENT_TOPIC, from.into_word(), to.into_word()], + value.to_be_bytes::<32>().into(), )); } } +} - fn on_transfer_end(&mut self, failed: bool, host: &mut Evm<'_, T>) { - if let Some(checkpoint) = self.checkpoints.pop() - && failed - { - self.transfers.truncate(checkpoint.transfers); - host.logs_mut().truncate(checkpoint.logs); +impl Inspector for TransferInspector { + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::without_steps() + } + + fn log(&mut self, log: &Log, _host: &mut T::Host<'_>) { + if !self.collect_logs { + return; + } + match self.frames.last_mut() { + Some(frame) => frame.logs.push(log.clone()), + // Embedders can emit logs before the first call or after the last; no frame failure + // reverts them. + None => self.logs.push(log.clone()), } } -} -impl Inspector for TransferInspector { fn call( &mut self, interp: &mut Interpreter<'_, '_, T>, @@ -132,11 +194,11 @@ impl Inspector for TransferInspector { fn call_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, _message: &Message, result: &mut MessageResult, ) { - self.on_transfer_end::(!result.is_success(), interp.host()); + self.on_transfer_end(!result.is_success()); } fn create( @@ -150,14 +212,11 @@ impl Inspector for TransferInspector { fn create_end( &mut self, - interp: &mut Interpreter<'_, '_, T>, + _interp: &mut Interpreter<'_, '_, T>, _message: &Message, result: &mut MessageResult, ) { - // A create that stops early without a created address, such as on nonce overflow, also - // counts as failed even though its stop is `Return`. - let failed = !result.is_success() || result.created_address.is_none(); - self.on_transfer_end::(failed, interp.host()); + self.on_transfer_end(!result.is_success()); } fn selfdestruct( @@ -165,14 +224,9 @@ impl Inspector for TransferInspector { contract: &Address, target: &Address, value: &U256, - _host: &mut T::Host<'_>, + host: &mut T::Host<'_>, ) { - self.transfers.push(TransferOperation { - kind: TransferKind::SelfDestruct, - from: *contract, - to: *target, - value: *value, - }); + self.record_transfer(TransferKind::SelfDestruct, *contract, *target, *value, host); } } @@ -203,11 +257,11 @@ pub enum TransferKind { SelfDestruct, } -/// Transfer state at the start of a call or create frame. -#[derive(Clone, Copy, Debug)] -struct TransferCheckpoint { +/// A call or create frame currently executing. +#[derive(Clone, Debug)] +struct TransferFrame { /// Number of transfers recorded before the frame. transfers: usize, - /// Number of logs emitted before the frame. - logs: usize, + /// Logs of the frame and of its succeeded subframes. + logs: Vec, } diff --git a/crates/inspectors/tests/it/accesslist.rs b/crates/inspectors/tests/it/accesslist.rs index 98f16de92..007311c4f 100644 --- a/crates/inspectors/tests/it/accesslist.rs +++ b/crates/inspectors/tests/it/accesslist.rs @@ -1,11 +1,16 @@ //! Accesslist tests -use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, TransactTo, TxEnv}; +use crate::utils::{AccountInfo, Bytecode, Context, TransactTo, TxEnv, a}; use alloy_consensus::{TxEip7702, crypto::secp256k1::sign_message}; use alloy_eip2930::{AccessList, AccessListItem}; use alloy_eips::eip7702::Authorization; -use alloy_primitives::{Address, B256, Signature, U256, address, hex}; -use evm2::ethereum::TxEnvelope; +use alloy_primitives::{Address, B256, Bytes, Signature, TxKind, U256, address, bytes, hex}; +use evm2::{ + BaseEvmTypes, Evm, ExecutionConfig, Precompiles, SpecId, Version, + ethereum::{TxEnvelope, ethereum_tx_registry, execute_initial_frame, prepare_initial_frame}, + evm::InMemoryDB, + interpreter::GasTracker, +}; use evm2_inspectors::access_list::AccessListInspector; #[test] @@ -22,17 +27,16 @@ fn initial_access_list_is_normalized_and_pruned() { item(lower, vec![]), ]); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &callee, - AccountInfo { - // Read slot 1 so it is re-added after pruning the callee's stale seeded slot. - code: Some(Bytecode::new_raw(hex!("60015400").into())), - ..Default::default() - }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &callee, + AccountInfo { + // Read slot 1 so it is re-added after pruning the callee's stale seeded slot. + code: Some(Bytecode::new_raw(bytes!("60015400"))), + ..Default::default() + }, + ); + }); let mut inspector = AccessListInspector::new(initial); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let result = evm @@ -71,13 +75,12 @@ fn test_access_list_precompile() { let account = address!("341348115259a8bf69f1f50101c227fced83bac6"); let caller = address!("341348115259a8bf69f1f50101c227fced83bac5"); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &account, - AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &account, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + }); let mut accesslist = AccessListInspector::default(); let mut evm = context.build_mainnet().with_inspector(&mut accesslist); @@ -86,14 +89,14 @@ fn test_access_list_precompile() { caller, gas_limit: 1000000, kind: TransactTo::Call(account), - data: hex!("a5399705").into(), + data: bytes!("a5399705"), nonce: 0, ..Default::default() }) .unwrap(); assert!(res.result.is_success(), "{res:#?}"); - let erecover = address!("0x0000000000000000000000000000000000000001"); + let erecover = Address::with_last_byte(1); assert!(accesslist.excluded().contains(&erecover)); assert!(accesslist.into_access_list().is_empty()); } @@ -138,3 +141,493 @@ fn test_access_list_authorization_exclusions() { // The unrecoverable authorization has no authority and therefore adds nothing. assert_eq!(excluded.len(), 2); } + +#[test] +fn later_top_level_call_keeps_slots_touched_by_earlier_calls() { + let caller = Address::repeat_byte(0xaa); + // Calls `token` with no input. + let router = Address::with_last_byte(0x42); + // Reads the slot keyed by its caller. + let token = Address::with_last_byte(0x43); + let mut db = InMemoryDB::default(); + let code = |code: &[u8]| AccountInfo { + code: Some(Bytecode::new_raw(Bytes::copy_from_slice(code))), + ..Default::default() + }; + db.insert_account_info(&router, code(&hex!("5f5f5f5f5f60435af100"))); + db.insert_account_info(&token, code(&hex!("335400"))); + let mut inspector = AccessListInspector::default(); + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(SpecId::CANCUN, Version::new(SpecId::CANCUN)), + SpecId::CANCUN, + Default::default(), + ethereum_tx_registry(SpecId::CANCUN), + db, + Precompiles::base(SpecId::CANCUN), + ); + evm.set_inspector(&mut inspector); + + // Two top-level calls of one transaction, as in an account-abstraction batch. + for to in [router, token] { + let mut gas = GasTracker::new(100_000); + let frame = prepare_initial_frame( + &mut evm, + caller, + 0, + TxKind::Call(to), + &Bytes::new(), + U256::ZERO, + &mut gas, + ) + .unwrap(); + assert!(execute_initial_frame(&mut evm, frame, &mut gas, 100_000, 0).unwrap().is_success()); + } + drop(evm); + + // The slot `token` read under `router` stays after the second call targets `token`. + assert_eq!( + inspector.into_access_list(), + access_list(&[(token, &[router.into_word(), caller.into_word()])]) + ); +} + +/// A call run through `go-ethereum`'s `eth_createAccessList`, and the list it returned. +struct AccessListCase { + name: &'static str, + /// Address, code and balance. + accounts: &'static [(Address, &'static [u8], u64)], + to: Option
, + input: &'static [u8], + gas_limit: u64, + /// The access list of the request. + seed: &'static [(Address, &'static [B256])], + expected: &'static [(Address, &'static [B256])], +} + +fn access_list(items: &[(Address, &[B256])]) -> AccessList { + AccessList( + items + .iter() + .map(|&(address, storage_keys)| AccessListItem { + address, + storage_keys: storage_keys.to_vec(), + }) + .collect(), + ) +} + +/// Lists returned by `go-ethereum` a0cba163 (`geth --dev`, Osaka) for the same calls. +#[test] +fn access_list_matches_geth() { + let sender = a(0x1001); + for case in CASES { + let mut db = InMemoryDB::default(); + db.insert_account_info( + &sender, + AccountInfo { balance: U256::from(10u128.pow(18)), ..Default::default() }, + ); + for &(address, code, balance) in case.accounts { + db.insert_account_info( + &address, + AccountInfo { + balance: U256::from(balance), + code: Some(Bytecode::new_raw(code.to_vec().into())), + ..Default::default() + }, + ); + } + let context = Context::mainnet().with_db(db); + + // Like `go-ethereum`, run again with the list found so far until it stops growing. + let mut list = access_list(case.seed); + let actual = loop { + let mut inspector = AccessListInspector::new(list.clone()); + let mut evm = context.clone().build_mainnet().with_inspector(&mut inspector); + evm.inspect_tx(TxEnv { + caller: sender, + gas_limit: case.gas_limit, + kind: case.to.map_or(TransactTo::Create, TransactTo::Call), + data: case.input.to_vec().into(), + nonce: 0, + access_list: list.clone(), + ..Default::default() + }) + .unwrap(); + let next = inspector.access_list(); + if next == list { + break next; + } + list = next; + }; + + assert_eq!(actual, access_list(case.expected), "{}", case.name); + } +} + +const CASES: &[AccessListCase] = &[ + AccessListCase { + name: "call_underflow_2", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b0016000f1"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "call_underflow_4", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b001f1"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "call_underflow_6", + accounts: &[( + a(0xc1), + &hex!("600060006000600073000000000000000000000000000000000000b0016000f1"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "call_underflow_5", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b0016000f1"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "staticcall_underflow_4", + accounts: &[( + a(0xc1), + &hex!("6000600073000000000000000000000000000000000000b0016000fa"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "staticcall_underflow_5", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b0016000fa"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "delegatecall_underflow_5", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b0016000f4"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "callcode_underflow_4", + accounts: &[( + a(0xc1), + &hex!("6000600073000000000000000000000000000000000000b0016000f2"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "call_ok", + accounts: &[( + a(0xc1), + &hex!("6000600060006000600073000000000000000000000000000000000000b00161fffff150"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "balance_ok", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b0013100"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "balance_empty_stack", + accounts: &[(a(0xc1), &hex!("31"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "extcodecopy_underflow", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b0013c"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "sload_slot", + accounts: &[(a(0xc1), &hex!("60055400"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xc1), &[B256::with_last_byte(5)])], + }, + AccessListCase { + name: "sstore_underflow", + accounts: &[(a(0xc1), &hex!("600755"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xc1), &[B256::with_last_byte(7)])], + }, + AccessListCase { + name: "balance_oog", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b0013100"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 23453, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "sload_oog", + accounts: &[(a(0xc1), &hex!("60055400"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 25353, + seed: &[], + expected: &[(a(0xc1), &[B256::with_last_byte(5)])], + }, + AccessListCase { + name: "sstore_oog_sentry", + accounts: &[(a(0xc1), &hex!("600160095500"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 27300, + seed: &[], + expected: &[(a(0xc1), &[B256::with_last_byte(9)])], + }, + AccessListCase { + name: "call_oog", + accounts: &[( + a(0xc1), + &hex!("6000600060006000600073000000000000000000000000000000000000b00161fffff150"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 23481, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "sstore_static", + accounts: &[ + ( + a(0xc1), + &hex!("60006000600060007300000000000000000000000000000000000000c261fffffa50"), + 0, + ), + (a(0xc2), &hex!("6001600355"), 0), + ], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xc2), &[B256::with_last_byte(3)])], + }, + AccessListCase { + name: "delegatecall_sload", + accounts: &[ + ( + a(0xc1), + &hex!("60006000600060007300000000000000000000000000000000000000c261fffff450"), + 0, + ), + (a(0xc2), &hex!("60045400"), 0), + ], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xc1), &[B256::with_last_byte(4)]), (a(0xc2), &[])], + }, + AccessListCase { + name: "callcode_sload", + accounts: &[ + ( + a(0xc1), + &hex!("600060006000600060007300000000000000000000000000000000000000c261fffff250"), + 0, + ), + (a(0xc2), &hex!("60045400"), 0), + ], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xc1), &[B256::with_last_byte(4)]), (a(0xc2), &[])], + }, + AccessListCase { + name: "selfdestruct", + accounts: &[(a(0xc1), &hex!("73000000000000000000000000000000000000b001ff"), 1)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "selfdestruct_underflow", + accounts: &[(a(0xc1), &hex!("ff"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[], + }, + AccessListCase { + name: "excluded_sender_precompile", + accounts: &[( + a(0xc1), + &hex!( + "73000000000000000000000000000000000000100131507300000000000000000000000000000000000000c13150730000000000000000000000000000000000000002315073000000000000000000000000000000000000b0013100" + ), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "callcode_underflow_5", + accounts: &[( + a(0xc1), + &hex!("60006000600073000000000000000000000000000000000000b0016000f2"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xb001), &[])], + }, + AccessListCase { + name: "create_tx_sload", + accounts: &[], + to: None, + input: &hex!("6005545f5ff3"), + gas_limit: 5000000, + seed: &[], + expected: &[( + address!("0x3817e247023b4f489352758397040b1fd33b300a"), + &[B256::with_last_byte(5)], + )], + }, + AccessListCase { + name: "create_nested", + accounts: &[( + a(0xc1), + &hex!("606060005360056001536054600253605f600353605f60045360f3600553600660006000f050"), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[( + address!("0x724572a7f7b8eb3aa9763a66f2511269ec3764d5"), + &[B256::with_last_byte(5)], + )], + }, + AccessListCase { + name: "seeded", + accounts: &[(a(0xc1), &hex!("60055400"), 0)], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[ + (a(0xd1), &[B256::with_last_byte(9)]), + (a(0xc1), &[B256::with_last_byte(7)]), + (a(0x1001), &[B256::with_last_byte(1)]), + (a(0x2), &[]), + ], + expected: &[(a(0xc1), &[B256::with_last_byte(5)]), (a(0xd1), &[B256::with_last_byte(9)])], + }, + AccessListCase { + name: "reverted_frame_touch", + accounts: &[ + ( + a(0xc1), + &hex!("600060006000600060007300000000000000000000000000000000000000c261fffff150"), + 0, + ), + (a(0xc2), &hex!("73000000000000000000000000000000000000b001315060065460006000fd"), 0), + ], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xc2), &[B256::with_last_byte(6)]), (a(0xb001), &[])], + }, + AccessListCase { + name: "extcode_ops", + accounts: &[( + a(0xc1), + &hex!( + "7300000000000000000000000000000000000000e13b507300000000000000000000000000000000000000e23f506000600060007300000000000000000000000000000000000000e33c00" + ), + 0, + )], + to: Some(a(0xc1)), + input: &hex!(""), + gas_limit: 5000000, + seed: &[], + expected: &[(a(0xe1), &[]), (a(0xe2), &[]), (a(0xe3), &[])], + }, +]; diff --git a/crates/inspectors/tests/it/compat.rs b/crates/inspectors/tests/it/compat.rs index 8becc1584..d8b6d33b7 100644 --- a/crates/inspectors/tests/it/compat.rs +++ b/crates/inspectors/tests/it/compat.rs @@ -1,12 +1,14 @@ //! API shim so that the test files can stay as close to upstream as possible. -use alloy_consensus::{TxEip4844, TxLegacy, transaction::Recovered}; +use alloy_consensus::{TxEip2930, TxEip4844, TxEip7702, TxLegacy, transaction::Recovered}; +use alloy_eip2930::AccessList; +use alloy_eips::eip7702::SignedAuthorization; use alloy_primitives::{Address, B256, Bytes, TxKind, U256}; use evm2::{ - BaseEvmTypes, Evm, EvmTypesHost, Inspector, NoopInspector, Precompiles, TxResult, - TxResultWithState, env as evm_env, + BaseEvmTypes, Evm, EvmTypesHost, Inspector, InspectorConfig, NoopInspector, Precompiles, + TxResult, TxResultWithState, env as evm_env, ethereum::{RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, - evm::PendingState, + evm::{InMemoryDB, PendingState}, interpreter::{Interpreter, Message, MessageResult}, registry::HandlerError, }; @@ -36,6 +38,8 @@ pub struct TxEnv { pub chain_id: Option, pub blob_hashes: Vec, pub max_fee_per_blob_gas: u128, + pub authorization_list: Vec, + pub access_list: AccessList, } impl Default for TxEnv { @@ -52,6 +56,8 @@ impl Default for TxEnv { chain_id: None, blob_hashes: Vec::new(), max_fee_per_blob_gas: 0, + authorization_list: Vec::new(), + access_list: AccessList::default(), } } } @@ -121,7 +127,7 @@ impl TxEnvBuilder { #[derive(Clone, Debug)] pub struct Context { - pub db: CacheDB, + pub db: InMemoryDB, pub(crate) tx: TxEnv, block: evm_env::BlockEnv, pub(crate) spec: SpecId, @@ -136,19 +142,19 @@ impl Default for Context { impl Context { pub fn mainnet() -> Self { Self { - db: CacheDB::new(EmptyDB::default()), + db: InMemoryDB::default(), tx: TxEnv::default(), block: evm_env::BlockEnvExt::default(), spec: SpecId::OSAKA, } } - pub fn with_db(mut self, db: CacheDB) -> Self { + pub fn with_db(mut self, db: InMemoryDB) -> Self { self.db = db; self } - pub fn modify_db_chained(mut self, f: impl FnOnce(&mut CacheDB)) -> Self { + pub fn modify_db_chained(mut self, f: impl FnOnce(&mut InMemoryDB)) -> Self { f(&mut self.db); self } @@ -166,7 +172,7 @@ impl Context { } pub fn modify_tx_chained(mut self, f: impl FnOnce(&mut TxEnv)) -> Self { - f(&mut self.tx); + self.modify_tx(f); self } @@ -279,15 +285,15 @@ impl Context { &self.block } - pub const fn db(&self) -> &CacheDB { + pub const fn db(&self) -> &InMemoryDB { &self.db } - pub const fn db_ref(&self) -> &CacheDB { + pub const fn db_ref(&self) -> &InMemoryDB { &self.db } - pub const fn db_mut(&mut self) -> &mut CacheDB { + pub const fn db_mut(&mut self) -> &mut InMemoryDB { &mut self.db } @@ -338,6 +344,39 @@ impl TxEnv { self.caller, ); } + if !self.authorization_list.is_empty() { + let TxKind::Call(to) = self.kind else { panic!("EIP-7702 transactions must be calls") }; + return Recovered::new_unchecked( + TxEnvelope::from(TxEip7702 { + chain_id: self.chain_id.unwrap_or(1), + nonce: self.nonce, + gas_limit: self.gas_limit, + max_fee_per_gas: self.gas_price, + max_priority_fee_per_gas: self.gas_priority_fee.unwrap_or(self.gas_price), + to, + value: self.value, + access_list: Default::default(), + authorization_list: self.authorization_list.clone(), + input: self.data.clone(), + }), + self.caller, + ); + } + if !self.access_list.is_empty() { + return Recovered::new_unchecked( + TxEnvelope::Eip2930(TxEip2930 { + chain_id: self.chain_id.unwrap_or(1), + nonce: self.nonce, + gas_price: self.gas_price, + gas_limit: self.gas_limit, + to: self.kind, + value: self.value, + access_list: self.access_list.clone(), + input: self.data.clone(), + }), + self.caller, + ); + } Recovered::new_unchecked( TxEnvelope::Legacy(TxLegacy { chain_id: self.chain_id, @@ -381,14 +420,14 @@ pub enum ExecutionResult { impl ExecutionResult { fn from_tx_result(result: TxResult) -> Self { let gas_used = result.tx_gas_used(); - if result.status { + if result.is_success() { let output = if result.created_address.is_some() { Output::Create(result.output, result.created_address) } else { Output::Call(result.output) }; Self::Success { output, gas_used } - } else if result.stop.is_revert() { + } else if result.stop.is_frame_revert() { Self::Revert { output: result.output, gas_used } } else { Self::Halt { reason: result.stop, gas_used } @@ -456,7 +495,7 @@ pub trait DatabaseCommit { fn commit(&mut self, changes: PendingState); } -impl DatabaseCommit for CacheDB { +impl DatabaseCommit for InMemoryDB { fn commit(&mut self, changes: PendingState) { self.commit_source(&changes); } @@ -471,7 +510,7 @@ pub trait TestDbExt { fn load_account(&mut self, address: Address) -> LoadedAccountMut<'_>; } -impl TestDbExt for CacheDB { +impl TestDbExt for InMemoryDB { fn load_account(&mut self, address: Address) -> LoadedAccountMut<'_> { let info = self.cache.accounts.entry(address).or_default().get_or_insert_default(); LoadedAccountMut { info } @@ -534,6 +573,11 @@ impl> Inspector for RawInspector { self.inner().initialize_interp(interp); } + fn inspector_config(&self) -> InspectorConfig { + // SAFETY: As in `inner`. + unsafe { &*self.inspector }.inspector_config() + } + fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { self.inner().step(interp); } diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index c0147e4e6..4c19d59b9 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -4,23 +4,39 @@ mod consistency; mod runtime_oog; use crate::utils::{ - AccountInfo, Bytecode, CacheDB, Context, ETH_TRANSFER_LOG_ADDRESS, EmptyDB, SpecId, TransactTo, - TxEnv, deploy_contract, op, + AccountInfo, Bytecode, Context, ETH_TRANSFER_LOG_ADDRESS, SpecId, TransactTo, TxEnv, + deploy_contract, op, +}; +use alloy_consensus::crypto::secp256k1::sign_message; +use alloy_eips::eip7702::Authorization; +use alloy_primitives::{ + Address, B256, Bytes, TxKind, U256, address, bytes, hex, keccak256, map::HashMap, }; -use alloy_primitives::{Address, B256, Bytes, TxKind, U256, address, hex, map::HashMap}; use alloy_rpc_types_eth::TransactionInfo; -use alloy_rpc_types_trace::geth::{ - CallConfig, FlatCallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig, - GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, - PreStateConfig, PreStateFrame, erc7562::Erc7562Config, mux::MuxConfig, +use alloy_rpc_types_trace::{ + geth::{ + CallConfig, FlatCallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig, + GethDebugTracerType, GethDebugTracingOptions, GethDefaultTracingOptions, GethTrace, + PreStateConfig, PreStateFrame, StateGasTrace, + erc7562::{AccessedSlots, Erc7562Config}, + mux::MuxConfig, + }, + parity::{Action, CallType}, }; use evm2::{ - BaseEvmTypes, Evm, EvmFeatures, ExecutionConfig, Precompiles, Version, + AnyError, BaseEvmTypes, DatabaseError, Evm, EvmFeatures, ExecutionConfig, Inspector, + InspectorConfig, OpcodeSet, Precompiles, Version, ethereum::ethereum_tx_registry, + evm::{DbResult, DynDatabase, InMemoryDB}, + interpreter::{Interpreter, Word}, }; use evm2_inspectors::tracing::{ - DebugInspector, MuxInspector, TracingInspector, TracingInspectorConfig, + DebugInspector, Erc7562Error, Erc7562Inspector, KECCAK256_PREIMAGE_TRACER, + Keccak256PreimageInspector, MEMORY_PAD_LIMIT, MuxInspector, TraceLimitBehavior, TraceLimits, + TracingInspector, TracingInspectorConfig, }; +use serde_json::{Value, json}; +use std::collections::BTreeMap; #[test] fn test_calltracer_invalid_opcode_errors() { @@ -35,7 +51,7 @@ fn test_calltracer_invalid_opcode_errors() { let options = serde_json::from_value(serde_json::json!({"tracer": "callTracer"})).unwrap(); let mut inspector = DebugInspector::new(options).unwrap(); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &account, AccountInfo { @@ -43,10 +59,13 @@ fn test_calltracer_invalid_opcode_errors() { ..Default::default() }, ); - let mut parent_code = hex!("60006000600060006000").to_vec(); - parent_code.push(op::PUSH20); - parent_code.extend_from_slice(account.as_slice()); - parent_code.extend_from_slice(&hex!("61fffff15000")); + let parent_code = [ + &hex!("60006000600060006000")[..], + &[op::PUSH20][..], + account.as_slice(), + &hex!("61fffff15000")[..], + ] + .concat(); db.insert_account_info( &parent, AccountInfo { @@ -85,7 +104,7 @@ fn test_calltracer_eip7702_call_targets() { let implementation = address!("3000000000000000000000000000000000000003"); for opcode in [op::CALL, op::CALLCODE, op::DELEGATECALL, op::STATICCALL] { let trace = |tracer: &str| { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &target, AccountInfo { @@ -96,7 +115,7 @@ fn test_calltracer_eip7702_call_targets() { db.insert_account_info( &implementation, AccountInfo { - code: Some(Bytecode::new_raw(hex!("602a60005260206000f3").into())), + code: Some(Bytecode::new_raw(bytes!("602a60005260206000f3"))), ..Default::default() }, ); @@ -173,16 +192,12 @@ fn test_debug_empty_tracer() { // Write memory and storage, and call the identity precompile to populate return data. let code = hex!("602a6000526020600060206000600461fffffa50602a60005560206000f3"); let account = address!("1000000000000000000000000000000000000001"); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &account, - AccountInfo { - code: Some(Bytecode::new_raw(code.into())), - ..Default::default() - }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &account, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + }); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let res = evm .inspect_tx(TxEnv { @@ -196,7 +211,7 @@ fn test_debug_empty_tracer() { let tx = ctx.tx().envelope(); let block = *ctx.block(); let result = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); - assert!(matches!(result, GethTrace::Default(_))); + assert!(result.is_default()); let result = serde_json::to_value(result).unwrap(); let logs = result["structLogs"].as_array().unwrap(); assert!(!logs.is_empty()); @@ -262,6 +277,20 @@ fn test_debug_nonempty_tracer() { } } +#[test] +fn test_debug_invalid_tracer_config() { + let opts = serde_json::from_value( + serde_json::json!({"tracer": "callTracer", "tracerConfig": {"onlyTopCall": "yes"}}), + ) + .unwrap(); + let err = DebugInspector::new(opts).unwrap_err(); + assert!(matches!(err, evm2_inspectors::tracing::DebugInspectorError::InvalidTracerConfig(_))); + assert_eq!( + err.to_string(), + r#"invalid tracer config: invalid type: string "yes", expected a boolean"# + ); +} + #[test] fn test_geth_calltracer_logs() { /* @@ -296,7 +325,7 @@ fn test_geth_calltracer_logs() { } } */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let code = hex!( "608060405234801561001057600080fd5b506103ac806100206000396000f3fe60806040526004361061003f5760003560e01c80630332ed131461014d5780636ae1ad40146101625780638384a00214610177578063de7eb4f31461018c575b60405134815233906000805160206103578339815191529060200160405180910390a2306001600160a01b0316636ae1ad406040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561009d57600080fd5b505af19250505080156100ae575060015b50306001600160a01b0316630332ed136040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156100ea57600080fd5b505af19250505080156100fb575060015b50306001600160a01b0316638384a0026040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561013757600080fd5b505af115801561014b573d6000803e3d6000fd5b005b34801561015957600080fd5b5061014b6101a1565b34801561016e57600080fd5b5061014b610253565b34801561018357600080fd5b5061014b6102b7565b34801561019857600080fd5b5061014b6102dd565b306001600160a01b031663de7eb4f36040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156101dc57600080fd5b505af11580156101f0573d6000803e3d6000fd5b505060405162461bcd60e51b8152602060048201526024808201527f6e6573746564456d6974576974684661696c75726541667465724e6573746564604482015263115b5a5d60e21b6064820152608401915061024a9050565b60405180910390fd5b6040516000815233906000805160206103578339815191529060200160405180910390a260405162461bcd60e51b81526020600482015260156024820152746e6573746564456d6974576974684661696c75726560581b604482015260640161024a565b6040516000815233906000805160206103578339815191529060200160405180910390a2565b6040516000815233906000805160206103578339815191529060200160405180910390a2306001600160a01b0316638384a0026040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561033c57600080fd5b505af1158015610350573d6000803e3d6000fd5b5050505056fef950957d2407bed19dc99b718b46b4ce6090c05589006dfb86fd22c34865b23ea2646970667358221220090a696b9fbd22c7d1cc2a0b6d4a48c32d3ba892480713689a3145b73cfeb02164736f6c63430008130033" ); @@ -304,8 +333,7 @@ fn test_geth_calltracer_logs() { let addr = deploy_contract(&mut evm, code.into(), deployer, SpecId::LONDON).created_address().unwrap(); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); // Three logs emitted by preceding transactions in the block. insp.set_next_log_index(3); @@ -361,13 +389,12 @@ fn test_geth_erc7562_tracer() { let account = address!("1000000000000000000000000000000000000001"); let caller = address!("1000000000000000000000000000000000000002"); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &account, - AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &account, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + }); let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); let mut inspector = DebugInspector::new(opts).unwrap(); @@ -401,6 +428,442 @@ fn test_geth_erc7562_tracer() { } } +#[test] +fn test_geth_erc7562_counts_like_geth() { + let (root, target, failing, empty) = ( + address!("1000000000000000000000000000000000000001"), + address!("1000000000000000000000000000000000000002"), + address!("1000000000000000000000000000000000000003"), + address!("1000000000000000000000000000000000000004"), + ); + let push_address = |code: &mut Vec, address: Address| { + code.push(op::PUSH20); + code.extend_from_slice(address.as_slice()); + }; + // A call with no value, no data and no return data. + let call = |code: &mut Vec, address: Address, gas: &[u8]| { + code.extend_from_slice(&hex!("60006000600060006000")); + push_address(code, address); + code.extend_from_slice(gas); + code.extend_from_slice(&[op::CALL, op::POP]); + }; + let mut code = vec![op::GAS, op::POP]; + // `ISZERO` after `EXTCODESIZE` is a check, not an access. + push_address(&mut code, target); + code.extend_from_slice(&[op::EXTCODESIZE, op::ISZERO, op::POP]); + push_address(&mut code, target); + code.extend_from_slice(&[op::EXTCODESIZE, op::POP]); + // The second call runs out of gas at its `KECCAK256`, and `GAS` before a call does not count. + call(&mut code, empty, &[op::GAS]); + call(&mut code, failing, &hex!("6028")); + // The same preimage twice, from a range that memory does not reach yet. + for _ in 0..2 { + code.extend_from_slice(&hex!("60206000")); + code.extend_from_slice(&[op::KECCAK256, op::POP]); + } + code.push(op::STOP); + let target_code = Bytes::from_static(&[op::STOP]); + let failing_code = bytes!("6001600020"); + + let mut db = InMemoryDB::default(); + for (address, code) in [ + (root, Bytes::from(code)), + (target, target_code), + (failing, failing_code), + (empty, Bytes::from_static(&[op::STOP, op::STOP])), + ] { + db.insert_account_info( + &address, + AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, + ); + } + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = Context::mainnet().with_db(db).build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(root), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + + // `GAS` counts unless a call follows, and the ignored opcodes do not count. + assert_eq!( + frame.used_opcodes, + HashMap::from_iter([ + (op::GAS, 1), + (op::EXTCODESIZE, 2), + (op::CALL, 2), + (op::KECCAK256, 2), + (op::STOP, 1), + ]) + ); + assert_eq!(frame.ext_code_access_info, [format!("{target:?}")]); + let size = |address: Address| frame.contract_size[&address].clone(); + assert_eq!( + [size(target), size(empty), size(failing)].map(|size| (size.contract_size, size.opcode)), + [(1, op::EXTCODESIZE), (2, op::CALL), (5, op::CALL)] + ); + assert!(!frame.out_of_gas); + assert_eq!(frame.calls.len(), 2); + assert_eq!(frame.calls[0].used_opcodes, HashMap::from_iter([(op::STOP, 1)])); + // A frame that fails at its `KECCAK256` still counts it and leaves its preimage. + assert!(frame.calls[1].out_of_gas); + assert_eq!(frame.calls[1].used_opcodes, HashMap::from_iter([(op::KECCAK256, 1)])); + // The preimages of all frames are on the root, sorted and without repeats. + assert_eq!(frame.keccak, [Bytes::from_static(&[0]), Bytes::from(vec![0; 32])]); + assert!(frame.calls.iter().all(|call| call.keccak.is_empty())); +} + +#[test] +fn test_geth_erc7562_accessed_slots_and_ignored_opcodes() { + let contract = address!("1000000000000000000000000000000000000001"); + let code = Bytes::from(vec![ + // SLOAD of slot 1 reads 0x2a, and the SSTORE after it is counted. + op::PUSH1, + 1, + op::SLOAD, + op::POP, + op::PUSH1, + 7, + op::PUSH1, + 1, + op::SSTORE, + // The slot was read and written, so a second SLOAD adds no read. Slot 2 is read once. + op::PUSH1, + 1, + op::SLOAD, + op::POP, + op::PUSH1, + 2, + op::SLOAD, + op::POP, + // Slot 3 is written before it is read, so it has no read at all. + op::PUSH1, + 7, + op::PUSH1, + 3, + op::SSTORE, + op::PUSH1, + 3, + op::SLOAD, + op::POP, + op::PUSH1, + 4, + op::TLOAD, + op::POP, + op::PUSH1, + 5, + op::PUSH1, + 4, + op::TSTORE, + op::STOP, + ]); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &contract, + AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, + ); + db.insert_account_storage(&contract, &U256::ONE, &U256::from(0x2a)); + db.insert_account_storage(&contract, &U256::from(2), &U256::from(0x2b)); + + // Only `SLOAD` and `STOP` are ignored, which is not the default list. + let ignored_opcodes = vec![op::SLOAD, op::STOP]; + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config { + ignored_opcodes, + ..Default::default() + }); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = Context::mainnet().with_db(db).build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + + let slot = |slot: u8| B256::with_last_byte(slot); + let slots = &frame.accessed_slots; + assert_eq!( + *slots, + AccessedSlots { + reads: HashMap::from_iter([ + (slot(1), vec![B256::with_last_byte(0x2a)]), + (slot(2), vec![B256::with_last_byte(0x2b)]), + ]), + writes: HashMap::from_iter([(slot(1), 1), (slot(3), 1)]), + transient_reads: HashMap::from_iter([(slot(4), 1)]), + transient_writes: HashMap::from_iter([(slot(4), 1)]), + } + ); + // The opcodes of the config are not counted, and every other opcode is, including `PUSH1`. + assert_eq!( + frame.used_opcodes, + HashMap::from_iter([ + (op::PUSH1, 11), + (op::POP, 5), + (op::SSTORE, 2), + (op::TLOAD, 1), + (op::TSTORE, 1), + ]) + ); +} + +#[test] +fn test_geth_erc7562_bounds_retained_preimages() { + let contract = address!("1000000000000000000000000000000000000001"); + let spec = SpecId::CANCUN; + // KECCAK256 of 60 and of 61 bytes of zero padding. + let code = Bytes::from(vec![ + op::PUSH1, + 60, + op::PUSH0, + op::KECCAK256, + op::POP, + op::PUSH1, + 61, + op::PUSH0, + op::KECCAK256, + op::STOP, + ]); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &contract, + AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, + ); + let block = *Context::mainnet().block(); + for (limit, preimages) in [(121, Some(2)), (120, None)] { + let mut inspector = Erc7562Inspector::new(Erc7562Config::default()) + .with_limits(TraceLimits::default().set_max_recorded_bytes(Some(limit))); + let mut evm = Evm::::new( + spec, + block, + ethereum_tx_registry(spec), + db.clone(), + Precompiles::base(spec), + ); + evm.set_inspector(&mut inspector); + let tx = TxEnv { + gas_limit: 1_000_000, + gas_price: 0, + kind: TransactTo::Call(contract), + ..Default::default() + }; + let envelope = tx.envelope(); + let res = evm.transact(&envelope).unwrap().detach(); + drop(evm); + + assert_eq!(inspector.limit_exceeded(), preimages.is_none()); + assert_eq!( + inspector.frame(&envelope, &res.result).ok().map(|frame| frame.keccak.len()), + preimages + ); + } +} + +/// A database whose storage reads fail. +struct NoStorageDb(InMemoryDB); + +impl DynDatabase for NoStorageDb { + fn get_account(&mut self, address: &Address) -> DbResult> { + DynDatabase::get_account(&mut self.0, address) + } + + fn get_code_by_hash(&mut self, code_hash: &B256) -> DbResult { + DynDatabase::get_code_by_hash(&mut self.0, code_hash) + } + + fn get_storage(&mut self, _address: &Address, _key: &Word) -> DbResult { + Err(DatabaseError::new(AnyError::from("no storage"), true)) + } + + fn get_block_hash(&mut self, number: &Word) -> DbResult { + DynDatabase::get_block_hash(&mut self.0, number) + } +} + +#[test] +fn test_geth_erc7562_reports_a_failed_state_read() { + let contract = address!("1000000000000000000000000000000000000001"); + let spec = SpecId::CANCUN; + let mut db = InMemoryDB::default(); + db.insert_account_info( + &contract, + AccountInfo { + code: Some(Bytecode::new_raw(Bytes::from_static(&[op::PUSH1, 1, op::SLOAD, op::STOP]))), + ..Default::default() + }, + ); + let block = *Context::mainnet().block(); + // The transaction runs out of gas at the `SLOAD`, before it reads the slot, and does not fail. + let tx = TxEnv { + gas_limit: 21_000 + 3 + 2_099, + gas_price: 0, + kind: TransactTo::Call(contract), + ..Default::default() + }; + let envelope = tx.envelope(); + for fails in [false, true] { + let mut inspector = Erc7562Inspector::new(Erc7562Config::default()); + let mut evm = if fails { + Evm::::new( + spec, + block, + ethereum_tx_registry(spec), + NoStorageDb(db.clone()), + Precompiles::base(spec), + ) + } else { + Evm::::new( + spec, + block, + ethereum_tx_registry(spec), + db.clone(), + Precompiles::base(spec), + ) + }; + evm.set_inspector(&mut inspector); + let res = evm.transact(&envelope).unwrap().detach(); + drop(evm); + + assert!(!res.result.is_success()); + let frame = inspector.frame(&envelope, &res.result); + assert_eq!(matches!(frame, Err(Erc7562Error::Database(_))), fails, "{frame:?}"); + } +} + +#[test] +fn test_geth_erc7562_contract_size_reads_accepted_state() { + let (root, target) = ( + address!("1000000000000000000000000000000000000001"), + address!("1000000000000000000000000000000000000002"), + ); + let spec = SpecId::CANCUN; + let mut code = vec![op::PUSH20]; + code.extend_from_slice(target.as_slice()); + code.extend_from_slice(&[op::EXTCODESIZE, op::STOP]); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &root, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + let block = *Context::mainnet().block(); + let mut inspector = + DebugInspector::new(GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default())) + .unwrap(); + let mut evm = Evm::::new( + spec, + block, + ethereum_tx_registry(spec), + db.clone(), + Precompiles::base(spec), + ); + // The code is in the accepted state only, like that of an earlier transaction of the block. + evm.overlay_db_mut().insert_account_info( + &target, + AccountInfo { + code: Some(Bytecode::new_raw(Bytes::from_static(&[op::STOP; 3]))), + ..Default::default() + }, + ); + evm.set_inspector(&mut inspector); + let tx = TxEnv { + gas_limit: 1_000_000, + gas_price: 0, + kind: TransactTo::Call(root), + ..Default::default() + }; + let envelope = tx.envelope(); + let res = evm.transact(&envelope).unwrap().detach(); + drop(evm); + + let trace = inspector.get_result(None, &envelope, &block, &res, &mut db).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + let size = &frame.contract_size[&target]; + assert_eq!((size.contract_size, size.opcode), (3, op::EXTCODESIZE)); +} + +/// geth reads each slot in `OnOpcode`, so a frame records its first read of a slot that an earlier +/// frame already warmed. +#[test] +fn test_geth_erc7562_records_warm_first_reads() { + let outer = address!("1000000000000000000000000000000000000001"); + let inner = address!("1000000000000000000000000000000000000003"); + let caller = address!("1000000000000000000000000000000000000002"); + + // SLOAD 1, then DELEGATECALL the inner code, which SLOADs the same, now warm, slot. + let outer_code = + hex!("6001545060006000600060007310000000000000000000000000000000000000035af45000").to_vec(); + let inner_code = hex!("6001545000").to_vec(); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + for (address, code) in [(outer, outer_code), (inner, inner_code)] { + db.insert_account_info( + &address, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + } + db.insert_account_storage(&outer, &U256::ONE, &U256::from(0x2a)); + }); + + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + caller, + gas_limit: 1_000_000, + kind: TransactTo::Call(outer), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + + let slot = B256::with_last_byte(1); + let value = B256::with_last_byte(0x2a); + assert_eq!(frame.accessed_slots.reads[&slot], vec![value]); + assert_eq!(frame.calls.len(), 1); + assert_eq!(frame.calls[0].accessed_slots.reads[&slot], vec![value]); +} + +/// geth reads the code size when the opcode runs, so an account created earlier in the same +/// transaction has a size. +#[test] +fn test_geth_erc7562_contract_size_of_account_created_in_same_transaction() { + // CREATE a contract whose runtime code is 5 bytes, then EXTCODESIZE the created address. + let code = hex!("6460056000f36000526005601b6000f03b00"); + let (trace, _) = trace_contract( + SpecId::CANCUN, + GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()), + code.into(), + ); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + + let created = frame.calls[0].to.unwrap(); + assert_eq!(frame.contract_size.len(), 1); + assert_eq!(frame.contract_size[&created].contract_size, 5); + assert_eq!(frame.contract_size[&created].opcode, op::EXTCODESIZE); +} + fn trace_contract( spec: SpecId, opts: GethDebugTracingOptions, @@ -409,7 +872,7 @@ fn trace_contract( let contract = address!("1000000000000000000000000000000000000001"); let caller = address!("1000000000000000000000000000000000000002"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = spec) .modify_db_chained(|db| { db.insert_account_info( @@ -448,13 +911,12 @@ fn trace_contract_with_version( ) -> (GethTrace, evm2::TxResult) { let contract = address!("1000000000000000000000000000000000000001"); let caller = address!("1000000000000000000000000000000000000002"); - let mut context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &contract, - AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, - ); - }); + let mut context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &contract, + AccountInfo { code: Some(Bytecode::new_raw(code)), ..Default::default() }, + ); + }); let block = *context.block(); let mut inspector = DebugInspector::new(opts).unwrap(); let mut evm = Evm::::new_with_execution_config( @@ -490,16 +952,21 @@ fn test_geth_state_gas_tracer() { let (trace, gas) = trace_contract( SpecId::AMSTERDAM, GethDebugTracingOptions::state_gas_tracer(), - hex!("600160005500").into(), + bytes!("600160005500"), ); assert!(gas.state_gas_spent() > 0); match trace { GethTrace::StateGasTracer(frame) => { - assert_eq!(frame.gas_used, gas.tx_gas_used()); - assert_eq!(frame.execution_gas_used, gas.execution_gas_spent()); - assert_eq!(frame.state_gas_used, gas.state_gas_spent()); - assert_eq!(frame.gas_refund, gas.refunded); + assert_eq!( + frame, + StateGasTrace { + gas_used: gas.tx_gas_used(), + execution_gas_used: gas.execution_gas_spent(), + state_gas_used: gas.state_gas_spent(), + gas_refund: gas.refunded, + } + ); } _ => panic!("Expected StateGasTracer"), } @@ -644,7 +1111,7 @@ fn test_geth_mux_tracer() { } */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let code = hex!( "608060405234801561001057600080fd5b506103ac806100206000396000f3fe60806040526004361061003f5760003560e01c80630332ed131461014d5780636ae1ad40146101625780638384a00214610177578063de7eb4f31461018c575b60405134815233906000805160206103578339815191529060200160405180910390a2306001600160a01b0316636ae1ad406040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561009d57600080fd5b505af19250505080156100ae575060015b50306001600160a01b0316630332ed136040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156100ea57600080fd5b505af19250505080156100fb575060015b50306001600160a01b0316638384a0026040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561013757600080fd5b505af115801561014b573d6000803e3d6000fd5b005b34801561015957600080fd5b5061014b6101a1565b34801561016e57600080fd5b5061014b610253565b34801561018357600080fd5b5061014b6102b7565b34801561019857600080fd5b5061014b6102dd565b306001600160a01b031663de7eb4f36040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156101dc57600080fd5b505af11580156101f0573d6000803e3d6000fd5b505060405162461bcd60e51b8152602060048201526024808201527f6e6573746564456d6974576974684661696c75726541667465724e6573746564604482015263115b5a5d60e21b6064820152608401915061024a9050565b60405180910390fd5b6040516000815233906000805160206103578339815191529060200160405180910390a260405162461bcd60e51b81526020600482015260156024820152746e6573746564456d6974576974684661696c75726560581b604482015260640161024a565b6040516000815233906000805160206103578339815191529060200160405180910390a2565b6040516000815233906000805160206103578339815191529060200160405180910390a2306001600160a01b0316638384a0026040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561033c57600080fd5b505af1158015610350573d6000803e3d6000fd5b5050505056fef950957d2407bed19dc99b718b46b4ce6090c05589006dfb86fd22c34865b23ea2646970667358221220090a696b9fbd22c7d1cc2a0b6d4a48c32d3ba892480713689a3145b73cfeb02164736f6c63430008130033" @@ -692,9 +1159,9 @@ fn test_geth_mux_tracer() { assert!(res.result.is_success()); let (ctx, inspector) = evm.ctx_inspector(); - let frame = inspector - .try_into_mux_frame(&res.tx_result, ctx.db_mut(), TransactionInfo::default()) - .unwrap(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); assert_eq!(frame.0.len(), 5); assert!(frame.0.contains_key(&GethDebugTracerType::BuiltInTracer( @@ -775,10 +1242,15 @@ fn test_geth_mux_tracer() { .clone(); match state_gas_frame { GethTrace::StateGasTracer(state_gas) => { - assert_eq!(state_gas.gas_used, res.tx_result.result.tx_gas_used()); - assert_eq!(state_gas.execution_gas_used, res.tx_result.result.execution_gas_spent()); - assert_eq!(state_gas.state_gas_used, res.tx_result.result.state_gas_spent()); - assert_eq!(state_gas.gas_refund, res.tx_result.result.refunded); + assert_eq!( + state_gas, + StateGasTrace { + gas_used: res.tx_result.result.tx_gas_used(), + execution_gas_used: res.tx_result.result.execution_gas_spent(), + state_gas_used: res.tx_result.result.state_gas_spent(), + gas_refund: res.tx_result.result.refunded, + } + ); } _ => panic!("Expected StateGasTracer"), } @@ -789,7 +1261,7 @@ fn test_geth_inspector_reset() { let insp = TracingInspector::new(TracingInspectorConfig::default_geth()); let context = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON); assert_eq!(insp.traces().nodes().first().unwrap().trace.gas_limit, 0); @@ -844,7 +1316,7 @@ fn test_geth_calltracer_top_call_reverting() { Test that verifies the behavior of only_top_call with a reverting transaction. Uses the LogTracing contract which has functions that make nested calls and revert. */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Use the LogTracing contract from test_geth_calltracer_logs let code = hex!( @@ -864,7 +1336,7 @@ fn test_geth_calltracer_top_call_reverting() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("0332ed13").into(), // nestedEmitWithFailureAfterNestedEmit() selector + data: bytes!("0332ed13"), // nestedEmitWithFailureAfterNestedEmit() selector nonce: 1, ..Default::default() }) @@ -885,7 +1357,7 @@ fn test_geth_calltracer_top_call_reverting() { // Now test with only_top_call = false to verify we see the nested structure let mut insp2 = TracingInspector::new(TracingInspectorConfig::default_geth()); - let mut evm2 = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm2 = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let addr2 = deploy_contract(&mut evm2, code.into(), deployer, SpecId::LONDON) .created_address() @@ -897,7 +1369,7 @@ fn test_geth_calltracer_top_call_reverting() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr2), - data: hex!("0332ed13").into(), // nestedEmitWithFailureAfterNestedEmit() selector + data: bytes!("0332ed13"), // nestedEmitWithFailureAfterNestedEmit() selector nonce: 1, ..Default::default() }) @@ -934,7 +1406,7 @@ fn test_geth_calltracer_nested_revert() { Test that verifies the behavior of only_top_call with the nestedRevert function. This function calls nestedEmitWithFailure which emits a log and then reverts. */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Use the LogTracing contract with nestedRevert function let code = hex!( @@ -954,7 +1426,7 @@ fn test_geth_calltracer_nested_revert() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("c8cc8494").into(), // nestedRevert() selector + data: bytes!("c8cc8494"), // nestedRevert() selector nonce: 1, ..Default::default() }) @@ -975,7 +1447,7 @@ fn test_geth_calltracer_nested_revert() { // Now test with only_top_call = false let mut insp2 = TracingInspector::new(TracingInspectorConfig::default_geth()); - let mut evm2 = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm2 = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let addr2 = deploy_contract(&mut evm2, code.into(), deployer, SpecId::LONDON) .created_address() @@ -987,7 +1459,7 @@ fn test_geth_calltracer_nested_revert() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr2), - data: hex!("c8cc8494").into(), // nestedRevert() selector + data: bytes!("c8cc8494"), // nestedRevert() selector nonce: 1, ..Default::default() }) @@ -1011,9 +1483,8 @@ fn test_geth_calltracer_nested_revert() { ); // Test revert with topcall - let mut insp3 = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); - let mut evm3 = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut insp3 = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); + let mut evm3 = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let addr3 = deploy_contract(&mut evm3, code.into(), deployer, SpecId::LONDON) .created_address() @@ -1025,7 +1496,7 @@ fn test_geth_calltracer_nested_revert() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr3), - data: hex!("c8cc8494").into(), // nestedRevert() selector + data: bytes!("c8cc8494"), // nestedRevert() selector nonce: 1, ..Default::default() }) @@ -1054,7 +1525,7 @@ fn test_geth_prestate_disable_code_in_diff_mode() { Test that verifies the disableCode flag works correctly in diff mode for PreStateTracer. When disableCode is true, code should be excluded from both pre and post states. */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Deploy a simple contract let code = hex!( @@ -1083,7 +1554,7 @@ fn test_geth_prestate_disable_code_in_diff_mode() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("8384a002").into(), // nestedEmitWithSuccess() selector + data: bytes!("8384a002"), // nestedEmitWithSuccess() selector nonce: 1, ..Default::default() }) @@ -1130,7 +1601,7 @@ fn test_geth_prestate_disable_code_in_diff_mode() { let mut insp2 = TracingInspector::new(TracingInspectorConfig::from_geth_prestate_config( &prestate_config_with_code, )); - let evm2 = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let evm2 = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); let mut db2 = evm2.ctx.db.clone(); let res2 = { @@ -1198,7 +1669,7 @@ fn test_geth_calltracer_null_bytes_revert_reason_omitted() { This simulates scenarios where contract reverts with empty or null-byte-only data, ensuring compatibility with Geth behavior of omitting such meaningless revert reasons. */ - let mut evm = Context::mainnet().with_db(CacheDB::new(EmptyDB::default())).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Deploy a simple contract that reverts with empty or null bytes // This bytecode reverts with 32 null bytes: revert(0, 32) @@ -1219,7 +1690,7 @@ fn test_geth_calltracer_null_bytes_revert_reason_omitted() { .inspect_tx(TxEnv { caller: deployer, kind: TxKind::Call(addr), - data: hex!("3bc5de30").into(), // call function selector + data: bytes!("3bc5de30"), // call function selector gas_limit: 1_000_000, nonce: 1, // Set correct nonce after contract deployment ..Default::default() @@ -1258,13 +1729,13 @@ fn test_geth_prestate_diff_selfdestruct_cancun() { } #[test] -fn test_geth_default_tracer_empty_return_data_is_serialized_when_enabled() { +fn test_geth_default_tracer_omits_empty_return_data() { let contract = address!("0xc000000000000000000000000000000000000003"); let caller = address!("0xa000000000000000000000000000000000000003"); let code = hex!("00"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( @@ -1273,10 +1744,7 @@ fn test_geth_default_tracer_empty_return_data_is_serialized_when_enabled() { ); db.insert_account_info( &caller, - AccountInfo { - balance: alloy_primitives::U256::from(1_000_000_000), - ..Default::default() - }, + AccountInfo { balance: U256::from(1_000_000_000), ..Default::default() }, ); }); @@ -1304,7 +1772,7 @@ fn test_geth_default_tracer_empty_return_data_is_serialized_when_enabled() { ); assert!(!frame.struct_logs.is_empty(), "Expected struct logs for STOP execution"); - assert!(frame.struct_logs.iter().all(|log| log.return_data == Some(Bytes::default()))); + assert!(frame.struct_logs.iter().all(|log| log.return_data.is_none())); let struct_logs = serde_json::to_value(&frame) .unwrap() @@ -1313,9 +1781,7 @@ fn test_geth_default_tracer_empty_return_data_is_serialized_when_enabled() { .cloned() .unwrap(); - assert!(struct_logs.iter().all(|log| { - log.get("returnData") == Some(&serde_json::Value::String("0x".to_string())) - })); + assert!(struct_logs.iter().all(|log| log.get("returnData").is_none())); } fn test_geth_prestate_diff_selfdestruct(spec_id: SpecId) { @@ -1334,11 +1800,11 @@ fn test_geth_prestate_diff_selfdestruct(spec_id: SpecId) { ); let deployer = alloy_primitives::address!("341348115259a8bf69f1f50101c227fced83bac6"); - let value = alloy_primitives::U256::from(69); + let value = U256::from(69); // Deploy the contract in a separate transaction first let mut context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_db_chained(|db| { db.insert_account_info(&deployer, AccountInfo { balance: value, ..Default::default() }); }) @@ -1365,7 +1831,7 @@ fn test_geth_prestate_diff_selfdestruct(spec_id: SpecId) { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("43d726d6").into(), + data: bytes!("43d726d6"), nonce: 1, ..Default::default() }) @@ -1427,14 +1893,14 @@ fn test_geth_calltracer_logs_eip7708() { let caller = address!("0xa000000000000000000000000000000000000001"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) .modify_db_chained(|db| { // Fund the contract so it can send 1 wei db.insert_account_info( &contract, AccountInfo { - balance: alloy_primitives::U256::from(1_000_000), + balance: U256::from(1_000_000), code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, @@ -1442,15 +1908,11 @@ fn test_geth_calltracer_logs_eip7708() { // Fund the caller db.insert_account_info( &caller, - AccountInfo { - balance: alloy_primitives::U256::from(1_000_000_000), - ..Default::default() - }, + AccountInfo { balance: U256::from(1_000_000_000), ..Default::default() }, ); }); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); let mut evm = context.build_mainnet().with_inspector(&mut insp); @@ -1519,7 +1981,7 @@ fn test_geth_calltracer_logs_address_regular() { let caller = address!("0xa000000000000000000000000000000000000002"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( @@ -1528,15 +1990,11 @@ fn test_geth_calltracer_logs_address_regular() { ); db.insert_account_info( &caller, - AccountInfo { - balance: alloy_primitives::U256::from(1_000_000_000), - ..Default::default() - }, + AccountInfo { balance: U256::from(1_000_000_000), ..Default::default() }, ); }); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); let mut evm = context.build_mainnet().with_inspector(&mut insp); @@ -1575,8 +2033,8 @@ fn test_geth_calltracer_logs_address_regular() { /// rather than reconstructing it from `execution_address()`. #[test] fn test_geth_calltracer_logs_delegatecall() { - let proxy = address!("0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); - let implementation = address!("0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); + let proxy = Address::repeat_byte(0xaa); + let implementation = Address::repeat_byte(0xbb); let caller = address!("0xa000000000000000000000000000000000000099"); // Implementation bytecode: LOG0(offset=0, size=0); STOP @@ -1584,18 +2042,20 @@ fn test_geth_calltracer_logs_delegatecall() { // Proxy bytecode: DELEGATECALL(gas=0xFFFF, addr=implementation, 0, 0, 0, 0); STOP let proxy_code = { - let mut code = Vec::new(); - code.extend_from_slice(&hex!("6000600060006000")); // retSize, retOffset, argsSize, argsOffset - code.push(0x73); // PUSH20 - code.extend_from_slice(implementation.as_slice()); // implementation address - code.extend_from_slice(&hex!("61FFFF")); // PUSH2 gas - code.push(0xF4); // DELEGATECALL - code.push(0x00); // STOP + let code = [ + &hex!("6000600060006000")[..], // retSize, retOffset, argsSize, argsOffset + &[0x73][..], // PUSH20 + implementation.as_slice(), // implementation address + &hex!("61FFFF")[..], // PUSH2 gas + &[0xF4][..], // DELEGATECALL + &[0x00][..], // STOP + ] + .concat(); Bytes::from(code) }; let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( @@ -1611,15 +2071,11 @@ fn test_geth_calltracer_logs_delegatecall() { ); db.insert_account_info( &caller, - AccountInfo { - balance: alloy_primitives::U256::from(1_000_000_000), - ..Default::default() - }, + AccountInfo { balance: U256::from(1_000_000_000), ..Default::default() }, ); }); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); let mut evm = context.build_mainnet().with_inspector(&mut insp); @@ -1664,10 +2120,8 @@ fn test_geth_calltracer_logs_delegatecall() { /// Exercise JSON options, tracer dispatch, EVM execution, and serialized trace output together. #[test] fn test_geth_opcode_limit_end_to_end() { - use serde_json::{Value, json}; - let account = address!("1000000000000000000000000000000000000001"); - let child = address!("00000000000000000000000000000000000000ff"); + let child = Address::with_last_byte(0xff); for terminal in [op::RETURN, op::REVERT] { let run = |options: Value| { let options: GethDebugTracingOptions = serde_json::from_value(options).unwrap(); @@ -1676,9 +2130,8 @@ fn test_geth_opcode_limit_end_to_end() { let mut code = hex!("6000600060006000600060ff61fffff150602a60005360016000").to_vec(); code.push(terminal); let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - for (addr, code) in - [(account, Bytes::from(code)), (child, hex!("60015000").into())] + Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + for (addr, code) in [(account, Bytes::from(code)), (child, bytes!("60015000"))] { db.insert_account_info( &addr, @@ -1717,11 +2170,15 @@ fn test_geth_opcode_limit_end_to_end() { assert_eq!(steps[8]["depth"], 2); assert_eq!(steps[11]["depth"], 1); - // The cap must count steps across call frames, including a cap inside the child. - for limit in [2, 9, 12] { + // The limit counts bytes of JSON across call frames, including a cap inside the child. + // geth logs a step while the JSON written so far is within the limit, so the bytes of the + // first `steps - 1` steps let `steps` steps through. + let sizes: Vec<_> = steps.iter().map(geth_json_len).collect(); + for steps in [2, 9, 12] { + let limit: usize = sizes[..steps - 1].iter().sum(); let (limited, limited_gas) = run(json!({ "limit": limit })); let mut expected = full.clone(); - expected["structLogs"].as_array_mut().unwrap().truncate(limit); + expected["structLogs"].as_array_mut().unwrap().truncate(steps); assert_eq!(limited, expected); assert_eq!(limited_gas, gas_used); } @@ -1734,20 +2191,85 @@ fn test_geth_opcode_limit_end_to_end() { } } +/// geth emits a child's steps right after the call step that entered it. A zero-value call to an +/// excluded precompile attaches no child, so it must not take the next call's child. +#[test] +fn test_geth_struct_logs_follow_calls_to_precompiles() { + let account = address!("1000000000000000000000000000000000000001"); + let child = address!("0000000000000000000000000000000000001234"); + let call = |target: [u8; 2]| { + [ + vec![op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0, op::PUSH1, 0], + vec![op::PUSH2, target[0], target[1], op::PUSH2, 0xff, 0xff, op::CALL, op::POP], + ] + .concat() + }; + // Call the identity precompile, then the child, which runs a few opcodes. + let code = [call([0, 4]), call([0x12, 0x34]), vec![op::STOP]].concat(); + let child_code = vec![op::PUSH1, 1, op::PUSH1, 1, op::ADD, op::STOP]; + + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + for (addr, code) in [(account, code), (child, child_code)] { + db.insert_account_info( + &addr, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + } + }); + let mut inspector = TracingInspector::new(TracingInspectorConfig::default_geth()); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TxKind::Call(account), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let frame = inspector.geth_builder().geth_traces(0, Bytes::new(), Default::default()); + let ops: Vec<_> = frame.struct_logs.iter().map(|log| (&*log.op, log.depth)).collect(); + let mut expected = vec![("PUSH1", 1); 5]; + expected.extend([("PUSH2", 1), ("PUSH2", 1), ("CALL", 1), ("POP", 1)]); + expected.extend(vec![("PUSH1", 1); 5]); + expected.extend([("PUSH2", 1), ("PUSH2", 1), ("CALL", 1)]); + expected.extend([("PUSH1", 2), ("PUSH1", 2), ("ADD", 2), ("STOP", 2)]); + expected.extend([("POP", 1), ("STOP", 1)]); + assert_eq!(ops, expected); +} + +/// Returns the bytes of the JSON geth writes for a struct log, which leaves out a zero refund and +/// empty return data, memory and storage. +fn geth_json_len(log: &serde_json::Value) -> usize { + let mut log = log.clone(); + let fields = log.as_object_mut().unwrap(); + for (name, empty) in [ + ("refund", serde_json::json!(0)), + ("returnData", serde_json::json!("0x")), + ("memory", serde_json::json!([])), + ("storage", serde_json::json!({})), + ] { + if fields.get(name) == Some(&empty) { + fields.remove(name); + } + } + log.to_string().len() +} + /// Execute real bytecode and compare capped capture with the complete execution. #[test] fn test_geth_opcode_limit() { let account = address!("1000000000000000000000000000000000000001"); - let child = address!("00000000000000000000000000000000000000ff"); + let child = Address::with_last_byte(0xff); for terminal in [op::RETURN, op::REVERT, op::INVALID] { // Call a child, then return/revert with one byte (0x2a), or halt exceptionally. let mut code = hex!("6000600060006000600060ff61fffff150602a60005360016000").to_vec(); code.push(terminal); let run = |inspector: &mut TracingInspector, opts: GethDefaultTracingOptions| { let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { for (addr, code) in - [(account, Bytes::from(code.clone())), (child, hex!("60015000").into())] + [(account, Bytes::from(code.clone())), (child, bytes!("60015000"))] { db.insert_account_info( &addr, @@ -1782,13 +2304,36 @@ fn test_geth_opcode_limit() { assert_eq!(full.failed, terminal != op::RETURN); assert_eq!( full.return_value, - if terminal == op::INVALID { Bytes::new() } else { hex!("2a").into() } + if terminal == op::INVALID { Bytes::new() } else { bytes!("2a") } ); - // Includes caps before CALL, on CALL, in the child, and after returning to the parent. - for limit in [None, Some(0), Some(2), Some(8), Some(9), Some(12), Some(u64::MAX)] { + // Includes caps before CALL, on CALL, in the child, and after returning to the parent. The + // limit counts bytes of JSON, and the first `steps - 1` steps let `steps` steps through. + let sizes: Vec<_> = full + .struct_logs + .iter() + .map(|log| geth_json_len(&serde_json::to_value(log).unwrap())) + .collect(); + let step_limit = |steps: usize| sizes[..steps - 1].iter().sum::() as u64; + for limit in [ + None, + Some(0), + Some(step_limit(2)), + Some(step_limit(8)), + Some(step_limit(9)), + Some(step_limit(12)), + Some(u64::MAX), + ] { let opts = GethDefaultTracingOptions { limit, ..Default::default() }; - let expected_len = limit.filter(|n| *n != 0).map_or(full.struct_logs.len(), |n| { - full.struct_logs.len().min(usize::try_from(n).unwrap_or(usize::MAX)) + let expected_len = limit.filter(|n| *n != 0).map_or(full.struct_logs.len(), |limit| { + let mut written = 0; + sizes + .iter() + .take_while(|size| { + let logged = written as u64 <= limit; + written += **size; + logged + }) + .count() }); let mut expected = full.clone(); expected.struct_logs.truncate(expected_len); @@ -1797,10 +2342,14 @@ fn test_geth_opcode_limit() { for _ in 0..2 { let actual = run(&mut inspector, opts); assert_eq!(actual, expected, "limit {limit:?}, terminal {terminal}"); - assert_eq!( - inspector.traces().nodes().iter().map(|n| n.trace.steps.len()).sum::(), - expected_len - ); + // Capture stops at the most steps the limit can hold, at least a byte count's worth + // of the smallest log each. + let recorded = + inspector.traces().nodes().iter().map(|n| n.trace.steps.len()).sum::(); + assert!(recorded >= expected_len); + if let Some(limit) = limit.filter(|limit| *limit != 0) { + assert!(recorded as u64 <= limit / 48 + 1); + } inspector.fuse(); } // Builder options also apply to already collected unlimited traces. @@ -1814,22 +2363,14 @@ fn test_geth_opcode_limit() { #[test] fn test_geth_opcode_limit_ignores_named_tracers() { - for tracer in - [GethDebugBuiltInTracerType::CallTracer, GethDebugBuiltInTracerType::Erc7562Tracer] - { - let opts = GethDebugTracingOptions { - tracer: Some(GethDebugTracerType::BuiltInTracer(tracer)), - config: GethDefaultTracingOptions { limit: Some(2), ..Default::default() }, - ..Default::default() - }; - let inspector = DebugInspector::new(opts).unwrap(); - let config = match &inspector { - DebugInspector::CallTracer(inspector, _) - | DebugInspector::Erc7562Tracer(inspector, _) => inspector.config(), - _ => panic!("unexpected tracer"), - }; - assert_eq!(config.step_limit, None); - } + let opts = GethDebugTracingOptions { + tracer: Some(GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)), + config: GethDefaultTracingOptions { limit: Some(2), ..Default::default() }, + ..Default::default() + }; + let inspector = DebugInspector::new(opts).unwrap(); + let DebugInspector::CallTracer(inspector, _) = &inspector else { panic!("unexpected tracer") }; + assert_eq!(inspector.config().step_limit, None); } #[test] @@ -1839,15 +2380,12 @@ fn test_geth_struct_log_out_of_gas_cost() { let code = [op::PUSH1, 0x01, op::PUSH1, 0x00, op::SSTORE]; let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( &contract, - AccountInfo { - code: Some(Bytecode::new_raw(code.to_vec().into())), - ..Default::default() - }, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, ); }); @@ -1891,7 +2429,7 @@ fn test_geth_calltracer_log_index_eip7708_revert() { let reverts = address!("0000000000000000000000000000000000002222"); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) .modify_db_chained(|db| { db.insert_account_info( @@ -1922,8 +2460,7 @@ fn test_geth_calltracer_log_index_eip7708_revert() { ); }); - let mut insp = - TracingInspector::new(TracingInspectorConfig::default_geth().set_record_logs(true)); + let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth().record_logs()); let mut evm = context.build_mainnet().with_inspector(&mut insp); let res = evm @@ -1977,12 +2514,12 @@ fn test_debug_call_log_index_seed_and_reset() { inspector.set_next_log_index(seed); } let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { db.insert_account_info( &account, AccountInfo { // PUSH0 PUSH0 LOG0 STOP. - code: Some(Bytecode::new_raw(hex!("5f5fa000").into())), + code: Some(Bytecode::new_raw(bytes!("5f5fa000"))), ..Default::default() }, ); @@ -2028,13 +2565,13 @@ fn test_debug_call_log_index_seed_and_reset() { #[test] fn test_geth_4byte_tracer_skips_precompiles() { // STATICCALL the sha256 precompile with 36 bytes, then an empty account with 4 bytes. - let code = hex!("600060006024600060025afa50600060006004600061dead5afa5000").into(); + let code = bytes!("600060006024600060025afa50600060006004600061dead5afa5000"); let opts = serde_json::from_value(serde_json::json!({ "tracer": "4byteTracer" })).unwrap(); let (trace, _) = trace_contract(SpecId::PRAGUE, opts, code); match trace { GethTrace::FourByteTracer(frame) => { - assert_eq!(frame.0, std::collections::BTreeMap::from([("0x00000000-0".into(), 1)])); + assert_eq!(frame.0, BTreeMap::from([("0x00000000-0".into(), 1)])); } _ => panic!("Expected FourByteTracer"), } @@ -2046,12 +2583,12 @@ fn test_debug_default_tracer_moves_stacks() { let caller = address!("1000000000000000000000000000000000000001"); let callee = address!("1000000000000000000000000000000000000002"); // Call the callee with zero arguments, then push a value. - let mut caller_code = hex!("6000600060006000600073").to_vec(); - caller_code.extend_from_slice(callee.as_slice()); - caller_code.extend_from_slice(&hex!("5af150600700")); + let caller_code = + [&hex!("6000600060006000600073")[..], callee.as_slice(), &hex!("5af150600700")[..]] + .concat(); for disable_stack in [false, true] { - for limit in [None, Some(0), Some(10)] { + for limit in [None, Some(0), Some(1000)] { let config = GethDefaultTracingOptions { disable_stack: Some(disable_stack), limit, @@ -2060,7 +2597,7 @@ fn test_debug_default_tracer_moves_stacks() { let mut inspector = DebugInspector::new(GethDebugTracingOptions { config, ..Default::default() }) .unwrap(); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (address, code) in [(caller, caller_code.clone()), (callee, hex!("6001600201").to_vec())] { @@ -2133,7 +2670,7 @@ fn test_geth_failed_root_logs() { let code = code.parse::().unwrap(); for create in [false, true] { let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) .modify_db_chained(|db| { db.insert_account_info( @@ -2147,7 +2684,7 @@ fn test_geth_failed_root_logs() { let mut inspector = TracingInspector::new( TracingInspectorConfig::from_geth_call_config(&CallConfig::default().with_log()), ); - let mut evm = context.build_mainnet().with_inspector(&mut inspector); + let mut evm = context.clone().build_mainnet().with_inspector(&mut inspector); let res = evm .inspect_tx(TxEnv { kind: if create { TxKind::Create } else { TxKind::Call(contract) }, @@ -2175,14 +2712,32 @@ fn test_geth_failed_root_logs() { assert_eq!(serde_json::to_value(frame).unwrap().get("logs"), None); } } + } - let frame = builder - .geth_erc7562_traces( - Erc7562Config { with_log: Some(with_log), ..Default::default() }, - res.result.tx_gas_used(), - &mut EmptyDB::default(), - ) + for with_log in [false, true] { + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config { + with_log: Some(with_log), + ..Default::default() + }); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = context.clone().build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + kind: if create { TxKind::Create } else { TxKind::Call(contract) }, + data: if create { code.clone() } else { Bytes::new() }, + gas_limit: 100_000, + ..Default::default() + }) .unwrap(); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = + inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { + panic!("Expected Erc7562Tracer") + }; + let expected_logs = usize::from(with_log && error.is_none()); assert_eq!(frame.error.as_deref(), error); assert_eq!(frame.logs.len(), expected_logs); if expected_logs == 0 { @@ -2192,3 +2747,830 @@ fn test_geth_failed_root_logs() { } } } + +fn contract_with_code(code: &[u8]) -> (Address, InMemoryDB) { + let contract = Address::with_last_byte(0x42); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &contract, + AccountInfo::default().with_code(Bytecode::new_raw(code.to_vec().into())), + ); + (contract, db) +} + +#[test] +fn keccak256_preimage_tracer_collects_padded_preimages() { + let (contract, db) = contract_with_code(&hex!( + // MSTORE(0, 0x2a), then KECCAK256 of (0, 32), of (0, 0), of (0x10, 0x40) and of (0, 2 MiB). + "602a5f52" "60205f2050" "5f5f2050" "6040601020" "50" "62200000" "5f2050" + )); + let mut inspector = Keccak256PreimageInspector::default(); + Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(&mut inspector) + .inspect_tx(TxEnv { + gas_limit: 10_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let word: [u8; 32] = B256::with_last_byte(0x2a).into(); + let mut padded = [0; 0x40]; + padded[..0x10].copy_from_slice(&word[0x10..]); + let expected = serde_json::Value::Object( + [&word[..], &[], &padded] + .into_iter() + .map(|preimage| { + (hex::encode_prefixed(keccak256(preimage)), hex::encode_prefixed(preimage).into()) + }) + .collect(), + ); + assert_eq!(inspector.json_result().unwrap(), expected); +} + +#[test] +fn keccak256_preimage_tracer_runs_standalone_and_in_a_mux() { + let options: GethDebugTracingOptions = + serde_json::from_value(serde_json::json!({"tracer": "keccak256PreimageTracer"})).unwrap(); + assert!(matches!(DebugInspector::new(options).unwrap(), DebugInspector::Keccak256Preimage(_))); + + let (contract, db) = contract_with_code(&hex!("602a5f5260205f2050")); + let key = GethDebugTracerType::JsTracer(KECCAK256_PREIMAGE_TRACER.into()); + let config = MuxConfig(HashMap::from_iter([(key.clone(), None)])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let (ctx, inspector) = evm.ctx_inspector(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); + let GethTrace::JS(preimages) = &frame.0[&key] else { panic!("expected a JSON object") }; + assert_eq!(preimages.as_object().unwrap().len(), 1); +} + +#[test] +fn mux_and_debug_inspector_forward_the_steps_of_their_members() { + let steps = |inspector: &dyn Inspector| inspector.inspector_config().steps(); + let four_byte = GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::FourByteTracer); + let keccak = GethDebugTracerType::JsTracer(KECCAK256_PREIMAGE_TRACER.into()); + let mux = |tracers: &[&GethDebugTracerType]| { + let config = MuxConfig(tracers.iter().map(|tracer| ((*tracer).clone(), None)).collect()); + MuxInspector::try_from_config(config).unwrap() + }; + + // The 4byte tracer only needs message hooks, and the keccak tracer steps `KECCAK256`. + let keccak_steps = [op::KECCAK256].into_iter().collect::(); + assert_eq!(steps(&mux(&[&four_byte])), OpcodeSet::EMPTY); + assert_eq!(steps(&mux(&[&four_byte, &keccak])), keccak_steps); + let options = serde_json::from_value::( + serde_json::json!({"tracer": "4byteTracer"}), + ) + .unwrap(); + assert_eq!(steps(&DebugInspector::new(options).unwrap()), OpcodeSet::EMPTY); + let options = serde_json::from_value::( + serde_json::json!({"tracer": "keccak256PreimageTracer"}), + ) + .unwrap(); + assert_eq!(steps(&DebugInspector::new(options).unwrap()), keccak_steps); +} + +#[test] +fn keccak256_preimage_tracer_bounds_the_padding_like_geth() { + // KECCAK256 of `MEMORY_PAD_LIMIT` and of one byte more at offset 0 of empty memory. + for (size, recorded) in [(MEMORY_PAD_LIMIT, 1), (MEMORY_PAD_LIMIT + 1, 0)] { + // PUSH3 size, PUSH0, KECCAK256. + let code = [&[0x62][..], &size.to_be_bytes()[5..], &hex!("5f20")].concat(); + let (contract, db) = contract_with_code(&code); + let mut inspector = Keccak256PreimageInspector::default(); + Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(&mut inspector) + .inspect_tx(TxEnv { + gas_limit: 10_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + assert_eq!(inspector.json_result().unwrap().as_object().unwrap().len(), recorded); + } +} + +#[test] +fn keccak256_preimage_tracer_stops_recording_at_the_byte_limit() { + // MSTORE(0, 0x2a), then KECCAK256 of 32 and of 64 bytes. + let code = hex!("602a5f52" "60205f2050" "60405f2050"); + for (limit, recorded, exceeded) in [(40, 1, true), (96, 2, false)] { + let (contract, db) = contract_with_code(&code); + let mut inspector = Keccak256PreimageInspector::new( + TraceLimits::default().set_max_recorded_bytes(Some(limit)), + ); + Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(&mut inspector) + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + assert_eq!(inspector.limit_exceeded(), exceeded); + match inspector.json_result() { + Ok(preimages) => { + assert_eq!((preimages.as_object().unwrap().len(), exceeded), (recorded, false)) + } + Err(_) => assert!(exceeded), + } + } +} + +fn precompile_call_contract(code: &[u8]) -> (Address, InMemoryDB) { + let contract = Address::with_last_byte(0x42); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &contract, + AccountInfo::default() + .with_balance(U256::from(2)) + .with_code(Bytecode::new_raw(code.to_vec().into())), + ); + (contract, db) +} + +#[test] +fn flat_call_tracer_excludes_call_and_staticcall_to_precompiles() { + // CALL 0x04 with value 1, STATICCALL 0x04, CALLCODE 0x04 with value 1, DELEGATECALL 0x02 and + // CALLCODE 0x06 without value. + let (contract, db) = precompile_call_contract(&hex!( + "5f5f5f5f600160045af1505f5f5f5f60045afa505f5f5f5f600160045af2505f5f5f5f60025af450" + "5f5f5f5f5f60065af25000" + )); + let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( + TracingInspector::new(TracingInspectorConfig::default_parity()), + ); + evm.inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + for (include_precompiles, expected) in [ + ( + None, + vec![CallType::Call, CallType::CallCode, CallType::DelegateCall, CallType::CallCode], + ), + ( + Some(true), + vec![ + CallType::Call, + CallType::Call, + CallType::StaticCall, + CallType::CallCode, + CallType::DelegateCall, + CallType::CallCode, + ], + ), + ] { + let config = FlatCallConfig { include_precompiles, ..Default::default() }; + let traces = evm + .inspector + .clone() + .into_flat_call_builder(&config) + .into_localized_transaction_traces(TransactionInfo::default()); + let call_types: Vec<_> = traces + .iter() + .map(|trace| match &trace.trace.action { + Action::Call(call) => call.call_type, + action => panic!("unexpected action {action:?}"), + }) + .collect(); + assert_eq!(call_types, expected); + assert_eq!(traces[0].trace.subtraces, expected.len() - 1); + } +} + +#[test] +fn mux_flat_call_tracer_does_not_shift_call_tracer_log_positions() { + // CALL 0x01, then LOG0. + let (contract, db) = precompile_call_contract(&hex!("5f5f5f5f5f60015af1505f5fa000")); + let call_config = CallConfig { only_top_call: Some(false), with_log: Some(true) }; + let config = MuxConfig(HashMap::from_iter([ + ( + GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer), + Some(GethDebugTracerConfig(serde_json::to_value(call_config).unwrap())), + ), + (GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::FlatCallTracer), None), + ])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let (ctx, inspector) = evm.ctx_inspector(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); + + let GethTrace::CallTracer(call_frame) = + &frame.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)] + else { + panic!("expected a call tracer frame"); + }; + assert_eq!(call_frame.calls.len(), 1); + assert_eq!(call_frame.logs[0].position, Some(1)); + + let GethTrace::FlatCallTracer(traces) = + &frame.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::FlatCallTracer)] + else { + panic!("expected a flat call tracer frame"); + }; + assert_eq!(traces.len(), 1); +} + +#[test] +fn mux_runs_nested_mux_and_erc7562_tracers() { + let (contract, db) = contract_with_code(&hex!("5f5f00")); + let nested = serde_json::json!({"callTracer": {}, "noopTracer": {}}); + let nested_key = GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::MuxTracer); + let config = MuxConfig(HashMap::from_iter([ + (GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::FourByteTracer), None), + (GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::Erc7562Tracer), None), + (nested_key.clone(), Some(GethDebugTracerConfig(nested))), + ])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let (ctx, inspector) = evm.ctx_inspector(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); + + assert_eq!(frame.0.len(), 3); + assert!(matches!( + frame.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::Erc7562Tracer)], + GethTrace::Erc7562Tracer(_) + )); + let GethTrace::MuxTracer(nested) = &frame.0[&nested_key] else { + panic!("expected a nested mux frame"); + }; + assert_eq!(nested.0.len(), 2); + assert!( + nested.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer)] + .is_call() + ); + assert!( + nested.0[&GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::NoopTracer)] + .is_noop() + ); +} + +/// The expected snapshots come from go-ethereum master a0cba163: the struct logger adds the slot +/// of every SLOAD and SSTORE that does not fail before it runs, with the value an SLOAD reads or an +/// SSTORE writes, and snapshots all slots of the contract so far. +#[test] +fn struct_log_storage_snapshots_match_geth() { + let account = address!("1000000000000000000000000000000000000001"); + let slot = |slot: u8| B256::with_last_byte(slot); + let value = |value: u8| B256::with_last_byte(value); + // SLOAD 5 (holds 7), SSTORE 5 = 7 (unchanged), SLOAD 5 (warm), SSTORE 6 = 1, SLOAD 6, and an + // SSTORE that runs out of gas, which fails before it runs. + let code = hex!("60055450600760055560055450600160065560065450600160075500"); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &account, + AccountInfo { code: Some(Bytecode::new_raw(Bytes::from(code))), ..Default::default() }, + ); + db.insert_account_storage(&account, &U256::from(5), &U256::from(7)); + let mut inspector = DebugInspector::new(Default::default()).unwrap(); + let mut evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) + .build_mainnet() + .with_inspector(&mut inspector); + let result = evm + .inspect_tx(TxEnv { + gas_limit: 21000 + 3 * 1000 + 30000, + kind: TransactTo::Call(account), + ..Default::default() + }) + .unwrap(); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &result.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Default(frame) = trace else { panic!("expected a default trace") }; + + let snapshots: Vec<_> = frame + .struct_logs + .iter() + .filter(|log| matches!(log.opcode(), "SLOAD" | "SSTORE")) + .map(|log| (log.op.to_string(), log.storage.clone(), log.error.is_some())) + .collect(); + let after_first = BTreeMap::from([(slot(5), value(7))]); + let after_sstore = BTreeMap::from([(slot(5), value(7)), (slot(6), value(1))]); + assert_eq!(snapshots[0], ("SLOAD".into(), Some(after_first.clone()), false)); + assert_eq!(snapshots[1], ("SSTORE".into(), Some(after_first.clone()), false)); + assert_eq!(snapshots[2], ("SLOAD".into(), Some(after_first), false)); + assert_eq!(snapshots[3].1, Some(after_sstore.clone())); + assert_eq!(snapshots[4], ("SLOAD".into(), Some(after_sstore), false)); + assert_eq!((snapshots[5].1.clone(), snapshots[5].2), (None, true)); +} + +/// A delegatecall child writes to the storage of its caller, and an SSTORE that fails in a static +/// call leaves no snapshot. +#[test] +fn struct_log_storage_snapshots_follow_the_storage_context() { + let parent = address!("1000000000000000000000000000000000000001"); + let writer = Address::with_last_byte(0xc0); + let static_writer = Address::with_last_byte(0xd0); + // The parent delegatecalls the writer, loads the slot it wrote, then static calls the second + // writer. + let parent_code = hex!( + "600060006000600060c05af450" + "60015450" + "600060006000600060d05afa5000" + ); + let mut db = InMemoryDB::default(); + for (address, code) in [ + (parent, parent_code.to_vec()), + (writer, hex!("6002600155").to_vec()), + (static_writer, hex!("6003600455").to_vec()), + ] { + db.insert_account_info( + &address, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + } + let mut inspector = DebugInspector::new(Default::default()).unwrap(); + let mut evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) + .build_mainnet() + .with_inspector(&mut inspector); + let result = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(parent), + ..Default::default() + }) + .unwrap(); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &result.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Default(frame) = trace else { panic!("expected a default trace") }; + + let snapshot = BTreeMap::from([(B256::with_last_byte(1), B256::with_last_byte(2))]); + let logs: Vec<_> = frame + .struct_logs + .iter() + .filter(|log| matches!(log.opcode(), "SLOAD" | "SSTORE")) + .map(|log| (log.depth, log.op.to_string(), log.storage.clone())) + .collect(); + assert_eq!( + logs, + [ + (2, "SSTORE".to_string(), Some(snapshot.clone())), + (1, "SLOAD".to_string(), Some(snapshot)), + (2, "SSTORE".to_string(), None), + ] + ); +} + +/// Geth omits every account that was empty before the transaction from the pre state, including +/// accounts that the transaction only funds, such as a fresh recipient and a fresh coinbase. +#[test] +fn test_geth_prestate_diff_omits_empty_accounts() { + let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); + let coinbase = address!("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"); + let recipient = Address::repeat_byte(0x22); + + let mut db = InMemoryDB::default(); + db.insert_account_info( + &sender, + AccountInfo { balance: U256::from(100_000_000_000_000_000u64), ..Default::default() }, + ); + let evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) + .modify_block_chained(|block| { + block.beneficiary = coinbase; + block.basefee = U256::from(8); + }) + .build_mainnet(); + + let prestate_config = PreStateConfig { diff_mode: Some(true), ..Default::default() }; + let mut insp = + TracingInspector::new(TracingInspectorConfig::from_geth_prestate_config(&prestate_config)); + let mut db = evm.ctx.db.clone(); + let res = evm + .with_inspector(&mut insp) + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 100_000, + gas_price: 0x600, + kind: TransactTo::Call(recipient), + value: U256::from(5), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let frame = insp + .with_transaction_gas_used(res.result.tx_gas_used()) + .geth_builder() + .geth_prestate_traces(&res.tx_result, &prestate_config, &mut db) + .unwrap(); + + // Output of geth's `evm t8n --trace.tracer prestateTracer --trace.jsonconfig + // '{"diffMode":true}'`. + assert_eq!( + serde_json::to_value(frame).unwrap(), + serde_json::json!({ + "post": { + "0x2222222222222222222222222222222222222222": { "balance": "0x5" }, + "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba": { "balance": "0x1e99fc0" }, + "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": { + "balance": "0x16345785b9dcffb", + "nonce": 1 + } + }, + "pre": { + "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": { "balance": "0x16345785d8a0000" } + } + }) + ); +} + +/// Geth reports `"code":"0x"` in the post state when the transaction clears an account's code, as +/// an EIP-7702 authorization to the zero address does with a delegation. +#[test] +fn test_geth_prestate_diff_reports_cleared_code() { + let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); + let authority = address!("7e5f4552091a69125d5dfcb7b8c2659029395bdf"); + let delegation = bytes!("ef0100000000000000000000000000000000000000aaaa"); + + let mut db = InMemoryDB::default(); + db.insert_account_info( + &sender, + AccountInfo { balance: U256::from(100_000_000_000_000_000u64), ..Default::default() }, + ); + db.insert_account_info( + &authority, + AccountInfo { + balance: U256::ONE, + code_hash: keccak256(&delegation), + code: Some(Bytecode::new_raw(delegation.clone())), + ..Default::default() + }, + ); + let evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::PRAGUE) + .build_mainnet(); + + // Signed by the key of `authority`; delegates to the zero address, which clears the code. + let authorization = Authorization { chain_id: U256::ONE, address: Address::ZERO, nonce: 0 }; + let signature = sign_message(B256::with_last_byte(1), authorization.signature_hash()).unwrap(); + let authorization = authorization.into_signed(signature); + assert_eq!(authorization.recover_authority().unwrap(), authority); + + let prestate_config = PreStateConfig { diff_mode: Some(true), ..Default::default() }; + let mut insp = + TracingInspector::new(TracingInspectorConfig::from_geth_prestate_config(&prestate_config)); + let mut db = evm.ctx.db.clone(); + let res = evm + .with_inspector(&mut insp) + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 100_000, + kind: TransactTo::Call(Address::repeat_byte(0x22)), + authorization_list: vec![authorization], + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let frame = insp + .with_transaction_gas_used(res.result.tx_gas_used()) + .geth_builder() + .geth_prestate_traces(&res.tx_result, &prestate_config, &mut db) + .unwrap(); + let PreStateFrame::Diff(diff) = frame else { panic!("expected a diff frame") }; + + assert_eq!(diff.pre[&authority].code, Some(delegation)); + assert_eq!(diff.post[&authority].code, Some(Bytes::new())); + assert_eq!(diff.post[&authority].nonce, Some(1)); + assert_eq!( + serde_json::to_value(&diff.post[&authority]).unwrap(), + serde_json::json!({ "code": "0x", "nonce": 1 }) + ); +} + +/// Runs a London transaction that reads slots 1 (holds `0x2a`) and 2 (zero) of a contract and +/// selfdestructs it, and returns the contract and the prestate frame. +fn trace_london_selfdestruct(prestate_config: PreStateConfig) -> (Address, PreStateFrame) { + let sender = address!("a94f5374fce5edbc8e2a8697c15331677e6ebf0b"); + let contract = Address::repeat_byte(0x11); + + // SLOAD 1, SLOAD 2, SELFDESTRUCT to the beneficiary. + let code = bytes!("6001545060025450733333333333333333333333333333333333333333ff"); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &sender, + AccountInfo { balance: U256::from(100_000_000_000_000_000u64), ..Default::default() }, + ); + db.insert_account_info( + &contract, + AccountInfo::new( + U256::from(7), + 1, + alloy_primitives::keccak256(&code), + Some(Bytecode::new_raw(code)), + ), + ); + db.insert_account_storage(&contract, &U256::ONE, &U256::from(0x2a)); + let evm = Context::mainnet() + .with_db(db) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::LONDON) + .build_mainnet(); + + let mut insp = + TracingInspector::new(TracingInspectorConfig::from_geth_prestate_config(&prestate_config)); + let mut db = evm.ctx.db.clone(); + let res = evm + .with_inspector(&mut insp) + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 100_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + + let frame = insp + .with_transaction_gas_used(res.result.tx_gas_used()) + .geth_builder() + .geth_prestate_traces(&res.tx_result, &prestate_config, &mut db) + .unwrap(); + (contract, frame) +} + +/// Geth keeps the pre state of a selfdestructed account whole, with every slot the transaction +/// read, including unchanged and zero ones. +#[test] +fn test_geth_prestate_diff_selfdestruct_keeps_read_slots() { + let beneficiary = Address::repeat_byte(0x33); + let (contract, frame) = + trace_london_selfdestruct(PreStateConfig { diff_mode: Some(true), ..Default::default() }); + let PreStateFrame::Diff(diff) = frame else { panic!("expected a diff frame") }; + + // Output of geth's `evm t8n --state.fork London` with the prestate tracer in diff mode. + assert!(!diff.post.contains_key(&contract)); + assert_eq!(diff.post[&beneficiary].balance, Some(U256::from(7))); + assert_eq!( + serde_json::to_value(&diff.pre[&contract]).unwrap(), + serde_json::json!({ + "balance": "0x7", + "code": "0x6001545060025450733333333333333333333333333333333333333333ff", + "nonce": 1, + "storage": { + "0x0000000000000000000000000000000000000000000000000000000000000001": + "0x000000000000000000000000000000000000000000000000000000000000002a", + "0x0000000000000000000000000000000000000000000000000000000000000002": + "0x0000000000000000000000000000000000000000000000000000000000000000" + } + }) + ); +} + +/// geth reads a slot before the transaction runs, so the pre state of a contract that selfdestructs +/// reports the value the slot held, not the zero its storage wipe leaves behind. +#[test] +fn test_geth_prestate_selfdestruct_reports_slots_before_the_wipe() { + let (contract, frame) = trace_london_selfdestruct(PreStateConfig::default()); + let PreStateFrame::Default(prestate) = frame else { panic!("expected a prestate frame") }; + + // Output of geth's `evm t8n --state.fork London` with the prestate tracer. + assert_eq!( + serde_json::to_value(&prestate.0[&contract]).unwrap(), + serde_json::json!({ + "balance": "0x7", + "code": "0x6001545060025450733333333333333333333333333333333333333333ff", + "nonce": 1, + "storage": { + "0x0000000000000000000000000000000000000000000000000000000000000001": + "0x000000000000000000000000000000000000000000000000000000000000002a", + "0x0000000000000000000000000000000000000000000000000000000000000002": + "0x0000000000000000000000000000000000000000000000000000000000000000" + } + }) + ); +} + +#[test] +fn test_geth_erc7562_operand_overflow_is_not_out_of_gas() { + let root = address!("1000000000000000000000000000000000000001"); + let child = address!("1000000000000000000000000000000000000002"); + // geth rejects a >64-bit memory operand before metering dynamic gas, but an + // affordable-size expansion with insufficient gas is still an out-of-gas error. + for (offset, out_of_gas) in + [(U256::MAX, false), (U256::from(0x10_0000), true), (U256::from(u32::MAX), true)] + { + let mut child_code = vec![op::PUSH1, 0, op::PUSH32]; + child_code.extend_from_slice(&offset.to_be_bytes::<32>()); + child_code.push(op::MSTORE); + let mut root_code = hex!("60006000600060006000").to_vec(); + root_code.push(op::PUSH20); + root_code.extend_from_slice(child.as_slice()); + root_code.extend_from_slice(&hex!("61fffff15000")); + let mut db = InMemoryDB::default(); + for (address, code) in [(root, root_code), (child, child_code)] { + db.insert_account_info( + &address, + AccountInfo { code: Some(Bytecode::new_raw(code.into())), ..Default::default() }, + ); + } + let opts = GethDebugTracingOptions::erc7562_tracer(Erc7562Config::default()); + let mut inspector = DebugInspector::new(opts).unwrap(); + let mut evm = Context::mainnet().with_db(db).build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(root), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); + let (ctx, inspector) = evm.ctx_inspector(); + let tx = ctx.tx().envelope(); + let block = *ctx.block(); + let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); + let GethTrace::Erc7562Tracer(frame) = trace else { panic!("Expected Erc7562Tracer") }; + assert!(!frame.out_of_gas); + assert_eq!(frame.calls.len(), 1); + let call = &frame.calls[0]; + assert!(call.error.is_some(), "offset={offset:#x}"); + assert_eq!(call.used_opcodes, HashMap::from_iter([(op::MSTORE, 1)])); + assert_eq!(call.out_of_gas, out_of_gas, "offset={offset:#x}"); + } +} + +/// Asks to step `steps` and records the opcodes it is stepped for, which are those of every +/// inspector it runs with. +struct StepRecorder { + steps: OpcodeSet, + stepped: OpcodeSet, +} + +impl Inspector for StepRecorder { + fn inspector_config(&self) -> InspectorConfig { + InspectorConfig::new().with_steps(self.steps) + } + + fn step(&mut self, interp: &mut Interpreter<'_, '_, BaseEvmTypes>) { + self.stepped.insert(interp.opcode()); + } +} + +/// Traces a contract that stores, loads, writes memory, logs and calls a second contract that +/// stores and loads. +fn run_with_recorder( + config: TracingInspectorConfig, + recorder_steps: OpcodeSet, +) -> (TracingInspector, OpcodeSet) { + let (contract, mut db) = contract_with_code(&hex!( + // SSTORE(0, 5), SLOAD(0), MSTORE8(0, 0xaa), LOG0(0, 1), CALL(0x2000, 0x43), STOP. + "6005" "5f55" "5f54" "50" "60aa" "5f53" "6001" "5f" "a0" + "5f5f5f5f5f" "6043" "612000" "f1" "50" "00" + )); + db.insert_account_info( + &Address::with_last_byte(0x43), + AccountInfo::default().with_code(Bytecode::new_raw( + // SSTORE(1, 7), SLOAD(0), STOP. + bytes!("6007" "6001" "55" "5f54" "50" "00"), + )), + ); + let mut inspectors = ( + TracingInspector::new(config), + StepRecorder { steps: recorder_steps, stepped: OpcodeSet::EMPTY }, + ); + Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(&mut inspectors) + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + (inspectors.0, inspectors.1.stepped) +} + +#[test] +fn tracing_inspector_steps_only_what_its_config_records() { + let steps = |config: TracingInspectorConfig| { + Inspector::::inspector_config(&TracingInspector::new(config)).steps() + }; + let filter = [op::SLOAD, op::SSTORE, op::CALL].into_iter().collect::(); + let filtered = TracingInspectorConfig { + record_opcodes_filter: Some(filter), + ..TracingInspectorConfig::default_geth() + }; + + // The call tracer, the prestate tracer and the parity traces other than `vmTrace` record no + // steps, and the struct logger, `vmTrace` and an unfiltered config step every opcode. + for config in [ + TracingInspectorConfig::none(), + TracingInspectorConfig::from_geth_call_config(&CallConfig { + with_log: Some(true), + ..Default::default() + }), + TracingInspectorConfig::from_geth_prestate_config(&PreStateConfig::default()), + TracingInspectorConfig::default_parity(), + TracingInspectorConfig::parity_statediff(), + TracingInspectorConfig { record_opcodes_filter: Some(filter), ..Default::default() }, + ] { + assert_eq!(steps(config), OpcodeSet::EMPTY, "{config:?}"); + } + for config in [ + TracingInspectorConfig::all(), + TracingInspectorConfig::default_geth(), + TracingInspectorConfig::parity_vm_trace(), + ] { + assert_eq!(steps(config), OpcodeSet::ALL, "{config:?}"); + } + assert_eq!(steps(filtered), filter); + + // Exceeding a halting budget in any hook halts the next step, so every opcode steps. + let halting = TraceLimits::default() + .set_max_recorded_bytes(Some(1024)) + .set_behavior(TraceLimitBehavior::Halt); + for config in [TracingInspectorConfig::none(), filtered] { + let inspector = TracingInspector::new(config).with_limits(halting); + assert_eq!(Inspector::::inspector_config(&inspector).steps(), OpcodeSet::ALL); + } +} + +#[test] +fn tracing_inspector_traces_the_same_with_the_steps_it_asks_for_and_with_every_step() { + let filter = [op::SLOAD, op::SSTORE, op::CALL].into_iter().collect::(); + for (config, stepped) in [ + (TracingInspectorConfig::none(), Some(OpcodeSet::EMPTY)), + ( + TracingInspectorConfig::from_geth_call_config(&CallConfig { + with_log: Some(true), + ..Default::default() + }), + Some(OpcodeSet::EMPTY), + ), + (TracingInspectorConfig::default_parity(), Some(OpcodeSet::EMPTY)), + (TracingInspectorConfig::parity_statediff(), Some(OpcodeSet::EMPTY)), + (TracingInspectorConfig::default_geth(), None), + (TracingInspectorConfig::parity_vm_trace(), None), + ( + TracingInspectorConfig { + record_opcodes_filter: Some(filter), + record_memory_snapshots: true, + ..TracingInspectorConfig::default_geth() + }, + Some(filter), + ), + ] { + // The recorder asks for nothing in the first run, so the tracer's own set decides what + // both are stepped for. In the second run the recorder asks for every opcode. + let (asked, asked_steps) = run_with_recorder(config, OpcodeSet::EMPTY); + let (every, every_steps) = run_with_recorder(config, OpcodeSet::ALL); + assert_eq!(format!("{:?}", asked.traces()), format!("{:?}", every.traces()), "{config:?}"); + match stepped { + Some(set) => { + assert_eq!(asked_steps, set, "{config:?}"); + assert_ne!(every_steps, set, "{config:?}"); + } + None => assert_eq!(asked_steps, every_steps, "{config:?}"), + } + } +} diff --git a/crates/inspectors/tests/it/geth/consistency.rs b/crates/inspectors/tests/it/geth/consistency.rs index 20cd9fefe..ff0c3571b 100644 --- a/crates/inspectors/tests/it/geth/consistency.rs +++ b/crates/inspectors/tests/it/geth/consistency.rs @@ -1,6 +1,8 @@ //! Multiplexed tracer configuration and flat-call root gas. use super::*; +use alloy_primitives::bytes; +use evm2::evm::InMemoryDB; use serde_json::{Value, json}; #[test] @@ -28,11 +30,11 @@ fn mux_config_matches_standalone() { fn flat_call_root_gas_matches_mux() { let trace = |options| { let target = Address::repeat_byte(0xaa); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &target, AccountInfo { - code: Some(Bytecode::new_raw(hex!("600160005200").into())), + code: Some(Bytecode::new_raw(bytes!("600160005200"))), ..Default::default() }, ); @@ -64,7 +66,7 @@ fn flat_call_runtime_oog_root_matches_mux() { let recipient = address!("1000000000000000000000000000000000000002"); let trace = |options| { let inspector = DebugInspector::new(serde_json::from_value(options).unwrap()).unwrap(); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, diff --git a/crates/inspectors/tests/it/geth/runtime_oog.rs b/crates/inspectors/tests/it/geth/runtime_oog.rs index 9da7f99fc..1a654e42e 100644 --- a/crates/inspectors/tests/it/geth/runtime_oog.rs +++ b/crates/inspectors/tests/it/geth/runtime_oog.rs @@ -1,8 +1,10 @@ -//! Call tracer regressions for halts before evm2 initializes the root frame. +//! Regressions for the root frame of a transaction that runs out of gas before execution. use super::*; +use alloy_primitives::bytes; use alloy_rpc_types_trace::geth::{CallFrame, CallLogFrame}; -use evm2::interpreter::InstrStop; +use evm2::{evm::InMemoryDB, interpreter::InstrStop}; +use evm2_inspectors::tracing::TracingInspector; #[test] fn runtime_oog_call_frames_and_reuse() { @@ -19,7 +21,7 @@ fn runtime_oog_call_frames_and_reuse() { let mut inspector = DebugInspector::new(serde_json::from_value(options).unwrap()).unwrap(); let context = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) .modify_db_chained(|db| { db.insert_account_info( @@ -29,13 +31,13 @@ fn runtime_oog_call_frames_and_reuse() { }); let mut evm = context.build_mainnet().with_inspector(&mut inspector); - // Alternate recorded and missing roots to catch stale state across fuse(). The + // Alternate halted and executed roots to catch stale state across fuse(). The // successful empty-code call also uses its full gas limit without executing opcodes. for (gas_limit, input, kind, halted) in [ (200_000, Bytes::new(), TxKind::Call(recipient), true), (204_600, Bytes::new(), TxKind::Call(recipient), false), - (200_000, hex!("01020304").into(), TxKind::Call(recipient), true), - (200_000, hex!("00").into(), TxKind::Create, true), + (200_000, bytes!("01020304"), TxKind::Call(recipient), true), + (200_000, bytes!("00"), TxKind::Create, true), (204_600, Bytes::new(), TxKind::Call(recipient), false), ] { let res = evm @@ -49,7 +51,7 @@ fn runtime_oog_call_frames_and_reuse() { }) .unwrap(); let result = &res.tx_result.result; - assert_eq!(result.status, !halted); + assert_eq!(result.is_success(), !halted); assert_eq!(result.tx_gas_used(), gas_limit); let (ctx, inspector) = evm.ctx_inspector(); let tx = ctx.tx().envelope(); @@ -86,10 +88,9 @@ fn runtime_oog_call_frames_and_reuse() { index: Some(index as u64), }) .collect(), - // Only recorded roots currently capture the EVM features. - execution_gas_used: (!halted).then(|| U256::from(result.execution_gas_spent())), - state_gas_used: (!halted).then(|| U256::from(result.state_gas_spent())), - gas_refund: (!halted).then(|| U256::from(result.final_refunded())), + execution_gas_used: Some(U256::from(result.execution_gas_spent())), + state_gas_used: Some(U256::from(result.state_gas_spent())), + gas_refund: Some(U256::from(result.final_refunded())), ..Default::default() }; assert_eq!(frame, serde_json::to_value(expected).unwrap()); @@ -100,19 +101,68 @@ fn runtime_oog_call_frames_and_reuse() { } #[test] -fn missing_root_other_halt_is_not_out_of_gas() { - let mut inspector = - DebugInspector::new(GethDebugTracingOptions::call_tracer(CallConfig::default())).unwrap(); - let mut evm = Context::mainnet().build_mainnet().with_inspector(&mut inspector); - let mut res = evm.inspect_tx(TxEnv { gas_limit: 100_000, ..Default::default() }).unwrap(); - res.tx_result.result.status = false; - res.tx_result.result.stop = InstrStop::InvalidFEOpcode; +fn runtime_oog_frame_reaches_four_byte_tracer() { + let caller = address!("1000000000000000000000000000000000000001"); + let recipient = address!("1000000000000000000000000000000000000002"); + let options = serde_json::json!({"tracer": "4byteTracer"}); + let mut inspector = DebugInspector::new(serde_json::from_value(options).unwrap()).unwrap(); + let context = Context::mainnet() + .with_db(InMemoryDB::default()) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) + .modify_db_chained(|db| { + db.insert_account_info( + &caller, + AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, + ); + }); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + // The gas covers the intrinsic cost but not the new account's state gas. + let res = evm + .inspect_tx(TxEnv { + caller, + kind: TxKind::Call(recipient), + value: U256::ONE, + data: hex!("0102030405").into(), + gas_limit: 200_000, + ..Default::default() + }) + .unwrap(); + assert!(!res.tx_result.result.status); let (ctx, inspector) = evm.ctx_inspector(); - inspector.fuse().unwrap(); let tx = ctx.tx().envelope(); let block = *ctx.block(); let trace = inspector.get_result(None, &tx, &block, &res.tx_result, ctx.db_mut()).unwrap(); - let GethTrace::CallTracer(frame) = trace else { panic!("expected call trace") }; - // An unknown missing frame must not be mislabeled as a runtime OOG. - assert_eq!(frame.error, None); + assert_eq!(serde_json::to_value(trace).unwrap(), serde_json::json!({"0x01020304-1": 1})); +} + +#[test] +fn runtime_oog_root_reports_the_execution_budget() { + let caller = address!("1000000000000000000000000000000000000001"); + let recipient = address!("1000000000000000000000000000000000000002"); + let context = Context::mainnet() + .with_db(InMemoryDB::default()) + .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) + .modify_db_chained(|db| { + db.insert_account_info( + &caller, + AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, + ); + }); + let mut evm = context.build_mainnet_with_inspector(TracingInspector::new(Default::default())); + evm.inspect_tx(TxEnv { + caller, + kind: TxKind::Call(recipient), + value: U256::ONE, + gas_limit: 200_000, + ..Default::default() + }) + .unwrap(); + let nodes = evm.inspector.traces().nodes(); + assert_eq!(nodes.len(), 1); + let root = &nodes[0].trace; + assert_eq!(root.status, Some(InstrStop::OutOfGas)); + assert_eq!(root.address, recipient); + // The halt burns the whole budget, which is what the first frame was given. + assert!(root.gas_limit < 200_000); + assert_eq!(root.gas_used, root.gas_limit); } diff --git a/crates/inspectors/tests/it/geth_js.rs b/crates/inspectors/tests/it/geth_js.rs index 3bd78492e..a91d6cd9d 100644 --- a/crates/inspectors/tests/it/geth_js.rs +++ b/crates/inspectors/tests/it/geth_js.rs @@ -4,12 +4,19 @@ mod errors; mod values; use crate::utils::{ - AccountInfo, Bytecode, CacheDB, Context, EmptyDB, ResultAndState, SpecId, TestDbExt, - TransactTo, TxEnv, deploy_contract, + AccountInfo, Bytecode, Context, ResultAndState, SpecId, TestDbExt, TransactTo, TxEnv, + deploy_contract, +}; +use alloy_primitives::{Address, U256, address, bytes, hex, map::HashMap}; +use alloy_rpc_types_trace::geth::{ + GethDebugBuiltInTracerType, GethDebugTracerType, GethDebugTracingOptions, GethTrace, + mux::MuxConfig, +}; +use evm2::evm::InMemoryDB; +use evm2_inspectors::tracing::{ + DebugInspector, MuxInspector, + js::{JsInspector, JsInspectorInterrupt}, }; -use alloy_primitives::{Address, U256, address, hex}; -use alloy_rpc_types_trace::geth::{GethDebugTracingOptions, GethTrace}; -use evm2_inspectors::tracing::{DebugInspector, js::JsInspector}; use serde_json::json; fn js_result( @@ -35,10 +42,8 @@ fn test_geth_jstracer_bigint_compatibility() { } }"#; let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); - let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) - .build_mainnet() - .with_inspector(insp); + let mut evm = + Context::mainnet().with_db(InMemoryDB::default()).build_mainnet().with_inspector(insp); let res = evm .inspect_tx(TxEnv { caller: Address::ZERO, @@ -79,7 +84,7 @@ fn test_geth_jstracer_revert() { let deployer = Address::ZERO; let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) .build_mainnet(); @@ -99,7 +104,7 @@ fn test_geth_jstracer_revert() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("c2985578").into(), // call foo + data: bytes!("c2985578"), // call foo nonce: 1, ..Default::default() }) @@ -120,7 +125,7 @@ fn test_geth_jstracer_revert() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("febb0f7e").into(), // call bar + data: bytes!("febb0f7e"), // call bar nonce: 1, ..Default::default() }) @@ -167,7 +172,7 @@ fn test_geth_jstracer_proxy_contract() { let deployer = address!("f077b491b355e64048ce21e3a6fc4751eeea77fa"); let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) .build_mainnet(); @@ -184,9 +189,8 @@ fn test_geth_jstracer_proxy_contract() { evm.ctx().db_mut().load_account(deployer).info.balance = U256::from(1_000_000); // Set input data for ProxyFactory.transfer(address) - let mut input_data = hex!("1a695230").to_vec(); // keccak256("transfer(address)")[:4] - input_data.extend_from_slice(&[0u8; 12]); // Pad with zeros - input_data.extend_from_slice(token_addr.as_slice()); + // keccak256("transfer(address)")[:4], zero padding, then the address. + let input_data = [&hex!("1a695230")[..], &[0u8; 12][..], token_addr.as_slice()].concat(); println!("token {token_addr:?} proxy: {proxy_addr:?}"); let code = r#" @@ -232,7 +236,7 @@ fn test_geth_jstracer_proxy_contract() { #[test] fn test_geth_jstracer_records_call_step_before_nested_execution() { let child = address!("0000000000000000000000000000000000001234"); - let parent = address!("0000000000000000000000000000000000000022"); + let parent = Address::with_last_byte(0x22); let child_code = Bytecode::new_legacy([0x60, 1, 0x60, 0x40, 0x52, 0x60, 1, 0x60, 1, 0x01, 0x00].into()); @@ -244,17 +248,16 @@ fn test_geth_jstracer_records_call_step_before_nested_execution() { .into(), ); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &child, - AccountInfo { code: Some(child_code), ..Default::default() }, - ); - db.insert_account_info( - &parent, - AccountInfo { code: Some(parent_code), ..Default::default() }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &child, + AccountInfo { code: Some(child_code), ..Default::default() }, + ); + db.insert_account_info( + &parent, + AccountInfo { code: Some(parent_code), ..Default::default() }, + ); + }); let code = r#" { @@ -325,18 +328,17 @@ fn test_geth_jstracer_call_step_cost_and_depth() { "5f5f5f5f600161111161fffff15000" ); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - for (address, code) in [ - (leaf, &leaf_code[..]), - (mid, &mid_code[..]), - (reverter, &reverter_code[..]), - (main, &main_code[..]), - ] { - let code = Some(Bytecode::new_legacy(code.to_vec().into())); - db.insert_account_info(&address, AccountInfo { code, ..Default::default() }); - } - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + for (address, code) in [ + (leaf, &leaf_code[..]), + (mid, &mid_code[..]), + (reverter, &reverter_code[..]), + (main, &main_code[..]), + ] { + let code = Some(Bytecode::new_legacy(code.to_vec().into())); + db.insert_account_info(&address, AccountInfo { code, ..Default::default() }); + } + }); let code = r#" { @@ -431,20 +433,19 @@ fn test_geth_debug_inspector_jstracer() { let caller = address!("1000000000000000000000000000000000000002"); let coinbase = address!("1000000000000000000000000000000000000003"); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &caller, - AccountInfo::default().with_balance(U256::from(1_000_000_000u64)), - ); - db.insert_account_info( - &account, - AccountInfo { - code: Some(Bytecode::new_legacy(hex!("6001600100").into())), - ..Default::default() - }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &caller, + AccountInfo::default().with_balance(U256::from(1_000_000_000u64)), + ); + db.insert_account_info( + &account, + AccountInfo { + code: Some(Bytecode::new_legacy(bytes!("6001600100"))), + ..Default::default() + }, + ); + }); let context = context .modify_block_chained(|block| { block.number = U256::from(42); @@ -471,10 +472,9 @@ fn test_geth_debug_inspector_jstracer() { exists: db.exists(ctx.to), codeLength: db.getCode(ctx.to).length, gas: ctx.gas, - gasPrice: ctx.gasPrice, + gasPrice: ctx.gasPrice.toString(), value: ctx.value.toString(), block: ctx.block, - time: ctx.time, coinbase: toHex(ctx.coinbase), error: !!ctx.error }; @@ -500,10 +500,130 @@ fn test_geth_debug_inspector_jstracer() { assert_eq!(result["exists"], json!(true)); assert_eq!(result["codeLength"], json!(5)); assert_eq!(result["gas"], json!(1_000_000)); - assert_eq!(result["gasPrice"], json!(17)); + assert_eq!(result["gasPrice"], json!("17")); assert_eq!(result["value"], json!("3")); assert_eq!(result["block"], json!(42)); - assert_eq!(result["time"], json!("1234")); assert_eq!(result["coinbase"], json!(coinbase)); assert_eq!(result["error"], json!(false)); } + +#[test] +fn test_geth_jstracer_get_code_does_not_load_the_account() { + let target = address!("1000000000000000000000000000000000000001"); + let observed = address!("1000000000000000000000000000000000000002"); + let caller = address!("1000000000000000000000000000000000000003"); + + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &caller, + AccountInfo::default().with_balance(U256::from(1_000_000_000u64)), + ); + db.insert_account_info( + &target, + AccountInfo { code: Some(Bytecode::new_legacy(bytes!("00"))), ..Default::default() }, + ); + db.insert_account_info( + &observed, + AccountInfo { + code: Some(Bytecode::new_legacy(bytes!("6001600100"))), + ..Default::default() + }, + ); + }); + let context = context.modify_tx_chained(|tx| { + tx.gas_limit = 1_000_000; + tx.gas_price = 1; + tx.kind = TransactTo::Call(target); + }); + + let code = r#" +{ + codeLength: -1, + step: function(log, db) { + this.codeLength = db.getCode(toAddress("1000000000000000000000000000000000000002")).length; + }, + fault: function() {}, + result: function() { return this.codeLength; } +}"#; + let mut inspector = DebugInspector::new(GethDebugTracingOptions::js_tracer(code)).unwrap(); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + + let tx = evm.ctx().tx().clone(); + let res = evm.inspect_tx(TxEnv { caller, ..tx }).unwrap(); + assert!(res.result.is_success()); + + let (context, inspector) = evm.ctx_inspector(); + let tx_env = context.tx().envelope(); + let block_env = *context.block(); + let trace = + inspector.get_result(None, &tx_env, &block_env, &res.tx_result, context.db_mut()).unwrap(); + let GethTrace::JS(result) = trace else { panic!("expected JS trace") }; + assert_eq!(result, json!(5)); + + // The transaction loads its caller and target. The code read does not add `observed`. + let pending = &res.tx_result.pending_state; + assert!(pending.account_changes_for(&observed).is_none()); + assert!(pending.account_changes_for(&target).is_some()); +} + +#[test] +fn mux_runs_a_js_tracer_next_to_a_built_in_tracer() { + let code = "{ steps: 0, step: function() { this.steps++; }, fault: function() {}, \ + result: function() { return this.steps; } }"; + let contract = Address::with_last_byte(0x42); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &contract, + AccountInfo::default().with_code(Bytecode::new_raw(bytes!("5f5f00"))), + ); + let js_key = GethDebugTracerType::JsTracer(code.to_string()); + let call_key = GethDebugTracerType::BuiltInTracer(GethDebugBuiltInTracerType::CallTracer); + let config = MuxConfig(HashMap::from_iter([(js_key.clone(), None), (call_key.clone(), None)])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let (ctx, inspector) = evm.ctx_inspector(); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + let frame = + inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).unwrap(); + + let GethTrace::JS(steps) = &frame.0[&js_key] else { panic!("expected a JS result") }; + assert_eq!(steps.as_u64(), Some(3)); + assert!(frame.0[&call_key].is_call()); +} + +#[test] +fn mux_interrupts_its_js_tracers() { + let code = "{ fault: function() {}, result: function() { return 1; } }"; + let mut db = InMemoryDB::default(); + db.insert_account_info(&Address::ZERO, AccountInfo::default()); + let config = + MuxConfig(HashMap::from_iter([(GethDebugTracerType::JsTracer(code.into()), None)])); + let mut evm = Context::mainnet() + .with_db(db) + .build_mainnet_with_inspector(MuxInspector::try_from_config(config).unwrap()); + let res = evm + .inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(Address::ZERO), + ..Default::default() + }) + .unwrap(); + + let interrupt = JsInspectorInterrupt::new(); + let (ctx, inspector) = evm.ctx_inspector(); + inspector.set_js_interrupt(&interrupt); + let (tx, block) = (ctx.tx().envelope(), *ctx.block()); + assert!(inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).is_ok()); + + interrupt.interrupt(); + assert!(inspector.try_into_mux_frame(&res.tx_result, &tx, &block, None, ctx.db_mut()).is_err()); +} diff --git a/crates/inspectors/tests/it/geth_js/errors.rs b/crates/inspectors/tests/it/geth_js/errors.rs index ed949cad9..16697b1e0 100644 --- a/crates/inspectors/tests/it/geth_js/errors.rs +++ b/crates/inspectors/tests/it/geth_js/errors.rs @@ -21,7 +21,7 @@ fn execute_with_child( child: &[u8], gas_limit: u64, ) -> (Result, ResultAndState) { - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); for (address, code, balance) in [ (Address::ZERO, &[][..], U256::from(1e18)), (TARGET, code, U256::from(5)), @@ -94,10 +94,11 @@ fn faults_follow_steps_and_preserve_instruction_metadata() { assert!(!result.result.is_success()); assert_eq!(actual.unwrap(), expected); } - let fault_only = "{e:[],fault:function(l){this.e.push([l.getPC(),l.op.toString(),l.getCost()])},result:function(){return this.e}}"; + // Geth fills the log in its step hook, so a tracer without `step` sees an empty one. + let fault_only = "{e:[],fault:function(l){this.e.push([l.getPC(),l.op.toString(),l.getCost(),l.getDepth(),l.stack.length(),l.memory.length(),l.getError()])},result:function(){return this.e}}"; assert_eq!( - execute(fault_only, &hex!("60ff5600"), 1_000_000).0.unwrap(), - json!([[2, "JUMP", 8]]) + execute(fault_only, &hex!("600160005260ff5600"), 1_000_000).0.unwrap(), + json!([[0, "STOP", 0, 0, 0, 0, "invalid jump destination"]]) ); } @@ -124,9 +125,8 @@ fn oversized_initcode_is_reported_by_step() { #[test] fn static_write_errors_follow_the_opcode_validation_stage() { - let mut parent = hex!("600060006000600073").to_vec(); - parent.extend_from_slice(CHILD.as_slice()); - parent.extend_from_slice(&hex!("61fffffa00")); + let parent = + [&hex!("600060006000600073")[..], CHILD.as_slice(), &hex!("61fffffa00")[..]].concat(); for (child, opcode, callback) in [ (&hex!("6000600055")[..], "SSTORE", "step"), (&hex!("600060006000f0")[..], "CREATE", "step"), @@ -142,8 +142,11 @@ fn static_write_errors_follow_the_opcode_validation_stage() { assert!(result.result.is_success()); assert_eq!( output.unwrap(), - if has_step || callback == "fault" { + if has_step { json!([[callback, opcode, "write protection"]]) + } else if callback == "fault" { + // A tracer without `step` sees geth's empty log. + json!([[callback, "STOP", "write protection"]]) } else { json!([]) }, @@ -155,9 +158,12 @@ fn static_write_errors_follow_the_opcode_validation_stage() { #[test] fn hook_errors_preserve_execution() { - let mut call = hex!("6000600060006000600073").to_vec(); - call.extend_from_slice(CHILD.as_slice()); - call.extend_from_slice(&hex!("61fffff150600160015500")); + let call = [ + &hex!("6000600060006000600073")[..], + CHILD.as_slice(), + &hex!("61fffff150600160015500")[..], + ] + .concat(); let baseline = "{fault:function(){},result:function(){return 42}}"; for hook in ["step", "enter", "exit", "fault"] { let child = if hook == "fault" { &hex!("60ff56")[..] } else { &hex!("600160005500")[..] }; @@ -178,9 +184,8 @@ fn hook_errors_preserve_execution() { #[test] fn call_step_errors_preserve_execution() { - let mut call = hex!("6000600060006000600073").to_vec(); - call.extend_from_slice(CHILD.as_slice()); - call.extend_from_slice(&hex!("61fffff100")); + let call = + [&hex!("6000600060006000600073")[..], CHILD.as_slice(), &hex!("61fffff100")[..]].concat(); let baseline = "{fault:function(){},result:function(){return 42}}"; let script = "{fault:function(){},result:function(){return 42},step:function(l){var o=l.op.toString();if(o=='CALL'||o=='CREATE')throw new Error('boom')}}"; // The step ends before the child runs, which must still run with the gas it was given. diff --git a/crates/inspectors/tests/it/geth_js/values.rs b/crates/inspectors/tests/it/geth_js/values.rs index b35d4cde2..af1a0f8e9 100644 --- a/crates/inspectors/tests/it/geth_js/values.rs +++ b/crates/inspectors/tests/it/geth_js/values.rs @@ -2,7 +2,7 @@ use super::*; -fn trace_tx(code: &str, db: CacheDB, tx: TxEnv) -> serde_json::Value { +fn trace_tx(code: &str, db: InMemoryDB, tx: TxEnv) -> serde_json::Value { let insp = JsInspector::new(code.to_string(), serde_json::Value::Null).unwrap(); let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = SpecId::CANCUN) @@ -13,8 +13,8 @@ fn trace_tx(code: &str, db: CacheDB, tx: TxEnv) -> serde_json::Value { js_result(inspector, &res, ctx) } -fn funded_db() -> CacheDB { - let mut db = CacheDB::new(EmptyDB::default()); +fn funded_db() -> InMemoryDB { + let mut db = InMemoryDB::default(); db.insert_account_info( &Address::ZERO, AccountInfo { balance: U256::from(1e18), ..Default::default() }, diff --git a/crates/inspectors/tests/it/inputs.rs b/crates/inspectors/tests/it/inputs.rs index 9f791bb09..f3097ef74 100644 --- a/crates/inspectors/tests/it/inputs.rs +++ b/crates/inspectors/tests/it/inputs.rs @@ -1,16 +1,18 @@ //! Recording inputs is optional and does not affect execution. -use crate::utils::{AccountInfo, Bytecode, CacheDB, EmptyDB, SpecId, TransactTo, TxEnv}; -use alloy_primitives::{Address, Bytes, U256, hex}; +use crate::utils::{AccountInfo, Bytecode, SpecId, TransactTo, TxEnv}; +use alloy_primitives::{Address, Bytes, U256, bytes, hex}; use evm2::{ - BaseEvmTypes, Evm, ExecutionConfig, Inspector, Precompiles, Version, - constants::CALL_DEPTH_LIMIT, ethereum::ethereum_tx_registry, + BaseEvmTypes, Evm, Inspector, Precompiles, constants::CALL_DEPTH_LIMIT, + ethereum::ethereum_tx_registry, evm::InMemoryDB, interpreter::opcode::op, }; use evm2_inspectors::{ access_list::AccessListInspector, opcode::OpcodeGasInspector, otterscan::InternalOperationsInspector, - tracing::{TraceLimitBehavior, TraceLimits, TracingInspector, TracingInspectorConfig}, + tracing::{ + TraceLimitBehavior, TraceLimits, TracingInspector, TracingInspectorConfig, types::CallTrace, + }, transfer::TransferInspector, }; @@ -41,7 +43,7 @@ fn inspect_with_limits( limits: TraceLimits, ) -> (TracingInspector, bool) { let target = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (address, code) in [(target, code), (Address::with_last_byte(0x43), &hex!("00")[..])] { db.insert_account_info( &address, @@ -193,7 +195,7 @@ fn byte_budget_counts_memory_snapshots() { fn byte_budget_covers_immediates_and_memory_deltas() { let code = hex!("60015f5200"); for (config, limit) in [ - (TracingInspectorConfig::none().steps().set_immediate_bytes(true), 0), + (TracingInspectorConfig::none().steps().record_immediate_bytes(), 0), (TracingInspectorConfig::none().steps().set_step_deltas(true), 31), ] { let (inspector, completed) = inspect_with_limits( @@ -213,7 +215,7 @@ fn byte_budget_can_halt_from_a_step() { let (inspector, completed) = inspect_with_limits( &hex!("600100"), Bytes::new(), - TracingInspectorConfig::none().steps().set_immediate_bytes(true), + TracingInspectorConfig::none().steps().record_immediate_bytes(), TraceLimits::default() .set_max_recorded_bytes(Some(0)) .set_behavior(TraceLimitBehavior::Halt), @@ -223,24 +225,58 @@ fn byte_budget_can_halt_from_a_step() { } #[test] -fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { +fn byte_budget_halt_before_sload_records_no_storage_access() { + // The memory snapshot of the SLOAD step exceeds the budget, which halts before the SLOAD runs. + let (inspector, completed) = inspect_with_limits( + &hex!("600060015f525400"), + Bytes::new(), + TracingInspectorConfig::none().steps().memory_snapshots().set_state_diffs(true), + TraceLimits::default() + .set_max_recorded_bytes(Some(31)) + .set_behavior(TraceLimitBehavior::Halt), + ); + assert!(!completed); + let step = inspector.traces().nodes()[0].trace.steps.last().unwrap(); + assert_eq!(step.op.get(), op::SLOAD); + assert!(step.storage_change.is_none()); +} + +#[test] +fn byte_budget_halt_before_sstore_reports_no_store_delta() { + // The memory snapshot of the SSTORE step exceeds the budget, which halts before it runs. + let (inspector, completed) = inspect_with_limits( + &hex!("6001600060015f525500"), + Bytes::new(), + TracingInspectorConfig::none().steps().memory_snapshots().set_step_deltas(true), + TraceLimits::default() + .set_max_recorded_bytes(Some(32)) + .set_behavior(TraceLimitBehavior::Halt), + ); + assert!(!completed); + let trace = &inspector.traces().nodes()[0].trace; + assert_eq!(trace.steps.last().unwrap().op.get(), op::SSTORE); + assert!(trace.step_deltas.iter().all(|delta| delta.store.is_none())); +} + +#[test] +fn nested_roots_match_revm_inspectors() { let caller = Address::repeat_byte(0xaa); let child = Address::with_last_byte(0x43); // Send 1 wei to a child that executes STOP. - let code = Bytes::from_static(&hex!("5f5f5f5f600160435af100")); + let code = bytes!("5f5f5f5f600160435af100"); for initial_call_depth in [0, 7, u16::MAX - CALL_DEPTH_LIMIT - 1] { for kind in [TransactTo::Call(Address::with_last_byte(0x42)), TransactTo::Create] { - let target = kind.to().copied().unwrap_or_else(|| caller.create(0)); + let target = kind.into_to().unwrap_or_else(|| caller.create(0)); let data = if kind.is_create() { code.clone() } else { Bytes::from_static(&[1, 2]) }; let run = |inspector: &mut dyn Inspector| { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo::default().with_balance(U256::from(100)), ); db.insert_account_info( &child, - AccountInfo::default().with_code(Bytecode::new_raw(hex!("00").into())), + AccountInfo::default().with_code(Bytecode::new_raw(bytes!("00"))), ); if !kind.is_create() { db.insert_account_info( @@ -248,16 +284,14 @@ fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { AccountInfo::default().with_code(Bytecode::new_raw(code.clone())), ); } - let mut version = Version::new(SpecId::CANCUN); - version.initial_call_depth = initial_call_depth; - let mut evm = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(SpecId::CANCUN, version), + let mut evm = Evm::::new( SpecId::CANCUN, Default::default(), ethereum_tx_registry(SpecId::CANCUN), db, Precompiles::base(SpecId::CANCUN), ); + evm.set_initial_call_depth(initial_call_depth); evm.set_inspector(inspector); let tx = TxEnv { caller, @@ -267,7 +301,7 @@ fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { data: data.clone(), ..Default::default() }; - assert!(evm.transact(&tx.envelope()).unwrap().result().status); + assert!(evm.transact(&tx.envelope()).unwrap().result().is_success()); }; let mut tracer = TracingInspector::new( TracingInspectorConfig::default_geth().set_record_inputs(true), @@ -275,35 +309,51 @@ fn nested_roots_preserve_trace_depth_and_top_level_inspector_semantics() { .with_limits(TraceLimits::default().set_max_recorded_bytes(Some(0))); run(&mut tracer); let nodes = tracer.traces().nodes(); - assert_eq!(nodes.len(), 2); - assert_eq!(nodes[0].parent, None); - assert_eq!(nodes[0].trace.depth, usize::from(initial_call_depth)); - assert_eq!(nodes[0].trace.data, data); - assert_eq!(nodes[0].children, [1]); - assert_eq!(nodes[1].parent, Some(0)); - assert_eq!(nodes[1].trace.depth, usize::from(initial_call_depth) + 1); + // Like revm-inspectors, a top-level call above depth zero hangs under an empty root. + let root = usize::from(initial_call_depth > 0); + assert_eq!(nodes.len(), 2 + root); + if root == 1 { + assert_eq!(nodes[0].trace, CallTrace::default()); + assert_eq!(nodes[0].children, [1]); + } + assert_eq!(nodes[root].parent, root.checked_sub(1)); + assert_eq!(nodes[root].trace.depth, usize::from(initial_call_depth)); + assert_eq!(nodes[root].trace.data, data); + assert_eq!(nodes[root].children, [root + 1]); + assert_eq!(nodes[root + 1].parent, Some(root)); + assert_eq!(nodes[root + 1].trace.depth, usize::from(initial_call_depth) + 1); let geth = tracer.geth_builder().geth_traces(0, Bytes::new(), Default::default()); - assert_eq!(geth.struct_logs[0].depth, u64::from(initial_call_depth) + 1); - assert!( - geth.struct_logs.iter().any(|log| log.depth == u64::from(initial_call_depth) + 2) - ); + if root == 1 { + // As with revm-inspectors, struct logs only follow the root's own steps. + assert!(geth.struct_logs.is_empty()); + } else { + assert_eq!(geth.struct_logs[0].depth, u64::from(initial_call_depth) + 1); + assert!( + geth.struct_logs + .iter() + .any(|log| log.depth == u64::from(initial_call_depth) + 2) + ); + } + // Like revm-inspectors, only a call at depth zero counts as top-level. let mut access = AccessListInspector::default(); run(&mut access); - assert!(access.excluded().contains(&caller)); - assert!(access.excluded().contains(&target)); + assert_eq!(access.excluded().contains(&caller), root == 0); + assert_eq!(access.excluded().contains(&target), root == 0); assert!(access.touched_slots().contains_key(&child)); let mut operations = InternalOperationsInspector::default(); run(&mut operations); - assert_eq!(operations.operations().len(), 1); - assert_eq!(operations.operations()[0].from, target); - assert_eq!(operations.operations()[0].to, child); + let operations = &operations.operations()[root..]; + assert_eq!(operations.len(), 1); + assert_eq!(operations[0].from, target); + assert_eq!(operations[0].to, child); let mut transfers = TransferInspector::internal_only(); run(&mut transfers); - assert_eq!(transfers.transfers().len(), 1); - assert_eq!(transfers.transfers()[0].from, target); - assert_eq!(transfers.transfers()[0].to, child); - let mut opcodes = OpcodeGasInspector::default(); + let transfers = &transfers.transfers()[root..]; + assert_eq!(transfers.len(), 1); + assert_eq!(transfers[0].from, target); + assert_eq!(transfers[0].to, child); + let mut opcodes = OpcodeGasInspector::new(); run(&mut opcodes); assert!( opcodes diff --git a/crates/inspectors/tests/it/main.rs b/crates/inspectors/tests/it/main.rs index ca161642c..254e7d0d1 100644 --- a/crates/inspectors/tests/it/main.rs +++ b/crates/inspectors/tests/it/main.rs @@ -25,6 +25,8 @@ mod test_native_bigint; #[cfg(feature = "std")] mod transfer; #[cfg(feature = "std")] +mod unlisted_precompiles; +#[cfg(feature = "std")] mod writer; mod compat; diff --git a/crates/inspectors/tests/it/opcode.rs b/crates/inspectors/tests/it/opcode.rs index 035e8529f..3ae1b8f01 100644 --- a/crates/inspectors/tests/it/opcode.rs +++ b/crates/inspectors/tests/it/opcode.rs @@ -1,14 +1,16 @@ -use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, TransactTo, TxEnv}; +use crate::utils::{AccountInfo, Bytecode, Context, TransactTo, TxEnv}; use alloy_primitives::{Address, address}; -use evm2::interpreter::opcode::{OpCode, op}; +use evm2::{ + evm::InMemoryDB, + interpreter::opcode::{OpCode, op}, +}; use evm2_inspectors::opcode::OpcodeGasInspector; #[test] fn opcode_gas_records_parent_call_after_child_execution() { let child = address!("0000000000000000000000000000000000001234"); - let parent = address!("0000000000000000000000000000000000000022"); + let parent = Address::with_last_byte(0x22); - let child_code = Bytecode::new_legacy([op::PUSH1, 1, op::PUSH1, 1, op::ADD, op::STOP].into()); let parent_code = Bytecode::new_legacy( [ op::PUSH1, @@ -33,51 +35,61 @@ fn opcode_gas_records_parent_call_after_child_execution() { .into(), ); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { + for (child_code, child_steps, adds) in [ + (vec![op::PUSH1, 1, op::PUSH1, 1, op::ADD, op::STOP], 4, 1), + (vec![op::INVALID], 1, 0), + (vec![0x0c], 0, 0), + ] { + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { db.insert_account_info( &child, - AccountInfo { code: Some(child_code), ..Default::default() }, + AccountInfo { + code: Some(Bytecode::new_legacy(child_code.into())), + ..Default::default() + }, ); db.insert_account_info( &parent, - AccountInfo { code: Some(parent_code), ..Default::default() }, + AccountInfo { code: Some(parent_code.clone()), ..Default::default() }, ); }); - let mut inspector = OpcodeGasInspector::new(); - let mut evm = context.build_mainnet().with_inspector(&mut inspector); - let res = evm - .inspect_tx(TxEnv { - caller: Address::ZERO, - gas_limit: 1_000_000, - kind: TransactTo::Call(parent), - ..Default::default() - }) - .unwrap(); - assert!(res.result.is_success(), "{res:#?}"); + let mut inspector = OpcodeGasInspector::new(); + let mut evm = context.build_mainnet().with_inspector(&mut inspector); + let res = evm + .inspect_tx(TxEnv { + caller: Address::ZERO, + gas_limit: 1_000_000, + kind: TransactTo::Call(parent), + ..Default::default() + }) + .unwrap(); + assert!(res.result.is_success(), "{res:#?}"); - let call = OpCode::new(op::CALL).unwrap(); - let add = OpCode::new(op::ADD).unwrap(); - assert_eq!(evm.inspector().opcode_counts().get(&call), Some(&1)); - assert_eq!(evm.inspector().opcode_counts().get(&add), Some(&1)); - assert!(evm.inspector().opcode_gas().get(&call).copied().unwrap_or_default() > 0); + let call = OpCode::new(op::CALL).unwrap(); + let add = OpCode::new(op::ADD).unwrap(); + assert_eq!(evm.inspector().opcode_counts().get(&call), Some(&1)); + assert_eq!(evm.inspector().opcode_counts().get(&add).copied().unwrap_or_default(), adds); + // Seven PUSHes, CALL and STOP in the parent; undefined bytes are not counted. + assert_eq!(evm.inspector().opcode_counts().values().sum::(), 9 + child_steps); + // The cold CALL costs 2600 regardless of how much gas its child consumes. + assert_eq!(evm.inspector().opcode_gas().get(&call), Some(&2600)); + } } #[test] fn opcode_gas_excludes_uncharged_sentry_gas() { - let target = address!("0000000000000000000000000000000000000022"); + let target = Address::with_last_byte(0x22); let code = Bytecode::new_legacy([op::PUSH1, 1, op::PUSH1, 0, op::SSTORE, op::STOP].into()); for (gas_limit, expected_cost, success) in [(23_306, 0, false), (24_000, 2_994, false), (100_000, 22_100, true)] { - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info( - &target, - AccountInfo { code: Some(code.clone()), ..Default::default() }, - ); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info( + &target, + AccountInfo { code: Some(code.clone()), ..Default::default() }, + ); + }); let mut inspector = OpcodeGasInspector::new(); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let res = evm @@ -97,7 +109,7 @@ fn opcode_gas_excludes_uncharged_sentry_gas() { #[test] fn opcode_gas_preserves_cold_call_out_of_gas() { - let target = address!("0000000000000000000000000000000000000022"); + let target = Address::with_last_byte(0x22); let code = Bytecode::new_legacy( [ op::PUSH1, @@ -120,10 +132,9 @@ fn opcode_gas_preserves_cold_call_out_of_gas() { ] .into(), ); - let context = - Context::mainnet().with_db(CacheDB::::default()).modify_db_chained(|db| { - db.insert_account_info(&target, AccountInfo { code: Some(code), ..Default::default() }); - }); + let context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info(&target, AccountInfo { code: Some(code), ..Default::default() }); + }); let mut inspector = OpcodeGasInspector::new(); let mut evm = context.build_mainnet().with_inspector(&mut inspector); let res = evm diff --git a/crates/inspectors/tests/it/otterscan.rs b/crates/inspectors/tests/it/otterscan.rs index 7beb36e8a..64f749dc6 100644 --- a/crates/inspectors/tests/it/otterscan.rs +++ b/crates/inspectors/tests/it/otterscan.rs @@ -1,16 +1,15 @@ //! Otterscan internal operations tests. -use crate::utils::{ - AccountInfo, Bytecode, CacheDB, Context, EmptyDB, ExecutionResult, SpecId, TxEnv, -}; -use alloy_primitives::{Address, Bytes, TxKind, U256, hex}; +use crate::utils::{AccountInfo, Bytecode, Context, ExecutionResult, SpecId, TxEnv}; +use alloy_primitives::{Address, Bytes, TxKind, U256, bytes}; use alloy_rpc_types_trace::otterscan::{InternalOperation, OperationType}; +use evm2::evm::InMemoryDB; use evm2_inspectors::otterscan::InternalOperationsInspector; const CONTRACT: Address = Address::repeat_byte(0x11); fn execute(code: Bytes, spec: SpecId, kind: TxKind) -> (ExecutionResult, Vec) { - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &Address::ZERO, AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, @@ -42,7 +41,7 @@ fn execute(code: Bytes, spec: SpecId, kind: TxKind) -> (ExecutionResult, Vec::default()).modify_db_chained(|db| { - db.insert_account_info(&deployer, AccountInfo { balance: value, ..Default::default() }); - }); + let mut context = Context::mainnet().with_db(InMemoryDB::default()).modify_db_chained(|db| { + db.insert_account_info(&deployer, AccountInfo { balance: value, ..Default::default() }); + }); context.modify_tx(|tx| tx.value = value); let mut evm = context.build_mainnet(); @@ -62,7 +62,7 @@ fn test_parity_selfdestruct(spec_id: SpecId) { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("43d726d6").into(), + data: bytes!("43d726d6"), nonce: 1, ..Default::default() }) @@ -118,7 +118,7 @@ fn test_parity_constructor_selfdestruct() { let deployer = Address::ZERO; let mut evm = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_tx_chained(|tx| tx.caller = deployer) .build_mainnet_with_inspector(TracingInspector::new( TracingInspectorConfig::default_parity(), @@ -137,7 +137,7 @@ fn test_parity_constructor_selfdestruct() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Call(addr), - data: hex!("43d726d6").into(), + data: bytes!("43d726d6"), nonce: 1, ..Default::default() } @@ -179,7 +179,7 @@ fn test_parity_call_selfdestruct() { let value = U256::from(69); let mut evm = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_db_chained(|db| { db.insert_account_info(&deployer, AccountInfo { balance: value, ..Default::default() }); }) @@ -202,7 +202,7 @@ fn test_parity_call_selfdestruct() { caller, gas_limit: 10000000, kind: TransactTo::Call(to), - data: input.to_vec().into(), + data: input.into(), nonce: 0, ..Default::default() }) @@ -249,10 +249,10 @@ fn test_parity_call_selfdestruct_create() { "0x60806040526040516102283803806102288339818101604052810190610025919061011c565b335f5f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055503481604051610072906100b2565b61007c9190610156565b6040518091039082f0905080158015610097573d5f5f3e3d5ffd5b50508073ffffffffffffffffffffffffffffffffffffffff16ff5b60b88061017083390190565b5f5ffd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6100eb826100c2565b9050919050565b6100fb816100e1565b8114610105575f5ffd5b50565b5f81519050610116816100f2565b92915050565b5f60208284031215610131576101306100be565b5b5f61013e84828501610108565b91505092915050565b610150816100e1565b82525050565b5f6020820190506101695f830184610147565b9291505056fe608060405260405160b838038060b88339818101604052810190602191906091565b8073ffffffffffffffffffffffffffffffffffffffff16ff5b5f5ffd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f606582603e565b9050919050565b607381605d565b8114607c575f5ffd5b50565b5f81519050608b81606c565b92915050565b5f6020828403121560a35760a2603a565b5b5f60ae84828501607f565b9150509291505056fe000000000000000000000000f2719d5f7ec66c38abd64bc5efda6876590b3b30" ); - let value = U256::from(1); + let value = U256::ONE; let evm = Context::mainnet() - .with_db(CacheDB::::default()) + .with_db(InMemoryDB::default()) .modify_db_chained(|db| { db.insert_account_info( &caller, @@ -273,9 +273,9 @@ fn test_parity_call_selfdestruct_create() { caller, gas_limit: 10000000, kind: TransactTo::Create, - data: code.to_vec().into(), + data: code.into(), nonce: 24, - value: U256::from(1), + value: U256::ONE, ..Default::default() }) .unwrap(); @@ -300,7 +300,7 @@ fn test_parity_call_selfdestruct_create() { let last = traces.len() - 1; let second_sd = Action::Selfdestruct(SelfdestructAction { address: address!("0x076af20059617d9bf2e49f4f19bd3f48d582dcef"), - balance: U256::from(0), + balance: U256::ZERO, refund_address: address!("0xf2719d5f7ec66c38abd64bc5efda6876590b3b30"), }); @@ -309,7 +309,7 @@ fn test_parity_call_selfdestruct_create() { let first_sd = Action::Selfdestruct(SelfdestructAction { address: address!("0x9f2e7bebe157dfd9f1322bd252f1a5af37629687"), - balance: U256::from(1), + balance: U256::ONE, refund_address: address!("0xf2719d5f7ec66c38abd64bc5efda6876590b3b30"), }); @@ -324,7 +324,7 @@ fn test_parity_statediff_blob_commit() { let caller = address!("283b5b7d75e3e6b84b8e2161e8a468d733bbbe8d"); let to = address!("15dd773dad3f630773a0e771e9b221f4c8b9b939"); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(u64::MAX), ..Default::default() }, @@ -350,11 +350,9 @@ fn test_parity_statediff_blob_commit() { gas_limit: 1000000, kind: TransactTo::Call(to), gas_price: 150, - blob_hashes: vec![ + blob_hashes: vec![b256!( "0x01af2fd94f17364bc8ef371c4c90c3a33855ff972d10b9c03d0445b3fca063ea" - .parse() - .unwrap(), - ], + )], max_fee_per_blob_gas: 1000000000, ..Default::default() }) @@ -398,7 +396,7 @@ fn test_parity_delegatecall_selfdestruct() { let deployer = address!("341348115259a8bf69f1f50101c227fced83bac6"); - let mut evm = Context::mainnet().with_db(CacheDB::::default()).build_mainnet(); + let mut evm = Context::mainnet().with_db(InMemoryDB::default()).build_mainnet(); // Deploy DelegateCall contract let delegate_addr = @@ -412,9 +410,8 @@ fn test_parity_delegatecall_selfdestruct() { .unwrap(); // Prepare the input data for the close(address) function call - let mut input_data = hex!("c74073a1").to_vec(); // keccak256("close(address)")[:4] - input_data.extend_from_slice(&[0u8; 12]); // Pad with zeros - input_data.extend_from_slice(target_addr.as_slice()); + // keccak256("close(address)")[:4], zero padding, then the address. + let input_data = [&hex!("c74073a1")[..], &[0u8; 12][..], target_addr.as_slice()].concat(); // Call DelegateCall contract with SelfDestructTarget address let mut evm = @@ -466,7 +463,7 @@ fn test_parity_delegatecall_selfdestruct() { assert_eq!(action2.refund_address, deployer); } -fn trace_vm_code(code: &[u8], child: &[u8]) -> alloy_rpc_types_trace::parity::VmTrace { +fn trace_vm_code(code: &[u8], child: &[u8]) -> VmTrace { let config = TracingInspectorConfig::from_parity_config(&HashSet::from_iter([TraceType::VmTrace])); inspect_code(code, child, SpecId::default(), config).into_parity_builder().vm_trace() @@ -478,26 +475,35 @@ fn inspect_code( spec: SpecId, config: TracingInspectorConfig, ) -> TracingInspector { - use crate::utils::Bytecode; + inspect_funded_code(code, child, spec, config, 0, 1_000_000) +} + +/// Runs `code` at `0x42` with `balance` and a transaction gas limit of `gas_limit`. +fn inspect_funded_code( + code: &[u8], + child: &[u8], + spec: SpecId, + config: TracingInspectorConfig, + balance: u64, + gas_limit: u64, +) -> TracingInspector { let target = Address::with_last_byte(0x42); let child_address = Address::with_last_byte(0x43); - let mut db = CacheDB::::default(); - for (address, code) in [(target, code), (child_address, child)] { + let mut db = InMemoryDB::default(); + for (address, code, balance) in [(target, code, balance), (child_address, child, 0)] { db.insert_account_info( &address, - AccountInfo::default().with_code(Bytecode::new_raw(code.to_vec().into())), + AccountInfo::default() + .with_code(Bytecode::new_raw(code.to_vec().into())) + .with_balance(U256::from(balance)), ); } let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = spec) .with_db(db) .build_mainnet_with_inspector(TracingInspector::new(config)); - evm.inspect_tx(TxEnv { - gas_limit: 1_000_000, - kind: TransactTo::Call(target), - ..Default::default() - }) - .unwrap(); + evm.inspect_tx(TxEnv { gas_limit, kind: TransactTo::Call(target), ..Default::default() }) + .unwrap(); evm.into_inspector() } @@ -506,7 +512,7 @@ fn vmtrace_memory_is_attached_to_the_writing_instruction() { let trace = trace_vm_code(&hex!("60016000525f00"), &[]); let mem = trace.ops[2].ex.as_ref().unwrap().mem.as_ref().unwrap(); assert_eq!(mem.off, 0); - assert_eq!(mem.data.as_ref(), U256::from(1).to_be_bytes::<32>()); + assert_eq!(mem.data.as_ref(), B256::with_last_byte(1).0); assert!(trace.ops[3].ex.as_ref().unwrap().mem.is_none()); } @@ -524,22 +530,25 @@ fn vmtrace_reports_remaining_gas_after_execution() { #[test] fn vmtrace_reports_remaining_gas_after_storage_credit() { // Restore a newly written slot to zero, crediting gas spilled from the state gas reservoir. - let inspector = inspect_code( - &hex!("60015f555f5f5500"), - &[], - SpecId::AMSTERDAM, - TracingInspectorConfig::parity_vm_trace(), - ); - let steps = &inspector.traces().nodes()[0].trace.steps; - let gas_after = steps[6].gas_remaining; - assert!(gas_after > steps[5].gas_remaining); - let trace = inspector.into_parity_builder().vm_trace(); - assert_eq!(trace.ops[5].ex.as_ref().unwrap().used, gas_after); + for deltas in [false, true] { + let inspector = inspect_code( + &hex!("60015f555f5f5500"), + &[], + SpecId::AMSTERDAM, + TracingInspectorConfig::parity_vm_trace().set_step_deltas(deltas), + ); + let steps = &inspector.traces().nodes()[0].trace.steps; + let gas_after = steps[6].gas_remaining; + assert!(gas_after > steps[5].gas_remaining); + let trace = inspector.into_parity_builder().vm_trace(); + assert_eq!(trace.ops[5].ex.as_ref().unwrap().used, gas_after); + } } #[test] -fn non_vm_traces_do_not_record_step_deltas() { - // Exercise a memory write, a call and a storage gas credit. +fn non_vm_traces_only_record_gas_deltas() { + // Exercise a memory write, a call, a storage write that draws state gas from the remaining + // gas and a storage gas credit. let code = hex!("602a5f525f5f5f5f5f604361fffff15060015f555f5f5500"); for config in [ TracingInspectorConfig::default_geth(), @@ -547,7 +556,20 @@ fn non_vm_traces_do_not_record_step_deltas() { TracingInspectorConfig::from_geth_call_config(&Default::default()), ] { let inspector = inspect_code(&code, &hex!("00"), SpecId::AMSTERDAM, config); - assert!(inspector.traces().nodes().iter().all(|node| node.trace.step_deltas.is_empty())); + for node in inspector.traces().nodes() { + for delta in &node.trace.step_deltas { + assert!(delta.gas_remaining_after.is_some()); + assert!(delta.memory.is_none()); + assert!(delta.store.is_none()); + } + } + if config.record_steps { + assert_eq!(inspector.traces().nodes()[0].trace.step_deltas.len(), 3); + } else { + assert!( + inspector.traces().nodes().iter().all(|node| node.trace.step_deltas.is_empty()) + ); + } } } @@ -761,10 +783,10 @@ fn vmtrace_revert_keeps_its_execution_record() { fn vmtrace_call_pushes_result_and_copies_return_memory() { let trace = trace_vm_code(&hex!("602060205f5f5f604361fffff15f00"), &hex!("602a5f5260205ff3")); let call = &trace.ops[7]; - assert_eq!(call.ex.as_ref().unwrap().push.as_slice(), &[U256::from(1)]); + assert_eq!(call.ex.as_ref().unwrap().push, &[U256::ONE]); let mem = call.ex.as_ref().unwrap().mem.as_ref().unwrap(); assert_eq!(mem.off, 32); - assert_eq!(mem.data.as_ref(), U256::from(42).to_be_bytes::<32>()); + assert_eq!(mem.data.as_ref(), B256::with_last_byte(42).0); } #[test] @@ -788,11 +810,13 @@ fn vmtrace_terminal_call_finalizes_delta_at_capture_limit() { let call = trace.ops.last().unwrap(); assert_eq!(call.op.as_deref(), Some("CALL")); let ex = call.ex.as_ref().unwrap(); - assert_eq!(ex.push, vec![U256::from(1)]); + assert_eq!(ex.push, vec![U256::ONE]); let mem = ex.mem.as_ref().unwrap(); assert_eq!(mem.off, 32); assert_eq!(mem.data.as_ref(), &[42]); - assert_eq!(ex.used, trace.ops[6].ex.as_ref().unwrap().used - call.cost); + // The cost includes the forwarded gas. The child spends 16 gas and returns the rest. + assert_eq!(call.cost, 2_600 + 6 + 65_535); + assert_eq!(ex.used, trace.ops[6].ex.as_ref().unwrap().used - call.cost + 65_535 - 16); } } @@ -837,8 +861,8 @@ fn vmtrace_stop_only_child_preserves_parent_and_creation_result() { assert_eq!(trace.ops.len(), 6); let create = &trace.ops[3]; assert_eq!( - create.ex.as_ref().unwrap().push.as_slice(), - &[U256::from_be_slice(Address::with_last_byte(0x42).create(0).as_slice())] + create.ex.as_ref().unwrap().push, + &[address_to_word(&Address::with_last_byte(0x42).create(0))] ); assert!(create.sub.is_some()); } @@ -851,7 +875,7 @@ fn vmtrace_nested_sibling_calls_keep_their_own_subtraces() { root.extend(hex!("505f5f5f5f5f604461fffff100")); let mut branch = hex!("5f5f5f5f5f604561fffff1").to_vec(); branch.push(0x00); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); for (last, code) in [ (0x42, root), (0x43, hex!("60015000").to_vec()), @@ -884,7 +908,7 @@ fn vmtrace_precompile_does_not_steal_the_next_calls_subtrace() { trace_vm_code(&hex!("5f5f5f5f5f600461fffff1505f5f5f5f5f604361fffff100"), &hex!("60015000")); assert!(trace.ops[7].sub.as_ref().unwrap().ops.is_empty()); assert_eq!(trace.ops[16].sub.as_ref().unwrap().ops.len(), 3); - assert_eq!(trace.ops[7].ex.as_ref().unwrap().push, vec![U256::from(1)]); + assert_eq!(trace.ops[7].ex.as_ref().unwrap().push, vec![U256::ONE]); } #[test] @@ -901,7 +925,7 @@ fn vmtrace_memory_copy_and_terminal_writes() { .find_map(|op| op.ex.as_ref().and_then(|ex| ex.mem.as_ref())) .unwrap(); assert_eq!(mem.off, offset); - assert_eq!(mem.data.as_ref(), expected); + assert_eq!(mem.data, expected); } } @@ -939,12 +963,11 @@ fn frame_errors(inspector: &TracingInspector) -> (Vec, Vec) { fn reverted_create_reports_gas_and_revert_data() { let initcode = hex!("63deadbeef5f526004601cfd"); let factory = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &factory, - AccountInfo::default().with_code(Bytecode::new_raw( - hex!("6b63deadbeef5f526004601cfd5f52600c60145ff000").into(), - )), + AccountInfo::default() + .with_code(Bytecode::new_raw(bytes!("6b63deadbeef5f526004601cfd5f52600c60145ff000"))), ); let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( TracingInspector::new(TracingInspectorConfig::default_parity()), @@ -1058,12 +1081,12 @@ fn failed_frames_use_parity_labels() { fn create_nonce_overflow_is_a_failed_frame() { for nonce in [u64::MAX, u64::MAX - 1] { let factory = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &factory, AccountInfo { nonce, - ..AccountInfo::default().with_code(Bytecode::new_raw(hex!("5f5f5ff000").into())) + ..AccountInfo::default().with_code(Bytecode::new_raw(bytes!("5f5f5ff000"))) }, ); let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( @@ -1091,9 +1114,58 @@ fn create_nonce_overflow_is_a_failed_frame() { } } +#[test] +fn create_nonce_overflow_runs_no_frame_since_amsterdam() { + let factory = Address::with_last_byte(0x42); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &factory, + AccountInfo { + nonce: u64::MAX, + ..AccountInfo::default().with_code(Bytecode::new_raw(bytes!("5f5f5ff000"))) + }, + ); + let mut evm = Context::mainnet() + .modify_cfg_chained(|cfg| cfg.spec = SpecId::AMSTERDAM) + .with_db(db) + .build_mainnet_with_inspector(TracingInspector::new( + TracingInspectorConfig::default_parity(), + )); + evm.inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(factory), + ..Default::default() + }) + .unwrap(); + let traces = evm.inspector.into_parity_builder().into_transaction_traces(); + assert_eq!(traces.len(), 1); +} + +#[test] +fn create_with_empty_initcode_runs_no_steps() { + let factory = Address::with_last_byte(0x42); + for (kind, depth_nodes) in [(TransactTo::Create, 1), (TransactTo::Call(factory), 2)] { + let mut db = InMemoryDB::default(); + db.insert_account_info( + &factory, + AccountInfo::default().with_code(Bytecode::new_raw(bytes!("5f5f5ff000"))), + ); + let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( + TracingInspector::new(TracingInspectorConfig::default_parity()), + ); + evm.inspect_tx(TxEnv { gas_limit: 1_000_000, kind, ..Default::default() }).unwrap(); + let nodes = evm.inspector.traces().nodes(); + assert_eq!(nodes.len(), depth_nodes); + let create = nodes.last().unwrap(); + assert!(create.kind().is_any_create()); + assert!(create.trace.success && create.trace.steps.is_empty()); + assert_eq!(create.trace.gas_used, 0); + } +} + #[test] fn precompile_exclusion_uses_original_callee_with_delegation() { - let parent = Address::from([0x11; 20]); + let parent = Address::repeat_byte(0x11); for callee in [2, 0x55] { for opcode in [op::CALL, op::CALLCODE, op::DELEGATECALL, op::STATICCALL] { let mut code = vec![op::PUSH0, op::PUSH0, op::PUSH1, 4, op::PUSH0]; @@ -1101,7 +1173,7 @@ fn precompile_exclusion_uses_original_callee_with_delegation() { code.push(op::PUSH0); } code.extend([op::PUSH1, callee, op::GAS, opcode, op::POP, op::STOP]); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &parent, AccountInfo::default().with_code(Bytecode::new_raw(code.into())), @@ -1134,6 +1206,287 @@ fn precompile_exclusion_uses_original_callee_with_delegation() { } } +/// Gas of an Amsterdam account creation: 120 state bytes at 1530 gas per byte. +const NEW_ACCOUNT_STATE_GAS: u64 = 183_600; + +/// The cost and used gas of the call or create step before the final `STOP`, as reported by +/// parity `vmTrace` and geth struct logs under each config. +/// +/// The expected costs in the tests below come from running the same bytecode on go-ethereum +/// `a0cba163b1f17f6044df84502ff13036dffb5de1` (`evm 1.17.11-unstable-a0cba163`) with +/// `evm run --prestate --trace --trace.format=json`. Its interpreter reports each +/// opcode's execution gas only (core/vm/interpreter.go): state gas is not part of the cost. +struct CallStep { + /// Gas remaining before the step. + gas_remaining: u64, + /// Gas limit of the child, if one started. + child_gas_limit: Option, + /// Parity cost, which equals the geth cost. + cost: u64, + /// Parity used gas, with and without step deltas. + used: u64, +} + +fn call_step(code: &[u8], child: &[u8], spec: SpecId) -> CallStep { + call_step_funded(code, child, spec, 0, 1_000_000) +} + +fn call_step_funded( + code: &[u8], + child: &[u8], + spec: SpecId, + balance: u64, + gas_limit: u64, +) -> CallStep { + let vm_trace = |config| { + let inspector = inspect_funded_code(code, child, spec, config, balance, gas_limit); + let nodes = inspector.traces().nodes(); + let step = &nodes[0].trace.steps[nodes[0].trace.steps.len() - 2]; + let step = (step.gas_remaining, nodes.get(1).map(|node| node.trace.gas_limit)); + let trace = inspector.into_parity_builder().vm_trace(); + let call = &trace.ops[trace.ops.len() - 2]; + (step, call.cost, call.ex.as_ref().unwrap().used) + }; + let (step, cost, used) = vm_trace(TracingInspectorConfig::parity_vm_trace()); + let (_, cost_without_deltas, used_without_deltas) = + vm_trace(TracingInspectorConfig::parity_vm_trace().set_step_deltas(false)); + assert_eq!(used_without_deltas, used); + let geth = inspect_funded_code( + code, + child, + spec, + TracingInspectorConfig::default_geth(), + balance, + gas_limit, + ) + .geth_builder() + .geth_traces(0, Default::default(), Default::default()); + let log = geth.struct_logs.iter().rev().find(|log| log.gas == step.0).unwrap(); + assert_eq!(cost_without_deltas, cost); + assert_eq!(log.gas_cost, cost); + CallStep { gas_remaining: step.0, child_gas_limit: step.1, cost, used } +} + +#[test] +fn call_cost_includes_forwarded_gas() { + for (spec, cold) in [(SpecId::PRAGUE, 2_600), (SpecId::AMSTERDAM, 3_000)] { + for (prefix, opcode) in + [("5f5f5f5f5f", "f1"), ("5f5f5f5f5f", "f2"), ("5f5f5f5f", "f4"), ("5f5f5f5f", "fa")] + { + for (child, spent) in [(&hex!("00")[..], 0), (&hex!("5f5ffd")[..], 4), (&[][..], 0)] { + let code = hex::decode(format!("{prefix}604361ffff{opcode}00")).unwrap(); + let step = call_step(&code, child, spec); + assert_eq!(step.cost, cold + 65_535); + assert_eq!(step.used, step.gas_remaining - step.cost + 65_535 - spent); + } + } + } +} + +#[test] +fn call_cost_excludes_stipend() { + // A cold call forwarding 1024 gas. + for (spec, cost) in [(SpecId::PRAGUE, 3_624), (SpecId::AMSTERDAM, 4_024)] { + for (prefix, opcode) in + [("5f5f5f5f5f", "f1"), ("5f5f5f5f5f", "f2"), ("5f5f5f5f", "f4"), ("5f5f5f5f", "fa")] + { + let code = hex::decode(format!("{prefix}6043610400{opcode}00")).unwrap(); + let step = call_step(&code, &hex!("00"), spec); + assert_eq!(step.cost, cost); + assert_eq!(step.used, step.gas_remaining - step.cost + 1_024); + } + } +} + +#[test] +fn precompile_call_cost_includes_forwarded_gas() { + for spec in [SpecId::PRAGUE, SpecId::AMSTERDAM] { + let step = call_step(&hex!("5f5f5f5f5f600461fffff100"), &[], spec); + assert_eq!(step.cost, 100 + 65_535); + assert_eq!(step.used, step.gas_remaining - step.cost + 65_535 - 15); + } +} + +#[test] +fn unfunded_call_cost_includes_forwarded_gas() { + // The child fails with out of funds and returns its gas, including the stipend. The cost + // has the base, cold, value and, before Amsterdam, new account charges plus the forwarded + // 1000 gas, without the stipend. Amsterdam charges the new account as state gas, which the + // failed call gives back. + for (spec, child, cost) in [ + (SpecId::PRAGUE, &hex!("00")[..], 2_600 + 9_000 + 1_000), + (SpecId::PRAGUE, &[][..], 37_600), + (SpecId::AMSTERDAM, &hex!("00")[..], 3_000 + 11_300 + 1_000), + (SpecId::AMSTERDAM, &[][..], 3_000 + 11_300 + 1_000), + ] { + let step = call_step(&hex!("5f5f5f5f600160436103e8f100"), child, spec); + assert_eq!(step.child_gas_limit, Some(1_000 + 2_300)); + assert_eq!(step.cost, cost); + assert_eq!(step.used, step.gas_remaining - cost + 3_300); + } +} + +#[test] +fn create_cost_excludes_forwarded_gas() { + for (spec, cost, state_gas) in + [(SpecId::PRAGUE, 32_000, 0), (SpecId::AMSTERDAM, 12_000, NEW_ACCOUNT_STATE_GAS)] + { + for code in [&hex!("5f5f5ff000")[..], &hex!("5f5f5f5ff500")[..]] { + let step = call_step(code, &[], spec); + let available = step.gas_remaining - cost - state_gas; + assert_eq!(step.cost, cost); + assert_eq!(step.child_gas_limit, Some(available - available / 64)); + assert_eq!(step.used, available); + } + } +} + +#[test] +fn create_cost_counts_all_gas_charged_before_the_child_split() { + // Amsterdam charges the account creation state gas, which spills into regular gas when the + // reservoir is empty. It is not part of the cost but the child gets 63/64 of what is left + // after it. + for (spec, state_gas) in [(SpecId::PRAGUE, 0), (SpecId::AMSTERDAM, NEW_ACCOUNT_STATE_GAS)] { + for code in [&hex!("5f5f5ff000")[..], &hex!("5f5f5f5ff500")[..]] { + let step = call_step(code, &[], spec); + let remaining = step.gas_remaining - step.cost - state_gas; + assert_eq!(step.child_gas_limit, Some(remaining - remaining / 64)); + } + } +} + +#[test] +fn create_cost_counts_initcode_memory_and_hash_words() { + // The initcode `5f5f5f5f00` spends 8 gas. + for (spec, base, state_gas) in + [(SpecId::PRAGUE, 32_000, 0), (SpecId::AMSTERDAM, 12_000, NEW_ACCOUNT_STATE_GAS)] + { + for (code, cost) in [ + (&hex!("645f5f5f5f005f526005601b5ff000")[..], base + 2), + (&hex!("645f5f5f5f005f526005601b5f5ff500")[..], base + 8), + (&hex!("6001600052600160ff5ff000")[..], base + 23), + ] { + let step = call_step(code, &[], spec); + assert_eq!(step.cost, cost); + } + let step = call_step(&hex!("645f5f5f5f005f526005601b5ff000"), &[], spec); + assert_eq!(step.used, step.gas_remaining - step.cost - state_gas - 8); + } +} + +#[test] +fn unfunded_create_cost_excludes_forwarded_gas() { + let code = hex!("5f5f6001f000"); + assert!(trace_vm_code(&code, &[]).ops[3].sub.is_none()); + for (spec, cost) in [(SpecId::PRAGUE, 32_000), (SpecId::AMSTERDAM, 12_000)] { + let step = call_step(&code, &[], spec); + assert_eq!(step.cost, cost); + assert_eq!(step.used, step.gas_remaining - cost); + } +} + +#[test] +fn call_like_step_used_gas_is_the_gas_after_the_child_returns() { + // `ex.used` of a call or create step is the parent's gas once the child returned, which the + // next step reports as its remaining gas, whatever the config records. + let codes = [&hex!("5f5f5f5f5f604361fffff100")[..], &hex!("5f5f5ff000"), &hex!("5f5f5f5ff500")]; + for spec in [SpecId::PRAGUE, SpecId::AMSTERDAM] { + for code in codes { + for child in [&hex!("00")[..], &hex!("5f5ffd"), &[]] { + for config in [ + TracingInspectorConfig::default_geth(), + TracingInspectorConfig::parity_vm_trace(), + TracingInspectorConfig::parity_vm_trace().set_step_deltas(false), + TracingInspectorConfig::all(), + ] { + let inspector = inspect_code(code, child, spec, config); + let after = + inspector.traces().nodes()[0].trace.steps.last().unwrap().gas_remaining; + let trace = inspector.into_parity_builder().vm_trace(); + let used = trace.ops[trace.ops.len() - 2].ex.as_ref().unwrap().used; + assert_eq!(used, after, "{code:x?} {child:x?} {spec:?}"); + } + } + } + } +} + +#[test] +fn nested_call_step_used_gas_is_the_gas_after_the_child_returns() { + let code = hex!("5f5f5f5f5f604361fffff100"); + let child = hex!("5f5f5f5f5f600461fffff100"); + for config in [ + TracingInspectorConfig::default_geth(), + TracingInspectorConfig::parity_vm_trace().set_step_deltas(false), + ] { + let inspector = inspect_code(&code, &child, SpecId::AMSTERDAM, config); + let nodes = inspector.traces().nodes(); + let after = + [&nodes[0], &nodes[1]].map(|node| node.trace.steps.last().unwrap().gas_remaining); + let trace = inspector.into_parity_builder().vm_trace(); + let outer = &trace.ops[trace.ops.len() - 2]; + let inner = &outer.sub.as_ref().unwrap().ops; + let inner = &inner[inner.len() - 2]; + assert_eq!(outer.ex.as_ref().unwrap().used, after[0]); + assert_eq!(inner.ex.as_ref().unwrap().used, after[1]); + } +} + +#[test] +fn nested_calls_keep_each_forwarded_cost() { + let trace = trace_vm_code(&hex!("5f5f5f5f5f604361fffff100"), &hex!("5f5f5f5f5f60046103e8f100")); + let outer = &trace.ops[7]; + let child = outer.sub.as_ref().unwrap(); + let inner = &child.ops[7]; + assert_eq!(outer.cost, 2_600 + 65_535); + assert_eq!(inner.cost, 100 + 1_000); + assert_eq!( + outer.ex.as_ref().unwrap().used, + trace.ops[6].ex.as_ref().unwrap().used - 2_600 - 16 - 100 - 15 + ); + assert_eq!(inner.ex.as_ref().unwrap().used, child.ops[6].ex.as_ref().unwrap().used - 100 - 15); +} + +#[test] +fn parity_traces_leave_out_zero_value_precompile_calls() { + let contract = Address::with_last_byte(0x42); + let mut db = InMemoryDB::default(); + db.insert_account_info( + &contract, + AccountInfo::default().with_balance(U256::from(1)).with_code(Bytecode::new_raw( + // CALL 0x04 with no value, CALL 0x04 with value 1, CALL 0x99 with no value. + hex!("5f5f5f5f5f60045af1505f5f5f5f600160045af1505f5f5f5f5f60995af15000").into(), + )), + ); + let mut evm = Context::mainnet().with_db(db).build_mainnet_with_inspector( + TracingInspector::new(TracingInspectorConfig::default_parity()), + ); + evm.inspect_tx(TxEnv { + gas_limit: 1_000_000, + kind: TransactTo::Call(contract), + ..Default::default() + }) + .unwrap(); + + let traces = evm.inspector.clone().into_parity_builder().into_transaction_traces(); + let calls: Vec<_> = traces + .iter() + .map(|trace| { + let Action::Call(call) = &trace.action else { panic!("expected a call") }; + (call.to, call.value, trace.trace_address.clone()) + }) + .collect(); + assert_eq!( + calls, + [ + (contract, U256::ZERO, vec![]), + (Address::with_last_byte(4), U256::from(1), vec![0]), + (Address::with_last_byte(0x99), U256::ZERO, vec![1]), + ] + ); + assert_eq!(traces[0].subtraces, 2); +} + #[path = "parity/vm_bytecode.rs"] mod vm_bytecode; @@ -1148,3 +1501,6 @@ mod state_diff_selfdestruct; #[path = "parity/oog_cost.rs"] mod oog_cost; + +#[path = "parity/state_gas_cost.rs"] +mod state_gas_cost; diff --git a/crates/inspectors/tests/it/parity/state_diff_7702.rs b/crates/inspectors/tests/it/parity/state_diff_7702.rs index 7be439cfd..539df7025 100644 --- a/crates/inspectors/tests/it/parity/state_diff_7702.rs +++ b/crates/inspectors/tests/it/parity/state_diff_7702.rs @@ -1,12 +1,12 @@ use alloy_consensus::{TxEip7702, transaction::Recovered}; -use alloy_eips::eip7702::{Authorization, RecoveredAuthority, RecoveredAuthorization}; +use alloy_eips::eip7702::{Authorization, SignedAuthorization}; use alloy_primitives::{Address, U64, U256, hex, map::HashSet}; use alloy_rpc_types_trace::parity::{Delta, TraceType}; use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, bytecode::Bytecode, ethereum::{LazyTxEip7702, TxEnvelope, ethereum_tx_registry}, - evm::{AccountInfo, CacheDB, EmptyDB}, + evm::{AccountInfo, InMemoryDB}, }; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; @@ -24,7 +24,7 @@ fn authorization_code_changes() { for revert in [false, true] { let authority = Address::with_last_byte(0x43); let target = Address::with_last_byte(0x44); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &authority, AccountInfo { nonce: 1, ..Default::default() }.with_code(before.clone()), @@ -46,17 +46,25 @@ fn authorization_code_changes() { evm.set_inspector(&mut inspector); let tx = Recovered::new_unchecked( TxEnvelope::Eip7702(LazyTxEip7702::from_cached_recovered_authorizations( - TxEip7702 { chain_id: 1, to: target, gas_limit: 100_000, ..Default::default() }, - vec![RecoveredAuthorization::new_unchecked( - Authorization { chain_id: U256::from(1), address: delegate, nonce: 1 }, - RecoveredAuthority::Valid(authority), - )], + TxEip7702 { + chain_id: 1, + to: target, + gas_limit: 100_000, + authorization_list: vec![SignedAuthorization::new_unchecked( + Authorization { chain_id: U256::ONE, address: delegate, nonce: 1 }, + 0, + U256::ZERO, + U256::ZERO, + )], + ..Default::default() + }, + vec![Some(authority)], )), Address::ZERO, ); let result = evm.transact(&tx).unwrap().detach(); drop(evm); - assert_eq!(result.result.status, !revert); + assert_eq!(result.result.is_success(), !revert); let after = if delegate.is_zero() { Bytecode::default() } else { diff --git a/crates/inspectors/tests/it/parity/state_diff_birth.rs b/crates/inspectors/tests/it/parity/state_diff_birth.rs index f7b708064..3af1c0098 100644 --- a/crates/inspectors/tests/it/parity/state_diff_birth.rs +++ b/crates/inspectors/tests/it/parity/state_diff_birth.rs @@ -1,11 +1,10 @@ -use crate::utils::{ - AccountInfo, CacheDB, Context, DatabaseCommit, EmptyDB, SpecId, TransactTo, TxEnv, -}; +use crate::utils::{AccountInfo, Context, DatabaseCommit, SpecId, TransactTo, TxEnv}; use alloy_primitives::{Address, U64, U256, map::HashSet}; use alloy_rpc_types_trace::parity::{Delta, StateDiff, TraceType}; +use evm2::evm::InMemoryDB; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; -fn trace(db: &mut CacheDB, tx: TxEnv, beneficiary: Address, spec: SpecId) -> StateDiff { +fn trace(db: &mut InMemoryDB, tx: TxEnv, beneficiary: Address, spec: SpecId) -> StateDiff { let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = spec) .modify_block_chained(|block| block.beneficiary = beneficiary) @@ -33,7 +32,7 @@ fn transfer_creates_account_then_changes_existing_account() { let caller = Address::with_last_byte(0x41); let recipient = Address::with_last_byte(0x42); let beneficiary = Address::with_last_byte(0x43); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, @@ -73,7 +72,7 @@ fn transfer_creates_account_then_changes_existing_account() { #[test] fn zero_value_call_does_not_create_account() { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); let recipient = Address::with_last_byte(0x42); let beneficiary = Address::with_last_byte(0x43); let diff = trace( @@ -92,7 +91,7 @@ fn creation_uses_account_existence_not_initial_balance() { let target = caller.create(0); for initial_balance in [None, Some(0), Some(9)] { for runtime in [false, true] { - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); if let Some(balance) = initial_balance { db.insert_account_info( &target, @@ -139,7 +138,7 @@ fn creation_uses_account_existence_not_initial_balance() { fn frontier_materializes_empty_account() { let caller = Address::with_last_byte(0x41); let recipient = Address::with_last_byte(0x42); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(100_000), ..Default::default() }, diff --git a/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs b/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs index b1f442a85..e8200ab03 100644 --- a/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs +++ b/crates/inspectors/tests/it/parity/state_diff_selfdestruct.rs @@ -1,6 +1,7 @@ -use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, SpecId, TransactTo, TxEnv}; -use alloy_primitives::{Address, B256, U64, U256, hex, map::HashSet}; +use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, SpecId, TransactTo, TxEnv}; +use alloy_primitives::{Address, B256, U64, U256, bytes, hex, map::HashSet}; use alloy_rpc_types_trace::parity::{Delta, TraceType}; +use evm2::evm::InMemoryDB; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; #[test] @@ -11,23 +12,22 @@ fn existing_account_selfdestruct_across_cancun() { let parent = Address::with_last_byte(0x43); // Read an unchanged slot, overwrite a nonzero slot, write a new slot, read zero, // then selfdestruct. - let code = Bytecode::new_raw( - hex!("600054506009600155600360025560035450600060045533ff").into(), - ); - let mut db = CacheDB::::default(); + let code = + Bytecode::new_raw(bytes!("600054506009600155600360025560035450600060045533ff")); + let mut db = InMemoryDB::default(); db.insert_account_info( &target, AccountInfo { balance: U256::from(69), nonce: 1, ..Default::default() } .with_code(code.clone()), ); db.insert_account_storage(&target, &U256::ZERO, &U256::from(7)); - db.insert_account_storage(&target, &U256::from(1), &U256::from(8)); + db.insert_account_storage(&target, &U256::ONE, &U256::from(8)); db.insert_account_storage(&target, &U256::from(4), &U256::from(10)); db.insert_account_info( &parent, - AccountInfo::default().with_code(Bytecode::new_raw( - hex!("60006000600060006000604261fffff15060006000fd").into(), - )), + AccountInfo::default().with_code(Bytecode::new_raw(bytes!( + "60006000600060006000604261fffff15060006000fd" + ))), ); let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = spec) @@ -77,16 +77,16 @@ fn existing_account_selfdestruct_across_cancun() { assert_eq!(diff.code, Delta::Unchanged); assert_eq!( diff.storage[&B256::with_last_byte(4)], - Delta::changed(U256::from(10).into(), U256::ZERO.into()) + Delta::changed(B256::with_last_byte(10), B256::ZERO) ); assert_eq!(diff.storage.len(), 3); assert_eq!( diff.storage[&B256::with_last_byte(1)], - Delta::changed(U256::from(8).into(), U256::from(9).into()) + Delta::changed(B256::with_last_byte(8), B256::with_last_byte(9)) ); assert_eq!( diff.storage[&B256::with_last_byte(2)], - Delta::changed(U256::ZERO.into(), U256::from(3).into()) + Delta::changed(B256::ZERO, B256::with_last_byte(3)) ); } } @@ -99,7 +99,7 @@ fn created_account_selfdestruct_across_cancun() { for prefunded in [false, true] { let caller = Address::with_last_byte(0x42); let target = caller.create(0); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); if prefunded { db.insert_account_info( &target, @@ -117,7 +117,7 @@ fn created_account_selfdestruct_across_cancun() { TxEnv::builder() .caller(caller) .kind(TransactTo::Create) - .data(hex!("33ff").into()) + .data(bytes!("33ff")) .gas_limit(100_000) .build_fill(), ) @@ -165,7 +165,7 @@ fn selfdestruct_preserves_balance_only_account() { TxEnv::builder() .caller(caller) .kind(TransactTo::Create) - .data(hex!("30ff").into()) + .data(bytes!("30ff")) .gas_limit(200_000) .build_fill(), ) @@ -195,13 +195,13 @@ fn deleted_account_diff_does_not_depend_on_accessed_slots() { let target = Address::with_last_byte(0x42); let diffs = [&hex!("60005450600960015533ff")[..], &hex!("33ff")[..]].map(|code| { let code = Bytecode::new_raw(code.to_vec().into()); - let mut db = CacheDB::::default(); + let mut db = InMemoryDB::default(); db.insert_account_info( &target, AccountInfo { balance: U256::from(69), nonce: 1, ..Default::default() }.with_code(code), ); db.insert_account_storage(&target, &U256::ZERO, &U256::from(7)); - db.insert_account_storage(&target, &U256::from(1), &U256::from(8)); + db.insert_account_storage(&target, &U256::ONE, &U256::from(8)); let mut evm = Context::mainnet() .modify_cfg_chained(|cfg| cfg.spec = SpecId::SHANGHAI) .with_db(db) diff --git a/crates/inspectors/tests/it/parity/state_gas_cost.rs b/crates/inspectors/tests/it/parity/state_gas_cost.rs new file mode 100644 index 000000000..fff29f163 --- /dev/null +++ b/crates/inspectors/tests/it/parity/state_gas_cost.rs @@ -0,0 +1,120 @@ +//! Amsterdam step costs leave out state gas, in both trace formats. +//! +//! The expected values come from go-ethereum `a0cba163b1f17f6044df84502ff13036dffb5de1` +//! (`evm 1.17.11-unstable-a0cba163`): the interpreter passes each opcode's execution and state +//! gas separately (core/vm/interpreter.go) and the struct logger records the execution gas only +//! (core/tracing/hooks.go, `EmitOpcode`). + +use super::*; + +/// Transaction gas limit that leaves a reservoir of 100,000 gas, below the 183,600 gas of an +/// account creation: the gas above the 2^24 transaction cap. +const SHORT_RESERVOIR_GAS_LIMIT: u64 = 16_777_216 + 100_000; +/// Transaction gas limit that leaves a reservoir larger than any state charge below. +const FULL_RESERVOIR_GAS_LIMIT: u64 = 20_000_000; + +/// Returns the cost of each step of `code` in parity `vmTrace` and in geth struct logs, which +/// must agree, and the state gas of each step. +fn step_costs(code: &[u8], balance: u64, gas_limit: u64) -> (Vec, Vec>) { + let inspect = + |config| inspect_funded_code(code, &[], SpecId::AMSTERDAM, config, balance, gas_limit); + let inspector = inspect(TracingInspectorConfig::all()); + let steps = &inspector.traces().nodes()[0].trace.steps; + let costs = steps.iter().map(|step| step.gas_cost).collect::>(); + let state_costs = steps.iter().map(|step| step.state_gas_cost).collect(); + for config in [ + TracingInspectorConfig::parity_vm_trace(), + TracingInspectorConfig::parity_vm_trace().set_step_deltas(false), + ] { + let vm_trace = inspect(config).into_parity_builder().vm_trace(); + assert_eq!(vm_trace.ops.iter().map(|op| op.cost).collect::>(), costs); + // The gas left after a step is the gas the next step starts with. + for (op, next) in vm_trace.ops.iter().zip(steps.iter().skip(1)) { + assert_eq!(op.ex.as_ref().unwrap().used, next.gas_remaining); + } + } + let geth = inspect(TracingInspectorConfig::default_geth()).geth_builder().geth_traces( + 0, + Default::default(), + Default::default(), + ); + assert_eq!(geth.struct_logs.iter().map(|log| log.gas_cost).collect::>(), costs); + (costs, state_costs) +} + +#[test] +fn sstore_cost_leaves_out_state_gas() { + // A new slot costs 12,100 gas and 64 state bytes, drawn from the remaining gas when the + // reservoir is empty. + for gas_limit in [1_000_000, FULL_RESERVOIR_GAS_LIMIT] { + let (costs, state_costs) = step_costs(&hex!("602a5f5500"), 0, gas_limit); + assert_eq!(costs[2], 12_100); + assert_eq!(state_costs[2], Some(64 * 1_530)); + } +} + +#[test] +fn restoring_sstore_cost_leaves_out_state_gas_refill() { + // Writing the original zero back refills the state gas of the first write, which returns + // to the remaining gas when it spilled into it. The cost stays 100. + for gas_limit in [1_000_000, SHORT_RESERVOIR_GAS_LIMIT, FULL_RESERVOIR_GAS_LIMIT] { + let (costs, _) = step_costs(&hex!("602a5f555f5f5500"), 0, gas_limit); + assert_eq!(costs[2], 12_100); + assert_eq!(costs[5], 100); + } +} + +#[test] +fn selfdestruct_cost_leaves_out_state_gas() { + // Sending the balance to a new account costs 17,000 gas plus the account creation state + // gas, which the remaining gas pays when the reservoir is short. + for (gas_limit, spilled) in [ + (1_000_000, NEW_ACCOUNT_STATE_GAS), + (SHORT_RESERVOIR_GAS_LIMIT, NEW_ACCOUNT_STATE_GAS - 100_000), + (FULL_RESERVOIR_GAS_LIMIT, 0), + ] { + let code = hex!("6044ff"); + let (costs, state_costs) = step_costs(&code, 16, gas_limit); + assert_eq!(costs[1], 17_000); + assert_eq!(state_costs[1], Some(NEW_ACCOUNT_STATE_GAS as i64)); + for config in [ + TracingInspectorConfig::parity_vm_trace(), + TracingInspectorConfig::parity_vm_trace().set_step_deltas(false), + ] { + let inspector = + inspect_funded_code(&code, &[], SpecId::AMSTERDAM, config, 16, gas_limit); + let remaining = inspector.traces().nodes()[0].trace.steps[1].gas_remaining; + let trace = inspector.into_parity_builder().vm_trace(); + assert_eq!(trace.ops[1].ex.as_ref().unwrap().used, remaining - 17_000 - spilled); + } + } +} + +#[test] +fn value_call_to_new_account_cost_leaves_out_state_gas() { + // A cold CALL with value forwarding 0xffff gas to a new account. The child returns its gas + // and the stipend. + let code = hex!("5f5f5f5f6001604461fffff100"); + for (gas_limit, spilled) in [ + (1_000_000, NEW_ACCOUNT_STATE_GAS), + (SHORT_RESERVOIR_GAS_LIMIT, NEW_ACCOUNT_STATE_GAS - 100_000), + (FULL_RESERVOIR_GAS_LIMIT, 0), + ] { + let step = call_step_funded(&code, &[], SpecId::AMSTERDAM, 16, gas_limit); + assert_eq!(step.cost, 3_000 + 11_300 + 65_535); + assert_eq!(step.used, step.gas_remaining - step.cost - spilled + 65_535 + 2_300); + } +} + +#[test] +fn create_cost_leaves_out_state_gas_spilled_from_a_short_reservoir() { + for (gas_limit, spilled) in [ + (1_000_000, NEW_ACCOUNT_STATE_GAS), + (SHORT_RESERVOIR_GAS_LIMIT, NEW_ACCOUNT_STATE_GAS - 100_000), + (FULL_RESERVOIR_GAS_LIMIT, 0), + ] { + let step = call_step_funded(&hex!("5f5f5ff000"), &[], SpecId::AMSTERDAM, 0, gas_limit); + assert_eq!(step.cost, 12_000); + assert_eq!(step.used, step.gas_remaining - 12_000 - spilled); + } +} diff --git a/crates/inspectors/tests/it/parity/vm_bytecode.rs b/crates/inspectors/tests/it/parity/vm_bytecode.rs index af3bedf95..1f693acdf 100644 --- a/crates/inspectors/tests/it/parity/vm_bytecode.rs +++ b/crates/inspectors/tests/it/parity/vm_bytecode.rs @@ -1,19 +1,20 @@ -use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, EmptyDB, SpecId, TransactTo, TxEnv}; +use crate::utils::{AccountInfo, Bytecode, CacheDB, Context, SpecId, TransactTo, TxEnv}; use alloy_consensus::{TxEip7702, transaction::Recovered}; -use alloy_eips::eip7702::{Authorization, RecoveredAuthority, RecoveredAuthorization}; -use alloy_primitives::{Address, Bytes, U256, hex, map::HashSet}; +use alloy_eips::eip7702::{Authorization, SignedAuthorization}; +use alloy_primitives::{Address, Bytes, U256, bytes, hex, map::HashSet}; use alloy_rpc_types_trace::parity::{TraceResults, TraceType}; use evm2::{ BaseEvmTypes, Evm, Precompiles, ethereum::{LazyTxEip7702, RecoveredTxEnvelope, TxEnvelope, ethereum_tx_registry}, + evm::InMemoryDB, }; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; -fn trace(db: CacheDB, tx: TxEnv) -> TraceResults { +fn trace(db: InMemoryDB, tx: TxEnv) -> TraceResults { trace_envelope(db, tx.envelope()) } -fn trace_envelope(mut db: CacheDB, tx: RecoveredTxEnvelope) -> TraceResults { +fn trace_envelope(mut db: InMemoryDB, tx: RecoveredTxEnvelope) -> TraceResults { let types = HashSet::from_iter([TraceType::VmTrace]); let mut inspector = TracingInspector::new(TracingInspectorConfig::from_parity_config(&types)); let mut evm = Evm::::new( @@ -87,15 +88,15 @@ fn authorization_resolves_executed_bytecode() { let delegate = Address::with_last_byte(0x44); for before in [Bytecode::default(), Bytecode::new_eip7702(old_delegate)] { for (delegate, code) in [ - (delegate, Bytes::from_static(&hex!("602a5f5260205ff3"))), - (delegate, Bytes::from_static(&hex!("5f5ffd"))), + (delegate, bytes!("602a5f5260205ff3")), + (delegate, bytes!("5f5ffd")), (Address::ZERO, Bytes::new()), ] { let mut db = CacheDB::default(); db.insert_account_info(&authority, AccountInfo::default().with_code(before.clone())); db.insert_account_info( &old_delegate, - AccountInfo::default().with_code(Bytecode::new_raw(hex!("60015000").into())), + AccountInfo::default().with_code(Bytecode::new_raw(bytes!("60015000"))), ); if !delegate.is_zero() { db.insert_account_info( @@ -111,12 +112,15 @@ fn authorization_resolves_executed_bytecode() { chain_id: 1, to: authority, gas_limit: 100_000, + authorization_list: vec![SignedAuthorization::new_unchecked( + Authorization { chain_id: U256::ONE, address: delegate, nonce: 0 }, + 0, + U256::ZERO, + U256::ZERO, + )], ..Default::default() }, - vec![RecoveredAuthorization::new_unchecked( - Authorization { chain_id: U256::from(1), address: delegate, nonce: 0 }, - RecoveredAuthority::Valid(authority), - )], + vec![Some(authority)], )), Address::ZERO, ), diff --git a/crates/inspectors/tests/it/repro/mod.rs b/crates/inspectors/tests/it/repro/mod.rs index c4814df32..69ec0fcf9 100644 --- a/crates/inspectors/tests/it/repro/mod.rs +++ b/crates/inspectors/tests/it/repro/mod.rs @@ -47,10 +47,11 @@ mod prestate; -use crate::utils::{AccountInfo, Bytecode, CacheDB, EmptyDB, SpecId}; +use crate::utils::{AccountInfo, Bytecode, SpecId}; use alloy_hardforks::EthereumHardfork; use alloy_primitives::Address; use alloy_rpc_types_trace::geth::AccountState; +use evm2::evm::InMemoryDB; use serde::Deserialize; use std::collections::BTreeMap; @@ -89,8 +90,8 @@ pub const fn spec_id_from_block(block_number: u64) -> SpecId { } /// Build a CacheDB from prestate AccountState map. -pub fn build_db_from_prestate(prestate: &BTreeMap) -> CacheDB { - let mut db = CacheDB::new(EmptyDB::default()); +pub fn build_db_from_prestate(prestate: &BTreeMap) -> InMemoryDB { + let mut db = InMemoryDB::default(); for (addr, state) in prestate { let balance = state.balance.unwrap_or_default(); @@ -99,13 +100,12 @@ pub fn build_db_from_prestate(prestate: &BTreeMap) -> Cac db.insert_account_info( addr, - AccountInfo { + AccountInfo::new( balance, nonce, - code_hash: code.as_ref().map(|c| c.hash_slow()).unwrap_or_default(), + code.as_ref().map(|c| c.hash_slow()).unwrap_or_default(), code, - ..Default::default() - }, + ), ); // Insert storage @@ -149,7 +149,7 @@ pub struct ReproContext { /// The EVM spec to use for execution. pub spec_id: SpecId, /// The database populated with prestate. - pub db: CacheDB, + pub db: InMemoryDB, } impl ReproContext { diff --git a/crates/inspectors/tests/it/repro/prestate.rs b/crates/inspectors/tests/it/repro/prestate.rs index 0a788cbc7..1cef9a37c 100644 --- a/crates/inspectors/tests/it/repro/prestate.rs +++ b/crates/inspectors/tests/it/repro/prestate.rs @@ -4,7 +4,7 @@ use super::ReproContext; use crate::utils::{Context, SpecId, TransactTo, TxEnv}; -use alloy_primitives::{Bytes, address, hex}; +use alloy_primitives::{address, bytes}; use alloy_rpc_types_trace::geth::{DiffMode, PreStateConfig, PreStateFrame}; use evm2_inspectors::tracing::{TracingInspector, TracingInspectorConfig}; @@ -23,9 +23,9 @@ fn tx_selfdestruct_env() -> TxEnv { TxEnv { caller: address!("a7fb5ca286fc3fd67525629048a4de3ba24cba2e"), kind: TransactTo::Call(address!("c77ad0a71008d7094a62cfbd250a2eb2afdf2776")), - data: Bytes::from_static(&hex!( + data: bytes!( "f3fef3a3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000f6b64" - )), + ), nonce: 236280, gas_limit: 150000, ..Default::default() diff --git a/crates/inspectors/tests/it/stack.rs b/crates/inspectors/tests/it/stack.rs index 5f49274f3..e4acb513c 100644 --- a/crates/inspectors/tests/it/stack.rs +++ b/crates/inspectors/tests/it/stack.rs @@ -1,27 +1,22 @@ //! Stack snapshot tests -use crate::utils::{CacheDB, Context, EmptyDB, SpecId, inspect_deploy_contract}; -use alloy_primitives::{Address, U256, hex}; +use crate::utils::{Context, SpecId, inspect_deploy_contract}; +use alloy_primitives::{Address, U256, bytes}; +use evm2::evm::InMemoryDB; use evm2_inspectors::tracing::{StackSnapshotType, TracingInspector, TracingInspectorConfig}; /// Runs `PUSH1 1; PUSH1 2; PUSH1 3; STOP` as create init code and returns the recorded stack /// snapshot of each step. fn stack_snapshots(snapshot_type: StackSnapshotType) -> Vec>> { let mut insp = TracingInspector::new( - TracingInspectorConfig::none().set_steps(true).set_stack_snapshots(snapshot_type), + TracingInspectorConfig::none().steps().set_stack_snapshots(snapshot_type), ); - let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) - .build_mainnet() - .with_inspector(&mut insp); + let mut evm = + Context::mainnet().with_db(InMemoryDB::default()).build_mainnet().with_inspector(&mut insp); - let res = inspect_deploy_contract( - &mut evm, - hex!("60016002600300").into(), - Address::ZERO, - SpecId::CANCUN, - ); + let res = + inspect_deploy_contract(&mut evm, bytes!("60016002600300"), Address::ZERO, SpecId::CANCUN); assert!(res.result.is_success()); let nodes = insp.traces().nodes(); @@ -39,7 +34,7 @@ fn test_top_stack_snapshots() { vec![ // The stack is recorded before each step executes. Some(Box::default()), - Some(Box::from([U256::from(1)])), + Some(Box::from([U256::ONE])), Some(Box::from([U256::from(2)])), Some(Box::from([U256::from(3)])), ] diff --git a/crates/inspectors/tests/it/test_native_bigint.rs b/crates/inspectors/tests/it/test_native_bigint.rs index 232949a88..1cff213a9 100644 --- a/crates/inspectors/tests/it/test_native_bigint.rs +++ b/crates/inspectors/tests/it/test_native_bigint.rs @@ -1,6 +1,6 @@ //! Tests for native BigInt support in Boa -use alloy_primitives::U256; +use alloy_primitives::{U256, uint}; use boa_engine::{Context, JsValue, Source, js_string}; #[test] @@ -45,9 +45,9 @@ fn test_u256_to_boa_bigint() { // Test converting U256 values to BigInt let test_values = [ U256::ZERO, - U256::from(1u64), + U256::ONE, U256::from(u64::MAX), - U256::from_str_radix("123456789012345678901234567890", 10).unwrap(), + uint!(123456789012345678901234567890_U256), U256::MAX, ]; diff --git a/crates/inspectors/tests/it/transfer.rs b/crates/inspectors/tests/it/transfer.rs index 85d08bf2a..d2ea975aa 100644 --- a/crates/inspectors/tests/it/transfer.rs +++ b/crates/inspectors/tests/it/transfer.rs @@ -1,13 +1,19 @@ //! Transfer tests use crate::utils::{ - AccountInfo, Bytecode, CacheDB, Context, DatabaseCommit, EmptyDB, ExecutionResult, Output, - SpecId, TestDbExt, TransactTo, TxEnv, + AccountInfo, Bytecode, Context, DatabaseCommit, ExecutionResult, Output, SpecId, TestDbExt, + TransactTo, TxEnv, a, +}; +use alloy_primitives::{Address, B256, Bytes, Log, U256, address, bytes, hex}; +use evm2::{ + BaseEvmTypes, Evm, Inspector, Precompiles, ethereum::ethereum_tx_registry, evm::InMemoryDB, }; -use alloy_primitives::{Address, U256, address, hex}; use evm2_inspectors::{ tracing::{TracingInspector, TracingInspectorConfig}, - transfer::{TransferInspector, TransferKind, TransferOperation}, + transfer::{ + TRANSFER_EVENT_TOPIC, TRANSFER_LOG_EMITTER, TransferInspector, TransferKind, + TransferOperation, + }, }; #[test] @@ -26,7 +32,7 @@ fn test_internal_transfers() { ); let deployer = Address::ZERO; - let db = CacheDB::new(EmptyDB::default()); + let db = InMemoryDB::default(); let context = Context::mainnet().with_db(db).modify_cfg_chained(|c| c.spec = SpecId::LONDON); @@ -59,8 +65,7 @@ fn test_internal_transfers() { caller: deployer, gas_limit: 100000000, kind: TransactTo::Call(addr), - data: hex!("830c29ae0000000000000000000000000000000000000000000000000000000000000000") - .into(), + data: bytes!("830c29ae0000000000000000000000000000000000000000000000000000000000000000"), value: U256::from(10), nonce: 0, ..Default::default() @@ -110,7 +115,7 @@ fn test_internal_transfers() { #[test] fn discards_failed_create_transfer() { let deployer = Address::ZERO; - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(deployer).info.balance = U256::from(u64::MAX); let context = Context::mainnet().with_db(db).modify_cfg_chained(|c| c.spec = SpecId::LONDON); @@ -120,7 +125,7 @@ fn discards_failed_create_transfer() { caller: deployer, gas_limit: 1000000, kind: TransactTo::Create, - data: hex!("fe").into(), + data: bytes!("fe"), value: U256::from(10), nonce: 0, ..Default::default() @@ -136,7 +141,7 @@ fn discards_failed_call_transfers_and_logs() { let deployer = Address::ZERO; let caller = address!("0x1000000000000000000000000000000000000000"); let reverter = address!("0x2000000000000000000000000000000000000000"); - let recipient = address!("0x00000000000000000000000000000000000000ff"); + let recipient = Address::with_last_byte(0xff); // Calls `reverter` with 1 wei, `recipient` with 100 wei (more than the balance), and // `recipient` with 2 wei. `reverter` forwards 1 wei to `recipient` and then reverts. @@ -146,7 +151,7 @@ fn discards_failed_call_transfers_and_logs() { "6000600060006000600260ff5af150" "00" ); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(deployer).info.balance = U256::from(u64::MAX); db.insert_account_info( &caller, @@ -155,9 +160,7 @@ fn discards_failed_call_transfers_and_logs() { db.insert_account_info( &reverter, AccountInfo { - code: Some(Bytecode::new_legacy( - hex!("6000600060006000600160ff5af15060006000fd").into(), - )), + code: Some(Bytecode::new_legacy(bytes!("6000600060006000600160ff5af15060006000fd"))), ..Default::default() }, ); @@ -194,11 +197,10 @@ fn discards_failed_call_transfers_and_logs() { }, ] ); - let logs = &res.tx_result.result.logs; - assert_eq!(logs.len(), 2); - assert_eq!(logs[0].topics()[2], caller.into_word()); - assert_eq!(logs[1].topics()[1], caller.into_word()); - assert_eq!(logs[1].topics()[2], recipient.into_word()); + assert_eq!( + evm.inspector().logs(), + &[transfer(deployer, caller, 10), transfer(caller, recipient, 2)] + ); } #[test] @@ -208,7 +210,7 @@ fn discards_failed_internal_create_transfer_and_log() { // Creates a contract with 1 wei and init code `INVALID`. let factory_code = hex!("60fe600053600160006001f05000"); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(deployer).info.balance = U256::from(u64::MAX); db.insert_account_info( &factory, @@ -239,9 +241,7 @@ fn discards_failed_internal_create_transfer_and_log() { value: U256::from(10), }] ); - let logs = &res.tx_result.result.logs; - assert_eq!(logs.len(), 1); - assert_eq!(logs[0].topics()[2], factory.into_word()); + assert_eq!(evm.inspector().logs(), &[transfer(deployer, factory, 10)]); } #[test] @@ -251,7 +251,7 @@ fn discards_internal_create_transfer_on_nonce_overflow() { // Creates a contract with 1 wei from a factory whose nonce cannot be bumped. let factory_code = hex!("60fe600053600160006001f05000"); - let mut db = CacheDB::new(EmptyDB::default()); + let mut db = InMemoryDB::default(); db.load_account(deployer).info.balance = U256::from(u64::MAX); db.insert_account_info( &factory, @@ -286,5 +286,471 @@ fn discards_internal_create_transfer_on_nonce_overflow() { value: U256::from(10), }] ); - assert_eq!(res.tx_result.result.logs.len(), 1); + assert_eq!(evm.inspector().logs(), &[transfer(deployer, factory, 10)]); +} + +fn transfer(from: Address, to: Address, value: u64) -> Log { + Log::new_unchecked( + TRANSFER_LOG_EMITTER, + vec![TRANSFER_EVENT_TOPIC, from.into_word(), to.into_word()], + U256::from(value).to_be_bytes::<32>().into(), + ) +} + +/// A log returned by `go-ethereum`. +enum ExpectedLog { + Transfer(Address, Address, u64), + Emitted(Address, &'static [B256], &'static [u8]), +} + +impl ExpectedLog { + fn to_log(&self) -> Log { + match *self { + Self::Transfer(from, to, value) => transfer(from, to, value), + Self::Emitted(address, topics, data) => { + Log::new_unchecked(address, topics.to_vec(), data.to_vec().into()) + } + } + } +} + +/// A call executed on `go-ethereum`'s `eth_simulateV1` with `traceTransfers` enabled, and the logs +/// it returned. +struct TransferCase { + name: &'static str, + /// Address, code, balance and nonce. + accounts: &'static [(Address, &'static [u8], u64, u64)], + to: Option
, + value: u64, + input: &'static [u8], + expected: &'static [ExpectedLog], +} + +/// Logs returned by `go-ethereum` a0cba163 (`geth --dev`, Osaka) for the same calls. +#[test] +fn simulate_transfer_logs_match_geth() { + let sender = a(0x1001); + for case in CASES { + let mut db = InMemoryDB::default(); + db.load_account(sender).info.balance = U256::from(10u128.pow(18)); + for &(address, code, balance, nonce) in case.accounts { + db.insert_account_info( + &address, + AccountInfo { + balance: U256::from(balance), + nonce, + code: Some(Bytecode::new_legacy(code.to_vec().into())), + ..Default::default() + }, + ); + } + + let context = Context::mainnet().with_db(db).modify_cfg_chained(|c| c.spec = SpecId::OSAKA); + let mut evm = + context.build_mainnet_with_inspector(TransferInspector::new(false).with_logs(true)); + let res = evm + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 5_000_000, + kind: case.to.map_or(TransactTo::Create, TransactTo::Call), + value: U256::from(case.value), + data: case.input.to_vec().into(), + nonce: 0, + ..Default::default() + }) + .unwrap(); + + let expected = case.expected.iter().map(ExpectedLog::to_log).collect::>(); + assert_eq!(evm.inspector().logs(), expected, "{}", case.name); + let transfers = case + .expected + .iter() + .filter_map(|log| match *log { + ExpectedLog::Transfer(from, to, value) => Some((from, to, U256::from(value))), + ExpectedLog::Emitted(..) => None, + }) + .collect::>(); + assert_eq!( + evm.inspector().iter().map(|t| (t.from, t.to, t.value)).collect::>(), + transfers, + "{}", + case.name + ); + // `go-ethereum` keeps the transfer logs in the tracer, so they are not in the receipt. + let receipt_logs = expected + .into_iter() + .filter(|log| log.address != TRANSFER_LOG_EMITTER) + .collect::>(); + assert_eq!(res.tx_result.result.logs, receipt_logs, "{}", case.name); + } +} + +/// Once EIP-7708 is active the EVM logs the transfers itself, and `go-ethereum` stops tracing them. +#[test] +fn simulate_transfer_logs_use_eip7708_logs() { + let sender = a(0x1001); + let contract = a(0xc6); + let mut db = InMemoryDB::default(); + db.load_account(sender).info.balance = U256::from(10u128.pow(18)); + db.insert_account_info( + &contract, + AccountInfo { + balance: U256::from(7), + code: Some(Bytecode::new_legacy(bytes!( + "73000000000000000000000000000000000000a002ff" + ))), + ..Default::default() + }, + ); + + let context = Context::mainnet().with_db(db).modify_cfg_chained(|c| c.spec = SpecId::AMSTERDAM); + let mut evm = + context.build_mainnet_with_inspector(TransferInspector::new(false).with_logs(true)); + let res = evm + .inspect_tx(TxEnv { + caller: sender, + gas_limit: 5_000_000, + kind: TransactTo::Call(contract), + value: U256::from(5), + nonce: 0, + ..Default::default() + }) + .unwrap(); + + assert!(res.result.is_success()); + assert_eq!(evm.inspector().transfers().len(), 2); + assert_eq!(res.tx_result.result.logs.len(), 2); + assert_eq!(evm.inspector().logs(), res.tx_result.result.logs); +} + +const CASES: &[TransferCase] = &[ + TransferCase { + name: "tx_value", + accounts: &[(a(0xc1), &hex!(""), 0, 0)], + to: Some(a(0xc1)), + value: 10, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0x1001), a(0xc1), 10)], + }, + TransferCase { + name: "tx_value_zero", + accounts: &[(a(0xc1), &hex!(""), 0, 0)], + to: Some(a(0xc1)), + value: 0, + input: &hex!(""), + expected: &[], + }, + TransferCase { + name: "callcode_value", + accounts: &[( + a(0xc2), + &hex!("6000600060006000600373000000000000000000000000000000000000a00361fffff25000"), + 100, + 0, + )], + to: Some(a(0xc2)), + value: 0, + input: &hex!(""), + expected: &[], + }, + TransferCase { + name: "delegatecall_log", + accounts: &[ + ( + a(0xc4), + &hex!("60006000600060007300000000000000000000000000000000000000c561fffff450"), + 0, + 0, + ), + (a(0xc5), &hex!("601160006000a1"), 0, 0), + ], + to: Some(a(0xc4)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted(a(0xc4), &[B256::with_last_byte(0x11)], &hex!(""))], + }, + TransferCase { + name: "selfdestruct_balance", + accounts: &[(a(0xc6), &hex!("73000000000000000000000000000000000000a002ff"), 7, 0)], + to: Some(a(0xc6)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xc6), a(0xa002), 7)], + }, + TransferCase { + name: "selfdestruct_tx_value", + accounts: &[(a(0xc6), &hex!("73000000000000000000000000000000000000a002ff"), 7, 0)], + to: Some(a(0xc6)), + value: 5, + input: &hex!(""), + expected: &[ + ExpectedLog::Transfer(a(0x1001), a(0xc6), 5), + ExpectedLog::Transfer(a(0xc6), a(0xa002), 12), + ], + }, + TransferCase { + name: "selfdestruct_zero", + accounts: &[(a(0xc7), &hex!("73000000000000000000000000000000000000a002ff"), 0, 0)], + to: Some(a(0xc7)), + value: 0, + input: &hex!(""), + expected: &[], + }, + TransferCase { + name: "selfdestruct_self", + accounts: &[(a(0xc8), &hex!("7300000000000000000000000000000000000000c8ff"), 4, 0)], + to: Some(a(0xc8)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xc8), a(0xc8), 4)], + }, + TransferCase { + name: "selfdestruct_reverted_frame", + accounts: &[ + ( + a(0xc9), + &hex!( + "600060006000600060027300000000000000000000000000000000000000ca61fffff1506000600060006000600173000000000000000000000000000000000000a00161fffff15000" + ), + 50, + 0, + ), + ( + a(0xca), + &hex!( + "600060006000600060007300000000000000000000000000000000000000c661fffff15060006000fd" + ), + 50, + 0, + ), + (a(0xc6), &hex!("73000000000000000000000000000000000000a002ff"), 7, 0), + ], + to: Some(a(0xc9)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xc9), a(0xa001), 1)], + }, + TransferCase { + name: "logs_interleaved", + accounts: &[ + ( + a(0xcb), + &hex!( + "602160006000a16000600060006000600173000000000000000000000000000000000000a00161fffff15060006000a0600060006000600060017300000000000000000000000000000000000000cc61fffff1506000600060006000600273000000000000000000000000000000000000a00161fffff150602260006000a1" + ), + 50, + 0, + ), + ( + a(0xcc), + &hex!( + "602360006000a16000600060006000600173000000000000000000000000000000000000a00161fffff15060006000fd" + ), + 50, + 0, + ), + ], + to: Some(a(0xcb)), + value: 0, + input: &hex!(""), + expected: &[ + ExpectedLog::Emitted(a(0xcb), &[B256::with_last_byte(0x21)], &hex!("")), + ExpectedLog::Transfer(a(0xcb), a(0xa001), 1), + ExpectedLog::Emitted(a(0xcb), &[], &hex!("")), + ExpectedLog::Transfer(a(0xcb), a(0xa001), 2), + ExpectedLog::Emitted(a(0xcb), &[B256::with_last_byte(0x22)], &hex!("")), + ], + }, + TransferCase { + name: "toplevel_revert", + accounts: &[( + a(0xcd), + &hex!( + "603160006000a16000600060006000600173000000000000000000000000000000000000a00161fffff15060006000fd" + ), + 50, + 0, + )], + to: Some(a(0xcd)), + value: 3, + input: &hex!(""), + expected: &[], + }, + TransferCase { + name: "create_tx_value", + accounts: &[], + to: None, + value: 10, + input: &hex!("600060005360016000f3"), + expected: &[ExpectedLog::Transfer( + a(0x1001), + address!("0x3817e247023b4f489352758397040b1fd33b300a"), + 10, + )], + }, + TransferCase { + name: "nested_create", + accounts: &[(a(0xce), &hex!("6000600053600160006005f05000"), 50, 0)], + to: Some(a(0xce)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer( + a(0xce), + address!("0x351888951695dc68547cfda22ae5d8bae8ab3b7a"), + 5, + )], + }, + TransferCase { + name: "nested_create2", + accounts: &[(a(0xcf), &hex!("60006000536007600160006003f55000"), 50, 0)], + to: Some(a(0xcf)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer( + a(0xcf), + address!("0xbc77ab833843214da7525df5458cc0a1392deee5"), + 3, + )], + }, + TransferCase { + name: "nested_create_failed", + accounts: &[(a(0xd0), &hex!("60fe600053600160006005f05060006000a000"), 50, 0)], + to: Some(a(0xd0)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted(a(0xd0), &[], &hex!(""))], + }, + TransferCase { + name: "insufficient_balance_call", + accounts: &[( + a(0xd1), + &hex!( + "60006000600060006103e873000000000000000000000000000000000000a00161fffff15060006000a0" + ), + 50, + 0, + )], + to: Some(a(0xd1)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted(a(0xd1), &[], &hex!(""))], + }, + TransferCase { + name: "precompile_value", + accounts: &[( + a(0xd2), + &hex!("6000600060006000600173000000000000000000000000000000000000000461fffff15000"), + 50, + 0, + )], + to: Some(a(0xd2)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xd2), a(0x4), 1)], + }, + TransferCase { + name: "eoa_and_zero", + accounts: &[( + a(0xd3), + &hex!( + "6000600060006000600073000000000000000000000000000000000000a00161fffff1506000600060006000600173000000000000000000000000000000000000a00161fffff150600060006000600073000000000000000000000000000000000000a00161fffffa5000" + ), + 50, + 0, + )], + to: Some(a(0xd3)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xd3), a(0xa001), 1)], + }, + TransferCase { + name: "callcode_selfdestruct_value", + accounts: &[ + ( + a(0xd4), + &hex!( + "600060006000600060017300000000000000000000000000000000000000c662030000f25000" + ), + 50, + 0, + ), + (a(0xc6), &hex!("73000000000000000000000000000000000000a002ff"), 7, 0), + (a(0xa002), &hex!(""), 1, 0), + ], + to: Some(a(0xd4)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Transfer(a(0xd4), a(0xa002), 50)], + }, + TransferCase { + name: "create_tx_failed", + accounts: &[], + to: None, + value: 10, + input: &hex!("fe"), + expected: &[], + }, + TransferCase { + name: "nested_create_nonce_overflow", + accounts: &[( + a(0xd5), + &hex!("6000600053600160006005f05060006000a000"), + 50, + 18446744073709551615, + )], + to: Some(a(0xd5)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted(a(0xd5), &[], &hex!(""))], + }, + TransferCase { + name: "invalid_child", + accounts: &[ + ( + a(0xd6), + &hex!( + "600060006000600060007300000000000000000000000000000000000000d761fffff15060006000a000" + ), + 50, + 0, + ), + ( + a(0xd7), + &hex!( + "604160006000a16000600060006000600173000000000000000000000000000000000000a00161fffff150fe" + ), + 50, + 0, + ), + ], + to: Some(a(0xd6)), + value: 0, + input: &hex!(""), + expected: &[ExpectedLog::Emitted(a(0xd6), &[], &hex!(""))], + }, + TransferCase { + name: "create_tx_code_too_large", + accounts: &[], + to: None, + value: 10, + input: &hex!("6160015ff3"), + expected: &[], + }, +]; + +#[test] +fn logs_outside_any_frame_are_kept() { + // Embedders emit logs before the first call or after the last, e.g. a fee transfer. + let spec = evm2::SpecId::CANCUN; + let mut host = Evm::::new( + spec, + Default::default(), + ethereum_tx_registry(spec), + InMemoryDB::default(), + Precompiles::base(spec), + ); + let mut inspector = TransferInspector::new(false).with_logs(true); + let log = Log::new_unchecked(a(1), vec![TRANSFER_EVENT_TOPIC], Bytes::new()); + Inspector::::log(&mut inspector, &log, &mut host); + assert_eq!(inspector.logs(), [log]); } diff --git a/crates/inspectors/tests/it/unlisted_precompiles.rs b/crates/inspectors/tests/it/unlisted_precompiles.rs new file mode 100644 index 000000000..0675f2740 --- /dev/null +++ b/crates/inspectors/tests/it/unlisted_precompiles.rs @@ -0,0 +1,118 @@ +//! Precompiles a provider dispatches to without listing them, like Tempo's token precompiles, +//! trace like contracts. + +use alloy_primitives::{Address, Bytes, TxKind, U256, hex}; +use alloy_rpc_types_eth::TransactionInfo; +use alloy_rpc_types_trace::parity::Action; +use evm2::{ + BaseEvmTypes, Evm, ExecutionConfig, Precompiles, SpecId, Version, + bytecode::Bytecode, + ethereum::{ethereum_tx_registry, execute_initial_frame, prepare_initial_frame}, + evm::{AccountInfo, InMemoryDB}, + interpreter::{GasTracker, Message}, + precompile::{PrecompileOutput, PrecompileProvider}, + precompiles::{MovePrecompileError, PrecompileId, PrecompileResult}, +}; +use evm2_inspectors::tracing::{FourByteInspector, TracingInspector, TracingInspectorConfig}; + +const CALLER: Address = Address::repeat_byte(0xaa); +/// Calls `ON_DEMAND` and the identity precompile with the selector `0xaabbccdd`, then stops. +const PROBE: Address = Address::with_last_byte(0x45); +/// A precompile the provider dispatches to without listing it. +const ON_DEMAND: Address = Address::repeat_byte(0x20); + +/// Runs a call to `PROBE` under `inspector`. +fn run_probe>(inspector: &mut I) { + let mut db = InMemoryDB::default(); + let code = hex!( + // mem[0..4] = 0xaabbccdd + "63aabbccdd60e01b5f52" + // CALL(gas, ON_DEMAND, 0, 0, 4, 0, 0) + "5f5f60045f5f7320202020202020202020202020202020202020205af150" + // CALL(gas, identity, 0, 0, 4, 0, 0) + "5f5f60045f5f60045af15000" + ); + db.insert_account_info( + &PROBE, + AccountInfo::default().with_code(Bytecode::new_raw(Bytes::copy_from_slice(&code))), + ); + let mut evm = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(SpecId::CANCUN, Version::new(SpecId::CANCUN)), + SpecId::CANCUN, + Default::default(), + ethereum_tx_registry(SpecId::CANCUN), + db, + OnDemandPrecompiles(Precompiles::base(SpecId::CANCUN)), + ); + evm.set_inspector(inspector); + let mut gas = GasTracker::new(100_000); + let frame = prepare_initial_frame( + &mut evm, + CALLER, + 0, + TxKind::Call(PROBE), + &Bytes::new(), + U256::ZERO, + &mut gas, + ) + .unwrap(); + assert!(execute_initial_frame(&mut evm, frame, &mut gas, 100_000, 0).unwrap().is_success()); +} + +#[test] +fn unlisted_precompiles_trace_like_contracts() { + let mut tracer = TracingInspector::new(TracingInspectorConfig::default_parity()); + run_probe(&mut tracer); + let targets: Vec<_> = tracer + .into_parity_builder() + .into_localized_transaction_traces(TransactionInfo::default()) + .into_iter() + .map(|trace| match trace.trace.action { + Action::Call(call) => (trace.trace.trace_address, call.to), + action => panic!("unexpected action {action:?}"), + }) + .collect(); + // The zero-value identity call is left out; the unlisted precompile is not. + assert_eq!(targets, [(vec![], PROBE), (vec![0], ON_DEMAND)]); + + let mut fourbyte = FourByteInspector::default(); + run_probe(&mut fourbyte); + let selectors: Vec<_> = fourbyte.inner().iter().map(|(key, count)| (*key, *count)).collect(); + assert_eq!(selectors, [((hex!("aabbccdd").into(), 0), 1)]); +} + +/// The base precompiles, plus [`ON_DEMAND`], which is dispatched to but not listed. +struct OnDemandPrecompiles(Precompiles); + +impl PrecompileProvider for OnDemandPrecompiles { + fn addresses(&self) -> Vec
{ + self.0.addresses() + } + + fn precompile_ids(&self) -> Vec<(Address, PrecompileId)> { + self.0.precompile_ids() + } + + fn contains(&self, address: &Address) -> bool { + *address == ON_DEMAND || self.0.contains(address) + } + + fn move_precompiles( + &mut self, + moves: &[(Address, Address)], + ) -> Result<(), MovePrecompileError> { + self.0.move_precompiles(moves) + } + + fn execute( + &mut self, + evm: &mut Evm<'_, BaseEvmTypes>, + message: &Message, + gas: &mut GasTracker, + ) -> Option { + if message.call_target == ON_DEMAND { + return Some(Ok(PrecompileOutput::new(Bytes::new()))); + } + self.0.execute(evm, message, gas) + } +} diff --git a/crates/inspectors/tests/it/utils.rs b/crates/inspectors/tests/it/utils.rs index 89f685d4b..2b44cc645 100644 --- a/crates/inspectors/tests/it/utils.rs +++ b/crates/inspectors/tests/it/utils.rs @@ -3,6 +3,7 @@ use colorchoice::ColorChoice; use evm2_inspectors::tracing::{TraceWriter, TraceWriterConfig, TracingInspector}; pub use crate::compat::*; +pub(crate) use evm2::utils::u64_to_address as a; pub fn write_traces(tracer: &TracingInspector) -> String { write_traces_with(tracer, TraceWriterConfig::new().color_choice(ColorChoice::Never)) diff --git a/crates/inspectors/tests/it/writer.rs b/crates/inspectors/tests/it/writer.rs index 981a6191b..5bcdafeba 100644 --- a/crates/inspectors/tests/it/writer.rs +++ b/crates/inspectors/tests/it/writer.rs @@ -1,10 +1,11 @@ use crate::utils::{ - AccountInfo, CacheDB, Context, EmptyDB, SpecId, TransactTo, TxEnv, inspect_deploy_contract, - write_traces, write_traces_with, + AccountInfo, Context, SpecId, TransactTo, TxEnv, inspect_deploy_contract, write_traces, + write_traces_with, }; use alloy_primitives::{Address, B256, U256, address, b256, bytes, hex}; use alloy_sol_types::{SolCall, sol}; use colorchoice::ColorChoice; +use evm2::evm::InMemoryDB; use evm2_inspectors::tracing::{ TraceWriterConfig, TracingInspector, TracingInspectorConfig, types::{DecodedCallData, DecodedInternalCall, DecodedTraceStep}, @@ -23,7 +24,7 @@ fn test_trace_printing() { let base_path = &Path::new(OUT_DIR).join("test_trace_printing"); let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .build_mainnet_with_inspector(TracingInspector::new(TracingInspectorConfig::all())); //let address = evm.deploy(CREATION_CODE.parse().unwrap(), &mut tracer).unwrap(); @@ -92,7 +93,7 @@ fn deploy_fail() { let base_path = &Path::new(OUT_DIR).join("deploy_fail"); let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .build_mainnet_with_inspector(TracingInspector::new(TracingInspectorConfig::all())); inspect_deploy_contract( @@ -312,8 +313,8 @@ fn test_receive_vs_fallback_empty_calldata() { "00" // Runtime code: STOP ); - let caller = address!("0x0000000000000000000000000000000000000001"); - let mut db = CacheDB::new(EmptyDB::default()); + let caller = Address::with_last_byte(1); + let mut db = InMemoryDB::default(); db.insert_account_info( &caller, AccountInfo { balance: U256::from(1_000_000), ..Default::default() }, @@ -334,7 +335,7 @@ fn test_receive_vs_fallback_empty_calldata() { TxEnv::builder() .caller(caller) .data(bytes!()) // Empty calldata. - .value(U256::from(1)) // Nonzero value. + .value(U256::ONE) // Nonzero value. .kind(TransactTo::Call(address)) .gas_priority_fee(None) .nonce(1) @@ -366,7 +367,7 @@ fn test_fallback_short_calldata() { ); let mut evm = Context::mainnet() - .with_db(CacheDB::new(EmptyDB::default())) + .with_db(InMemoryDB::default()) .build_mainnet_with_inspector(TracingInspector::new(TracingInspectorConfig::all())); let address = inspect_deploy_contract(&mut evm, initcode, Address::default(), SpecId::CANCUN) diff --git a/crates/jit/AGENTS.md b/crates/jit/AGENTS.md index 3c0991fa7..848306393 100644 --- a/crates/jit/AGENTS.md +++ b/crates/jit/AGENTS.md @@ -8,8 +8,8 @@ Use the root commands for lint, format, and docs. Targeted JIT commands: cargo nextest run -p evm2-jit-codegen --test codegen # compiler tests EVM2_JIT_TEST_DUMP=1 cargo nextest run -p evm2-jit-codegen --test codegen "test_name" # dump single compiler test cargo nextest run -p evm2-jit-runtime # runtime tests -cargo st "statetests::devnet::jit" # EEST JIT state tests -cargo st "blockchain_tests::devnet::jit" # EEST JIT blockchain tests +cargo st "statetests::eest::jit" # EEST JIT state tests +cargo st "blockchain_tests::eest::jit" # EEST JIT blockchain tests ``` ## Architecture diff --git a/crates/jit/EXPERIMENTS.md b/crates/jit/EXPERIMENTS.md index 28182a41f..d49879dda 100644 --- a/crates/jit/EXPERIMENTS.md +++ b/crates/jit/EXPERIMENTS.md @@ -27,6 +27,15 @@ ones here with the numbers that rejected them. - Dropping `relieve_vstack_pressure`, so the virtual stack only stores at section boundaries and builtin calls: i256 stores -23%, but JIT size +13%, spills ×6 and runtime instructions +9.6% (onchain_lm_v2 +13.5%). +- Making `single_error = false` the default, so the JIT reports the + interpreter's halt reason instead of `OutOfGas`: `opt.s` changed in 23/31 + benchmarks, JIT size +4.0% total, compile instructions +10.7% total (geomean + +6.1%) and runtime instructions +3.8% total (geomean +0.5%; `onchain_lm_v2` + +4.3%, `burntpix` +4.2%, `snailtracer` +2.8%). +- Branch weights on the `MRESIZE` check (`then` block cold): moving the resize + call out of line made `opt.s` change in 28/31 benchmarks, JIT size + +1.8% total (geomean +1.3%), spills +3.9%, compile instructions +0.8% and + runtime instructions +0.8% (cycles +1.4%). ## Optimization passes @@ -47,6 +56,47 @@ ones here with the numbers that rejected them. - LICM at every function size: opt instructions +75% on the mainnet corpus, JIT size +0.4%, reloads +12%. No LICM at all: JIT size -0.1%, but hash_10k runtime +1.2%. +- LICM at every size, and the old gate of 4000 blocks summed over the module (the + largest function's block count plus two, on every benchmark). The gate is now + `LICM_MAX_BLOCKS` in `optimize_module`: 1000 blocks in the largest function. + Sweep: the pipeline with and without the LICM group (`EVM2_JIT_PASSES`) on the 31 + benchmarks and on the 1371 distinct contract codes of 32 bytes or more in the + pre-state of `data/mainnet-25347446-25347455.bin.zst`, with compile instructions + (`perf stat`, median of 3) and JIT size per contract, binned by the largest + function's block count in `unopt.ll`. The last column is the mean per contract: + + | Largest function (blocks) | Contracts | Compile instructions | JIT size | Extra instructions | + | --- | ---: | ---: | ---: | ---: | + | ≤250 | 111 | +0.8% | +3.2% | 0M | + | 251–500 | 33 | +3.5% | -0.0% | 4M | + | 501–1000 | 91 | +5.2% | +0.5% | 12M | + | 1001–2000 | 164 | +8.0% | +0.9% | 37M | + | 2001–4000 | 217 | +8.8% | +1.0% | 87M | + | 4001–8000 | 298 | +10.3% | +1.1% | 216M | + | >8000 | 457 | +20.2% | +0.7% | 1170M | + + The extra cost per block rises from about 15k instructions at 500–1000 blocks to + 30–36k at 2000–8000 and 80k above 8000, so it is mildly super-linear and steepest + in the largest functions. It is not a cliff: snailtracer (8007 blocks) pays +37% + compile instructions, seaport (17620) +32%, balancer_vault (20019) +82%. + + Runtime instructions per execution with LICM, on the benchmarks: fibonacci -8.3% + and factorial -7.8% (43 blocks), hash_10k -1.0% (133), bswap64 +0.9% and + bswap64_opt +1.2% (about 250), every other function of at most 261 blocks within + ±1%. From 897 blocks up (22 benchmarks) the median is +0.02% and the range -1.7% + (balancer_vault) to +1.2% (curve_stableswap); burntpix (1248 blocks) and + snailtracer (8007), the two compute loops in that range, change by +0.2% and + +0.04%. JIT size is 0.5–1.1% larger with LICM above 500 blocks. + + So LICM pays only on loops in small functions. Above 1000 blocks it costs 8% or + more compile instructions and about 1% more code for no measured runtime gain. + Up to 1000 blocks it costs 3.5–5.2% (at most 12M instructions per contract) and + stays: the benchmarks have no function between 262 and 896 blocks, so the + runtime effect there is unmeasured, and a cutoff of 500 would save at most that + 5.2% on 91 contracts. Across the corpus the new gate saves almost no total + compile cost against the old one (+0.0% over no LICM at all, against +0.7%, + since the largest functions dominate the total) but removes the 8–9% cost from + the 381 contracts with 1001–4000 blocks. - `bdce`, `aggressive-instcombine`, `mldst-motion`, `dfa-jump-threading`, and `simplifycfg` with `switch-range-to-icmp`, `switch-to-arithmetic`, `forward-switch-cond` or `no-keep-loops` change at most one benchmark. diff --git a/crates/jit/backend/src/traits.rs b/crates/jit/backend/src/traits.rs index ffb155323..b17d96fb8 100644 --- a/crates/jit/backend/src/traits.rs +++ b/crates/jit/backend/src/traits.rs @@ -48,9 +48,9 @@ impl Default for BackendConfig { opt_level: OptimizationLevel::Default, is_dumping: false, debug_assertions: cfg!(debug_assertions), - debug_support: true, + debug_support: false, profiling_support: false, - simple_perf: true, + simple_perf: false, debug_file: None, } } @@ -134,6 +134,9 @@ pub enum Attribute { AlwaysInline, NoInline, Speculatable, + /// `"probe-stack"="inline-asm"` — touches every page of a frame larger than a page, so that + /// a stack overflow faults on the guard page instead of skipping past it. + InlineStackProbes, // Parameter attributes. NoAlias, @@ -294,7 +297,6 @@ pub trait Builder: BackendTypes + TypeMethods { fn switch_to_block(&mut self, block: Self::BasicBlock); fn seal_block(&mut self, block: Self::BasicBlock); fn seal_all_blocks(&mut self); - fn set_current_block_cold(&mut self); fn current_block(&mut self) -> Option; fn block_addr(&mut self, block: Self::BasicBlock) -> Option; @@ -527,9 +529,9 @@ mod tests { assert_eq!(config.opt_level, OptimizationLevel::Default); assert!(!config.is_dumping); assert_eq!(config.debug_assertions, cfg!(debug_assertions)); - assert!(config.debug_support); + assert!(!config.debug_support); assert!(!config.profiling_support); - assert!(config.simple_perf); + assert!(!config.simple_perf); assert_eq!(config.debug_file, None); } diff --git a/crates/jit/builtins/src/ir.rs b/crates/jit/builtins/src/ir.rs index ac9fcbe04..a7650d23f 100644 --- a/crates/jit/builtins/src/ir.rs +++ b/crates/jit/builtins/src/ir.rs @@ -225,6 +225,7 @@ macro_rules! builtins { const PANIC: u8 = _0_0; const ASSERTSPECID: u8 = _0_0; + const POW: u8 = EXP; const KECCAK256CC: u8 = _0_1; const CALLDATALOADC: u8 = _0_1; @@ -314,6 +315,7 @@ builtins! { AddMod = __revmc_builtin_addmod(@[sp] ptr) None, MulMod = __revmc_builtin_mulmod(@[sp] ptr) None, Exp = __revmc_builtin_exp(@[ecx] ptr, @[sp] ptr) None, + Pow = __revmc_builtin_pow(@[sp] ptr) None, Keccak256 = __revmc_builtin_keccak256(@[ecx] ptr, @[sp] ptr) None, Keccak256CC = __revmc_builtin_keccak256_cc(@[ecx] ptr, @[sp] ptr, usize, usize) None, Balance = __revmc_builtin_balance(@[ecx] ptr, @[sp] ptr) None, diff --git a/crates/jit/builtins/src/lib.rs b/crates/jit/builtins/src/lib.rs index 5b3296ae7..c0f5709fc 100644 --- a/crates/jit/builtins/src/lib.rs +++ b/crates/jit/builtins/src/lib.rs @@ -18,7 +18,10 @@ use evm2::{ BaseEvmTypes, EvmTypesHost, bytecode::Bytecode, constants::BLOCK_HASH_HISTORY, - interpreter::{Host, MessageExt, MessageKind, Word, derive_create_destination, i256}, + interpreter::{ + Host, MessageExt, MessageKind, Word, create_preaccess_failure, derive_create_destination, + i256, + }, utils::read_word_padded, version::{EvmFeatures, GasId}, }; @@ -142,13 +145,19 @@ pub unsafe fn __revmc_builtin_exp( ecx: &mut EvmContext<'_, '_, '_, T>, sp: &mut [EvmWord; 2], ) -> BuiltinResult { - let rev![base, exponent_ptr] = sp; - let exponent = exponent_ptr.to_u256(); - ecx.gas.spend(ecx.gas_params().exp_cost(exponent))?; - *exponent_ptr = base.to_u256().pow(exponent).into(); + let rev![_, exponent] = &*sp; + ecx.gas.spend(ecx.gas_params().exp_cost(exponent.to_u256()))?; + unsafe { __revmc_builtin_pow::(sp) }; Ok(()) } +/// `EXP` without gas, for compiled code that already charged the dynamic gas of a constant +/// exponent in its section. +#[inline] +pub unsafe fn __revmc_builtin_pow(rev![base, exponent]: &mut [EvmWord; 2]) { + *exponent = base.to_u256().pow(exponent.to_u256()).into(); +} + #[inline] pub unsafe fn __revmc_builtin_keccak256( ecx: &mut EvmContext<'_, '_, '_, T>, @@ -201,7 +210,7 @@ pub unsafe fn __revmc_builtin_calldataload( ecx: &EvmContext<'_, '_, '_, T>, offset_ptr: &mut EvmWord, ) { - do_calldataload(ecx, offset_ptr, word_to_usize_saturated(offset_ptr.to_u256())); + do_calldataload(ecx, offset_ptr, offset_ptr.to_u256().saturating_to::()); } #[inline] @@ -232,8 +241,8 @@ pub unsafe fn __revmc_builtin_calldatacopy( ecx.gas.spend(ecx.gas_params().copy_cost(len))?; let memory_offset = word_to_usize(memory_offset.to_u256())?; ensure_memory(ecx, memory_offset, len)?; - let data_offset = word_to_usize_saturated(data_offset.to_u256()); - let input = ecx.input().as_ref(); + let data_offset = data_offset.to_u256().saturating_to::(); + let input = ecx.input(); let input = unsafe { core::slice::from_raw_parts(input.as_ptr(), input.len()) }; ecx.memory_mut().set_data(memory_offset, data_offset, len, input); } @@ -278,7 +287,7 @@ pub unsafe fn __revmc_builtin_extcodecopy( let code = account.code.original_bytes(); let code_offset_usize = - core::cmp::min(word_to_usize_saturated(code_offset.to_u256()), code.len()); + core::cmp::min(code_offset.to_u256().saturating_to::(), code.len()); ecx.memory_mut().set_data(memory_offset_usize, code_offset_usize, len, &code); Ok(()) } @@ -290,7 +299,7 @@ pub unsafe fn __revmc_builtin_returndatacopy( ) -> BuiltinResult { let rev![memory_offset, offset, len] = sp; let len = word_to_usize(len.to_u256())?; - let data_offset = word_to_usize_saturated(offset.to_u256()); + let data_offset = offset.to_u256().saturating_to::(); // Bounds check before charging gas. let data_end = data_offset.saturating_add(len); @@ -335,7 +344,7 @@ pub unsafe fn __revmc_builtin_blockhash( return Ok(()); }; - let diff = word_to_u64_saturated(diff); + let diff = diff.saturating_to::(); // Current block returns 0 if diff == 0 { @@ -389,7 +398,7 @@ pub unsafe fn __revmc_builtin_blob_hash( index_ptr: &mut EvmWord, ) { let index = index_ptr.to_u256(); - let index_usize = word_to_usize_saturated(index); + let index_usize = index.saturating_to::(); *index_ptr = ecx.tx_env().blob_hashes.get(index_usize).copied().unwrap_or_default().into(); } @@ -516,12 +525,35 @@ pub unsafe fn __revmc_builtin_host_instruction( ecx.interpreter_mut().set_gas(gas); let result = unsafe { ecx.interpreter_mut().run_external_instruction(pc, len) }; ecx.gas = ecx.interpreter().gas(); - ecx.return_data_len = ecx.interpreter().return_data().len(); + ecx.return_data_len = ecx.return_data().len(); ecx.refresh_memory_cache(); let new_len = result?; - // Compiled code relies on memory never shrinking, and on the opcode's stack effect. - if new_len != len - inputs + outputs || ecx.memory().len() < memory_len { - return Err(InstrStop::FatalExternalError.into()); + // Compiled code relies on the opcode's stack effect, and on memory never shrinking. + let expected_len = len - inputs + outputs; + if new_len != expected_len { + return Err(ecx + .interpreter_mut() + .fail_host_instruction( + pc, + format_args!( + "left {new_len} stack items, but compiled code expects {expected_len} from \ + the opcode's fixed inputs and outputs" + ), + ) + .into()); + } + let new_memory_len = ecx.memory().len(); + if new_memory_len < memory_len { + return Err(ecx + .interpreter_mut() + .fail_host_instruction( + pc, + format_args!( + "shrank memory from {memory_len} to {new_memory_len} bytes, but compiled \ + code expects memory never to shrink" + ), + ) + .into()); } Ok(()) } @@ -652,9 +684,14 @@ pub unsafe fn __revmc_builtin_create( if ecx.enables(EvmFeatures::EIP8037) { // `caller_info` is always `Some` when EIP-8037 is enabled. if let Some(caller_info) = caller_info.as_ref() - && (caller_info.balance < value - || caller_info.nonce == u64::MAX - || depth > ecx.version().max_call_depth()) + && create_preaccess_failure( + caller_info.balance, + caller_info.nonce, + value, + depth, + ecx.host().initial_call_depth(), + ) + .is_some() { unsafe { sp.write(EvmWord::ZERO); @@ -667,8 +704,9 @@ pub unsafe fn __revmc_builtin_create( // failures return above before the destination is accessed. if ecx .host() - .target_is_empty_for_new_account_gas(&destination, version.features) + .load_account(&destination, false, false) .map_err(|error| ecx.interpreter_mut().fail(error))? + .is_empty_for_new_account_gas { ecx.gas.spend_state(version.gas_params.create_state_gas())?; charged_create_state_gas = true; @@ -681,7 +719,6 @@ pub unsafe fn __revmc_builtin_create( } ecx.gas.spend(gas_limit)?; - let tx_env = ecx.tx_env(); let mut message = MessageExt { kind, depth, @@ -691,9 +728,9 @@ pub unsafe fn __revmc_builtin_create( call_target: destination, caller, code: Bytecode::new_legacy(code.clone()), - input: code, + input: code.into(), value, - code_address: caller, + code_address: destination, caller_is_static: false, salt, ext: T::MessageExt::default(), @@ -701,7 +738,7 @@ pub unsafe fn __revmc_builtin_create( }; let mut result = ecx .host() - .execute_message(tx_env, &mut message) + .execute_message(&mut message) .map_err(|error| ecx.interpreter_mut().fail(error))?; if result.stop.is_fatal() { return Err(result.stop.into()); @@ -709,17 +746,14 @@ pub unsafe fn __revmc_builtin_create( ecx.gas.merge_child_gas(result.gas, result.stop); // EIP-8037: refund the conditional create state gas when the create fails // to deploy (no new account leaf is created). - let create_failed = result.created_address.is_none() || !result.stop.is_success(); + let create_failed = result.created_address.is_none() || !result.is_success(); if charged_create_state_gas && create_failed { ecx.gas.refill_reservoir(version.gas_params.create_state_gas()); } let address = EvmWord::from(result.created_address_for_parent()); - let return_data = if result.stop == InstrStop::Revert { - core::mem::take(&mut result.output) - } else { - Bytes::new() - }; + let return_data = + if result.stop.is_revert() { core::mem::take(&mut result.output) } else { Bytes::new() }; unsafe { sp.write(address); } @@ -743,30 +777,30 @@ pub unsafe fn __revmc_builtin_call( let local_gas_limit = local_gas_limit.to_u256(); let to = to.to_address(); - let local_gas_limit = word_to_u64_saturated(local_gas_limit); + let local_gas_limit = local_gas_limit.saturating_to::(); let value = match call_kind { CallKind::Call | CallKind::CallCode => { pop!(sp; value); let value = value.to_u256(); - if call_kind == CallKind::Call && ecx.is_static() && value != U256::ZERO { + if call_kind == CallKind::Call && ecx.is_static() && !value.is_zero() { return Err(InstrStop::CallNotAllowedInsideStatic.into()); } value } CallKind::DelegateCall | CallKind::StaticCall => U256::ZERO, }; - let transfers_value = value != U256::ZERO; + let transfers_value = !value.is_zero(); pop!(sp; in_offset, in_len, out_offset, out_len); let in_len = word_to_usize(in_len.to_u256())?; - let input = if in_len != 0 { + let in_offset = if in_len != 0 { let in_offset = word_to_usize(in_offset.to_u256())?; ensure_memory(ecx, in_offset, in_len)?; - Bytes::copy_from_slice(ecx.memory().slice(in_offset, in_len)) + in_offset } else { - Bytes::new() + 0 }; let out_len = word_to_usize(out_len.to_u256())?; @@ -786,6 +820,7 @@ pub unsafe fn __revmc_builtin_call( call_kind == CallKind::Call, local_gas_limit, )?; + let input = ecx.interpreter().call_input(in_offset, in_len); let current = ecx.message(); let (destination, caller, call_value, code_address) = match call_kind { @@ -816,16 +851,15 @@ pub unsafe fn __revmc_builtin_call( _non_exhaustive: (), }; - let tx_env = ecx.tx_env(); let mut result = ecx .host() - .execute_message(tx_env, &mut message) + .execute_message(&mut message) .map_err(|error| ecx.interpreter_mut().fail(error))?; if result.stop.is_fatal() { return Err(result.stop.into()); } ecx.gas.merge_child_gas(result.gas, result.stop); - if new_account_state_gas != 0 && !result.stop.is_success() { + if new_account_state_gas != 0 && !result.is_success() { ecx.gas.refill_reservoir(new_account_state_gas); } @@ -833,7 +867,7 @@ pub unsafe fn __revmc_builtin_call( if copy_len != 0 { ecx.memory_mut().set(out_offset, &result.output[..copy_len]); } - let success = EvmWord::from(Word::from(u8::from(result.stop.is_success()))); + let success = EvmWord::from(Word::from(u8::from(result.is_success()))); unsafe { sp.write(success); } @@ -895,14 +929,9 @@ fn load_acc_and_calc_gas( code_address = delegated_address; } let features = ecx.version().features; - // Check emptiness only when new-account gas can apply: after EIP-161 a zero-value call never - // pays it, so it skips the extra account lookup. if create_empty_account && (!features.contains(EvmFeatures::EIP161) || transfers_value) - && ecx - .host() - .target_is_empty_for_new_account_gas(&to, features) - .map_err(|error| ecx.interpreter_mut().fail(error))? + && account.is_empty_for_new_account_gas { cost += version.gas_params.get(GasId::NewAccountCost); if features.contains(EvmFeatures::EIP8037) && transfers_value { @@ -934,21 +963,8 @@ pub unsafe fn __revmc_builtin_do_return( ) -> BuiltinResult { let rev![offset, len] = sp; let len = word_to_usize(len.to_u256())?; - let output = if len != 0 { - let offset = word_to_usize(offset.to_u256())?; - let Some(end) = offset.checked_add(len) else { - return Err(InstrStop::MemoryOOG.into()); - }; - if end > u32::MAX as usize { - return Err(InstrStop::MemoryLimitOOG.into()); - } - ensure_memory(ecx, offset, len)?; - offset as u32..end as u32 - } else { - 0..0 - }; - ecx.interpreter_mut().set_output(output); - Err(result.into()) + let offset = if len != 0 { word_to_usize(offset.to_u256())? } else { 0 }; + do_return_range(ecx, offset, len, result) } #[inline] @@ -958,15 +974,20 @@ pub unsafe fn __revmc_builtin_do_return_cc( len: u64, result: InstrStop, ) -> BuiltinResult { - let offset = offset as usize; - let len = len as usize; + do_return_range(ecx, offset as usize, len as usize, result) +} + +#[inline] +fn do_return_range( + ecx: &mut EvmContext<'_, '_, '_, T>, + offset: usize, + len: usize, + result: InstrStop, +) -> BuiltinResult { let output = if len != 0 { let Some(end) = offset.checked_add(len) else { return Err(InstrStop::MemoryOOG.into()); }; - if end > u32::MAX as usize { - return Err(InstrStop::MemoryLimitOOG.into()); - } ensure_memory(ecx, offset, len)?; offset as u32..end as u32 } else { @@ -1003,7 +1024,7 @@ pub unsafe fn __revmc_builtin_selfdestruct( // State gas for new account creation (EIP-8037). if ecx.enables(EvmFeatures::EIP8037) && should_charge_topup { - ecx.gas.spend_state(ecx.gas_params().get(GasId::NewAccountState))?; + ecx.gas.spend_state(ecx.gas_params().new_account_state_gas())?; } if !res.previously_destroyed { @@ -1067,6 +1088,7 @@ mod abi { __revmc_builtin_mulmod(sp: &mut [EvmWord; 3]); __revmc_builtin_assert_spec_id(ecx: &EvmContext<'_, '_, '_, T>, expected: u32); __revmc_builtin_exp(ecx: &mut EvmContext<'_, '_, '_, T>, sp: &mut [EvmWord; 2]) -> BuiltinResult; + __revmc_builtin_pow(sp: &mut [EvmWord; 2]); __revmc_builtin_keccak256(ecx: &mut EvmContext<'_, '_, '_, T>, sp: &mut [EvmWord; 2]) -> BuiltinResult; __revmc_builtin_keccak256_cc(ecx: &mut EvmContext<'_, '_, '_, T>, out: &mut EvmWord, offset: u64, len: u64) -> BuiltinResult; __revmc_builtin_balance(ecx: &mut EvmContext<'_, '_, '_, T>, address: &mut EvmWord) -> BuiltinResult; @@ -1107,7 +1129,7 @@ mod tests { use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, constants::CALL_DEPTH_LIMIT, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, ethereum::ethereum_tx_registry, evm::{AccountInfo, EmptyDB, InMemoryDB, inspector::Inspector}, interpreter::{GasTracker, Message, MessageResult, MessageResultExt, op}, @@ -1144,7 +1166,9 @@ mod tests { ) -> Option> { self.call_static_flags .push(interp.is_static() || message.kind == MessageKind::StaticCall); - self.calls.push(message.clone()); + let mut message = message.clone(); + message.input = message.input.to_bytes(interp.memory()).into(); + self.calls.push(message); Some(self.execute_result.clone()) } @@ -1209,8 +1233,8 @@ mod tests { #[test] fn call_builtin_executes_message() { - let target = Address::from([0x22; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let caller = Address::repeat_byte(0x11); let child_output = Bytes::from_static(&[0xaa, 0xbb, 0xcc]); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector { @@ -1222,15 +1246,13 @@ mod tests { }, ..MessageInspector::default() }); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, destination: caller, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1247,7 +1269,7 @@ mod tests { let sp = frame.stack.as_mut_ptr(); unsafe { abi::__revmc_builtin_call(&mut frame.ecx, sp, CallKind::Call) }; - assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::from(1)); + assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::ONE); assert_eq!(frame.ecx.return_data(), child_output.as_ref()); assert_eq!(frame.ecx.memory().slice(8, 2), &[0xaa, 0xbb]); let output = b"frame-output"; @@ -1263,19 +1285,18 @@ mod tests { assert_eq!(inspector.calls[0].kind, MessageKind::Call); assert_eq!(inspector.calls[0].destination, target); assert_eq!(inspector.calls[0].caller, caller); - assert_eq!(inspector.calls[0].input.as_ref(), b"in"); + assert_eq!(inspector.calls[0].input.as_owned().unwrap()[..], *b"in"); assert!(!inspector.call_static_flags[0]); } #[test] fn callcode_builtin_maps_message_fields() { - let target = Address::from([0x22; 20]); - let destination = Address::from([0x33; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let destination = Address::repeat_byte(0x33); + let caller = Address::repeat_byte(0x11); let stack_value = Word::from(0x12); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector::default()); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, destination, @@ -1284,8 +1305,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1294,7 +1314,7 @@ mod tests { let sp = frame.stack.as_mut_ptr(); unsafe { abi::__revmc_builtin_call(&mut frame.ecx, sp, CallKind::CallCode) }; - assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::from(1)); + assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::ONE); } let inspector = host.clear_inspector_as::().unwrap(); @@ -1309,13 +1329,12 @@ mod tests { #[test] fn delegatecall_builtin_maps_message_fields() { - let target = Address::from([0x22; 20]); - let destination = Address::from([0x33; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let destination = Address::repeat_byte(0x33); + let caller = Address::repeat_byte(0x11); let current_value = Word::from(0x99); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector::default()); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, destination, @@ -1324,8 +1343,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1334,7 +1352,7 @@ mod tests { let sp = frame.stack.as_mut_ptr(); unsafe { abi::__revmc_builtin_call(&mut frame.ecx, sp, CallKind::DelegateCall) }; - assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::from(1)); + assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::ONE); } let inspector = host.clear_inspector_as::().unwrap(); @@ -1349,12 +1367,11 @@ mod tests { #[test] fn staticcall_builtin_maps_message_fields() { - let target = Address::from([0x22; 20]); - let destination = Address::from([0x33; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let destination = Address::repeat_byte(0x33); + let caller = Address::repeat_byte(0x11); let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector::default()); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, destination, @@ -1363,8 +1380,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1373,7 +1389,7 @@ mod tests { let sp = frame.stack.as_mut_ptr(); unsafe { abi::__revmc_builtin_call(&mut frame.ecx, sp, CallKind::StaticCall) }; - assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::from(1)); + assert_eq!(unsafe { frame.stack.get_unchecked(0) }.to_u256(), Word::ONE); } let inspector = host.clear_inspector_as::().unwrap(); @@ -1388,7 +1404,7 @@ mod tests { #[test] fn create_builtin_executes_message() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let initcode = [op::STOP]; let mut host = base_evm(SpecId::CANCUN); host.set_inspector(MessageInspector { @@ -1400,14 +1416,12 @@ mod tests { }, ..MessageInspector::default() }); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1427,12 +1441,12 @@ mod tests { let inspector = host.clear_inspector_as::().unwrap(); assert_eq!(inspector.creates.len(), 1); assert_eq!(inspector.creates[0].kind, MessageKind::Create); - assert_eq!(inspector.creates[0].input.as_ref(), initcode); + assert_eq!(inspector.creates[0].input.as_owned().unwrap()[..], initcode); } #[test] fn create2_builtin_maps_salt() { - let created = Address::from([0x77; 20]); + let created = Address::repeat_byte(0x77); let initcode = [op::STOP]; let salt = EvmWord::from(Word::from(0xabcdu64)); let mut host = base_evm(SpecId::CANCUN); @@ -1445,14 +1459,12 @@ mod tests { }, ..MessageInspector::default() }); - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); @@ -1473,16 +1485,16 @@ mod tests { let inspector = host.clear_inspector_as::().unwrap(); assert_eq!(inspector.creates.len(), 1); assert_eq!(inspector.creates[0].kind, MessageKind::Create2); - assert_eq!(inspector.creates[0].input.as_ref(), initcode); + assert_eq!(inspector.creates[0].input.as_owned().unwrap()[..], initcode); assert_eq!(inspector.creates[0].salt, B256::from(salt.to_be_bytes())); } #[test] fn create_builtin_preaccess_failure_skips_child_message() { - let contract = Address::from([0x11; 20]); + let contract = Address::repeat_byte(0x11); for create_kind in [CreateKind::Create, CreateKind::Create2] { for (balance, nonce, value, depth) in [ - (Word::ZERO, 0, Word::from(1), 0), + (Word::ZERO, 0, Word::ONE, 0), (Word::MAX, u64::MAX, Word::ZERO, 0), (Word::MAX, 0, Word::ZERO, CALL_DEPTH_LIMIT), ] { @@ -1499,7 +1511,6 @@ mod tests { Precompiles::base(SpecId::AMSTERDAM), ); host.set_inspector(MessageInspector::default()); - let tx_env = TxEnvExt::default(); let message = MessageExt { depth, gas_limit: 100_000, @@ -1507,8 +1518,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..MessageExt::default() }; - let mut interpreter = - evm2::interpreter::Interpreter::::new(&tx_env, &message); + let mut interpreter = evm2::interpreter::Interpreter::::new(&message); { let mut frame = prepare_frame(&mut interpreter, &mut host); diff --git a/crates/jit/builtins/src/utils.rs b/crates/jit/builtins/src/utils.rs index 33557266b..b74cadedc 100644 --- a/crates/jit/builtins/src/utils.rs +++ b/crates/jit/builtins/src/utils.rs @@ -36,11 +36,6 @@ pub(crate) fn require_non_staticcall( Ok(()) } -#[inline] -pub(crate) fn word_to_u64_saturated(value: U256) -> u64 { - value.try_into().unwrap_or(u64::MAX) -} - // JIT code writes stack words with 8-byte stores, so read them back with scalar loads. The // interpreter's helpers use one 32-byte load, which can't take its data from those stores. @@ -53,12 +48,6 @@ pub(crate) fn word_to_usize(value: U256) -> Result { }) } -/// Converts a word to `usize`, saturating on overflow. -#[inline] -pub(crate) fn word_to_usize_saturated(value: U256) -> usize { - value.try_into().unwrap_or(usize::MAX) -} - /// Loads an account, handling cold load gas accounting. /// /// Pre-Berlin, `cold_account_additional_cost` is 0, so the cold load logic is a no-op. @@ -113,7 +102,7 @@ pub(crate) unsafe fn copy_operation( ecx.gas.spend(ecx.gas_params().copy_cost(len))?; let memory_offset = word_to_usize(memory_offset.to_u256())?; ensure_memory(ecx, memory_offset, len)?; - let data_offset = word_to_usize_saturated(data_offset.to_u256()); + let data_offset = data_offset.to_u256().saturating_to::(); ecx.memory_mut().set_data(memory_offset, data_offset, len, data); } Ok(()) diff --git a/crates/jit/codegen/src/bytecode/asm.rs b/crates/jit/codegen/src/bytecode/asm.rs index caf9c2f5a..942ea1547 100644 --- a/crates/jit/codegen/src/bytecode/asm.rs +++ b/crates/jit/codegen/src/bytecode/asm.rs @@ -553,7 +553,7 @@ fn parse_items<'a>(tokens: &[Token<'a>]) -> Result>> { push_kind: PushKind::Fixed(1), })); } else { - let imm_len = opc.info().immediate_size(); + let imm_len = opc.immediate_size(); if imm_len > 0 { let imm = expect_imm(tokens, &mut i, opc)?; items.push(Item::Inst(Inst { @@ -562,12 +562,13 @@ fn parse_items<'a>(tokens: &[Token<'a>]) -> Result>> { push_kind: PushKind::Fixed(imm_len), })); } else { - if matches!( - tokens.get(i), - Some(Token { kind: TokenKind::Number(_), .. }) - ) { - eyre::bail!("unexpected immediate for opcode {opc}"); - } + eyre::ensure!( + !matches!( + tokens.get(i), + Some(Token { kind: TokenKind::Number(_), .. }) + ), + "unexpected immediate for opcode {opc}" + ); items.push(Item::Inst(Inst { opcode, imm: None, diff --git a/crates/jit/codegen/src/bytecode/fmt.rs b/crates/jit/codegen/src/bytecode/fmt.rs index b1d84ed6e..7a814af5a 100644 --- a/crates/jit/codegen/src/bytecode/fmt.rs +++ b/crates/jit/codegen/src/bytecode/fmt.rs @@ -1,8 +1,6 @@ use super::{ Bytecode, Inst, InstData, InstFlags, asm::{TokenKind, Tokenizer}, - bitvec_as_bytes, - passes::block_analysis::Block, }; use alloy_primitives::hex; use evm2::interpreter::{op, opcode::OpCode}; @@ -10,11 +8,6 @@ use oxc_index::{IndexVec, index_vec}; use std::{borrow::Cow, fmt, fmt::Write, io::IsTerminal}; impl Bytecode<'_> { - /// Resolves a jump target instruction to its CFG block index. - fn target_block(&self, target: Inst) -> Option { - self.cfg.inst_to_block.get(target).copied().flatten() - } - /// Collects formatted lines and builds the inst-to-line map stored in `self.inst_lines`. pub(super) fn collect_lines(&self) -> Vec<(String, String)> { let mut lines: Vec<(String, String)> = Vec::new(); @@ -143,7 +136,7 @@ impl Bytecode<'_> { if flags.contains(InstFlags::NOOP) { comment.push_str(" noop"); } - if flags.contains(InstFlags::DEAD_CODE) { + if data.is_dead_code() { comment.push_str(" dead"); } if flags.contains(InstFlags::DISABLED) { @@ -288,7 +281,7 @@ impl fmt::Debug for Bytecode<'_> { f.debug_struct("Bytecode") .field("code", &hex::encode(&*self.code)) .field("insts", &self.insts) - .field("jumpdests", &hex::encode(bitvec_as_bytes(&self.jumpdests))) + .field("jumpdests", &hex::encode(self.jumpdests.as_slice())) .field("spec_id", &self.spec_id) .field("has_dynamic_jumps", &self.has_dynamic_jumps) .finish() diff --git a/crates/jit/codegen/src/bytecode/info.rs b/crates/jit/codegen/src/bytecode/info.rs index eaefd42b2..698f32eaf 100644 --- a/crates/jit/codegen/src/bytecode/info.rs +++ b/crates/jit/codegen/src/bytecode/info.rs @@ -11,8 +11,6 @@ pub struct OpcodeInfo(u32); impl OpcodeInfo { /// The unknown flag. pub const UNKNOWN: u32 = 1 << 31; - /// The dynamic flag. - pub const DYNAMIC: u32 = 1 << 30; /// The disabled flag. pub const DISABLED: u32 = 1 << 29; /// The host-instruction flag. @@ -34,12 +32,6 @@ impl OpcodeInfo { self.0 == Self::UNKNOWN } - /// Returns `true` if the gas cost is dynamic. - #[inline] - pub const fn is_dynamic(self) -> bool { - self.0 & Self::DYNAMIC != 0 - } - /// Returns `true` if the opcode is known, but disabled in the current EVM /// version. #[inline] @@ -86,12 +78,6 @@ impl OpcodeInfo { self.0 = Self::UNKNOWN; } - /// Sets the dynamic flag. - #[inline] - pub const fn set_dynamic(&mut self) { - self.0 |= Self::DYNAMIC; - } - /// Sets the disabled flag. #[inline] pub const fn set_disabled(&mut self) { @@ -117,69 +103,10 @@ pub const fn op_info_map(spec_id: SpecId) -> &'static [OpcodeInfo; 256] { &MAPS[spec_id as usize] } -/// Opcodes with a dynamic gas component that also have a base (static) cost deducted upfront. -/// Only the dynamic part is paid in builtins. -const DYNAMIC_WITH_BASE_GAS: &[u8] = &[ - op::EXP, - op::KECCAK256, - op::BALANCE, - op::CALLDATACOPY, - op::CODECOPY, - op::EXTCODESIZE, - op::EXTCODECOPY, - op::RETURNDATACOPY, - op::EXTCODEHASH, - op::MLOAD, - op::MSTORE, - op::MSTORE8, - op::MCOPY, - op::LOG0, - op::LOG1, - op::LOG2, - op::LOG3, - op::LOG4, - op::SLOAD, - op::CALL, - op::CALLCODE, - op::DELEGATECALL, - op::STATICCALL, - op::SELFDESTRUCT, -]; - -/// Opcodes whose gas cost is entirely dynamic — computed fully in builtins at runtime. +/// Opcodes whose builtin pays the whole gas cost, including the static part, at runtime. Core +/// gives them no static gas, so a host cannot change it without running its own instruction. const FULLY_DYNAMIC: &[u8] = &[op::SSTORE, op::CREATE, op::CREATE2]; -/// Opcodes that are gated behind a specific `SpecId`, paired with the spec they were introduced in. -const SPEC_GATED_OPCODES: &[(u8, SpecId)] = &[ - (op::SHL, SpecId::PETERSBURG), - (op::SHR, SpecId::PETERSBURG), - (op::SAR, SpecId::PETERSBURG), - (op::CLZ, SpecId::OSAKA), - (op::RETURNDATASIZE, SpecId::BYZANTIUM), - (op::RETURNDATACOPY, SpecId::BYZANTIUM), - (op::EXTCODEHASH, SpecId::PETERSBURG), - (op::CHAINID, SpecId::ISTANBUL), - (op::SELFBALANCE, SpecId::ISTANBUL), - (op::BASEFEE, SpecId::LONDON), - (op::BLOBHASH, SpecId::CANCUN), - (op::BLOBBASEFEE, SpecId::CANCUN), - (op::TLOAD, SpecId::CANCUN), - (op::TSTORE, SpecId::CANCUN), - (op::MCOPY, SpecId::CANCUN), - (op::PUSH0, SpecId::SHANGHAI), - (op::DELEGATECALL, SpecId::HOMESTEAD), - (op::CREATE2, SpecId::PETERSBURG), - (op::STATICCALL, SpecId::BYZANTIUM), - (op::REVERT, SpecId::BYZANTIUM), - (op::DUPN, SpecId::AMSTERDAM), - (op::SWAPN, SpecId::AMSTERDAM), - (op::EXCHANGE, SpecId::AMSTERDAM), - (op::SLOTNUM, SpecId::AMSTERDAM), -]; - -/// Opcodes defined by the opcode table but intentionally disabled for compiled execution. -const UNSUPPORTED_OPCODES: &[u8] = &[]; - const fn contains(haystack: &[u8], needle: u8) -> bool { let mut i = 0; while i < haystack.len() { @@ -196,7 +123,6 @@ const fn make_map() -> [OpcodeInfo; 256] { BASE_SPEC_ID, { u32::MAX }, > as EvmConfig>::OPCODE_CONFIG; - let spec_id = SpecId::try_from_u32(BASE_SPEC_ID).expect("invalid spec id"); let mut map = [OpcodeInfo(OpcodeInfo::UNKNOWN); 256]; let mut i = 0u16; @@ -209,49 +135,20 @@ const fn make_map() -> [OpcodeInfo; 256] { continue; } - // Mark unsupported opcodes as disabled rather than - // unknown, so they return `NotActivated` instead of `OpcodeNotFound` at runtime. - if contains(UNSUPPORTED_OPCODES, op) { - map[op as usize].set_disabled(); - i += 1; - continue; - } - - let is_fully_dynamic = contains(FULLY_DYNAMIC, op); - let is_dynamic_with_base = contains(DYNAMIC_WITH_BASE_GAS, op); - - // Fully dynamic opcodes have their entire gas cost handled in builtins. - let gas = if is_fully_dynamic { - 0u16 + // Mark opcodes the version leaves out as disabled rather than unknown, so they return + // `NotActivated` instead of `OpcodeNotFound` at runtime. `INVALID` is never in a config + // and keeps its own stop reason. + let mut info = OpcodeInfo::new(0); + if !config.is_defined(op) && op != op::INVALID { + info.set_disabled(); } else { - let static_gas = config.static_gas(op) as u64; - assert!( - static_gas <= OpcodeInfo::MASK as u64, - "static gas exceeds OpcodeInfo capacity" - ); - static_gas as u16 - }; - - let mut info = OpcodeInfo::new(gas); - - if is_fully_dynamic || is_dynamic_with_base { - info.set_dynamic(); + info.set_gas(config.static_gas(op)); } map[op as usize] = info; i += 1; } - // Apply spec-gating: mark opcodes as disabled if the current spec is before their introduction. - let mut j = 0; - while j < SPEC_GATED_OPCODES.len() { - let (op, required_spec) = SPEC_GATED_OPCODES[j]; - if !spec_id.enables(required_spec) { - map[op as usize].set_disabled(); - } - j += 1; - } - map } @@ -302,10 +199,9 @@ pub(crate) fn opcode_infos(view: &OpcodeView, spec_id: SpecId) -> [OpcodeInfo; 2 continue; } let mut info = base[i]; - info.set_gas(if contains(FULLY_DYNAMIC, op) && !host { 0 } else { view.static_gas(op) }); + info.set_gas(view.static_gas(op)); if host { info.set_host(); - info.set_dynamic(); } map[i] = info; } @@ -315,6 +211,27 @@ pub(crate) fn opcode_infos(view: &OpcodeView, spec_id: SpecId) -> [OpcodeInfo; 2 #[cfg(test)] mod tests { use super::*; + use evm2::OpcodeConfig; + + #[test] + fn matches_core_opcode_config() { + for spec in 0..SpecId::COUNT as u32 { + let spec_id = SpecId::try_from_u32(spec).unwrap(); + let config = OpcodeConfig::::base_for_spec(spec_id); + let map = op_info_map(spec_id); + for op in 0..=u8::MAX { + let info = map[op as usize]; + if OpCode::new(op).is_none() { + assert!(info.is_unknown(), "{spec_id:?} {op:#04x}"); + } else if op == op::INVALID || config.is_defined(op) { + assert!(!info.is_unknown() && !info.is_disabled(), "{spec_id:?} {op:#04x}"); + assert_eq!(info.base_gas(), config.static_gas(op), "{spec_id:?} {op:#04x}"); + } else { + assert!(info.is_disabled(), "{spec_id:?} {op:#04x}"); + } + } + } + } #[test] fn test_clz_flags() { @@ -362,9 +279,8 @@ mod tests { assert_eq!(cancun[op::MUL as usize].base_gas(), 5); assert_eq!(cancun[op::ADDMOD as usize].base_gas(), 8); - // EXP: base 10, dynamic. + // EXP: static gas only; the dynamic part is paid in builtins. assert_eq!(cancun[op::EXP as usize].base_gas(), 10); - assert!(cancun[op::EXP as usize].is_dynamic()); // PUSH/DUP/SWAP gas. assert_eq!(cancun[op::PUSH1 as usize].base_gas(), 3); @@ -378,13 +294,10 @@ mod tests { assert_eq!(cancun[op::LOG2 as usize].base_gas(), 375); assert_eq!(cancun[op::LOG3 as usize].base_gas(), 375); assert_eq!(cancun[op::LOG4 as usize].base_gas(), 375); - assert!(cancun[op::LOG0 as usize].is_dynamic()); - // Memory ops: dynamic with base cost 3. + // Memory ops: static gas only. assert_eq!(cancun[op::MLOAD as usize].base_gas(), 3); - assert!(cancun[op::MLOAD as usize].is_dynamic()); assert_eq!(cancun[op::KECCAK256 as usize].base_gas(), 30); - assert!(cancun[op::KECCAK256 as usize].is_dynamic()); // Transient storage (Cancun). assert_eq!(cancun[op::TLOAD as usize].base_gas(), 100); @@ -412,18 +325,16 @@ mod tests { assert!(pre_shanghai[op::PUSH0 as usize].is_disabled()); assert!(!cancun[op::PUSH0 as usize].is_disabled()); - // Dynamic-with-base-gas opcodes: base gas varies by spec. + // Static gas of opcodes with a dynamic component varies by spec. let frontier = op_info_map(SpecId::FRONTIER); assert_eq!(frontier[op::SLOAD as usize].base_gas(), 50); assert_eq!(frontier[op::SELFDESTRUCT as usize].base_gas(), 0); - assert!(frontier[op::SELFDESTRUCT as usize].is_dynamic()); assert_eq!(frontier[op::CALL as usize].base_gas(), 40); assert_eq!(frontier[op::BALANCE as usize].base_gas(), 20); assert_eq!(frontier[op::EXTCODESIZE as usize].base_gas(), 20); let tangerine = op_info_map(SpecId::TANGERINE); assert_eq!(tangerine[op::SLOAD as usize].base_gas(), 200); assert_eq!(tangerine[op::SELFDESTRUCT as usize].base_gas(), 5000); - assert!(tangerine[op::SELFDESTRUCT as usize].is_dynamic()); assert_eq!(tangerine[op::CALL as usize].base_gas(), 700); assert_eq!(tangerine[op::BALANCE as usize].base_gas(), 400); assert_eq!(tangerine[op::EXTCODESIZE as usize].base_gas(), 700); diff --git a/crates/jit/codegen/src/bytecode/interner.rs b/crates/jit/codegen/src/bytecode/interner.rs index 73342cad1..4bd1bd1b0 100644 --- a/crates/jit/codegen/src/bytecode/interner.rs +++ b/crates/jit/codegen/src/bytecode/interner.rs @@ -55,7 +55,7 @@ impl std::ops::Index for Interner { #[inline] fn index(&self, idx: I) -> &T { - &self.set[idx.index()] + self.get(idx) } } diff --git a/crates/jit/codegen/src/bytecode/mod.rs b/crates/jit/codegen/src/bytecode/mod.rs index efcba46f1..9a4568e35 100644 --- a/crates/jit/codegen/src/bytecode/mod.rs +++ b/crates/jit/codegen/src/bytecode/mod.rs @@ -2,16 +2,16 @@ use crate::FxHashMap; use alloy_primitives::U256; -use bitvec::vec::BitVec; use evm2::{ OpcodeView, SpecId, + bytecode::{JumpTable, analyze_legacy}, interpreter::{op, opcode::OpCode}, version::GasParams, }; use evm2_jit_backend::{Result, eyre::bail}; use oxc_index::IndexVec; use smallvec::SmallVec; -use std::{borrow::Cow, cell::RefCell}; +use std::{borrow::Cow, cell::RefCell, fmt::Write}; mod passes; pub(crate) use passes::{ @@ -107,7 +107,7 @@ impl U256Imm { if self.0 & Self::INLINE_TAG != 0 { U256::from(self.0 & !Self::INLINE_TAG) } else { - *interner.get(U256Idx::from_usize(self.0 as usize)) + *interner.get(U256Idx::new(self.0 as usize)) } } @@ -161,8 +161,9 @@ pub struct Bytecode<'a> { pub(crate) code: Cow<'a, [u8]>, /// The instructions. insts: IndexVec, - /// `JUMPDEST` opcode map. `jumpdests[pc]` is `true` if `code[pc] == op::JUMPDEST`. - jumpdests: BitVec, + /// `JUMPDEST` opcode map: `jumpdests.is_valid(pc)` is `true` if `code[pc] == op::JUMPDEST` + /// and `pc` is not inside `PUSH` data. + jumpdests: JumpTable, /// The [`SpecId`]. pub(crate) spec_id: SpecId, /// Gas parameters for dynamic gas folding. Defaults to the evm2 spec gas schedule. @@ -238,10 +239,10 @@ impl<'a> Bytecode<'a> { let mut inst_to_pc = IndexVec::with_capacity(code.len() + 8); // EIP-8024: JUMPDEST analysis is unchanged by DUPN/SWAPN/EXCHANGE: only PUSH // data is skipped, their immediates are not. Compute the valid-jump-target - // bitmap with the legacy scan over the raw bytes, independent of instruction + // bitmap with the legacy analysis over the raw bytes, independent of instruction // decoding below (which does consume DUPN/SWAPN/EXCHANGE immediates and can // therefore desynchronize from the analysis). - let jumpdests = legacy_jumpdests(&code); + let jumpdests = analyze_legacy(&code); let mut pc_to_inst = FxHashMap::with_capacity_and_hasher(code.len(), Default::default()); let mut u256_interner = U256Interner::new(); let owned_infos = opcodes.map(|opcodes| opcode_infos(opcodes, spec_id)); @@ -271,8 +272,12 @@ impl<'a> Bytecode<'a> { let (base_gas, stack_io) = if info.is_unknown() || info.is_disabled() || info.is_unsupported_host() { (0, (0, 0)) + } else if let Some(stack_io) = compute_stack_io(opcode, immediate) { + (info.base_gas(), stack_io) } else { - (info.base_gas(), compute_stack_io(opcode, immediate)) + // Pays its static gas, then halts before touching the stack. + flags |= InstFlags::INVALID_IMMEDIATE; + (info.base_gas(), (0, 0)) }; let data = match opcode { @@ -331,7 +336,7 @@ impl<'a> Bytecode<'a> { min_imm_len(opcode) as usize }; let imm_end = (pc + 1 + imm_len).min(code.len()); - if let Some(target) = (pc + 1..imm_end).find(|&p| jumpdests[p]) { + if let Some(target) = (pc + 1..imm_end).find(|&p| jumpdests.is_valid(p)) { // SAFETY: we just consumed the immediate bytes at `pc + 1..imm_end`. unsafe { iter.rewind(imm_end - target) }; } @@ -361,7 +366,7 @@ impl<'a> Bytecode<'a> { redirects: FxHashMap::default(), cfg: Cfg::default(), config: AnalysisConfig::default(), - compiler_gas_limit: 100_000, + compiler_gas_limit: DEFAULT_COMPILER_GAS_LIMIT, compiler_gas_used: 0, } } @@ -459,7 +464,7 @@ impl<'a> Bytecode<'a> { self.dead_store_elim(); } - self.construct_sections(); + self.construct_sections()?; self.construct_memory_sections(); debug!( @@ -499,7 +504,7 @@ impl<'a> Bytecode<'a> { #[inline(never)] pub(crate) fn recompute_reachable_jumpdests(&mut self) { for data in &mut self.insts.raw { - if data.opcode == op::JUMPDEST { + if data.is_jumpdest() { data.clear_jumpdest_reachable(); } } @@ -525,7 +530,7 @@ impl<'a> Bytecode<'a> { while let Some(&next) = self.redirects.get(&target) { target = next; } - if self.insts[target].opcode == op::JUMPDEST { + if self.insts[target].is_jumpdest() { self.insts[target].set_jumpdest_reachable(); } } @@ -573,7 +578,7 @@ impl<'a> Bytecode<'a> { if data.is_reachable_jumpdest(self.has_dynamic_jumps) { break; } - if !data.flags.contains(InstFlags::DEAD_CODE) { + if !data.is_dead_code() { any_new = true; marked_jump_dead |= data.is_jump(); } @@ -591,7 +596,7 @@ impl<'a> Bytecode<'a> { /// Constructs the sections in the bytecode. #[instrument(name = "sections", level = "debug", skip_all)] #[inline(never)] - fn construct_sections(&mut self) { + fn construct_sections(&mut self) -> Result<()> { let mut analysis = SectionsAnalysis::default(); for inst in self.insts.indices() { if !self.inst(inst).is_dead_code() { @@ -599,6 +604,20 @@ impl<'a> Bytecode<'a> { } } analysis.finish(self); + + if let Some((inst, _)) = self + .insts + .iter_enumerated() + .find(|(_, data)| data.gas_section.gas_cost == GasSection::SATURATED) + { + bail!( + "the gas section at instruction {} costs at least {} gas, which compiled code \ + cannot charge ahead of its instructions", + inst.index(), + GasSection::SATURATED, + ); + } + Ok(()) } /// Constructs the memory sections in the bytecode. @@ -646,11 +665,8 @@ impl<'a> Bytecode<'a> { } /// Returns `true` if the given program counter is a valid jump destination. - pub(crate) fn is_valid_jump(&self, pc: usize) -> bool { - // Reads the raw words: `BitVec::get` costs several times more in the jump analyses. - let bits = usize::BITS as usize; - pc < self.jumpdests.len() - && self.jumpdests.as_raw_slice()[pc / bits] >> (pc % bits) & 1 != 0 + pub(crate) const fn is_valid_jump(&self, pc: usize) -> bool { + self.jumpdests.is_valid(pc) } /// Returns `true` if the bytecode has dynamic jumps. @@ -698,6 +714,16 @@ impl<'a> Bytecode<'a> { self.multi_jump_targets.get(&inst).map(|v| v.as_slice()) } + /// Resolves an instruction through dead-block redirects created by dedup. + pub(crate) fn effective_inst(&self, inst: Inst) -> Inst { + self.redirects.get(&inst).copied().unwrap_or(inst) + } + + /// Resolves a jump target instruction to its CFG block index. + pub(crate) fn target_block(&self, target: Inst) -> Option { + self.cfg.inst_to_block.get(target).copied().flatten() + } + /// Returns the known constant value of a stack operand at the given instruction. /// /// `depth` 0 is TOS (first popped by this instruction), 1 is second, etc. @@ -720,15 +746,26 @@ impl<'a> Bytecode<'a> { /// Returns the known output of the given instruction if translation can store it instead of /// executing the instruction. /// - /// EXP's dynamic gas is only charged statically when its exponent is known, and a constant - /// output joined across call contexts does not imply a constant exponent. + /// A constant output joined across call contexts does not imply that EXP's exponent is + /// constant, so EXP's dynamic gas must also be static. pub(crate) fn foldable_output(&self, inst: Inst) -> Option { - if self.insts[inst].opcode == op::EXP && self.const_operand(inst, 1).is_none() { + if self.insts[inst].opcode == op::EXP && self.exp_static_exponent(inst).is_none() { return None; } self.const_output(inst) } + /// Returns the exponent of the EXP instruction if its dynamic gas is charged statically with + /// the section. + /// + /// This holds exactly when the exponent is a known constant. Translation then never charges + /// the dynamic gas itself, whether it folds, inlines or calls the gas-free builtin; otherwise + /// it charges the gas at run time. + pub(crate) fn exp_static_exponent(&self, inst: Inst) -> Option { + debug_assert_eq!(self.insts[inst].opcode, op::EXP); + self.const_operand(inst, 1) + } + /// Returns the first constant EVM memory access `(offset, len)` for the given instruction. #[allow(dead_code)] pub(crate) fn const_memory_access(&self, inst: Inst) -> Option<(Option, Option)> { @@ -782,7 +819,6 @@ impl<'a> Bytecode<'a> { #[inline(never)] fn log_const_input_stats(&self) { use op::*; - use std::fmt::Write; // per_input[depth] = [total, const_count, fits_usize_count]. struct Entry { @@ -907,8 +943,6 @@ impl<'a> Bytecode<'a> { /// Logs IR statistics and top 5 longest blocks at trace level. #[inline(never)] fn log_ir_stats(&self) { - use std::fmt::Write; - let s = self.ir_stats(); trace!( total_insts = s.total, @@ -941,8 +975,6 @@ impl<'a> Bytecode<'a> { /// Returns the name for a basic block. pub(crate) fn op_block_name(&self, inst: Option, name: &str) -> String { - use std::fmt::Write; - let Some(inst) = inst else { return format!("entry.{name}"); }; @@ -1146,7 +1178,7 @@ impl InstData { #[inline] pub(crate) fn static_jump_target(&self) -> Inst { debug_assert!(self.is_static_jump()); - Inst::from_usize(self.data as usize) + Inst::new(self.data as usize) } /// Sets the static jump target. Caller must also set [`InstFlags::STATIC_JUMP`]. @@ -1226,6 +1258,7 @@ impl InstData { (self.opcode == op::JUMP && self.flags.contains(InstFlags::INVALID_JUMP)) || self.flags.contains(InstFlags::DISABLED) || self.flags.contains(InstFlags::UNKNOWN) + || self.flags.contains(InstFlags::INVALID_IMMEDIATE) || matches!( self.opcode, op::STOP | op::RETURN | op::REVERT | op::INVALID | op::SELFDESTRUCT @@ -1274,40 +1307,13 @@ bitflags::bitflags! { /// The host implements this opcode in a way compiled code cannot reproduce, so a /// program where it is reachable is not compiled. const UNSUPPORTED_HOST = 1 << 12; + /// The `DUPN`, `SWAPN`, or `EXCHANGE` immediate is invalid. + /// Always returns [`InvalidImmediateEncoding`](evm2::interpreter::InstrStop::InvalidImmediateEncoding) + /// at runtime. + const INVALID_IMMEDIATE = 1 << 13; } } -/// Computes the legacy `JUMPDEST` analysis bitmap: a `0x5b` byte is a valid jump -/// target unless it is part of the data of a `PUSH1..=PUSH32` instruction. -/// -/// This is the analysis of every fork: EIP-8024 explicitly leaves it unchanged, so -/// DUPN/SWAPN/EXCHANGE immediates are scanned as if they were opcodes. -fn legacy_jumpdests(code: &[u8]) -> BitVec { - let mut jumpdests = BitVec::repeat(false, code.len()); - let mut pc = 0; - while pc < code.len() { - let opcode = code[pc]; - if opcode == op::JUMPDEST { - jumpdests.set(pc, true); - } - pc += 1; - if let op::PUSH1..=op::PUSH32 = opcode { - pc += (opcode - op::PUSH1 + 1) as usize; - } - } - jumpdests -} - -fn bitvec_as_bytes( - bitvec: &BitVec, -) -> &[u8] { - slice_as_bytes(bitvec.as_raw_slice()) -} - -const fn slice_as_bytes(a: &[T]) -> &[u8] { - unsafe { std::slice::from_raw_parts(a.as_ptr().cast(), std::mem::size_of_val(a)) } -} - #[cfg(test)] mod tests { use super::*; @@ -1317,7 +1323,7 @@ mod tests { // EVM spec: missing bytes are zero, so the value is 0x4200. let code = [op::PUSH2, 0x42]; let bc = Bytecode::test(&code); - let inst = Inst::from_usize(0); + let inst = Inst::new(0); let data = bc.inst(inst); assert_eq!(bc.get_imm(inst), Some([0x42].as_slice())); assert_eq!(bc.get_push_value(data), U256::from(0x4200)); @@ -1325,7 +1331,7 @@ mod tests { // PUSH3 followed by two bytes — truncated at EOF. let code = [op::PUSH3, 0xAB, 0xCD]; let bc = Bytecode::test(&code); - let inst = Inst::from_usize(0); + let inst = Inst::new(0); let data = bc.inst(inst); assert_eq!(bc.get_imm(inst), Some([0xAB, 0xCD].as_slice())); assert_eq!(bc.get_push_value(data), U256::from(0xABCD00)); @@ -1333,7 +1339,7 @@ mod tests { // PUSH1 with no immediate bytes at all. let code = [op::PUSH1]; let bc = Bytecode::test(&code); - let inst = Inst::from_usize(0); + let inst = Inst::new(0); let data = bc.inst(inst); assert_eq!(bc.get_imm(inst), None); assert_eq!(bc.get_push_value(data), U256::ZERO); @@ -1341,7 +1347,7 @@ mod tests { // Non-truncated PUSH2 — full immediate. let code = [op::PUSH2, 0x42, 0xFF]; let bc = Bytecode::test(&code); - let data = bc.inst(Inst::from_usize(0)); + let data = bc.inst(Inst::new(0)); assert_eq!(bc.get_push_value(data), U256::from(0x42FF)); } @@ -1355,9 +1361,9 @@ mod tests { op::CALLDATALOAD, op::JUMP, ]); - let jumpi = Inst::from_usize(2); + let jumpi = Inst::new(2); let fallthrough = jumpi + 1; - let dynamic_jump = Inst::from_usize(5); + let dynamic_jump = Inst::new(5); bc.inst_mut(jumpi).flags |= InstFlags::STATIC_JUMP | InstFlags::CONST_JUMP_CONDITION; bc.inst_mut(jumpi).set_static_jump_target(fallthrough); @@ -1365,7 +1371,7 @@ mod tests { bc.mark_dead_code(); assert!(!bc.inst(fallthrough).is_dead_code()); - assert!(bc.inst(Inst::from_usize(4)).is_dead_code()); + assert!(bc.inst(Inst::new(4)).is_dead_code()); assert!(bc.inst(dynamic_jump).is_dead_code()); assert!(!bc.has_dynamic_jumps); } @@ -1386,10 +1392,10 @@ mod tests { op::JUMPDEST, op::STOP, ]); - let jumpi = Inst::from_usize(2); - let dead_jump = Inst::from_usize(4); - let live_target = Inst::from_usize(5); - let stale_target = Inst::from_usize(7); + let jumpi = Inst::new(2); + let dead_jump = Inst::new(4); + let live_target = Inst::new(5); + let stale_target = Inst::new(7); bc.inst_mut(jumpi).flags |= InstFlags::STATIC_JUMP | InstFlags::CONST_JUMP_CONDITION; bc.inst_mut(jumpi).set_static_jump_target(live_target); diff --git a/crates/jit/codegen/src/bytecode/opcode.rs b/crates/jit/codegen/src/bytecode/opcode.rs index a3b3599c5..c34edc8a1 100644 --- a/crates/jit/codegen/src/bytecode/opcode.rs +++ b/crates/jit/codegen/src/bytecode/opcode.rs @@ -5,6 +5,8 @@ use evm2::{ }; use std::{fmt, slice}; +pub use evm2::interpreter::instructions::{decode_pair, decode_single, encode_pair, encode_single}; + /// A bytecode iterator that yields opcodes and their immediate data, alongside the program counter. /// /// Created by calling [`OpcodesIter::with_pc`]. @@ -78,7 +80,7 @@ impl<'a> OpcodesIter<'a> { /// Create a new iterator over the given bytecode slice. #[inline] pub fn new(slice: &'a [u8], spec_id: SpecId) -> Self { - Self { iter: slice.iter(), info: op_info_map(spec_id) } + Self::with_infos(slice, op_info_map(spec_id)) } /// Create a new iterator over the given bytecode slice with the given opcode infos. @@ -208,68 +210,15 @@ pub const fn stack_io(op: u8) -> (u8, u8) { /// Returns the real `(inputs, outputs)` stack I/O for an opcode, decoding the immediate for /// `DUPN`, `SWAPN`, and `EXCHANGE` whose opcode-table entries are placeholders. -pub(crate) fn compute_stack_io(op: u8, immediate: Option<&[u8]>) -> (u8, u8) { +/// +/// Returns `None` if the immediate of one of those opcodes is invalid. +pub(crate) fn compute_stack_io(op: u8, immediate: Option<&[u8]>) -> Option<(u8, u8)> { let imm_u8 = || immediate.map_or(0, |b| b[0]); match op { op::DUPN => decode_single(imm_u8()).map(|n| (n, n + 1)), op::SWAPN => decode_single(imm_u8()).map(|n| (n + 1, n + 1)), op::EXCHANGE => decode_pair(imm_u8()).map(|(_n, m)| (m + 1, m + 1)), - _ => return stack_io(op), - } - .unwrap_or((0, 0)) // Invalid immediate. -} - -/// Decodes a DUPN/SWAPN immediate byte into a stack index. -/// -/// Returns `None` if the immediate is in the invalid range `[91, 127]`. -pub const fn decode_single(x: u8) -> Option { - if x <= 90 || x >= 128 { Some(x.wrapping_add(145)) } else { None } -} - -/// Encodes a stack index into a DUPN/SWAPN immediate byte. -/// -/// Returns `None` if the value is outside the valid range. -pub fn encode_single(n: u8) -> Option { - let x = n.wrapping_sub(145); - if decode_single(x) == Some(n) { Some(x) } else { None } -} - -/// Decodes an EXCHANGE immediate byte into a pair of stack indices `(n, m)`. -/// -/// Returns `None` if the value is outside the valid range. -pub const fn decode_pair(x: u8) -> Option<(u8, u8)> { - if x > 81 && x < 128 { - return None; - } - let k = (x ^ 143) as u16; - let q = (k / 16) as u8; - let r = (k % 16) as u8; - if q < r { Some((q + 1, r + 1)) } else { Some((r + 1, 29 - q)) } -} - -/// Encodes a pair of stack indices into an EXCHANGE immediate byte. -/// -/// Returns `None` if the pair cannot be encoded. -pub fn encode_pair(n: u8, m: u8) -> Option { - if n == 0 || m == 0 { - return None; - } - // Try both (q,r) orderings and check round-trip. - let try_k = |q: u8, r: u8| -> Option { - if q >= 16 || r >= 16 { - return None; - } - let k = q as u16 * 16 + r as u16; - let x = (k as u8) ^ 143; - if decode_pair(x) == Some((n, m)) { Some(x) } else { None } - }; - - // Case 1: q < r → n = q+1, m = r+1. - if n < m { - try_k(n - 1, m - 1) - } else { - // Case 2: q >= r → n = r+1, m = 29-q. - if let Some(q) = 29u8.checked_sub(m) { try_k(q, n - 1) } else { None } + _ => Some(stack_io(op)), } } @@ -334,64 +283,4 @@ mod tests { let s = format_bytecode(&bytecode, DEF_SPEC); assert_eq!(s, "PUSH0 PUSH1 0x69 PUSH2 0x0102"); } - - #[test] - fn encode_decode_single_roundtrip() { - for x in 0..=255u8 { - if let Some(n) = decode_single(x) { - assert_eq!(encode_single(n), Some(x), "roundtrip failed for x={x}, n={n}"); - } - } - for n in 0..=255u8 { - if let Some(x) = encode_single(n) { - assert_eq!(decode_single(x), Some(n), "roundtrip failed for n={n}, x={x}"); - } - } - // Out-of-range: values in [236,255] ∪ [0,16] have no valid encoding. - assert_eq!(encode_single(0), None); - assert_eq!(encode_single(16), None); - assert_eq!(encode_single(236), None); - // Invalid raw bytes. - assert_eq!(decode_single(91), None); - assert_eq!(decode_single(127), None); - } - - #[test] - fn encode_decode_pair_roundtrip() { - // encode → decode is always exact. - for n in 1..=30u8 { - for m in 1..=30u8 { - if let Some(x) = encode_pair(n, m) { - assert_eq!( - decode_pair(x), - Some((n, m)), - "roundtrip failed for (n,m)=({n},{m}), x={x}" - ); - } - } - } - // decode → encode: some pairs have multiple encodings, so the raw byte may differ, - // but decoding the result must produce the same pair. - // Some decoded pairs may have values too large to re-encode. - for x in 0..=255u8 { - if let Some((n, m)) = decode_pair(x) - && let Some(re) = encode_pair(n, m) - { - assert_eq!( - decode_pair(re), - Some((n, m)), - "decode roundtrip failed for x={x}, re={re}, (n,m)=({n},{m})" - ); - } - } - // Zero indices are invalid. - assert_eq!(encode_pair(0, 1), None); - assert_eq!(encode_pair(1, 0), None); - // Invalid raw bytes: [82, 127]. - assert_eq!(decode_pair(82), None); - assert_eq!(decode_pair(127), None); - // Edge: 80 and 81 are now valid. - assert!(decode_pair(80).is_some()); - assert!(decode_pair(81).is_some()); - } } diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index 58c6288b9..d8d7b03ed 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -61,7 +61,7 @@ use crate::{ use alloy_primitives::{U256, map::FxHasher}; use bitvec::vec::BitVec; use either::Either; -use evm2::{constants::STACK_LIMIT, interpreter::op}; +use evm2::{bytecode::JumpTable, constants::STACK_LIMIT, interpreter::op}; use oxc_index::IndexVec; use smallvec::SmallVec; use std::{ @@ -172,22 +172,22 @@ impl AbsValue { struct ConstSetInterner { interner: Interner>, /// `JUMPDEST` map: sets only keep valid jump targets. - jumpdests: BitVec, + jumpdests: JumpTable, /// Number of constants merged by [`Self::join`], for the fixpoint budget. merged: u64, } impl ConstSetInterner { - fn new(jumpdests: BitVec) -> Self { + fn new(jumpdests: JumpTable) -> Self { Self { interner: Interner::new(), jumpdests, merged: 0 } } /// Returns `true` if `value` may be part of a set. fn is_set_member(&self, value: AbsValue) -> bool { match value { - AbsValue::Const(imm) => imm - .get_inline() - .is_some_and(|pc| self.jumpdests.get(pc as usize).is_some_and(|bit| *bit)), + AbsValue::Const(imm) => { + imm.get_inline().is_some_and(|pc| self.jumpdests.is_valid(pc as usize)) + } AbsValue::ConstSet(_) => true, AbsValue::Range(..) | AbsValue::Top => false, } @@ -766,7 +766,7 @@ impl BlockData { pub(crate) fn insts( &self, ) -> impl DoubleEndedIterator + ExactSizeIterator + use<> { - (self.insts.start.index()..self.insts.end.index()).map(Inst::from_usize) + (self.insts.start.index()..self.insts.end.index()).map(Inst::new) } } @@ -882,7 +882,7 @@ impl Bytecode<'_> { pub(crate) fn block_analysis_local(&mut self) { self.init_snapshots(); - let empty_sets = ConstSetInterner::new(BitVec::new()); + let empty_sets = ConstSetInterner::new(JumpTable::default()); let mut resolved = Vec::new(); let mut stack = Vec::new(); let trace_logs = enabled!(tracing::Level::TRACE); @@ -1067,12 +1067,12 @@ impl Bytecode<'_> { let finish_block = |cfg: &mut Cfg, start: usize, end: usize| { debug_assert!(start < end, "empty block range: {start}..{end}"); let bid = cfg.blocks.push(BlockData { - insts: Inst::from_usize(start)..Inst::from_usize(end), + insts: Inst::new(start)..Inst::new(end), preds: SmallVec::new(), succs: SmallVec::new(), }); for i in start..end { - cfg.inst_to_block[Inst::from_usize(i)] = Some(bid); + cfg.inst_to_block[Inst::new(i)] = Some(bid); } }; @@ -1240,7 +1240,7 @@ impl Bytecode<'_> { } else { // Blocks the fixpoint never reached are unreachable, even when the static jumps of // other such blocks, such as a loop's back edge, target them. - for bid in reached.iter_zeros().map(Block::from_usize) { + for bid in reached.iter_zeros().map(Block::new) { for i in self.cfg.blocks[bid].insts() { self.insts[i].flags |= InstFlags::DEAD_CODE; } @@ -1382,7 +1382,9 @@ impl Bytecode<'_> { let end = (last + 1).min(self.jumpdests.len()); let start = first.min(end); (last - first < MAX_RANGE_JUMP_WIDTH).then(|| { - self.jumpdests[start..end].iter_ones().map(move |i| self.pc_to_inst(start + i)) + (start..end) + .filter(move |&pc| self.jumpdests.is_valid(pc)) + .map(move |pc| self.pc_to_inst(pc)) }) } @@ -1532,7 +1534,7 @@ impl Bytecode<'_> { // pass a stack. let no_mem = MemWords::default(); let entry_mem = MemWords::entry(self.stack_observed()); - let entry = Block::from_usize(0); + let entry = Block::new(0); parts.join(entry, 0, &[], self.stack_observed(), &[], &entry_mem, &mut const_sets, &budget); // Discovered dynamic-jump target edges per block. @@ -1954,10 +1956,7 @@ impl Bytecode<'_> { return None; } - let resolve = |inst: Inst| { - let inst = self.redirects.get(&inst).copied().unwrap_or(inst); - self.cfg.inst_to_block.get(inst).copied().flatten() - }; + let resolve = |inst: Inst| self.target_block(self.effective_inst(inst)); let fallthrough = resolve(term + 1); let fallthrough_is_target = if data.flags.contains(InstFlags::MULTI_JUMP) { self.multi_jump_targets @@ -2230,7 +2229,7 @@ impl Bytecode<'_> { } // Unknown, disabled, or an invalid immediate. op::DUP1..=op::DUP16 | op::SWAP1..=op::SWAP16 | op::DUPN | op::SWAPN | op::EXCHANGE - if inp == 0 => + if inst.is_diverging() => { return false; } @@ -2431,11 +2430,11 @@ pub(crate) mod tests { ", ); // At inst 2 (ADD), operand 0 (TOS) = 0x01, operand 1 = 0x42. - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), Some(U256::from(0x01))); - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 1), Some(U256::from(0x42))); + assert_eq!(bytecode.const_operand(Inst::new(2), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(2), 1), Some(U256::from(0x42))); // At inst 4 (MSTORE), operand 0 (TOS) = 0x00, operand 1 = 0x43 (folded ADD result). - assert_eq!(bytecode.const_operand(Inst::from_usize(4), 0), Some(U256::from(0x00))); - assert_eq!(bytecode.const_operand(Inst::from_usize(4), 1), Some(U256::from(0x43))); + assert_eq!(bytecode.const_operand(Inst::new(4), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(4), 1), Some(U256::from(0x43))); } #[test] @@ -2451,8 +2450,8 @@ pub(crate) mod tests { ", ); // At inst 3 (MSTORE), operand 0 (TOS) = 0x00, operand 1 = unknown (CALLDATALOAD result). - assert_eq!(bytecode.const_operand(Inst::from_usize(3), 0), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(3), 1), None); + assert_eq!(bytecode.const_operand(Inst::new(3), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(3), 1), None); } #[test] @@ -2473,12 +2472,12 @@ pub(crate) mod tests { STOP ", ); - assert_eq!(bytecode.const_output(Inst::from_usize(0)), Some(U256::from(0x42))); - assert_eq!(bytecode.const_output(Inst::from_usize(1)), Some(U256::from(0x01))); - assert_eq!(bytecode.const_output(Inst::from_usize(2)), Some(U256::from(0x43))); - assert_eq!(bytecode.const_output(Inst::from_usize(3)), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(0)), Some(U256::from(0x42))); + assert_eq!(bytecode.const_output(Inst::new(1)), Some(U256::ONE)); + assert_eq!(bytecode.const_output(Inst::new(2)), Some(U256::from(0x43))); + assert_eq!(bytecode.const_output(Inst::new(3)), Some(U256::ZERO)); // MSTORE has no outputs. - assert_eq!(bytecode.const_output(Inst::from_usize(4)), None); + assert_eq!(bytecode.const_output(Inst::new(4)), None); } #[test] @@ -2491,8 +2490,8 @@ pub(crate) mod tests { STOP ", ); - assert_eq!(bytecode.const_output(Inst::from_usize(0)), Some(U256::ZERO)); - assert_eq!(bytecode.const_output(Inst::from_usize(1)), None); + assert_eq!(bytecode.const_output(Inst::new(0)), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(1)), None); } /// DUPN, SWAPN, and EXCHANGE should propagate constants like their fixed counterparts. @@ -2506,9 +2505,9 @@ pub(crate) mod tests { code.push(op::STOP); // inst 18 let bytecode = analyze_code_spec(code, SpecId::AMSTERDAM); // DUPN duplicates the 17th item (bottom PUSH0 = 0x00). - assert_eq!(bytecode.const_output(Inst::from_usize(17)), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(17)), Some(U256::ZERO)); // The PUSH1 0xAA is still there. - assert_eq!(bytecode.const_output(Inst::from_usize(16)), Some(U256::from(0xAA))); + assert_eq!(bytecode.const_output(Inst::new(16)), Some(U256::from(0xAA))); } #[test] @@ -2539,26 +2538,26 @@ pub(crate) mod tests { ); // The JUMP at inst 5 should remain dynamic. assert!(bytecode.has_dynamic_jumps, "expected unresolved dynamic jump"); - let jump = bytecode.inst(Inst::from_usize(5)); + let jump = bytecode.inst(Inst::new(5)); assert!(jump.is_jump()); assert!(!jump.flags.contains(InstFlags::STATIC_JUMP)); // JUMP's TOS operand is Top (CALLDATALOAD result). - assert_eq!(bytecode.const_operand(Inst::from_usize(5), 0), None); + assert_eq!(bytecode.const_operand(Inst::new(5), 0), None); // ADD 1 - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), Some(U256::from(0x01))); - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 1), Some(U256::from(0x42))); - assert_eq!(bytecode.const_output(Inst::from_usize(2)), Some(U256::from(0x43))); + assert_eq!(bytecode.const_operand(Inst::new(2), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(2), 1), Some(U256::from(0x42))); + assert_eq!(bytecode.const_output(Inst::new(2)), Some(U256::from(0x43))); // ADD 2 - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 0), Some(U256::from(0x01))); - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 1), None); - assert_eq!(bytecode.const_output(Inst::from_usize(8)), None); + assert_eq!(bytecode.const_operand(Inst::new(8), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(8), 1), None); + assert_eq!(bytecode.const_output(Inst::new(8)), None); // SUB - assert_eq!(bytecode.const_operand(Inst::from_usize(12), 0), Some(U256::from(0x02))); - assert_eq!(bytecode.const_operand(Inst::from_usize(12), 1), None); - assert_eq!(bytecode.const_output(Inst::from_usize(12)), None); + assert_eq!(bytecode.const_operand(Inst::new(12), 0), Some(U256::from(0x02))); + assert_eq!(bytecode.const_operand(Inst::new(12), 1), None); + assert_eq!(bytecode.const_output(Inst::new(12)), None); } #[test] @@ -2757,7 +2756,7 @@ pub(crate) mod tests { // Build a stack with 70 items, where the deepest is the final JUMP target. // A dispatcher JUMP is resolvable; a later JUMP in a block that uses DUPN 70 // must also be correctly resolved (not marked INVALID_JUMP). - let code = crate::parse_asm( + let bytecode = analyze_asm_spec( " PUSH %target ; 69 × PUSH0 @@ -2786,9 +2785,8 @@ pub(crate) mod tests { JUMPDEST STOP ", - ) - .unwrap(); - let bytecode = analyze_code_spec(code, SpecId::AMSTERDAM); + SpecId::AMSTERDAM, + ); // The "problem" JUMP must NOT be marked INVALID_JUMP. let jumps: Vec<_> = bytecode.iter_insts().filter(|(_, d)| d.opcode == op::JUMP).collect(); @@ -2857,7 +2855,7 @@ pub(crate) mod tests { assert!(!bytecode.has_dynamic_jumps, "expected all jumps to be resolved"); } - fn fixture_entry_code(json: &str) -> Vec { + pub(crate) fn fixture_entry_code(json: &str) -> Vec { let v: serde_json::Value = serde_json::from_str(json).unwrap(); let case = v.as_object().unwrap().values().next().unwrap(); let transaction = &case["transaction"]; @@ -2871,8 +2869,7 @@ pub(crate) mod tests { }) .and_then(|to| to.as_str()) .unwrap(); - let code = case["pre"][to]["code"].as_str().unwrap().trim_start_matches("0x"); - hex::decode(code).unwrap() + hex::decode(case["pre"][to]["code"].as_str().unwrap()).unwrap() } } @@ -2981,10 +2978,10 @@ mod tests_edge_cases { ", ); // DUP2 output should be 0xAA. - assert_eq!(bytecode.const_output(Inst::from_usize(2)), Some(U256::from(0xAA))); + assert_eq!(bytecode.const_output(Inst::new(2)), Some(U256::from(0xAA))); // At MSTORE (inst 5): TOS = 0x00, second = 0xBB (swapped back). - assert_eq!(bytecode.const_operand(Inst::from_usize(5), 0), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(5), 1), Some(U256::from(0xBB))); + assert_eq!(bytecode.const_operand(Inst::new(5), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(5), 1), Some(U256::from(0xBB))); } /// JUMPI with a constant condition should still record both operands. @@ -3094,7 +3091,7 @@ mod tests_edge_cases { let (_, jump) = bytecode.iter_insts().find(|(_, data)| data.opcode == op::JUMPI).unwrap(); assert!(jump.flags.contains(InstFlags::STATIC_JUMP)); assert!(!jump.flags.contains(InstFlags::INVALID_JUMP)); - assert_eq!(jump.static_jump_target(), Inst::from_usize(3)); + assert_eq!(jump.static_jump_target(), Inst::new(3)); assert!(!bytecode.has_dynamic_jumps); } @@ -3115,7 +3112,7 @@ mod tests_edge_cases { let (_, jump) = bytecode.iter_insts().find(|(_, data)| data.opcode == op::JUMPI).unwrap(); assert!(jump.flags.contains(InstFlags::STATIC_JUMP)); assert!(!jump.flags.contains(InstFlags::INVALID_JUMP)); - assert_eq!(jump.static_jump_target(), Inst::from_usize(3)); + assert_eq!(jump.static_jump_target(), Inst::new(3)); assert!(!bytecode.has_dynamic_jumps); } @@ -3138,12 +3135,8 @@ mod tests_edge_cases { bytecode.iter_insts().find(|(_, data)| data.opcode == op::JUMPI).unwrap(); let jump_block = bytecode.cfg.inst_to_block[jump_inst].unwrap(); let fallthrough_block = bytecode.cfg.inst_to_block[jump_inst + 1].unwrap(); - let target_inst = bytecode - .iter_all_insts() - .rev() - .find(|(_, data)| data.opcode == op::JUMPDEST) - .unwrap() - .0; + let target_inst = + bytecode.iter_all_insts().rev().find(|(_, data)| data.is_jumpdest()).unwrap().0; assert!(jump.has_const_jumpi_condition()); assert_eq!(jump.static_jump_target(), jump_inst + 1); @@ -3177,12 +3170,8 @@ mod tests_edge_cases { bytecode.iter_insts().find(|(_, data)| data.opcode == op::JUMPI).unwrap(); let jump_block = bytecode.cfg.inst_to_block[jump_inst].unwrap(); let fallthrough_block = bytecode.cfg.inst_to_block[jump_inst + 1].unwrap(); - let target_inst = bytecode - .iter_all_insts() - .rev() - .find(|(_, data)| data.opcode == op::JUMPDEST) - .unwrap() - .0; + let target_inst = + bytecode.iter_all_insts().rev().find(|(_, data)| data.is_jumpdest()).unwrap().0; assert!(jump.has_const_jumpi_condition()); assert_eq!(jump.static_jump_target(), jump_inst + 1); @@ -3698,10 +3687,10 @@ mod tests_edge_cases { ); // DUP1 at inst 2: counter is loop-variant. - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), None); + assert_eq!(bytecode.const_operand(Inst::new(2), 0), None); // ADD at inst 9: TOS=1 (const), second=counter (Top). - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::from(1))); - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 1), None); + assert_eq!(bytecode.const_operand(Inst::new(9), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(9), 1), None); } /// Loop counter incremented via single-caller subroutine must be Top. @@ -3761,7 +3750,7 @@ mod tests_edge_cases { ", ); - assert_eq!(bytecode.const_operand(Inst::from_usize(2), 0), None); + assert_eq!(bytecode.const_operand(Inst::new(2), 0), None); } /// Loop counter incremented via multi-caller subroutine must be Top. @@ -3833,7 +3822,7 @@ mod tests_edge_cases { ); // DUP1 at inst 11: counter is loop-variant. - assert_eq!(bytecode.const_operand(Inst::from_usize(11), 0), None); + assert_eq!(bytecode.const_operand(Inst::new(11), 0), None); } /// loopExp statetest: loop counters through checked_add subroutine must be Top. @@ -3882,10 +3871,10 @@ mod tests_edge_cases { ); assert!(bytecode.has_dynamic_jumps); // Block-local constant survives. - assert_eq!(bytecode.const_output(Inst::from_usize(8)), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(8)), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(9), 0), Some(U256::ZERO)); // Cross-block value must be restored to Top. - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 1), None); + assert_eq!(bytecode.const_operand(Inst::new(9), 1), None); } /// Same as above but asserting const_output: block-local PUSH output survives, @@ -3912,11 +3901,11 @@ mod tests_edge_cases { ); assert!(bytecode.has_dynamic_jumps); // Purely local push output preserved. - assert_eq!(bytecode.const_output(Inst::from_usize(8)), Some(U256::from(0x01))); + assert_eq!(bytecode.const_output(Inst::new(8)), Some(U256::ONE)); // ADD: local operand preserved, inherited operand restored to Top. - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::from(0x01))); - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 1), None); - assert_eq!(bytecode.const_output(Inst::from_usize(9)), None); + assert_eq!(bytecode.const_operand(Inst::new(9), 0), Some(U256::ONE)); + assert_eq!(bytecode.const_operand(Inst::new(9), 1), None); + assert_eq!(bytecode.const_output(Inst::new(9)), None); } /// Fallthrough block after a JUMPI in a suspect JUMPDEST block becomes @@ -3951,10 +3940,10 @@ mod tests_edge_cases { ); assert!(bytecode.has_dynamic_jumps); // Block-local constant in propagated-suspect fallthrough survives. - assert_eq!(bytecode.const_output(Inst::from_usize(13)), Some(U256::ZERO)); - assert_eq!(bytecode.const_operand(Inst::from_usize(14), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_output(Inst::new(13)), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(14), 0), Some(U256::ZERO)); // Cross-block inherited value restored to Top. - assert_eq!(bytecode.const_operand(Inst::from_usize(14), 1), None); + assert_eq!(bytecode.const_operand(Inst::new(14), 1), None); } /// Non-suspect blocks must retain their fixpoint-derived snapshots even when @@ -3994,16 +3983,16 @@ mod tests_edge_cases { assert!(bytecode.has_dynamic_jumps); // Fallthrough block: block-local constants survive. // ADD - assert_eq!(bytecode.const_operand(Inst::from_usize(6), 0), Some(U256::from(0x0B))); - assert_eq!(bytecode.const_operand(Inst::from_usize(6), 1), Some(U256::from(0x0A))); - assert_eq!(bytecode.const_output(Inst::from_usize(6)), Some(U256::from(0x15))); + assert_eq!(bytecode.const_operand(Inst::new(6), 0), Some(U256::from(0x0B))); + assert_eq!(bytecode.const_operand(Inst::new(6), 1), Some(U256::from(0x0A))); + assert_eq!(bytecode.const_output(Inst::new(6)), Some(U256::from(0x15))); // MSTORE 1 - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 0), Some(U256::from(0))); - assert_eq!(bytecode.const_operand(Inst::from_usize(8), 1), Some(U256::from(0x15))); + assert_eq!(bytecode.const_operand(Inst::new(8), 0), Some(U256::ZERO)); + assert_eq!(bytecode.const_operand(Inst::new(8), 1), Some(U256::from(0x15))); // MLOAD: single predecessor (no JUMPDEST), so cross-block value resolves. - assert_eq!(bytecode.const_operand(Inst::from_usize(9), 0), Some(U256::from(0x69))); + assert_eq!(bytecode.const_operand(Inst::new(9), 0), Some(U256::from(0x69))); // Target block: block-local PUSH survives. - assert_eq!(bytecode.const_output(Inst::from_usize(18)), Some(U256::from(0x42))); + assert_eq!(bytecode.const_output(Inst::new(18)), Some(U256::from(0x42))); } /// JUMPI in a suspect JUMPDEST block: the fallthrough block inherits @@ -4048,9 +4037,9 @@ mod tests_edge_cases { assert!(bytecode.has_dynamic_jumps); // ADD at inst 18: TOS (depth 0) = 0xBB (block-local), depth 1 = Top // (incoming from suspect predecessor, block-local pass saw it as Top). - assert_eq!(bytecode.const_operand(Inst::from_usize(18), 0), Some(U256::from(0xBB))); - assert_eq!(bytecode.const_operand(Inst::from_usize(18), 1), None); - assert_eq!(bytecode.const_output(Inst::from_usize(18)), None); + assert_eq!(bytecode.const_operand(Inst::new(18), 0), Some(U256::from(0xBB))); + assert_eq!(bytecode.const_operand(Inst::new(18), 1), None); + assert_eq!(bytecode.const_output(Inst::new(18)), None); } /// Bytecode that is just STOP — minimal edge case. @@ -4133,7 +4122,7 @@ mod tests_edge_cases { .iter_insts() .filter(|(_, d)| d.is_jump() && !d.is_dead_code()) // `tramp` is the first `JUMPDEST JUMP` block. - .find(|&(i, _)| bytecode.inst(i - 1).opcode == op::JUMPDEST) + .find(|&(i, _)| bytecode.inst(i - 1).is_jumpdest()) .unwrap(); assert!(!tramp_jump.1.flags.contains(InstFlags::STATIC_JUMP)); } @@ -4261,11 +4250,11 @@ mod tests_edge_cases { ", ); assert!(!bytecode.has_dynamic_jumps); - assert_eq!(bytecode.const_operand(Inst::from_usize(16), 1), Some(U256::from(7))); - assert_eq!(bytecode.const_operand(Inst::from_usize(16), 0), None); - assert_eq!(bytecode.const_output(Inst::from_usize(16)), None); - let targets = bytecode.multi_jump_targets(Inst::from_usize(18)).unwrap(); - assert_eq!(targets, [Inst::from_usize(5), Inst::from_usize(12)]); + assert_eq!(bytecode.const_operand(Inst::new(16), 1), Some(U256::from(7))); + assert_eq!(bytecode.const_operand(Inst::new(16), 0), None); + assert_eq!(bytecode.const_output(Inst::new(16)), None); + let targets = bytecode.multi_jump_targets(Inst::new(18)).unwrap(); + assert_eq!(targets, [Inst::new(5), Inst::new(12)]); } /// A recursive function's return must resolve to all of its return sites. @@ -4411,14 +4400,14 @@ mod tests_edge_cases { // Memory that was never written is not assumed to be zero. assert_eq!(mload_0x40_after("PUSH 0x60 MLOAD POP"), known); let bytecode = analyze_asm("PUSH 0x60 MLOAD PUSH0 MSTORE STOP"); - assert_eq!(bytecode.const_output(Inst::from_usize(1)), None); + assert_eq!(bytecode.const_output(Inst::new(1)), None); } #[test] fn mload_known_word_after_mstore() { let known = Some(U256::from(0x80)); assert_eq!(mload_0x40_after("PUSH 1 PUSH0 CALLDATALOAD MSTORE"), None); - assert_eq!(mload_0x40_after("PUSH 1 PUSH 0x40 MSTORE"), Some(U256::from(1))); + assert_eq!(mload_0x40_after("PUSH 1 PUSH 0x40 MSTORE"), Some(U256::ONE)); assert_eq!(mload_0x40_after("PUSH0 CALLDATALOAD PUSH 0x40 MSTORE"), None); assert_eq!(mload_0x40_after("PUSH 1 PUSH 0x21 MSTORE"), None); assert_eq!(mload_0x40_after("PUSH 1 PUSH 0x5f MSTORE"), None); diff --git a/crates/jit/codegen/src/bytecode/passes/const_fold.rs b/crates/jit/codegen/src/bytecode/passes/const_fold.rs index 6292b656d..497a39a66 100644 --- a/crates/jit/codegen/src/bytecode/passes/const_fold.rs +++ b/crates/jit/codegen/src/bytecode/passes/const_fold.rs @@ -129,10 +129,10 @@ pub(crate) fn try_const_fold( op::EXP => a.pow(b), op::SIGNEXTEND => { if a < U256::from(31) { - let ext = a.as_limbs()[0]; + let ext = a.wrapping_to::(); let bit_index = (8 * ext + 7) as usize; let bit = b.bit(bit_index); - let mask = (U256::from(1) << bit_index) - U256::from(1); + let mask = (U256::ONE << bit_index) - U256::ONE; if bit { b | !mask } else { b & mask } } else { b @@ -479,7 +479,6 @@ mod tests { /// Operands are in natural order matching the yellow paper specification: /// for `SUB(a, b)` = `a - b`, pass `&[a, b]` (first popped = first element). fn const_fold(opcode: u8, operands: &[U256]) -> Option { - use std::fmt::Write; let mut src = String::new(); let mut num_insts = 0usize; // Push in reverse so that operands[0] ends up on TOS. @@ -495,7 +494,7 @@ mod tests { let bytecode = analyze_asm(&src); // The folded result is operand 1 (second from top) at MSTORE. - bytecode.const_operand(Inst::from_usize(mstore_inst), 1) + bytecode.const_operand(Inst::new(mstore_inst), 1) } #[test] @@ -510,7 +509,7 @@ mod tests { #[test] fn const_fold_1_to_1() { // ISZERO. - assert_eq!(const_fold(op::ISZERO, &[U256::ZERO]), Some(U256::from(1))); + assert_eq!(const_fold(op::ISZERO, &[U256::ZERO]), Some(U256::ONE)); assert_eq!(const_fold(op::ISZERO, &[U256::from(42)]), Some(U256::ZERO)); // NOT. @@ -521,7 +520,7 @@ mod tests { #[test] fn const_fold_add() { assert_eq!(const_fold(op::ADD, &[U256::from(3), U256::from(4)]), Some(U256::from(7))); - assert_eq!(const_fold(op::ADD, &[U256::MAX, U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::ADD, &[U256::MAX, U256::ONE]), Some(U256::ZERO)); } #[test] @@ -534,7 +533,7 @@ mod tests { fn const_fold_sub() { // SUB(10, 3) = 7. assert_eq!(const_fold(op::SUB, &[U256::from(10), U256::from(3)]), Some(U256::from(7))); - assert_eq!(const_fold(op::SUB, &[U256::ZERO, U256::from(1)]), Some(U256::MAX)); + assert_eq!(const_fold(op::SUB, &[U256::ZERO, U256::ONE]), Some(U256::MAX)); } #[test] @@ -557,7 +556,7 @@ mod tests { #[test] fn const_fold_mod() { // MOD(10, 3) = 1. - assert_eq!(const_fold(op::MOD, &[U256::from(10), U256::from(3)]), Some(U256::from(1))); + assert_eq!(const_fold(op::MOD, &[U256::from(10), U256::from(3)]), Some(U256::ONE)); assert_eq!(const_fold(op::MOD, &[U256::from(7), U256::ZERO]), Some(U256::ZERO)); } @@ -580,7 +579,7 @@ mod tests { Some(U256::from(4)), ); assert_eq!( - const_fold(op::ADDMOD, &[U256::from(1), U256::from(2), U256::ZERO]), + const_fold(op::ADDMOD, &[U256::ONE, U256::from(2), U256::ZERO]), Some(U256::ZERO), ); } @@ -599,7 +598,7 @@ mod tests { // EXP(2, 10) = 1024. assert_eq!(const_fold(op::EXP, &[U256::from(2), U256::from(10)]), Some(U256::from(1024))); // EXP(5, 0) = 1. - assert_eq!(const_fold(op::EXP, &[U256::from(5), U256::ZERO]), Some(U256::from(1))); + assert_eq!(const_fold(op::EXP, &[U256::from(5), U256::ZERO]), Some(U256::ONE)); } #[test] @@ -621,12 +620,12 @@ mod tests { #[test] fn const_fold_lt_gt_eq() { // LT(1, 2) = 1. - assert_eq!(const_fold(op::LT, &[U256::from(1), U256::from(2)]), Some(U256::from(1))); - assert_eq!(const_fold(op::LT, &[U256::from(2), U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::LT, &[U256::ONE, U256::from(2)]), Some(U256::ONE)); + assert_eq!(const_fold(op::LT, &[U256::from(2), U256::ONE]), Some(U256::ZERO)); // GT(2, 1) = 1. - assert_eq!(const_fold(op::GT, &[U256::from(2), U256::from(1)]), Some(U256::from(1))); - assert_eq!(const_fold(op::GT, &[U256::from(1), U256::from(2)]), Some(U256::ZERO)); - assert_eq!(const_fold(op::EQ, &[U256::from(5), U256::from(5)]), Some(U256::from(1))); + assert_eq!(const_fold(op::GT, &[U256::from(2), U256::ONE]), Some(U256::ONE)); + assert_eq!(const_fold(op::GT, &[U256::ONE, U256::from(2)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::EQ, &[U256::from(5), U256::from(5)]), Some(U256::ONE)); assert_eq!(const_fold(op::EQ, &[U256::from(5), U256::from(6)]), Some(U256::ZERO)); } @@ -634,11 +633,11 @@ mod tests { fn const_fold_slt_sgt() { let neg1 = U256::MAX; // SLT(-1, 1) = 1. - assert_eq!(const_fold(op::SLT, &[neg1, U256::from(1)]), Some(U256::from(1))); + assert_eq!(const_fold(op::SLT, &[neg1, U256::ONE]), Some(U256::ONE)); // SLT(1, -1) = 0. - assert_eq!(const_fold(op::SLT, &[U256::from(1), neg1]), Some(U256::ZERO)); + assert_eq!(const_fold(op::SLT, &[U256::ONE, neg1]), Some(U256::ZERO)); // SGT(1, -1) = 1. - assert_eq!(const_fold(op::SGT, &[U256::from(1), neg1]), Some(U256::from(1))); + assert_eq!(const_fold(op::SGT, &[U256::ONE, neg1]), Some(U256::ONE)); } #[test] @@ -670,26 +669,23 @@ mod tests { #[test] fn const_fold_shl_shr() { // SHL(1, 0x80) = 0x100. - assert_eq!( - const_fold(op::SHL, &[U256::from(1), U256::from(0x80)]), - Some(U256::from(0x100)), - ); - assert_eq!(const_fold(op::SHL, &[U256::from(256), U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::SHL, &[U256::ONE, U256::from(0x80)]), Some(U256::from(0x100)),); + assert_eq!(const_fold(op::SHL, &[U256::from(256), U256::ONE]), Some(U256::ZERO)); // SHR(1, 0x80) = 0x40. - assert_eq!(const_fold(op::SHR, &[U256::from(1), U256::from(0x80)]), Some(U256::from(0x40)),); - assert_eq!(const_fold(op::SHR, &[U256::from(256), U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::SHR, &[U256::ONE, U256::from(0x80)]), Some(U256::from(0x40)),); + assert_eq!(const_fold(op::SHR, &[U256::from(256), U256::ONE]), Some(U256::ZERO)); } #[test] fn const_fold_sar() { // SAR(1, 2) = 1. - assert_eq!(const_fold(op::SAR, &[U256::from(1), U256::from(2)]), Some(U256::from(1))); + assert_eq!(const_fold(op::SAR, &[U256::ONE, U256::from(2)]), Some(U256::ONE)); // SAR(4, -1) = -1. assert_eq!(const_fold(op::SAR, &[U256::from(4), U256::MAX]), Some(U256::MAX)); // SAR(256, -1) = -1. assert_eq!(const_fold(op::SAR, &[U256::from(256), U256::MAX]), Some(U256::MAX)); // SAR(256, 1) = 0. - assert_eq!(const_fold(op::SAR, &[U256::from(256), U256::from(1)]), Some(U256::ZERO)); + assert_eq!(const_fold(op::SAR, &[U256::from(256), U256::ONE]), Some(U256::ZERO)); } /// Builds bytecode for N repetitions of `PUSH U256::MAX, PUSH U256::MAX, EXP, POP`. @@ -811,7 +807,7 @@ mod tests { assert_eq!(bytecode.compiler_gas_used, 0); // inst layout: PUSH(0), PUSH(1), ADD(2), PUSH0(3), MSTORE(4), STOP(5). // The ADD result should NOT be folded — operand 1 at MSTORE should be None. - assert!(bytecode.const_operand(Inst::from_usize(4), 1).is_none()); + assert!(bytecode.const_operand(Inst::new(4), 1).is_none()); } /// Verify that a very large gas limit allows all folding to proceed. @@ -827,7 +823,7 @@ mod tests { assert!(bytecode.compiler_gas_used > 0); // inst layout: PUSH(0), PUSH(1), ADD(2), PUSH0(3), MSTORE(4), STOP(5). - assert_eq!(bytecode.const_operand(Inst::from_usize(4), 1), Some(U256::from(5))); + assert_eq!(bytecode.const_operand(Inst::new(4), 1), Some(U256::from(5))); } /// Exhaustively verify that `const_fold_gas` and `try_const_fold` agree on which opcodes @@ -836,7 +832,7 @@ mod tests { #[test] fn const_fold_gas_sync() { let mut interner: crate::bytecode::U256Interner = crate::bytecode::Interner::new(); - let one = U256Imm::new(U256::from(1), &mut interner); + let one = U256Imm::new(U256::ONE, &mut interner); let two = U256Imm::new(U256::from(2), &mut interner); let three = U256Imm::new(U256::from(3), &mut interner); let c1 = AbsValue::Const(one); @@ -879,7 +875,7 @@ mod tests { (250, 258), ((1 << 31) - 4, (1 << 31) - 1), ]; - let consts = [U256::ZERO, U256::from(7), U256::from(225), U256::from(1) << 224, U256::MAX]; + let consts = [U256::ZERO, U256::from(7), U256::from(225), U256::ONE << 224, U256::MAX]; let mut inputs: Vec<_> = ranges.into_iter().map(|(lo, hi)| AbsValue::Range((lo..=hi).into())).collect(); inputs.extend(consts.into_iter().map(|c| AbsValue::Const(U256Imm::new(c, interner)))); @@ -895,7 +891,7 @@ mod tests { AbsValue::Top => [0u64, 1, 2, 7, 31, 32, 255, 256, (1 << 31) - 1, 1 << 31] .into_iter() .map(U256::from) - .chain([U256::from(1) << 224, U256::from(1) << 255, U256::MAX - U256::from(1)]) + .chain([U256::ONE << 224, U256::ONE << 255, U256::MAX - U256::ONE]) .chain([U256::MAX]) .collect(), AbsValue::ConstSet(_) => unreachable!(), @@ -968,88 +964,97 @@ mod tests { let (c16, c40, c224, c255, c256) = (c(16), c(40), c(224), c(255), c(256)); let (c0xffff, word) = (c(0xffff), c(0x1100_2233)); let mut c = |v: U256| AbsValue::Const(U256Imm::new(v, &mut interner)); - let (c_big, c_max) = (c(U256::from(1) << 226), c(U256::MAX)); + let (c_big, c_max) = (c(U256::ONE << 226), c(U256::MAX)); let top = AbsValue::Top; - // (opcode, operands in natural order, expected bounds). - type Case<'a> = (u8, &'a [AbsValue], Option<(u32, u32)>); - let cases: &[Case<'_>] = &[ - (op::ADD, &[r0_3, r5_9], Some((5, 12))), - (op::ADD, &[r0_3, top], None), - (op::SUB, &[r5_9, r0_3], Some((2, 9))), + struct Case { + opcode: u8, + operands: Vec, + expected: Option<(u32, u32)>, + } + + let case = |opcode, operands: &[AbsValue], expected| Case { + opcode, + operands: operands.to_vec(), + expected, + }; + let cases = [ + case(op::ADD, &[r0_3, r5_9], Some((5, 12))), + case(op::ADD, &[r0_3, top], None), + case(op::SUB, &[r5_9, r0_3], Some((2, 9))), // `1 - 4` wraps. - (op::SUB, &[r1_4, r1_4], None), - (op::SUB, &[r0_3, c1], None), - (op::MUL, &[r0_3, c40], Some((0, 120))), - (op::DIV, &[r5_9, r1_4], Some((1, 9))), - (op::DIV, &[r5_9, r0_3], Some((0, 9))), - (op::DIV, &[r5_9, top], Some((0, 9))), - (op::DIV, &[top, c_big], Some((0, (1 << 30) - 1))), - (op::DIV, &[top, c224], None), - (op::DIV, &[top, c0], Some((0, 0))), - (op::SDIV, &[r5_9, r1_4], Some((1, 9))), - (op::SDIV, &[r5_9, top], None), + case(op::SUB, &[r1_4, r1_4], None), + case(op::SUB, &[r0_3, c1], None), + case(op::MUL, &[r0_3, c40], Some((0, 120))), + case(op::DIV, &[r5_9, r1_4], Some((1, 9))), + case(op::DIV, &[r5_9, r0_3], Some((0, 9))), + case(op::DIV, &[r5_9, top], Some((0, 9))), + case(op::DIV, &[top, c_big], Some((0, (1 << 30) - 1))), + case(op::DIV, &[top, c224], None), + case(op::DIV, &[top, c0], Some((0, 0))), + case(op::SDIV, &[r5_9, r1_4], Some((1, 9))), + case(op::SDIV, &[r5_9, top], None), // Solar's selector dispatch indexes with `selector % 16`. - (op::MOD, &[top, c16], Some((0, 15))), - (op::MOD, &[r0_3, c5], Some((0, 3))), - (op::MOD, &[r5_9, r0_3], Some((0, 2))), - (op::MOD, &[r5_9, c_max], Some((5, 9))), - (op::MOD, &[top, c0], Some((0, 0))), - (op::MOD, &[top, top], None), - (op::SMOD, &[r5_9, c3], Some((0, 2))), - (op::SMOD, &[r5_9, c_max], Some((0, 9))), - (op::SMOD, &[top, c16], None), - (op::ADDMOD, &[r0_3, r0_3, c40], Some((0, 6))), - (op::ADDMOD, &[top, top, c40], Some((0, 39))), - (op::ADDMOD, &[top, top, c0], Some((0, 0))), - (op::MULMOD, &[r1_4, r1_4, top], Some((0, 16))), - (op::EXP, &[c2, r0_3], Some((1, 8))), - (op::EXP, &[r0_3, c2], Some((0, 9))), - (op::EXP, &[r0_3, r0_3], Some((0, 27))), - (op::EXP, &[c2, r0_39], None), - (op::SIGNEXTEND, &[c0, r0_3], Some((0, 3))), - (op::SIGNEXTEND, &[c0, r0_255], None), - (op::SIGNEXTEND, &[c1, r0_255], Some((0, 255))), - (op::SIGNEXTEND, &[top, r0_39], Some((0, 39))), - (op::LT, &[r0_3, r5_9], Some((1, 1))), - (op::LT, &[r5_9, r0_3], Some((0, 0))), - (op::LT, &[r0_3, r1_4], Some((0, 1))), - (op::GT, &[r5_9, r0_3], Some((1, 1))), - (op::SLT, &[top, c0], Some((0, 1))), - (op::SGT, &[r0_3, r5_9], Some((0, 0))), - (op::EQ, &[r0_3, r5_9], Some((0, 0))), - (op::EQ, &[top, top], Some((0, 1))), - (op::ISZERO, &[r1_4], Some((0, 0))), - (op::ISZERO, &[r0_3], Some((0, 1))), - (op::ISZERO, &[top], Some((0, 1))), - (op::AND, &[top, c0xffff], Some((0, 0xffff))), - (op::AND, &[r5_9, r0_3], Some((0, 3))), - (op::AND, &[top, c_max], None), - (op::OR, &[r5_9, r1_4], Some((5, 15))), - (op::OR, &[r0_3, top], None), - (op::XOR, &[r5_9, r0_3], Some((0, 15))), - (op::NOT, &[r0_3], None), - (op::BYTE, &[r(28, 31), word], Some((0x00, 0x33))), - (op::BYTE, &[r(30, 40), word], Some((0, 0x33))), - (op::BYTE, &[top, top], Some((0, 255))), - (op::BYTE, &[r(0, 32), r0_39], Some((0, 39))), - (op::SHL, &[r0_3, c1], Some((1, 8))), - (op::SHL, &[c256, top], Some((0, 0))), - (op::SHL, &[r(0, 40), c1], None), - (op::SHR, &[c224, top], None), - (op::SHR, &[r(225, 230), top], Some((0, (1 << 31) - 1))), - (op::SHR, &[c255, top], Some((0, 1))), - (op::SHR, &[r0_3, r(16, 31)], Some((2, 31))), - (op::SHR, &[r(0, 16), c_big], None), - (op::SHR, &[r(222, 230), c_big], Some((0, 16))), - (op::SAR, &[r0_3, r(16, 31)], Some((2, 31))), - (op::SAR, &[r(220, 230), c_max], None), - (op::CLZ, &[r1_4], Some((253, 255))), - (op::CLZ, &[r0_3], Some((254, 256))), - (op::CLZ, &[top], Some((0, 256))), - (op::CALLDATALOAD, &[r0_3], None), + case(op::MOD, &[top, c16], Some((0, 15))), + case(op::MOD, &[r0_3, c5], Some((0, 3))), + case(op::MOD, &[r5_9, r0_3], Some((0, 2))), + case(op::MOD, &[r5_9, c_max], Some((5, 9))), + case(op::MOD, &[top, c0], Some((0, 0))), + case(op::MOD, &[top, top], None), + case(op::SMOD, &[r5_9, c3], Some((0, 2))), + case(op::SMOD, &[r5_9, c_max], Some((0, 9))), + case(op::SMOD, &[top, c16], None), + case(op::ADDMOD, &[r0_3, r0_3, c40], Some((0, 6))), + case(op::ADDMOD, &[top, top, c40], Some((0, 39))), + case(op::ADDMOD, &[top, top, c0], Some((0, 0))), + case(op::MULMOD, &[r1_4, r1_4, top], Some((0, 16))), + case(op::EXP, &[c2, r0_3], Some((1, 8))), + case(op::EXP, &[r0_3, c2], Some((0, 9))), + case(op::EXP, &[r0_3, r0_3], Some((0, 27))), + case(op::EXP, &[c2, r0_39], None), + case(op::SIGNEXTEND, &[c0, r0_3], Some((0, 3))), + case(op::SIGNEXTEND, &[c0, r0_255], None), + case(op::SIGNEXTEND, &[c1, r0_255], Some((0, 255))), + case(op::SIGNEXTEND, &[top, r0_39], Some((0, 39))), + case(op::LT, &[r0_3, r5_9], Some((1, 1))), + case(op::LT, &[r5_9, r0_3], Some((0, 0))), + case(op::LT, &[r0_3, r1_4], Some((0, 1))), + case(op::GT, &[r5_9, r0_3], Some((1, 1))), + case(op::SLT, &[top, c0], Some((0, 1))), + case(op::SGT, &[r0_3, r5_9], Some((0, 0))), + case(op::EQ, &[r0_3, r5_9], Some((0, 0))), + case(op::EQ, &[top, top], Some((0, 1))), + case(op::ISZERO, &[r1_4], Some((0, 0))), + case(op::ISZERO, &[r0_3], Some((0, 1))), + case(op::ISZERO, &[top], Some((0, 1))), + case(op::AND, &[top, c0xffff], Some((0, 0xffff))), + case(op::AND, &[r5_9, r0_3], Some((0, 3))), + case(op::AND, &[top, c_max], None), + case(op::OR, &[r5_9, r1_4], Some((5, 15))), + case(op::OR, &[r0_3, top], None), + case(op::XOR, &[r5_9, r0_3], Some((0, 15))), + case(op::NOT, &[r0_3], None), + case(op::BYTE, &[r(28, 31), word], Some((0x00, 0x33))), + case(op::BYTE, &[r(30, 40), word], Some((0, 0x33))), + case(op::BYTE, &[top, top], Some((0, 255))), + case(op::BYTE, &[r(0, 32), r0_39], Some((0, 39))), + case(op::SHL, &[r0_3, c1], Some((1, 8))), + case(op::SHL, &[c256, top], Some((0, 0))), + case(op::SHL, &[r(0, 40), c1], None), + case(op::SHR, &[c224, top], None), + case(op::SHR, &[r(225, 230), top], Some((0, (1 << 31) - 1))), + case(op::SHR, &[c255, top], Some((0, 1))), + case(op::SHR, &[r0_3, r(16, 31)], Some((2, 31))), + case(op::SHR, &[r(0, 16), c_big], None), + case(op::SHR, &[r(222, 230), c_big], Some((0, 16))), + case(op::SAR, &[r0_3, r(16, 31)], Some((2, 31))), + case(op::SAR, &[r(220, 230), c_max], None), + case(op::CLZ, &[r1_4], Some((253, 255))), + case(op::CLZ, &[r0_3], Some((254, 256))), + case(op::CLZ, &[top], Some((0, 256))), + case(op::CALLDATALOAD, &[r0_3], None), ]; - for &(opcode, operands, expected) in cases { + for Case { opcode, operands, expected } in cases { let inputs: Vec<_> = operands.iter().rev().copied().collect(); let folded = try_range_fold(opcode, &inputs, &mut interner).map(|v| { let r = v.bounds().unwrap(); diff --git a/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs b/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs index bbb4df0a6..510df28eb 100644 --- a/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs +++ b/crates/jit/codegen/src/bytecode/passes/dead_store_elim.rs @@ -15,7 +15,7 @@ use super::{ StackSectionAnalysis, block_analysis::{AbsValue, Snapshots}, }; -use crate::bytecode::{AnalysisConfig, Bytecode, Inst, InstFlags}; +use crate::bytecode::{Bytecode, Inst, InstFlags}; use bitvec::vec::BitVec; use evm2::interpreter::op; @@ -37,7 +37,7 @@ impl Bytecode<'_> { #[instrument(name = "dse", level = "debug", skip_all)] pub(crate) fn dead_store_elim(&mut self) { let mut eliminated = 0u32; - let inspect_stack = self.config.contains(AnalysisConfig::INSPECT_STACK); + let inspect_stack = self.stack_observed(); // Reusable buffers hoisted out of the per-block loop. let mut heights = Vec::new(); @@ -69,7 +69,7 @@ impl Bytecode<'_> { // If the block's terminator diverges, no successor will read the stack, // so all exit positions are dead unless `inspect_stack` is set. - let term_diverges = !inspect_stack && self.insts[block.terminator()].is_diverging(); + let term_diverges = !inspect_stack && self.is_instr_diverging(block.terminator()); live.clear(); live.resize(exit_height.max(0) as usize, !term_diverges); @@ -86,6 +86,7 @@ impl Bytecode<'_> { if data.flags.contains(InstFlags::NOOP) || data.flags.contains(InstFlags::DISABLED) || data.flags.contains(InstFlags::UNKNOWN) + || data.flags.contains(InstFlags::INVALID_IMMEDIATE) { continue; } @@ -241,38 +242,25 @@ fn transfer_liveness( match opcode { op::SWAP1..=op::SWAP16 => { let depth = (opcode - op::SWAP1 + 1) as usize; - transfer_swap(live, h_before, depth); + transfer_exchange(live, h_before, 0, depth); } op::SWAPN => { - if let Some(DecodedImm::Single(depth)) = imm - && (depth as usize) < h_before - { - transfer_swap(live, h_before, depth as usize); + if let Some(DecodedImm::Single(depth)) = imm { + transfer_exchange(live, h_before, 0, depth as usize); } - // Infeasible depth or decode failure: conservative no-op. } op::DUP1..=op::DUP16 => { let depth = (opcode - op::DUP1 + 1) as usize; transfer_dup(live, h_before, depth); } op::DUPN => { - if let Some(DecodedImm::Single(depth)) = imm - && depth >= 1 - && (depth as usize) <= h_before - { + if let Some(DecodedImm::Single(depth)) = imm { transfer_dup(live, h_before, depth as usize); } } op::EXCHANGE => { - if let Some(DecodedImm::Pair(n, m)) = imm - && (m as usize) < h_before - { - let tos = h_before - 1; - let pos_a = tos - n as usize; - let pos_b = tos - m as usize; - let (a, b) = (live[pos_a], live[pos_b]); - live.set(pos_a, b); - live.set(pos_b, a); + if let Some(DecodedImm::Pair(n, m)) = imm { + transfer_exchange(live, h_before, n as usize, m as usize); } } op::POP => { @@ -322,13 +310,13 @@ fn generic_transfer( } } -/// SWAP liveness: permute liveness of TOS and the swapped position. -fn transfer_swap(live: &mut BitVec, h_before: usize, depth: usize) { +/// SWAP/EXCHANGE liveness: permute liveness of the positions `n` and `m` below TOS. +fn transfer_exchange(live: &mut BitVec, h_before: usize, n: usize, m: usize) { let tos = h_before - 1; - let other = tos - depth; - let (a, b) = (live[tos], live[other]); - live.set(tos, b); - live.set(other, a); + let (pos_a, pos_b) = (tos - n, tos - m); + let (a, b) = (live[pos_a], live[pos_b]); + live.set(pos_a, b); + live.set(pos_b, a); } /// Returns `true` if all positions actually written by the instruction are dead. @@ -347,21 +335,18 @@ fn all_outputs_dead( match (opcode, imm) { // SWAP: only TOS and the deep slot are written. (op::SWAP1..=op::SWAP16, _) => { - swap_all_dead(live, h_before, (opcode - op::SWAP1 + 1) as usize) + exchange_all_dead(live, h_before, 0, (opcode - op::SWAP1 + 1) as usize) } - (op::SWAPN, Some(DecodedImm::Single(d))) if (d as usize) < h_before => { - swap_all_dead(live, h_before, d as usize) + (op::SWAPN, Some(DecodedImm::Single(d))) => { + exchange_all_dead(live, h_before, 0, d as usize) } // DUP: only the new TOS is a real output. (op::DUP1..=op::DUP16, _) => !live[h_before], (op::DUPN, Some(DecodedImm::Single(_))) => !live[h_before], // EXCHANGE: only the two exchanged non-TOS positions are written. - (op::EXCHANGE, Some(DecodedImm::Pair(n, m))) if (m as usize) < h_before => { - let tos = h_before - 1; - !live[tos - n as usize] && !live[tos - m as usize] + (op::EXCHANGE, Some(DecodedImm::Pair(n, m))) => { + exchange_all_dead(live, h_before, n as usize, m as usize) } - // Infeasible immediates — conservatively not dead. - (op::DUPN | op::SWAPN | op::EXCHANGE, _) => false, // Generic: all output positions must be dead. _ if out > 0 => { let write_base = h_before - inp; @@ -374,10 +359,10 @@ fn all_outputs_dead( } } -/// SWAP dead check: both TOS and the swapped position must be dead. -fn swap_all_dead(live: &BitVec, h_before: usize, depth: usize) -> bool { +/// SWAP/EXCHANGE dead check: both written positions, `n` and `m` below TOS, must be dead. +fn exchange_all_dead(live: &BitVec, h_before: usize, n: usize, m: usize) -> bool { let tos = h_before - 1; - !live[tos] && !live[tos - depth] + !live[tos - n] && !live[tos - m] } /// DUP liveness: the new TOS is killed; if it was live, the source becomes live. @@ -424,7 +409,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH should be skipped (dead store)" ); } @@ -442,7 +427,7 @@ mod tests { ); for (i, name) in [(0, "PUSH 3"), (1, "PUSH 4"), (2, "ADD")] { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "{name} should be skipped" ); } @@ -462,11 +447,11 @@ mod tests { ); // PUSH 1 feeds MSTORE's value input; known constant, so codegen pre-writes it. assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH 1 should be skipped (const input)" ); assert!( - bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "PUSH 2 should be skipped" ); } @@ -488,11 +473,11 @@ mod tests { ); // A is swapped to TOS then popped → dead. B is consumed by RET_WORD → live. assert!( - bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD A should be skipped (dead after swap+pop)" ); assert!( - !bytecode.inst(Inst::from_usize(3)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(3)).flags.contains(InstFlags::NOOP), "CALLDATALOAD B should NOT be skipped (consumed by RET_WORD)" ); } @@ -511,7 +496,7 @@ mod tests { ); for i in 0..4 { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "inst at {i} should be skipped (exit stack dead)" ); } @@ -532,7 +517,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH should be skipped (const input)" ); } @@ -551,7 +536,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH should be skipped when both DUP copy and source are dead" ); } @@ -567,7 +552,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "ADDRESS should be skipped when dead" ); } @@ -582,7 +567,7 @@ mod tests { STOP ", ); - assert!(!bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP)); + assert!(!bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP)); } #[test] @@ -591,7 +576,7 @@ mod tests { // CDL at bottom, 17 × PUSH0 above → SWAP 17 swaps TOS with CDL. let bytecode = with_dynamic_and_prefix(17, "SWAP 17 RET_WORD"); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped (consumed via SWAPN + MSTORE)" ); } @@ -602,7 +587,7 @@ mod tests { // CDL at bottom, 16 × PUSH0 above → DUP 17 copies CDL to TOS. let bytecode = with_dynamic_and_prefix(16, "DUP 17 RET_WORD"); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -613,11 +598,11 @@ mod tests { // remains on the stack and feeds RET_WORD. let bytecode = analyze_asm("PUSH0 CALLDATALOAD DUP1 POP RET_WORD"); assert!( - bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "DUP1 should be skipped (copy is dead)" ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -633,7 +618,7 @@ mod tests { SpecId::AMSTERDAM, ); assert!( - !bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped (consumed via EXCHANGE + SWAP + MSTORE)" ); } @@ -650,8 +635,8 @@ mod tests { STOP ", ); - assert!(bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP)); - assert!(bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP)); + assert!(bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP)); + assert!(bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP)); } #[test] @@ -669,7 +654,7 @@ mod tests { ); for (i, name) in [(0, "PUSH 1"), (1, "PUSH 2"), (2, "SWAP1")] { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "{name} should be skipped" ); } @@ -702,7 +687,7 @@ mod tests { .enumerate() { assert_eq!( - !bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), alive, "inst {i}" ); @@ -719,7 +704,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "RETURNDATASIZE should be skipped when dead" ); } @@ -736,7 +721,7 @@ mod tests { ", ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "RETURNDATASIZE should NOT be skipped when live" ); } @@ -753,11 +738,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH0 should be skipped" ); assert!( - bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "BLOBHASH should be skipped when dead" ); } @@ -775,7 +760,7 @@ mod tests { ", ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "BLOBHASH should NOT be skipped when live" ); } @@ -787,12 +772,12 @@ mod tests { let bytecode = with_prefix(17, &format!("DUP 17 {pops} STOP")); for i in 0..17 { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "PUSH0 at {i} should be skipped" ); } assert!( - bytecode.inst(Inst::from_usize(17)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(17)).flags.contains(InstFlags::NOOP), "DUPN should be skipped" ); } @@ -807,7 +792,7 @@ mod tests { SpecId::AMSTERDAM, ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "DUPN with invalid immediate should NOT be skipped" ); } @@ -819,7 +804,7 @@ mod tests { // perspective the immediate is valid and the instruction is eliminable. let bytecode = with_prefix(1, "DUP 17 POP POP STOP"); assert!( - bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "DUPN with valid immediate should be skipped when dead" ); } @@ -831,12 +816,12 @@ mod tests { let bytecode = with_prefix(18, &format!("SWAP 17 {pops} STOP")); for i in 0..18 { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "PUSH0 at {i} should be skipped" ); } assert!( - bytecode.inst(Inst::from_usize(18)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(18)).flags.contains(InstFlags::NOOP), "SWAPN should be skipped" ); } @@ -847,12 +832,12 @@ mod tests { let bytecode = with_prefix(3, "EXCHANGE 1 2 POP POP POP STOP"); for i in 0..3 { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "PUSH0 at {i} should be skipped" ); } assert!( - bytecode.inst(Inst::from_usize(3)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(3)).flags.contains(InstFlags::NOOP), "EXCHANGE should be skipped" ); } @@ -891,10 +876,10 @@ mod tests { ", ); for dead in [0, 1, 2, 3] { - assert!(bytecode.inst(Inst::from_usize(dead)).flags.contains(InstFlags::NOOP)); + assert!(bytecode.inst(Inst::new(dead)).flags.contains(InstFlags::NOOP)); } for not_dead in [4, 5] { - assert!(!bytecode.inst(Inst::from_usize(not_dead)).flags.contains(InstFlags::NOOP)); + assert!(!bytecode.inst(Inst::new(not_dead)).flags.contains(InstFlags::NOOP)); } } @@ -916,7 +901,7 @@ mod tests { ); for (i, name) in [(0, "PUSH 2"), (1, "PUSH 3"), (2, "ADD"), (3, "PUSH 4")] { assert!( - bytecode.inst(Inst::from_usize(i)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(i)).flags.contains(InstFlags::NOOP), "{name} should be skipped" ); } @@ -937,11 +922,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "PUSH 1 (addend) should be skipped (const input)" ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -961,11 +946,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "PUSH0 (addend) should be skipped (const input)" ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -985,11 +970,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH0 (deeper const input) should be skipped" ); assert!( - !bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -1011,11 +996,11 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(2)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(2)).flags.contains(InstFlags::NOOP), "PUSH 2 (divisor) should be skipped (builtin const input)" ); assert!( - !bytecode.inst(Inst::from_usize(1)).flags.contains(InstFlags::NOOP), + !bytecode.inst(Inst::new(1)).flags.contains(InstFlags::NOOP), "CALLDATALOAD should NOT be skipped" ); } @@ -1034,7 +1019,7 @@ mod tests { ", ); assert!( - bytecode.inst(Inst::from_usize(0)).flags.contains(InstFlags::NOOP), + bytecode.inst(Inst::new(0)).flags.contains(InstFlags::NOOP), "PUSH 0 should be skipped (const input)" ); } diff --git a/crates/jit/codegen/src/bytecode/passes/dedup.rs b/crates/jit/codegen/src/bytecode/passes/dedup.rs index 944f81a91..ea5b43ea2 100644 --- a/crates/jit/codegen/src/bytecode/passes/dedup.rs +++ b/crates/jit/codegen/src/bytecode/passes/dedup.rs @@ -223,7 +223,8 @@ fn block_fingerprint(insts: &IndexVec, block: &BlockData) -> Sma #[cfg(test)] mod tests { use crate::bytecode::{ - AnalysisConfig, InstFlags, passes::block_analysis::tests::analyze_asm_with, + AnalysisConfig, InstFlags, + passes::block_analysis::tests::{analyze_asm_with, fixture_entry_code}, }; use evm2::interpreter::op; @@ -525,24 +526,6 @@ mod tests { assert_eq!(bytecode.redirects.len(), 20); } - fn fixture_entry_code(json: &str) -> Vec { - let v: serde_json::Value = serde_json::from_str(json).unwrap(); - let case = v.as_object().unwrap().values().next().unwrap(); - let transaction = &case["transaction"]; - let to = transaction - .get("to") - .or_else(|| { - transaction - .as_array() - .and_then(|transactions| transactions.first()) - .and_then(|transaction| transaction.get("to")) - }) - .and_then(|to| to.as_str()) - .unwrap(); - let code = case["pre"][to]["code"].as_str().unwrap().trim_start_matches("0x"); - alloy_primitives::hex::decode(code).unwrap() - } - #[test] fn dedup_skips_dynamic_jump_sstore_tail() { // Two non-JUMPDEST byte-identical `SSTORE ; JUMP` tails with unresolved dynamic diff --git a/crates/jit/codegen/src/bytecode/passes/memory_sections.rs b/crates/jit/codegen/src/bytecode/passes/memory_sections.rs index dc25a8872..358e95999 100644 --- a/crates/jit/codegen/src/bytecode/passes/memory_sections.rs +++ b/crates/jit/codegen/src/bytecode/passes/memory_sections.rs @@ -186,7 +186,7 @@ impl MemorySectionAnalysis { } fn compute_block_entry_states(&mut self, bytecode: &Bytecode<'_>) { - let entry = Block::from_usize(0); + let entry = Block::new(0); self.block_entry_states[entry] = BlockMemoryState { known_size: Some(0), exact_known_size: Some(0) }; diff --git a/crates/jit/codegen/src/bytecode/passes/sections.rs b/crates/jit/codegen/src/bytecode/passes/sections.rs index d93e0d276..c5f021e92 100644 --- a/crates/jit/codegen/src/bytecode/passes/sections.rs +++ b/crates/jit/codegen/src/bytecode/passes/sections.rs @@ -1,5 +1,4 @@ use crate::bytecode::{Bytecode, Inst, InstFlags}; -use alloy_primitives::U256; use core::{fmt, ops::Range}; use evm2::interpreter::op; use smallvec::SmallVec; @@ -24,6 +23,13 @@ impl fmt::Debug for GasSection { } impl GasSection { + /// The cost recorded for a section that costs at least this much. + /// + /// A section's cost is stored in 32 bits, so a larger one saturates here. Section gas is + /// charged ahead of the instructions, so a saturated cost would charge too little, and + /// [`Bytecode::analyze`] rejects the bytecode instead. + pub(crate) const SATURATED: u32 = u32::MAX; + /// Returns `true` if the section is empty. #[inline] pub(crate) fn is_empty(self) -> bool { @@ -83,7 +89,7 @@ pub(crate) struct GasSectionAnalysis { impl Default for GasSectionAnalysis { fn default() -> Self { - Self { gas_cost: 0, start_inst: Inst::from_usize(0), gas_insts: SmallVec::new() } + Self { gas_cost: 0, start_inst: Inst::new(0), gas_insts: SmallVec::new() } } } @@ -91,7 +97,7 @@ impl GasSectionAnalysis { /// Accumulates a base gas cost. #[inline] pub(crate) const fn process(&mut self, base_gas: u16) { - self.gas_cost += base_gas as u64; + self.process_extra(base_gas as u64); } /// Accumulates extra gas (e.g. pre-computed dynamic gas for folded instructions). @@ -156,7 +162,7 @@ pub(crate) struct StackSectionAnalysis { impl Default for StackSectionAnalysis { fn default() -> Self { - Self { inputs: 0, diff: 0, max_growth: 0, start_inst: Inst::from_usize(0) } + Self { inputs: 0, diff: 0, max_growth: 0, start_inst: Inst::new(0) } } } @@ -235,12 +241,9 @@ impl SectionsAnalysis { self.gas.process_gas(inst); } - // When EXP's builtin will be skipped — either because both operands are known - // (foldable_output) or because the exponent is known and handled by a peephole (≤ 2) — - // fold the dynamic gas into the section statically. + // The exponent is known, so translation does not charge EXP's dynamic gas. if data.opcode == op::EXP - && let Some(exponent) = bytecode.const_operand(inst, 1) - && (bytecode.foldable_output(inst).is_some() || exponent <= U256::from(2)) + && let Some(exponent) = bytecode.exp_static_exponent(inst) { self.gas.process_extra(bytecode.gas_params.exp_cost(exponent)); } @@ -338,7 +341,7 @@ mod tests { /// Returns the gas section cost for the first non-dead instruction (the section head). fn section_gas(src: &str) -> u32 { let bytecode = analyze_asm(src); - bytecode.inst(Inst::from_usize(0)).gas_section.gas_cost + bytecode.inst(Inst::new(0)).gas_section.gas_cost } /// EXP with an asymmetric base/exponent: base is 1 byte (2), exponent is 2 bytes (256). @@ -398,10 +401,10 @@ mod tests { ], SpecId::CANCUN, ); - let head = bytecode.inst(Inst::from_usize(0)); - let gas = bytecode.inst(Inst::from_usize(7)); - let call = bytecode.inst(Inst::from_usize(8)); - let after_call = bytecode.inst(Inst::from_usize(9)); + let head = bytecode.inst(Inst::new(0)); + let gas = bytecode.inst(Inst::new(7)); + let call = bytecode.inst(Inst::new(8)); + let after_call = bytecode.inst(Inst::new(9)); // `GAS` does not end the section: 4 * PUSH1 + CALLVALUE + PUSH1 + CALLDATALOAD + GAS + // CALL. assert_eq!(head.gas_section.gas_cost, 4 * 3 + 2 + 3 + 3 + 2 + 100); @@ -419,7 +422,7 @@ mod tests { "PUSH0 GAS PUSH 256 PUSH 2 EXP GAS POP SLOAD PUSH0 STOP", SpecId::CANCUN, ); - let head = bytecode.inst(Inst::from_usize(0)); + let head = bytecode.inst(Inst::new(0)); assert_eq!(head.gas_section.gas_cost, 2 + 2 + 3 + 3 + 110 + 2 + 2 + 100); let owed = bytecode .iter_insts() @@ -465,7 +468,7 @@ mod tests { fn disabled_opcode_does_not_poison_section() { // CALLDATASIZE(0→1) ; TSTORE(2→0, disabled before Cancun) let bytecode = analyze_asm_spec("CALLDATASIZE TSTORE", SpecId::SHANGHAI); - let head = bytecode.inst(Inst::from_usize(0)); + let head = bytecode.inst(Inst::new(0)); assert_eq!( head.stack_section.inputs, 0, "CALLDATASIZE needs 0 inputs; disabled TSTORE must not inflate the section" @@ -473,21 +476,21 @@ mod tests { assert_eq!(head.gas_section.gas_cost, 2, "only CALLDATASIZE gas (2) should be charged"); } - /// DUPN with an invalid immediate must have stack_io = (0, 0) so that section analysis - /// matches translate (which emits `InvalidImmediateEncoding` and diverges). - /// Previously, the fallback to OPCODE_INFO's placeholder `(0, 1)` inflated the section's - /// cumulative diff, causing translate to underflow the virtual stack on a subsequent SSTORE. + /// DUPN with an invalid immediate diverges like a disabled opcode, so the instructions after + /// it do not join its section: translate halts with `InvalidImmediateEncoding` before them. #[test] - fn invalid_dupn_imm_does_not_inflate_section() { + fn invalid_dupn_imm_diverges() { // PUSH0, DUPN 0x5b (invalid immediate), PUSH0, SSTORE, STOP - // Without the fix, DUPN had stack_io=(0,1), inflating the section diff by 1. - // Translate would skip the +1 (goto_return!(fail)), then SSTORE's diff=-2 would - // underflow the virtual stack. let bytecode = analyze_code_spec( vec![op::PUSH0, op::DUPN, 0x5b, op::PUSH0, op::SSTORE, op::STOP], SpecId::AMSTERDAM, ); - let dupn = bytecode.inst(Inst::from_usize(1)); + let dupn = bytecode.inst(Inst::new(1)); + assert!(dupn.is_diverging(), "DUPN with invalid immediate must diverge"); assert_eq!(dupn.stack_io(), (0, 0), "DUPN with invalid immediate must have zero stack I/O"); + + // The instructions after it are dead, so they add no stack inputs or gas to a section. + let after = bytecode.inst(Inst::new(3)); + assert!(after.is_dead_code()); } } diff --git a/crates/jit/codegen/src/compiler/mod.rs b/crates/jit/codegen/src/compiler/mod.rs index 20da64194..47c53721e 100644 --- a/crates/jit/codegen/src/compiler/mod.rs +++ b/crates/jit/codegen/src/compiler/mod.rs @@ -4,7 +4,6 @@ use crate::{ Backend, Builder, Bytecode, EvmCompilerFn, EvmContext, EvmStack, FxHashMap, Result, bytecode::AnalysisConfig, }; -use alloy_primitives::Bytes; use evm2::{OpcodeView, SpecId, version::GasParams}; use evm2_jit_backend::{ Attribute, BackendConfig, FunctionAttributeLocation, Linkage, OptimizationLevel, eyre::ensure, @@ -320,7 +319,7 @@ impl EvmCompiler { /// This setting is applied once per process on first JIT compilation. /// Subsequent compilers inherit the value set by the first. /// - /// Defaults to `true`. + /// Defaults to `false`. pub fn debug_support(&self) -> bool { self.backend.config().debug_support } @@ -367,7 +366,7 @@ impl EvmCompiler { /// This setting is applied once per process on first JIT compilation. /// Subsequent compilers inherit the value set by the first. /// - /// Defaults to `true`. + /// Defaults to `false`. pub fn simple_perf(&self) -> bool { self.backend.config().simple_perf } @@ -514,7 +513,10 @@ impl EvmCompiler { /// this lets LLVM DCE the per-failure-site materialization and the failure-block phi. /// Successful exits (`STOP`/`RETURN`/`REVERT`) keep their original codes. /// - /// Useful for benchmarking the cost of failure-result materialization. + /// This is a deliberate trade-off: the compiled function reports `OutOfGas` where the + /// interpreter reports the real halt reason (for example, an invalid jump). Preserving the + /// reasons (`false`) makes the JIT 4.0% larger, compile 10.7% more instructions, and + /// execute 3.8% more instructions in total across the benchmarks (`onchain_lm_v2` +4.3%). /// /// Defaults to `true`. pub const fn set_single_error(&mut self, yes: bool) { @@ -567,14 +569,9 @@ impl EvmCompiler { /// /// NOTE: `name` must be unique for each function, as it is used as the name of the final /// symbol. - pub fn translate<'a>( - &mut self, - name: &str, - input: impl Into>, - spec_id: SpecId, - ) -> Result { + pub fn translate(&mut self, name: &str, input: &[u8], spec_id: SpecId) -> Result { ensure!(!self.finalized, "cannot compile more functions after finalizing the module"); - let bytecode = self.parse(input.into(), spec_id)?; + let bytecode = self.parse(input, spec_id)?; self.translate_inner(name, &bytecode) } @@ -586,13 +583,13 @@ impl EvmCompiler { /// /// The returned function pointer is owned by the module, and must not be called after the /// module is cleared or the function is freed. - pub unsafe fn jit<'a>( + pub unsafe fn jit( &mut self, name: &str, - bytecode: impl Into>, + bytecode: &[u8], spec_id: SpecId, ) -> Result { - let id = self.translate(name, bytecode.into(), spec_id)?; + let id = self.translate(name, bytecode, spec_id)?; unsafe { self.jit_function(id) } } @@ -685,14 +682,8 @@ impl EvmCompiler { /// Parses the given EVM bytecode. Not public API. #[doc(hidden)] // Not public API. - pub fn parse<'a>( - &mut self, - input: EvmCompilerInput<'a>, - spec_id: SpecId, - ) -> Result> { + pub fn parse<'a>(&mut self, bytecode: &'a [u8], spec_id: SpecId) -> Result> { let _t = self.remarks.time(|r| &r.parse); - let EvmCompilerInput::Code(bytecode) = input; - let mut bytecode = Bytecode::new(bytecode, spec_id, self.gas_params, self.opcode_view.as_ref()); bytecode.compiler_gas_limit = self.compiler_gas_limit; @@ -1104,42 +1095,17 @@ total: {total:>11.3?} } } -/// [`EvmCompiler`] input. -#[allow(missing_debug_implementations)] -pub enum EvmCompilerInput<'a> { - /// EVM bytecode. - Code(&'a [u8]), -} - -impl<'a> From<&'a [u8]> for EvmCompilerInput<'a> { - fn from(code: &'a [u8]) -> Self { - EvmCompilerInput::Code(code) - } -} - -impl<'a> From<&'a Vec> for EvmCompilerInput<'a> { - fn from(code: &'a Vec) -> Self { - EvmCompilerInput::Code(code) - } -} - -impl<'a> From<&'a Bytes> for EvmCompilerInput<'a> { - fn from(code: &'a Bytes) -> Self { - EvmCompilerInput::Code(code) - } -} - -#[allow(dead_code)] mod default_attrs { use evm2_jit_backend::Attribute; pub(crate) fn for_fn() -> impl Iterator { [ - Attribute::WillReturn, // Always returns. - Attribute::NoSync, // No thread synchronization. - Attribute::NativeTargetCpu, // Optimization. - Attribute::NonLazyBind, // Skip PLT indirection. - Attribute::UWTable, // Unwind tables for profilers/debuggers. + Attribute::WillReturn, // Always returns. + Attribute::NoSync, // No thread synchronization. + Attribute::NativeTargetCpu, // Optimization. + Attribute::NonLazyBind, // Skip PLT indirection. + Attribute::UWTable, // Unwind tables for profilers/debuggers. + Attribute::InlineStackProbes, // Overflow faults on the guard page, as in Rust code. ] .into_iter() } @@ -1152,14 +1118,6 @@ mod default_attrs { for_param().chain([Attribute::NoCapture]) } - pub(crate) fn for_sized_ptr((size, align): (usize, usize)) -> impl Iterator { - for_ptr().chain([Attribute::Dereferenceable(size as u64), Attribute::Align(align as u64)]) - } - - pub(crate) fn for_ptr_t() -> impl Iterator { - for_sized_ptr(size_align::()) - } - pub(crate) fn for_ref() -> impl Iterator { // `NoFree` keeps `dereferenceable` valid past control flow, which LICM needs to promote // stack slots in loops. @@ -1169,14 +1127,6 @@ mod default_attrs { pub(crate) fn for_sized_ref((size, align): (usize, usize)) -> impl Iterator { for_ref().chain([Attribute::Dereferenceable(size as u64), Attribute::Align(align as u64)]) } - - pub(crate) fn for_ref_t() -> impl Iterator { - for_sized_ref(size_align::()) - } - - pub(crate) const fn size_align() -> (usize, usize) { - (std::mem::size_of::(), std::mem::align_of::()) - } } /// Resolves a `# bytecode.txt:LL:CC` or `# bytecode.txt:LL` comment in an assembly line diff --git a/crates/jit/codegen/src/compiler/translate/mod.rs b/crates/jit/codegen/src/compiler/translate/mod.rs index 1e8539ac0..5c2580f82 100644 --- a/crates/jit/codegen/src/compiler/translate/mod.rs +++ b/crates/jit/codegen/src/compiler/translate/mod.rs @@ -1,6 +1,5 @@ //! EVM to IR translation. -use super::default_attrs; use crate::{ Backend, Builder, Bytecode, EvmContext, Inst, InstData, InstFlags, IntCC, Result, StackSection, decode_pair, decode_single, @@ -11,10 +10,10 @@ use evm2::{ env::{BlockEnv, TxEnv}, interpreter::{InstrStop, Interpreter, Message, op}, }; -use evm2_jit_backend::{Attribute, BackendTypes, FunctionAttributeLocation, Pointer, TypeMethods}; +use evm2_jit_backend::{BackendTypes, Pointer, TypeMethods}; use evm2_jit_builtins::{Builtin, Builtins, CallKind, CreateKind}; use oxc_index::IndexVec; -use std::{mem, ops::Range}; +use std::{fmt::Write, mem, ops::Range}; mod peephole; @@ -163,10 +162,6 @@ impl HostLayout { /// A list of incoming values for a block. Represents a `phi` node. type Incoming = Vec<(::Value, ::BasicBlock)>; -/// A list of `switch` targets. -#[allow(dead_code)] -type SwitchTargets = Vec<(u64, ::BasicBlock)>; - pub(super) struct FunctionCx<'a, B: Backend> { // Configuration. config: FcxConfig, @@ -403,7 +398,7 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } // The bytecode is guaranteed to have at least one instruction. - let first_inst_block = fx.inst_entries[Inst::from_usize(0)]; + let first_inst_block = fx.inst_entries[Inst::new(0)]; let post_entry_block = fx.bcx.create_block_after(entry_block, "entry.post"); fx.bcx.br(post_entry_block); @@ -462,7 +457,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { failure_value } }; - fx.bcx.set_current_block_cold(); // Do not sync the stack here: this is a shared synthetic merge block for // failures that may occur before their instruction body has materialized // the current virtual stack. Syncing here can use values that do not @@ -497,19 +491,14 @@ impl<'a, B: Backend> FunctionCx<'a, B> { Some(next) } - /// Resolves an instruction through dead-block redirects created by dedup. - fn effective_inst(&self, inst: Inst) -> Inst { - self.bytecode.redirects.get(&inst).copied().unwrap_or(inst) - } - /// Returns the IR block for an instruction after applying dedup redirects. fn effective_entry(&self, inst: Inst) -> B::BasicBlock { - self.inst_entries[self.effective_inst(inst)] + self.inst_entries[self.bytecode.effective_inst(inst)] } /// Returns the runtime fallthrough target after applying dedup redirects. fn effective_next_inst(&self, inst: Inst) -> Option { - self.lexical_next_inst(inst).map(|next| self.effective_inst(next)) + self.lexical_next_inst(inst).map(|next| self.bytecode.effective_inst(next)) } /// Returns the IR block for the runtime fallthrough target. @@ -529,8 +518,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { self.bcx.set_debug_location(self.inst_lines[inst], 1); } - // self.call_printf(format_printf!("{}\n", self.op_block_name("")), &[]); - let branch_to_next_opcode = |this: &mut Self| { debug_assert!( !this.bytecode.is_instr_diverging(inst), @@ -577,7 +564,7 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } // Assert that we already skipped the block. - debug_assert!(!data.flags.contains(InstFlags::DEAD_CODE)); + debug_assert!(!data.is_dead_code()); // Disabled instructions don't pay gas. if data.flags.contains(InstFlags::DISABLED) { @@ -648,10 +635,8 @@ impl<'a, B: Backend> FunctionCx<'a, B> { // skip the real logic and just write the result. // The inputs are not loaded; we simply adjust the stack offset to consume them and // push the folded constant. This turns e.g. `PUSH 3, PUSH 4, ADD` into a single - // store of `7`. - // Pure ops with a known-constant output: skip the opcode logic and just - // store the folded constant. Works for single-output arithmetic (ADD, MUL, - // ...) and DUP (which adds one new TOS without consuming its input). + // store of `7`. Works for single-output arithmetic (ADD, MUL, ...) and DUP (which adds + // one new TOS without consuming its input). if out >= 1 && let Some(const_out) = self.bytecode.foldable_output(inst) { @@ -1016,13 +1001,13 @@ impl<'a, B: Backend> FunctionCx<'a, B> { let offset = self.pop(); let addr = self.build_ensure_memory(offset, 32); let value = self.bcx.load_aligned(self.word_type, addr, 1, "mload.value"); - let value = if self.little_endian() { self.bcx.bswap(value) } else { value }; + let value = self.bcx.bswap(value); self.push(value); } op::MSTORE => { let [offset, value] = self.popn(); let addr = self.build_ensure_memory(offset, 32); - let value = if self.little_endian() { self.bcx.bswap(value) } else { value }; + let value = self.bcx.bswap(value); self.bcx.store_aligned(value, addr, 1); } op::MSTORE8 => { @@ -1307,7 +1292,7 @@ impl<'a, B: Backend> FunctionCx<'a, B> { // Walk backward to find the section head for diagnostic context. let mut head = inst; for i in (0..inst.index()).rev() { - let idx = crate::Inst::from_usize(i); + let idx = Inst::new(i); let d = self.bytecode.inst(idx); if d.is_dead_code() { continue; @@ -1320,12 +1305,11 @@ impl<'a, B: Backend> FunctionCx<'a, B> { // Dump the section from head to current inst. let mut section_dump = String::new(); for i in head.index()..=inst.index() { - let idx = crate::Inst::from_usize(i); + let idx = Inst::new(i); let d = self.bytecode.inst(idx); if d.is_dead_code() { continue; } - use std::fmt::Write; let _ = write!( section_dump, "\n ic{i} pc={} {:?} io={:?} flags={:?} gas={:?} stack={:?}{}{}", @@ -1513,14 +1497,19 @@ impl<'a, B: Backend> FunctionCx<'a, B> { self.bcx.load(self.word_type, ptr, name) } - fn load_interpreter_ptr(&mut self) -> B::Value { + /// Loads the pointer stored in the `EvmContext` field at the given offset. + fn load_ecx_ptr(&mut self, offset: usize, addr_name: &str, name: &str) -> B::Value { let ptr_type = self.bcx.type_ptr(); - let field = self.get_field( - self.ecx, + let field = self.get_field(self.ecx, offset, addr_name); + self.bcx.load(ptr_type, field, name) + } + + fn load_interpreter_ptr(&mut self) -> B::Value { + self.load_ecx_ptr( mem::offset_of!(EvmContext<'_, '_, '_>, interpreter), "ecx.interpreter.addr", - ); - self.bcx.load(ptr_type, field, "ecx.interpreter") + "ecx.interpreter", + ) } fn load_message_ptr(&mut self) -> B::Value { @@ -1535,23 +1524,19 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } fn load_block_env_ptr(&mut self) -> B::Value { - let ptr_type = self.bcx.type_ptr(); - let field = self.get_field( - self.ecx, + self.load_ecx_ptr( mem::offset_of!(EvmContext<'_, '_, '_>, block_env), "ecx.block_env.addr", - ); - self.bcx.load(ptr_type, field, "ecx.block_env") + "ecx.block_env", + ) } fn load_tx_env_ptr(&mut self) -> B::Value { - let ptr_type = self.bcx.type_ptr(); - let field = self.get_field( - self.ecx, + self.load_ecx_ptr( mem::offset_of!(EvmContext<'_, '_, '_>, tx_env), "ecx.tx_env.addr", - ); - self.bcx.load(ptr_type, field, "ecx.tx_env") + "ecx.tx_env", + ) } /// Gets a field at the given offset. @@ -1928,7 +1913,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { self.cached_mem_base = None; self.if_then(exceeds, |this| { - this.bcx.set_current_block_cold(); this.call_fallible_builtin(Builtin::Mresize, &[this.ecx, min_size]); }); self.build_memory_addr(offset) @@ -1965,23 +1949,12 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } fn load_memory_base(&mut self) -> B::Value { - let ptr_type = self.bcx.type_ptr(); - let mem_base_field = self.get_field( - self.ecx, + self.load_ecx_ptr( mem::offset_of!(EvmContext<'_, '_, '_>, mem_base), "ecx.mem_base.addr", - ); - self.bcx.load(ptr_type, mem_base_field, "ecx.mem_base") - } - - /* - /// Builds a check, failing if the condition is false. - /// - /// `if success_cond { ... } else { return ret }` - fn build_failure_inv(&mut self, success_cond: B::Value, ret: InstrStop) { - self.build_failure_imm_inner(false, success_cond, ret); + "ecx.mem_base", + ) } - */ /// Emits under/overflow bounds checks for a stack section. fn check_stack_bounds(&mut self, stack_section: StackSection) { @@ -2140,7 +2113,7 @@ impl<'a, B: Backend> FunctionCx<'a, B> { let jumpdests = bytecode .iter_insts() .filter(|(_, data)| { - data.opcode == op::JUMPDEST && bytecode.is_valid_jump(data.jumpdest_pc() as usize) + data.is_jumpdest() && bytecode.is_valid_jump(data.jumpdest_pc() as usize) }) .map(|(inst, data)| (data.jumpdest_pc() as usize, inst)) .collect::>(); @@ -2208,15 +2181,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { block } - #[allow(dead_code)] - fn call_printf(&mut self, template: &std::ffi::CStr, values: &[B::Value]) { - let mut args = Vec::with_capacity(values.len() + 1); - args.push(self.bcx.cstr_const(template)); - args.extend_from_slice(values); - let printf = self.bcx.get_printf_function(); - let _ = self.bcx.call(printf, &args); - } - /// Build a call to a fallible builtin. /// /// The builtin longjmps on error, so no return value check is needed. @@ -2229,10 +2193,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { #[must_use] fn call_builtin(&mut self, builtin: Builtin, args: &[B::Value]) -> Option { let function = self.builtin_function(builtin); - // self.call_printf( - // format_printf!("{} - calling {}\n", self.op_block_name(""), builtin.name()), - // &[], - // ); let invalidate_mem_base = !self.current_inst.is_some_and(|inst| self.can_skip_ensure_memory(inst)); let value = self.bcx.call(function, args); @@ -2290,18 +2250,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { self.bcx.current_block().expect("no blocks") } - const fn little_endian(&self) -> bool { - true - } - - /* - /// Creates a named block. - fn create_block(&mut self, name: &str) -> B::BasicBlock { - let name = self.op_block_name(name); - self.bcx.create_block(&name) - } - */ - /// Creates a named block after the given block. fn create_block_after(&mut self, after: B::BasicBlock, name: &str) -> B::BasicBlock { let name = self.op_block_name(name); @@ -2329,78 +2277,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> { } } -/// IR builtins. -impl FunctionCx<'_, B> { - #[allow(dead_code)] - #[must_use] - fn call_ir_builtin( - &mut self, - name: &str, - args: &[B::Value], - arg_types: &[B::Type], - ret: Option, - build: impl FnOnce(&mut Self), - ) -> Option { - let prefix = "__revmc_ir_builtin_"; - let name = &format!("{prefix}{name}")[..]; - - // self.call_printf(format_printf!("{} - calling {name}\n", self.op_block_name("")), &[]); - - debug_assert_eq!(args.len(), arg_types.len()); - let linkage = evm2_jit_backend::Linkage::Private; - // SAFETY: `this` aliases `self` to work around the borrow checker: we need `self.bcx` - // borrowed by `get_or_build_function` while also swapping `self.bcx` inside the closure. - // The closure's `bcx` is a fresh builder (not `self.bcx`), so the swap is safe. - let debug_location = self - .config - .debug - .then(|| self.current_inst.map(|inst| self.inst_lines[inst])) - .flatten(); - self.bcx.clear_debug_location(); - - let this = unsafe { &mut *(self as *mut Self) }; - let f = self.bcx.get_or_build_function(name, arg_types, ret, linkage, |bcx| { - let prev_return_block = this.return_block.take(); - let prev_failure_block = this.failure_block.take(); - // SAFETY: `this.bcx` and `bcx` are non-overlapping (bcx is a fresh builder). - unsafe { std::ptr::swap(&mut this.bcx, bcx) }; - - for attr in default_attrs::for_fn().chain(std::iter::once(Attribute::NoUnwind)) { - this.bcx.add_function_attribute(None, attr, FunctionAttributeLocation::Function) - } - for i in 0..this.bcx.num_fn_params() as u32 { - for attr in default_attrs::for_param() { - this.bcx.add_function_attribute(None, attr, FunctionAttributeLocation::Param(i)) - } - } - build(this); - - // SAFETY: same as above. - unsafe { std::ptr::swap(&mut this.bcx, bcx) }; - this.failure_block = prev_failure_block; - this.return_block = prev_return_block; - }); - if let Some(line) = debug_location { - self.bcx.set_debug_location(line, 1); - } - self.bcx.call(f, args) - } -} - -// HACK: Need these structs' fields to be public for `offset_of!`. -// `pf == private_fields`. -#[allow(dead_code)] -mod pf { - use super::*; - - #[repr(C)] // See core::ptr::metadata::PtrComponents - pub(super) struct Slice { - pub(super) ptr: *const u8, - pub(super) len: usize, - } - const _: [(); mem::size_of::<&'static [u8]>()] = [(); mem::size_of::()]; -} - fn get_field(bcx: &mut B, ptr: B::Value, offset: usize, name: &str) -> B::Value { // A field at offset 0 is `ptr` itself; a zero GEP would only clutter the IR. if offset == 0 { @@ -2409,12 +2285,3 @@ fn get_field(bcx: &mut B, ptr: B::Value, offset: usize, name: &str) let offset = bcx.iconst(bcx.type_ptr_sized_int(), offset as i64); bcx.gep(bcx.type_int(8), ptr, &[offset], name) } - -#[allow(unused)] -macro_rules! format_printf { - ($($t:tt)*) => { - &std::ffi::CString::new(format!($($t)*)).unwrap() - }; -} -#[allow(unused)] -use format_printf; diff --git a/crates/jit/codegen/src/compiler/translate/peephole.rs b/crates/jit/codegen/src/compiler/translate/peephole.rs index 9aadd3656..ea28cee54 100644 --- a/crates/jit/codegen/src/compiler/translate/peephole.rs +++ b/crates/jit/codegen/src/compiler/translate/peephole.rs @@ -208,12 +208,13 @@ impl<'a, B: Backend> FunctionCx<'a, B> { /// EXP base, exp => base ** exp. /// - /// Dynamic gas is folded into the section gas cost by `SectionsAnalysis` when the - /// exponent is a compile-time constant, so the section gas check already covers it. - /// For constant-base cases with a dynamic exponent, charge the dynamic gas and compute the - /// result inline. + /// A constant exponent (`Bytecode::exp_static_exponent`) has its dynamic gas folded into the + /// section gas cost by `SectionsAnalysis`, so it is lowered without charging gas. For + /// constant-base cases with a dynamic exponent, charge the dynamic gas and compute the result + /// inline. fn peephole_exp(&mut self) -> bool { - let [base, exponent] = self.const_operands(); + let [base, _] = self.const_operands(); + let exponent = self.bytecode.exp_static_exponent(self.current_inst.unwrap()); match exponent { // x ** 0 => 1. Some(U256::ZERO) => self.fold_const(1), @@ -228,7 +229,11 @@ impl<'a, B: Backend> FunctionCx<'a, B> { let r = self.bcx.imul(a, a); self.push(r); } - Some(_) => return false, + // x ** e => x ** e, with the gas already paid. + Some(_) => { + let sp = self.sp_after_inputs(); + let _ = self.call_builtin(Builtin::Pow, &[sp]); + } None => {} } if exponent.is_some() { diff --git a/crates/jit/codegen/src/compiler/translate/vstack.rs b/crates/jit/codegen/src/compiler/translate/vstack.rs index a7c0a0144..3678750be 100644 --- a/crates/jit/codegen/src/compiler/translate/vstack.rs +++ b/crates/jit/codegen/src/compiler/translate/vstack.rs @@ -118,28 +118,30 @@ impl VStack { /// Sets a slot to virtual at the given operand depth. pub(super) fn set(&mut self, depth: usize, value: T) { - let idx = self.idx(self.offset_at_depth(depth)); - self.slots[idx] = Some(VSlot::Virtual(value)); + self.set_slot(self.offset_at_depth(depth), VSlot::Virtual(value)); + } + + /// Sets the slot at the given section-relative offset. + fn set_slot(&mut self, offset: i32, slot: VSlot) { + let idx = self.idx(offset); + self.slots[idx] = Some(slot); } /// Records that memory at a section-relative offset holds `value`. pub(super) fn set_stored_at_offset(&mut self, offset: i32, value: T) { - let idx = self.idx(offset); - self.slots[idx] = Some(VSlot::Materialized(Some(value))); + self.set_slot(offset, VSlot::Materialized(Some(value))); } /// Pushes a virtual value onto the top of the stack. pub(super) fn push(&mut self, value: T) { - let idx = self.idx(self.top_offset); - self.slots[idx] = Some(VSlot::Virtual(value)); + self.set_slot(self.top_offset, VSlot::Virtual(value)); self.top_offset += 1; } /// Pushes a materialized marker onto the top of the stack. /// Used when a builtin writes directly to the physical stack slot. pub(super) fn push_mem(&mut self) { - let idx = self.idx(self.top_offset); - self.slots[idx] = Some(VSlot::Materialized(None)); + self.set_slot(self.top_offset, VSlot::Materialized(None)); self.top_offset += 1; } diff --git a/crates/jit/codegen/src/lib.rs b/crates/jit/codegen/src/lib.rs index 1bd60d924..4133fca93 100644 --- a/crates/jit/codegen/src/lib.rs +++ b/crates/jit/codegen/src/lib.rs @@ -13,13 +13,14 @@ mod bytecode; pub use bytecode::*; mod compiler; -pub use compiler::{CompileTimings, EvmCompiler, EvmCompilerInput, HostLayout}; +pub use compiler::{CompileTimings, EvmCompiler, HostLayout}; mod linker; pub use linker::{Linker, shared_library_path}; -/// ABI version of compiled artifacts. Bump when the calling convention or execution layout changes. -pub const ABI_VERSION: u32 = 3; +/// ABI version of compiled artifacts. Bump when the calling convention, the execution layout, the +/// `InstrStop` discriminants, or the stored artifact format change. +pub const ABI_VERSION: u32 = 8; /// Internal tests and testing utilities. Not public API. #[cfg(test)] diff --git a/crates/jit/codegen/src/tests/fibonacci.rs b/crates/jit/codegen/src/tests/fibonacci.rs index e18028fd1..b1742cb1f 100644 --- a/crates/jit/codegen/src/tests/fibonacci.rs +++ b/crates/jit/codegen/src/tests/fibonacci.rs @@ -91,8 +91,8 @@ const FIBONACCI_CODE: &[u8] = &[ ]; fn fibonacci_rust(n: u16) -> U256 { - let mut a = U256::from(0); - let mut b = U256::from(1); + let mut a = U256::ZERO; + let mut b = U256::ONE; for _ in 0..n { let tmp = a; a = b; diff --git a/crates/jit/codegen/src/tests/large_section_gas.rs b/crates/jit/codegen/src/tests/large_section_gas.rs new file mode 100644 index 000000000..0f4b6c12b --- /dev/null +++ b/crates/jit/codegen/src/tests/large_section_gas.rs @@ -0,0 +1,92 @@ +//! Sections whose static gas is close to or past what compiled code can charge ahead. + +use super::{DEF_SPEC, with_evm_context}; +use crate::{Backend, EvmCompiler, parse_asm}; +use alloy_primitives::U256; +use evm2::{ + Version, + interpreter::{Gas, InstrStop}, + version::GasId, +}; + +/// A 32-byte exponent. +const MAX: &str = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; + +/// The largest `ExpByte` for which a section of `PUSH32` (3), `CALLVALUE` (2) and `EXP` (10 plus +/// 32 bytes of exponent) still costs less than `u32::MAX`. +const FITTING_EXP_BYTE: u32 = (1 << 27) - 1; +/// The gas of that section. +const FITTING_GAS: u64 = 3 + 2 + 10 + FITTING_EXP_BYTE as u64 * 32; + +/// Compiles `src` with the given `ExpByte`, returning the compile error if any. +fn compile( + compiler: &mut EvmCompiler, + code: &[u8], + exp_byte: u32, +) -> Result { + unsafe { compiler.clear() }.unwrap(); + let mut gas_params = Version::base(DEF_SPEC).gas_params; + gas_params.set(GasId::ExpByte, exp_byte); + compiler.set_gas_params(gas_params); + compiler.set_inspect_stack(true); + unsafe { compiler.jit("large_section_gas", code, DEF_SPEC) }.map_err(|e| e.to_string()) +} + +/// Runs `src` with a gas limit of `limit`, returning the stop reason, the final stack and the gas +/// spent. +fn run( + compiler: &mut EvmCompiler, + src: &str, + limit: u64, +) -> (InstrStop, Vec, u64) { + let code = parse_asm(src).unwrap(); + let f = compile(compiler, &code, FITTING_EXP_BYTE).unwrap(); + with_evm_context(&code, DEF_SPEC, |ecx, stack, stack_len| { + ecx.gas = Gas::new(limit); + let r = unsafe { f.call(ecx, stack, stack_len) }; + let stack = + unsafe { stack.as_slice(*stack_len) }.iter().map(|word| word.to_u256()).collect(); + (r, stack, ecx.gas.spent()) + }) +} + +// A section costing more than `u32::MAX` is not compiled, instead of being charged too little. +matrix_tests!( + large_section_gas_rejected = |compiler| { + let code = parse_asm(&format!("PUSH32 {MAX} CALLVALUE EXP STOP")).unwrap(); + let err = compile(compiler, &code, u32::MAX).err().unwrap(); + assert_eq!( + err, + "the gas section at instruction 0 costs at least 4294967295 gas, which compiled code \ + cannot charge ahead of its instructions", + ); + } +); + +// A section just under the bound compiles and charges its exact cost, with a constant exponent +// above 2. +matrix_tests!( + large_section_gas_const_exponent = |compiler| { + let src = format!("PUSH32 {MAX} CALLVALUE EXP STOP"); + let (r, _, spent) = run(compiler, &src, 2 * FITTING_GAS); + assert_eq!((r, spent), (InstrStop::Stop, FITTING_GAS)); + // The exact cost is enough, and one less is not. + let (r, _, spent) = run(compiler, &src, FITTING_GAS); + assert_eq!((r, spent), (InstrStop::Stop, FITTING_GAS)); + let (r, _, _) = run(compiler, &src, FITTING_GAS - 1); + assert_eq!(r, InstrStop::OutOfGas); + } +); + +// The section head charges the whole cost, so the `GAS` after it owes the rest back to the +// remaining gas it reads. +matrix_tests!( + large_section_gas_owed = |compiler| { + let limit = 2 * FITTING_GAS; + let (r, stack, spent) = + run(compiler, &format!("GAS PUSH32 {MAX} CALLVALUE EXP GAS STOP"), limit); + assert_eq!((r, spent), (InstrStop::Stop, FITTING_GAS + 4)); + assert_eq!(stack[0], U256::from(limit - 2)); + assert_eq!(stack[2], U256::from(limit - FITTING_GAS - 4)); + } +); diff --git a/crates/jit/codegen/src/tests/mod.rs b/crates/jit/codegen/src/tests/mod.rs index 47603b1e7..9c83fd64b 100644 --- a/crates/jit/codegen/src/tests/mod.rs +++ b/crates/jit/codegen/src/tests/mod.rs @@ -17,15 +17,19 @@ use evm2::{ }; use evm2_jit_builtins::gas; -/// `KECCAK256` opcode gas cost (base + dynamic). -const fn keccak256_cost(len: u64) -> Option { - let words = len.div_ceil(32); - match words.checked_mul(gas::KECCAK256WORD as u64) { - Some(dyn_cost) => Some((gas::KECCAK256 as u64).saturating_add(dyn_cost)), +/// Opcode gas cost of `base` plus `per_word` for each 32-byte word of `len`. +const fn word_cost(base: u64, per_word: u64, len: u64) -> Option { + match len.div_ceil(32).checked_mul(per_word) { + Some(dyn_cost) => Some(base.saturating_add(dyn_cost)), None => None, } } +/// `KECCAK256` opcode gas cost (base + dynamic). +const fn keccak256_cost(len: u64) -> Option { + word_cost(gas::KECCAK256 as u64, gas::KECCAK256WORD as u64, len) +} + /// `LOG` opcode gas cost (base + topics + dynamic). const fn log_cost(n_topics: u8, len: u64) -> Option { match (gas::LOGDATA as u64).checked_mul(len) { @@ -38,11 +42,7 @@ const fn log_cost(n_topics: u8, len: u64) -> Option { /// `CALLDATACOPY`, `CODECOPY`, `RETURNDATACOPY` opcode gas cost (base + dynamic). const fn verylowcopy_cost(len: u64) -> Option { - let words = len.div_ceil(32); - match words.checked_mul(gas::COPY as u64) { - Some(dyn_cost) => Some((gas::VERYLOW as u64).saturating_add(dyn_cost)), - None => None, - } + word_cost(gas::VERYLOW as u64, gas::COPY as u64, len) } #[macro_use] @@ -54,6 +54,8 @@ mod fibonacci; mod entry_stack; +mod large_section_gas; + mod runner; pub use runner::*; @@ -65,8 +67,6 @@ pub fn with_jit_compiler( init_tracing(); let mut compiler = EvmCompiler::new_llvm(false).unwrap(); compiler.set_opt_level(opt_level); - compiler.set_profiling_support(false); - compiler.set_simple_perf(false); compiler.set_single_error(false); f(&mut compiler) } @@ -115,34 +115,26 @@ tests! { invalid(@raw { bytecode: &[op::INVALID], expected_return: InstrStop::InvalidFEOpcode, - expected_gas: 0, }), unknown(@raw { bytecode: &[0x21], expected_return: InstrStop::OpcodeNotFound, - expected_gas: 0, }), underflow1(@raw { bytecode: &[op::ADD], expected_return: InstrStop::StackUnderflow, - expected_gas: 3, }), underflow2(@raw { bytecode: &[op::PUSH0, op::ADD], expected_return: InstrStop::StackUnderflow, - expected_stack: &[U256::ZERO], - expected_gas: 5, }), underflow3(@raw { bytecode: &[op::PUSH0, op::POP, op::ADD], expected_return: InstrStop::StackUnderflow, - expected_gas: 7, }), underflow4(@raw { bytecode: &[op::PUSH0, op::ADD, op::POP], expected_return: InstrStop::StackUnderflow, - expected_stack: &[U256::ZERO], - expected_gas: 5, }), overflow_not0(@raw { bytecode: &[op::PUSH0; 1023], @@ -159,14 +151,10 @@ tests! { overflow0(@raw { bytecode: &[op::PUSH0; 1025], expected_return: InstrStop::StackOverflow, - expected_stack: &[0_U256; 1024], - expected_gas: 2 * 1025, }), overflow1(@raw { bytecode: &[op::PUSH0; 1026], expected_return: InstrStop::StackOverflow, - expected_stack: &[0_U256; 1024], - expected_gas: 2 * 1025, }), } @@ -175,7 +163,6 @@ tests! { bytecode: &[op::PUSH0], spec_id: SpecId::MERGE, expected_return: InstrStop::NotActivated, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), push0_shanghai(@raw { bytecode: &[op::PUSH0], @@ -193,44 +180,32 @@ tests! { bytecode: &[op::MSIZE, op::CLZ], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), clz_arrow_glacier(@raw { bytecode: &[op::MSIZE, op::CLZ], spec_id: SpecId::LONDON, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dupn_cancun(@raw { bytecode: &[op::PUSH0, op::DUPN, 0x00], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), swapn_cancun(@raw { bytecode: &[op::PUSH0, op::SWAPN, 0x00], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), exchange_cancun(@raw { bytecode: &[op::PUSH0, op::EXCHANGE, 0x01], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), slotnum_cancun(@raw { bytecode: &[op::SLOTNUM], spec_id: SpecId::CANCUN, expected_return: InstrStop::NotActivated, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // EXP gas: pre-SPURIOUS_DRAGON exp_byte_gas=10, post=50. @@ -410,32 +385,24 @@ tests! { dupn_underflow(@raw { bytecode: &[op::PUSH0, op::DUPN, 0x80], spec_id: SpecId::AMSTERDAM, - expected_return: InstrStop::StackOverflow, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, + expected_return: InstrStop::StackUnderflow, }), // Insufficient stack depth: SWAPN 0x80 decodes to depth 17, but only 1 item on stack. swapn_underflow(@raw { bytecode: &[op::PUSH0, op::SWAPN, 0x80], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::StackUnderflow, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Insufficient stack depth: EXCHANGE 0x8E decodes to (1,2), but only 2 items on stack. exchange_underflow(@raw { bytecode: &[op::PUSH0, op::PUSH0, op::EXCHANGE, 0x8E], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::StackUnderflow, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), swap16_dup10_selfdestruct_underflow(@raw { bytecode: &[op::SWAP16, op::DUP10, op::SELFDESTRUCT], spec_id: SpecId::OSAKA, expected_return: InstrStop::StackUnderflow, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Invalid immediate: 0x5B (91) is in the invalid range [91, 127] for decode_single. @@ -443,13 +410,18 @@ tests! { bytecode: &[op::PUSH0, op::DUPN, 0x5B], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidImmediateEncoding, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), swapn_invalid_imm(@raw { bytecode: &[op::PUSH0, op::SWAPN, 0x5B], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidImmediateEncoding, + }), + // The invalid immediate halts before the following instructions are charged or + // stack-checked, so an empty stack does not underflow at the section head. + dupn_invalid_imm_before_underflow(@raw { + bytecode: &[op::DUPN, 0x5B, op::ADD], + spec_id: SpecId::AMSTERDAM, + expected_return: InstrStop::InvalidImmediateEncoding, expected_stack: STACK_WHAT_INTERPRETER_SAYS, expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), @@ -458,8 +430,6 @@ tests! { bytecode: &[op::PUSH0, op::PUSH0, op::PUSH0, op::EXCHANGE, 82], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidImmediateEncoding, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Truncated trailing DUPN at EOF: immediate byte missing, zero-padded to 0x00. @@ -552,8 +522,6 @@ tests! { ], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidJump, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Same masked target, but reached through an unresolved dynamic jump // (CALLDATASIZE is unknown at compile time): the dynamic dispatch table must @@ -567,8 +535,6 @@ tests! { ], spec_id: SpecId::AMSTERDAM, expected_return: InstrStop::InvalidJump, - expected_stack: STACK_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // A valid target inside a PUSH immediate: DUPN desynchronizes decoding, so the // 0x5b sits in the data of a decoded PUSH1 while the analysis (which does not @@ -646,30 +612,22 @@ tests! { unmodified_stack_after_push_jump(@raw { bytecode: &[op::PUSH1, 3, op::JUMP, op::JUMPDEST, op::PUSH0, op::ADD], expected_return: InstrStop::StackUnderflow, - expected_stack: &[U256::ZERO], - expected_gas: 3 + 8 + 1 + 2 + 3, }), bad_jump(@raw { bytecode: &[op::JUMP], expected_return: InstrStop::StackUnderflow, - expected_gas: 8, }), bad_jumpi1(@raw { bytecode: &[op::JUMPI], expected_return: InstrStop::StackUnderflow, - expected_gas: 10, }), bad_jumpi2(@raw { bytecode: &[op::PUSH0, op::JUMPI], expected_return: InstrStop::StackUnderflow, - expected_stack: &[0_U256], - expected_gas: 2 + 10, }), bad_jumpi3(@raw { bytecode: &[op::JUMPDEST, op::PUSH0, op::JUMPI], expected_return: InstrStop::StackUnderflow, - expected_stack: &[0_U256], - expected_gas: 1 + 2 + 10, }), basic_jumpi1(@raw { @@ -690,13 +648,10 @@ tests! { basic_jumpi2_lazy_invalid_target(@raw { bytecode: &[op::PUSH1, 1, op::PUSH0, op::JUMPI, op::PUSH1, 69], expected_return: InstrStop::InvalidJump, - expected_gas: 3 + 2 + 10, }), unmodified_stack_after_push_jumpi(@raw { bytecode: &[op::PUSH1, 1, op::PUSH1, 5, op::JUMPI, op::JUMPDEST, op::PUSH0, op::ADD], expected_return: InstrStop::StackUnderflow, - expected_stack: &[U256::ZERO], - expected_gas: 3 + 3 + 10 + 1 + 2 + 3, }), basic_loop(@raw { @@ -843,8 +798,8 @@ tests! { ].concat()), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // A return through `JUMPI` whose condition is opaque and whose target has two callers. @@ -885,7 +840,7 @@ tests! { inspect_stack: Some(false), expected_return: InstrStop::Return, expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Jumps into a table whose index the analysis only knows to be at most 3. @@ -893,15 +848,14 @@ tests! { bytecode: &asm(&range_jump_table(2)), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x22).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x22).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x22).as_slice(), + expected_output: Some(B256::with_last_byte(0x22).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), range_jump_table_non_jumpdest_entry(@raw { bytecode: &asm(&range_jump_table(1)), inspect_stack: Some(false), expected_return: InstrStop::InvalidJump, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // The `JUMPI` condition is a different valid jump target in each caller, so its // constant set proves it nonzero. @@ -944,8 +898,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Like the above, but pc 0 is a `JUMPDEST` and one caller passes 0, so the condition @@ -987,8 +941,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x07).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x07).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x07).as_slice(), + expected_output: Some(B256::with_last_byte(0x07).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // The constant folding budget runs out between the fixpoint and the snapshot pass: 20 @@ -1023,8 +977,8 @@ tests! { inspect_stack: Some(false), gas_limit: 1_000_000, expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Here the budget runs out before the fold inside `b` in the snapshot pass: the fixpoint @@ -1058,8 +1012,8 @@ tests! { inspect_stack: Some(false), gas_limit: 1_000_000, expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // A function returns to pc 0, which is a `JUMPDEST`, so the entry block is also a @@ -1091,8 +1045,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // An unreached block ends in a `JUMPI` with an opaque target; it must not disturb the @@ -1125,8 +1079,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // A function pointer chosen by `JUMPI`, called through a shared call block, where the @@ -1171,8 +1125,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0xf0).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0xf0).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0xf0).as_slice(), + expected_output: Some(B256::with_last_byte(0xf0).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // EXP folds to 1 in both call contexts, but its exponent differs, so its gas does too. @@ -1275,8 +1229,8 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(140).to_be_bytes::<32>(), - expected_output: Some(&U256::from(140).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(140).as_slice(), + expected_output: Some(B256::with_last_byte(140).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // One caller passes an invalid return address, so the return must stay dynamic and @@ -1297,7 +1251,6 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::InvalidJump, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // The return address sits below the tracked abstract stack and is brought up with // `SWAPN`, so the return must stay dynamic. @@ -1330,8 +1283,8 @@ tests! { spec_id: SpecId::AMSTERDAM, inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Same with `DUPN` copying the deep return address. @@ -1364,8 +1317,8 @@ tests! { spec_id: SpecId::AMSTERDAM, inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x42).to_be_bytes::<32>(), - expected_output: Some(&U256::from(0x42).to_be_bytes::<32>()), + expected_memory: B256::with_last_byte(0x42).as_slice(), + expected_output: Some(B256::with_last_byte(0x42).as_slice()), expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Two callers of a function that moves its return address with `EXCHANGE`. @@ -1413,7 +1366,7 @@ tests! { bytecode: &asm(&binary_choice_chain(11)), inspect_stack: Some(false), modify_message: Some(|message| { - message.input = Bytes::from_static(&[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]); + message.input = Bytes::from_static(&[0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]).into(); }), expected_return: InstrStop::Return, expected_memory: &U256::from(606).to_be_bytes::<32>(), @@ -1617,38 +1570,26 @@ tests! { dynamic_jump_to_65(@raw { bytecode: &bytecode_dynamic_jump(65_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_97(@raw { bytecode: &bytecode_dynamic_jump(97_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_code_end(@raw { bytecode: &bytecode_dynamic_jump(133_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_table_end(@raw { bytecode: &bytecode_dynamic_jump(160_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_u64_max(@raw { bytecode: &bytecode_dynamic_jump(U256::from(u64::MAX)), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), dynamic_jump_to_u64_overflow(@raw { bytecode: &bytecode_dynamic_jump((1_U256 << 64) + 63_U256), expected_return: InstrStop::InvalidJump, - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), } @@ -1904,6 +1845,13 @@ tests! { expected_stack: &[DEF_GAS_LIMIT_U256 - 2_U256, DEF_GAS_LIMIT_U256 - 122_U256], expected_gas: 2 + 3 + 3 + 110 + 2 + 2, }), + // A constant exponent above 2 lowers to a gas-free builtin: its dynamic gas (10 + 50 * 2) + // is charged up front with the section, once. + gas_around_const_exponent_pow(@raw { + bytecode: &[op::GAS, op::PUSH2, 1, 0, op::ADDRESS, op::EXP, op::POP, op::GAS], + expected_stack: &[DEF_GAS_LIMIT_U256 - 2_U256, DEF_GAS_LIMIT_U256 - 121_U256], + expected_gas: 2 + 3 + 2 + 110 + 2 + 2, + }), // `SLOAD` ends the gas section, so only its base gas is owed by the first `GAS`. gas_around_sload(@raw { bytecode: &[op::GAS, op::PUSH1, 69, op::SLOAD, op::GAS], @@ -2007,7 +1955,6 @@ tests! { // No return data exists, so copying 32 bytes from offset 0 fails with OutOfOffset bytecode: &[op::PUSH1, 32, op::PUSH0, op::PUSH0, op::RETURNDATACOPY], expected_return: InstrStop::OutOfOffset, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), } @@ -2156,50 +2103,35 @@ tests! { mload_overflow1(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX)], - expected_gas: 3 + 3, }), mload_overflow2(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff - 1, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX - 1)], - expected_gas: 3 + 3, }), mload_overflow3(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff - 31, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX - 31)], - expected_gas: 3 + 3, }), mload_overflow4(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff - 32, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX - 32)], - expected_gas: 3 + 3, }), mload_overflow5(@raw { bytecode: &[op::PUSH8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff - 33, op::MLOAD], expected_return: InstrStop::MemoryOOG, - expected_stack: &[U256::from(u64::MAX - 33)], - expected_gas: 3 + 3, }), mload_overflow6(@raw { bytecode: &[op::ADDRESS, op::MLOAD], expected_return: InstrStop::InvalidOperandOOG, - expected_stack: &[DEF_ADDR.into_word().into()], - expected_gas: 5, }), mload_const_offset_too_large(@raw { - bytecode: &{ - let mut code = Vec::new(); - code.push(op::PUSH9); - code.extend_from_slice(&0x1_0000_0000_0000_0000_U256.to_be_bytes::<32>()[23..]); - code.push(op::MLOAD); - code - }, + bytecode: &[ + &[op::PUSH9][..], + &0x1_0000_0000_0000_0000_U256.to_be_bytes::<32>()[23..], + &[op::MLOAD][..], + ] + .concat(), expected_return: InstrStop::InvalidOperandOOG, - expected_stack: &[0x1_0000_0000_0000_0000_U256], - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), mstore1(@raw { bytecode: &[op::PUSH0, op::PUSH0, op::MSTORE], @@ -2336,9 +2268,6 @@ tests! { MLOAD "), expected_return: InstrStop::InvalidOperandOOG, - expected_stack: &[!64_U256], - expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // `TLOAD` overwrites the stack slot holding `p = 64` with `0x1000`. dyn_offset_overwritten_by_builtin(@raw { @@ -2605,7 +2534,7 @@ tests! { assert_host: Some(|host| { assert_eq!(host.logs, [Log { address: DEF_ADDR, - data: LogData::new(vec![], Bytes::copy_from_slice(&0x6942_U256.to_be_bytes::<32>())).unwrap(), + data: LogData::new(vec![], Bytes::from(0x6942_U256.to_be_bytes::<32>())).unwrap(), }]); }), }), @@ -2633,7 +2562,7 @@ tests! { address: DEF_ADDR, data: LogData::new( vec![0xffffffffffffffffffffffffffffffffffffffff_U256.into()], - Bytes::copy_from_slice(&[0; 50]), + Bytes::from([0; 50]), ).unwrap(), }]); }), @@ -2759,14 +2688,12 @@ tests! { is_static: true, gas_limit: 4000, expected_return: InstrStop::OutOfGas, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Static-context SELFDESTRUCT: gas >= 5000 → charges 5000, then StateChangeDuringStaticCall. selfdestruct_static_enough_gas(@raw { bytecode: &[op::PUSH1, 0x01, op::SELFDESTRUCT], is_static: true, expected_return: InstrStop::StateChangeDuringStaticCall, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), } @@ -3105,7 +3032,7 @@ tests! { ], spec_id: SpecId::CANCUN, modify_message: Some(|message| { - message.input = Bytes::copy_from_slice(&U256::from(0x68).to_be_bytes::<32>()); + message.input = Bytes::from(B256::with_last_byte(0x68)).into(); message.value = U256::from(10); }), expected_return: RETURN_WHAT_INTERPRETER_SAYS, @@ -3399,9 +3326,9 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x2a).to_be_bytes::<32>(), + expected_memory: B256::with_last_byte(0x2a).as_slice(), expected_gas: GAS_WHAT_INTERPRETER_SAYS, - expected_output: Some(&U256::from(0x2a).to_be_bytes::<32>()), + expected_output: Some(B256::with_last_byte(0x2a).as_slice()), }), // Same class of bug but triggered by a deduped reachable JUMPDEST block rather than @@ -3450,9 +3377,9 @@ tests! { "), inspect_stack: Some(false), expected_return: InstrStop::Return, - expected_memory: &U256::from(0x2a).to_be_bytes::<32>(), + expected_memory: B256::with_last_byte(0x2a).as_slice(), expected_gas: GAS_WHAT_INTERPRETER_SAYS, - expected_output: Some(&U256::from(0x2a).to_be_bytes::<32>()), + expected_output: Some(B256::with_last_byte(0x2a).as_slice()), }), dedup_false_jumpi_non_jumpdest_fallthrough(@raw { @@ -3592,7 +3519,6 @@ tests! { bytecode: &[op::CALLDATASIZE, op::TSTORE], spec_id: SpecId::SHANGHAI, expected_return: InstrStop::NotActivated, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // PUSH0(0->1) ; TLOAD(1->1, disabled before Cancun). @@ -3600,7 +3526,6 @@ tests! { bytecode: &[op::PUSH0, op::TLOAD], spec_id: SpecId::SHANGHAI, expected_return: InstrStop::NotActivated, - expected_gas: GAS_WHAT_INTERPRETER_SAYS, }), // Mismatched costs in < BERLIN. @@ -3610,7 +3535,7 @@ tests! { spec_id: SpecId::ISTANBUL, modify_message: Some(|message| { message.value = 1_U256; - message.input = Bytes::from(&hex!("c0406226")); + message.input = alloy_primitives::bytes!("c0406226").into(); }), expected_return: RETURN_WHAT_INTERPRETER_SAYS, expected_stack: STACK_WHAT_INTERPRETER_SAYS, @@ -3702,7 +3627,7 @@ tests! { modify_message: Some(|message| { let mut input = [0; 96]; input[63] = 1; - message.input = Bytes::copy_from_slice(&input); + message.input = Bytes::from(input).into(); }), inspect_stack: Some(false), expected_return: InstrStop::Stop, diff --git a/crates/jit/codegen/src/tests/runner.rs b/crates/jit/codegen/src/tests/runner.rs index ee042b578..b50cc74b6 100644 --- a/crates/jit/codegen/src/tests/runner.rs +++ b/crates/jit/codegen/src/tests/runner.rs @@ -1,4 +1,8 @@ use super::*; +use alloy_primitives::{ + address, + map::{AddressMap, U256Map}, +}; use evm2::{ BaseEvmTypes, Evm, Precompiles, bytecode::Bytecode, @@ -58,11 +62,11 @@ impl DefEnv { pub fn def_env() -> DefEnv { DefEnv { tx: DefTx { - caller: Address::new(hex!("cc0102030405060708090a0b0c0d0e0f101112dd")), + caller: address!("cc0102030405060708090a0b0c0d0e0f101112dd"), blob_hashes: vec![B256::repeat_byte(0x01), B256::repeat_byte(0x02)], }, block: DefBlock { - coinbase: Address::new(hex!("cb1112131415161718191a1b1c1d1e1f202122ee")), + coinbase: address!("cb1112131415161718191a1b1c1d1e1f202122ee"), timestamp: uint!(0x01_0000000000000002_0000000000001234_U256), number: DEF_BN, difficulty: uint!(0x03_0000000000000004_000000000000cdef_U256), @@ -90,7 +94,6 @@ pub struct TestCase<'a> { /// Override `inspect_stack` on the compiler. `None` uses the default (`true`). pub inspect_stack: Option, pub modify_message: Option)>, - pub modify_ecx: Option)>, pub expected_return: InstrStop, pub expected_stack: &'a [U256], @@ -110,7 +113,6 @@ impl Default for TestCase<'_> { gas_limit: DEF_GAS_LIMIT, inspect_stack: None, modify_message: None, - modify_ecx: None, expected_return: InstrStop::Stop, expected_stack: &[], expected_memory: &[], @@ -129,7 +131,6 @@ impl fmt::Debug for TestCase<'_> { .field("spec_id", &self.spec_id) .field("inspect_stack", &self.inspect_stack) .field("modify_message", &self.modify_message.is_some()) - .field("modify_ecx", &self.modify_ecx.is_some()) .field("expected_return", &self.expected_return) .field("expected_stack", &self.expected_stack) .field("expected_memory", &MemDisplay(self.expected_memory)) @@ -150,7 +151,6 @@ impl<'a> TestCase<'a> { gas_limit: DEF_GAS_LIMIT, inspect_stack: None, modify_message: None, - modify_ecx: None, expected_return: RETURN_WHAT_INTERPRETER_SAYS, expected_stack: STACK_WHAT_INTERPRETER_SAYS, expected_memory: MEMORY_WHAT_INTERPRETER_SAYS, @@ -177,8 +177,8 @@ pub static DEF_CD: &[u8] = &[0xaa; 64]; pub static DEF_RD: &[u8] = &[0xbb; 64]; pub static DEF_DATA: &[u8] = &[0xcc; 64]; pub const DEF_VALUE: U256 = uint!(123_456_789_U256); -pub static DEF_STORAGE: OnceLock> = OnceLock::new(); -pub static DEF_CODEMAP: OnceLock> = OnceLock::new(); +pub static DEF_STORAGE: OnceLock> = OnceLock::new(); +pub static DEF_CODEMAP: OnceLock> = OnceLock::new(); pub const OTHER_ADDR: Address = Address::repeat_byte(0x69); pub const DEF_BN: U256 = uint!(500_U256); @@ -188,17 +188,17 @@ pub const STACK_WHAT_INTERPRETER_SAYS: &[U256] = pub const MEMORY_WHAT_INTERPRETER_SAYS: &[u8] = &GAS_WHAT_INTERPRETER_SAYS.to_be_bytes(); pub const GAS_WHAT_INTERPRETER_SAYS: u64 = 0x4682e332d6612de1; -pub fn def_storage() -> &'static HashMap { +pub fn def_storage() -> &'static U256Map { DEF_STORAGE.get_or_init(|| { let mut map = HashMap::default(); - map.insert(U256::from(0), U256::from(1)); - map.insert(U256::from(1), U256::from(2)); + map.insert(U256::ZERO, U256::ONE); + map.insert(U256::ONE, U256::from(2)); map.insert(U256::from(69), U256::from(42)); map }) } -pub fn def_codemap() -> &'static HashMap { +pub fn def_codemap() -> &'static AddressMap { DEF_CODEMAP.get_or_init(|| { let mut map = HashMap::default(); map.insert( @@ -272,7 +272,7 @@ fn def_message(gas_limit: u64) -> Message { Message:: { destination: DEF_ADDR, caller: DEF_CALLER, - input: Bytes::from_static(DEF_CD), + input: Bytes::from_static(DEF_CD).into(), value: DEF_VALUE, code_address: DEF_ADDR, gas_limit, @@ -280,6 +280,21 @@ fn def_message(gas_limit: u64) -> Message { } } +fn test_message( + gas_limit: u64, + bytecode: &[u8], + modify_message: Option)>, +) -> Message { + let mut message = def_message(gas_limit); + if let Some(modify_message) = modify_message { + modify_message(&mut message); + } + Message:: { + code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), + ..message + } +} + fn def_database() -> InMemoryDB { let mut database = InMemoryDB::default(); database.insert_account_info( @@ -307,7 +322,7 @@ fn def_database() -> InMemoryDB { for (key, value) in def_storage() { database.insert_account_storage(&DEF_ADDR, key, value); } - for number in [DEF_BN - U256::from(1), DEF_BN - U256::from(255), DEF_BN - U256::from(256)] { + for number in [DEF_BN - U256::ONE, DEF_BN - U256::from(255), DEF_BN - U256::from(256)] { database.insert_block_hash(&number, &number.into()); } database @@ -315,16 +330,16 @@ fn def_database() -> InMemoryDB { /// Host state snapshot for codegen assertions. pub struct HostState { - pub storage: HashMap, - pub transient_storage: HashMap, + pub storage: U256Map, + pub transient_storage: U256Map, pub logs: Vec, } impl HostState { fn from_evm(evm: &mut Evm<'_, BaseEvmTypes>) -> Self { let keys = [ - U256::from(0), - U256::from(1), + U256::ZERO, + U256::ONE, U256::from(69), U256::from(70), U256::from(200), @@ -359,18 +374,14 @@ fn prepare_host(spec_id: SpecId) -> Evm<'static, BaseEvmTypes> { def_database(), Precompiles::base(spec_id), ); + *evm.tx_env_mut() = def_tx_env(); for address in [Address::ZERO, DEF_ADDR, DEF_CALLER, OTHER_ADDR, Address::with_last_byte(0x69)] { evm.state_mut().prewarm(&address); } - for key in [ - U256::from(0), - U256::from(1), - U256::from(69), - U256::from(70), - U256::from(200), - U256::from(0xff), - ] { + for key in + [U256::ZERO, U256::ONE, U256::from(69), U256::from(70), U256::from(200), U256::from(0xff)] + { evm.state_mut().prewarm_storage_slot(&DEF_ADDR, key); } evm @@ -396,16 +407,8 @@ fn with_evm_context_and_host_mut< modify_message: Option)>, f: F, ) -> R { - let tx_env = def_tx_env(); - let mut message = def_message(DEF_GAS_LIMIT); - if let Some(modify_message) = modify_message { - modify_message(&mut message); - } - let message = Message:: { - code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), - ..message - }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let message = test_message(DEF_GAS_LIMIT, bytecode, modify_message); + let mut interpreter = Interpreter::::new(&message); interpreter.prepare_run(host); let (mut ecx, stack, stack_len) = @@ -490,7 +493,6 @@ fn run_compiled_test_case_with_context( gas_limit, inspect_stack: _, modify_message, - modify_ecx, expected_return, expected_stack, expected_memory, @@ -506,104 +508,43 @@ fn run_compiled_test_case_with_context( if gas_limit != DEF_GAS_LIMIT { ecx.gas = Gas::new(gas_limit); } - if let Some(modify_ecx) = modify_ecx { - modify_ecx(ecx); - ecx.refresh_memory_cache(); - } // Interpreter - run evm2 as the oracle - let tx_env = def_tx_env(); - let mut message = def_message(gas_limit); - if let Some(modify_message) = modify_message { - modify_message(&mut message); - } + let mut message = test_message(gas_limit, bytecode, modify_message); message.caller_is_static = is_static; - let message = Message:: { - code: Bytecode::new_legacy(Bytes::copy_from_slice(bytecode)), - ..message - }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let mut int_host = prepare_host(spec_id); - let int_stop = interpreter.run(&mut int_host).unwrap(); - let int_result = int_stop; + let int_result = interpreter.run(&mut int_host).unwrap(); let interpreter_output = interpreter.output(); - let mut expected_return = expected_return; - if expected_return == RETURN_WHAT_INTERPRETER_SAYS { - expected_return = int_result; - } else if modify_ecx.is_none() { - // Only check interpreter return if modify_ecx is not set. - // When modify_ecx is used, it only modifies the JIT context, not the interpreter's - // input, so the interpreter may return a different result. - assert!( - instruction_results_match_for_oracle(int_result, expected_return), - "interpreter return value mismatch: {int_result:?} != {expected_return:?}" - ); - } - - // When modify_ecx is set, the interpreter runs with different inputs than the JIT, - // so we cannot use interpreter results as expected values or compare against them. - let skip_interpreter_checks = modify_ecx.is_some() || expected_return.is_halt(); - let interpreter_stack_ref = (!skip_interpreter_checks).then(|| interpreter.stack()); - let interpreter_stack = interpreter_stack_ref.as_ref().map(|stack| stack.as_slice()); - - let mut expected_stack = expected_stack; - if expected_stack == STACK_WHAT_INTERPRETER_SAYS { - if skip_interpreter_checks { - expected_stack = &[]; // Will skip comparison below - } else { - expected_stack = interpreter_stack.unwrap(); - } - } else if !skip_interpreter_checks { - assert_eq!(interpreter_stack.unwrap(), expected_stack, "interpreter stack mismatch"); - } + let expected_return = if expected_return == RETURN_WHAT_INTERPRETER_SAYS { + int_result + } else { + assert_eq!(int_result, expected_return, "interpreter return value mismatch"); + expected_return + }; - let interpreter_memory = interpreter.memory().as_slice(); - let mut expected_memory = expected_memory; - if expected_memory == MEMORY_WHAT_INTERPRETER_SAYS { - if skip_interpreter_checks { - expected_memory = &[]; // Will skip comparison below - } else { - expected_memory = interpreter_memory; - } - } else if !skip_interpreter_checks { - assert_eq!( - MemDisplay(interpreter_memory), - MemDisplay(expected_memory), - "interpreter memory mismatch" + // On EVM halt all available gas is consumed, so the resulting stack, memory, and gas are + // unspecified and nothing compares them. A halting case must not set expectations for them. + let halts = expected_return.is_halt(); + if halts { + assert!( + matches!(expected_stack, &[] | STACK_WHAT_INTERPRETER_SAYS) + && matches!(expected_memory, &[] | MEMORY_WHAT_INTERPRETER_SAYS) + && matches!(expected_gas, 0 | GAS_WHAT_INTERPRETER_SAYS), + "halting test case sets an unchecked stack, memory, or gas expectation" ); } - let mut expected_gas = expected_gas; - if expected_gas == GAS_WHAT_INTERPRETER_SAYS { - if skip_interpreter_checks { - expected_gas = 0; // Will skip comparison below - } else { - expected_gas = interpreter.gas().spent(); - } - } else if !skip_interpreter_checks { - assert_eq!(interpreter.gas().spent(), expected_gas, "interpreter gas mismatch"); - } - let expected_output = if let Some(expected_output) = expected_output { - if !skip_interpreter_checks { - assert_eq!(interpreter_output, expected_output, "interpreter output mismatch"); - } + assert_eq!(interpreter_output, expected_output, "interpreter output mismatch"); Some(expected_output) - } else if skip_interpreter_checks { + } else if halts { None } else { Some(interpreter_output) }; - // Track whether we should skip JIT stack/gas/memory comparisons - let skip_jit_stack = - skip_interpreter_checks && test_case.expected_stack == STACK_WHAT_INTERPRETER_SAYS; - let skip_jit_memory = - skip_interpreter_checks && test_case.expected_memory == MEMORY_WHAT_INTERPRETER_SAYS; - let skip_jit_gas = - skip_interpreter_checks && test_case.expected_gas == GAS_WHAT_INTERPRETER_SAYS; - let actual_return = unsafe { f.call(ecx, stack, stack_len) }; if matches!( @@ -625,27 +566,41 @@ fn run_compiled_test_case_with_context( assert_eq!(actual_return, expected_return, "return value mismatch"); } - let actual_stack = - unsafe { stack.as_slice(*stack_len).iter().map(|x| x.to_u256()).collect::>() }; - - // On EVM halt all available gas is consumed, so resulting stack, memory, and gas do not - // matter. We do less work than the interpreter by bailing out earlier due to sections. - if !actual_return.is_halt() { - if !skip_jit_stack { - assert_eq!(actual_stack, *expected_stack, "stack mismatch"); - } - - if !skip_jit_memory { + if !halts { + let interpreter_stack = interpreter.stack(); + let expected_stack = if expected_stack == STACK_WHAT_INTERPRETER_SAYS { + interpreter_stack.as_slice() + } else { + assert_eq!(interpreter_stack.as_slice(), expected_stack, "interpreter stack mismatch"); + expected_stack + }; + let interpreter_memory = interpreter.memory().as_slice(); + let expected_memory = if expected_memory == MEMORY_WHAT_INTERPRETER_SAYS { + interpreter_memory + } else { assert_eq!( - MemDisplay(ecx.memory().as_slice()), + MemDisplay(interpreter_memory), MemDisplay(expected_memory), - "memory mismatch" + "interpreter memory mismatch" ); - } + expected_memory + }; + let expected_gas = if expected_gas == GAS_WHAT_INTERPRETER_SAYS { + interpreter.gas().spent() + } else { + assert_eq!(interpreter.gas().spent(), expected_gas, "interpreter gas mismatch"); + expected_gas + }; - if !skip_jit_gas { - assert_eq!(ecx.gas.spent(), expected_gas, "gas mismatch"); - } + let actual_stack = + unsafe { stack.as_slice(*stack_len).iter().map(|x| x.to_u256()).collect::>() }; + assert_eq!(actual_stack, *expected_stack, "stack mismatch"); + assert_eq!( + MemDisplay(ecx.memory().as_slice()), + MemDisplay(expected_memory), + "memory mismatch" + ); + assert_eq!(ecx.gas.spent(), expected_gas, "gas mismatch"); } if let Some(expected_output) = expected_output { @@ -657,11 +612,6 @@ fn run_compiled_test_case_with_context( } } -fn instruction_results_match_for_oracle(actual: InstrStop, expected: InstrStop) -> bool { - actual == expected - || matches!((actual, expected), (InstrStop::StackUnderflow, InstrStop::StackOverflow)) -} - #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] struct MemDisplay<'a>(&'a [u8]); impl fmt::Debug for MemDisplay<'_> { diff --git a/crates/jit/codegen/tests.rs b/crates/jit/codegen/tests.rs index 6c6e59039..1ce219d35 100644 --- a/crates/jit/codegen/tests.rs +++ b/crates/jit/codegen/tests.rs @@ -111,8 +111,6 @@ fn compile_test(path: &Path, build_dir: &Path, test_name: &str) { let mut compiler = EvmCompiler::new_llvm(false).unwrap(); compiler.set_opt_level(OptimizationLevel::Default); - compiler.set_profiling_support(false); - compiler.set_simple_perf(false); compiler.set_module_name(test_name); compiler.set_dump_to(Some(build_dir.to_path_buf())); unsafe { diff --git a/crates/jit/context/src/lib.rs b/crates/jit/context/src/lib.rs index c04e25ca7..c89e06711 100644 --- a/crates/jit/context/src/lib.rs +++ b/crates/jit/context/src/lib.rs @@ -26,6 +26,16 @@ mod arch; use arch::evm2_jit_entry; pub use arch::evm2_jit_exit; +/// Returns `true` if compiled code can run the interpreter's frame. +/// +/// Compiled code keeps the remaining gas in a signed counter, so a frame with more than +/// `i64::MAX` gas must run on the interpreter. Check this before calling +/// [`EvmCompilerFn::call_with_interpreter`] or [`EvmCompilerFn::call`]. +#[inline] +pub const fn can_run_compiled(interpreter: &Interpreter<'_, '_, T>) -> bool { + interpreter.gas().remaining() <= i64::MAX as u64 +} + /// The EVM bytecode compiler runtime context. /// /// This is a simple wrapper around the interpreter's resources, allowing the compiled function to @@ -64,8 +74,8 @@ pub struct EvmContext<'ctx, 'frame, 'host, T: EvmTypesHost = BaseEvmTypes> { /// The host's block environment, cached for block opcodes. pub block_env: NonNull>, - /// The transaction environment, cached for transaction opcodes. - pub tx_env: &'frame TxEnv, + /// The host's transaction environment, cached for transaction opcodes. + pub tx_env: NonNull>, _marker: PhantomData<&'ctx mut Interpreter<'frame, 'host, T>>, } @@ -96,10 +106,9 @@ impl<'ctx, 'frame, 'host, T: EvmTypesHost> EvmContext<'ctx, 'frame, 'host, T> { let interpreter_ptr = ptr::from_mut(&mut *interpreter); let interpreter_ptr = unsafe { NonNull::new_unchecked(interpreter_ptr) }; let block_env = NonNull::from(interpreter.host().block_env()); - let tx_env = interpreter.tx_env(); - let message = interpreter.message(); + let tx_env = NonNull::from(interpreter.host().tx_env()); let gas = interpreter.gas(); - let calldatasize = message.input.len(); + let calldatasize = interpreter.input().len(); let return_data_len = interpreter.return_data().len(); let (stack_ptr, stack_len) = unsafe { interpreter.stack_mut().into_raw_parts() }; let stack = unsafe { EvmStack::from_mut_ptr(stack_ptr.cast()) }; @@ -170,8 +179,8 @@ impl<'ctx, 'frame, 'host, T: EvmTypesHost> EvmContext<'ctx, 'frame, 'host, T> { /// Returns calldata bytes. #[inline] - pub const fn input(&self) -> &Bytes { - &self.message().input + pub const fn input(&self) -> &[u8] { + self.interpreter().input() } /// Returns the current block environment. @@ -180,10 +189,10 @@ impl<'ctx, 'frame, 'host, T: EvmTypesHost> EvmContext<'ctx, 'frame, 'host, T> { unsafe { self.block_env.as_ref() } } - /// Returns the transaction-global environment. + /// Returns the current transaction environment. #[inline] - pub const fn tx_env(&self) -> &'frame TxEnv { - self.tx_env + pub const fn tx_env(&self) -> &TxEnv { + unsafe { self.tx_env.as_ref() } } /// Returns active runtime version data. @@ -342,7 +351,7 @@ impl EvmCompilerFn { /// # Safety /// /// The caller must ensure that the function is safe to call for this interpreter state, and - /// that the remaining gas is at most `i64::MAX`. + /// that [`can_run_compiled`] holds. #[inline] pub unsafe fn call_with_interpreter<'ctx, 'frame, 'host, T: EvmTypesHost>( self, @@ -370,7 +379,7 @@ impl EvmCompilerFn { /// # Safety /// /// The caller must ensure that the arguments are valid, that the function is safe to call, and - /// that the remaining gas is at most `i64::MAX`. + /// that [`can_run_compiled`] holds. #[inline] pub unsafe fn call( self, @@ -831,7 +840,7 @@ mod tests { use evm2::{ DatabaseError, Evm, ExecutionError, InterpreterRunner, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, evm::{EmptyDB, precompile::NoPrecompiles}, interpreter::MessageExt, registry::TxRegistry, @@ -864,7 +873,6 @@ mod tests { .interpreter_mut() .fail(DatabaseError::new(core::fmt::Error, false)) } - let tx = TxEnvExt::default(); let mut message = MessageExt { gas_limit: 30_000, code: Bytecode::new_legacy(Bytes::from_static(&[0x00])), @@ -878,12 +886,12 @@ mod tests { NoPrecompiles::default(), ); host.set_interpreter_runner(TestRunner(EvmCompilerFn::new(fail))); - let error = host.execute_message(&tx, &mut message).unwrap_err(); + let error = host.execute_message(&mut message).unwrap_err(); let ExecutionError::Database(error) = error else { panic!("expected database error") }; assert!(!error.is_fatal()); assert!(error.downcast_ref::().is_some()); host.set_interpreter_runner(TestRunner(EvmCompilerFn::new(__test_fn))); - assert_eq!(host.execute_message(&tx, &mut message).unwrap().stop, InstrStop::Stop); + assert_eq!(host.execute_message(&mut message).unwrap().stop, InstrStop::Stop); } #[test] @@ -910,8 +918,8 @@ mod tests { #[test] fn extern_macro() { - let f1 = EvmCompilerFn::new(test_fn).0; - let f2 = EvmCompilerFn::new(__test_fn).0; + let f1 = EvmCompilerFn::new(test_fn).into_inner(); + let f2 = EvmCompilerFn::new(__test_fn).into_inner(); assert!(core::ptr::fn_addr_eq(f1, f2), "{f1:?} != {f2:?}"); } } diff --git a/crates/jit/llvm/Cargo.toml b/crates/jit/llvm/Cargo.toml index 61742d0d2..2c8181745 100644 --- a/crates/jit/llvm/Cargo.toml +++ b/crates/jit/llvm/Cargo.toml @@ -18,7 +18,7 @@ workspace = true [dependencies] evm2-jit-backend.workspace = true -inkwell = { version = "0.10.0", features = ["llvm23-1"] } +llvm-sys = "231" alloy-primitives = { workspace = true, features = ["std", "map-fxhash"] } object = { version = "0.40", default-features = false, features = ["read_core", "elf"] } tracing.workspace = true diff --git a/crates/jit/llvm/build.rs b/crates/jit/llvm/build.rs index 4f551b65f..ed1fb8bdd 100644 --- a/crates/jit/llvm/build.rs +++ b/crates/jit/llvm/build.rs @@ -1,46 +1,38 @@ #![allow(missing_docs)] +use std::process::Command; + fn main() { println!("cargo:rerun-if-changed=cpp/lib.cpp"); - let cxxflags = ["llvm-config", "llvm-config-23"] - .into_iter() - .find_map(detect_cxxflags) - .expect("no llvm-config found"); - - cc::Build::new() - .cpp(true) - .flags(cxxflags.split_whitespace()) - .flag("-w") - .file("cpp/lib.cpp") - .compile("evm2_jit_llvm_cpp"); -} + // `llvm-sys` (`links = "llvm-23"`) exports the `llvm-config` it links against, so `lib.cpp` + // compiles against the same headers. + let llvm_config = std::env::var("DEP_LLVM_23_CONFIG_PATH") + .expect("DEP_LLVM_23_CONFIG_PATH is not set; `llvm-sys` did not report its llvm-config"); -/// Returns the `--cxxflags` reported by `llvm_config`, or `None` if it cannot be used. -/// -/// [`Command::output`] only reports whether the process could be spawned, so a `llvm-config` that -/// exists but exits with a non-zero status still yields `Ok`. Checking the status keeps such a -/// broken binary from shadowing a working one later in the candidate list. -fn detect_cxxflags(llvm_config: &str) -> Option { - let output = std::process::Command::new(llvm_config) + let output = Command::new(&llvm_config) .arg("--cxxflags") .output() - .inspect_err(|error| eprintln!("failed to run {llvm_config}: {error}")) - .ok()?; + .unwrap_or_else(|error| panic!("failed to run {llvm_config}: {error}")); + assert!( + output.status.success(), + "{llvm_config} --cxxflags failed with {}: {}", + output.status, + String::from_utf8_lossy(&output.stderr).trim(), + ); + let cxxflags = String::from_utf8(output.stdout) + .unwrap_or_else(|error| panic!("{llvm_config} --cxxflags is not valid UTF-8: {error}")); - let status = output.status; - if !status.success() { - let stderr = String::from_utf8_lossy(&output.stderr); - let stderr = stderr.trim(); - if stderr.is_empty() { - eprintln!("{llvm_config} --cxxflags failed with {status}"); - } else { - eprintln!("{llvm_config} --cxxflags failed with {status}: {stderr}"); - } - return None; + let mut build = cc::Build::new(); + build.cpp(true); + for flag in cxxflags.split_whitespace() { + // Mark LLVM's include dirs as system dirs to silence warnings from its headers only. A + // distro LLVM reports `-I/usr/include`, which as `-isystem` would precede the C++ standard + // library's own directories and break `#include_next`. + match flag.strip_prefix("-I") { + Some(dir) if dir != "/usr/include" => build.flag("-isystem").flag(dir), + _ => build.flag(flag), + }; } - - String::from_utf8(output.stdout) - .inspect_err(|error| eprintln!("{llvm_config} --cxxflags is not valid UTF-8: {error}")) - .ok() + build.file("cpp/lib.cpp").compile("evm2_jit_llvm_cpp"); } diff --git a/crates/jit/llvm/cpp/lib.cpp b/crates/jit/llvm/cpp/lib.cpp index f749ea007..e72bac64f 100644 --- a/crates/jit/llvm/cpp/lib.cpp +++ b/crates/jit/llvm/cpp/lib.cpp @@ -35,6 +35,18 @@ extern "C" LLVMAttributeRef evm2_jit_llvm_create_initializes_attr(LLVMContextRef return wrap(Attribute::get(Ctx, Attribute::Initializes, ArrayRef(CR))); } +extern "C" LLVMAttributeRef evm2_jit_llvm_create_arg_mem_only_attr(LLVMContextRef C) { + return wrap(Attribute::getWithMemoryEffects(*unwrap(C), MemoryEffects::argMemOnly())); +} + +extern "C" LLVMAttributeRef evm2_jit_llvm_create_async_uwtable_attr(LLVMContextRef C) { + return wrap(Attribute::getWithUWTableKind(*unwrap(C), UWTableKind::Async)); +} + +extern "C" LLVMAttributeRef evm2_jit_llvm_create_no_captures_attr(LLVMContextRef C) { + return wrap(Attribute::getWithCaptureInfo(*unwrap(C), CaptureInfo::none())); +} + /// Remove a JITDylib from the ExecutionSession. extern "C" LLVMErrorRef evm2_jit_llvm_execution_session_remove_jit_dylib(LLVMOrcExecutionSessionRef ES, @@ -378,7 +390,7 @@ extern "C" void evm2_jit_llvm_set_dso_local(LLVMValueRef Val, bool Local) { /// The registration is leaked so that no destructor edits LLVM's global /// callback list at process exit while a compile thread may still read it. extern "C" void evm2_jit_llvm_expand_ir_insts_after_lsr() { - static auto *Callback = new RegisterTargetPassConfigCallback( + [[maybe_unused]] static auto *Callback = new RegisterTargetPassConfigCallback( [](TargetMachine &, legacy::PassManagerBase &, TargetPassConfig *PC) { // Only the IDs matter: the pipeline builds the inserted pass with its default // constructor, which uses `CodeGenOptLevel::None`. diff --git a/crates/jit/llvm/src/cpp.rs b/crates/jit/llvm/src/cpp.rs index f4ce9b5c6..cfc14208e 100644 --- a/crates/jit/llvm/src/cpp.rs +++ b/crates/jit/llvm/src/cpp.rs @@ -1,18 +1,14 @@ //! Custom bindings to LLVM. -use inkwell::{ - attributes::Attribute, - context::Context, - llvm_sys::{ - error::LLVMErrorRef, - orc2::{ - LLVMOrcExecutionSessionRef, LLVMOrcExecutorAddress, LLVMOrcJITDylibRef, - lljit::{LLVMOrcLLJITBuilderRef, LLVMOrcLLJITRef}, - }, - prelude::{LLVMAttributeRef, LLVMContextRef, LLVMValueRef}, - target_machine::{LLVMCodeGenOptLevel, LLVMTargetMachineRef}, +use crate::wrap::{Attribute, Context, Function}; +use llvm_sys::{ + error::LLVMErrorRef, + orc2::{ + LLVMOrcExecutionSessionRef, LLVMOrcExecutorAddress, LLVMOrcJITDylibRef, + lljit::{LLVMOrcLLJITBuilderRef, LLVMOrcLLJITRef}, }, - values::AsValueRef, + prelude::{LLVMAttributeRef, LLVMContextRef, LLVMValueRef}, + target_machine::{LLVMCodeGenOptLevel, LLVMTargetMachineRef}, }; use std::{ffi::c_char, sync::atomic::AtomicUsize}; @@ -24,6 +20,12 @@ unsafe extern "C" { upper: i64, ) -> LLVMAttributeRef; + fn evm2_jit_llvm_create_arg_mem_only_attr(ctx: LLVMContextRef) -> LLVMAttributeRef; + + fn evm2_jit_llvm_create_async_uwtable_attr(ctx: LLVMContextRef) -> LLVMAttributeRef; + + fn evm2_jit_llvm_create_no_captures_attr(ctx: LLVMContextRef) -> LLVMAttributeRef; + pub(crate) fn evm2_jit_llvm_jit_dylib_add_to_link_order( jd: LLVMOrcJITDylibRef, other: LLVMOrcJITDylibRef, @@ -67,13 +69,22 @@ unsafe extern "C" { pub(crate) fn evm2_jit_llvm_expand_ir_insts_after_lsr(); } -pub(crate) fn set_dso_local(f: inkwell::values::FunctionValue<'_>) { +pub(crate) fn set_dso_local(f: Function) { unsafe { evm2_jit_llvm_set_dso_local(f.as_value_ref(), true) }; } pub(crate) fn create_initializes_attr(cx: &Context, lower: i64, upper: i64) -> Attribute { - unsafe { - let raw = evm2_jit_llvm_create_initializes_attr(cx.raw(), lower, upper); - Attribute::new(raw) - } + Attribute::new(unsafe { evm2_jit_llvm_create_initializes_attr(cx.raw(), lower, upper) }) +} + +pub(crate) fn create_arg_mem_only_attr(cx: &Context) -> Attribute { + Attribute::new(unsafe { evm2_jit_llvm_create_arg_mem_only_attr(cx.raw()) }) +} + +pub(crate) fn create_async_uwtable_attr(cx: &Context) -> Attribute { + Attribute::new(unsafe { evm2_jit_llvm_create_async_uwtable_attr(cx.raw()) }) +} + +pub(crate) fn create_no_captures_attr(cx: &Context) -> Attribute { + Attribute::new(unsafe { evm2_jit_llvm_create_no_captures_attr(cx.raw()) }) } diff --git a/crates/jit/llvm/src/dh.rs b/crates/jit/llvm/src/dh.rs index 174eb90ed..d62a0a54a 100644 --- a/crates/jit/llvm/src/dh.rs +++ b/crates/jit/llvm/src/dh.rs @@ -1,14 +1,20 @@ -use inkwell::{ - context::{AsContextRef, Context}, - llvm_sys::{LLVMDiagnosticHandler, LLVMDiagnosticSeverity::*, core::*, prelude::*}, -}; -use std::{ffi::c_void, fmt, ptr}; +use crate::wrap::{Context, LLVMString}; +use evm2_jit_backend::{Result, eyre}; +use llvm_sys::{LLVMDiagnosticHandler, LLVMDiagnosticSeverity::*, core::*, prelude::*}; +use std::{ffi::c_void, fmt, sync::Mutex}; /// LLVM diagnostic handler guard. +/// +/// LLVM keeps compiling after an error diagnostic such as "ran out of registers" and then emits +/// broken code, so the handler records the first error and [`check`](Self::check) returns it to +/// the caller. pub(crate) struct DiagnosticHandlerGuard { cx: LLVMContextRef, prev_dh: LLVMDiagnosticHandler, prev_dhc: *mut c_void, + /// First error diagnostic since the last [`check`](Self::check). Boxed so the + /// handler's context pointer stays valid when the guard moves. + error: Box>>, } impl fmt::Debug for DiagnosticHandlerGuard { @@ -20,21 +26,37 @@ impl fmt::Debug for DiagnosticHandlerGuard { impl DiagnosticHandlerGuard { pub(crate) fn new(cx: &Context) -> Self { unsafe { - let c = cx.as_ctx_ref(); + let c = cx.raw(); let prev_dh = LLVMContextGetDiagnosticHandler(c); let prev_dhc = LLVMContextGetDiagnosticContext(c); - LLVMContextSetDiagnosticHandler(c, Some(Self::diagnostic_handler), ptr::null_mut()); - Self { cx: c, prev_dh, prev_dhc } + let error = Box::new(Mutex::new(None)); + let error_ptr = &*error as *const Mutex> as *mut c_void; + LLVMContextSetDiagnosticHandler(c, Some(Self::diagnostic_handler), error_ptr); + Self { cx: c, prev_dh, prev_dhc, error } } } - extern "C" fn diagnostic_handler(di: LLVMDiagnosticInfoRef, _context: *mut c_void) { + /// Returns `result`, adding the first error LLVM reported since the last call to it. Always + /// clears the recorded error so it does not leak into the next operation. + pub(crate) fn check(&self, result: Result) -> Result { + let Some(msg) = self.error.lock().unwrap().take() else { return result }; + match result { + Ok(_) => Err(eyre::eyre!("LLVM error: {msg}")), + Err(e) => Err(e.wrap_err(format!("LLVM error: {msg}"))), + } + } + + extern "C" fn diagnostic_handler(di: LLVMDiagnosticInfoRef, context: *mut c_void) { unsafe { // `LLVMGetDiagInfoDescription` returns an LLVM `Message`. - let msg_cstr = crate::llvm_string(LLVMGetDiagInfoDescription(di)); + let msg_cstr = LLVMString::from_raw(LLVMGetDiagInfoDescription(di)); let msg = msg_cstr.to_string_lossy(); match LLVMGetDiagInfoSeverity(di) { - LLVMDSError => error!(target: "llvm", "{msg}"), + LLVMDSError => { + error!(target: "llvm", "{msg}"); + let error = &*(context as *const Mutex>); + error.lock().unwrap().get_or_insert_with(|| msg.into_owned()); + } LLVMDSWarning => warn!(target: "llvm", "{msg}"), LLVMDSRemark => trace!(target: "llvm", "{msg}"), LLVMDSNote => debug!(target: "llvm", "{msg}"), diff --git a/crates/jit/llvm/src/lib.rs b/crates/jit/llvm/src/lib.rs index 1245890ff..9d80a93ea 100644 --- a/crates/jit/llvm/src/lib.rs +++ b/crates/jit/llvm/src/lib.rs @@ -8,30 +8,14 @@ extern crate tracing; use alloy_primitives::map::{FxBuildHasher, HashSet}; use evm2_jit_backend::{ Backend, BackendConfig, BackendTypes, Builder, CallConv, IntCC, OptimizationLevel, Result, - TailCallKind, TypeMethods, U256, eyre, format_bytes, + TailCallKind, TypeMethods, U256, eyre, eyre::OptionExt, format_bytes, }; -use inkwell::{ - AddressSpace, IntPredicate, - attributes::{Attribute, AttributeLoc}, - basic_block::BasicBlock, - debug_info::{ - AsDIScope, DICompileUnit, DIFlags, DIFlagsConstants, DISubprogram, DWARFEmissionKind, - DWARFSourceLanguage, DebugInfoBuilder, - }, - module::{FlagBehavior, Module}, - passes::PassBuilderOptions, - support::error_handling::install_fatal_error_handler, - targets::{ - CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine, TargetTriple, - }, - types::{ - AnyType, AnyTypeEnum, BasicType, BasicTypeEnum, FunctionType, IntType, PointerType, - StringRadix, VoidType, - }, - values::{ - AsValueRef, BasicMetadataValueEnum, BasicValue, BasicValueEnum, CallSiteValue, - FunctionValue, InstructionValue, PointerValue, UnnamedAddress, - }, +use llvm_sys::{ + LLVMCallConv, LLVMIntPredicate, LLVMLinkage, LLVMModuleFlagBehavior, LLVMTailCallKind, + LLVMUnnamedAddr, + error_handling::LLVMInstallFatalErrorHandler, + support::LLVMParseCommandLineOptions, + target_machine::{LLVMCodeGenFileType, LLVMCodeGenOptLevel, LLVMCodeModel, LLVMRelocMode}, }; use object::{Object, ObjectSymbol}; use std::{ @@ -42,24 +26,30 @@ use std::{ mem::ManuallyDrop, path::Path, sync::{ - Arc, Once, OnceLock, + Arc, OnceLock, atomic::{AtomicU64, AtomicUsize, Ordering}, }, }; - -pub use inkwell::{self, context::Context}; +use wrap::{ + Attribute, AttributeLoc, DICompileUnit, DebugInfoBuilder, Metadata, PassBuilderOptions, Target, +}; mod cpp; mod dh; pub mod orc; - -mod utils; -pub(crate) use utils::*; +mod wrap; +pub use wrap::{Block, Context, Function, Inst, LLVMString, Module, TargetMachine, Type, Value}; /// Branch weight for non-cold branches. const DEFAULT_WEIGHT: u32 = 20000; +/// Largest function, in basic blocks, that [`EvmLlvmBackend::optimize_module`] runs LICM on. +/// +/// LICM only pays off on hot loops in small functions. The cutoff comes from the sweep in +/// `EXPERIMENTS.md`. +const LICM_MAX_BLOCKS: u32 = 1000; + /// Current JIT memory usage counters. /// /// Counters reflect live memory: bytes are added on compilation and @@ -130,9 +120,12 @@ type FxHashMap = alloy_primitives::map::HashMap; #[must_use] pub struct EvmLlvmBackend { cx: &'static Context, - _dh: dh::DiagnosticHandlerGuard, - bcx: inkwell::builder::Builder<'static>, - module: Module<'static>, + dh: dh::DiagnosticHandlerGuard, + /// LLVM debug info builder and compile unit, created lazily when `debug_file` is set. + /// Dropped before `module`, which its builder refers to. + di_state: Option, + bcx: wrap::Builder, + module: Module, machine: TargetMachine, /// ORC JIT state. `None` in AOT mode. @@ -145,17 +138,14 @@ pub struct EvmLlvmBackend { /// Owned context for AOT mode. `None` in JIT mode. _aot_cx: Option>, - /// LLVM debug info builder and compile unit, created lazily when `debug_file` is set. - di_state: Option, - - ty_void: VoidType<'static>, - ty_ptr: PointerType<'static>, - ty_i1: IntType<'static>, - ty_i8: IntType<'static>, - ty_i32: IntType<'static>, - ty_i64: IntType<'static>, - ty_i256: IntType<'static>, - ty_isize: IntType<'static>, + ty_void: Type, + ty_ptr: Type, + ty_i1: Type, + ty_i8: Type, + ty_i32: Type, + ty_i64: Type, + ty_i256: Type, + ty_isize: Type, aot: bool, backend_config: BackendConfig, @@ -288,30 +278,29 @@ impl GlobalOrcJit { } /// Creates a fresh JITDylib for a compiler. - fn create_jit_dylib(&self) -> orc::JITDylibRef { + fn create_jit_dylib(&self) -> Result { + // Create the process symbol generator first, so a failure leaves no dylib behind. + // It lets the JITDylib resolve libc and other process-level symbols (e.g. printf, + // memcpy) that JIT-compiled code may reference. + let prefix = self.jit.get_global_prefix(); + let generator = orc::DefinitionGenerator::for_current_process(prefix).map_err(error_msg)?; let id = self.next_dylib_id.fetch_add(1, Ordering::Relaxed); let name = CString::new(format!("evm2_jit.compiler.{id}")).unwrap(); let es = self.jit.get_execution_session(); let jd = es.create_bare_jit_dylib(&name); // Link against the builtins JD so compiled code can resolve builtin symbols. jd.add_to_link_order(self.builtins_jd); - // Attach a process symbol generator so the JITDylib can resolve libc and other - // process-level symbols (e.g. printf, memcpy) that JIT-compiled code may reference. - let prefix = self.jit.get_global_prefix(); - if let Ok(generator) = orc::DefinitionGenerator::for_current_process(prefix) { - jd.add_generator(generator); - } - jd + jd.add_generator(generator); + Ok(jd) } /// Removes a JITDylib from the ExecutionSession, freeing all its resources. fn remove_jit_dylib(&self, jd: orc::JITDylibRef) { - let es = self.jit.get_execution_session(); - if let Err(e) = es.remove_jit_dylib(jd) { + if let Err(e) = self.jit.get_execution_session().remove_jit_dylib(jd) { error!("failed to remove JITDylib: {e}"); } // Reclaim unreferenced interned strings after removing symbols. - es.get_symbol_string_pool().clear_dead_entries(); + self.gc(); } /// Defines absolute symbols in the shared builtins JITDylib, skipping any @@ -380,7 +369,7 @@ struct OrcJitState { /// must run while the tracker handles are still live. jd_guard: Arc, /// Functions in the current staging module (not yet committed to JIT). - staged_functions: FxHashMap>, + staged_functions: FxHashMap, /// Absolute symbols collected during translation, flushed to the global /// builtins JITDylib before commit. pending_symbols: Vec<(CString, usize)>, @@ -409,7 +398,7 @@ impl fmt::Debug for OrcJitState { impl OrcJitState { fn new(debug_support: bool, profiling_support: bool, simple_perf: bool) -> Result { let global = GlobalOrcJit::get(debug_support, profiling_support, simple_perf)?; - let jd = global.create_jit_dylib(); + let jd = global.create_jit_dylib()?; let jd_guard = Arc::new(JitDylibGuard { global, jd }); Ok(Self { global, @@ -444,7 +433,7 @@ impl OrcJitState { fn maybe_clear_dead_pool_entries(&mut self) { self.clear_pool_counter += 1; if self.clear_pool_counter.is_multiple_of(256) { - self.global.jit.get_execution_session().get_symbol_string_pool().clear_dead_entries(); + self.global.gc(); } } @@ -467,19 +456,6 @@ fn link_jit_object_in_dylib( Ok((addr, tracker)) } -/// Wraps a module in a [`orc::ThreadSafeModule`] for transfer to LLJIT. -/// -/// Uses a raw pointer cast to work around `Module<'ctx>` invariance — the module's -/// context is genuinely owned by `tscx`. -fn create_thread_safe_module( - tscx: &orc::ThreadSafeContext, - module: Module<'static>, -) -> orc::ThreadSafeModule { - let module = std::mem::ManuallyDrop::new(module); - // SAFETY: The module was created in the context owned by `tscx`. - unsafe { orc::ThreadSafeModule::create_in_context(Module::new(module.as_mut_ptr()), tscx) } -} - /// Creates an ORC-owned LLVM context, returning a `&'static` reference to it. /// /// In JIT mode, ORC owns the context via a [`orc::ThreadSafeContext`] so that modules can be @@ -500,8 +476,8 @@ fn create_orc_context() -> (&'static Context, orc::ThreadSafeContext, Box, - compile_unit: DICompileUnit<'static>, + dibuilder: DebugInfoBuilder, + compile_unit: DICompileUnit, finalized: bool, } @@ -523,7 +499,7 @@ impl EvmLlvmBackend { let config = BackendConfig::default(); init()?; - let target_info = TargetInfo::new(); + let target_info = TargetInfo::new()?; let target = &target_info.target; let machine = target .create_target_machine( @@ -531,10 +507,10 @@ impl EvmLlvmBackend { &target_info.cpu, &target_info.features, convert_opt_level(config.opt_level), - if aot { RelocMode::PIC } else { RelocMode::Static }, - CodeModel::Default, + if aot { LLVMRelocMode::LLVMRelocPIC } else { LLVMRelocMode::LLVMRelocStatic }, + LLVMCodeModel::LLVMCodeModelDefault, ) - .ok_or_else(|| eyre::eyre!("failed to create target machine"))?; + .ok_or_eyre("failed to create target machine")?; // In JIT mode, ORC owns the context via a ThreadSafeContext so that modules can be // safely transferred to the JIT without double-ownership issues with the TLS context. @@ -568,13 +544,12 @@ impl EvmLlvmBackend { let ty_i8 = cx.i8_type(); let ty_i32 = cx.i32_type(); let ty_i64 = cx.i64_type(); - let ty_i256 = - cx.custom_width_int_type(std::num::NonZeroU32::new(256).unwrap()).expect("i256"); - let ty_isize = cx.ptr_sized_int_type(&machine.get_target_data(), None); - let ty_ptr = cx.ptr_type(AddressSpace::default()); + let ty_i256 = cx.int_type(256); + let ty_isize = cx.ptr_sized_int_type(&machine.get_target_data()); + let ty_ptr = cx.ptr_type(); Ok(Self { cx, - _dh: dh::DiagnosticHandlerGuard::new(cx), + dh: dh::DiagnosticHandlerGuard::new(cx), bcx, module, machine, @@ -606,7 +581,7 @@ impl EvmLlvmBackend { } #[inline] - const fn module(&self) -> &Module<'static> { + const fn module(&self) -> &Module { &self.module } @@ -621,16 +596,8 @@ impl EvmLlvmBackend { Ok(self.orc.as_mut().unwrap()) } - fn fn_type( - &self, - ret: Option>, - params: &[BasicTypeEnum<'static>], - ) -> FunctionType<'static> { - let params = params.iter().copied().map(Into::into).collect::>(); - match ret { - Some(ret) => ret.fn_type(¶ms, false), - None => self.ty_void.fn_type(¶ms, false), - } + fn fn_type(&self, ret: Option, params: &[Type]) -> Type { + ret.unwrap_or(self.ty_void).fn_type(params, false) } /// Returns the given name if IR output is being dumped, otherwise an empty string. @@ -660,12 +627,12 @@ impl EvmLlvmBackend { // Add required module flags for debug info. self.module().add_basic_value_flag( "Debug Info Version", - FlagBehavior::Warning, - self.ty_i32.const_int(inkwell::debug_info::debug_metadata_version() as u64, false), + LLVMModuleFlagBehavior::LLVMModuleFlagBehaviorWarning, + self.ty_i32.const_int(wrap::debug_metadata_version() as u64, false), ); self.module().add_basic_value_flag( "Dwarf Version", - FlagBehavior::Warning, + LLVMModuleFlagBehavior::LLVMModuleFlagBehaviorWarning, self.ty_i32.const_int(5, false), ); @@ -681,22 +648,13 @@ impl EvmLlvmBackend { flags.push(if self.aot { "--aot" } else { "--jit" }); let flags = flags.join(" "); - let (dibuilder, compile_unit) = self.module().create_debug_info_builder( - true, - DWARFSourceLanguage::C, + let (dibuilder, compile_unit) = DebugInfoBuilder::new( + self.module(), &filename, &directory, "evm2_jit", is_optimized, &flags, - 0, - "", - DWARFEmissionKind::Full, - 0, - false, - false, - "", - "", ); self.di_state = Some(DiState { dibuilder, compile_unit, finalized: false }); @@ -725,8 +683,10 @@ impl EvmLlvmBackend { let tracker = orc.jd().create_resource_tracker(); - let tsm = create_thread_safe_module(tscx, old_module); - orc.global.jit.add_module_with_rt(tsm, &tracker).map_err(error_msg)?; + orc.global + .jit + .add_module_with_rt(tscx.create_module(old_module), &tracker) + .map_err(error_msg)?; let tracker_idx = orc.loaded_trackers.len(); for &id in orc.staged_functions.keys() { @@ -752,6 +712,14 @@ impl EvmLlvmBackend { Ok(()) } + /// Removes the committed module that contains `id`, with the code of all its functions. + fn remove_committed_module(&mut self, id: u32) -> Result<()> { + let Some(orc) = &mut self.orc else { return Ok(()) }; + let Some(tracker_idx) = orc.committed_functions.remove(&id) else { return Ok(()) }; + orc.committed_functions.retain(|_, idx| *idx != tracker_idx); + orc.loaded_trackers[tracker_idx].remove().map_err(error_msg) + } + /// Returns pending absolute symbols collected while translating the current JIT module. pub fn pending_symbol_names(&self) -> Vec { self.orc @@ -784,7 +752,7 @@ impl EvmLlvmBackend { symbols: &[(CString, usize)], ) -> Result<(usize, orc::ResourceTracker, Arc)> { let orc = self.ensure_orc()?; - let jd = orc.global.create_jit_dylib(); + let jd = orc.global.create_jit_dylib()?; let jd_guard = Arc::new(JitDylibGuard { global: orc.global, jd }); let (addr, tracker) = link_jit_object_in_dylib(orc, jd, symbol_name, object, symbols)?; Ok((addr, tracker, jd_guard)) @@ -820,21 +788,21 @@ impl EvmLlvmBackend { } impl BackendTypes for EvmLlvmBackend { - type Type = BasicTypeEnum<'static>; - type Value = BasicValueEnum<'static>; - type StackSlot = PointerValue<'static>; - type BasicBlock = BasicBlock<'static>; - type Function = FunctionValue<'static>; - type Inst = InstructionValue<'static>; + type Type = Type; + type Value = Value; + type StackSlot = Value; + type BasicBlock = Block; + type Function = Function; + type Inst = Inst; } impl TypeMethods for EvmLlvmBackend { fn type_ptr(&self) -> Self::Type { - self.ty_ptr.into() + self.ty_ptr } fn type_ptr_sized_int(&self) -> Self::Type { - self.ty_isize.into() + self.ty_isize } fn type_int(&self, bits: u32) -> Self::Type { @@ -846,20 +814,16 @@ impl TypeMethods for EvmLlvmBackend { 64 => self.ty_i64, 128 => self.cx.i128_type(), 256 => self.ty_i256, - bits => self - .cx - .custom_width_int_type(std::num::NonZeroU32::new(bits).unwrap()) - .expect("custom int type"), + bits => self.cx.int_type(bits), } - .into() } fn type_array(&self, ty: Self::Type, size: u32) -> Self::Type { - ty.array_type(size).into() + ty.array_type(size) } fn type_bit_width(&self, ty: Self::Type) -> u32 { - ty.into_int_type().get_bit_width() + ty.bit_width() } } @@ -890,7 +854,7 @@ impl Backend for EvmLlvmBackend { unsafe { cpp::evm2_jit_llvm_target_machine_set_opt_level( self.machine.as_mut_ptr(), - convert_opt_level(config.opt_level).into(), + convert_opt_level(config.opt_level), ); } } @@ -920,7 +884,11 @@ impl Backend for EvmLlvmBackend { } fn dump_disasm(&mut self, path: &Path) -> Result<()> { - self.machine.write_to_file(self.module(), FileType::Assembly, path).map_err(error_msg) + let result = self + .machine + .write_to_file(self.module(), LLVMCodeGenFileType::LLVMAssemblyFile, path) + .map_err(error_msg); + self.dh.check(result) } fn build_function( @@ -968,28 +936,15 @@ impl Backend for EvmLlvmBackend { // Attach debug info subprogram if debug is active. self.ensure_di_state(); - let debug_scope = if let Some(di) = &self.di_state { - let file = di.compile_unit.get_file(); - let subroutine_type = - di.dibuilder.create_subroutine_type(file, None, &[], DIFlags::PUBLIC); - let subprogram = di.dibuilder.create_function( - di.compile_unit.as_debug_info_scope(), + let debug_scope = self.di_state.as_ref().map(|di| { + let subprogram = di.dibuilder.create_subprogram( + di.compile_unit, name, - None, - file, - 0, - subroutine_type, - true, - true, - 0, - DIFlags::PUBLIC, self.backend_config.opt_level != OptimizationLevel::None, ); function.set_subprogram(subprogram); - Some(subprogram) - } else { - None - }; + subprogram + }); let builder = EvmLlvmBuilder { backend: self, function, debug_scope }; Ok((builder, id)) @@ -1007,20 +962,18 @@ impl Backend for EvmLlvmBackend { // // The standard `default` is also slow (~730ms on snailtracer) because the loop // analysis infrastructure (LoopInfo, DominatorTree, MemorySSA, LCSSA) is expensive to - // compute on functions with thousands of basic blocks, even though the loop passes - // themselves do nothing useful — EVM has no natural loops to optimize. + // compute on functions with thousands of basic blocks. // // LICM (Loop Invariant Code Motion) helps tight EVM loops by hoisting gas counter and - // stack slot loads/stores into registers. However, the loop analysis infrastructure is - // quadratic on large functions — e.g. +430ms on snailtracer (7770 BBs) vs +0ms on - // fibonacci (45 BBs). We skip it for functions with >4000 basic blocks. + // stack slot loads/stores into registers. It only pays off on small functions, and its + // compile cost grows faster than the function: see `LICM_MAX_BLOCKS`. // // Can be overridden with `EVM2_JIT_PASSES` env var for experimentation. // From `opt --help`, `-passes`. - static PASSES_OVERRIDE: std::sync::OnceLock> = std::sync::OnceLock::new(); - static PASSES: std::sync::OnceLock = std::sync::OnceLock::new(); - static PASSES_WITH_LICM: std::sync::OnceLock = std::sync::OnceLock::new(); + static PASSES_OVERRIDE: OnceLock> = OnceLock::new(); + static PASSES: OnceLock = OnceLock::new(); + static PASSES_WITH_LICM: OnceLock = OnceLock::new(); let passes = PASSES_OVERRIDE.get_or_init(|| std::env::var("EVM2_JIT_PASSES").ok()); let passes = passes.as_deref().unwrap_or_else(|| match self.backend_config.opt_level { @@ -1028,22 +981,24 @@ impl Backend for EvmLlvmBackend { OptimizationLevel::Less | OptimizationLevel::Default | OptimizationLevel::Aggressive => { - let total_bbs: u32 = - self.module.get_functions().map(|f| f.count_basic_blocks()).sum(); - let with_licm = total_bbs <= 4000; + let max_blocks = + self.module.get_functions().map(|f| f.count_basic_blocks()).max().unwrap_or(0); + let with_licm = max_blocks <= LICM_MAX_BLOCKS; let passes = if with_licm { &PASSES_WITH_LICM } else { &PASSES }; passes.get_or_init(|| build_pass_pipeline(with_licm)) } }); let opts = PassBuilderOptions::create(); - self.module().run_passes(passes, &self.machine, opts).map_err(error_msg) + let result = self.module().run_passes(passes, &self.machine, &opts).map_err(error_msg); + self.dh.check(result) } fn write_object(&mut self, mut w: W) -> Result<()> { let buffer = self .machine - .write_to_memory_buffer(self.module(), FileType::Object) - .map_err(error_msg)?; + .write_to_memory_buffer(self.module(), LLVMCodeGenFileType::LLVMObjectFile) + .map_err(error_msg); + let buffer = self.dh.check(buffer)?; w.write_all(buffer.as_slice())?; Ok(()) } @@ -1059,10 +1014,23 @@ impl Backend for EvmLlvmBackend { // in lookup_in when the symbol is first requested. let mut captured = None; let _guard = ScopedObjCapture::install(&mut captured); - let addr = orc.global.jit.lookup_in(orc.jd(), &name).map_err(error_msg)?; + let addr = orc.global.jit.lookup_in(orc.jd(), &name).map_err(error_msg); drop(_guard); + let compiled = addr.is_ok(); + let addr = match self.dh.check(addr) { + Ok(addr) => addr, + Err(e) => { + // ORC compiled the whole module and keeps it, so later lookups would return the + // broken code without the error. Remove the module's code but keep the function + // names, so a later lookup fails with an error instead of a missing name. + if compiled && let Err(remove_err) = self.remove_committed_module(id) { + warn!("failed to remove module after an LLVM error: {remove_err}"); + } + return Err(e); + } + }; if captured.is_some() { - orc.last_compiled_object = captured; + self.orc.as_mut().unwrap().last_compiled_object = captured; } Ok(addr) } @@ -1137,38 +1105,27 @@ impl Backend for EvmLlvmBackend { /// Cached target information for the host machine. #[derive(Debug)] struct TargetInfo { - triple: TargetTriple, + triple: CString, target: Target, cpu: String, features: String, } -// SAFETY: No mutability is exposed and `TargetTriple` is an owned string. -unsafe impl std::marker::Send for TargetInfo {} -unsafe impl std::marker::Sync for TargetInfo {} - -impl Clone for TargetInfo { - fn clone(&self) -> Self { - let triple = TargetTriple::create(self.triple.as_str().to_str().unwrap()); - Self { - target: Target::from_triple(&triple).unwrap(), - triple, - cpu: self.cpu.clone(), - features: self.features.clone(), - } - } -} - impl TargetInfo { - fn new() -> &'static Self { - static HOST_TARGET_INFO: OnceLock = OnceLock::new(); - HOST_TARGET_INFO.get_or_init(|| { - let triple = TargetMachine::get_default_triple(); - let target = Target::from_triple(&triple).unwrap(); - let cpu = TargetMachine::get_host_cpu_name().to_string_lossy().into_owned(); - let features = TargetMachine::get_host_cpu_features().to_string_lossy().into_owned(); - Self { target, triple, cpu, features } - }) + fn new() -> Result<&'static Self> { + static HOST_TARGET_INFO: OnceLock> = OnceLock::new(); + HOST_TARGET_INFO + .get_or_init(|| { + let triple = TargetMachine::get_default_triple().to_owned(); + let target = Target::from_triple(&triple) + .map_err(|e| e.to_string_lossy().trim_end().to_string())?; + let cpu = TargetMachine::get_host_cpu_name().to_string_lossy().into_owned(); + let features = + TargetMachine::get_host_cpu_features().to_string_lossy().into_owned(); + Ok(Self { target, triple, cpu, features }) + }) + .as_ref() + .map_err(|e| eyre::eyre!("{e}")) } } @@ -1177,8 +1134,8 @@ impl TargetInfo { #[must_use] pub struct EvmLlvmBuilder<'a> { backend: &'a mut EvmLlvmBackend, - function: FunctionValue<'static>, - debug_scope: Option>, + function: Function, + debug_scope: Option, } impl std::ops::Deref for EvmLlvmBuilder<'_> { @@ -1199,75 +1156,37 @@ impl std::ops::DerefMut for EvmLlvmBuilder<'_> { impl EvmLlvmBuilder<'_> { #[allow(dead_code)] - fn extract_value( - &mut self, - value: BasicValueEnum<'static>, - index: u32, - name: &str, - ) -> BasicValueEnum<'static> { - self.bcx.build_extract_value(value.into_struct_value(), index, self.name(name)).unwrap() + fn extract_value(&mut self, value: Value, index: u32, name: &str) -> Value { + self.bcx.build_extract_value(value, index, self.name(name)) } - fn memcpy_inner( - &mut self, - dst: BasicValueEnum<'static>, - src: BasicValueEnum<'static>, - len: BasicValueEnum<'static>, - inline: bool, - ) { - let dst = dst.into_pointer_value(); - let src = src.into_pointer_value(); - let len = len.into_int_value(); + fn memcpy_inner(&mut self, dst: Value, src: Value, len: Value, inline: bool) { let volatile = self.bool_const(false); - let name = format!( - "llvm.memcpy{}.p0.p0.{}", - if inline { ".inline" } else { "" }, - fmt_ty(len.get_type().into()), - ); let len_ty = len.get_type(); + let name = + format!("llvm.memcpy{}.p0.p0.{}", if inline { ".inline" } else { "" }, fmt_ty(len_ty),); let memcpy = self.get_or_add_function(&name, |this| { - this.ty_void.fn_type( - &[this.ty_ptr.into(), this.ty_ptr.into(), len_ty.into(), this.ty_i1.into()], - false, - ) + this.ty_void.fn_type(&[this.ty_ptr, this.ty_ptr, len_ty, this.ty_i1], false) }); - self.bcx - .build_call(memcpy, &[dst.into(), src.into(), len.into(), volatile.into()], "") - .unwrap(); + self.bcx.build_call(memcpy, &[dst, src, len, volatile], ""); } #[allow(dead_code)] - fn call_overflow_function( - &mut self, - name: &str, - lhs: BasicValueEnum<'static>, - rhs: BasicValueEnum<'static>, - ) -> (BasicValueEnum<'static>, BasicValueEnum<'static>) { + fn call_overflow_function(&mut self, name: &str, lhs: Value, rhs: Value) -> (Value, Value) { let f = self.get_overflow_function(name, lhs.get_type()); let result = self.call(f, &[lhs, rhs]).unwrap(); (self.extract_value(result, 0, "result"), self.extract_value(result, 1, "overflow")) } #[allow(dead_code)] - fn get_overflow_function( - &mut self, - name: &str, - ty: BasicTypeEnum<'static>, - ) -> FunctionValue<'static> { + fn get_overflow_function(&mut self, name: &str, ty: Type) -> Function { let name = format!("llvm.{name}.with.overflow.{}", fmt_ty(ty)); self.get_or_add_function(&name, |this| { - this.fn_type( - Some(this.cx.struct_type(&[ty, this.ty_i1.into()], false).into()), - &[ty, ty], - ) + this.fn_type(Some(this.cx().struct_type(&[ty, this.ty_i1], false)), &[ty, ty]) }) } - fn get_sat_function( - &mut self, - name: &str, - ty: BasicTypeEnum<'static>, - ) -> FunctionValue<'static> { + fn get_sat_function(&mut self, name: &str, ty: Type) -> Function { let name = format!("llvm.{name}.sat.{}", fmt_ty(ty)); self.get_or_add_function(&name, |this| this.fn_type(Some(ty), &[ty, ty])) } @@ -1275,40 +1194,24 @@ impl EvmLlvmBuilder<'_> { fn get_or_add_function( &mut self, name: &str, - mk_ty: impl FnOnce(&mut Self) -> FunctionType<'static>, - ) -> FunctionValue<'static> { - match self.module().get_function(name) { - Some(function) => function, - None => { - let ty = mk_ty(self); - self.module().add_function(name, ty, None) - } - } + mk_ty: impl FnOnce(&mut Self) -> Type, + ) -> Function { + self.module().get_function(name).unwrap_or_else(|| { + let ty = mk_ty(self); + self.module().add_function(name, ty, None) + }) } - fn set_branch_weights( - &self, - inst: InstructionValue<'static>, - weights: impl IntoIterator, - ) { + fn set_branch_weights(&self, inst: Inst, weights: impl IntoIterator) { let weights = weights.into_iter(); - let mut values = Vec::>::with_capacity( - 1 + weights.size_hint().1.unwrap(), - ); - values.push(self.cx.metadata_string("branch_weights").into()); + let mut values = Vec::::with_capacity(1 + weights.size_hint().1.unwrap()); + values.push(self.cx().metadata_string("branch_weights")); for weight in weights { - values.push(self.ty_i32.const_int(weight as u64, false).into()); + values.push(self.ty_i32.const_int(weight as u64, false).as_metadata()); } - let metadata = self.cx.metadata_node(&values); - let kind_id = self.cx.get_kind_id("prof"); - inst.set_metadata(metadata, kind_id).unwrap(); - } - - fn assume_inner(&mut self, cond: BasicValueEnum<'static>) -> CallSiteValue<'static> { - let function = self.get_or_add_function("llvm.assume", |this| { - this.ty_void.fn_type(&[this.ty_i1.into()], false) - }); - self.bcx.build_call(function, &[cond.into()], "").unwrap() + let metadata = self.cx().metadata_node(&values); + let kind_id = self.cx().get_kind_id("prof"); + inst.set_metadata(metadata, kind_id); } } @@ -1345,11 +1248,11 @@ impl TypeMethods for EvmLlvmBuilder<'_> { impl Builder for EvmLlvmBuilder<'_> { fn create_block(&mut self, name: &str) -> Self::BasicBlock { - self.cx.append_basic_block(self.function, self.name(name)) + self.cx().append_basic_block(self.function, self.name(name)) } fn create_block_after(&mut self, after: Self::BasicBlock, name: &str) -> Self::BasicBlock { - self.cx.insert_basic_block_after(after, self.name(name)) + self.cx().insert_basic_block_after(after, self.name(name)) } fn switch_to_block(&mut self, block: Self::BasicBlock) { @@ -1365,19 +1268,12 @@ impl Builder for EvmLlvmBuilder<'_> { // Nothing to do. } - fn set_current_block_cold(&mut self) { - let true_ = self.bool_const(true); - let callsite = self.assume_inner(true_); - let cold = self.cx.create_enum_attribute(Attribute::get_named_enum_kind_id("cold"), 0); - callsite.add_attribute(AttributeLoc::Function, cold); - } - fn current_block(&mut self) -> Option { self.bcx.get_insert_block() } fn block_addr(&mut self, block: Self::BasicBlock) -> Option { - unsafe { block.get_address().map(Into::into) } + unsafe { block.get_address() } } fn add_comment_to_current_inst(&mut self, comment: &str) { @@ -1386,21 +1282,15 @@ impl Builder for EvmLlvmBuilder<'_> { } let Some(block) = self.current_block() else { return }; let Some(ins) = block.get_last_instruction() else { return }; - let metadata = self.cx.metadata_string(comment); - let metadata = self.cx.metadata_node(&[metadata.into()]); - ins.set_metadata(metadata, self.cx.get_kind_id("annotation")).unwrap(); + let metadata = self.cx().metadata_string(comment); + let metadata = self.cx().metadata_node(&[metadata]); + ins.set_metadata(metadata, self.cx().get_kind_id("annotation")); } fn set_debug_location(&mut self, line: u32, col: u32) { let Some(scope) = self.debug_scope else { return }; let Some(di) = &self.di_state else { return }; - let loc = di.dibuilder.create_debug_location( - self.cx, - line, - col, - scope.as_debug_info_scope(), - None, - ); + let loc = di.dibuilder.create_debug_location(self.cx(), line, col, scope); self.bcx.set_current_debug_location(loc); } @@ -1419,73 +1309,72 @@ impl Builder for EvmLlvmBuilder<'_> { } fn bool_const(&mut self, value: bool) -> Self::Value { - self.ty_i1.const_int(value as u64, false).into() + self.ty_i1.const_int(value as u64, false) } fn iconst(&mut self, ty: Self::Type, value: i64) -> Self::Value { - ty.into_int_type().const_int(value as u64, value.is_negative()).into() + ty.const_int(value as u64, value.is_negative()) } fn uconst(&mut self, ty: Self::Type, value: u64) -> Self::Value { - ty.into_int_type().const_int(value, false).into() + ty.const_int(value, false) } fn iconst_256(&mut self, value: impl TryInto) -> Self::Value { let value = value.try_into().ok().expect("invalid U256 value"); if let Ok(low) = value.try_into() { - return self.ty_i256.const_int(low, false).into(); + return self.ty_i256.const_int(low, false); } self.scratch.clear(); write!(self.scratch, "{value:x}").unwrap(); - self.ty_i256.const_int_from_string(&self.scratch, StringRadix::Hexadecimal).unwrap().into() + self.ty_i256.const_int_from_hex(&self.scratch) } fn str_const(&mut self, value: &str) -> Self::Value { - self.bcx.build_global_string_ptr(value, "").unwrap().as_pointer_value().into() + self.bcx.build_global_string_ptr(value, "") } fn const_array(&mut self, ty: Self::Type, values: &[u64]) -> Self::Value { - let ty = ty.into_int_type(); let values = values.iter().map(|&v| ty.const_int(v, false)).collect::>(); let init = ty.const_array(&values); - let global = self.module().add_global(init.get_type(), None, ""); - global.set_initializer(&init); + let global = self.module().add_global(init.get_type(), ""); + global.set_initializer(init); global.set_constant(true); global.set_linkage(convert_linkage(evm2_jit_backend::Linkage::Private)); - global.set_unnamed_address(UnnamedAddress::Global); - global.as_pointer_value().into() + global.set_unnamed_address(LLVMUnnamedAddr::LLVMGlobalUnnamedAddr); + global.as_pointer_value() } fn nullptr(&mut self) -> Self::Value { - self.ty_ptr.const_null().into() + self.ty_ptr.const_null() } fn new_stack_slot_raw(&mut self, ty: Self::Type, name: &str) -> Self::StackSlot { // let ty = self.ty_i8.array_type(size); - // let ptr = self.bcx.build_alloca(ty, self.name(name)).unwrap(); - // ptr.as_instruction().unwrap().set_alignment(align).unwrap(); + // let ptr = self.bcx.build_alloca(ty, self.name(name)); + // ptr.as_instruction().unwrap().set_alignment(align); // ptr - self.bcx.build_alloca(ty, self.name(name)).unwrap() + self.bcx.build_alloca(ty, self.name(name)) } fn stack_load(&mut self, ty: Self::Type, slot: Self::StackSlot, name: &str) -> Self::Value { - self.load(ty, slot.into(), name) + self.load(ty, slot, name) } fn stack_store(&mut self, value: Self::Value, slot: Self::StackSlot) { - self.store(value, slot.into()); + self.store(value, slot); } fn stack_addr(&mut self, ty: Self::Type, slot: Self::StackSlot) -> Self::Value { let _ = ty; - slot.into() + slot } fn load(&mut self, ty: Self::Type, ptr: Self::Value, name: &str) -> Self::Value { - let value = self.bcx.build_load(ty, ptr.into_pointer_value(), self.name(name)).unwrap(); - if ty == self.ty_i256.into() { - self.current_block().unwrap().get_last_instruction().unwrap().set_alignment(8).unwrap(); + let value = self.bcx.build_load(ty, ptr, self.name(name)); + if ty == self.ty_i256 { + value.set_alignment(8); } value } @@ -1497,27 +1386,22 @@ impl Builder for EvmLlvmBuilder<'_> { align: usize, name: &str, ) -> Self::Value { - let value = self.bcx.build_load(ty, ptr.into_pointer_value(), self.name(name)).unwrap(); - self.current_block() - .unwrap() - .get_last_instruction() - .unwrap() - .set_alignment(align as u32) - .unwrap(); + let value = self.bcx.build_load(ty, ptr, self.name(name)); + value.set_alignment(align as u32); value } fn store(&mut self, value: Self::Value, ptr: Self::Value) -> Self::Inst { - let inst = self.bcx.build_store(ptr.into_pointer_value(), value).unwrap(); - if value.get_type() == self.ty_i256.into() { - inst.set_alignment(8).unwrap(); + let inst = self.bcx.build_store(ptr, value); + if value.get_type() == self.ty_i256 { + inst.set_alignment(8); } inst } fn store_aligned(&mut self, value: Self::Value, ptr: Self::Value, align: usize) { - let inst = self.bcx.build_store(ptr.into_pointer_value(), value).unwrap(); - inst.set_alignment(align as u32).unwrap(); + let inst = self.bcx.build_store(ptr, value); + inst.set_alignment(align as u32); } fn remove_inst(&mut self, inst: Self::Inst) { @@ -1531,21 +1415,19 @@ impl Builder for EvmLlvmBuilder<'_> { fn ret(&mut self, values: &[Self::Value]) { match values { [] => self.bcx.build_return(None), - [value] => self.bcx.build_return(Some(value)), + [value] => self.bcx.build_return(Some(*value)), values => self.bcx.build_aggregate_return(values), - } - .unwrap(); + }; } fn assume(&mut self, cond: Self::Value) { - self.assume_inner(cond); + let function = self + .get_or_add_function("llvm.assume", |this| this.ty_void.fn_type(&[this.ty_i1], false)); + self.bcx.build_call(function, &[cond], ""); } fn icmp(&mut self, cond: IntCC, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_compare(convert_intcc(cond), lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_compare(convert_intcc(cond), lhs, rhs, "") } fn icmp_imm(&mut self, cond: IntCC, lhs: Self::Value, rhs: i64) -> Self::Value { @@ -1554,15 +1436,15 @@ impl Builder for EvmLlvmBuilder<'_> { } fn is_null(&mut self, ptr: Self::Value) -> Self::Value { - self.bcx.build_is_null(ptr.into_pointer_value(), "").unwrap().into() + self.bcx.build_is_null(ptr, "") } fn is_not_null(&mut self, ptr: Self::Value) -> Self::Value { - self.bcx.build_is_not_null(ptr.into_pointer_value(), "").unwrap().into() + self.bcx.build_is_not_null(ptr, "") } fn br(&mut self, dest: Self::BasicBlock) { - self.bcx.build_unconditional_branch(dest).unwrap(); + self.bcx.build_unconditional_branch(dest); } fn brif( @@ -1571,7 +1453,7 @@ impl Builder for EvmLlvmBuilder<'_> { then_block: Self::BasicBlock, else_block: Self::BasicBlock, ) { - self.bcx.build_conditional_branch(cond.into_int_value(), then_block, else_block).unwrap(); + self.bcx.build_conditional_branch(cond, then_block, else_block); } fn brif_cold( @@ -1581,10 +1463,7 @@ impl Builder for EvmLlvmBuilder<'_> { else_block: Self::BasicBlock, then_is_cold: bool, ) { - let inst = self - .bcx - .build_conditional_branch(cond.into_int_value(), then_block, else_block) - .unwrap(); + let inst = self.bcx.build_conditional_branch(cond, then_block, else_block); let weights = if then_is_cold { [1, DEFAULT_WEIGHT] } else { [DEFAULT_WEIGHT, 1] }; self.set_branch_weights(inst, weights); } @@ -1596,10 +1475,10 @@ impl Builder for EvmLlvmBuilder<'_> { targets: &[(u64, Self::BasicBlock)], default_is_cold: bool, ) { - let ty = index.get_type().into_int_type(); + let ty = index.get_type(); let targets = targets.iter().map(|(v, b)| (ty.const_int(*v, false), *b)).collect::>(); - let inst = self.bcx.build_switch(index.into_int_value(), default, &targets).unwrap(); + let inst = self.bcx.build_switch(index, default, &targets); if default_is_cold { let weights = iter::once(1).chain(iter::repeat_n(DEFAULT_WEIGHT, targets.len())); self.set_branch_weights(inst, weights); @@ -1607,17 +1486,11 @@ impl Builder for EvmLlvmBuilder<'_> { } fn br_indirect(&mut self, address: Self::Value, destinations: &[Self::BasicBlock]) { - let _ = self.bcx.build_indirect_branch(address, destinations).unwrap(); + self.bcx.build_indirect_branch(address, destinations); } fn phi(&mut self, ty: Self::Type, incoming: &[(Self::Value, Self::BasicBlock)]) -> Self::Value { - let incoming = incoming - .iter() - .map(|(value, block)| (value as &dyn BasicValue<'_>, *block)) - .collect::>(); - let phi = self.bcx.build_phi(ty, "").unwrap(); - phi.add_incoming(&incoming); - phi.as_basic_value() + self.bcx.build_phi(ty, incoming, "") } fn select( @@ -1626,47 +1499,35 @@ impl Builder for EvmLlvmBuilder<'_> { then_value: Self::Value, else_value: Self::Value, ) -> Self::Value { - self.bcx.build_select(cond.into_int_value(), then_value, else_value, "").unwrap() + self.bcx.build_select(cond, then_value, else_value, "") } fn iadd(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_int_add(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_int_add(lhs, rhs, "") } fn isub(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_int_sub(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_int_sub(lhs, rhs, "") } fn imul(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_int_mul(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_int_mul(lhs, rhs, "") } fn udiv(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_unsigned_div(lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_unsigned_div(lhs, rhs, "") } fn sdiv(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_signed_div(lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_signed_div(lhs, rhs, "") } fn urem(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_unsigned_rem(lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_unsigned_rem(lhs, rhs, "") } fn srem(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_int_signed_rem(lhs.into_int_value(), rhs.into_int_value(), "") - .unwrap() - .into() + self.bcx.build_int_signed_rem(lhs, rhs, "") } fn iadd_imm(&mut self, lhs: Self::Value, rhs: i64) -> Self::Value { @@ -1726,19 +1587,19 @@ impl Builder for EvmLlvmBuilder<'_> { } fn bitor(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_or(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_or(lhs, rhs, "") } fn bitand(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_and(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_and(lhs, rhs, "") } fn bitxor(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_xor(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_xor(lhs, rhs, "") } fn bitnot(&mut self, value: Self::Value) -> Self::Value { - self.bcx.build_not(value.into_int_value(), "").unwrap().into() + self.bcx.build_not(value, "") } fn clz(&mut self, value: Self::Value) -> Self::Value { @@ -1773,40 +1634,31 @@ impl Builder for EvmLlvmBuilder<'_> { } fn ishl(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx.build_left_shift(lhs.into_int_value(), rhs.into_int_value(), "").unwrap().into() + self.bcx.build_left_shift(lhs, rhs, "") } fn ushr(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_right_shift(lhs.into_int_value(), rhs.into_int_value(), false, "") - .unwrap() - .into() + self.bcx.build_right_shift(lhs, rhs, false, "") } fn sshr(&mut self, lhs: Self::Value, rhs: Self::Value) -> Self::Value { - self.bcx - .build_right_shift(lhs.into_int_value(), rhs.into_int_value(), true, "") - .unwrap() - .into() + self.bcx.build_right_shift(lhs, rhs, true, "") } fn zext(&mut self, ty: Self::Type, value: Self::Value) -> Self::Value { - self.bcx.build_int_z_extend(value.into_int_value(), ty.into_int_type(), "").unwrap().into() + self.bcx.build_int_z_extend(value, ty, "") } fn sext(&mut self, ty: Self::Type, value: Self::Value) -> Self::Value { - self.bcx.build_int_s_extend(value.into_int_value(), ty.into_int_type(), "").unwrap().into() + self.bcx.build_int_s_extend(value, ty, "") } fn ireduce(&mut self, to: Self::Type, value: Self::Value) -> Self::Value { - self.bcx.build_int_truncate(value.into_int_value(), to.into_int_type(), "").unwrap().into() + self.bcx.build_int_truncate(value, to, "") } fn inttoptr(&mut self, value: Self::Value, ty: Self::Type) -> Self::Value { - self.bcx - .build_int_to_ptr(value.into_int_value(), ty.into_pointer_type(), "") - .unwrap() - .into() + self.bcx.build_int_to_ptr(value, ty, "") } fn gep( @@ -1816,17 +1668,7 @@ impl Builder for EvmLlvmBuilder<'_> { indexes: &[Self::Value], name: &str, ) -> Self::Value { - let indexes = indexes.iter().map(|idx| idx.into_int_value()).collect::>(); - unsafe { - self.bcx.build_in_bounds_gep( - elem_ty, - ptr.into_pointer_value(), - &indexes, - self.name(name), - ) - } - .unwrap() - .into() + unsafe { self.bcx.build_in_bounds_gep(elem_ty, ptr, indexes, self.name(name)) } } fn tail_call( @@ -1835,27 +1677,20 @@ impl Builder for EvmLlvmBuilder<'_> { args: &[Self::Value], tail_call: TailCallKind, ) -> Option { - let args = args.iter().copied().map(Into::into).collect::>(); - let callsite = self.bcx.build_call(function, &args, "").unwrap(); + let callsite = self.bcx.build_call(function, args, ""); if let Some(call_conv) = function_call_conv(function) { - unsafe { - inkwell::llvm_sys::core::LLVMSetInstructionCallConv( - callsite.as_value_ref(), - call_conv as _, - ); - } + callsite.set_call_conv(call_conv); } if tail_call != TailCallKind::None { callsite.set_tail_call_kind(convert_tail_call_kind(tail_call)); } - callsite.try_as_basic_value().basic() + callsite.value() } fn is_compile_time_known(&mut self, value: Self::Value) -> Option { let ty = value.get_type(); let name = format!("llvm.is.constant.{}", fmt_ty(ty)); - let f = - self.get_or_add_function(&name, |this| this.fn_type(Some(this.ty_i1.into()), &[ty])); + let f = self.get_or_add_function(&name, |this| this.fn_type(Some(this.ty_i1), &[ty])); Some(self.call(f, &[value]).unwrap()) } @@ -1864,12 +1699,12 @@ impl Builder for EvmLlvmBuilder<'_> { } fn memcpy_inline(&mut self, dst: Self::Value, src: Self::Value, len: i64) { - let len = self.iconst(self.ty_i64.into(), len); + let len = self.iconst(self.ty_i64, len); self.memcpy_inner(dst, src, len, true); } fn unreachable(&mut self) { - self.bcx.build_unreachable().unwrap(); + self.bcx.build_unreachable(); } fn get_or_build_function( @@ -1891,7 +1726,7 @@ impl Builder for EvmLlvmBuilder<'_> { cpp::set_dso_local(function); let prev_function = std::mem::replace(&mut self.function, function); - let entry = self.cx.append_basic_block(function, self.name("entry")); + let entry = self.cx().append_basic_block(function, self.name("entry")); self.bcx.position_at_end(entry); build(self); if let Some(before) = before { @@ -1913,9 +1748,8 @@ impl Builder for EvmLlvmBuilder<'_> { return function; } - let ty = self.cx.void_type().fn_type(&[self.ty_ptr.into()], true); - let function = - self.module().add_function(name, ty, Some(inkwell::module::Linkage::External)); + let ty = self.cx().void_type().fn_type(&[self.ty_ptr], true); + let function = self.module().add_function(name, ty, Some(LLVMLinkage::LLVMExternalLinkage)); cpp::set_dso_local(function); function } @@ -1946,18 +1780,15 @@ impl Builder for EvmLlvmBuilder<'_> { function: Self::Function, call_conv: CallConv, ) -> Self::Function { - let name = function.get_name().to_string_lossy(); + let name = function.get_name(); let stub_name = format!("{name}.stub"); if let Some(stub) = self.module().get_function(&stub_name) { return stub; } let func_ty = function.get_type(); - let stub = self.module().add_function( - &stub_name, - func_ty, - Some(inkwell::module::Linkage::Internal), - ); + let stub = + self.module().add_function(&stub_name, func_ty, Some(LLVMLinkage::LLVMInternalLinkage)); set_function_call_conv(stub, call_conv); cpp::set_dso_local(stub); self.add_function_attribute( @@ -1970,15 +1801,12 @@ impl Builder for EvmLlvmBuilder<'_> { let debug_location = self.debug_scope.and_then(|_| self.bcx.get_current_debug_location()); self.bcx.unset_current_debug_location(); - let entry = self.cx.append_basic_block(stub, self.name("stub")); + let entry = self.cx().append_basic_block(stub, self.name("stub")); self.bcx.position_at_end(entry); let args = (0..stub.count_params()).map(|i| stub.get_nth_param(i).unwrap()).collect::>(); - if let Some(value) = self.call(function, &args) { - self.bcx.build_return(Some(&value)).unwrap(); - } else { - self.bcx.build_return(None).unwrap(); - } + let value = self.call(function, &args); + self.bcx.build_return(value); if let Some(before) = before { self.bcx.position_at_end(before); @@ -2038,25 +1866,22 @@ fn build_pass_pipeline(with_licm: bool) -> String { } fn init() -> Result<()> { - let mut init_result = Ok(()); - static INIT: Once = Once::new(); - INIT.call_once(|| init_result = init_()); - init_result + static INIT: OnceLock> = OnceLock::new(); + match INIT.get_or_init(|| init_().map_err(|e| format!("{e:#}"))) { + Ok(()) => Ok(()), + Err(e) => Err(eyre::eyre!("{e}")), + } } fn init_() -> Result<()> { - // TODO: This also reports "PLEASE submit a bug report to..." when the segfault is - // outside of LLVM. - // enable_llvm_pretty_stack_trace(); - extern "C" fn report_fatal_error(msg: *const std::ffi::c_char) { - let msg_cstr = unsafe { std::ffi::CStr::from_ptr(msg) }; + let msg_cstr = unsafe { CStr::from_ptr(msg) }; let msg = msg_cstr.to_string_lossy(); error!(target: "llvm", "LLVM fatal error: {msg}"); } unsafe { - install_fatal_error_handler(report_fatal_error); + LLVMInstallFatalErrorHandler(Some(report_fatal_error)); } // Collect extra LLVM args from `EVM2_JIT_LLVM_ARGS` env var (space-separated). @@ -2113,36 +1938,22 @@ fn init_() -> Result<()> { // functions alias analysis lets EVM memory and builtin calls alias the stack, and LICM // cannot promote loop-carried stack slots to registers. c"-capture-tracking-max-uses-to-explore=1000000".as_ptr(), + // Emit a trap for `unreachable`, as rustc does, so that a builtin call that is expected + // to leave through `evm2_jit_exit` but returns faults instead of running into the next + // block. + c"-trap-unreachable".as_ptr(), ]; args.extend(extra.iter().map(|s| s.as_ptr())); - unsafe { - inkwell::llvm_sys::support::LLVMParseCommandLineOptions( - args.len() as i32, - args.as_ptr(), - std::ptr::null(), - ) - } + unsafe { LLVMParseCommandLineOptions(args.len() as i32, args.as_ptr(), std::ptr::null()) } // TODO: https://github.com/llvm/llvm-project/issues/235937 unsafe { cpp::evm2_jit_llvm_expand_ir_insts_after_lsr() }; - let config = InitializationConfig { - asm_parser: false, - asm_printer: true, - base: true, - disassembler: true, - info: true, - machine_code: true, - }; - Target::initialize_native(&config).map_err(|e| eyre::eyre!(e))?; + Target::initialize_native().map_err(|e| eyre::eyre!(e))?; Ok(()) } -fn create_module<'ctx>( - cx: &'ctx Context, - machine: &TargetMachine, - aot: bool, -) -> Result> { +fn create_module(cx: &Context, machine: &TargetMachine, aot: bool) -> Result { let module_name = "evm"; let module = cx.create_module(module_name); module.set_source_file_name(module_name); @@ -2151,41 +1962,41 @@ fn create_module<'ctx>( if aot { module.add_basic_value_flag( "PIC Level", - FlagBehavior::Error, // TODO: Min + LLVMModuleFlagBehavior::LLVMModuleFlagBehaviorError, // TODO: Min cx.i32_type().const_int(2, false), ); module.add_basic_value_flag( "RtLibUseGOT", - FlagBehavior::Warning, + LLVMModuleFlagBehavior::LLVMModuleFlagBehaviorWarning, cx.i32_type().const_int(1, false), ); } Ok(module) } -const fn convert_intcc(cond: IntCC) -> IntPredicate { +const fn convert_intcc(cond: IntCC) -> LLVMIntPredicate { match cond { - IntCC::Equal => IntPredicate::EQ, - IntCC::NotEqual => IntPredicate::NE, - IntCC::SignedLessThan => IntPredicate::SLT, - IntCC::SignedGreaterThanOrEqual => IntPredicate::SGE, - IntCC::SignedGreaterThan => IntPredicate::SGT, - IntCC::SignedLessThanOrEqual => IntPredicate::SLE, - IntCC::UnsignedLessThan => IntPredicate::ULT, - IntCC::UnsignedGreaterThanOrEqual => IntPredicate::UGE, - IntCC::UnsignedGreaterThan => IntPredicate::UGT, - IntCC::UnsignedLessThanOrEqual => IntPredicate::ULE, + IntCC::Equal => LLVMIntPredicate::LLVMIntEQ, + IntCC::NotEqual => LLVMIntPredicate::LLVMIntNE, + IntCC::SignedLessThan => LLVMIntPredicate::LLVMIntSLT, + IntCC::SignedGreaterThanOrEqual => LLVMIntPredicate::LLVMIntSGE, + IntCC::SignedGreaterThan => LLVMIntPredicate::LLVMIntSGT, + IntCC::SignedLessThanOrEqual => LLVMIntPredicate::LLVMIntSLE, + IntCC::UnsignedLessThan => LLVMIntPredicate::LLVMIntULT, + IntCC::UnsignedGreaterThanOrEqual => LLVMIntPredicate::LLVMIntUGE, + IntCC::UnsignedGreaterThan => LLVMIntPredicate::LLVMIntUGT, + IntCC::UnsignedLessThanOrEqual => LLVMIntPredicate::LLVMIntULE, } } /// Maps to the codegen level of the target machine, used for AOT and dumps. `Less` skips the /// post-RA scheduler on CPUs that enable it (znver), saving about 5% of compile instructions for /// no runtime gain. The JIT compiler in `lib.cpp` always uses `Less`, whatever the level. -const fn convert_opt_level(level: OptimizationLevel) -> inkwell::OptimizationLevel { +const fn convert_opt_level(level: OptimizationLevel) -> LLVMCodeGenOptLevel { match level { - OptimizationLevel::None => inkwell::OptimizationLevel::None, + OptimizationLevel::None => LLVMCodeGenOptLevel::LLVMCodeGenLevelNone, OptimizationLevel::Less | OptimizationLevel::Default | OptimizationLevel::Aggressive => { - inkwell::OptimizationLevel::Less + LLVMCodeGenOptLevel::LLVMCodeGenLevelLess } } } @@ -2196,7 +2007,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute enum AttrValue<'a> { String(&'a str), Enum(u64), - Type(AnyTypeEnum<'a>), + Type(Type), } let cpu; @@ -2207,7 +2018,7 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::NoSync => ("nosync", AttrValue::Enum(0)), OurAttr::NoUnwind => ("nounwind", AttrValue::Enum(0)), OurAttr::NonLazyBind => ("nonlazybind", AttrValue::Enum(0)), - OurAttr::UWTable => ("uwtable", AttrValue::Enum(2)), + OurAttr::UWTable => return cpp::create_async_uwtable_attr(bcx.cx()), OurAttr::AllFramePointers => ("frame-pointer", AttrValue::String("all")), OurAttr::NativeTargetCpu => ( "target-cpu", @@ -2222,41 +2033,39 @@ fn convert_attribute(bcx: &EvmLlvmBuilder<'_>, attr: evm2_jit_backend::Attribute OurAttr::AlwaysInline => ("alwaysinline", AttrValue::Enum(0)), OurAttr::NoInline => ("noinline", AttrValue::Enum(0)), OurAttr::Speculatable => ("speculatable", AttrValue::Enum(0)), + OurAttr::InlineStackProbes => ("probe-stack", AttrValue::String("inline-asm")), OurAttr::NoAlias => ("noalias", AttrValue::Enum(0)), - OurAttr::NoCapture => ("captures", AttrValue::Enum(0)), // captures(none) - no capture + OurAttr::NoCapture => return cpp::create_no_captures_attr(bcx.cx()), OurAttr::NoUndef => ("noundef", AttrValue::Enum(0)), OurAttr::Align(n) => ("align", AttrValue::Enum(n)), OurAttr::NonNull => ("nonnull", AttrValue::Enum(0)), OurAttr::Dereferenceable(n) => ("dereferenceable", AttrValue::Enum(n)), - OurAttr::SRet(n) => { - ("sret", AttrValue::Type(bcx.type_array(bcx.ty_i8.into(), n as _).as_any_type_enum())) - } + OurAttr::SRet(n) => ("sret", AttrValue::Type(bcx.type_array(bcx.ty_i8, n as _))), OurAttr::ReadNone => ("readnone", AttrValue::Enum(0)), OurAttr::ReadOnly => ("readonly", AttrValue::Enum(0)), OurAttr::WriteOnly => ("writeonly", AttrValue::Enum(0)), OurAttr::Writable => ("writable", AttrValue::Enum(0)), - // memory(argmem: readwrite) = ModRef(3) << ArgMem(0) = 3. - OurAttr::ArgMemOnly => ("memory", AttrValue::Enum(3)), + OurAttr::ArgMemOnly => return cpp::create_arg_mem_only_attr(bcx.cx()), // `u64::MAX` marks all reachable memory as dead; `0` would mark zero bytes. OurAttr::DeadOnReturn => ("dead_on_return", AttrValue::Enum(u64::MAX)), OurAttr::DeadOnUnwind => ("dead_on_unwind", AttrValue::Enum(0)), OurAttr::Initializes(size) => { - return cpp::create_initializes_attr(bcx.cx, 0, size as i64); + return cpp::create_initializes_attr(bcx.cx(), 0, size as i64); } attr => unimplemented!("llvm attribute conversion: {attr:?}"), }; match value { - AttrValue::String(value) => bcx.cx.create_string_attribute(key, value), + AttrValue::String(value) => bcx.cx().create_string_attribute(key, value), AttrValue::Enum(value) => { let id = Attribute::get_named_enum_kind_id(key); - bcx.cx.create_enum_attribute(id, value) + bcx.cx().create_enum_attribute(id, value) } AttrValue::Type(ty) => { let id = Attribute::get_named_enum_kind_id(key); - bcx.cx.create_type_attribute(id, ty) + bcx.cx().create_type_attribute(id, ty) } } } @@ -2269,48 +2078,98 @@ const fn convert_attribute_loc(loc: evm2_jit_backend::FunctionAttributeLocation) } } -fn function_call_conv(function: FunctionValue<'_>) -> Option { - let call_conv = - unsafe { inkwell::llvm_sys::core::LLVMGetFunctionCallConv(function.as_value_ref()) }; - (call_conv != inkwell::llvm_sys::LLVMCallConv::LLVMCCallConv as u32).then_some(call_conv) +fn function_call_conv(function: Function) -> Option { + let call_conv = function.get_call_conv(); + (call_conv != LLVMCallConv::LLVMCCallConv as u32).then_some(call_conv) } -fn set_function_call_conv(function: FunctionValue<'_>, call_conv: CallConv) { +fn set_function_call_conv(function: Function, call_conv: CallConv) { let Some(call_conv) = convert_call_conv(call_conv) else { return }; - unsafe { - inkwell::llvm_sys::core::LLVMSetFunctionCallConv(function.as_value_ref(), call_conv as _); - } + function.set_call_conv(call_conv as _); } -const fn convert_call_conv(call_conv: CallConv) -> Option { +const fn convert_call_conv(call_conv: CallConv) -> Option { match call_conv { CallConv::Default => None, - CallConv::Cold => Some(inkwell::llvm_sys::LLVMCallConv::LLVMPreserveMostCallConv), + CallConv::Cold => Some(LLVMCallConv::LLVMPreserveMostCallConv), } } -const fn convert_linkage(linkage: evm2_jit_backend::Linkage) -> inkwell::module::Linkage { +const fn convert_linkage(linkage: evm2_jit_backend::Linkage) -> LLVMLinkage { match linkage { - evm2_jit_backend::Linkage::Public => inkwell::module::Linkage::External, - evm2_jit_backend::Linkage::Import => inkwell::module::Linkage::External, - evm2_jit_backend::Linkage::Private => inkwell::module::Linkage::Private, + evm2_jit_backend::Linkage::Public => LLVMLinkage::LLVMExternalLinkage, + evm2_jit_backend::Linkage::Import => LLVMLinkage::LLVMExternalLinkage, + evm2_jit_backend::Linkage::Private => LLVMLinkage::LLVMPrivateLinkage, } } -const fn convert_tail_call_kind(kind: TailCallKind) -> inkwell::llvm_sys::LLVMTailCallKind { +const fn convert_tail_call_kind(kind: TailCallKind) -> LLVMTailCallKind { match kind { - TailCallKind::None => inkwell::llvm_sys::LLVMTailCallKind::LLVMTailCallKindNone, - TailCallKind::Tail => inkwell::llvm_sys::LLVMTailCallKind::LLVMTailCallKindTail, - TailCallKind::MustTail => inkwell::llvm_sys::LLVMTailCallKind::LLVMTailCallKindMustTail, - TailCallKind::NoTail => inkwell::llvm_sys::LLVMTailCallKind::LLVMTailCallKindNoTail, + TailCallKind::None => LLVMTailCallKind::LLVMTailCallKindNone, + TailCallKind::Tail => LLVMTailCallKind::LLVMTailCallKindTail, + TailCallKind::MustTail => LLVMTailCallKind::LLVMTailCallKindMustTail, + TailCallKind::NoTail => LLVMTailCallKind::LLVMTailCallKindNoTail, } } // No `#[track_caller]` because `map_err` doesn't propagate it. -fn error_msg(msg: inkwell::support::LLVMString) -> evm2_jit_backend::Error { +fn error_msg(msg: LLVMString) -> evm2_jit_backend::Error { evm2_jit_backend::Error::msg(msg.to_string_lossy().trim_end().to_string()) } -fn fmt_ty(ty: BasicTypeEnum<'_>) -> impl std::fmt::Display { +fn fmt_ty(ty: Type) -> impl std::fmt::Display { ty.print_to_string().to_str().unwrap().trim_matches('"').to_string() } + +#[cfg(test)] +mod tests { + use super::*; + use llvm_sys::{LLVMInlineAsmDialect, core::*}; + + /// An error diagnostic LLVM raises while emitting code must fail `write_object`, and must not + /// leak into the next operation. + #[test] + fn diagnostic_error_fails_write_object() { + const ARGS: usize = 64; + + let mut backend = EvmLlvmBackend::new(true).unwrap(); + unsafe { + let cx = backend.cx().raw(); + let void = LLVMVoidTypeInContext(cx); + let i32 = LLVMInt32TypeInContext(cx); + let function_ty = LLVMFunctionType(void, std::ptr::null_mut(), 0, 0); + let function = LLVMAddFunction(backend.module.as_mut_ptr(), c"f".as_ptr(), function_ty); + let entry = LLVMAppendBasicBlockInContext(cx, function, c"entry".as_ptr()); + let builder = LLVMCreateBuilderInContext(cx); + LLVMPositionBuilderAtEnd(builder, entry); + + // More register inputs than the target has registers. + let mut param_tys = [i32; ARGS]; + let asm_ty = LLVMFunctionType(void, param_tys.as_mut_ptr(), ARGS as u32, 0); + let constraints = vec!["r"; ARGS].join(","); + let asm = LLVMGetInlineAsm( + asm_ty, + c"".as_ptr().cast_mut(), + 0, + constraints.as_ptr().cast::().cast_mut(), + constraints.len(), + 1, + 0, + LLVMInlineAsmDialect::LLVMInlineAsmDialectATT, + 0, + ); + let mut args = [LLVMConstInt(i32, 1, 0); ARGS]; + LLVMBuildCall2(builder, asm_ty, asm, args.as_mut_ptr(), ARGS as u32, c"".as_ptr()); + LLVMBuildRetVoid(builder); + LLVMDisposeBuilder(builder); + } + + let err = backend.write_object(Vec::new()).unwrap_err(); + assert_eq!( + err.to_string(), + "LLVM error: inline assembly requires more registers than available" + ); + + backend.dh.check(Ok(())).unwrap(); + } +} diff --git a/crates/jit/llvm/src/orc.rs b/crates/jit/llvm/src/orc.rs index 5d65dcfce..67d480436 100644 --- a/crates/jit/llvm/src/orc.rs +++ b/crates/jit/llvm/src/orc.rs @@ -1,4 +1,4 @@ -//! Safe wrappers for LLVM ORC and LLJIT, which are not yet implemented in [`inkwell`]. +//! Safe wrappers for LLVM ORC and LLJIT. //! //! Header files implemented: //! - `LLJIT.h` @@ -13,21 +13,15 @@ #![allow(clippy::missing_safety_doc)] #![allow(clippy::too_many_arguments)] -use crate::llvm_string; -use inkwell::{ - context::Context, - llvm_sys::{ - core::{ - LLVMContextCreate, LLVMCreateMemoryBufferWithMemoryRangeCopy, - LLVMModuleCreateWithNameInContext, - }, - error::*, - orc2::{lljit::*, *}, - prelude::*, +use crate::wrap::{Context, LLVMString, Module, TargetMachine}; +use llvm_sys::{ + core::{ + LLVMContextCreate, LLVMCreateMemoryBufferWithMemoryRangeCopy, LLVMDisposeMemoryBuffer, + LLVMGetBufferSize, LLVMGetBufferStart, LLVMModuleCreateWithNameInContext, }, - module::Module, - support::LLVMString, - targets::TargetMachine, + error::*, + orc2::{lljit::*, *}, + prelude::*, }; use std::{ ffi::{CStr, CString, c_char, c_void}, @@ -82,7 +76,7 @@ impl ThreadSafeContext { } /// Create a ThreadSafeModule wrapper around the given LLVM module. - pub fn create_module<'ctx>(&'ctx self, module: Module<'ctx>) -> ThreadSafeModule { + pub fn create_module(&self, module: Module) -> ThreadSafeModule { ThreadSafeModule::create_in_context(module, self) } } @@ -121,9 +115,8 @@ impl ThreadSafeModule { } /// Create a ThreadSafeModule wrapper around the given LLVM module. - pub fn create_in_context<'ctx>(module: Module<'ctx>, ctx: &'ctx ThreadSafeContext) -> Self { - let module = mem::ManuallyDrop::new(module); - let ptr = unsafe { LLVMOrcCreateNewThreadSafeModule(module.as_mut_ptr(), ctx.as_inner()) }; + pub fn create_in_context(module: Module, ctx: &ThreadSafeContext) -> Self { + let ptr = unsafe { LLVMOrcCreateNewThreadSafeModule(module.into_raw(), ctx.as_inner()) }; Self { ptr } } @@ -140,18 +133,18 @@ impl ThreadSafeModule { /// Runs the given closure with the module. /// /// This implicitly locks the associated context. - pub fn with_module<'tsm>( - &'tsm self, - mut f: impl FnMut(&Module<'tsm>) -> Result<(), String>, + pub fn with_module( + &self, + mut f: impl FnMut(&Module) -> Result<(), String>, ) -> Result<(), LLVMString> { extern "C" fn shim(ctx: *mut c_void, m: LLVMModuleRef) -> LLVMErrorRef { - let f = ctx.cast::<&mut dyn FnMut(&Module<'_>) -> Result<(), String>>(); + let f = ctx.cast::<&mut dyn FnMut(&Module) -> Result<(), String>>(); let m = mem::ManuallyDrop::new(unsafe { Module::new(m) }); let res = std::panic::catch_unwind(AssertUnwindSafe(|| unsafe { (*f)(&m) })); cvt_cb_res(res) } - let mut f = &mut f as &mut dyn FnMut(&Module<'tsm>) -> Result<(), String>; + let mut f = &mut f as &mut dyn FnMut(&Module) -> Result<(), String>; let ctx = &mut f as *mut _ as *mut c_void; cvt(unsafe { LLVMOrcThreadSafeModuleWithModuleDo(self.as_inner(), shim, ctx) }) } @@ -300,7 +293,7 @@ impl SymbolFlags { /// Set the `Exported` flag. pub const fn set_exported(&mut self) { - self.generic |= LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsExported as u8; + self.set_generic(LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsExported); } /// Set the `Exported` flag. @@ -316,7 +309,7 @@ impl SymbolFlags { /// Set the `Weak` flag. pub const fn set_weak(&mut self) { - self.generic |= LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsWeak as u8; + self.set_generic(LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsWeak); } /// Set the `Weak` flag. @@ -332,7 +325,7 @@ impl SymbolFlags { /// Set the `Callable` flag. pub const fn set_callable(&mut self) { - self.generic |= LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsCallable as u8; + self.set_generic(LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsCallable); } /// Set the `Callable` flag. @@ -348,9 +341,9 @@ impl SymbolFlags { /// Set the `MaterializationSideEffectsOnly` flag. pub const fn set_materialization_side_effects_only(&mut self) { - self.generic |= - LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly - as u8; + self.set_generic( + LLVMJITSymbolGenericFlags::LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly, + ); } /// Set the `MaterializationSideEffectsOnly` flag. @@ -1207,7 +1200,9 @@ impl JITTargetMachineBuilder { /// Returns the target triple for the given JITTargetMachineBuilder as a string. pub fn get_target_triple(&self) -> LLVMString { - unsafe { llvm_string(LLVMOrcJITTargetMachineBuilderGetTargetTriple(self.as_inner())) } + unsafe { + LLVMString::from_raw(LLVMOrcJITTargetMachineBuilderGetTargetTriple(self.as_inner())) + } } /// Sets the target triple for the given JITTargetMachineBuilder to the given string. @@ -1345,7 +1340,7 @@ impl LLJIT { /// Creates a new ORC JIT, auto-detecting the host target. pub fn new() -> Result { - LLJITBuilder::new().build() + Self::builder().build() } /// Wraps a raw pointer. @@ -1575,10 +1570,6 @@ impl ObjectTransformLayerRef { /// Returning `Err` aborts materialization. pub fn set_transform(&self, f: ObjTransformFn) { extern "C" fn shim(ctx: *mut c_void, obj_in_out: *mut LLVMMemoryBufferRef) -> LLVMErrorRef { - use inkwell::llvm_sys::core::{ - LLVMCreateMemoryBufferWithMemoryRangeCopy, LLVMDisposeMemoryBuffer, - LLVMGetBufferSize, LLVMGetBufferStart, - }; let f: ObjTransformFn = unsafe { mem::transmute(ctx) }; let buf = unsafe { *obj_in_out }; let data = unsafe { @@ -1707,7 +1698,11 @@ unsafe impl Send for LLJITBuilder {} /// Converts an `LLVMErrorRef` to a `Result`. pub(crate) fn cvt(ptr: LLVMErrorRef) -> Result<(), LLVMString> { - if ptr.is_null() { Ok(()) } else { Err(unsafe { llvm_string(LLVMGetErrorMessage(ptr)) }) } + if ptr.is_null() { + Ok(()) + } else { + Err(unsafe { LLVMString::from_raw(LLVMGetErrorMessage(ptr)) }) + } } fn cvt_cb_res(res: Result, Box>) -> LLVMErrorRef { @@ -1796,13 +1791,17 @@ impl Drop for ManuallyDropElements { #[cfg(test)] mod tests { use super::*; - use inkwell::{passes::PassBuilderOptions, targets::Target}; + use crate::wrap::{PassBuilderOptions, Target}; + use llvm_sys::{ + LLVMIntPredicate, LLVMLinkage, + core::LLVMGetModuleContext, + target_machine::{LLVMCodeGenOptLevel, LLVMCodeModel, LLVMRelocMode}, + }; + use std::mem::ManuallyDrop; #[test] fn e2e_with_tsc() { - use mem::ManuallyDrop; - - Target::initialize_native(&Default::default()).unwrap(); + Target::initialize_native().unwrap(); let triple = TargetMachine::get_default_triple(); let cpu = TargetMachine::get_host_cpu_name(); let features = TargetMachine::get_host_cpu_features(); @@ -1819,13 +1818,13 @@ mod tests { &triple, &cpu.to_string_lossy(), &features.to_string_lossy(), - inkwell::OptimizationLevel::None, - inkwell::targets::RelocMode::Static, - inkwell::targets::CodeModel::JITDefault, + LLVMCodeGenOptLevel::LLVMCodeGenLevelNone, + LLVMRelocMode::LLVMRelocStatic, + LLVMCodeModel::LLVMCodeModelJITDefault, ) .unwrap(); - let jit = LLJITBuilder::new().build().unwrap(); + let jit = LLJIT::new().unwrap(); // Build module. let m = cx.create_module("test"); @@ -1839,9 +1838,8 @@ mod tests { extern "C" fn my_external(x: i64) -> i64 { x + 100 } - let ext_ty = cx.i64_type().fn_type(&[cx.i64_type().into()], false); - let _ext_fn = - m.add_function("my_external", ext_ty, Some(inkwell::module::Linkage::External)); + let ext_ty = cx.i64_type().fn_type(&[cx.i64_type()], false); + let _ext_fn = m.add_function("my_external", ext_ty, Some(LLVMLinkage::LLVMExternalLinkage)); let ext_name = CString::new("my_external").unwrap(); let sym = SymbolMapPair::new( jit.mangle_and_intern(&ext_name), @@ -1856,10 +1854,10 @@ mod tests { .unwrap(); let ty = cx.i64_type().fn_type(&[], false); - let f = m.add_function(fn_name, ty, Some(inkwell::module::Linkage::External)); + let f = m.add_function(fn_name, ty, Some(LLVMLinkage::LLVMExternalLinkage)); let bb = cx.append_basic_block(f, "entry"); bcx.position_at_end(bb); - bcx.build_return(Some(&cx.i64_type().const_int(142, false))).unwrap(); + bcx.build_return(Some(cx.i64_type().const_int(142, false))); eprintln!("module:\n{}", m.print_to_string().to_string_lossy()); m.verify().unwrap(); @@ -1892,9 +1890,7 @@ mod tests { #[test] fn lookup_in_custom_jd() { - use mem::ManuallyDrop; - - Target::initialize_native(&Default::default()).unwrap(); + Target::initialize_native().unwrap(); let triple = TargetMachine::get_default_triple(); let cpu = TargetMachine::get_host_cpu_name(); let features = TargetMachine::get_host_cpu_features(); @@ -1911,13 +1907,13 @@ mod tests { &triple, &cpu.to_string_lossy(), &features.to_string_lossy(), - inkwell::OptimizationLevel::None, - inkwell::targets::RelocMode::Static, - inkwell::targets::CodeModel::JITDefault, + LLVMCodeGenOptLevel::LLVMCodeGenLevelNone, + LLVMRelocMode::LLVMRelocStatic, + LLVMCodeModel::LLVMCodeModelJITDefault, ) .unwrap(); - let jit = LLJITBuilder::new().build().unwrap(); + let jit = LLJIT::new().unwrap(); // Create two custom JITDylibs with functions returning different values. let jd_a = jit.get_execution_session().create_bare_jit_dylib(c"test_a"); @@ -1931,17 +1927,13 @@ mod tests { let fn_name = "test_fn"; let bcx = cx.create_builder(); let ty = cx.i64_type().fn_type(&[], false); - let f = m.add_function(fn_name, ty, Some(inkwell::module::Linkage::External)); + let f = m.add_function(fn_name, ty, Some(LLVMLinkage::LLVMExternalLinkage)); let bb = cx.append_basic_block(f, "entry"); bcx.position_at_end(bb); - bcx.build_return(Some(&cx.i64_type().const_int(value, false))).unwrap(); + bcx.build_return(Some(cx.i64_type().const_int(value, false))); m.verify().unwrap(); - let module = ManuallyDrop::new(m); - let tsm = unsafe { - ThreadSafeModule::create_in_context(Module::new(module.as_mut_ptr()), &tscx) - }; - jit.add_module_with_dylib(tsm, unsafe { + jit.add_module_with_dylib(tscx.create_module(m), unsafe { JITDylibRef::from_inner_unchecked(jd.as_inner()) }) .unwrap(); @@ -1967,28 +1959,29 @@ mod tests { let tsm = ThreadSafeModule::create("test"); let fn_name = "my_fn"; tsm.with_module(|m| { - let cx = m.get_context(); + let cx = mem::ManuallyDrop::new(unsafe { + Context::new(LLVMGetModuleContext(m.as_mut_ptr())) + }); let bcx = cx.create_builder(); let ty = cx.i64_type().fn_type(&[], false); - let f = m.add_function(fn_name, ty, Some(inkwell::module::Linkage::External)); + let f = m.add_function(fn_name, ty, Some(LLVMLinkage::LLVMExternalLinkage)); let bb = cx.append_basic_block(f, "entry"); bcx.position_at_end(bb); bcx.build_int_compare( - inkwell::IntPredicate::EQ, - cx.i64_type().const_zero(), - cx.i64_type().const_all_ones(), + LLVMIntPredicate::LLVMIntEQ, + cx.i64_type().const_null(), + cx.i64_type().const_int(u64::MAX, false), "a", - ) - .unwrap(); - bcx.build_return(Some(&cx.i64_type().const_int(69, false))).unwrap(); + ); + bcx.build_return(Some(cx.i64_type().const_int(69, false))); eprintln!("--before--"); eprintln!("{}", m.print_to_string().to_string_lossy()); m.verify().map_err(|e| e.to_string())?; - Target::initialize_native(&Default::default()).unwrap(); + Target::initialize_native().unwrap(); let triple = TargetMachine::get_default_triple(); let cpu = TargetMachine::get_host_cpu_name(); let features = TargetMachine::get_host_cpu_features(); @@ -1998,13 +1991,13 @@ mod tests { &triple, &cpu.to_string_lossy(), &features.to_string_lossy(), - Default::default(), - inkwell::targets::RelocMode::Default, - inkwell::targets::CodeModel::Default, + LLVMCodeGenOptLevel::LLVMCodeGenLevelDefault, + LLVMRelocMode::LLVMRelocDefault, + LLVMCodeModel::LLVMCodeModelDefault, ) .ok_or_else(|| String::from("failed to create target machine"))?; - m.run_passes("default", &machine, PassBuilderOptions::create()).unwrap(); + m.run_passes("default", &machine, &PassBuilderOptions::create()).unwrap(); eprintln!("--after--"); eprintln!("{}", m.print_to_string().to_string_lossy()); diff --git a/crates/jit/llvm/src/utils.rs b/crates/jit/llvm/src/utils.rs deleted file mode 100644 index 59c71c8d2..000000000 --- a/crates/jit/llvm/src/utils.rs +++ /dev/null @@ -1,7 +0,0 @@ -use inkwell::support::LLVMString; -use std::ffi::c_char; - -pub(crate) unsafe fn llvm_string(ptr: *const c_char) -> LLVMString { - // `LLVMString::new` is private - unsafe { std::mem::transmute(ptr) } -} diff --git a/crates/jit/llvm/src/wrap/di.rs b/crates/jit/llvm/src/wrap/di.rs new file mode 100644 index 000000000..3b5fc50a8 --- /dev/null +++ b/crates/jit/llvm/src/wrap/di.rs @@ -0,0 +1,161 @@ +//! Debug info. + +use super::{Context, Module}; +use llvm_sys::{ + debuginfo::*, + prelude::{LLVMDIBuilderRef, LLVMMetadataRef}, +}; +use std::ptr; + +/// Returns the debug info metadata version LLVM emits, for the `Debug Info Version` module flag. +pub(crate) fn debug_metadata_version() -> u32 { + unsafe { LLVMDebugMetadataVersion() } +} + +/// An LLVM metadata node. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub(crate) struct Metadata(LLVMMetadataRef); + +impl Metadata { + pub(crate) const fn new(raw: LLVMMetadataRef) -> Self { + Self(raw) + } + + pub(crate) const fn raw(self) -> LLVMMetadataRef { + self.0 + } +} + +/// A debug location, attached to the instructions a [`Builder`](super::Builder) creates. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub(crate) struct DILocation(LLVMMetadataRef); + +impl DILocation { + pub(crate) fn new(raw: LLVMMetadataRef) -> Option { + (!raw.is_null()).then_some(Self(raw)) + } + + pub(crate) const fn raw(self) -> LLVMMetadataRef { + self.0 + } +} + +/// A compile unit and the file it describes. +#[derive(Clone, Copy, Debug)] +pub(crate) struct DICompileUnit { + unit: LLVMMetadataRef, + file: LLVMMetadataRef, +} + +/// An owned debug info builder. +#[derive(Debug)] +pub(crate) struct DebugInfoBuilder { + raw: LLVMDIBuilderRef, +} + +impl DebugInfoBuilder { + /// Creates a builder for `module` and a C compile unit for the file `filename` in `directory`. + pub(crate) fn new( + module: &Module, + filename: &str, + directory: &str, + producer: &str, + is_optimized: bool, + flags: &str, + ) -> (Self, DICompileUnit) { + unsafe { + let builder = Self { raw: LLVMCreateDIBuilder(module.as_mut_ptr()) }; + let file = LLVMDIBuilderCreateFile( + builder.raw, + filename.as_ptr().cast(), + filename.len(), + directory.as_ptr().cast(), + directory.len(), + ); + let unit = LLVMDIBuilderCreateCompileUnit( + builder.raw, + LLVMDWARFSourceLanguage::LLVMDWARFSourceLanguageC, + file, + producer.as_ptr().cast(), + producer.len(), + is_optimized as _, + flags.as_ptr().cast(), + flags.len(), + 0, + c"".as_ptr(), + 0, + LLVMDWARFEmissionKind::LLVMDWARFEmissionKindFull, + 0, + false as _, + false as _, + c"".as_ptr(), + 0, + c"".as_ptr(), + 0, + ); + (builder, DICompileUnit { unit, file }) + } + } + + /// Describes a public, local function without parameters and line information. + pub(crate) fn create_subprogram( + &self, + unit: DICompileUnit, + name: &str, + is_optimized: bool, + ) -> Metadata { + unsafe { + // A null return type means `void`. + let mut types = [ptr::null_mut()]; + let ty = LLVMDIBuilderCreateSubroutineType( + self.raw, + unit.file, + types.as_mut_ptr(), + types.len() as u32, + LLVMDIFlagPublic, + ); + Metadata(LLVMDIBuilderCreateFunction( + self.raw, + unit.unit, + name.as_ptr().cast(), + name.len(), + name.as_ptr().cast(), + name.len(), + unit.file, + 0, + ty, + true as _, + true as _, + 0, + LLVMDIFlagPublic, + is_optimized as _, + )) + } + } + + /// Creates a debug location in `scope`. + pub(crate) fn create_debug_location( + &self, + cx: &Context, + line: u32, + column: u32, + scope: Metadata, + ) -> DILocation { + DILocation(unsafe { + LLVMDIBuilderCreateDebugLocation(cx.raw(), line, column, scope.raw(), ptr::null_mut()) + }) + } + + /// Resolves forward references. Call before emitting the module. + pub(crate) fn finalize(&self) { + unsafe { LLVMDIBuilderFinalize(self.raw) } + } +} + +impl Drop for DebugInfoBuilder { + fn drop(&mut self) { + self.finalize(); + unsafe { LLVMDisposeDIBuilder(self.raw) } + } +} diff --git a/crates/jit/llvm/src/wrap/ir.rs b/crates/jit/llvm/src/wrap/ir.rs new file mode 100644 index 000000000..54ad1b5e1 --- /dev/null +++ b/crates/jit/llvm/src/wrap/ir.rs @@ -0,0 +1,1040 @@ +//! LLVM IR: contexts, modules, types, values, and the instruction builder. + +use super::{DILocation, LLVMString, Metadata, TargetData, TargetMachine, to_c_str}; +use llvm_sys::{ + LLVMAttributeFunctionIndex, LLVMIntPredicate, LLVMLinkage, LLVMModuleFlagBehavior, + LLVMTailCallKind, LLVMTypeKind, LLVMUnnamedAddr, + analysis::{LLVMVerifierFailureAction, LLVMVerifyModule}, + core::*, + debuginfo::LLVMSetSubprogram, + error::LLVMGetErrorMessage, + prelude::*, + target::LLVMIntPtrTypeInContext, + transforms::pass_builder::{ + LLVMCreatePassBuilderOptions, LLVMDisposePassBuilderOptions, LLVMPassBuilderOptionsRef, + LLVMRunPasses, + }, +}; +use std::{ffi::CStr, fmt, mem::ManuallyDrop, path::Path, ptr}; + +/// An owned LLVM context. +/// +/// Every other handle in this module belongs to a context and is valid only while the context is +/// alive. +#[derive(Debug)] +pub struct Context { + raw: LLVMContextRef, +} + +impl Context { + /// Creates a new context. + pub(crate) fn create() -> Self { + Self { raw: unsafe { LLVMContextCreate() } } + } + + /// Wraps a raw context, taking ownership of it. + /// + /// # Safety + /// + /// `raw` must be a valid context. Wrap the result in [`ManuallyDrop`] to borrow a context that + /// something else owns. + pub(crate) const unsafe fn new(raw: LLVMContextRef) -> Self { + Self { raw } + } + + /// Returns the raw context. + pub const fn raw(&self) -> LLVMContextRef { + self.raw + } + + /// Creates an instruction builder. + pub(crate) fn create_builder(&self) -> Builder { + Builder { raw: unsafe { LLVMCreateBuilderInContext(self.raw) } } + } + + /// Creates an empty module. + pub(crate) fn create_module(&self, name: &str) -> Module { + let name = to_c_str(name); + Module { raw: unsafe { LLVMModuleCreateWithNameInContext(name.as_ptr(), self.raw) } } + } + + /// The `void` type. + pub(crate) fn void_type(&self) -> Type { + Type(unsafe { LLVMVoidTypeInContext(self.raw) }) + } + + /// The `i1` type. + pub(crate) fn bool_type(&self) -> Type { + self.int_type(1) + } + + /// The `i8` type. + pub(crate) fn i8_type(&self) -> Type { + self.int_type(8) + } + + /// The `i16` type. + pub(crate) fn i16_type(&self) -> Type { + self.int_type(16) + } + + /// The `i32` type. + pub(crate) fn i32_type(&self) -> Type { + self.int_type(32) + } + + /// The `i64` type. + pub(crate) fn i64_type(&self) -> Type { + self.int_type(64) + } + + /// The `i128` type. + pub(crate) fn i128_type(&self) -> Type { + self.int_type(128) + } + + /// The integer type with the given bit width. + pub(crate) fn int_type(&self, bits: u32) -> Type { + Type(unsafe { LLVMIntTypeInContext(self.raw, bits) }) + } + + /// The integer type as wide as a pointer of the given target. + pub(crate) fn ptr_sized_int_type(&self, target_data: &TargetData) -> Type { + Type(unsafe { LLVMIntPtrTypeInContext(self.raw, target_data.as_mut_ptr()) }) + } + + /// The opaque pointer type in the default address space. + pub(crate) fn ptr_type(&self) -> Type { + Type(unsafe { LLVMPointerTypeInContext(self.raw, 0) }) + } + + /// An anonymous struct type. + pub(crate) fn struct_type(&self, fields: &[Type], packed: bool) -> Type { + Type(unsafe { + LLVMStructTypeInContext( + self.raw, + fields.as_ptr().cast_mut().cast(), + fields.len() as u32, + packed as LLVMBool, + ) + }) + } + + /// Appends a basic block to `function`. + pub(crate) fn append_basic_block(&self, function: Function, name: &str) -> Block { + let name = to_c_str(name); + Block(unsafe { + LLVMAppendBasicBlockInContext(self.raw, function.as_value_ref(), name.as_ptr()) + }) + } + + /// Inserts a basic block right after `block`. + pub(crate) fn insert_basic_block_after(&self, block: Block, name: &str) -> Block { + unsafe { + let next = LLVMGetNextBasicBlock(block.0); + if next.is_null() { + return self.append_basic_block(Function(LLVMGetBasicBlockParent(block.0)), name); + } + let name = to_c_str(name); + Block(LLVMInsertBasicBlockInContext(self.raw, next, name.as_ptr())) + } + } + + /// Creates a metadata string. + pub(crate) fn metadata_string(&self, string: &str) -> Metadata { + let string = to_c_str(string); + Metadata::new(unsafe { + LLVMMDStringInContext2(self.raw, string.as_ptr(), string.to_bytes().len()) + }) + } + + /// Creates a metadata node holding `values`. + pub(crate) fn metadata_node(&self, values: &[Metadata]) -> MetadataNode { + unsafe { + let node = + LLVMMDNodeInContext2(self.raw, values.as_ptr().cast_mut().cast(), values.len()); + MetadataNode(LLVMMetadataAsValue(self.raw, node)) + } + } + + /// Returns the ID of the metadata kind `name`, such as `prof`. + pub(crate) fn get_kind_id(&self, name: &str) -> u32 { + unsafe { LLVMGetMDKindIDInContext(self.raw, name.as_ptr().cast(), name.len() as u32) } + } + + /// Creates an enum attribute. Look the kind up with [`Attribute::get_named_enum_kind_id`]. + pub(crate) fn create_enum_attribute(&self, kind_id: u32, value: u64) -> Attribute { + Attribute(unsafe { LLVMCreateEnumAttribute(self.raw, kind_id, value) }) + } + + /// Creates a string attribute. + pub(crate) fn create_string_attribute(&self, key: &str, value: &str) -> Attribute { + Attribute(unsafe { + LLVMCreateStringAttribute( + self.raw, + key.as_ptr().cast(), + key.len() as u32, + value.as_ptr().cast(), + value.len() as u32, + ) + }) + } + + /// Creates a type attribute, such as `sret`. + pub(crate) fn create_type_attribute(&self, kind_id: u32, ty: Type) -> Attribute { + Attribute(unsafe { LLVMCreateTypeAttribute(self.raw, kind_id, ty.0) }) + } +} + +impl Drop for Context { + fn drop(&mut self) { + unsafe { LLVMContextDispose(self.raw) } + } +} + +/// An owned LLVM module. +#[derive(Debug)] +pub struct Module { + raw: LLVMModuleRef, +} + +impl Module { + /// Wraps a raw module, taking ownership of it. + /// + /// # Safety + /// + /// `raw` must be a valid module that nothing else disposes. Wrap the result in + /// [`ManuallyDrop`] to borrow a module that something else owns. + pub(crate) const unsafe fn new(raw: LLVMModuleRef) -> Self { + Self { raw } + } + + /// Returns the raw module. + pub(crate) const fn as_mut_ptr(&self) -> LLVMModuleRef { + self.raw + } + + /// Releases ownership of the raw module. + pub(crate) fn into_raw(self) -> LLVMModuleRef { + ManuallyDrop::new(self).raw + } + + /// Sets the module identifier. + pub(crate) fn set_name(&self, name: &str) { + unsafe { LLVMSetModuleIdentifier(self.raw, name.as_ptr().cast(), name.len()) } + } + + /// Sets the source file name. + pub(crate) fn set_source_file_name(&self, name: &str) { + unsafe { LLVMSetSourceFileName(self.raw, name.as_ptr().cast(), name.len()) } + } + + /// Sets the data layout string. + pub(crate) fn set_data_layout(&self, data_layout: &CStr) { + unsafe { LLVMSetDataLayout(self.raw, data_layout.as_ptr()) } + } + + /// Sets the target triple. + pub(crate) fn set_triple(&self, triple: &CStr) { + unsafe { LLVMSetTarget(self.raw, triple.as_ptr()) } + } + + /// Adds a module flag holding an integer constant. + pub(crate) fn add_basic_value_flag( + &self, + key: &str, + behavior: LLVMModuleFlagBehavior, + flag: Value, + ) { + unsafe { + LLVMAddModuleFlag( + self.raw, + behavior, + key.as_ptr().cast(), + key.len(), + LLVMValueAsMetadata(flag.0), + ) + } + } + + /// Adds a function declaration. + pub(crate) fn add_function( + &self, + name: &str, + ty: Type, + linkage: Option, + ) -> Function { + let name = to_c_str(name); + let function = Function(unsafe { LLVMAddFunction(self.raw, name.as_ptr(), ty.0) }); + if let Some(linkage) = linkage { + function.set_linkage(linkage); + } + function + } + + /// Returns the function named `name`, if any. + pub(crate) fn get_function(&self, name: &str) -> Option { + let name = to_c_str(name); + Function::new(unsafe { LLVMGetNamedFunction(self.raw, name.as_ptr()) }) + } + + /// Iterates over the functions of the module. + pub(crate) fn get_functions(&self) -> impl Iterator { + let first = Function::new(unsafe { LLVMGetFirstFunction(self.raw) }); + std::iter::successors(first, |f| { + Function::new(unsafe { LLVMGetNextFunction(f.as_value_ref()) }) + }) + } + + /// Adds a global variable in the default address space. + pub(crate) fn add_global(&self, ty: Type, name: &str) -> Global { + let name = to_c_str(name); + Global(unsafe { LLVMAddGlobal(self.raw, ty.0, name.as_ptr()) }) + } + + /// Checks the module for errors. + pub(crate) fn verify(&self) -> Result<(), LLVMString> { + let mut message = ptr::null_mut(); + let failed = unsafe { + LLVMVerifyModule( + self.raw, + LLVMVerifierFailureAction::LLVMReturnStatusAction, + &mut message, + ) + }; + let message = (!message.is_null()).then(|| unsafe { LLVMString::from_raw(message) }); + match message { + Some(message) if failed != 0 => Err(message), + _ => Ok(()), + } + } + + /// Returns the textual IR. + #[cfg(test)] + pub(crate) fn print_to_string(&self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMPrintModuleToString(self.raw)) } + } + + /// Writes the textual IR to `path`. + pub(crate) fn print_to_file(&self, path: &Path) -> Result<(), LLVMString> { + let path = to_c_str(path.to_str().expect("Did not find a valid Unicode path string")); + let mut message = ptr::null_mut(); + let failed = unsafe { LLVMPrintModuleToFile(self.raw, path.as_ptr(), &mut message) }; + if failed == 0 { + return Ok(()); + } + Err(if message.is_null() { + unsafe { + LLVMString::from_raw(LLVMCreateMessage(c"Error string was uninitialized.".as_ptr())) + } + } else { + unsafe { LLVMString::from_raw(message) } + }) + } + + /// Runs the new-pass-manager pipeline `passes`. + pub(crate) fn run_passes( + &self, + passes: &str, + machine: &TargetMachine, + options: &PassBuilderOptions, + ) -> Result<(), LLVMString> { + let passes = to_c_str(passes); + let error = + unsafe { LLVMRunPasses(self.raw, passes.as_ptr(), machine.as_mut_ptr(), options.raw) }; + if error.is_null() { + return Ok(()); + } + Err(unsafe { LLVMString::from_raw(LLVMGetErrorMessage(error)) }) + } +} + +impl Drop for Module { + fn drop(&mut self) { + unsafe { LLVMDisposeModule(self.raw) } + } +} + +/// Options for [`Module::run_passes`]. +#[derive(Debug)] +pub(crate) struct PassBuilderOptions { + raw: LLVMPassBuilderOptionsRef, +} + +impl PassBuilderOptions { + /// Creates the default options. + pub(crate) fn create() -> Self { + Self { raw: unsafe { LLVMCreatePassBuilderOptions() } } + } +} + +impl Drop for PassBuilderOptions { + fn drop(&mut self) { + unsafe { LLVMDisposePassBuilderOptions(self.raw) } + } +} + +/// An LLVM type. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Type(LLVMTypeRef); + +impl Type { + /// Creates a function type that returns `self`. + pub(crate) fn fn_type(self, params: &[Self], var_args: bool) -> Self { + Self(unsafe { + LLVMFunctionType( + self.0, + params.as_ptr().cast_mut().cast(), + params.len() as u32, + var_args as LLVMBool, + ) + }) + } + + /// Creates an array type of `size` elements of `self`. + pub(crate) fn array_type(self, size: u32) -> Self { + Self(unsafe { LLVMArrayType2(self.0, size as u64) }) + } + + /// The bit width of an integer type. + pub(crate) fn bit_width(self) -> u32 { + unsafe { LLVMGetIntTypeWidth(self.0) } + } + + /// Creates an integer constant of this type. + /// + /// `value` is truncated to the bit width of the type, and sign-extended from it if + /// `sign_extend` is set. + pub(crate) fn const_int(self, mut value: u64, sign_extend: bool) -> Value { + // LLVM 23 no longer truncates implicitly. + let bit_width = self.bit_width(); + if bit_width < u64::BITS { + value &= (1 << bit_width) - 1; + } + Value(unsafe { LLVMConstInt(self.0, value, sign_extend as LLVMBool) }) + } + + /// Creates an integer constant of this type from a hexadecimal string. + pub(crate) fn const_int_from_hex(self, hex: &str) -> Value { + Value(unsafe { + LLVMConstIntOfStringAndSize(self.0, hex.as_ptr().cast(), hex.len() as u32, 16) + }) + } + + /// Creates an array constant with element type `self`. + pub(crate) fn const_array(self, values: &[Value]) -> Value { + Value(unsafe { + LLVMConstArray2(self.0, values.as_ptr().cast_mut().cast(), values.len() as u64) + }) + } + + /// Creates the null constant of this type. + pub(crate) fn const_null(self) -> Value { + Value(unsafe { LLVMConstNull(self.0) }) + } + + /// Returns the textual IR of the type. + pub(crate) fn print_to_string(self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMPrintTypeToString(self.0)) } + } +} + +impl fmt::Debug for Type { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.print_to_string(), f) + } +} + +/// An LLVM value: a constant, an argument, or the result of an instruction. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Value(LLVMValueRef); + +impl Value { + /// Returns the type of the value. + pub(crate) fn get_type(self) -> Type { + Type(unsafe { LLVMTypeOf(self.0) }) + } + + /// Sets the name of the value. + pub(crate) fn set_name(self, name: &str) { + unsafe { LLVMSetValueName2(self.0, name.as_ptr().cast(), name.len()) } + } + + /// Sets the alignment of a load. + pub(crate) fn set_alignment(self, align: u32) { + assert!(align.is_power_of_two(), "alignment must be a power of two"); + unsafe { LLVMSetAlignment(self.0, align) } + } + + /// Wraps the value as metadata. + pub(crate) fn as_metadata(self) -> Metadata { + Metadata::new(unsafe { LLVMValueAsMetadata(self.0) }) + } +} + +impl fmt::Debug for Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt_value(self.0, f) + } +} + +/// An LLVM instruction. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Inst(LLVMValueRef); + +impl Inst { + /// Returns the result of the instruction, or `None` if its type is `void`. + pub(crate) fn value(self) -> Option { + let void = unsafe { LLVMGetTypeKind(LLVMTypeOf(self.0)) } == LLVMTypeKind::LLVMVoidTypeKind; + (!void).then_some(Value(self.0)) + } + + /// Sets the alignment of a load or store. + pub(crate) fn set_alignment(self, align: u32) { + assert!(align.is_power_of_two(), "alignment must be a power of two"); + unsafe { LLVMSetAlignment(self.0, align) } + } + + /// Attaches the metadata node `node` of kind `kind_id`. + pub(crate) fn set_metadata(self, node: MetadataNode, kind_id: u32) { + unsafe { LLVMSetMetadata(self.0, kind_id, node.0) } + } + + /// Removes the instruction from its block and deletes it. + pub(crate) fn erase_from_basic_block(self) { + unsafe { LLVMInstructionEraseFromParent(self.0) } + } + + /// Sets the tail call kind of a call. + pub(crate) fn set_tail_call_kind(self, kind: LLVMTailCallKind) { + unsafe { LLVMSetTailCallKind(self.0, kind) } + } + + /// Sets the calling convention of a call. + pub(crate) fn set_call_conv(self, call_conv: u32) { + unsafe { LLVMSetInstructionCallConv(self.0, call_conv) } + } +} + +impl fmt::Debug for Inst { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt_value(self.0, f) + } +} + +/// An LLVM function. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Function(LLVMValueRef); + +impl Function { + fn new(raw: LLVMValueRef) -> Option { + (!raw.is_null()).then_some(Self(raw)) + } + + /// Returns the name of the function. + pub(crate) fn get_name(self) -> String { + let mut len = 0; + unsafe { + let name = LLVMGetValueName2(self.0, &mut len); + String::from_utf8_lossy(std::slice::from_raw_parts(name.cast(), len)).into_owned() + } + } + + /// Returns the function type. + pub(crate) fn get_type(self) -> Type { + Type(unsafe { LLVMGlobalGetValueType(self.0) }) + } + + /// Returns the number of parameters. + pub(crate) fn count_params(self) -> u32 { + unsafe { LLVMCountParams(self.0) } + } + + /// Returns the `nth` parameter, if it exists. + pub(crate) fn get_nth_param(self, nth: u32) -> Option { + (nth < self.count_params()).then(|| Value(unsafe { LLVMGetParam(self.0, nth) })) + } + + /// Returns the number of basic blocks. + pub(crate) fn count_basic_blocks(self) -> u32 { + unsafe { LLVMCountBasicBlocks(self.0) } + } + + /// Returns the entry block, if the function has a body. + pub(crate) fn get_first_basic_block(self) -> Option { + Block::new(unsafe { LLVMGetFirstBasicBlock(self.0) }) + } + + /// Sets the linkage. + pub(crate) fn set_linkage(self, linkage: LLVMLinkage) { + unsafe { LLVMSetLinkage(self.0, linkage) } + } + + /// Returns the calling convention. + pub(crate) fn get_call_conv(self) -> u32 { + unsafe { LLVMGetFunctionCallConv(self.0) } + } + + /// Sets the calling convention. + pub(crate) fn set_call_conv(self, call_conv: u32) { + unsafe { LLVMSetFunctionCallConv(self.0, call_conv) } + } + + /// Adds an attribute. + pub(crate) fn add_attribute(self, loc: AttributeLoc, attribute: Attribute) { + unsafe { LLVMAddAttributeAtIndex(self.0, loc.index(), attribute.0) } + } + + /// Attaches the debug info subprogram. + pub(crate) fn set_subprogram(self, subprogram: Metadata) { + unsafe { LLVMSetSubprogram(self.0, subprogram.raw()) } + } + + /// Returns the raw value. + pub(crate) const fn as_value_ref(self) -> LLVMValueRef { + self.0 + } + + /// Deletes the function from its module. + /// + /// # Safety + /// + /// Nothing may use the function afterwards. + pub(crate) unsafe fn delete(self) { + unsafe { LLVMDeleteFunction(self.0) } + } +} + +impl fmt::Debug for Function { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt_value(self.0, f) + } +} + +/// A global variable. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub(crate) struct Global(LLVMValueRef); + +impl Global { + /// Returns the address of the global, a pointer value. + pub(crate) const fn as_pointer_value(self) -> Value { + Value(self.0) + } + + /// Sets the initial value. + pub(crate) fn set_initializer(self, value: Value) { + unsafe { LLVMSetInitializer(self.0, value.0) } + } + + /// Marks the global as constant. + pub(crate) fn set_constant(self, constant: bool) { + unsafe { LLVMSetGlobalConstant(self.0, constant as LLVMBool) } + } + + /// Sets the linkage. + pub(crate) fn set_linkage(self, linkage: LLVMLinkage) { + unsafe { LLVMSetLinkage(self.0, linkage) } + } + + /// Sets whether the address is significant. + pub(crate) fn set_unnamed_address(self, address: LLVMUnnamedAddr) { + unsafe { LLVMSetUnnamedAddress(self.0, address) } + } +} + +/// A basic block. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct Block(LLVMBasicBlockRef); + +impl Block { + fn new(raw: LLVMBasicBlockRef) -> Option { + (!raw.is_null()).then_some(Self(raw)) + } + + /// Returns the last instruction, if the block is not empty. + pub(crate) fn get_last_instruction(self) -> Option { + let inst = unsafe { LLVMGetLastInstruction(self.0) }; + (!inst.is_null()).then_some(Inst(inst)) + } + + /// Returns the address of the block, or `None` for an entry block or a detached block. + /// + /// # Safety + /// + /// The block must belong to a function that is not deleted. + pub(crate) unsafe fn get_address(self) -> Option { + unsafe { + let parent = LLVMGetBasicBlockParent(self.0); + // Taking the address of the entry block is illegal. + if parent.is_null() || LLVMGetPreviousBasicBlock(self.0).is_null() { + return None; + } + let address = LLVMBlockAddress(parent, self.0); + (!address.is_null()).then_some(Value(address)) + } + } +} + +impl fmt::Debug for Block { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt_value(unsafe { LLVMBasicBlockAsValue(self.0) }, f) + } +} + +/// A metadata node, usable as the operand of an instruction's metadata. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub(crate) struct MetadataNode(LLVMValueRef); + +/// An LLVM attribute. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub(crate) struct Attribute(LLVMAttributeRef); + +impl Attribute { + /// Wraps a raw attribute. + pub(crate) const fn new(raw: LLVMAttributeRef) -> Self { + Self(raw) + } + + /// Returns the kind ID of the enum attribute `name`, or 0 if there is none. + pub(crate) fn get_named_enum_kind_id(name: &str) -> u32 { + unsafe { LLVMGetEnumAttributeKindForName(name.as_ptr().cast(), name.len()) } + } +} + +/// The place an attribute applies to. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum AttributeLoc { + /// The return value. + Return, + /// The parameter at the given index. + Param(u32), + /// The function itself. + Function, +} + +impl AttributeLoc { + fn index(self) -> u32 { + match self { + Self::Return => 0, + Self::Param(index) => { + assert!(index <= u32::MAX - 2, "param index must be <= u32::MAX - 2"); + index + 1 + } + Self::Function => LLVMAttributeFunctionIndex, + } + } +} + +/// An instruction builder. +#[derive(Debug)] +pub(crate) struct Builder { + raw: LLVMBuilderRef, +} + +macro_rules! binary_ops { + ($($(#[$meta:meta])* $name:ident => $llvm:ident),* $(,)?) => {$( + $(#[$meta])* + pub(crate) fn $name(&self, lhs: Value, rhs: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { $llvm(self.raw, lhs.0, rhs.0, name.as_ptr()) }) + } + )*}; +} + +macro_rules! cast_ops { + ($($(#[$meta:meta])* $name:ident => $llvm:ident),* $(,)?) => {$( + $(#[$meta])* + pub(crate) fn $name(&self, value: Value, ty: Type, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { $llvm(self.raw, value.0, ty.0, name.as_ptr()) }) + } + )*}; +} + +impl Builder { + /// Positions the builder at the end of `block`. + pub(crate) fn position_at_end(&self, block: Block) { + unsafe { LLVMPositionBuilderAtEnd(self.raw, block.0) } + } + + /// Returns the block the builder inserts into. + pub(crate) fn get_insert_block(&self) -> Option { + Block::new(unsafe { LLVMGetInsertBlock(self.raw) }) + } + + /// Sets the debug location attached to new instructions. + pub(crate) fn set_current_debug_location(&self, location: DILocation) { + unsafe { LLVMSetCurrentDebugLocation2(self.raw, location.raw()) } + } + + /// Returns the debug location attached to new instructions, if any. + pub(crate) fn get_current_debug_location(&self) -> Option { + DILocation::new(unsafe { LLVMGetCurrentDebugLocation2(self.raw) }) + } + + /// Stops attaching a debug location to new instructions. + pub(crate) fn unset_current_debug_location(&self) { + unsafe { LLVMSetCurrentDebugLocation2(self.raw, ptr::null_mut()) } + } + + /// Builds a call to `function`. + pub(crate) fn build_call(&self, function: Function, args: &[Value], name: &str) -> Inst { + let fn_ty = function.get_type(); + // LLVM rejects names on calls that return `void`. + let void = unsafe { LLVMGetTypeKind(LLVMGetReturnType(fn_ty.0)) } + == LLVMTypeKind::LLVMVoidTypeKind; + let name = to_c_str(if void { "" } else { name }); + Inst(unsafe { + LLVMBuildCall2( + self.raw, + fn_ty.0, + function.as_value_ref(), + args.as_ptr().cast_mut().cast(), + args.len() as u32, + name.as_ptr(), + ) + }) + } + + /// Builds a stack allocation of `ty`. + pub(crate) fn build_alloca(&self, ty: Type, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildAlloca(self.raw, ty.0, name.as_ptr()) }) + } + + /// Builds a load of `ty` from `ptr`. + pub(crate) fn build_load(&self, ty: Type, ptr: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildLoad2(self.raw, ty.0, ptr.0, name.as_ptr()) }) + } + + /// Builds a store of `value` to `ptr`. + pub(crate) fn build_store(&self, ptr: Value, value: Value) -> Inst { + Inst(unsafe { LLVMBuildStore(self.raw, value.0, ptr.0) }) + } + + /// Builds a GEP that stays inside the allocated object. + /// + /// # Safety + /// + /// The address must be in bounds, otherwise the result is poison. + pub(crate) unsafe fn build_in_bounds_gep( + &self, + ty: Type, + ptr: Value, + indexes: &[Value], + name: &str, + ) -> Value { + let name = to_c_str(name); + Value(unsafe { + LLVMBuildInBoundsGEP2( + self.raw, + ty.0, + ptr.0, + indexes.as_ptr().cast_mut().cast(), + indexes.len() as u32, + name.as_ptr(), + ) + }) + } + + /// Builds a global string constant and returns a pointer to it. + pub(crate) fn build_global_string_ptr(&self, value: &str, name: &str) -> Value { + let value = to_c_str(value); + let name = to_c_str(name); + Value(unsafe { LLVMBuildGlobalString(self.raw, value.as_ptr(), name.as_ptr()) }) + } + + /// Builds an extraction of the field `index` of an aggregate. + pub(crate) fn build_extract_value(&self, aggregate: Value, index: u32, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildExtractValue(self.raw, aggregate.0, index, name.as_ptr()) }) + } + + /// Builds a `phi` of `ty` with the given incoming values. + pub(crate) fn build_phi(&self, ty: Type, incoming: &[(Value, Block)], name: &str) -> Value { + let name = to_c_str(name); + let (mut values, mut blocks): (Vec<_>, Vec<_>) = + incoming.iter().map(|&(value, block)| (value.0, block.0)).unzip(); + unsafe { + let phi = LLVMBuildPhi(self.raw, ty.0, name.as_ptr()); + LLVMAddIncoming(phi, values.as_mut_ptr(), blocks.as_mut_ptr(), incoming.len() as u32); + Value(phi) + } + } + + /// Builds a `select`. + pub(crate) fn build_select( + &self, + cond: Value, + then: Value, + otherwise: Value, + name: &str, + ) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildSelect(self.raw, cond.0, then.0, otherwise.0, name.as_ptr()) }) + } + + /// Builds an integer comparison. + pub(crate) fn build_int_compare( + &self, + predicate: LLVMIntPredicate, + lhs: Value, + rhs: Value, + name: &str, + ) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildICmp(self.raw, predicate, lhs.0, rhs.0, name.as_ptr()) }) + } + + /// Builds a comparison of a pointer with null. + pub(crate) fn build_is_null(&self, ptr: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildIsNull(self.raw, ptr.0, name.as_ptr()) }) + } + + /// Builds a comparison of a pointer with non-null. + pub(crate) fn build_is_not_null(&self, ptr: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildIsNotNull(self.raw, ptr.0, name.as_ptr()) }) + } + + /// Builds a bitwise not. + pub(crate) fn build_not(&self, value: Value, name: &str) -> Value { + let name = to_c_str(name); + Value(unsafe { LLVMBuildNot(self.raw, value.0, name.as_ptr()) }) + } + + /// Builds a right shift, arithmetic if `sign_extend` is set. + pub(crate) fn build_right_shift( + &self, + lhs: Value, + rhs: Value, + sign_extend: bool, + name: &str, + ) -> Value { + if sign_extend { self.build_ashr(lhs, rhs, name) } else { self.build_lshr(lhs, rhs, name) } + } + + binary_ops! { + /// Builds an integer addition. + build_int_add => LLVMBuildAdd, + /// Builds an integer subtraction. + build_int_sub => LLVMBuildSub, + /// Builds an integer multiplication. + build_int_mul => LLVMBuildMul, + /// Builds an unsigned division. + build_int_unsigned_div => LLVMBuildUDiv, + /// Builds a signed division. + build_int_signed_div => LLVMBuildSDiv, + /// Builds an unsigned remainder. + build_int_unsigned_rem => LLVMBuildURem, + /// Builds a signed remainder. + build_int_signed_rem => LLVMBuildSRem, + /// Builds a bitwise or. + build_or => LLVMBuildOr, + /// Builds a bitwise and. + build_and => LLVMBuildAnd, + /// Builds a bitwise xor. + build_xor => LLVMBuildXor, + /// Builds a left shift. + build_left_shift => LLVMBuildShl, + /// Builds a logical right shift. + build_lshr => LLVMBuildLShr, + /// Builds an arithmetic right shift. + build_ashr => LLVMBuildAShr, + } + + cast_ops! { + /// Builds a zero extension to `ty`. + build_int_z_extend => LLVMBuildZExt, + /// Builds a sign extension to `ty`. + build_int_s_extend => LLVMBuildSExt, + /// Builds a truncation to `ty`. + build_int_truncate => LLVMBuildTrunc, + /// Builds an integer to pointer conversion. + build_int_to_ptr => LLVMBuildIntToPtr, + } + + /// Builds a `ret` with no value. + pub(crate) fn build_return(&self, value: Option) -> Inst { + Inst(unsafe { + match value { + Some(value) => LLVMBuildRet(self.raw, value.0), + None => LLVMBuildRetVoid(self.raw), + } + }) + } + + /// Builds a `ret` of an aggregate made of `values`. + pub(crate) fn build_aggregate_return(&self, values: &[Value]) -> Inst { + Inst(unsafe { + LLVMBuildAggregateRet(self.raw, values.as_ptr().cast_mut().cast(), values.len() as u32) + }) + } + + /// Builds an unconditional branch. + pub(crate) fn build_unconditional_branch(&self, dest: Block) -> Inst { + Inst(unsafe { LLVMBuildBr(self.raw, dest.0) }) + } + + /// Builds a conditional branch. + pub(crate) fn build_conditional_branch( + &self, + cond: Value, + then: Block, + otherwise: Block, + ) -> Inst { + Inst(unsafe { LLVMBuildCondBr(self.raw, cond.0, then.0, otherwise.0) }) + } + + /// Builds a `switch` on `value` with the given cases. + pub(crate) fn build_switch( + &self, + value: Value, + default: Block, + cases: &[(Value, Block)], + ) -> Inst { + unsafe { + let switch = LLVMBuildSwitch(self.raw, value.0, default.0, cases.len() as u32); + for &(value, block) in cases { + LLVMAddCase(switch, value.0, block.0); + } + Inst(switch) + } + } + + /// Builds an indirect branch to `address`, which is one of `destinations`. + pub(crate) fn build_indirect_branch(&self, address: Value, destinations: &[Block]) -> Inst { + unsafe { + let branch = LLVMBuildIndirectBr(self.raw, address.0, destinations.len() as u32); + for dest in destinations { + LLVMAddDestination(branch, dest.0); + } + Inst(branch) + } + } + + /// Builds an `unreachable`. + pub(crate) fn build_unreachable(&self) -> Inst { + Inst(unsafe { LLVMBuildUnreachable(self.raw) }) + } +} + +impl Drop for Builder { + fn drop(&mut self) { + unsafe { LLVMDisposeBuilder(self.raw) } + } +} + +fn fmt_value(value: LLVMValueRef, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&unsafe { LLVMString::from_raw(LLVMPrintValueToString(value)) }, f) +} diff --git a/crates/jit/llvm/src/wrap/mod.rs b/crates/jit/llvm/src/wrap/mod.rs new file mode 100644 index 000000000..efe75ba08 --- /dev/null +++ b/crates/jit/llvm/src/wrap/mod.rs @@ -0,0 +1,35 @@ +//! Small owned wrappers over the `llvm-sys` C API. +//! +//! The wrappers cover only what the backend uses. Handles ([`Type`], [`Value`], [`Block`], ...) are +//! `Copy` pointers into an LLVM context and carry no lifetime: the backend owns the [`Context`] and +//! keeps it alive for as long as any handle exists. + +use std::{ + borrow::Cow, + ffi::{CStr, CString}, +}; + +mod di; +mod ir; +mod string; +mod target; + +pub(crate) use di::*; +pub use ir::*; +pub use string::*; +pub use target::*; + +/// Converts `s` to a C string, truncating it at the first NUL byte. +/// +/// The empty string maps to a static empty C string without allocating. +#[inline] +pub(crate) fn to_c_str(s: &str) -> Cow<'_, CStr> { + if s.is_empty() { + return Cow::Borrowed(c""); + } + match CStr::from_bytes_until_nul(s.as_bytes()) { + Ok(c) => Cow::Borrowed(c), + // SAFETY: There is no NUL byte in `s`, otherwise `from_bytes_until_nul` succeeds. + Err(_) => Cow::Owned(unsafe { CString::from_vec_unchecked(s.as_bytes().to_vec()) }), + } +} diff --git a/crates/jit/llvm/src/wrap/string.rs b/crates/jit/llvm/src/wrap/string.rs new file mode 100644 index 000000000..aec57b8f4 --- /dev/null +++ b/crates/jit/llvm/src/wrap/string.rs @@ -0,0 +1,62 @@ +use llvm_sys::core::LLVMDisposeMessage; +use std::{ + error::Error, + ffi::{CStr, c_char}, + fmt, + ops::Deref, + ptr::NonNull, +}; + +/// An owned, NUL-terminated string allocated by LLVM, such as an error message. +pub struct LLVMString { + ptr: NonNull, +} + +// SAFETY: The string is uniquely owned and never mutated. +unsafe impl Send for LLVMString {} +unsafe impl Sync for LLVMString {} + +impl LLVMString { + /// Takes ownership of a string allocated by LLVM, to be freed with `LLVMDisposeMessage`. + /// + /// # Safety + /// + /// `ptr` must point to a valid NUL-terminated message that nothing else frees. + /// + /// # Panics + /// + /// Panics if `ptr` is null. + pub const unsafe fn from_raw(ptr: *mut c_char) -> Self { + Self { ptr: NonNull::new(ptr).expect("null LLVM string") } + } +} + +impl Deref for LLVMString { + type Target = CStr; + + fn deref(&self) -> &CStr { + // SAFETY: `from_raw` guarantees a valid NUL-terminated string. + unsafe { CStr::from_ptr(self.ptr.as_ptr()) } + } +} + +impl fmt::Debug for LLVMString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&**self, f) + } +} + +impl fmt::Display for LLVMString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.to_string_lossy()) + } +} + +impl Error for LLVMString {} + +impl Drop for LLVMString { + fn drop(&mut self) { + // SAFETY: `from_raw` guarantees the string came from LLVM and is owned by `self`. + unsafe { LLVMDisposeMessage(self.ptr.as_ptr()) } + } +} diff --git a/crates/jit/llvm/src/wrap/target.rs b/crates/jit/llvm/src/wrap/target.rs new file mode 100644 index 000000000..6745fb6f8 --- /dev/null +++ b/crates/jit/llvm/src/wrap/target.rs @@ -0,0 +1,238 @@ +//! Targets, target machines, and code generation. + +use super::{LLVMString, Module, to_c_str}; +use llvm_sys::{ + core::{LLVMDisposeMemoryBuffer, LLVMGetBufferSize, LLVMGetBufferStart}, + prelude::LLVMMemoryBufferRef, + target::*, + target_machine::*, +}; +use std::{ffi::CStr, path::Path, ptr, slice}; + +/// A registered LLVM target. +/// +/// Targets live in a process-wide registry and are never freed. +#[derive(Clone, Copy, Debug)] +pub(crate) struct Target { + raw: LLVMTargetRef, +} + +// SAFETY: The registry entry is immutable and lives for the whole process. +unsafe impl Send for Target {} +unsafe impl Sync for Target {} + +impl Target { + /// Initializes the native target, assembly printer, and disassembler. + pub(crate) fn initialize_native() -> Result<(), String> { + unsafe { + if LLVM_InitializeNativeTarget() == 1 { + return Err("Unknown error in initializing native target".into()); + } + if LLVM_InitializeNativeAsmPrinter() == 1 { + return Err("Unknown error in initializing native asm printer".into()); + } + if LLVM_InitializeNativeDisassembler() == 1 { + return Err("Unknown error in initializing native disassembler".into()); + } + } + Ok(()) + } + + /// Looks up the target for `triple`. + pub(crate) fn from_triple(triple: &CStr) -> Result { + let mut target = ptr::null_mut(); + let mut message = ptr::null_mut(); + let failed = unsafe { LLVMGetTargetFromTriple(triple.as_ptr(), &mut target, &mut message) }; + if failed == 1 { + return Err(unsafe { LLVMString::from_raw(message) }); + } + Ok(Self { raw: target }) + } + + /// Returns the name of the target. + pub(crate) fn get_name(&self) -> &CStr { + unsafe { CStr::from_ptr(LLVMGetTargetName(self.raw)) } + } + + /// Returns whether the target supports JIT compilation. + pub(crate) fn has_jit(&self) -> bool { + unsafe { LLVMTargetHasJIT(self.raw) == 1 } + } + + /// Returns whether the target has a target machine. + pub(crate) fn has_target_machine(&self) -> bool { + unsafe { LLVMTargetHasTargetMachine(self.raw) == 1 } + } + + /// Creates a target machine, or `None` if LLVM cannot. + pub(crate) fn create_target_machine( + &self, + triple: &CStr, + cpu: &str, + features: &str, + level: LLVMCodeGenOptLevel, + reloc_mode: LLVMRelocMode, + code_model: LLVMCodeModel, + ) -> Option { + let cpu = to_c_str(cpu); + let features = to_c_str(features); + let machine = unsafe { + LLVMCreateTargetMachine( + self.raw, + triple.as_ptr(), + cpu.as_ptr(), + features.as_ptr(), + level, + reloc_mode, + code_model, + ) + }; + (!machine.is_null()).then_some(TargetMachine { raw: machine }) + } +} + +/// An owned target machine. +#[derive(Debug)] +pub struct TargetMachine { + raw: LLVMTargetMachineRef, +} + +impl TargetMachine { + /// Returns the raw target machine. + pub(crate) const fn as_mut_ptr(&self) -> LLVMTargetMachineRef { + self.raw + } + + /// Returns the default target triple of the host. + pub(crate) fn get_default_triple() -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetDefaultTargetTriple()) } + } + + /// Returns the CPU name of the host. + pub(crate) fn get_host_cpu_name() -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetHostCPUName()) } + } + + /// Returns the comma-separated CPU features of the host. + pub(crate) fn get_host_cpu_features() -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetHostCPUFeatures()) } + } + + /// Returns the target triple of the machine. + pub(crate) fn get_triple(&self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetTargetMachineTriple(self.raw)) } + } + + /// Returns the CPU name of the machine. + pub(crate) fn get_cpu(&self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMGetTargetMachineCPU(self.raw)) } + } + + /// Creates the data layout of the machine. + pub(crate) fn get_target_data(&self) -> TargetData { + TargetData { raw: unsafe { LLVMCreateTargetDataLayout(self.raw) } } + } + + /// Sets whether the assembly output carries comments. + pub(crate) fn set_asm_verbosity(&self, verbose: bool) { + unsafe { LLVMSetTargetMachineAsmVerbosity(self.raw, verbose as i32) } + } + + /// Compiles `module` to assembly or an object file in memory. + pub(crate) fn write_to_memory_buffer( + &self, + module: &Module, + file_type: LLVMCodeGenFileType, + ) -> Result { + let mut buffer = ptr::null_mut(); + let mut message = ptr::null_mut(); + let failed = unsafe { + LLVMTargetMachineEmitToMemoryBuffer( + self.raw, + module.as_mut_ptr(), + file_type, + &mut message, + &mut buffer, + ) + }; + if failed == 1 { + return Err(unsafe { LLVMString::from_raw(message) }); + } + Ok(MemoryBuffer { raw: buffer }) + } + + /// Compiles `module` to assembly or an object file at `path`. + pub(crate) fn write_to_file( + &self, + module: &Module, + file_type: LLVMCodeGenFileType, + path: &Path, + ) -> Result<(), LLVMString> { + let path = to_c_str(path.to_str().expect("Did not find a valid Unicode path string")); + let mut message = ptr::null_mut(); + let failed = unsafe { + LLVMTargetMachineEmitToFile( + self.raw, + module.as_mut_ptr(), + path.as_ptr().cast_mut(), + file_type, + &mut message, + ) + }; + if failed == 1 { + return Err(unsafe { LLVMString::from_raw(message) }); + } + Ok(()) + } +} + +impl Drop for TargetMachine { + fn drop(&mut self) { + unsafe { LLVMDisposeTargetMachine(self.raw) } + } +} + +/// An owned target data layout. +#[derive(Debug)] +pub(crate) struct TargetData { + raw: LLVMTargetDataRef, +} + +impl TargetData { + /// Returns the raw target data. + pub(crate) const fn as_mut_ptr(&self) -> LLVMTargetDataRef { + self.raw + } + + /// Returns the data layout string. + pub(crate) fn get_data_layout(&self) -> LLVMString { + unsafe { LLVMString::from_raw(LLVMCopyStringRepOfTargetData(self.raw)) } + } +} + +impl Drop for TargetData { + fn drop(&mut self) { + unsafe { LLVMDisposeTargetData(self.raw) } + } +} + +/// An owned memory buffer, such as the output of [`TargetMachine::write_to_memory_buffer`]. +#[derive(Debug)] +pub(crate) struct MemoryBuffer { + raw: LLVMMemoryBufferRef, +} + +impl MemoryBuffer { + /// Returns the contents of the buffer. + pub(crate) fn as_slice(&self) -> &[u8] { + unsafe { + slice::from_raw_parts(LLVMGetBufferStart(self.raw).cast(), LLVMGetBufferSize(self.raw)) + } + } +} + +impl Drop for MemoryBuffer { + fn drop(&mut self) { + unsafe { LLVMDisposeMemoryBuffer(self.raw) } + } +} diff --git a/crates/jit/runtime/Cargo.toml b/crates/jit/runtime/Cargo.toml index a5e3f346f..05147768b 100644 --- a/crates/jit/runtime/Cargo.toml +++ b/crates/jit/runtime/Cargo.toml @@ -35,7 +35,6 @@ dashmap = "6" derive_more = { version = "2", default-features = false, features = ["debug"] } eyre.workspace = true libc.workspace = true -libloading = "0.9" tempfile = "3.10" quanta.workspace = true rayon.workspace = true diff --git a/crates/jit/runtime/src/evm2_evm.rs b/crates/jit/runtime/src/evm2_evm.rs index 69e704bbd..f8a5ba519 100644 --- a/crates/jit/runtime/src/evm2_evm.rs +++ b/crates/jit/runtime/src/evm2_evm.rs @@ -2,9 +2,10 @@ use crate::runtime::{JitBackend, LookupDecision, LookupRequest, RuntimeCacheKey}; use evm2::{ - EvmTypesHost, InterpreterRunner, + EvmTypesHost, ExecutionConfig, InterpreterRunner, interpreter::{Host, InstrStop, Interpreter}, }; +use evm2_jit_context::can_run_compiled; /// External interpreter runner backed by [`JitBackend`]. #[derive(Clone, Debug)] @@ -33,7 +34,18 @@ impl InterpreterRunner for JitInterpreterRunner { interpreter: &mut Interpreter<'frame, 'host, T>, host: &mut T::Host<'host>, ) -> Option { - run_interpreter(&self.backend, interpreter, host) + // The field layout the compiled code reads depends on the host type, so this check keeps + // direct calls memory safe. Direct callers must check `supports` for the gas schedule and + // opcode table. + if !self.backend.supports_host_type::() { + return None; + } + run_supported(&self.backend, interpreter, host) + } + + #[inline] + fn supports(&self, config: &ExecutionConfig) -> bool { + self.backend.supports_host::(config) } } @@ -41,18 +53,39 @@ impl InterpreterRunner for JitInterpreterRunner { /// /// Returns `None` when no compiled program is available, leaving the caller to /// run the same frame through the evm2 interpreter. +/// +/// Checks on every call that the backend supports the host's gas schedule and opcode table, and +/// counts a rejected host as a fallback each time without logging it. +/// [`InterpreterRunner::supports`] does that check once per runner or config change instead. #[inline] pub fn run_interpreter<'frame, 'host, T: EvmTypesHost>( backend: &JitBackend, interpreter: &mut Interpreter<'frame, 'host, T>, host: &mut T::Host<'host>, +) -> Option { + if !backend.enabled() { + return None; + } + if backend.host_mismatch::(host.execution_config()).is_some() { + backend.count_interpreter_fallback(); + return None; + } + run_supported(backend, interpreter, host) +} + +/// Like [`run_interpreter`], for a host whose execution config the backend already supports. +#[inline] +fn run_supported<'frame, 'host, T: EvmTypesHost>( + backend: &JitBackend, + interpreter: &mut Interpreter<'frame, 'host, T>, + host: &mut T::Host<'host>, ) -> Option { // Disabled runners must not hash code merely to discover that lookup is unavailable. - // Compiled code treats the remaining gas as signed, so it cannot hold more than `i64::MAX`. - if !backend.enabled() - || interpreter.gas().remaining() > i64::MAX as u64 - || !backend.supports_host::(host.execution_config()) - { + if !backend.enabled() { + return None; + } + if !can_run_compiled(interpreter) { + backend.count_interpreter_fallback(); return None; } let code_hash = interpreter.original_bytecode_hash(); @@ -78,7 +111,7 @@ mod tests { use evm2::{ BaseEvmTypes, Evm, Precompiles, SpecId, bytecode::Bytecode, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, ethereum::ethereum_tx_registry, evm::EmptyDB, interpreter::{MessageExt, op}, @@ -95,6 +128,7 @@ mod tests { env::BlockEnv, evm::{AccountInfo, InMemoryDB, JournalEntry}, interpreter::{Host, MessageKind, Word}, + utils::address_to_word, version::GasId, }; @@ -103,7 +137,7 @@ mod tests { &[op::PUSH1, 0x42, op::PUSH0, op::MSTORE, op::PUSH1, 0x20, op::PUSH0, op::RETURN]; #[cfg(feature = "llvm")] - fn blocking_backend() -> JitBackend { + pub(super) fn blocking_backend() -> JitBackend { JitBackend::new(RuntimeConfig { enabled: true, blocking: true, ..RuntimeConfig::default() }) .unwrap() } @@ -138,8 +172,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::copy_from_slice(code)), ..MessageExt::default() }; - let tx_env = TxEnvExt::default(); - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let stop = if with_jit { run_interpreter(&backend, &mut interpreter, &mut host).unwrap() } else { @@ -254,13 +287,12 @@ mod tests { #[test] fn disabled_backend_falls_back_to_interpreter() { - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: 1_000_000, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..Default::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let mut host = Evm::::new( SpecId::CANCUN, BlockEnvExt::default(), @@ -275,13 +307,12 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn gas_above_i64_max_falls_back_to_interpreter() { - let tx_env = TxEnvExt::default(); let message = MessageExt { gas_limit: i64::MAX as u64 + 1, code: Bytecode::new_legacy(Bytes::from_static(&[op::STOP])), ..Default::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let mut host = Evm::::new( SpecId::CANCUN, BlockEnvExt::default(), @@ -290,20 +321,62 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); - assert_eq!(run_interpreter(&blocking_backend(), &mut interpreter, &mut host), None); + let backend = blocking_backend(); + assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), None); + assert_eq!(backend.stats().interpreter_fallbacks, 1); + } + + #[test] + #[cfg(feature = "llvm")] + fn rejected_gas_schedule_counts_fallback() { + let spec = SpecId::CANCUN; + let mut version = Version::new(spec); + version.gas_params.set(GasId::SstoreStatic, 800); + let host = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(spec, version), + spec, + BlockEnv::::default(), + ethereum_tx_registry(spec), + InMemoryDB::default(), + Precompiles::base(spec), + ); + let supports = |backend: &JitBackend| { + InterpreterRunner::::supports( + &JitInterpreterRunner::new(backend.clone()), + host.execution_config(), + ) + }; + + let base = blocking_backend(); + assert!(!supports(&base)); + assert_eq!(base.stats().interpreter_fallbacks, 1); + + let matching = JitBackend::new(RuntimeConfig { + enabled: true, + blocking: true, + gas_params: Some(version.gas_params), + ..RuntimeConfig::default() + }) + .unwrap(); + assert!(supports(&matching)); + assert_eq!(matching.stats().interpreter_fallbacks, 0); + + let disabled = JitBackend::disabled(); + assert!(!supports(&disabled)); + assert_eq!(disabled.stats().interpreter_fallbacks, 0); } #[test] #[cfg(feature = "llvm")] fn compiled_call_executes_message() { - let target = Address::from([0x22; 20]); - let caller = Address::from([0x11; 20]); + let target = Address::repeat_byte(0x22); + let caller = Address::repeat_byte(0x11); let mut database = InMemoryDB::default(); database.insert_account_info(&caller, AccountInfo::default()); database.insert_account_info( &target, AccountInfo::default() - .with_code(Bytecode::new_legacy(Bytes::copy_from_slice(BYTECODE_RET42))), + .with_code(Bytecode::new_legacy(Bytes::from_static(BYTECODE_RET42))), ); let backend = blocking_backend(); let mut host = Evm::::new( @@ -314,7 +387,6 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); - let tx_env = TxEnvExt::default(); // The call output overwrites the word stored before it. let mut code = vec![op::PUSH1, 0x69, op::PUSH0, op::MSTORE]; code.extend([op::PUSH1, 0x20, op::PUSH0, op::PUSH0, op::PUSH0, op::PUSH0]); @@ -339,7 +411,7 @@ mod tests { code: Bytecode::new_legacy(code.into()), ..MessageExt::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), Some(InstrStop::Return)); assert_eq!(interpreter.output().len(), 32); @@ -349,7 +421,7 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_create_executes_message() { - let creator = Address::from([0x33; 20]); + let creator = Address::repeat_byte(0x33); let mut database = InMemoryDB::default(); database.insert_account_info(&creator, AccountInfo::default()); let backend = blocking_backend(); @@ -361,7 +433,6 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); - let tx_env = TxEnvExt::default(); let code = [ op::PUSH10, 0x60, @@ -394,10 +465,10 @@ mod tests { let message = MessageExt { gas_limit: 1_000_000, destination: creator, - code: Bytecode::new_legacy(Bytes::copy_from_slice(&code)), + code: Bytecode::new_legacy(Bytes::from(code)), ..MessageExt::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), Some(InstrStop::Return)); assert_eq!(interpreter.output().len(), 32); @@ -407,7 +478,7 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_staticcall_zeros_child_callvalue() { - let caller = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0x11); let target = Address::with_last_byte(0x68); let mut database = InMemoryDB::default(); database.insert_account_info(&caller, AccountInfo::default()); @@ -432,7 +503,6 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); - let tx_env = TxEnvExt::default(); let code = vec![ op::PUSH1, 0x20, @@ -458,7 +528,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::copy_from_slice(&code)), ..MessageExt::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), Some(InstrStop::Return)); assert_eq!(interpreter.output().len(), 32); @@ -468,7 +538,7 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_staticcall_child_callvalue_does_not_transfer() { - let caller = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0x11); let child = Address::with_last_byte(0x68); let beneficiary = Address::with_last_byte(0x69); let initial_beneficiary_balance = Word::from(7); @@ -502,7 +572,6 @@ mod tests { Precompiles::base(SpecId::CANCUN), ); host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); - let tx_env = TxEnvExt::default(); let code = vec![ op::PUSH1, 0x69, @@ -528,7 +597,7 @@ mod tests { code: Bytecode::new_legacy(Bytes::copy_from_slice(&code)), ..MessageExt::default() }; - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), Some(InstrStop::Stop)); let beneficiary_balance = host.read_account_info(&beneficiary).unwrap().unwrap().balance; @@ -538,32 +607,29 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_dynamic_call_to_staticcall_loop_matches_interpreter_gas() { - let caller = Address::from([0x11; 20]); + let caller = Address::repeat_byte(0x11); let outer = Address::with_last_byte(0xfd); let middle = Address::with_last_byte(0xed); let leaf = Address::with_last_byte(0xdd); let outer_code = dynamic_call_outer_code(); - let mut input = [0u8; 32]; - input[12..].copy_from_slice(middle.as_slice()); + let input = middle.into_word().0; let message = MessageExt { gas_limit: 0xcd79195900 - 21_368, destination: outer, caller, - input: Bytes::copy_from_slice(&input), + input: Bytes::from(input).into(), value: Word::from(10), code: Bytecode::new_legacy(Bytes::copy_from_slice(&outer_code)), code_address: outer, ..MessageExt::default() }; - let tx_env = TxEnvExt { gas_price: Word::from(10), ..TxEnvExt::default() }; - let run = |with_jit: bool| { let mut database = InMemoryDB::default(); database .insert_account_info(&caller, AccountInfo::default().with_balance(Word::from(100))); database .insert_account_info(&outer, AccountInfo::default().with_balance(Word::from(10))); - database.insert_account_storage(&outer, &Word::from(1), &Word::from(1)); + database.insert_account_storage(&outer, &Word::ONE, &Word::ONE); database.insert_account_info( &middle, AccountInfo::default() @@ -582,12 +648,13 @@ mod tests { database, Precompiles::base(SpecId::CANCUN), ); + host.tx_env_mut().gas_price = Word::from(10); host.state_mut().prewarm(&caller); host.state_mut().prewarm(&outer); if with_jit { host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); } - let mut interpreter = Interpreter::::new(&tx_env, &message); + let mut interpreter = Interpreter::::new(&message); let stop = if with_jit { run_interpreter(&backend, &mut interpreter, &mut host).unwrap() } else { @@ -604,6 +671,7 @@ mod tests { #[test] #[cfg(feature = "llvm")] fn compiled_precompile_dispatch_uses_original_callee() { + use alloy_primitives::bytes; let sha256 = Address::with_last_byte(2); let delegate = Address::with_last_byte(0x33); let parent = Address::with_last_byte(0x44); @@ -641,7 +709,7 @@ mod tests { call_target: parent, code_address: parent, caller, - input: hex!("01020304").into(), + input: bytes!("01020304").into(), code: Bytecode::new_legacy(code.into()), ..Default::default() }; @@ -669,8 +737,7 @@ mod tests { if with_jit { host.set_interpreter_runner(JitInterpreterRunner::new(blocking_backend())); } - Host::execute_message(&mut host, &TxEnvExt::default(), &mut message.clone()) - .unwrap() + Host::execute_message(&mut host, &mut message.clone()).unwrap() }; let interpreted = run(false); let compiled = run(true); @@ -710,6 +777,47 @@ mod tests { } } + #[test] + #[cfg(feature = "llvm")] + fn section_gas_too_large_to_compile_falls_back_to_interpreter() { + let spec = SpecId::CANCUN; + let mut version = Version::new(spec); + version.gas_params.set(GasId::ExpByte, u32::MAX); + let backend = JitBackend::new(RuntimeConfig { + enabled: true, + blocking: true, + gas_params: Some(version.gas_params), + ..RuntimeConfig::default() + }) + .unwrap(); + let mut host = Evm::::new_with_execution_config( + ExecutionConfig::for_spec_and_version(spec, version), + spec, + BlockEnv::::default(), + ethereum_tx_registry(spec), + InMemoryDB::default(), + Precompiles::base(spec), + ); + // `0 ** (2^256 - 1)`: the exponent's 32 bytes cost more than `u32::MAX` gas in one section. + let mut code = vec![op::PUSH32]; + code.extend([0xff; 32]); + code.extend([op::PUSH0, op::EXP, op::STOP]); + let expected_gas = 3 + 2 + 10 + 32 * u32::MAX as u64; + let message = MessageExt { + gas_limit: 2 * expected_gas, + code: Bytecode::new_legacy(Bytes::from(code)), + ..MessageExt::default() + }; + let mut interpreter = Interpreter::::new(&message); + + assert_eq!(run_interpreter(&backend, &mut interpreter, &mut host), None); + let stats = backend.stats(); + assert_eq!((stats.compilations_failed, stats.interpreter_fallbacks), (1, 1)); + + assert_eq!(interpreter.run(&mut host).unwrap(), InstrStop::Stop); + assert_eq!(interpreter.gas().spent(), expected_gas); + } + #[test] #[cfg(feature = "llvm")] fn compiled_sstore_stipend_uses_active_features() { @@ -779,16 +887,14 @@ mod tests { database .insert_account_info(&creator, AccountInfo::default().with_nonce(1)); database.insert_account_info(&callee, AccountInfo::default().with_nonce(1)); - let mut version = Version::new(spec); - version.initial_call_depth = initial_call_depth; - let mut host = Evm::::new_with_execution_config( - ExecutionConfig::for_spec_and_version(spec, version), + let mut host = Evm::::new( spec, BlockEnvExt::default(), ethereum_tx_registry(spec), database, Precompiles::base(spec), ); + host.set_initial_call_depth(initial_call_depth); if with_jit { host.set_interpreter_runner(JitInterpreterRunner::new(backend.clone())); } @@ -800,9 +906,7 @@ mod tests { code: code.clone(), ..Default::default() }; - let result = - Host::execute_message(&mut host, &TxEnvExt::default(), &mut message) - .unwrap(); + let result = Host::execute_message(&mut host, &mut message).unwrap(); let destination_journaled = host.state_mut().journal().iter().any(|entry| { matches!( @@ -821,9 +925,9 @@ mod tests { let returned = Word::from_be_slice(&compiled.0.output); if descendants == 1023 { let expected = if kind == MessageKind::Call { - Word::from(1) + Word::ONE } else { - Word::from_be_slice(destination.as_slice()) + address_to_word(&destination) }; assert_eq!(returned, expected); assert!(compiled.1); @@ -846,16 +950,17 @@ mod tests { #[cfg(all(test, feature = "llvm"))] mod custom_host_tests { - use super::*; + use super::{tests::blocking_backend, *}; use crate::runtime::{HostConfig, RuntimeConfig}; - use alloy_primitives::{Address, Bytes, U256}; + use alloy_primitives::{Address, B256, Bytes, U256}; use evm2::{ - BaseEvmConfig, Evm, EvmConfig, EvmConfigSelector, ExecutionConfig, OpcodeConfig, SpecId, + BaseEvmConfig, Evm, EvmConfig, EvmConfigSelector, ExecutionConfig, ExecutionError, + OpcodeConfig, SpecId, bytecode::Bytecode, config::BaseEvmConfigSelector, - env::{BlockEnvExt, TxEnvExt}, + env::BlockEnvExt, evm::{EmptyDB, precompile::NoPrecompiles}, - interpreter::{MessageExt, Word, instructions::sstore_with, op}, + interpreter::{MessageExt, MessageResult, Word, instructions::sstore_with, op}, registry::TxRegistry, }; use evm2_macros::instruction; @@ -892,14 +997,13 @@ mod custom_host_tests { EmptyDB::default(), NoPrecompiles::default(), ); - let tx = TxEnvExt::default(); // Exercise all direct message-field loads plus host and memory builtins. let message = MessageExt { gas_limit: 100_000, destination: Address::repeat_byte(0x11), caller: Address::repeat_byte(0x22), value: U256::from(0x33), - input: Bytes::from_static(&[0x44; 32]), + input: Bytes::from_static(&[0x44; 32]).into(), code: Bytecode::new_legacy(Bytes::from_static(&[ op::ADDRESS, op::PUSH0, @@ -928,20 +1032,38 @@ mod custom_host_tests { ])), ..Default::default() }; - let mut interpreted = Interpreter::::new(&tx, &message); + let mut interpreted = Interpreter::::new(&message); assert_eq!(interpreted.run(&mut host).unwrap(), InstrStop::Return); let expected_output = interpreted.output().to_vec(); let expected_gas = interpreted.gas(); - let mut compiled = Interpreter::::new(&tx, &message); + let mut compiled = Interpreter::::new(&message); assert_eq!(run_interpreter(&backend, &mut compiled, &mut host), Some(InstrStop::Return)); assert_eq!(compiled.output(), expected_output); assert_eq!(compiled.gas(), expected_gas); - let wrong = - JitBackend::new(RuntimeConfig { blocking: true, ..Default::default() }).unwrap(); - let mut fresh = Interpreter::::new(&tx, &message); + let wrong = blocking_backend(); + let mut fresh = Interpreter::::new(&message); assert_eq!(run_interpreter(&wrong, &mut fresh, &mut host), None); + let config = host.execution_config(); + let supports = |backend: &JitBackend| { + InterpreterRunner::::supports( + &JitInterpreterRunner::new(backend.clone()), + config, + ) + }; + assert!(supports(&backend)); + assert!(!supports(&wrong)); + assert_eq!( + InterpreterRunner::::run( + &JitInterpreterRunner::new(wrong.clone()), + &mut fresh, + &mut host, + ), + None + ); assert_eq!(fresh.run(&mut host).unwrap(), InstrStop::Return); assert_eq!(fresh.output(), expected_output); + assert_eq!(backend.stats().interpreter_fallbacks, 0); + assert_eq!(wrong.stats().interpreter_fallbacks, 2); } const SSTORE_SURCHARGE: u64 = 1_000; @@ -959,7 +1081,7 @@ mod custom_host_tests { cx.gas.spend(7)?; // Rewrites existing memory, which compiled code must not assume it still knows. if cx.state.memory().len() >= 32 { - cx.state.memory().set(0, &Word::from(2_u64).to_be_bytes::<32>()); + cx.state.memory().set(0, B256::with_last_byte(2).as_slice()); } *out = Word::from(0xbeef_u64); } @@ -989,6 +1111,18 @@ mod custom_host_tests { Ok(()) } + #[instruction(EvmTypes = TableTypes, no_stack_preamble)] + fn pushing_blockhash(_cx: _) -> Result { + // Keeps its operand, which the opcode's fixed inputs and outputs consume. + stack.push(Word::ZERO) + } + + #[instruction(EvmTypes = TableTypes, no_stack_preamble)] + fn memory_clearing_selfbalance(cx: _) -> Result { + cx.state.memory().clear(); + stack.push(Word::ZERO) + } + #[instruction(EvmTypes = TableTypes)] fn added(cx: _) -> out { *out = Word::from(0xdead_u64); @@ -1013,6 +1147,10 @@ mod custom_host_tests { config.set_instruction::(op::BALANCE, balance_gas); let extcodesize_gas = config.static_gas(op::EXTCODESIZE); config.set_instruction::(op::EXTCODESIZE, extcodesize_gas); + let blockhash_gas = config.static_gas(op::BLOCKHASH); + config.set_instruction::(op::BLOCKHASH, blockhash_gas); + let selfbalance_gas = config.static_gas(op::SELFBALANCE); + config.set_instruction::(op::SELFBALANCE, selfbalance_gas); config.set_instruction::(ADDED_OPCODE, 2); config.set_instruction::(op::DUPN, 2); config @@ -1069,20 +1207,47 @@ mod custom_host_tests { NoPrecompiles::default(), ) }; - let tx = Box::leak(Box::new(TxEnvExt::default())); let message = Box::leak(Box::new(MessageExt { gas_limit: 100_000, caller: Address::repeat_byte(0x11), code: Bytecode::new_legacy(Bytes::from_static(code)), ..Default::default() })); - let mut interpreted = Interpreter::::new(tx, message); + let mut interpreted = Interpreter::::new(message); let stop = interpreted.run(&mut new_host()).unwrap(); - let mut compiled = Interpreter::::new(tx, message); + let mut compiled = Interpreter::::new(message); let compiled_stop = run_interpreter(&backend, &mut compiled, &mut new_host()); (compiled_stop, compiled, stop, interpreted) } + /// Runs `code` as a message through a host that uses the compiled runner. + fn run_compiled_message( + host_config: HostConfig, + code: &'static [u8], + ) -> Result, ExecutionError> { + let backend = JitBackend::new(RuntimeConfig { + blocking: true, + single_error: false, + host: host_config, + ..Default::default() + }) + .unwrap(); + let mut host = Evm::::new( + SpecId::OSAKA, + BlockEnvExt::default(), + TxRegistry::new(), + EmptyDB::default(), + NoPrecompiles::default(), + ); + host.set_interpreter_runner(JitInterpreterRunner::new(backend)); + let mut message = MessageExt { + gas_limit: 100_000, + code: Bytecode::new_legacy(Bytes::from_static(code)), + ..Default::default() + }; + Host::execute_message(&mut host, &mut message) + } + fn table_host() -> HostConfig { let config = TableSelector::execution_config(SpecId::OSAKA); HostConfig::for_config::(&config) @@ -1190,12 +1355,32 @@ mod custom_host_tests { } #[test] - fn host_instruction_that_moves_the_program_counter_halts() { - // The interpreter skips the `POP` after the override, which compiled code cannot do. - let code = &[op::PUSH0, op::TLOAD, op::POP, op::STOP][..]; - let (compiled_stop, _, stop, _) = run_both(table_host(), code); - assert_eq!(stop, InstrStop::Stop); - assert_eq!(compiled_stop, Some(InstrStop::FatalExternalError)); + fn host_instruction_that_breaks_compiled_code_assumptions_fails() { + for (code, error) in [ + ( + // The interpreter skips the `POP` after the override, which compiled code cannot + // do. + &[op::PUSH0, op::TLOAD, op::POP, op::STOP][..], + "host instruction TLOAD (0x5c) at pc 1 moved the program counter, which compiled \ + code cannot follow", + ), + ( + &[op::PUSH0, op::BLOCKHASH, op::STOP][..], + "host instruction BLOCKHASH (0x40) at pc 1 left 2 stack items, but compiled code \ + expects 1 from the opcode's fixed inputs and outputs", + ), + ( + &[op::PUSH0, op::PUSH0, op::MSTORE, op::SELFBALANCE, op::STOP][..], + "host instruction SELFBALANCE (0x47) at pc 3 shrank memory from 32 to 0 bytes, \ + but compiled code expects memory never to shrink", + ), + ] { + let result = run_compiled_message(table_host(), code); + let Err(ExecutionError::Fatal(fatal)) = result else { + panic!("expected a fatal error, got {:?} for {code:?}", result.map(|r| r.stop)); + }; + assert_eq!(fatal.to_string(), error); + } } #[test] diff --git a/crates/jit/runtime/src/runtime/api.rs b/crates/jit/runtime/src/runtime/api.rs index 73ff1b8c4..abfcb6dd5 100644 --- a/crates/jit/runtime/src/runtime/api.rs +++ b/crates/jit/runtime/src/runtime/api.rs @@ -47,8 +47,8 @@ pub struct CompiledProgram { pub key: RuntimeCacheKey, /// Whether this is an AOT or JIT program. pub kind: ProgramKind, - /// Keeps the backing memory (shared library / JIT module) alive. - _backing: ProgramBacking, + /// Keeps the linked machine code alive. + _backing: Arc, } impl std::fmt::Debug for CompiledProgram { @@ -62,13 +62,13 @@ impl std::fmt::Debug for CompiledProgram { } impl CompiledProgram { - /// Creates a new compiled program backed by a loaded shared library. + /// Creates a new compiled program backed by a linked AOT object. pub(crate) const fn new_aot( key: RuntimeCacheKey, func: EvmCompilerFn, - library: Arc, + backing: Arc, ) -> Self { - Self { key, kind: ProgramKind::Aot, func, _backing: ProgramBacking::LoadedLibrary(library) } + Self { key, kind: ProgramKind::Aot, func, _backing: backing } } /// Creates a new compiled program backed by a JIT module's @@ -78,7 +78,7 @@ impl CompiledProgram { func: EvmCompilerFn, backing: Arc, ) -> Self { - Self { key, kind: ProgramKind::Jit, func, _backing: ProgramBacking::JitModule(backing) } + Self { key, kind: ProgramKind::Jit, func, _backing: backing } } } @@ -101,28 +101,3 @@ pub enum ProgramKind { /// Compiled at runtime by the JIT. Jit, } - -/// Backing storage that keeps compiled code alive. -#[expect(dead_code, reason = "variant fields are held alive for Drop")] -enum ProgramBacking { - /// A dynamically loaded shared library (AOT). - LoadedLibrary(Arc), - /// An ORCv2 ResourceTracker that owns JIT machine code. - /// Dropping the last reference calls `tracker.remove()`, freeing the code. - JitModule(Arc), -} - -/// Owns a loaded shared library. -/// -/// Dropping this unloads the library. -pub(crate) struct LoadedLibrary { - /// The loaded library. - _library: libloading::Library, -} - -impl LoadedLibrary { - /// Creates a new loaded library wrapper. - pub(crate) const fn new(library: libloading::Library) -> Self { - Self { _library: library } - } -} diff --git a/crates/jit/runtime/src/runtime/backend.rs b/crates/jit/runtime/src/runtime/backend.rs index ecb410c69..14d67ffb1 100644 --- a/crates/jit/runtime/src/runtime/backend.rs +++ b/crates/jit/runtime/src/runtime/backend.rs @@ -1,15 +1,17 @@ use crate::{ - EvmCompilerFn, eyre, + EvmCompilerFn, + eyre::{self, WrapErr}, runtime::{ LookupRequest, - api::{CompiledProgram, LoadedLibrary, ProgramKind}, + api::{CompiledProgram, ProgramKind}, config::{CompilationEvent, CompilationKind, RuntimeConfig, RuntimeTuning}, storage::{ - ArtifactKey, ArtifactManifest, ArtifactStore, RuntimeCacheKey, compile_config_hash, + ArtifactKey, ArtifactManifest, ArtifactStore, RuntimeCacheKey, StoredArtifact, + compile_config_hash, }, worker::{ AotSuccess, CompileJob, JitCodeBacking, JitObjectSuccess, SyncNotifier, WorkerPool, - WorkerResult, WorkerSuccess, + WorkerResult, WorkerSuccess, builtin_symbol_names, }, }, }; @@ -24,7 +26,7 @@ use quanta::Instant; #[cfg(feature = "llvm")] use std::ffi::CString; use std::{ - mem, + fs, mem, ops::ControlFlow, sync::{Arc, atomic::Ordering}, time::{SystemTime, UNIX_EPOCH}, @@ -64,33 +66,34 @@ fn jit_total_bytes() -> usize { } #[cfg(feature = "llvm")] -struct JitObjectLinker { +pub(crate) struct JitObjectLinker { backend: Option, /// Resolves the builtins of a custom host, which the helper leaves as symbols. resolver: Option usize>, } #[cfg(not(feature = "llvm"))] -struct JitObjectLinker; +pub(crate) struct JitObjectLinker; #[cfg(feature = "llvm")] impl JitObjectLinker { - const fn new(resolver: Option usize>) -> Self { + pub(crate) const fn new(resolver: Option usize>) -> Self { Self { backend: None, resolver } } fn link( &mut self, - success: &JitObjectSuccess, + symbol_name: &str, + object: &[u8], + builtin_symbols: &[String], ) -> eyre::Result<(EvmCompilerFn, Arc)> { let backend = match &mut self.backend { Some(backend) => backend, None => self.backend.insert(crate::EvmLlvmBackend::new(false)?), }; - let symbol_name = CString::new(success.symbol_name.clone())?; - let builtin_symbols = success - .builtin_symbols + let symbol_name = CString::new(symbol_name)?; + let builtin_symbols = builtin_symbols .iter() .map(|name| { // The helper suffixed the symbols with the resolver's address. @@ -107,11 +110,8 @@ impl JitObjectLinker { Ok((CString::new(name.as_str())?, addr)) }) .collect::>>()?; - let (addr, tracker, jd_guard) = backend.link_jit_object_in_fresh_dylib( - &symbol_name, - &success.object_bytes, - &builtin_symbols, - )?; + let (addr, tracker, jd_guard) = + backend.link_jit_object_in_fresh_dylib(&symbol_name, object, &builtin_symbols)?; let func = EvmCompilerFn::new(unsafe { std::mem::transmute::(addr) }); Ok((func, Arc::new(JitCodeBacking::new(tracker, jd_guard)))) @@ -120,18 +120,36 @@ impl JitObjectLinker { #[cfg(not(feature = "llvm"))] impl JitObjectLinker { - const fn new(_resolver: Option usize>) -> Self { + pub(crate) const fn new(_resolver: Option usize>) -> Self { Self } fn link( &mut self, - _success: &JitObjectSuccess, + _symbol_name: &str, + _object: &[u8], + _builtin_symbols: &[String], ) -> eyre::Result<(EvmCompilerFn, Arc)> { eyre::bail!("LLVM backend not available"); } } +impl JitObjectLinker { + /// Reads and links the object of a stored AOT artifact. + pub(crate) fn link_stored( + &mut self, + key: RuntimeCacheKey, + stored: &StoredArtifact, + ) -> eyre::Result { + let object = fs::read(&stored.object_path) + .wrap_err_with(|| format!("read {:?}", stored.object_path))?; + // The object may reference any builtin of the base host. + let (func, backing) = + self.link(&stored.manifest.symbol_name, &object, &builtin_symbol_names(None))?; + Ok(CompiledProgram::new_aot(key, func, backing)) + } +} + /// Commands sent to the backend thread on the bounded command channel. /// /// Lookup-observed events are NOT carried here — they go through the @@ -468,39 +486,25 @@ impl BackendState { ); false } - Ok(Some(stored)) => { - match (|| -> eyre::Result { - let library = unsafe { libloading::Library::new(&stored.dylib_path) } - .map_err(|e| eyre::eyre!("dlopen {:?}: {e}", stored.dylib_path))?; - let func: EvmCompilerFn = unsafe { - let sym: libloading::Symbol<'_, EvmCompilerFn> = - library.get(stored.manifest.symbol_name.as_bytes()).map_err(|e| { - eyre::eyre!("symbol '{}': {e}", stored.manifest.symbol_name) - })?; - *sym - }; - let library = Arc::new(LoadedLibrary::new(library)); - Ok(CompiledProgram::new_aot(*key, func, library)) - })() { - Ok(program) => { - debug!( - code_hash = %key.code_hash, - spec_id = ?key.spec_id, - "loaded existing AOT artifact from store, skipping recompilation", - ); - self.insert_resident(*key, Arc::new(program)); - true - } - Err(e) => { - warn!( - code_hash = %key.code_hash, - error = %e, - "failed to load persisted AOT artifact, will recompile", - ); - false - } + Ok(Some(stored)) => match self.jit_object_linker.link_stored(*key, &stored) { + Ok(program) => { + debug!( + code_hash = %key.code_hash, + spec_id = ?key.spec_id, + "loaded existing AOT artifact from store, skipping recompilation", + ); + self.insert_resident(*key, Arc::new(program)); + true } - } + Err(e) => { + warn!( + code_hash = %key.code_hash, + error = %e, + "failed to load persisted AOT artifact, will recompile", + ); + false + } + }, Ok(None) => false, Err(e) => { warn!( @@ -571,6 +575,7 @@ impl BackendState { current_gen = self.generation, "discarding stale worker result", ); + self.inner.stats.compilations_discarded.fetch_add(1, Ordering::Relaxed); result.sync_notifier.notify(); return; } @@ -646,7 +651,11 @@ impl BackendState { success: JitObjectSuccess, compile_duration: std::time::Duration, ) { - match self.jit_object_linker.link(&success) { + match self.jit_object_linker.link( + &success.symbol_name, + &success.object_bytes, + &success.builtin_symbols, + ) { Ok((func, backing)) => { let program = Arc::new(CompiledProgram::new_jit(key, func, backing)); self.insert_resident(key, program); @@ -678,13 +687,13 @@ impl BackendState { fn handle_aot_success(&mut self, key: RuntimeCacheKey, success: AotSuccess) { let artifact_key = self.artifact_key(key); - let content_hash = keccak256(&success.dylib_bytes).0; + let content_hash = keccak256(&success.object_bytes).0; let manifest = ArtifactManifest { artifact_key, symbol_name: success.symbol_name.clone(), bytecode_len: success.bytecode_len, - artifact_len: success.dylib_bytes.len(), + artifact_len: success.object_bytes.len(), created_at_unix_secs: SystemTime::now() .duration_since(UNIX_EPOCH) .map(|d| d.as_secs()) @@ -694,13 +703,13 @@ impl BackendState { // Persist to store if available. if let Some(store) = &self.store { - if let Err(e) = store.store(&artifact_key, &manifest, &success.dylib_bytes) { + if let Err(e) = store.store(&artifact_key, &manifest, &success.object_bytes) { warn!( code_hash = %key.code_hash, error = %e, "failed to persist AOT artifact", ); - self.remove_entry(&key); + self.fail_entry(&key, CompilationKind::Aot, false); self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); return; } @@ -708,55 +717,41 @@ impl BackendState { debug!( code_hash = %key.code_hash, spec_id = ?key.spec_id, - dylib_len = success.dylib_bytes.len(), + object_len = success.object_bytes.len(), "AOT artifact persisted to store", ); - // Load from store to get the canonical path, then dlopen. + // Load from store to get the canonical path, then link. match store.load(&artifact_key) { - Ok(Some(stored)) => { - match (|| -> eyre::Result { - let library = unsafe { libloading::Library::new(&stored.dylib_path) } - .map_err(|e| eyre::eyre!("dlopen {:?}: {e}", stored.dylib_path))?; - let func: EvmCompilerFn = unsafe { - let sym: libloading::Symbol<'_, EvmCompilerFn> = - library.get(success.symbol_name.as_bytes()).map_err(|e| { - eyre::eyre!("symbol '{}': {e}", success.symbol_name) - })?; - *sym - }; - let library = Arc::new(LoadedLibrary::new(library)); - Ok(CompiledProgram::new_aot(key, func, library)) - })() { - Ok(program) => { - self.insert_resident(key, Arc::new(program)); - self.remove_entry(&key); - self.inner.stats.compilations_succeeded.fetch_add(1, Ordering::Relaxed); - - debug!( - code_hash = %key.code_hash, - spec_id = ?key.spec_id, - "AOT program loaded into resident map", - ); - } - Err(e) => { - warn!( - code_hash = %key.code_hash, - error = %e, - "failed to load persisted AOT artifact", - ); - // Persisted successfully but couldn't load — remove so JIT can retry. - self.remove_entry(&key); - self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); - } + Ok(Some(stored)) => match self.jit_object_linker.link_stored(key, &stored) { + Ok(program) => { + self.insert_resident(key, Arc::new(program)); + self.remove_entry(&key); + self.inner.stats.compilations_succeeded.fetch_add(1, Ordering::Relaxed); + + debug!( + code_hash = %key.code_hash, + spec_id = ?key.spec_id, + "AOT program linked into resident map", + ); } - } + Err(e) => { + warn!( + code_hash = %key.code_hash, + error = %e, + "failed to link persisted AOT artifact", + ); + // Back off AOT attempts; admission still allows a JIT request to retry. + self.fail_entry(&key, CompilationKind::Aot, false); + self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); + } + }, Ok(None) => { warn!( code_hash = %key.code_hash, "stored AOT artifact not found on reload", ); - self.remove_entry(&key); + self.fail_entry(&key, CompilationKind::Aot, false); self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); } Err(e) => { @@ -765,7 +760,7 @@ impl BackendState { error = %e, "failed to reload persisted AOT artifact", ); - self.remove_entry(&key); + self.fail_entry(&key, CompilationKind::Aot, false); self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); } } @@ -775,7 +770,7 @@ impl BackendState { code_hash = %key.code_hash, "AOT compilation completed but no artifact store configured", ); - self.remove_entry(&key); + self.fail_entry(&key, CompilationKind::Aot, false); self.inner.stats.compilations_failed.fetch_add(1, Ordering::Relaxed); } } @@ -834,18 +829,13 @@ impl BackendState { } } - // Phase 3: enforce memory budget by evicting LRU JIT entries. + // Phase 3: enforce memory budget by evicting LRU entries. if budget > 0 && jit_total_bytes() > budget { - // Collect JIT entries sorted by last_hit_at ascending (oldest first). - // AOT entries are excluded because they don't contribute to `jit_total_bytes()`. - let mut entries: Vec<(RuntimeCacheKey, Instant)> = self - .resident_meta - .iter() - .filter(|(key, _)| { - self.inner.resident.get(key).is_some_and(|p| matches!(p.kind, ProgramKind::Jit)) - }) - .map(|(key, meta)| (*key, meta.last_hit_at)) - .collect(); + // Collect entries sorted by last_hit_at ascending (oldest first). AOT entries count + // too: their linked objects are part of `jit_total_bytes()`, and an evicted one + // reloads from the store. + let mut entries: Vec<(RuntimeCacheKey, Instant)> = + self.resident_meta.iter().map(|(key, meta)| (*key, meta.last_hit_at)).collect(); entries.sort_by_key(|(_, t)| *t); for (key, _) in entries { @@ -1582,7 +1572,7 @@ mod tests { let job = CompileJob { kind: CompilationKind::Jit, key: RuntimeCacheKey { code_hash: keccak256(BYTECODE_RET42), spec_id: SpecId::CANCUN }, - bytecode: Bytes::copy_from_slice(BYTECODE_RET42), + bytecode: Bytes::from_static(BYTECODE_RET42), symbol_name: symbol_name.to_owned(), opt_level: config.tuning.jit_opt_level, sync_notifier: SyncNotifier::none(), @@ -1598,16 +1588,13 @@ mod tests { #[test] fn jit_object_linker_relinks_live_symbol_name() { let success = compile_jit_object("jit_duplicate_symbol"); - let success2 = JitObjectSuccess { - symbol_name: success.symbol_name.clone(), - object_bytes: success.object_bytes.clone(), - builtin_symbols: success.builtin_symbols.clone(), - }; let mut linker = JitObjectLinker::new(None); - let (_first_func, _first_backing) = linker.link(&success).unwrap(); - let (_second_func, _second_backing) = linker - .link(&success2) + let link = |linker: &mut JitObjectLinker| { + linker.link(&success.symbol_name, &success.object_bytes, &success.builtin_symbols) + }; + let (_first_func, _first_backing) = link(&mut linker).unwrap(); + let (_second_func, _second_backing) = link(&mut linker) .expect("same symbol should link while previous backing remains alive"); } } diff --git a/crates/jit/runtime/src/runtime/config.rs b/crates/jit/runtime/src/runtime/config.rs index 58fd4b33a..1156c7004 100644 --- a/crates/jit/runtime/src/runtime/config.rs +++ b/crates/jit/runtime/src/runtime/config.rs @@ -62,7 +62,10 @@ pub struct RuntimeConfig { /// this lets LLVM DCE the per-failure-site materialization and the failure-block phi. /// Successful exits (`STOP`/`RETURN`/`REVERT`) keep their original codes. /// - /// Useful for benchmarking the cost of failure-result materialization. + /// This is a deliberate trade-off: the compiled function reports `OutOfGas` where the + /// interpreter reports the real halt reason (for example, an invalid jump). Preserving the + /// reasons (`false`) makes the JIT 4.0% larger, compile 10.7% more instructions, and + /// execute 3.8% more instructions in total across the benchmarks (`onchain_lm_v2` +4.3%). /// /// Defaults to `true`. pub single_error: bool, @@ -119,14 +122,19 @@ pub struct RuntimeConfig { /// Defaults to `None`. pub jit_helper_path: Option, - /// Blocking mode: every lookup synchronously JIT-compiles on miss and never - /// falls back to the interpreter. + /// Blocking mode: every lookup synchronously JIT-compiles on miss. /// /// When `true`, [`lookup()`](super::JitBackend::lookup) behaves like /// [`lookup_blocking()`](super::JitBackend::lookup_blocking): if the /// compiled function is not already resident, the calling thread blocks - /// until JIT compilation completes. This implies `enabled = true` and - /// `jit_hot_threshold = 0`. + /// until JIT compilation completes. This implies `enabled = true`, + /// `jit_hot_threshold = 0`, and `jit_max_bytecode_len = 0` (no size limit), so every + /// non-empty frame is compiled. + /// + /// A frame still runs on the interpreter when compilation fails, times out + /// (`sync_compile_timeout`), is dropped because the pending-job cap or the worker pool is + /// full, or the program is evicted before use. Each such frame is counted in + /// [`interpreter_fallbacks`](super::RuntimeStatsSnapshot::interpreter_fallbacks). /// /// Intended for debugging and testing only — not for production use. /// @@ -162,7 +170,7 @@ pub struct CompilationEvent { pub enum CompilationKind { /// JIT compilation (in-memory function pointer). Jit, - /// AOT compilation (shared library artifact). + /// AOT compilation (relocatable object artifact). Aot, } diff --git a/crates/jit/runtime/src/runtime/mod.rs b/crates/jit/runtime/src/runtime/mod.rs index 06f4c290a..7a40060a8 100644 --- a/crates/jit/runtime/src/runtime/mod.rs +++ b/crates/jit/runtime/src/runtime/mod.rs @@ -5,13 +5,11 @@ //! - Fire-and-forget lookup-observed events to the backend thread. //! - Background JIT compilation for hot keys (threshold-based promotion). -use crate::{ - EvmCompilerFn, - eyre::{self, WrapErr}, -}; +use crate::eyre::{self, WrapErr}; use alloy_primitives::B256; -use api::LoadedLibrary; -use backend::{Command, CompileJitRequest, EventQueue, PrepareAotRequest, ResidentMap}; +use backend::{ + Command, CompileJitRequest, EventQueue, JitObjectLinker, PrepareAotRequest, ResidentMap, +}; use crossbeam_channel as chan; use crossbeam_queue::ArrayQueue; use evm2::SpecId; @@ -107,13 +105,9 @@ pub(crate) struct BackendInner { enabled: AtomicBool, /// Whether the backend thread has been spawned and is draining the command channel. started: AtomicBool, - /// Blocking mode: every lookup synchronously compiles and never falls back. + /// Blocking mode: every lookup synchronously compiles and counts each fallback. blocking: bool, host: config::HostConfig, - /// Key of the last opcode config and spec that matched the host's opcode table. - verified_opcodes: std::sync::atomic::AtomicU64, - /// Key of the last opcode config and spec that did not match the host's opcode table. - rejected_opcodes: std::sync::atomic::AtomicU64, gas_params: Option, /// Tuning knobs (Copy). Cached for hot-path eligibility checks. tuning: crate::runtime::config::RuntimeTuning, @@ -179,21 +173,22 @@ impl JitBackend { if config.blocking { config.enabled = true; config.tuning.jit_hot_threshold = 0; + config.tuning.jit_max_bytecode_len = 0; } #[cfg(not(unix))] if config.jit_mode == JitMode::OutOfProcess { eyre::bail!("out-of-process JIT is only available on Unix"); } - if config.aot && config.store.is_none() { - eyre::bail!("AOT mode requires an artifact store"); - } - if config.aot && config.jit_mode == JitMode::OutOfProcess { - eyre::bail!("AOT mode does not support out-of-process compilation"); - } + eyre::ensure!(!config.aot || config.store.is_some(), "AOT mode requires an artifact store"); + eyre::ensure!( + !config.aot || config.jit_mode != JitMode::OutOfProcess, + "AOT mode does not support out-of-process compilation" + ); - if !config.host.is_default() && (config.aot || config.store.is_some()) { - eyre::bail!("custom hosts require JIT without an artifact store"); - } + eyre::ensure!( + config.host.is_default() || (!config.aot && config.store.is_none()), + "custom hosts require JIT without an artifact store" + ); let enabled = config.enabled; let (tx, rx) = chan::bounded::(config.tuning.channel_capacity); let events = ArrayQueue::new(config.tuning.channel_capacity); @@ -211,8 +206,6 @@ impl JitBackend { started: AtomicBool::new(false), blocking: config.blocking, host: config.host.clone(), - verified_opcodes: std::sync::atomic::AtomicU64::new(0), - rejected_opcodes: std::sync::atomic::AtomicU64::new(0), gas_params: config.gas_params, tx, thread: std::sync::Mutex::new(None), @@ -225,46 +218,73 @@ impl JitBackend { Ok(this) } + /// Counts a frame that ran on the interpreter because the runtime could not run it. + pub(crate) fn count_interpreter_fallback(&self) { + self.inner.shared.stats.interpreter_fallbacks.fetch_add(1, Ordering::Relaxed); + } + + /// Checks that generated code uses the field layout of the interpreter type `T`. + #[inline] + pub(crate) fn supports_host_type(&self) -> bool { + self.inner.host.type_id == std::any::TypeId::of::() + } + /// Checks that generated code matches the interpreter type, gas schedule, and opcode table. + /// + /// An enabled backend counts a rejection as an interpreter fallback and logs the reason, + /// because the caller then runs every frame on the interpreter. Compiled code is not keyed + /// by gas schedule, so a host whose schedule differs from the backend's is never compiled + /// for. pub(crate) fn supports_host( &self, config: &evm2::ExecutionConfig, ) -> bool { - let host = &self.inner.host; - host.type_id == std::any::TypeId::of::() - && config.version().gas_params - == *self - .inner - .gas_params - .as_ref() - .unwrap_or(&evm2::Version::base(config.base_spec_id()).gas_params) - && self.opcodes_match(config) + let Some(reason) = self.host_mismatch(config) else { return true }; + if self.enabled() { + self.count_interpreter_fallback(); + warn!( + spec_id = ?config.base_spec_id(), + reason, + "host unsupported, running the interpreter", + ); + } + false + } + + /// Returns why generated code does not match `config`, or `None` if it does. + pub(crate) fn host_mismatch( + &self, + config: &evm2::ExecutionConfig, + ) -> Option<&'static str> { + if !self.supports_host_type::() { + return Some("host type differs from the one the backend was built for"); + } + let gas_params = self + .inner + .gas_params + .as_ref() + .unwrap_or(&evm2::Version::base(config.base_spec_id()).gas_params); + if config.version().gas_params != *gas_params { + return Some("gas schedule differs from the one the backend compiles with"); + } + if !self.opcodes_match(config) { + return Some("opcode table differs from the one the backend compiles with"); + } + None } /// Checks the active opcode config against the host's opcode table. fn opcodes_match(&self, config: &evm2::ExecutionConfig) -> bool { - let active = config.opcode_config(); - // The verdict depends on the base spec too. Addresses leave the top byte free. - let address = - active as *const evm2::OpcodeConfig as u64 | (config.base_spec_id() as u64) << 56; - if self.inner.verified_opcodes.load(Ordering::Relaxed) == address { - return true; - } - if self.inner.rejected_opcodes.load(Ordering::Relaxed) == address { - return false; - } - let matches = match &self.inner.host.opcodes { + match &self.inner.host.opcodes { None => config.has_base_opcode_table(), Some(opcodes) => { // SAFETY: The caller checked that the host type is `T`, so `opcodes.config` is a // `&'static OpcodeConfig`. - active.same_table(unsafe { &*(opcodes.config as *const evm2::OpcodeConfig) }) + config + .opcode_config() + .same_table(unsafe { &*(opcodes.config as *const evm2::OpcodeConfig) }) } - }; - let cache = - if matches { &self.inner.verified_opcodes } else { &self.inner.rejected_opcodes }; - cache.store(address, Ordering::Relaxed); - matches + } } /// Looks up a compiled function for the given request. @@ -331,8 +351,18 @@ impl JitBackend { /// If the function is already compiled, returns it immediately. Otherwise, enqueues /// a synchronous JIT compilation request and blocks until the result is available. /// Returns [`LookupDecision::Unavailable`] if the bytecode is ineligible for - /// compilation (see [`RuntimeTuning::should_compile`]) or compilation fails. + /// compilation (see [`RuntimeTuning::should_compile`]) or compilation does not produce a + /// resident program. Each such miss is counted in + /// [`interpreter_fallbacks`](RuntimeStatsSnapshot::interpreter_fallbacks). pub fn lookup_blocking(&self, req: LookupRequest) -> LookupDecision { + let decision = self.lookup_blocking_inner(req); + if let LookupDecision::Unavailable(_) = decision { + self.count_interpreter_fallback(); + } + decision + } + + fn lookup_blocking_inner(&self, req: LookupRequest) -> LookupDecision { if !self.inner.tuning.should_compile(&req.code) { return LookupDecision::Unavailable(LookupMissReason::Ineligible); } @@ -657,6 +687,7 @@ impl JitBackend { info!(count = artifacts.len(), "loading AOT artifacts"); let config_hash = storage::compile_config_hash(config); + let mut linker = JitObjectLinker::new(config.host.resolver); let mut out: Vec<(RuntimeCacheKey, Arc)> = Vec::with_capacity(artifacts.len()); let mut loaded = 0u64; @@ -677,7 +708,7 @@ impl JitBackend { continue; } - match Self::load_artifact(&artifact_key, &stored) { + match linker.link_stored(key, &stored) { Ok(program) => { out.push((key, Arc::new(program))); loaded += 1; @@ -697,22 +728,6 @@ impl JitBackend { Ok(out) } - /// Loads a single artifact: `dlopen`s the dylib path and resolves the symbol. - fn load_artifact(key: &ArtifactKey, stored: &StoredArtifact) -> eyre::Result { - let library = unsafe { libloading::Library::new(&stored.dylib_path) } - .wrap_err_with(|| format!("dlopen {:?}", stored.dylib_path))?; - - let func: EvmCompilerFn = unsafe { - let sym: libloading::Symbol<'_, EvmCompilerFn> = library - .get(stored.manifest.symbol_name.as_bytes()) - .wrap_err_with(|| format!("symbol '{}'", stored.manifest.symbol_name))?; - *sym - }; - - let library = Arc::new(LoadedLibrary::new(library)); - Ok(CompiledProgram::new_aot(key.runtime, func, library)) - } - /// Applies a clear before startup, or queues it after the backend takes ownership. /// The startup lock serializes this decision with AOT preload and thread creation. fn send_clear(&self, command: Command) { diff --git a/crates/jit/runtime/src/runtime/out_of_process.rs b/crates/jit/runtime/src/runtime/out_of_process.rs index a7356ebf5..05eb6aa9a 100644 --- a/crates/jit/runtime/src/runtime/out_of_process.rs +++ b/crates/jit/runtime/src/runtime/out_of_process.rs @@ -19,6 +19,7 @@ use evm2::{ version::{GasId, GasParams}, }; use evm2_jit_codegen::HostLayout; +use eyre::OptionExt; use rayon::ThreadPoolBuilder; use std::{ cell::RefCell, @@ -699,14 +700,18 @@ struct HelperPauseState { impl HelperPauseState { fn pause(&self) { - *self.paused.lock().unwrap() = true; + self.set_paused(true); } fn resume(&self) { - *self.paused.lock().unwrap() = false; + self.set_paused(false); self.resumed.notify_all(); } + fn set_paused(&self, paused: bool) { + *self.paused.lock().unwrap() = paused; + } + fn wait_resumed(&self) { let mut paused = self.paused.lock().unwrap(); while *paused { @@ -793,8 +798,7 @@ fn read_helper_request(stdin: &mut BufReader) -> eyre::Resu eyre::bail!("JIT helper received duplicate init"); } }; - let spec_id = SpecId::try_from_u32(u32::from(req.spec_id)) - .ok_or_else(|| eyre::eyre!("invalid spec id"))?; + let spec_id = SpecId::try_from_u32(u32::from(req.spec_id)).ok_or_eyre("invalid spec id")?; let opt_level = opt_level_from_u8(req.opt_level)?; let job = CompileJob { @@ -837,7 +841,7 @@ fn write_pause_ack(stdout: &mut BufWriter, id: u64) -> eyr fn write_message(w: &mut BufWriter, message: &T) -> std::io::Result<()> where - T: wincode::SchemaWrite, + T: SchemaWrite, { wincode::serialize_into(w, message) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) @@ -896,9 +900,7 @@ fn gas_params_to_pairs(gas_params: &GasParams) -> GasParamPairs { } fn gas_params_from_pairs(pairs: GasParamPairs) -> eyre::Result { - if pairs.len() != GAS_PARAM_COUNT { - eyre::bail!("invalid gas params length: {}", pairs.len()); - } + eyre::ensure!(pairs.len() == GAS_PARAM_COUNT, "invalid gas params length: {}", pairs.len()); let mut seen = [false; GAS_PARAM_COUNT]; let mut gas_params = Version::base(SpecId::default()).gas_params; @@ -907,9 +909,7 @@ fn gas_params_from_pairs(pairs: GasParamPairs) -> eyre::Result { let Some(gas_id) = GasId::from_usize(index) else { eyre::bail!("invalid gas param id: {id}"); }; - if seen[index] { - eyre::bail!("duplicate gas param id: {id}"); - } + eyre::ensure!(!seen[index], "duplicate gas param id: {id}"); seen[index] = true; gas_params.set(gas_id, value); } diff --git a/crates/jit/runtime/src/runtime/stats.rs b/crates/jit/runtime/src/runtime/stats.rs index ebfaa0d8b..1d8d00f5d 100644 --- a/crates/jit/runtime/src/runtime/stats.rs +++ b/crates/jit/runtime/src/runtime/stats.rs @@ -11,6 +11,8 @@ pub(crate) struct RuntimeStats { pub(crate) lookup_misses: AtomicU64, /// Total lookup events dropped due to event-queue overflow. pub(crate) events_dropped: AtomicU64, + /// Total frames that fell back to the interpreter although the runtime is enabled. + pub(crate) interpreter_fallbacks: AtomicU64, /// Total control commands dropped because the command channel was full. pub(crate) commands_dropped: AtomicU64, /// Total number of entries evicted (idle + budget). @@ -43,6 +45,8 @@ pub(crate) struct RuntimeStats { pub(crate) jit_helper_resume_requests: AtomicU64, /// Total number of out-of-process JIT helper resume request failures. pub(crate) jit_helper_resume_failures: AtomicU64, + /// Total compilation results discarded because the resident cache was cleared. + pub(crate) compilations_discarded: AtomicU64, } /// Gauge values sampled at snapshot time. @@ -62,6 +66,20 @@ pub struct RuntimeStatsSnapshot { pub lookup_misses: u64, /// Total lookup events dropped due to event-queue overflow. pub events_dropped: u64, + /// Total frames that fell back to the interpreter although the runtime is enabled. + /// + /// Counted in [`blocking`](super::RuntimeConfig::blocking) mode, where every lookup that does + /// not return a compiled function is a fallback: a failed or timed-out compilation, a full + /// pending-job cap, a saturated worker pool, a program evicted before use, or empty bytecode. + /// A failed compilation is also counted once in `compilations_failed`; this counter counts + /// every frame, including later frames of the same failed code. + /// + /// Also counts frames whose gas exceeds `i64::MAX`, and hosts that the runtime rejects + /// because their type, gas schedule, or opcode table differs from the compiled code's. A + /// rejected host is counted once per support check of the runner, not once per frame, because + /// the `Evm` then stops asking the runtime to run frames. `run_interpreter` counts it on every + /// call. + pub interpreter_fallbacks: u64, /// Total control commands dropped because the command channel was full. /// /// Pause/resume commands are best-effort and dropped instead of blocking the caller when @@ -115,6 +133,8 @@ pub struct RuntimeStatsSnapshot { pub jit_helper_resume_requests: u64, /// Total number of out-of-process JIT helper resume request failures. pub jit_helper_resume_failures: u64, + /// Total compilation results discarded because the resident cache was cleared while they ran. + pub compilations_discarded: u64, } impl RuntimeStatsSnapshot { @@ -136,12 +156,15 @@ impl RuntimeStats { let dispatched = self.compilations_dispatched.load(Ordering::Relaxed); let succeeded = self.compilations_succeeded.load(Ordering::Relaxed); let failed = self.compilations_failed.load(Ordering::Relaxed); - let pending_jobs = dispatched.saturating_sub(succeeded.saturating_add(failed)); + let discarded = self.compilations_discarded.load(Ordering::Relaxed); + let pending_jobs = + dispatched.saturating_sub(succeeded.saturating_add(failed).saturating_add(discarded)); RuntimeStatsSnapshot { lookup_hits: self.lookup_hits.load(Ordering::Relaxed), lookup_misses: self.lookup_misses.load(Ordering::Relaxed), events_dropped: self.events_dropped.load(Ordering::Relaxed), + interpreter_fallbacks: self.interpreter_fallbacks.load(Ordering::Relaxed), commands_dropped: self.commands_dropped.load(Ordering::Relaxed), resident_entries: gauges.resident_entries, events_queued: gauges.events_queued, @@ -166,6 +189,7 @@ impl RuntimeStats { jit_helper_pause_timeouts: self.jit_helper_pause_timeouts.load(Ordering::Relaxed), jit_helper_resume_requests: self.jit_helper_resume_requests.load(Ordering::Relaxed), jit_helper_resume_failures: self.jit_helper_resume_failures.load(Ordering::Relaxed), + compilations_discarded: discarded, } } } diff --git a/crates/jit/runtime/src/runtime/storage.rs b/crates/jit/runtime/src/runtime/storage.rs index cbcbfc8fd..96a06e666 100644 --- a/crates/jit/runtime/src/runtime/storage.rs +++ b/crates/jit/runtime/src/runtime/storage.rs @@ -4,10 +4,15 @@ use crate::{OptimizationLevel, eyre, runtime::RuntimeConfig}; use alloy_primitives::{B256, keccak256}; use dashmap::DashMap; use evm2::{SpecId, version::GasId}; -use std::{fs, io::Write, path::PathBuf}; +use std::{ + array, fs, + hash::{Hash, Hasher}, + io::Write, + path::PathBuf, +}; /// Runtime cache key: the minimal identity for a compiled program at runtime. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct RuntimeCacheKey { /// The code hash of the contract bytecode. pub code_hash: B256, @@ -15,6 +20,17 @@ pub struct RuntimeCacheKey { pub spec_id: SpecId, } +impl Hash for RuntimeCacheKey { + /// Feeds the hasher one word: the XOR of the four code hash words and the spec id. + #[inline] + fn hash(&self, state: &mut H) { + let [a, b, c, d] = array::from_fn(|i| { + u64::from_le_bytes(self.code_hash[i * 8..i * 8 + 8].try_into().unwrap()) + }); + state.write_u64(a ^ b ^ c ^ d ^ self.spec_id as u64); + } +} + /// Full artifact identity for persisted artifacts. /// /// Persisted artifacts must match all fields of this key to be loaded. @@ -73,13 +89,13 @@ pub(crate) fn compile_config_hash(config: &RuntimeConfig) -> B256 { keccak256(bytes) } -/// A stored artifact consisting of a manifest and a path to the compiled dylib. +/// A stored artifact consisting of a manifest and a path to the relocatable object. #[derive(Clone, Debug)] pub struct StoredArtifact { /// Metadata about the artifact. pub manifest: ArtifactManifest, - /// Path to the shared library on disk. The store owns and manages these files. - pub dylib_path: PathBuf, + /// Path to the relocatable object on disk. The store owns and manages these files. + pub object_path: PathBuf, } /// Metadata for a stored artifact. @@ -87,7 +103,7 @@ pub struct StoredArtifact { pub struct ArtifactManifest { /// The full artifact key. pub artifact_key: ArtifactKey, - /// The symbol name to look up in the loaded library. + /// The symbol name of the compiled function in the object. pub symbol_name: String, /// Length of the original bytecode. pub bytecode_len: usize, @@ -95,7 +111,7 @@ pub struct ArtifactManifest { pub artifact_len: usize, /// Creation timestamp (unix seconds). pub created_at_unix_secs: u64, - /// Keccak-256 digest of the dylib bytes. + /// Keccak-256 digest of the object bytes. pub content_hash: [u8; 32], } @@ -111,8 +127,8 @@ pub enum BackendSelection { /// Trait for loading and storing compiled artifacts. /// -/// Implementations manage artifact files on the filesystem. The store owns the dylib files and -/// returns paths to them. The backend loads shared libraries directly from these paths. +/// Implementations manage artifact files on the filesystem. The store owns the object files and +/// returns paths to them. The backend links the objects in process from these paths. pub trait ArtifactStore: Send + Sync + 'static { /// Loads all available artifacts from storage. fn load_all(&self) -> eyre::Result>; @@ -120,13 +136,13 @@ pub trait ArtifactStore: Send + Sync + 'static { /// Loads a single artifact by key. fn load(&self, key: &ArtifactKey) -> eyre::Result>; - /// Stores an artifact. The `dylib_bytes` are the raw shared-library bytes to persist. + /// Stores an artifact. The `object_bytes` are the raw relocatable object bytes to persist. /// The store writes them to disk and the returned path (via subsequent `load`) points there. fn store( &self, key: &ArtifactKey, manifest: &ArtifactManifest, - dylib_bytes: &[u8], + object_bytes: &[u8], ) -> eyre::Result<()>; /// Deletes an artifact by key. @@ -136,7 +152,7 @@ pub trait ArtifactStore: Send + Sync + 'static { fn clear(&self) -> eyre::Result<()>; } -/// In-memory artifact index backed by dylib files in a temporary directory. +/// In-memory artifact index backed by object files in a temporary directory. #[derive(Debug)] pub struct RuntimeArtifactStore { dir: tempfile::TempDir, @@ -161,7 +177,7 @@ impl RuntimeArtifactStore { fn artifact_path(&self, key: &ArtifactKey) -> PathBuf { self.dir.path().join(format!( - "{:x}_{:?}_{:?}_{:?}_v{}_{:x}.so", + "{:x}_{:?}_{:?}_{:?}_v{}_{:x}.o", key.runtime.code_hash, key.runtime.spec_id, key.backend, @@ -185,28 +201,29 @@ impl ArtifactStore for RuntimeArtifactStore { &self, key: &ArtifactKey, manifest: &ArtifactManifest, - dylib_bytes: &[u8], + object_bytes: &[u8], ) -> eyre::Result<()> { let path = self.artifact_path(key); - // Write to a unique file and rename it over the target: a previous artifact for the key - // may be mapped, and rewriting it in place would corrupt the mapping. + // Write to a unique file and rename it over the target, so a reader never sees a partial + // artifact. let mut file = tempfile::NamedTempFile::new_in(self.dir.path())?; - file.write_all(dylib_bytes)?; + file.write_all(object_bytes)?; file.persist(&path).map_err(|err| err.error)?; self.artifacts - .insert(*key, StoredArtifact { manifest: manifest.clone(), dylib_path: path }); + .insert(*key, StoredArtifact { manifest: manifest.clone(), object_path: path }); Ok(()) } fn delete(&self, key: &ArtifactKey) -> eyre::Result<()> { if let Some((_, artifact)) = self.artifacts.remove(key) { - let _ = fs::remove_file(artifact.dylib_path); + let _ = fs::remove_file(artifact.object_path); } Ok(()) } fn clear(&self) -> eyre::Result<()> { - let paths = self.artifacts.iter().map(|entry| entry.dylib_path.clone()).collect::>(); + let paths = + self.artifacts.iter().map(|entry| entry.object_path.clone()).collect::>(); self.artifacts.clear(); for path in paths { let _ = fs::remove_file(path); @@ -227,13 +244,11 @@ mod tests { use std::io::Read; fn artifact_key(code_hash: B256) -> ArtifactKey { - ArtifactKey { - runtime: RuntimeCacheKey { code_hash, spec_id: SpecId::OSAKA }, - backend: BackendSelection::Llvm, - opt_level: OptimizationLevel::Default, - abi_version: crate::ABI_VERSION, - config_hash: B256::ZERO, - } + ArtifactKey::llvm( + RuntimeCacheKey { code_hash, spec_id: SpecId::OSAKA }, + OptimizationLevel::Default, + B256::ZERO, + ) } fn manifest(key: ArtifactKey, artifact_len: usize) -> ArtifactManifest { @@ -262,13 +277,13 @@ mod tests { let key = artifact_key(B256::with_last_byte(1)); let manifest = manifest(key, 4); - store.store(&key, &manifest, b"dylib").unwrap(); + store.store(&key, &manifest, b"object").unwrap(); assert_eq!(store.len(), 1); let loaded = store.load(&key).unwrap().unwrap(); assert_eq!(loaded.manifest.symbol_name, "main"); assert_eq!(loaded.manifest.artifact_len, 4); - assert_eq!(std::fs::read(&loaded.dylib_path).unwrap(), b"dylib"); + assert_eq!(std::fs::read(&loaded.object_path).unwrap(), b"object"); let all = store.load_all().unwrap(); assert_eq!(all.len(), 1); @@ -287,7 +302,7 @@ mod tests { assert_eq!(store.len(), 1); let loaded = store.load(&key).unwrap().unwrap(); assert_eq!(loaded.manifest.artifact_len, 5); - assert_eq!(std::fs::read(&loaded.dylib_path).unwrap(), b"three"); + assert_eq!(std::fs::read(&loaded.object_path).unwrap(), b"three"); } #[test] @@ -296,7 +311,7 @@ mod tests { let key = artifact_key(B256::with_last_byte(5)); store.store(&key, &manifest(key, 3), b"one").unwrap(); - let mut open = std::fs::File::open(store.load(&key).unwrap().unwrap().dylib_path).unwrap(); + let mut open = std::fs::File::open(store.load(&key).unwrap().unwrap().object_path).unwrap(); store.store(&key, &manifest(key, 5), b"three").unwrap(); let mut contents = Vec::new(); @@ -312,8 +327,8 @@ mod tests { store.store(&first, &manifest(first, 1), b"a").unwrap(); store.store(&second, &manifest(second, 1), b"b").unwrap(); - let first_path = store.load(&first).unwrap().unwrap().dylib_path; - let second_path = store.load(&second).unwrap().unwrap().dylib_path; + let first_path = store.load(&first).unwrap().unwrap().object_path; + let second_path = store.load(&second).unwrap().unwrap().object_path; store.delete(&first).unwrap(); assert!(store.load(&first).unwrap().is_none()); diff --git a/crates/jit/runtime/src/runtime/tests.rs b/crates/jit/runtime/src/runtime/tests.rs index c8f1f314b..393eda012 100644 --- a/crates/jit/runtime/src/runtime/tests.rs +++ b/crates/jit/runtime/src/runtime/tests.rs @@ -59,13 +59,28 @@ impl TestBackend { Self::new(RuntimeConfig { enabled: true, tuning, ..Default::default() }) } + /// Creates a backend with `enabled: false` and the given tuning overrides. + fn disabled_with_tuning(tuning: RuntimeTuning) -> Self { + Self::new(RuntimeConfig { enabled: false, tuning, ..Default::default() }) + } + + /// Creates a backend with `blocking: true` and the given tuning overrides. + fn blocking_with_tuning(tuning: RuntimeTuning) -> Self { + Self::new(RuntimeConfig { blocking: true, tuning, ..Default::default() }) + } + + /// Creates a backend with `enabled: true`, the given store, and the given tuning overrides. + #[cfg(feature = "llvm")] + fn with_store(store: Arc, tuning: RuntimeTuning) -> Self { + Self::new(RuntimeConfig { enabled: true, store: Some(store), tuning, ..Default::default() }) + } + /// Creates a backend with `enabled: true`, 1 worker, and the given tuning overrides. fn with_tuning_1w(tuning: RuntimeTuning) -> Self { Self::with_tuning(RuntimeTuning { jit_worker_count: 1, ..tuning }) } /// Creates a backend with `enabled: true`, 1 worker, and default tuning. - #[cfg(feature = "llvm")] fn default_1w() -> Self { Self::with_tuning_1w(RuntimeTuning::default()) } @@ -164,7 +179,7 @@ impl ArtifactStore for EmptyStore { &self, _key: &ArtifactKey, _manifest: &ArtifactManifest, - _dylib_bytes: &[u8], + _object_bytes: &[u8], ) -> eyre::Result<()> { Ok(()) } @@ -194,7 +209,7 @@ impl ArtifactStore for FailingStore { &self, _key: &ArtifactKey, _manifest: &ArtifactManifest, - _dylib_bytes: &[u8], + _object_bytes: &[u8], ) -> eyre::Result<()> { Ok(()) } @@ -226,7 +241,7 @@ impl ArtifactStore for CountingStore { &self, _key: &ArtifactKey, _manifest: &ArtifactManifest, - _dylib_bytes: &[u8], + _object_bytes: &[u8], ) -> eyre::Result<()> { Ok(()) } @@ -241,6 +256,74 @@ impl ArtifactStore for CountingStore { } } +#[cfg(feature = "llvm")] +#[derive(Clone, Copy, Debug)] +enum AotFailure { + Persist, + ReloadMissing, + ReloadError, + Link, + NoStore, +} + +#[cfg(feature = "llvm")] +struct AotFailureStore { + inner: RuntimeArtifactStore, + failure: AotFailure, +} + +#[cfg(feature = "llvm")] +impl ArtifactStore for AotFailureStore { + fn load_all(&self) -> eyre::Result> { + self.inner.load_all() + } + + fn load(&self, key: &ArtifactKey) -> eyre::Result> { + let Some(mut stored) = self.inner.load(key)? else { return Ok(None) }; + match self.failure { + AotFailure::ReloadMissing => return Ok(None), + AotFailure::ReloadError => eyre::bail!("injected AOT reload failure"), + // A malformed symbol makes linking fail without handing invalid object bytes to LLVM. + AotFailure::Link => stored.manifest.symbol_name.push('\0'), + _ => {} + } + Ok(Some(stored)) + } + + fn store( + &self, + key: &ArtifactKey, + manifest: &ArtifactManifest, + object_bytes: &[u8], + ) -> eyre::Result<()> { + if matches!(self.failure, AotFailure::Persist) { + eyre::bail!("injected AOT persist failure"); + } + self.inner.store(key, manifest, object_bytes) + } + + fn delete(&self, key: &ArtifactKey) -> eyre::Result<()> { + self.inner.delete(key) + } + + fn clear(&self) -> eyre::Result<()> { + self.inner.clear() + } +} + +#[cfg(feature = "llvm")] +static STALE_RESULT_GATE: Mutex, chan::Receiver<()>)>> = Mutex::new(None); + +#[cfg(feature = "llvm")] +fn gated_builtin_address(builtin: evm2_jit_builtins::Builtin) -> usize { + let gate = STALE_RESULT_GATE.lock().unwrap().take(); + if let Some((entered, release)) = gate { + entered.send(()).unwrap(); + release.recv_timeout(std::time::Duration::from_secs(30)).unwrap(); + } + builtin.addr::() +} + // =========================================================================== // Tests: startup / basic. // =========================================================================== @@ -294,9 +377,8 @@ fn aot_out_of_process_is_rejected() { #[test] fn zero_workers_fails_sync_compile_without_blocking() { - let tb = TestBackend::new(RuntimeConfig { - blocking: true, - tuning: RuntimeTuning { jit_worker_count: 0, ..Default::default() }, + let tb = TestBackend::blocking_with_tuning(RuntimeTuning { + jit_worker_count: 0, ..Default::default() }); @@ -306,11 +388,60 @@ fn zero_workers_fails_sync_compile_without_blocking() { let stats = tb.stats(); assert_eq!(stats.resident_entries, 0); assert_eq!(stats.compilations_dispatched, 0); + assert_eq!(stats.interpreter_fallbacks, 1); +} + +#[test] +fn blocking_pending_job_cap_counts_fallback() { + let tb = TestBackend::blocking_with_tuning(RuntimeTuning { + jit_max_pending_jobs: 0, + ..Default::default() + }); + + let decision = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); + assert!(matches!(decision, LookupDecision::Unavailable(LookupMissReason::JitFailed))); + assert_eq!(tb.stats().interpreter_fallbacks, 1); +} + +#[test] +#[cfg(feature = "llvm")] +fn blocking_sync_compile_timeout_counts_fallback() { + // The callback runs before the waiting caller is notified, so the caller times out first. + let tb = TestBackend::new(RuntimeConfig { + blocking: true, + tuning: RuntimeTuning { + jit_worker_count: 1, + sync_compile_timeout: std::time::Duration::from_millis(20), + ..Default::default() + }, + on_compilation: Some(Arc::new(|_| { + std::thread::sleep(std::time::Duration::from_millis(500)) + })), + ..Default::default() + }); + + let decision = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); + assert!(matches!(decision, LookupDecision::Unavailable(LookupMissReason::JitFailed))); + assert_eq!(tb.stats().interpreter_fallbacks, 1); +} + +#[test] +#[cfg(feature = "llvm")] +fn blocking_mode_compiles_bytecode_over_max_len() { + let tb = TestBackend::blocking_with_tuning(RuntimeTuning { + jit_worker_count: 1, + jit_max_bytecode_len: 4, + ..Default::default() + }); + + let decision = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); + assert!(matches!(decision, LookupDecision::Compiled(_))); + assert_eq!(tb.stats().interpreter_fallbacks, 0); } #[test] fn lookup_disabled() { - let tb = TestBackend::new(RuntimeConfig { enabled: false, ..Default::default() }); + let tb = TestBackend::disabled_with_tuning(Default::default()); let req = TestBackend::req_cancun(&[0x00]); let decision = tb.lookup(req); @@ -323,7 +454,7 @@ fn lookup_disabled() { #[test] fn lookup_miss_when_enabled() { - let tb = TestBackend::new(RuntimeConfig { enabled: true, ..Default::default() }); + let tb = TestBackend::with_tuning(Default::default()); let decision = tb.lookup(TestBackend::req_cancun(&[0x00])); assert!(matches!(decision, LookupDecision::Unavailable(LookupMissReason::NotReady))); @@ -335,7 +466,7 @@ fn lookup_miss_when_enabled() { #[test] fn set_enabled_toggle() { - let tb = TestBackend::new(RuntimeConfig { enabled: false, ..Default::default() }); + let tb = TestBackend::disabled_with_tuning(Default::default()); let req = TestBackend::req_cancun(&[0x00]); assert!(matches!( @@ -384,7 +515,7 @@ fn pause_processes_lookup_events() { #[test] fn lookup_increments_miss_counter() { - let tb = TestBackend::new(RuntimeConfig { enabled: true, ..Default::default() }); + let tb = TestBackend::with_tuning(Default::default()); for _ in 0..10 { let _ = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); @@ -408,7 +539,7 @@ fn drop_shuts_down_backend() { #[test] fn backend_clone() { - let tb = TestBackend::new(RuntimeConfig { enabled: true, ..Default::default() }); + let tb = TestBackend::with_tuning(Default::default()); let b2 = tb.backend.clone(); let _ = tb.lookup(TestBackend::req_cancun(&[0x00])); @@ -467,7 +598,7 @@ fn prepare_aot_enqueue() { let tb = TestBackend::with_tuning(RuntimeTuning { jit_worker_count: 0, ..Default::default() }); tb.prepare_aot(AotRequest { code_hash: alloy_primitives::keccak256(BYTECODE_RET42), - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }); } @@ -478,12 +609,12 @@ fn prepare_aot_batch_enqueue() { let reqs = vec![ AotRequest { code_hash: alloy_primitives::keccak256(BYTECODE_RET42), - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }, AotRequest { code_hash: alloy_primitives::keccak256(BYTECODE_ADD), - code: Bytes::copy_from_slice(BYTECODE_ADD), + code: Bytes::from_static(BYTECODE_ADD), spec_id: SpecId::CANCUN, }, ]; @@ -505,7 +636,7 @@ fn prepare_aot_batch_never_blocks_when_startup_fails() { let worker = std::thread::spawn(move || { let reqs = vec![AotRequest { code_hash: alloy_primitives::keccak256(BYTECODE_RET42), - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }]; // More requests than the channel can hold: deadlocks without the started check. @@ -529,7 +660,7 @@ fn prepare_aot_batch_never_blocks_when_startup_fails() { #[test] fn clear_resident() { - let tb = TestBackend::new(RuntimeConfig { enabled: true, ..Default::default() }); + let tb = TestBackend::with_tuning(Default::default()); tb.clear_resident(); let decision = tb.lookup(TestBackend::req_cancun(&[0x00])); @@ -566,9 +697,8 @@ fn pause_resume_never_block_without_backend_thread() { // after `channel_capacity` sends and block the caller forever (observed in production as a // consensus-engine deadlock after exactly capacity/2 blocks). They must be skipped, not // queued, while the backend is not started. - let tb = TestBackend::new(RuntimeConfig { - enabled: false, - tuning: RuntimeTuning { channel_capacity: 2, ..Default::default() }, + let tb = TestBackend::disabled_with_tuning(RuntimeTuning { + channel_capacity: 2, ..Default::default() }); @@ -632,9 +762,8 @@ fn clear_commands_are_queued_after_startup_takes_config() { (JitBackend::clear_persisted, Command::ClearPersisted), (JitBackend::clear_all, Command::ClearAll), ] { - let tb = TestBackend::new(RuntimeConfig { - enabled: false, - tuning: RuntimeTuning { channel_capacity: 1, ..Default::default() }, + let tb = TestBackend::disabled_with_tuning(RuntimeTuning { + channel_capacity: 1, ..Default::default() }); let mut guard = tb.inner.lazy_spawn.lock().unwrap(); @@ -689,9 +818,8 @@ fn clear_commands_racing_with_startup_reach_store() { #[test] fn clear_commands_never_block_after_startup_receiver_disconnects() { - let tb = TestBackend::new(RuntimeConfig { - enabled: false, - tuning: RuntimeTuning { channel_capacity: 1, ..Default::default() }, + let tb = TestBackend::disabled_with_tuning(RuntimeTuning { + channel_capacity: 1, ..Default::default() }); drop(tb.inner.lazy_spawn.lock().unwrap().take().unwrap()); @@ -748,7 +876,7 @@ fn clear_commands_after_failed_spawn_reach_store_and_allow_retry() { #[test] fn pause_resume_depth_tracking_with_backend_thread() { // With the backend running, pause/resume still track depth and deliver commands. - let tb = TestBackend::with_tuning_1w(RuntimeTuning::default()); + let tb = TestBackend::default_1w(); tb.pause(); tb.pause(); @@ -797,9 +925,8 @@ fn blocking_out_of_process_pause_resume_does_not_stop_helper() { #[test] #[cfg(feature = "llvm")] fn blocking_mode() { - let tb = TestBackend::new(RuntimeConfig { - blocking: true, - tuning: RuntimeTuning { jit_worker_count: 1, ..Default::default() }, + let tb = TestBackend::blocking_with_tuning(RuntimeTuning { + jit_worker_count: 1, ..Default::default() }); @@ -818,6 +945,7 @@ fn blocking_mode() { // Empty bytecodes return Ineligible (nothing to compile). let decision = tb.lookup(TestBackend::req_cancun(&[])); assert!(matches!(decision, LookupDecision::Unavailable(LookupMissReason::Ineligible))); + assert_eq!(tb.stats().interpreter_fallbacks, 1); } #[test] @@ -923,27 +1051,53 @@ fn failed_compile_is_not_retried_until_cleared() { #[test] #[cfg(feature = "llvm")] fn clear_resident_discards_inflight_jit() { - let tb = - TestBackend::with_tuning_1w(RuntimeTuning { jit_hot_threshold: 1, ..Default::default() }); - - let req = || TestBackend::req_cancun(BYTECODE_RET42); - - // Trigger JIT promotion. - for _ in 0..5 { - let _ = tb.lookup(req()); - } + for clear in [JitBackend::clear_resident, JitBackend::clear_all] { + let (entered_tx, entered_rx) = chan::bounded(1); + let (release_tx, release_rx) = chan::bounded(1); + *STALE_RESULT_GATE.lock().unwrap() = Some((entered_tx, release_rx)); + let tb = TestBackend::new(RuntimeConfig { + enabled: true, + // SAFETY: the resolver returns the base host's unchanged builtin addresses. + host: unsafe { + HostConfig::for_types::().with_resolver(gated_builtin_address) + }, + tuning: RuntimeTuning { jit_worker_count: 1, ..Default::default() }, + ..Default::default() + }); - // Immediately clear — in-flight JIT results should be discarded. - tb.clear_resident(); + std::thread::scope(|scope| { + let compiling = scope.spawn(|| { + tb.compile_jit_sync(TestBackend::req_cancun(BYTECODE_RET42)).unwrap(); + }); + entered_rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap(); + clear(&tb); + // An ineligible request completes without a worker. Its notification proves the + // earlier clear command was processed, while the real compilation is still held. + tb.compile_jit_sync(TestBackend::req_cancun(&[])).unwrap(); + assert_eq!(tb.stats().pending_jobs, 1); + release_tx.send(()).unwrap(); + compiling.join().unwrap(); + }); - // Give time for worker to finish and backend to process. - std::thread::sleep(std::time::Duration::from_millis(500)); + let stats = tb.stats(); + assert_eq!(stats.compilations_dispatched, 1); + assert_eq!(stats.compilations_succeeded, 0); + assert_eq!(stats.compilations_failed, 0); + assert_eq!(stats.pending_jobs, 0); + assert_eq!(stats.compilations_discarded, 1); + assert!( + tb.get_compiled(alloy_primitives::keccak256(BYTECODE_RET42), SpecId::CANCUN).is_none() + ); - // The result from the old generation should have been discarded. - assert!( - matches!(tb.lookup(req()), LookupDecision::Unavailable(LookupMissReason::NotReady)), - "stale JIT result should not appear after clear_resident", - ); + // The new generation still compiles and notifies normally. + tb.compile_jit_sync(TestBackend::req_cancun(BYTECODE_RET42)).unwrap(); + assert!( + tb.get_compiled(alloy_primitives::keccak256(BYTECODE_RET42), SpecId::CANCUN).is_some() + ); + assert_eq!(tb.stats().pending_jobs, 0); + assert_eq!(tb.stats().compilations_succeeded, 1); + assert_eq!(tb.stats().compilations_discarded, 1); + } } #[test] @@ -976,11 +1130,8 @@ fn compile_jit_sync_blocks() { #[test] fn sync_compile_wait_times_out() { - let tb = TestBackend::new(RuntimeConfig { - tuning: RuntimeTuning { - sync_compile_timeout: std::time::Duration::from_millis(1), - ..Default::default() - }, + let tb = TestBackend::disabled_with_tuning(RuntimeTuning { + sync_compile_timeout: std::time::Duration::from_millis(1), ..Default::default() }); let (_tx, rx) = chan::bounded(1); @@ -1132,6 +1283,31 @@ fn memory_budget_eviction() { }); } +#[test] +#[cfg(feature = "llvm")] +fn memory_budget_evicts_aot_entries() { + let tb = TestBackend::new(RuntimeConfig { + enabled: true, + store: Some(Arc::new(RuntimeArtifactStore::new().unwrap())), + tuning: RuntimeTuning { + jit_worker_count: 1, + resident_code_cache_bytes: 1, + eviction_sweep_interval: std::time::Duration::from_millis(50), + ..Default::default() + }, + ..Default::default() + }); + + tb.prepare_aot_sync(AotRequest { + code_hash: alloy_primitives::keccak256(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), + spec_id: SpecId::CANCUN, + }) + .unwrap(); + + poll_until(std::time::Duration::from_secs(10), || (tb.stats().evictions >= 1).then_some(())); +} + // =========================================================================== // Tests: concurrency. // =========================================================================== @@ -1355,7 +1531,7 @@ fn prepare_aot_persist_and_load() { let code_hash = alloy_primitives::keccak256(BYTECODE_RET42); tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1365,6 +1541,107 @@ fn prepare_aot_persist_and_load() { assert_eq!(store.len(), 1); } +#[test] +#[cfg(feature = "llvm")] +fn failed_aot_persist_is_not_retried_until_cleared() { + let mut dispatches = Vec::new(); + for failure in [ + AotFailure::Persist, + AotFailure::ReloadMissing, + AotFailure::ReloadError, + AotFailure::Link, + AotFailure::NoStore, + ] { + let store = (!matches!(failure, AotFailure::NoStore)).then(|| { + Arc::new(AotFailureStore { inner: RuntimeArtifactStore::new().unwrap(), failure }) + as Arc + }); + let tb = TestBackend::new(RuntimeConfig { + enabled: true, + aot: store.is_some(), + store, + tuning: RuntimeTuning { + jit_worker_count: 1, + jit_hot_threshold: 1, + idle_evict_duration: None, + ..Default::default() + }, + ..Default::default() + }); + let req = || AotRequest { + code_hash: alloy_primitives::keccak256(BYTECODE_RET42), + code: Bytes::copy_from_slice(BYTECODE_RET42), + spec_id: SpecId::CANCUN, + }; + + // Duplicate requests must all be notified even when persistence/linking fails. + tb.prepare_aot_batch_sync(vec![req(), req(), req()]).unwrap(); + assert_eq!(tb.stats().compilations_failed, 1, "failure case {failure:?}"); + for _ in 0..10 { + if !matches!(failure, AotFailure::NoStore) { + let misses = tb.stats().lookup_misses; + let _ = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); + tb.wait_stats(|s| s.lookup_misses > misses); + } + // Also acts as a completion barrier and checks suppressed requests notify callers. + tb.prepare_aot_sync(req()).unwrap(); + } + let attempts = tb.stats().compilations_dispatched; + eprintln!("{failure:?}: dispatched {attempts} AOT attempts before clear"); + dispatches.push(attempts); + + tb.clear_resident(); + tb.prepare_aot_sync(req()).unwrap(); + assert_eq!(tb.stats().compilations_dispatched, attempts + 1); + assert_eq!(tb.stats().compilations_failed, attempts + 1); + + // In JIT mode an explicit AOT failure must not suppress the other compilation kind. + // In AOT mode even compile_jit_sync deliberately requests AOT output. + if matches!(failure, AotFailure::NoStore) { + tb.compile_jit_sync(TestBackend::req_cancun(BYTECODE_RET42)).unwrap(); + let program = tb.get_compiled(req().code_hash, SpecId::CANCUN).unwrap(); + assert_eq!(program.kind, ProgramKind::Jit); + assert_eq!(tb.stats().compilations_succeeded, 1); + } + assert_eq!(tb.stats().pending_jobs, 0); + } + assert_eq!(dispatches, vec![1; 5]); +} + +#[test] +#[cfg(feature = "llvm")] +fn failed_aot_persist_allows_jit_retry() { + for failure in + [AotFailure::Persist, AotFailure::ReloadMissing, AotFailure::ReloadError, AotFailure::Link] + { + let tb = TestBackend::new(RuntimeConfig { + enabled: true, + store: Some(Arc::new(AotFailureStore { + inner: RuntimeArtifactStore::new().unwrap(), + failure, + })), + tuning: RuntimeTuning { jit_worker_count: 1, ..Default::default() }, + ..Default::default() + }); + let req = TestBackend::req_cancun(BYTECODE_RET42); + tb.prepare_aot_sync(AotRequest { + code_hash: req.key.code_hash, + code: req.code.clone(), + spec_id: req.key.spec_id, + }) + .unwrap(); + assert_eq!(tb.stats().compilations_failed, 1); + tb.compile_jit_sync(req.clone()).unwrap(); + assert_eq!( + tb.get_compiled(req.key.code_hash, req.key.spec_id).unwrap().kind, + ProgramKind::Jit + ); + assert_eq!(tb.stats().compilations_dispatched, 2); + assert_eq!(tb.stats().compilations_succeeded, 1); + assert_eq!(tb.stats().pending_jobs, 0); + } +} + #[test] #[cfg(feature = "llvm")] fn blocking_aot_mode_compiles_miss_to_aot() { @@ -1434,7 +1711,7 @@ fn aot_artifacts_survive_restart() { tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1444,11 +1721,7 @@ fn aot_artifacts_survive_restart() { // Second backend: should preload the artifact at startup. { - let tb = TestBackend::new(RuntimeConfig { - enabled: true, - store: Some(store), - ..Default::default() - }); + let tb = TestBackend::with_store(store, Default::default()); let decision = tb.lookup(TestBackend::req_cancun(BYTECODE_RET42)); assert!( @@ -1476,7 +1749,7 @@ fn preload_aot_seeds_resident() { tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1484,11 +1757,7 @@ fn preload_aot_seeds_resident() { // Second backend: preloaded AOT should be available immediately. { - let tb = TestBackend::new(RuntimeConfig { - enabled: true, - store: Some(store), - ..Default::default() - }); + let tb = TestBackend::with_store(store, Default::default()); assert_eq!(tb.stats().resident_entries, 1); } } @@ -1508,7 +1777,7 @@ fn preload_aot_skips_stale_abi_version() { }); tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1519,7 +1788,7 @@ fn preload_aot_skips_stale_abi_version() { let stale_manifest = ArtifactManifest { artifact_key: stale_key, ..stored.manifest.clone() }; let stale_store = Arc::new(RuntimeArtifactStore::new().unwrap()); stale_store - .store(&stale_key, &stale_manifest, &std::fs::read(&stored.dylib_path).unwrap()) + .store(&stale_key, &stale_manifest, &std::fs::read(&stored.object_path).unwrap()) .unwrap(); let tb = TestBackend::new(RuntimeConfig { @@ -1547,7 +1816,7 @@ fn preload_aot_skips_manifest_key_mismatch() { }); tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1557,20 +1826,18 @@ fn preload_aot_skips_manifest_key_mismatch() { let stale_key = ArtifactKey { abi_version: crate::ABI_VERSION - 1, ..key }; let manifest = ArtifactManifest { artifact_key: stale_key, ..stored.manifest.clone() }; let mismatched_store = Arc::new(RuntimeArtifactStore::new().unwrap()); - mismatched_store.store(&key, &manifest, &std::fs::read(&stored.dylib_path).unwrap()).unwrap(); + mismatched_store.store(&key, &manifest, &std::fs::read(&stored.object_path).unwrap()).unwrap(); - let tb = TestBackend::new(RuntimeConfig { - enabled: true, - store: Some(mismatched_store), - tuning: RuntimeTuning { jit_worker_count: 1, ..Default::default() }, - ..Default::default() - }); + let tb = TestBackend::with_store( + mismatched_store, + RuntimeTuning { jit_worker_count: 1, ..Default::default() }, + ); assert_eq!(tb.stats().resident_entries, 0); // The probe rejects it too, so the artifact is recompiled. tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1608,7 +1875,7 @@ fn artifacts_are_scoped_to_compile_config() { let code_hash = alloy_primitives::keccak256(BYTECODE_RET42); let req = || AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }; let config = |gas_params| RuntimeConfig { @@ -1656,7 +1923,7 @@ fn prepare_aot_already_persisted_not_resident() { // Compile and persist an AOT artifact. tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1671,7 +1938,7 @@ fn prepare_aot_already_persisted_not_resident() { // Requesting prepare_aot again should load from store without recompiling. tb.prepare_aot_sync(AotRequest { code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), + code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN, }) .unwrap(); @@ -1713,11 +1980,8 @@ fn prepare_aot_persists_jit_resident() { tb.compile_jit_sync(TestBackend::req_cancun(BYTECODE_RET42)).unwrap(); assert_eq!(tb.stats().compilations_dispatched, dispatched_before); - let req = AotRequest { - code_hash, - code: Bytes::copy_from_slice(BYTECODE_RET42), - spec_id: SpecId::CANCUN, - }; + let req = + AotRequest { code_hash, code: Bytes::from_static(BYTECODE_RET42), spec_id: SpecId::CANCUN }; tb.prepare_aot_sync(req.clone()).unwrap(); assert_eq!(store.len(), 1); diff --git a/crates/jit/runtime/src/runtime/worker.rs b/crates/jit/runtime/src/runtime/worker.rs index 4a7d1793d..597a7fb41 100644 --- a/crates/jit/runtime/src/runtime/worker.rs +++ b/crates/jit/runtime/src/runtime/worker.rs @@ -21,7 +21,7 @@ use alloy_primitives::Bytes; use crossbeam_channel as chan; use rayon::{ThreadPool, ThreadPoolBuilder}; #[cfg(feature = "llvm")] -use std::{cell::RefCell, fs::File, io::Read, time::Instant}; +use std::{cell::RefCell, time::Instant}; use std::{ sync::{ Arc, @@ -33,7 +33,7 @@ use std::{ #[cfg(feature = "llvm")] use crate::{ - EvmCompiler, EvmLlvmBackend, Linker, + EvmCompiler, EvmLlvmBackend, llvm::{JitDylibGuard, orc::ResourceTracker}, runtime::config::JitMode, }; @@ -120,7 +120,7 @@ pub(crate) struct WorkerResult { pub(crate) enum WorkerSuccess { /// JIT compilation produced an in-memory function pointer. Jit(JitSuccess), - /// AOT compilation produced shared-library bytes. + /// AOT compilation produced relocatable object bytes to persist and link in process. Aot(AotSuccess), /// JIT compilation produced relocatable object bytes to link in the parent. JitObject(JitObjectSuccess), @@ -147,10 +147,10 @@ pub(crate) struct JitObjectSuccess { } pub(crate) struct AotSuccess { - /// The symbol name in the shared library. + /// The symbol name in the object file. pub(crate) symbol_name: String, - /// The raw shared-library bytes (.so / .dylib). - pub(crate) dylib_bytes: Vec, + /// The raw relocatable object bytes. + pub(crate) object_bytes: Vec, /// Length of the original bytecode. pub(crate) bytecode_len: usize, } @@ -586,7 +586,7 @@ struct CompilerProfile { layout: evm2_jit_codegen::HostLayout, resolver: Option, symbol_suffix: Option, - opcode_view: Option>, + opcode_view: Option>, opt_level: crate::OptimizationLevel, gas_params: Option, debug_assertions: bool, @@ -685,36 +685,29 @@ fn compile_jit_artifact( } } -/// Compiles a single bytecode to a shared library and returns the raw bytes. +/// Compiles a single bytecode to a relocatable object and returns the raw bytes. #[cfg(feature = "llvm")] -pub(super) fn compile_jit_object_artifact( +fn compile_object( job: &CompileJob, compiler: &mut EvmCompiler, -) -> Result { +) -> Result, String> { compiler .translate(&job.symbol_name, &job.bytecode[..], job.key.spec_id) - .map_err(|e| format!("JIT object translate failed: {e}"))?; + .map_err(|e| format!("object translate failed: {e}"))?; let mut object_bytes = Vec::new(); - compiler - .write_object(&mut object_bytes) - .map_err(|e| format!("JIT object write failed: {e}"))?; - let mut builtin_symbols: Vec = compiler - .backend() - .pending_symbol_names() - .into_iter() - .map(|name| name.to_string_lossy().into_owned()) - .collect(); - if builtin_symbols.is_empty() { - let suffix = compiler.builtin_symbol_suffix(); - builtin_symbols = evm2_jit_builtins::Builtin::ALL - .iter() - .map(|builtin| match suffix { - Some(suffix) => format!("{}_{suffix:x}", builtin.name()), - None => builtin.name().to_string(), - }) - .collect(); - } + compiler.write_object(&mut object_bytes).map_err(|e| format!("object write failed: {e}"))?; + Ok(object_bytes) +} + +/// Compiles a single bytecode to a relocatable object that the parent links as JIT code. +#[cfg(feature = "llvm")] +pub(super) fn compile_jit_object_artifact( + job: &CompileJob, + compiler: &mut EvmCompiler, +) -> Result { + let object_bytes = compile_object(job, compiler)?; + let builtin_symbols = builtin_symbol_names(compiler.builtin_symbol_suffix()); debug!( bytecode_len = job.bytecode.len(), @@ -729,43 +722,33 @@ pub(super) fn compile_jit_object_artifact( })) } +/// Returns the names of all builtin symbols that an object compiled with `suffix` may reference. +pub(super) fn builtin_symbol_names(suffix: Option) -> Vec { + evm2_jit_builtins::Builtin::ALL + .iter() + .map(|builtin| match suffix { + Some(suffix) => format!("{}_{suffix:x}", builtin.name()), + None => builtin.name().to_string(), + }) + .collect() +} + #[cfg(feature = "llvm")] fn compile_aot_artifact( job: &CompileJob, compiler: &mut EvmCompiler, ) -> Result { - compiler - .translate(&job.symbol_name, &job.bytecode[..], job.key.spec_id) - .map_err(|e| format!("AOT translate failed: {e}"))?; - - let tmp_dir = tempfile::tempdir().map_err(|e| format!("failed to create temp dir: {e}"))?; - - let obj_path = tmp_dir.path().join("a.o"); - let so_path = tmp_dir.path().join("a.so"); - - compiler - .write_object_to_file(&obj_path) - .map_err(|e| format!("AOT write object failed: {e}"))?; - - let linker = Linker::new(); - linker - .link(&so_path, [obj_path.to_str().unwrap()]) - .map_err(|e| format!("AOT link failed: {e}"))?; - - let mut dylib_bytes = Vec::new(); - File::open(&so_path) - .and_then(|mut f| f.read_to_end(&mut dylib_bytes)) - .map_err(|e| format!("failed to read linked .so: {e}"))?; + let object_bytes = compile_object(job, compiler)?; debug!( bytecode_len = job.bytecode.len(), - dylib_len = dylib_bytes.len(), + object_len = object_bytes.len(), "AOT compilation succeeded", ); Ok(WorkerSuccess::Aot(AotSuccess { symbol_name: job.symbol_name.clone(), - dylib_bytes, + object_bytes, bytecode_len: job.bytecode.len(), })) } diff --git a/crates/macros/src/lib.rs b/crates/macros/src/lib.rs index 6f63e7455..351b250ce 100644 --- a/crates/macros/src/lib.rs +++ b/crates/macros/src/lib.rs @@ -25,8 +25,8 @@ use syn::{ /// reference per non-`_` pattern. Inputs that overlap output slots are shared reborrows of the /// output slot; other inputs are shared references. Use reference patterns like `[&x]: [Word]` to /// copy. -/// - `-> out`: Automatic stack output. The output name is a mutable `Word` slot. Multiple outputs -/// can be written as `-> Result` for fallible instructions. +/// - `-> out`: Automatic stack output. The output name is a mutable `Word` slot. At most one output +/// is supported. /// - `-> Result`: Fallible instruction with no automatic outputs. The body must evaluate to /// `evm2::interpreter::Result`, so `?` can be used to return an `InstrStop`. /// - `-> Result`: Fallible instruction with automatic outputs. The body gets mutable output @@ -39,6 +39,15 @@ use syn::{ /// checks. /// - `#[instruction(dynamic_gas)]`: Exposes `cx.gas` and marks the instruction as needing access to /// mutable gas state. +/// - `#[instruction(hooked)]`: Marks an instruction that may run inspector hooks or host code that +/// read the interpreter. Before the instruction runs, the dispatch stores the latest pc, stack +/// length and gas into the interpreter, so `hooked` implies `dynamic_gas`. After it returns, the +/// dispatch picks up a change of the inspector's config. Set it on an instruction that runs a +/// child message. The `call`, `call_end`, `create` and `create_end` hooks get the interpreter +/// from inside such an instruction. `step` and `step_end` get the current state from the +/// dispatch, and `initialize_interp` runs at frame start, so they need no flag. An instruction +/// that only reaches `log` or `selfdestruct` needs none either, because those hooks get the host, +/// which has no current frame. Flag it if a hook starts receiving the interpreter. /// - `#[instruction(EvmTypes = CustomTypes)]`: Implements the instruction for a concrete EVM type /// family instead of generating a generic implementation. /// - `#[instruction(EvmTypes: CustomTypesTrait)]`: Adds a trait bound to the generated generic EVM @@ -109,7 +118,7 @@ enum InstructionAttr { impl Parse for InstructionAttr { fn parse(input: ParseStream<'_>) -> syn::Result { let ident = input.parse::()?; - if ident == "EvmTypes" || ident == "evm_types" { + if ident == "EvmTypes" { let span = ident.span(); if input.peek(Token![=]) { input.parse::()?; @@ -140,6 +149,7 @@ impl Parse for InstructionAttr { struct InstructionAttrs { no_stack_preamble: bool, dynamic_gas: bool, + hooked: bool, evm_types: Option, evm_types_span: Option, evm_types_bounds: Vec>, @@ -157,6 +167,10 @@ impl InstructionAttrs { InstructionAttr::Flag(arg) if arg == "dynamic_gas" => { attrs.dynamic_gas = true; } + InstructionAttr::Flag(arg) if arg == "hooked" => { + attrs.hooked = true; + attrs.dynamic_gas = true; + } InstructionAttr::Flag(arg) => { return Err(syn::Error::new_spanned( arg, @@ -237,23 +251,50 @@ fn expand_instruction(instruction_attrs: InstructionAttrs, input: ItemFn) -> Tok } else { quote! { where #evm_types: #evm_types_bound #( + #evm_types_bounds)* } }; - let (_, outputs) = parse_return(sig.output); + let outputs = match parse_return(sig.output) { + Ok(outputs) => outputs, + Err(err) => return err.to_compile_error(), + }; let mut has_cx = false; let mut cx_arg = None; let mut inputs = Vec::new(); + let mut has_stack_inputs = false; for arg in sig.inputs { - let FnArg::Typed(arg) = arg else { continue }; + let FnArg::Typed(arg) = arg else { + return syn::Error::new_spanned(arg, "`self` is not supported in #[instruction]") + .to_compile_error(); + }; if is_infer(&arg.ty) { - let Pat::Ident(PatIdent { ident, .. }) = *arg.pat else { continue }; + let Pat::Ident(PatIdent { ident, .. }) = *arg.pat else { + return syn::Error::new_spanned(arg.pat, "expected an identifier for `cx: _`") + .to_compile_error(); + }; + if has_cx { + return syn::Error::new_spanned(ident, "duplicate `cx: _` argument") + .to_compile_error(); + } has_cx = true; cx_arg = Some(ident); } else if is_word_slice(&arg.ty) { - let Pat::Slice(PatSlice { elems, .. }) = *arg.pat else { continue }; + let Pat::Slice(PatSlice { elems, .. }) = *arg.pat else { + return syn::Error::new_spanned( + arg.pat, + "expected a slice pattern like `[a, b]` for `[Word]` stack inputs", + ) + .to_compile_error(); + }; if let Some(rest) = elems.iter().find(|pat| matches!(pat, Pat::Rest(_))) { return syn::Error::new_spanned(rest, "`..` is not supported in stack inputs") .to_compile_error(); } + if std::mem::replace(&mut has_stack_inputs, true) { + return syn::Error::new_spanned( + elems, + "duplicate `[..]: [Word]` stack inputs argument", + ) + .to_compile_error(); + } inputs.extend(elems); } else { return syn::Error::new_spanned( @@ -264,6 +305,14 @@ fn expand_instruction(instruction_attrs: InstructionAttrs, input: ItemFn) -> Tok } } + if instruction_attrs.no_stack_preamble && (!inputs.is_empty() || !outputs.is_empty()) { + return syn::Error::new( + ident.span(), + "stack inputs and outputs are not supported with `no_stack_preamble`", + ) + .to_compile_error(); + } + let body = body(input.block.stmts, outputs.is_empty()); let stack_setup = if instruction_attrs.no_stack_preamble { None @@ -294,7 +343,20 @@ fn expand_instruction(instruction_attrs: InstructionAttrs, input: ItemFn) -> Tok } } }); - let dynamic_gas = instruction_attrs.dynamic_gas; + let flags = { + let mut flags = quote! { evm2::interpreter::private::InstructionFlags::empty() }; + if instruction_attrs.dynamic_gas { + flags = quote! { + #flags.union(evm2::interpreter::private::InstructionFlags::DYNAMIC_GAS) + }; + } + if instruction_attrs.hooked { + flags = quote! { + #flags.union(evm2::interpreter::private::InstructionFlags::HOOKED) + }; + } + flags + }; quote! { #(#fn_attrs)* #[allow(non_camel_case_types)] @@ -305,7 +367,7 @@ fn expand_instruction(instruction_attrs: InstructionAttrs, input: ItemFn) -> Tok impl #struct_generics evm2::interpreter::private::Instruction<#evm_types> for #ident #type_generics #where_clause { - const DYNAMIC_GAS: bool = #dynamic_gas; + const FLAGS: evm2::interpreter::private::InstructionFlags = #flags; #[inline] fn execute( @@ -353,26 +415,34 @@ fn is_word_slice(ty: &Type) -> bool { path.get_ident().is_some_and(|ident| ident == "Word") } -fn parse_return(output: ReturnType) -> (bool, Vec) { +fn parse_return(output: ReturnType) -> syn::Result> { let ReturnType::Type(_, ty) = output else { - return (false, Vec::new()); + return Ok(Vec::new()); }; - let Type::Path(TypePath { path, .. }) = *ty else { - return (false, Vec::new()); + let unsupported = |ty: &dyn quote::ToTokens| { + syn::Error::new_spanned( + ty, + "unsupported #[instruction] return type; use `-> out`, `-> Result`, or `-> Result`", + ) + }; + let Type::Path(TypePath { path, .. }) = &*ty else { + return Err(unsupported(&ty)); }; let Some(segment) = path.segments.last() else { - return (false, Vec::new()); + return Err(unsupported(&ty)); }; - if segment.ident == "Result" { - let outputs = match &segment.arguments { - PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) => { - args.iter().filter_map(generic_ident).collect() + if segment.ident != "Result" { + return Ok(vec![path.get_ident().cloned().ok_or_else(|| unsupported(&ty))?]); + } + match &segment.arguments { + PathArguments::None => Ok(Vec::new()), + PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) => { + if let Some(extra) = args.iter().nth(1) { + return Err(syn::Error::new_spanned(extra, "multiple outputs not supported")); } - _ => Vec::new(), - }; - (true, outputs) - } else { - (false, vec![segment.ident.clone()]) + args.iter().map(|arg| generic_ident(arg).ok_or_else(|| unsupported(arg))).collect() + } + PathArguments::Parenthesized(_) => Err(unsupported(&ty)), } } @@ -414,14 +484,6 @@ fn stack_setup(inputs: &[Pat], outputs: &[Ident]) -> TokenStream2 { } fn stack_bindings(inputs: &[Pat], outputs: &[Ident]) -> TokenStream2 { - if outputs.len() > 1 { - return syn::Error::new( - outputs.iter().map(|i| i.span()).reduce(|a, b| a.join(b).unwrap_or(a)).unwrap(), - "multiple outputs not supported", - ) - .into_compile_error(); - } - let count = inputs.len().max(outputs.len()); let mut bindings = Vec::with_capacity(count); for i in 0..count { diff --git a/data/airdrop.json b/data/airdrop.json index c3017cf5f..0d52a802f 100644 --- a/data/airdrop.json +++ b/data/airdrop.json @@ -26,7 +26,7 @@ "0x5c975abb" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xdf3a11531397647396a4774087e1b0811e6e10bdbf39088917241a12bc6da4b7", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/bswap64.json b/data/bswap64.json index 1ce02b4af..0361b57e0 100644 --- a/data/bswap64.json +++ b/data/bswap64.json @@ -26,7 +26,7 @@ "0xff2f79f10000000000000000000000000000000000000000000000000000000000000102" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x7f29c37dbfd1d49b0596a5443125add9efe700f74f2a87ba048c9137d742a22b", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/bswap64_opt.json b/data/bswap64_opt.json index 049111b27..6812dc9c5 100644 --- a/data/bswap64_opt.json +++ b/data/bswap64_opt.json @@ -26,7 +26,7 @@ "0xff2f79f10000000000000000000000000000000000000000000000000000000000000102" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xdc36c0d0f84fc01e8dd1fdcae87f6137b0e048c027cf37f0113baf62184fbbbe", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/burntpix.json b/data/burntpix.json index 7a1ce3e4a..bec4bf6fc 100644 --- a/data/burntpix.json +++ b/data/burntpix.json @@ -78,8 +78,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x8424dec58ec41225ad9d8f98e05962103b581ee5d8e026560cd58bceb4c79037", + "logs": "0xec16cb4f899a9811f7a842392c0663fb7dc752d2c6a4ebf24040291be4c05700" } ] } diff --git a/data/counter.json b/data/counter.json index 97fea5e8a..b3259052a 100644 --- a/data/counter.json +++ b/data/counter.json @@ -26,7 +26,7 @@ "0xd09de08a" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x0a40f0c6a8da740f9cc742f23c18f71b5ed54c8824157ede1cb3778da52fe0b1", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/curve-stableswap-2pool.json b/data/curve-stableswap-2pool.json index 4d4a6977a..339d84831 100644 --- a/data/curve-stableswap-2pool.json +++ b/data/curve-stableswap-2pool.json @@ -86,8 +86,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xe1b7ab78dab505a9c9526c52fa8d3b07ddce732c624ce835d467dc2d1ed5069b", + "logs": "0xe433310b26e9377e85fd586c3b503c09fed9d91e34e1a85b58b3e18d6ed73f93" } ] } diff --git a/data/eip2935.json b/data/eip2935.json index 431118558..6f9e838f8 100644 --- a/data/eip2935.json +++ b/data/eip2935.json @@ -28,7 +28,7 @@ "0x0000000000000000000000000000000000000000000000000000000000000001" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -48,8 +48,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x13ed5357d9842645c6f78e84eb61be5033b5810857f43597e9c0617c77916707", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/eip4788.json b/data/eip4788.json index a06cfa5de..2349ae2c5 100644 --- a/data/eip4788.json +++ b/data/eip4788.json @@ -29,7 +29,7 @@ "0x0000000000000000000000000000000000000000000000000000000069c32393" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -49,8 +49,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xac30e699719355145636f52b3ad493fb8a5ef8f966818ee34c421898e9f82648", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/erc20_transfer.json b/data/erc20_transfer.json index 1916e1562..9ee485317 100644 --- a/data/erc20_transfer.json +++ b/data/erc20_transfer.json @@ -26,7 +26,7 @@ "0x30627b7c" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xdbf50f40e7f142ff5f8a947c46a98bbabfc37351145dfdb73dee3ebac3879e73", + "logs": "0x173caeedb3a7168fde25244b48d9e11e231df14c2a41179954d3993d66e56332" } ] } diff --git a/data/factorial.json b/data/factorial.json index 83a024981..014bb7e99 100644 --- a/data/factorial.json +++ b/data/factorial.json @@ -26,7 +26,7 @@ "0x00000000000000000000000000000000000000000000000000000000000003e8" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xee622c8e979017debf38b42d2cd7cbd9de7bd84d44341ec51ec118e7852d6c01", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/fiat_token.json b/data/fiat_token.json index 3f67e61a0..2974d4956 100644 --- a/data/fiat_token.json +++ b/data/fiat_token.json @@ -26,7 +26,7 @@ "0x313ce567" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x4b179ee014516854f22d175fb53d72309f502dd0f4c16f4042af45e6f9adc0f5", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/fibonacci-calldata.json b/data/fibonacci-calldata.json index 2926f174b..889dbde35 100644 --- a/data/fibonacci-calldata.json +++ b/data/fibonacci-calldata.json @@ -26,7 +26,7 @@ "0x00000000000000000000000000000000000000000000000000000000000003e8" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x6331b8166430337d838f03a97ab8cb51a78d8d866d9c9bf079959ad41bff5e90", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/hash_10k.json b/data/hash_10k.json index 74075e19d..aee2fcaa1 100644 --- a/data/hash_10k.json +++ b/data/hash_10k.json @@ -26,7 +26,7 @@ "0x30627b7c" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x4896f5b04bea1cffbce29379d8cdc0958d46efadb0a6045ab1762217cb5f3114", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/mainnet-25347446-25347455.bin.zst b/data/mainnet-25347446-25347455.bin.zst index f8f64abac..8577dd341 100644 Binary files a/data/mainnet-25347446-25347455.bin.zst and b/data/mainnet-25347446-25347455.bin.zst differ diff --git a/data/onchain-lm-v2.json b/data/onchain-lm-v2.json index cb22d9e3f..894e80145 100644 --- a/data/onchain-lm-v2.json +++ b/data/onchain-lm-v2.json @@ -277,8 +277,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x306f35f5f4ca399c283a24a310185458d8bebb371282aa2f10c7a4004dd39fef", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/precompile-blake2.json b/data/precompile-blake2.json index 8c942fc58..3327ad085 100644 --- a/data/precompile-blake2.json +++ b/data/precompile-blake2.json @@ -11,13 +11,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -293,13 +293,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -340,13 +340,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -387,13 +387,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -434,13 +434,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-bls12-381.json b/data/precompile-bls12-381.json index c040bab0f..891f0daa8 100644 --- a/data/precompile-bls12-381.json +++ b/data/precompile-bls12-381.json @@ -11,13 +11,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -293,13 +293,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -340,13 +340,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -387,13 +387,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -434,13 +434,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -481,13 +481,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -528,13 +528,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -575,13 +575,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -622,13 +622,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -669,13 +669,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -716,13 +716,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -763,13 +763,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -810,13 +810,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -857,13 +857,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -904,13 +904,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -951,13 +951,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -998,13 +998,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -1045,13 +1045,13 @@ "Prague": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-bn254.json b/data/precompile-bn254.json index e8047aa0a..48256aafa 100644 --- a/data/precompile-bn254.json +++ b/data/precompile-bn254.json @@ -11,13 +11,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-ecrecover.json b/data/precompile-ecrecover.json index 258e4cdb7..3bcf39399 100644 --- a/data/precompile-ecrecover.json +++ b/data/precompile-ecrecover.json @@ -11,13 +11,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Istanbul": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-identity.json b/data/precompile-identity.json index 62d2dc93d..f1fdee793 100644 --- a/data/precompile-identity.json +++ b/data/precompile-identity.json @@ -11,13 +11,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-kzg-point-evaluation.json b/data/precompile-kzg-point-evaluation.json index 300234108..83de10741 100644 --- a/data/precompile-kzg-point-evaluation.json +++ b/data/precompile-kzg-point-evaluation.json @@ -11,13 +11,13 @@ "Cancun": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Cancun": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-modexp.json b/data/precompile-modexp.json index 55b2d1a30..93cb1ffdf 100644 --- a/data/precompile-modexp.json +++ b/data/precompile-modexp.json @@ -11,13 +11,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Berlin": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-p256verify.json b/data/precompile-p256verify.json index a51534d25..0f44ee41b 100644 --- a/data/precompile-p256verify.json +++ b/data/precompile-p256verify.json @@ -11,13 +11,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-ripemd160.json b/data/precompile-ripemd160.json index c58c2f9d9..f076ad929 100644 --- a/data/precompile-ripemd160.json +++ b/data/precompile-ripemd160.json @@ -11,13 +11,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/precompile-sha256.json b/data/precompile-sha256.json index 57cd67c2e..d556f3c19 100644 --- a/data/precompile-sha256.json +++ b/data/precompile-sha256.json @@ -11,13 +11,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -58,13 +58,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -105,13 +105,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -152,13 +152,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -199,13 +199,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, @@ -246,13 +246,13 @@ "Osaka": [ { "expectException": null, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "hash": "0x80efccaaed10981cec1fdbbecc59419278f14b5269a2f885d8f7d0531a781c93", "indexes": { "data": 0, "gas": 0, "value": 0 }, - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] }, diff --git a/data/push0_proxy.json b/data/push0_proxy.json index 70be8faca..6cf8896fd 100644 --- a/data/push0_proxy.json +++ b/data/push0_proxy.json @@ -26,7 +26,7 @@ "0x" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x44b14b1e0aca7f0259055b7fc43f8db20ce53f748096bd63781c00cee651c06e", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/seaport.json b/data/seaport.json index e2a3278fa..7c05fa2e3 100644 --- a/data/seaport.json +++ b/data/seaport.json @@ -28,7 +28,7 @@ "0x46423aa70123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -48,8 +48,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x74332c5431cbce90a512d9826384c400f1a0b93665e82418fb2dff53e94869d0", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/snailtracer.json b/data/snailtracer.json index 8c78ff5f2..da96debb3 100644 --- a/data/snailtracer.json +++ b/data/snailtracer.json @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x0e1e0b07fefadcf18149b0a7307d4c70d63bbdd88e3403749eb299e89dc7937f", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/uniswap_v2_pair.json b/data/uniswap_v2_pair.json index 4a5e8d5a1..7ff402332 100644 --- a/data/uniswap_v2_pair.json +++ b/data/uniswap_v2_pair.json @@ -26,7 +26,7 @@ "0x0902f1ac" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xd9bdd4b0e4ba2763150008c869ae47512baf54e7d7950d6db2cefed3938649ca", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/univ2_router.json b/data/univ2_router.json index 4bde67f50..da558bbbb 100644 --- a/data/univ2_router.json +++ b/data/univ2_router.json @@ -26,7 +26,7 @@ "0x054d50d400000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000000000000000000000000000000000174876e8000000000000000000000000000000000000000000000000000000000ba43b7400" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xa8ce14d3936f11af4d2a568098debc1a043800dba6ae8fc9274adde376d85cbd", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/usdc_proxy.json b/data/usdc_proxy.json index f2338318d..8324b1fed 100644 --- a/data/usdc_proxy.json +++ b/data/usdc_proxy.json @@ -26,7 +26,7 @@ "0x" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -46,8 +46,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0xe543886e90abb4ab88976bc4036019f1cb5d35b0d2ec34a698974dadce8fa76b", + "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347" } ] } diff --git a/data/weth.json b/data/weth.json index e8d5f710f..84b1cf117 100644 --- a/data/weth.json +++ b/data/weth.json @@ -28,7 +28,7 @@ "0xa9059cbb000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e8" ], "gasLimit": [ - "0x7fffffffffffffff" + "0x1000000" ], "gasPrice": "0x0", "nonce": "0x0", @@ -48,8 +48,8 @@ "gas": 0, "value": 0 }, - "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "logs": "0x0000000000000000000000000000000000000000000000000000000000000000" + "hash": "0x5857a425635f19db7cedcf0f043d246fa048f189b80c07667989f2bd95e91fb7", + "logs": "0xb2ea7219aa8e8235c1c4871a071613dbfb0e943af175a1d1a28b253740a1785b" } ] } diff --git a/deny.toml b/deny.toml index 3d6a19675..f193798f7 100644 --- a/deny.toml +++ b/deny.toml @@ -9,6 +9,8 @@ ignore = [ # Its uses here verify public transaction/precompile inputs; signing uses fixed test keys. # Remove this exception when their RustCrypto dependencies are upgraded. { id = "RUSTSEC-2026-0387", reason = "Unpatched 0.5.5 is required by Alloy/revm; vulnerable newer versions are banned below." }, + # `pin-utils` is unmaintained - transitive dependency + "RUSTSEC-2026-0441", ] [bans] diff --git a/scripts/utils.py b/scripts/common.py similarity index 78% rename from scripts/utils.py rename to scripts/common.py index c6c20fd1a..a04ac31a4 100644 --- a/scripts/utils.py +++ b/scripts/common.py @@ -5,7 +5,7 @@ def repo_root() -> str: """Return the repository root.""" - return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + return os.path.dirname(os.path.dirname(os.path.realpath(__file__))) def cargo_env() -> dict[str, str]: diff --git a/scripts/dump_asm.py b/scripts/dump_asm.py index 88680602c..0455aaf7a 100755 --- a/scripts/dump_asm.py +++ b/scripts/dump_asm.py @@ -27,17 +27,15 @@ from rich.highlighter import NullHighlighter from rich.logging import RichHandler -from utils import cargo_env, repo_root +from common import cargo_env, repo_root ROOT = Path(repo_root()) OPCODE_RS = ROOT / "crates" / "evm2" / "src" / "interpreter" / "opcode.rs" DEFAULT_OUT = ROOT / "tmp" / "dump" DISPATCH_SYMBOLS = ( "evm2::interpreter::dispatch::table::packed::dispatch::<", - "evm2::interpreter::dispatch::table::single_return::dispatch::<", "evm2::interpreter::dispatch::table::unpacked::dispatch::<", "evm2::interpreter::dispatch::packed::dispatch::<", - "evm2::interpreter::dispatch::single_return::dispatch::<", "evm2::interpreter::dispatch::unpacked::dispatch::<", "evm2::interpreter::dispatch::tco::dispatch::<", ) diff --git a/scripts/eest.sh b/scripts/eest.sh index c9ae1e29c..e2457a7a2 100755 --- a/scripts/eest.sh +++ b/scripts/eest.sh @@ -8,7 +8,7 @@ # fixture found under it runs as one auto-detecting suite (state vs blockchain # kind is detected per file), so no test-name filter is needed. Extra arguments # are forwarded to `cargo nextest run`, for example: -# scripts/eest.sh ./test-fixtures/devnet/state_tests -E 'test(precompile)' +# scripts/eest.sh ./test-fixtures/eest/state_tests -E 'test(precompile)' set -euo pipefail if [[ $# -lt 1 ]]; then diff --git a/scripts/fuzzer_coverage.py b/scripts/fuzzer_coverage.py index 09e0573c1..b025189d6 100755 --- a/scripts/fuzzer_coverage.py +++ b/scripts/fuzzer_coverage.py @@ -19,12 +19,12 @@ import sys from pathlib import Path -from utils import cargo_env, repo_root +from common import cargo_env, repo_root ROOT = Path(repo_root()) DEFAULT_OUTPUT_DIR = ROOT / "target" / "llvm-cov" / "fuzzer-html" DEFAULT_IGNORE_REGEX = r"(^|/)crates/cli/src/fuzzer/" -BACKENDS = ("tco", "packed", "single_return", "unpacked") +BACKENDS = ("tco", "packed", "unpacked") def parse_args() -> argparse.Namespace: diff --git a/scripts/jit/bench.py b/scripts/jit/bench.py index 9fea6c9bb..0d56b1713 100755 --- a/scripts/jit/bench.py +++ b/scripts/jit/bench.py @@ -51,7 +51,9 @@ cargo_build, cargo_env, eprint, + geomean_pct, get_benches, + pct, perf_available, perf_counts, repo_root, @@ -138,17 +140,13 @@ def fmt_rel(pct: float) -> str: def fmt_pct(base: float | int, current: float | int) -> str: - if base == 0: - return "-" - return fmt_rel((current - base) / base * 100) + change = pct(base, current) + return "-" if change is None else fmt_rel(change) def fmt_geomean(pairs: list[tuple[float, float]]) -> str: - """Geometric mean of the current/base ratios, so every benchmark weighs the same.""" - ratios = [c / b for b, c in pairs if b > 0 and c > 0] - if not ratios: - return "-" - return fmt_rel((statistics.geometric_mean(ratios) - 1) * 100) + change = geomean_pct(pairs) + return "-" if change is None else fmt_rel(change) def fmt_detail(current: str | int, base: str | int, diff: str) -> str: @@ -1038,12 +1036,13 @@ def measure(sides: list[Side], benches: list[str], runs: int, perf: bool): RUNTIME_PROBE = 1e8 -def total_iters(n: int) -> int: - """`evm2 run N` runs `max(N / 10, 10)` warmup iterations before the timed ones. +def counted_iters(n: int, counted: int | None) -> int: + """Return the iterations a run of `N` counted: what the CLI reports, else what it did before. - Keep in sync with `bench` in `crates/cli/src/run.rs`. + A binary that predates the report enabled the counters before its `max(N / 10, 10)` + warmup iterations. """ - return n + max(n // 10, 10) + return n + max(n // 10, 10) if counted is None else counted def runtime_args(bench: str, n: int) -> list[str]: @@ -1053,13 +1052,13 @@ def runtime_args(bench: str, n: int) -> list[str]: def sample_iters(side: Side, bench: str) -> int: """Return the `N` whose run executes about `RUNTIME_TARGET / RUNTIME_SAMPLES` instructions.""" n = 10 - while ( - instructions := perf_counts(side.binary, runtime_args(bench, n))[0] - ) < RUNTIME_PROBE and n < 10**7: + while True: + instructions, _, counted = perf_counts(side.binary, runtime_args(bench, n)) + if instructions >= RUNTIME_PROBE or n >= 10**7: + break n *= 10 - iters = int(RUNTIME_TARGET / RUNTIME_SAMPLES / (instructions / total_iters(n))) - # Invert `total_iters`. - return max(10, iters - 10 if iters < 110 else int(iters / 1.1)) + per_iter = instructions / counted_iters(n, counted) + return max(10, int(RUNTIME_TARGET / RUNTIME_SAMPLES / per_iter)) def measure_runtime_bench(sides: list[Side], bench: str, skipped: set[str]): @@ -1097,8 +1096,8 @@ def measure_runtime_bench(sides: list[Side], bench: str, skipped: set[str]): side.failures[bench] = last_line(e.stderr) live.remove(side) for side in live: - total = total_iters(iters[side.label]) counts = samples[side.label] + total = counted_iters(iters[side.label], counts[0][2]) side.runtime[bench] = statistics.median(c[0] for c in counts) / total side.runtime_cycles[bench] = min(c[1] for c in counts) / total @@ -1226,20 +1225,15 @@ def base_binary(root: str, sha: str, base_dump: str) -> str: } ARTIFACT_FILES = ["bytecode.txt", "bytecode.dot", "opt.ll", "opt.s"] TARGET_CPU_RE = re.compile(r'"target-cpu"="([^"]+)"') -BENCH_FIXTURE_RE = re.compile(r'Bench::\w+\(\s*"([^"]+)",[^"]*"([^"]+)"') -def source_links(root: str, rev: str) -> dict[str, list[dict[str, str]]]: - """Link each built-in benchmark to its fixture in the benchmark catalog.""" - try: - with open(os.path.join(root, "crates/cli/src/evm_bench.rs")) as f: - catalog = f.read() - except FileNotFoundError: - return {} +def source_links(binary: str, rev: str) -> dict[str, list[dict[str, str]]]: + """Link each built-in benchmark to the fixture that ``binary`` lists for it.""" repo = os.environ.get("GITHUB_REPOSITORY", "alloy-rs/evm2") return { name: [{"label": path, "url": f"https://github.com/{repo}/blob/{rev}/{path}"}] - for name, path in BENCH_FIXTURE_RE.findall(catalog) + for name, path in get_benches(binary).items() + if path } @@ -1258,7 +1252,7 @@ def write_results(out_dir: str, benches: list[str], side: Side, perf: bool): """Write ``results.json`` and normalized artifacts for the perf site and ``compare.py``.""" codegen = CodegenLines()._collect(benches, side.dump_dir) times = CompileTime()._collect(benches, side) - links = source_links(side.root, side.rev) + links = source_links(side.binary, side.rev) artifacts = os.path.join(out_dir, "artifacts") if os.path.isdir(artifacts): shutil.rmtree(artifacts) @@ -1456,7 +1450,7 @@ def main(): base_root = os.path.join(base_dump, ".worktree") base = Side("base", base_sha, base_binary(root, base_sha, base_dump), base_dump, base_root) head = Side("head", head_sha, cargo_build(root), args.dump_dir, root) - builtin = args.benches or get_benches(head.binary) + builtin = args.benches or list(get_benches(head.binary)) extra_dirs = ( args.extra_dir if args.extra_dir is not None else ([] if args.benches else ["tmp/mainnet"]) ) diff --git a/scripts/jit/compare.py b/scripts/jit/compare.py index a4f61d082..b41dee52a 100644 --- a/scripts/jit/compare.py +++ b/scripts/jit/compare.py @@ -20,7 +20,6 @@ import json import os import re -import statistics import subprocess import sys import uuid @@ -46,6 +45,7 @@ runtime_changed, time_changed, ) +from utils import geomean_pct, pct PERF_SITE_URL = "https://www.getfoundry.sh/perf/evm2/" # Metrics whose changes are deterministic and decide whether a benchmark changed. @@ -130,19 +130,6 @@ def artifact_hashes(document: dict, test_id: str) -> dict[str, str]: } -def pct(before, after) -> float | None: - if before is None or after is None or before == 0: - return None - return (after - before) / before * 100 - - -def geomean_pct(pairs: list[tuple]) -> float | None: - ratios = [a / b for b, a in pairs if b and a and b > 0 and a > 0] - if not ratios: - return None - return (statistics.geometric_mean(ratios) - 1) * 100 - - def fmt_change(value: float | None) -> str: if value is None: return "n/a" diff --git a/scripts/jit/utils.py b/scripts/jit/utils.py index 79f502d83..94b8dfc25 100644 --- a/scripts/jit/utils.py +++ b/scripts/jit/utils.py @@ -4,11 +4,16 @@ import os import re import shutil +import statistics import subprocess import sys import tempfile import time +# `utils` names this module when a jit script runs, so the shared helpers live in `common`. +sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from common import cargo_env as base_cargo_env, repo_root # noqa: E402 + ANSI_RE = re.compile(r"\x1b\[[0-9;]*m") @@ -20,22 +25,33 @@ def strip_ansi(s: str) -> str: return ANSI_RE.sub("", s) -def repo_root() -> str: - return os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - - def _shared_target_dir() -> str: """Return a shared CARGO_TARGET_DIR so worktrees reuse the same build cache.""" return os.path.join(repo_root(), "target") def cargo_env(rust_log: str | None = None) -> dict[str, str]: - env = {**os.environ, "NO_COLOR": "1", "CARGO_TARGET_DIR": _shared_target_dir()} + env = {**base_cargo_env(), "CARGO_TARGET_DIR": _shared_target_dir()} if rust_log: env["RUST_LOG"] = rust_log return env +def pct(before: float | None, after: float | None) -> float | None: + """Percent change from ``before`` to ``after``, or ``None`` when there is no baseline.""" + if before is None or after is None or before == 0: + return None + return (after - before) / before * 100 + + +def geomean_pct(pairs: list[tuple[float, float]]) -> float | None: + """Geometric mean of the after/before ratios as a percent change, equal weight per pair.""" + ratios = [a / b for b, a in pairs if b and a and b > 0 and a > 0] + if not ratios: + return None + return (statistics.geometric_mean(ratios) - 1) * 100 + + def cargo_build(root: str, target_dir: str | None = None) -> str: """Build the CLI binary and return its path. @@ -57,12 +73,14 @@ def cargo_build(root: str, target_dir: str | None = None) -> str: def llvm_env(root: str) -> dict[str, str]: """Put the LLVM version that ``root`` requires first in PATH, if it is installed. - Base and head can require different LLVM versions, and both llvm-sys and the - evm2-jit-llvm build script otherwise use whichever llvm-config comes first in PATH. + Base and head can require different LLVM versions, and llvm-sys otherwise uses + whichever llvm-config comes first in PATH. """ with open(os.path.join(root, "crates", "jit", "llvm", "Cargo.toml")) as f: - m = re.search(r'"llvm(\d+)-\d+"', f.read()) - llvm_config = m and shutil.which(f"llvm-config-{m[1]}") + # Older code selects LLVM through an inkwell feature, newer code through llvm-sys. + m = re.search(r'"llvm(\d+)-\d+"|^llvm-sys = "(\d+)\d"', f.read(), re.M) + version = m and (m[1] or m[2]) + llvm_config = version and shutil.which(f"llvm-config-{version}") if not llvm_config: return {} bindir = subprocess.run( @@ -71,14 +89,20 @@ def llvm_env(root: str) -> dict[str, str]: return {"PATH": bindir + os.pathsep + os.environ.get("PATH", "")} -def get_benches(binary: str) -> list[str]: +def get_benches(binary: str) -> dict[str, str]: + """Map each built-in benchmark to its fixture path, as listed by ``run --list``.""" r = subprocess.run( [binary, "run", "--list"], stdout=subprocess.PIPE, text=True, check=True, ) - return [line.strip() for line in r.stdout.splitlines() if line.strip()] + benches = {} + for line in r.stdout.splitlines(): + name, _, path = line.strip().partition("\t") + if name: + benches[name] = path + return benches def run_cli( @@ -201,8 +225,12 @@ class NoBenchCounts(Exception): """The binary ran without counting a benchmark loop: it predates ``EVM2_PERF_CONTROL``.""" -def perf_counts(binary: str, args: list[str]) -> tuple[int, int]: - """Run ``evm2 run`` under ``perf stat``; return ``(instructions, cycles)`` of its bench loop. +def perf_counts(binary: str, args: list[str]) -> tuple[int, int, int | None]: + """Run ``evm2 run`` under ``perf stat``; return ``(instructions, cycles, iterations)``. + + The counts cover its bench loop, and ``iterations`` is the number of iterations the CLI + reports having counted on a pipe of its own (``EVM2_PERF_REPORT``), or ``None`` when the + binary predates that report. The pipe carries nothing a benchmark name can write. The counters start disabled and the CLI enables them around the loop through ``EVM2_PERF_CONTROL``, so the compile cost, which is large and noisy, is not counted. @@ -212,7 +240,8 @@ def perf_counts(binary: str, args: list[str]) -> tuple[int, int]: """ ctl_read, ctl_write = os.pipe() ack_read, ack_write = os.pipe() - fds = [ctl_read, ctl_write, ack_read, ack_write] + report_read, report_write = os.pipe() + fds = [ctl_read, ctl_write, ack_read, ack_write, report_read, report_write] try: with tempfile.NamedTemporaryFile("r") as out: subprocess.run( @@ -236,9 +265,21 @@ def perf_counts(binary: str, args: list[str]) -> tuple[int, int]: stderr=subprocess.PIPE, text=True, check=True, - env=fixed_stack_env(binary, args, {"EVM2_PERF_CONTROL": f"{ctl_write},{ack_read}"}), + env=fixed_stack_env( + binary, + args, + { + "EVM2_PERF_CONTROL": f"{ctl_write},{ack_read}", + "EVM2_PERF_REPORT": str(report_write), + }, + ), pass_fds=fds, ) + # The run is over, so the report is in the pipe once the write end closes. + os.close(report_write) + fds.remove(report_write) + with os.fdopen(report_read, closefd=False) as report: + counted = report.read().split() stats = {} for line in out: parts = line.strip().split(",") @@ -252,4 +293,4 @@ def perf_counts(binary: str, args: list[str]) -> tuple[int, int]: os.close(fd) if not stats.get("instructions:u"): raise NoBenchCounts(binary) - return int(stats["instructions:u"]), int(stats["cycles:u"]) + return int(stats["instructions:u"]), int(stats["cycles:u"]), sum(map(int, counted)) or None diff --git a/scripts/setup_test_fixtures.py b/scripts/setup_test_fixtures.py index ef6397f85..da89a0633 100755 --- a/scripts/setup_test_fixtures.py +++ b/scripts/setup_test_fixtures.py @@ -33,31 +33,31 @@ ) from rich.text import Text +from common import repo_root + MAIN_VERSION = os.environ.get("MAIN_VERSION", "v5.3.0") # ethereum/legacytests commit pinned by the `LegacyTests` submodule of ethereum/tests v17.2. The # ethereum/tests archives do not include submodules, so the tests are fetched from the submodule's # own repository. LEGACY_VERSION = os.environ.get("LEGACY_VERSION", "1f581b8ccdc4c63acf5f2c5c1b155c690c32a8eb") BASE_URL = "https://github.com/ethereum/execution-spec-tests/releases/download" -# Fork-specific devnet fixtures are published from the execution-specs repo. -DEVNET_BASE_URL = os.environ.get( - "DEVNET_BASE_URL", +# Mainline execution-specs fixtures cover Frontier..Amsterdam. The release is a URL path component, so +# the `@` is percent-encoded. Override EEST_VERSION/EEST_TAR/EEST_BASE_URL to select another release, +# for example a fork-specific devnet release. +EEST_BASE_URL = os.environ.get( + "EEST_BASE_URL", "https://github.com/ethereum/execution-specs/releases/download", ) -# Default fork-specific devnet fixtures: glamsterdam (Amsterdam) devnet-8. Downloaded by default so -# the Amsterdam tests run without extra configuration; override DEVNET_VERSION/DEVNET_TAR to select a -# different devnet release. The version is a URL path component, so the `@` is percent-encoded. -DEFAULT_DEVNET_VERSION = "tests-glamsterdam-devnet%40v8.1.4" -DEFAULT_DEVNET_TAR = "fixtures_glamsterdam-devnet.tar.gz" +DEFAULT_EEST_VERSION = "tests%40v21.0.1" +DEFAULT_EEST_TAR = "fixtures.tar.gz" LEGACY_URL = f"https://github.com/ethereum/legacytests/archive/{LEGACY_VERSION}.tar.gz" -SCRIPT_DIR = Path(__file__).resolve().parent -REPO_ROOT = SCRIPT_DIR.parent +REPO_ROOT = Path(repo_root()) FIXTURES_DIR = Path(os.environ.get("EVM2_TEST_FIXTURES", REPO_ROOT / "test-fixtures")) MAIN_STABLE_DIR = FIXTURES_DIR / "main" / "stable" MAIN_DEVELOP_DIR = FIXTURES_DIR / "main" / "develop" -DEVNET_DIR = FIXTURES_DIR / "devnet" +EEST_DIR = FIXTURES_DIR / "eest" LEGACY_DIR = FIXTURES_DIR / "legacytests" console = Console() @@ -87,6 +87,8 @@ class Fixture: url: str dest: Path exists_paths: tuple[Path, ...] + # tar member patterns to skip while extracting. + excludes: tuple[str, ...] = () def env_flag(name: str) -> bool: @@ -95,7 +97,7 @@ def env_flag(name: str) -> bool: def main_fixture() -> Fixture: - if env_flag("EVM2_STATETEST_STABLE") or env_flag("EVM2_BLOCKCHAINTEST_STABLE") or env_flag("EVM2_EEST_STABLE"): + if env_flag("EVM2_EEST_STABLE"): dest = MAIN_STABLE_DIR tar_name = os.environ.get("MAIN_TAR", "fixtures_stable.tar.gz") label = "main stable" @@ -124,37 +126,39 @@ def legacy_fixture() -> Fixture: ) -def devnet_fixture() -> Fixture | None: - version = os.environ.get("DEVNET_VERSION", DEFAULT_DEVNET_VERSION) - tar_name = os.environ.get("DEVNET_TAR", DEFAULT_DEVNET_TAR) +def eest_fixture() -> Fixture | None: + version = os.environ.get("EEST_VERSION", DEFAULT_EEST_VERSION) + tar_name = os.environ.get("EEST_TAR", DEFAULT_EEST_TAR) - # An explicitly cleared version or tar disables the devnet fixtures. + # An explicitly cleared version or tar disables the execution-specs fixtures. if not version or not tar_name: return None return Fixture( - label="devnet", - url=f"{DEVNET_BASE_URL}/{version}/{tar_name}", - dest=DEVNET_DIR, - exists_paths=(DEVNET_DIR / "state_tests", DEVNET_DIR / "blockchain_tests"), + label="eest", + url=f"{EEST_BASE_URL}/{version}/{tar_name}", + dest=EEST_DIR, + exists_paths=(EEST_DIR / "state_tests", EEST_DIR / "blockchain_tests"), + # The runner executes only the state and blockchain test formats. + excludes=("*/blockchain_tests_engine*", "*/blockchain_tests_sync", "*/transaction_tests"), ) def fixture_plan() -> list[Fixture]: fixtures: list[Fixture] = [] - # The glamsterdam devnet fixtures cover frontier..amsterdam, superseding the main develop suite - # by fork coverage, so main develop/stable is now opt-in via EVM2_STATETEST_MAIN. + # The execution-specs release covers frontier..amsterdam, superseding the main develop suite by + # fork coverage, so main develop/stable is opt-in via EVM2_STATETEST_MAIN. if env_flag("EVM2_STATETEST_MAIN"): fixtures.append(main_fixture()) - # Legacy hand-written GeneralStateTests, unless a devnet-only run was requested. - if not env_flag("EVM2_STATETEST_DEVNET_ONLY"): + # Legacy hand-written GeneralStateTests, unless an execution-specs-only run was requested. + if not env_flag("EVM2_STATETEST_EEST_ONLY"): fixtures.append(legacy_fixture()) - # Glamsterdam (Amsterdam) devnet fixtures: the default suite. - if devnet := devnet_fixture(): - fixtures.append(devnet) + # Mainline execution-specs fixtures: the default suite. + if eest := eest_fixture(): + fixtures.append(eest) return fixtures @@ -192,6 +196,7 @@ async def pipe_to_tar( "tar", "xzf", "-", + *(f"--exclude={pattern}" for pattern in fixture.excludes), "--strip-components=1", "-C", str(fixture.dest), @@ -354,8 +359,8 @@ def print_summary() -> None: MAIN_STABLE_DIR / "blockchain_tests", MAIN_DEVELOP_DIR / "state_tests", MAIN_DEVELOP_DIR / "blockchain_tests", - DEVNET_DIR / "state_tests", - DEVNET_DIR / "blockchain_tests", + EEST_DIR / "state_tests", + EEST_DIR / "blockchain_tests", LEGACY_DIR / "Cancun" / "GeneralStateTests", LEGACY_DIR / "Constantinople" / "GeneralStateTests", ]: