Skip to content

fix(revm): align BLOCKHASH with Morph geth semantics#38

Merged
chengwenxi merged 1 commit intomainfrom
fix/blockhash-morph-semantics
Mar 6, 2026
Merged

fix(revm): align BLOCKHASH with Morph geth semantics#38
chengwenxi merged 1 commit intomainfrom
fix/blockhash-morph-semantics

Conversation

@panos-xyz
Copy link
Contributor

@panos-xyz panos-xyz commented Mar 4, 2026

Summary

  • align BLOCKHASH opcode in morph-revm with Morph geth semantics: return keccak(chain_id || block_number) in the last-256-block window
  • keep Ethereum-compatible out-of-range behavior (0 for current/future/out-of-window requests)
  • add focused unit tests for a golden vector (chain_id=2818, number=2662437) and 256-window boundary behavior

Why

  • mainnet block 2662438 failed import with block gas used mismatch (got 823153, expected 825454)
  • root cause was an opcode semantics mismatch on BLOCKHASH, which changed downstream execution path and gas accounting

Test plan

  • cargo check -p morph-revm
  • cargo test -p morph-revm morph_blockhash
  • deploy validation completed on target environment (block import succeeds)

Closes #37

Override BLOCKHASH to return keccak(chain_id||block_number) within the 256-block window, matching Morph geth and removing the gas divergence at block 2662438.
@coderabbitai
Copy link

coderabbitai bot commented Mar 4, 2026

📝 Walkthrough

Walkthrough

Implements Morph-specific BLOCKHASH opcode behavior in the EVM, including helper functions to compute keccak256-hashed block references and enforce the 256-block history window, with corresponding test coverage and instruction registration.

Changes

Cohort / File(s) Summary
Morph BLOCKHASH Implementation
crates/revm/src/evm.rs
Adds helper functions for saturating U256-to-u64 conversion and keccak256-based blockhash computation; implements blockhash_morph instruction to enforce Morph's 256-block history window (returning hashed values for valid blocks, zero otherwise); registers new instruction handler and adds comprehensive test coverage for boundary conditions and known test vectors.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Suggested reviewers

  • anylots

Poem

🐰 A blockhash, hashed true, for Morph's embrace,
Two-hundred-fifty-six blocks in history's space,
Keccak whispers secrets of chain and block number,
Stack pops and pushes through EVM's grand thunder,
One instruction to rule them all! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(revm): align BLOCKHASH with Morph geth semantics' directly and accurately summarizes the main change: implementing Morph-specific BLOCKHASH semantics in the revm crate.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/blockhash-morph-semantics

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
crates/revm/src/evm.rs (1)

285-289: Strengthen the lower-bound test to assert exact value, not just non-zero.

Line 286 currently only proves “some non-zero value” was returned. Asserting equality against morph_blockhash_value(chain_id, current - 256) will catch hashing/input regressions too.

Suggested test tightening
-        // lower bound is inclusive (current - 256 is valid)
-        assert_ne!(
-            morph_blockhash_result(chain_id, current, current - 256),
-            U256::ZERO
-        );
+        // lower bound is inclusive (current - 256 is valid)
+        assert_eq!(
+            morph_blockhash_result(chain_id, current, current - 256),
+            morph_blockhash_value(chain_id, current - 256)
+        );
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/revm/src/evm.rs` around lines 285 - 289, The test currently only
asserts the blockhash is non-zero using morph_blockhash_result(chain_id,
current, current - 256); replace that non-zero check with an exact equality
assertion against the expected value by calling morph_blockhash_value(chain_id,
current - 256) and asserting morph_blockhash_result(chain_id, current, current -
256) == morph_blockhash_value(chain_id, current - 256) so the test fails on any
hashing/input regression.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@crates/revm/src/evm.rs`:
- Around line 285-289: The test currently only asserts the blockhash is non-zero
using morph_blockhash_result(chain_id, current, current - 256); replace that
non-zero check with an exact equality assertion against the expected value by
calling morph_blockhash_value(chain_id, current - 256) and asserting
morph_blockhash_result(chain_id, current, current - 256) ==
morph_blockhash_value(chain_id, current - 256) so the test fails on any
hashing/input regression.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: db784831-6f4c-422d-9a3a-300cc571598e

📥 Commits

Reviewing files that changed from the base of the PR and between 74c1a9c and a0a33a9.

📒 Files selected for processing (1)
  • crates/revm/src/evm.rs

@panos-xyz panos-xyz requested review from anylots and chengwenxi March 4, 2026 11:24
@chengwenxi chengwenxi merged commit 337469b into main Mar 6, 2026
9 checks passed
@chengwenxi chengwenxi deleted the fix/blockhash-morph-semantics branch March 6, 2026 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: block gas used mismatch at block 2662438 on mainnet (BLOCKHASH semantics mismatch)

2 participants