Skip to content

[Flow EVM] Optimize and reduce computation cost of four EVM functions#8434

Merged
fxamacker merged 1 commit intomasterfrom
fxamacker/avoid-creating-blockproposal-for-readonlyblockview
Feb 20, 2026
Merged

[Flow EVM] Optimize and reduce computation cost of four EVM functions#8434
fxamacker merged 1 commit intomasterfrom
fxamacker/avoid-creating-blockproposal-for-readonlyblockview

Conversation

@fxamacker
Copy link
Copy Markdown
Contributor

@fxamacker fxamacker commented Feb 20, 2026

Updates #8401

Currently, calling NewReadOnlyBlockView() incurs the overhead and computation cost of always reading a register from storage, and potentially also writing a register to the storage.

Specifically, Emulator.NewReadOnlyBlockView() receives BlockContext param without actually using it. Creating BlockContext requires reading BlockProposal from storage, which incurs GetValue and potentially SetValue computation cost.

This PR removes the unnecessary BlockContext param in Emulator.NewReadOnlyBlockView(). This optimization avoids reading a register and reduces computation cost in these EVM functions:

  • EVMAddress.balance()
  • EVMAddress.code()
  • EVMAddress.codeHash()
  • EVMAddress.nonce()

Summary by CodeRabbit

  • Refactor
    • Updated the EVM Emulator's read-only block view creation method by removing the block context parameter requirement. This change simplifies the internal API while maintaining existing functionality for querying account balance, code, and nonce information. All affected call sites and test utilities have been updated accordingly.

Currently, calling NewReadOnlyBlockView() incurs the overhead
and computation cost of always reading a register from storage,
and potentially also writing a register to the storage.

Specifically, Emulator.NewReadOnlyBlockView() receives BlockContext
param without actually using it.  Creating BlockContext requires
reading BlockProposal from storage, which incurs GetValue and
potentially SetValue computation cost.

This commit removes the unnecessary BlockContext param.  This
optimization reduces computation cost in these four EVM functions:
- EVMAddress.balance()
- EVMAddress.code()
- EVMAddress.codeHash()
- EVMAddress.nonce()
@github-actions
Copy link
Copy Markdown
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 20, 2026

📝 Walkthrough

Walkthrough

This PR removes the BlockContext parameter from the NewReadOnlyBlockView() method signature across the EVM emulator interface definition, implementations, and all call sites. The method now creates read-only block views without requiring an explicit context argument.

Changes

Cohort / File(s) Summary
Interface & Core Implementation
fvm/evm/types/emulator.go, fvm/evm/emulator/emulator.go
Removed BlockContext parameter from NewReadOnlyBlockView() method signature in both interface definition and main implementation.
Test Utilities
fvm/evm/testutils/emulator.go, fvm/evm/testutils/accounts.go, fvm/evm/testutils/contract.go
Updated test utility implementations and calls to remove BlockContext argument when calling NewReadOnlyBlockView().
Handler & Tests
fvm/evm/handler/handler.go, fvm/evm/emulator/emulator_test.go
Simplified read operation paths by removing block context retrieval steps and directly calling NewReadOnlyBlockView(). Updated test call sites accordingly.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

Suggested labels

Breaking Change

Suggested reviewers

  • zhangchiqing
  • janezpodhostnik
  • peterargue
  • turbolent

Poem

🐰 No context needed, we've simplified the way,
Block views now emerge without a context to play,
Seven files aligned in harmonious change,
The API grows lighter across the whole range! 🎉

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: optimizing and reducing computation costs of EVM functions by removing an unnecessary BlockContext parameter from NewReadOnlyBlockView().

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

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fxamacker/avoid-creating-blockproposal-for-readonlyblockview

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
Copy Markdown
Contributor

@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)
fvm/evm/emulator/emulator.go (1)

61-66: Optional: return nil on error for consistency.

newProcedure() (line 332) correctly returns nil, err when state.NewStateDB fails. NewReadOnlyBlockView returns a non-nil *ReadOnlyBlockView alongside the error — a pre-existing inconsistency that could silently bite callers who don't check the error.

♻️ Suggested fix
 func (em *Emulator) NewReadOnlyBlockView() (types.ReadOnlyBlockView, error) {
 	execState, err := state.NewStateDB(em.ledger, em.rootAddr)
+	if err != nil {
+		return nil, err
+	}
 	return &ReadOnlyBlockView{
 		state: execState,
-	}, err
+	}, nil
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@fvm/evm/emulator/emulator.go` around lines 61 - 66,
Emulator.NewReadOnlyBlockView currently returns a non-nil *ReadOnlyBlockView
even when state.NewStateDB fails; change it to mirror newProcedure() by checking
err immediately after calling state.NewStateDB and returning nil, err if
non-nil, otherwise construct and return &ReadOnlyBlockView{state: execState},
nil so callers never receive a non-nil view alongside an error.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@fvm/evm/emulator/emulator.go`:
- Around line 61-66: Emulator.NewReadOnlyBlockView currently returns a non-nil
*ReadOnlyBlockView even when state.NewStateDB fails; change it to mirror
newProcedure() by checking err immediately after calling state.NewStateDB and
returning nil, err if non-nil, otherwise construct and return
&ReadOnlyBlockView{state: execState}, nil so callers never receive a non-nil
view alongside an error.

Copy link
Copy Markdown
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Nice!

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 20, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
fvm/evm/handler/handler.go 75.00% 1 Missing ⚠️
fvm/evm/testutils/accounts.go 0.00% 1 Missing ⚠️
fvm/evm/testutils/contract.go 0.00% 1 Missing ⚠️
fvm/evm/testutils/emulator.go 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@fxamacker fxamacker added this pull request to the merge queue Feb 20, 2026
Copy link
Copy Markdown
Collaborator

@m-Peter m-Peter left a comment

Choose a reason for hiding this comment

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

Nice 👏

Merged via the queue into master with commit 94df417 Feb 20, 2026
61 checks passed
@fxamacker fxamacker deleted the fxamacker/avoid-creating-blockproposal-for-readonlyblockview branch February 20, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants