Skip to content

Conversation

@alarso16
Copy link
Contributor

@alarso16 alarso16 commented Dec 9, 2025

Why this should be merged

Now that using Revision is concurrent safe and guaranteed to not cause undefined behavior, it should be used a more canonical way of accessing state.

How this works

Uses Firewood's Revision instead of relying on a one-Revision cache internal to Rust. The Revision has a finalizer attached to release all related memory when there are no more references.

How this was tested

CI and re-execution tests

Need to be documented in RELEASES.md?

No

@alarso16 alarso16 self-assigned this Dec 9, 2025
@alarso16 alarso16 added cleanup Code quality improvement evm labels Dec 9, 2025
Base automatically changed from alarso16/firewood-v0.0.16 to master December 9, 2025 19:48
@alarso16 alarso16 force-pushed the alarso16/firewood-revisions branch from b571ed4 to 28bc9e7 Compare December 11, 2025 15:07
@alarso16 alarso16 marked this pull request as ready for review December 11, 2025 15:49
@alarso16 alarso16 requested a review from a team as a code owner December 11, 2025 15:49
Copilot AI review requested due to automatic review settings December 11, 2025 15:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the Firewood integration to use Revision objects directly for accessing state, improving concurrency safety and memory management. The change eliminates reliance on an internal one-Revision cache in favor of Firewood's native revision system with automatic memory cleanup via finalizers.

  • Replaces GetFromRoot calls with Revision objects that have built-in memory management
  • Updates the reader struct to hold a Revision instead of database reference and root hash
  • Adds explicit cleanup of state references in the legacy transaction pool

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
graft/coreth/triedb/firewood/database.go Refactors Reader to use Firewood Revision objects instead of root hash lookups
graft/coreth/core/txpool/legacypool/legacypool.go Adds explicit cleanup of state references during pool closure

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@AminR443 AminR443 left a comment

Choose a reason for hiding this comment

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

LGTM. One comment on dropping:

The Revision has a finalizer attached to release all related memory when there are no more references.

Revisions hanging around will prevent the revision manager from reaping the nodestores, and this may not be good from a memory perspective. Last I checked, finalizers were considered expensive and could run at an arbitrary time after becoming unreachable. What's preventing us from explicitly dropping the revision?

Also, if we're going with finalizers, is there any particular reason we're not using runtime.AddCleanup?

@alarso16
Copy link
Contributor Author

Revisions hanging around will prevent the revision manager from reaping the nodestores, and this may not be good from a memory perspective. Last I checked, finalizers were considered expensive and could run at an arbitrary time after becoming unreachable. What's preventing us from explicitly dropping the revision?

We don't know when it will no longer be used. There's very strict interfaces to adhere to, and that includes that we cannot assume there will be any behavior to indicate the Reader is no longer used.

Also, if we're going with finalizers, is there any particular reason we're not using runtime.AddCleanup?

No particular reason! In our case, they should be equivalent. We know that they are properly being cleaned because closing the database doesn't hang. We could use runtime.AddCleanup in Firewood, and that's worth thinking about

@AminR443
Copy link

Sounds good to me. I think we should consider using runtime.AddCleanup, since Go’s docs suggest it as the new go-to option:

New Go code should consider using [AddCleanup](https://pkg.go.dev/runtime#AddCleanup) instead, which is much less error-prone than SetFinalizer.

I'll create an issue for that in firewood repo.

@JonathanOppenheimer JonathanOppenheimer added coreth Related to the former coreth standalone repository evm Related to EVM functionality need-subnet-evm-port labels Dec 15, 2025
@alarso16 alarso16 force-pushed the alarso16/firewood-revisions branch from 28bc9e7 to 18464ef Compare December 15, 2025 20:27
@alarso16 alarso16 requested a review from a team as a code owner December 15, 2025 20:27
@alarso16 alarso16 changed the base branch from master to alarso16/shared-firewood December 15, 2025 20:28
@alarso16 alarso16 added subnet-evm Related to the former subnet-evm standalone repository and removed need-subnet-evm-port labels Dec 15, 2025
}

// remove all references to state to allow GC to reclaim memory
pool.pendingNonces = nil
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These keep around a StateDB, which holds onto the *firewood.accountTrie, so the database closure will hang unless we remove these references. Another (worse) option is that on state.Trie.Copy(), we don't copy the Reader, and instead use GetFromRoot then. This seems too messy, so I have this small line change instead.

@alarso16 alarso16 force-pushed the alarso16/shared-firewood branch from 6b31642 to 9ab772f Compare December 15, 2025 20:42
@alarso16 alarso16 force-pushed the alarso16/firewood-revisions branch from 18464ef to 72b24db Compare December 15, 2025 21:44
@blacksmith-sh

This comment was marked as outdated.

@alarso16 alarso16 force-pushed the alarso16/firewood-revisions branch from 72b24db to 236566e Compare December 15, 2025 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleanup Code quality improvement coreth Related to the former coreth standalone repository evm Related to EVM functionality subnet-evm Related to the former subnet-evm standalone repository

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants