Skip to content

feat: support CopyTrie() for reconstructed tries#5394

Open
RodrigoVillar wants to merge 5 commits into
masterfrom
rodrigo/fwd-copy-trie
Open

feat: support CopyTrie() for reconstructed tries#5394
RodrigoVillar wants to merge 5 commits into
masterfrom
rodrigo/fwd-copy-trie

Conversation

@RodrigoVillar
Copy link
Copy Markdown
Contributor

@RodrigoVillar RodrigoVillar commented May 19, 2026

Why this should be merged

Previously, trying to copy a reconstructed account trie returned nil as reconstructed revisions (i.e. the pointer backing reconstructed account tries) are not safe to share. However, methods such as eth_accessList and eth_estimateGas call CopyTrie() - in the case that the trie is a reconstructed account trie, this would eventually result in these operations trying to read against a nil-pointer, resulting in failed queries.

This PR fixes this by enabling CopyTrie() to deep copy a reconstructed account trie if one is passed in.

How this works

This PR adds a Copy() method to the reconstructed account trie type which returns a deep copy. This method utilizes the Clone() method which was added as part of Firewood v0.5.0.

How this was tested

CI + added two UTs:

  • TestReconstructedCopyTrie()
  • TestFirewoodCopyTrie()

Need to be documented in RELEASES.md?

No

@RodrigoVillar RodrigoVillar self-assigned this May 19, 2026
@RodrigoVillar RodrigoVillar added evm Related to EVM functionality firewood labels May 19, 2026
@RodrigoVillar RodrigoVillar force-pushed the rodrigo/fwd-copy-trie branch 2 times, most recently from 9e57724 to a18e346 Compare May 19, 2026 19:32
@RodrigoVillar RodrigoVillar requested a review from Copilot May 19, 2026 19:55
Copy link
Copy Markdown
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 fixes failures in RPC paths (e.g., eth_accessList, eth_estimateGas) when they call CopyTrie() on reconstructed account tries by adding support for deep-copying reconstructed tries rather than returning nil.

Changes:

  • Added (*reconstructedAccountTrie).Copy() that clones the underlying ffi.Reconstructed handle and rebinds the base trie reader to the clone.
  • Updated reconstructedStateAccessor.CopyTrie() to return a copied reconstructed trie (and log on copy errors) instead of always returning nil.
  • Added unit test TestReconstructedCopyTrie() to validate the copied trie is usable and independent.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
graft/evm/firewood/reconstructed_trie.go Adds Copy() to deep-copy reconstructed tries by cloning the ffi.Reconstructed handle and rebinding the reader.
graft/evm/firewood/reconstructed_state.go Uses the new Copy() implementation from CopyTrie() for reconstructed account tries.
graft/evm/firewood/reconstructed_state_test.go Adds a unit test verifying CopyTrie() returns an independent reconstructed trie.

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

Comment thread graft/evm/firewood/reconstructed_trie.go
Comment thread graft/evm/firewood/reconstructed_state.go
Comment thread graft/evm/firewood/reconstructed_state_test.go Outdated
@RodrigoVillar RodrigoVillar force-pushed the rodrigo/fwd-copy-trie branch from a18e346 to fd604af Compare May 19, 2026 20:41
@RodrigoVillar RodrigoVillar marked this pull request as ready for review May 19, 2026 20:48
@RodrigoVillar RodrigoVillar requested a review from a team as a code owner May 19, 2026 20:48
Copy link
Copy Markdown
Contributor

@alarso16 alarso16 left a comment

Choose a reason for hiding this comment

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

The point of this was to fix a unit-testable issue with an API - doesn't this fully fix that issue? Will you add that to this PR?

Comment thread graft/evm/firewood/reconstructed_state.go
@RodrigoVillar RodrigoVillar force-pushed the rodrigo/fwd-copy-trie branch from fd604af to 5b5476b Compare May 21, 2026 18:53
@RodrigoVillar
Copy link
Copy Markdown
Contributor Author

RodrigoVillar commented May 21, 2026

The point of this was to fix a unit-testable issue with an API - doesn't this fully fix that issue? Will you add that to this PR?

@alarso16 I added TestReconstructedCopyTrie() which tested the root cause of the bug (i.e inability to copy reconstructed account tries). To your point though, I've added another UT - TestFirewoodCopyTrie() - which calls both eth_estimateGas and eth_createAccessList under the reexecution path (which previously failed) and succeeds.

@RodrigoVillar RodrigoVillar requested a review from alarso16 May 21, 2026 19:34
// generated via block re-execution. In that case, Firewood serves a reconstructed account
// trie, and copying that trie used to fail as copying reconstructed revisions was
// previously unsupported.
func TestFirewoodCopyTrie(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You describe this test in relation to its fix, but it would be better to describe it relative to what functionality it actually needs: TestFirewoodModifiedStateRPC or something

Comment on lines +2385 to +2386
restartCTX.NetworkUpgrades = upgradetest.GetConfig(fork)
restartCTX.ChainDataDir = tvm.Ctx.ChainDataDir
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

these don't make sense immediately, add a short comment

blockNumOrHash := rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(blockNum))

// eth_estimateGas params.
var (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

subjective: separate subtest per block/transaction? it would help with debugging, but the nesting would likely feel unnecessarily deep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

evm Related to EVM functionality firewood

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants