Skip to content

feat: add tracing support for StateSyncTx#2062

Open
milando12 wants to merge 19 commits into0xPolygon:developfrom
milando12:md/feat-tracer
Open

feat: add tracing support for StateSyncTx#2062
milando12 wants to merge 19 commits into0xPolygon:developfrom
milando12:md/feat-tracer

Conversation

@milando12
Copy link

@milando12 milando12 commented Feb 18, 2026

Description

Adding tracing support for StateSyncTx.

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

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

Ran the Node with this code for few days - no problems showed up and I verified that StateSyncTx trace that we get is correct.

@milando12 milando12 marked this pull request as draft February 18, 2026 12:08
@milando12 milando12 changed the base branch from develop to master February 18, 2026 12:14
start := time.Now()
cx := statefull.ChainContext{Chain: chain, Bor: c}

// Start tracing StateSyncTx (if present in the block body)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we might have integration tests locally (we maintain our own Polygon Live Tracing branch for now).

I'll check if I could add that on top of your PR.

Copy link
Author

Choose a reason for hiding this comment

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

That sounds great. Feel free to add them on top of this PR whenever you're ready.

- Introduced `vm.Config` to multiple functions for customizable VM tracing.
- Added tracer configuration flags (`vmtrace` and `vmtrace.jsonconfig`) to streamline monitoring during blockchain synchronization.
- Updated GenesisContract and spanner interfaces for trace integration.
- Integrated tracing hooks for StateSyncTx events.
Remove the 4 [DEBUG] log lines that were added during development
to diagnose StateSyncTx tracing issues.
When Finalize is called, the chain parameter can be a *core.HeaderChain
(not just *core.BlockChain). Without this change, the tracer in vmCfg
is nil during finalization, so StateSyncTx tracing hooks never fire.

- Add vmConfig field and GetVMConfig() method to HeaderChain
- Set hc.vmConfig in NewBlockChain after creating the HeaderChain
- Add else-if branch in Finalize and FinalizeAndAssemble to extract
  VM config from HeaderChain
The Process function creates a tracingStateDB wrapper for tracing but
was passing the unwrapped statedb to Finalize, causing the tracer to
miss StateSyncTx state changes.
@milando12 milando12 marked this pull request as ready for review February 23, 2026 15:01
Expand the `CommitState` method signature to include the `vm.Config` parameter, enabling proper VM tracing integration and configuration.
EVM struct has a comment saying never reuse
@pratikspatil024 pratikspatil024 requested a review from a team February 25, 2026 11:05
@pratikspatil024 pratikspatil024 changed the base branch from master to develop February 25, 2026 11:08
Add support for preconf and private transaction APIs

- Introduce new flags and APIs for preconf and private transactions.
- Add methods for transaction preconfirmation and private transaction handling in backends and API layers.
- Update transaction submission logic to support preconf/private paths.
- Include tests for all new functionalities and edge cases.
- Update method signatures to include `vm.Config`, enabling better VM tracing and configuration integration in testing and state processing logic.
…lper function

- Consolidate duplicate code for retrieving `vm.Config` from `ChainHeaderReader`.
- Simplify `Finalize` method by utilizing the new helper.
The state processor now passes the traced (hooked) StateDB to the consensus engine's Finalize method, so live tracers can observe balance changes from block reward distribution. This is the correct behavior — previously, Finalize received the raw StateDB, making block rewards invisible to live tracers like the supply tracer.

Update TestSupplyRewards and TestSupplyEip1559Burn to include the expected ethash block reward (2 ETH) in their assertions, matching the new traced output.
@pratikspatil024
Copy link
Member

@claude - review this PR.

@claude
Copy link

claude bot commented Mar 2, 2026

Claude encountered an error —— View job

Command failed: git fetch origin --depth=20 md/feat-tracer

I'll analyze this and get back to you.

@kamuikatsurgi
Copy link
Member

kamuikatsurgi commented Mar 2, 2026

@claude - review this PR.

It won't be able to review this PR is from a fork, and hence it doesn't have perms to fetch the branch.

@claude
Copy link

claude bot commented Mar 2, 2026

Claude encountered an error —— View job

Command failed: git fetch origin --depth=20 md/feat-tracer

I'll analyze this and get back to you.

cx := statefull.ChainContext{Chain: chain, Bor: c}

// Start tracing StateSyncTx (if present in the block body)
if hooks := vmCfg.Tracer; hooks != nil && hooks.OnTxStart != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

it seems like this starts a single tx trace before checkAndCommitSpan and the CommitStates loop, but statefull.ApplyMessage does not emit per-call OnTxStart/OnTxEnd hooks. Can this cause any issue on sprint blocks (span commits and multiple state-syncs) breaking tracers that keep single tx state? If that's the case, maybe the hooks need to wrap each system call, not the whole finalize.

})
body.Transactions = append(body.Transactions, stateSyncTx)
receipts = insertStateSyncTransactionAndCalculateReceipt(stateSyncTx, header, body, state, receipts)
receipts = insertStateSyncTransactionAndCalculateReceipt(stateSyncTx, header, body, state, receipts, vmCfg)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we make the tracer lifecycle symmetric between Finalize and FinalizeAndAssemble? I think there's a difference there wrt insertStateSyncTransactionAndCalculateReceipt calls

"github.com/ethereum/go-ethereum/params"

// Force-load the tracer engines to trigger registration
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to register js and native tracers here? IIUC, you're only leveraging LiveDirectory in this PR.

backtrace = "" # Request a stack trace at a specific logging statement (e.g. "block.go:271")
debug = true # Prepends log messages with call-site location (file and line number)
enable-block-tracking = false # Enables additional logging of information collected while tracking block lifecycle
vmtrace = "supply" # Name of a tracer to record internal VM operations during blockchain synchronization (costly)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't VMTrace and VMTraceJsonConfig be top level fields? I see they’re under [log] here

return Callmsg{
ethereum.CallMsg{
From: systemAddress,
From: SystemAddress,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use a getter here instead of exposing?

@marcello33
Copy link
Collaborator

Can we ad integration tests that verify the trace output for a block containing a StateSyncTx?
Also, I think you need to solve the conflicts with base branch

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
5.2% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

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.

5 participants