Skip to content

core, consensus: handle statedb interface conversion for hooked state db#1623

Merged
manav2401 merged 6 commits intodevelopfrom
manav/hooked-statedb-fix
Jul 21, 2025
Merged

core, consensus: handle statedb interface conversion for hooked state db#1623
manav2401 merged 6 commits intodevelopfrom
manav/hooked-statedb-fix

Conversation

@manav2401
Copy link
Copy Markdown
Member

@manav2401 manav2401 commented Jul 9, 2025

Description

Fixes #1577

Earlier in bor consensus, we just used to extract the underlying db by type conversion (as it's just wrapped by the vm.StateDB interface) like this:

state := wrappedState.(*state.StateDB)

This may panic (as the issue suggests) when the underlying db is not simple *state.StateDB but a hooked state db if tracing is enabled. It's a wrapper over *state.StateDB but handles tracing hooks for abstraction. Moreover, it won't allow live tracing as we're directly using the state db instance.

This PR passes the wrapped state db (interface) to consensus methods (using state for committing spans and bridge events).

Moreover, it also adds 2 additional interface methods

  • SetBalance: For setting balance which is used by bor consensus when there's a hard fork (the new interface doesn't support it so need to add it for tracing)
  • Inner: Returns the inner state db instance (for hooked as well as normal state db)

We could have handled the interface conversion for hooked state db as well to solve this simply but the SetBalance call wouldn't be tracked if tracing is enabled.

Changes

  • Bugfix (non-breaking change that solves an issue)
  • Hotfix (change that solves an urgent issue, and requires immediate attention)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (change that is not backwards-compatible and/or changes current functionality)
  • Changes only for a subset of nodes

Breaking changes

Please complete this section if any breaking changes have been made, otherwise delete it

Nodes audience

In case this PR includes changes that must be applied only to a subset of nodes, please specify how you handled it (e.g. by adding a flag with a default value...)

Checklist

  • I have added at least 2 reviewer or the whole pos-v1 team
  • I have added sufficient documentation in code
  • I will be resolving comments - if any - by pushing each fix in a separate commit and linking the commit hash in the comment reply
  • Created a task in Jira and informed the team for implementation in Erigon client (if applicable)
  • Includes RPC methods changes, and the Notion documentation has been updated

Cross repository changes

  • This PR requires changes to heimdall
    • In case link the PR here:
  • This PR requires changes to matic-cli
    • In case link the PR here:

Testing

  • I have added unit tests
  • I have added tests to CI
  • I have tested this code manually on local environment
  • I have tested this code manually on remote devnet using express-cli
  • I have tested this code manually on amoy
  • I have created new e2e tests into express-cli

Manual tests

Please complete this section with the steps you performed if you ran manual tests for this functionality, otherwise delete it

Additional comments

Please post additional comments in this section if you have them, otherwise delete it

@manav2401 manav2401 requested a review from a team July 9, 2025 16:52
@maoueh
Copy link
Copy Markdown
Contributor

maoueh commented Jul 9, 2025

@manav2401 While fixing the problem at directly, the fix is incorrect from a Live Tracer perspective. Indeed, you are now passing around a non-hooked StateDB, which means tracing will never be enabled for state changes happening within Finalize.

This is problematic as it means a bunch of Polygon state changes wouldn't be traced correctly anymore, since all operation here are now performed using a "vanilla" StateDB.

Review Let me double check my claim, as you passed a hooked stateDB at least in changeContractCodeIfNeeded, I'll verify if it would traced everywhere correctly.

After review, there is still some places where the wrappedState would need to be passed along, in https://github.com/maticnetwork/bor/pull/1623/files#diff-e2acd76a9636afbcc13d882e2f46ca9bf74e0f9fa6c190c7ba75328a98fd364fL902 and the same below it.

But everything should work properly, we can update our own PR on top of yours just to adjust the changes related to commitSpan and the like.

@manav2401
Copy link
Copy Markdown
Member Author

Hi @maoueh, thanks for your inputs. I initially thought the remaining functions like checkAndCommitSpans and CommitStates won't need the wrapped db as they're not invoking functions where tracing is implemented. But they're doing calls to update the genesis contracts for spans and bridge events which means it needs tracing. I can update my PR to reflect those changes if that's fine.

I think your PR might also work but there are some extra interface methods you've added which may not be needed.

@manav2401
Copy link
Copy Markdown
Member Author

Okay, I found a few minor issues around using state db correctly which I've fixed. I've also updated the PR to pass around vm.StateDB for other consensus functions.

@maoueh
Copy link
Copy Markdown
Contributor

maoueh commented Jul 10, 2025

@manav2401 yeah with the remaining passing around of vm.StateDB interface, I think the PR will not be needed anymore.

But they're doing calls to update the genesis contracts for spans and bridge events which means it needs tracing. I can update my PR to reflect those changes if that's fine.

Yeah we have follow up PRs to wrap more nicely the tracing around those part too.

Copy link
Copy Markdown
Contributor

@cffls cffls left a comment

Choose a reason for hiding this comment

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

LGTM

@manav2401 manav2401 merged commit 0c1433c into develop Jul 21, 2025
9 of 10 checks passed
@manav2401 manav2401 deleted the manav/hooked-statedb-fix branch July 21, 2025 19:16
nebojsa94 pushed a commit to nebojsa94/bor that referenced this pull request Aug 14, 2025
… db (0xPolygon#1623)

Pass around a wrapper `vm.StateDB` instance and expose public methods for accessing underlying state db which can be different for normal v/s tracing mode.
@kamuikatsurgi kamuikatsurgi mentioned this pull request Aug 20, 2025
18 tasks
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.

4 participants