-
Notifications
You must be signed in to change notification settings - Fork 582
feat: add tracing support for StateSyncTx #2062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
715c921
7b13a88
12e21b6
276a404
2b43baf
afa51c2
ee7c122
f1e065e
9e48c5e
8ddbaad
911e13f
fbbc506
61ffef5
8c27ab3
52a90b4
d7d8dca
c2d6e33
ce855ca
bd255d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1170,6 +1170,19 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header, w | |
| return nil | ||
| } | ||
|
|
||
| // extractVMConfig retrieves the vm.Config from the chain reader, if available. | ||
| func extractVMConfig(chain consensus.ChainHeaderReader) vm.Config { | ||
| if bc, ok := chain.(*core.BlockChain); ok { | ||
| return *bc.GetVMConfig() | ||
| } | ||
| if hc, ok := chain.(*core.HeaderChain); ok { | ||
| if cfg := hc.GetVMConfig(); cfg != nil { | ||
| return *cfg | ||
| } | ||
| } | ||
| return vm.Config{} | ||
| } | ||
|
|
||
| // Finalize implements consensus.Engine, ensuring no uncles are set, nor block | ||
| // rewards given. | ||
| func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header, wrappedState vm.StateDB, body *types.Body, receipts []*types.Receipt) []*types.Receipt { | ||
|
|
@@ -1186,20 +1199,37 @@ func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header, | |
| err error | ||
| ) | ||
|
|
||
| vmCfg := extractVMConfig(chain) | ||
|
|
||
| if IsSprintStart(headerNumber, c.config.CalculateSprint(headerNumber)) { | ||
| start := time.Now() | ||
| cx := statefull.ChainContext{Chain: chain, Bor: c} | ||
|
|
||
| // Start tracing StateSyncTx (if present in the block body) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| if hooks := vmCfg.Tracer; hooks != nil && hooks.OnTxStart != nil { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems like this starts a single tx trace before |
||
| if c.config.IsMadhugiri(header.Number) && len(body.Transactions) > 0 { | ||
| lastTx := body.Transactions[len(body.Transactions)-1] | ||
| if lastTx.Type() == types.StateSyncTxType { | ||
| vmenv := vm.NewEVM( | ||
| core.NewEVMBlockContext(header, cx, &header.Coinbase), | ||
| wrappedState, c.chainConfig, vmCfg, | ||
| ) | ||
| hooks.OnTxStart(vmenv.GetVMContext(), lastTx, statefull.GetSystemAddress()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // check and commit span | ||
| if !c.config.IsRio(header.Number) { | ||
| if err := c.checkAndCommitSpan(wrappedState, header, cx); err != nil { | ||
| if err := c.checkAndCommitSpan(wrappedState, header, cx, vmCfg); err != nil { | ||
| log.Error("Error while committing span", "error", err) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| if c.HeimdallClient != nil { | ||
| // commit states | ||
| stateSyncData, err = c.CommitStates(wrappedState, header, cx) | ||
| stateSyncData, err = c.CommitStates(wrappedState, header, cx, vmCfg) | ||
| if err != nil { | ||
| log.Error("Error while committing states", "error", err) | ||
| return nil | ||
|
|
@@ -1230,7 +1260,7 @@ func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header, | |
| return receipts | ||
| } | ||
| if lastTx.Type() == types.StateSyncTxType { | ||
| receipts = insertStateSyncTransactionAndCalculateReceipt(lastTx, header, body, wrappedState, receipts) | ||
| receipts = insertStateSyncTransactionAndCalculateReceipt(lastTx, header, body, wrappedState, receipts, vmCfg) | ||
| } | ||
| } | ||
| } else { | ||
|
|
@@ -1241,7 +1271,7 @@ func (c *Bor) Finalize(chain consensus.ChainHeaderReader, header *types.Header, | |
| return receipts | ||
| } | ||
|
|
||
| func insertStateSyncTransactionAndCalculateReceipt(stateSyncTx *types.Transaction, header *types.Header, body *types.Body, state vm.StateDB, receipts []*types.Receipt) []*types.Receipt { | ||
| func insertStateSyncTransactionAndCalculateReceipt(stateSyncTx *types.Transaction, header *types.Header, body *types.Body, state vm.StateDB, receipts []*types.Receipt, vmConfig vm.Config) []*types.Receipt { | ||
| allLogs := state.Logs() | ||
| sort.SliceStable(allLogs, func(i, j int) bool { | ||
| return allLogs[i].Index < allLogs[j].Index | ||
|
|
@@ -1274,6 +1304,12 @@ func insertStateSyncTransactionAndCalculateReceipt(stateSyncTx *types.Transactio | |
| } | ||
|
|
||
| stateSyncReceipt.Bloom = types.CreateBloom(stateSyncReceipt) | ||
|
|
||
| // End tracing for StateSyncTx | ||
| if hooks := vmConfig.Tracer; hooks != nil && hooks.OnTxEnd != nil { | ||
| hooks.OnTxEnd(stateSyncReceipt, nil) | ||
| } | ||
|
|
||
| receipts = append(receipts, stateSyncReceipt) | ||
|
|
||
| return receipts | ||
|
|
@@ -1332,20 +1368,22 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *typ | |
| err error | ||
| ) | ||
|
|
||
| vmCfg := extractVMConfig(chain) | ||
|
|
||
| if IsSprintStart(headerNumber, c.config.CalculateSprint(headerNumber)) { | ||
| cx := statefull.ChainContext{Chain: chain, Bor: c} | ||
|
|
||
| // check and commit span | ||
| if !c.config.IsRio(header.Number) { | ||
| if err = c.checkAndCommitSpan(state, header, cx); err != nil { | ||
| if err = c.checkAndCommitSpan(state, header, cx, vmCfg); err != nil { | ||
| log.Error("Error while committing span", "error", err) | ||
| return nil, nil, 0, err | ||
| } | ||
| } | ||
|
|
||
| if c.HeimdallClient != nil { | ||
| // commit states | ||
| stateSyncData, err = c.CommitStates(state, header, cx) | ||
| stateSyncData, err = c.CommitStates(state, header, cx, vmCfg) | ||
| if err != nil { | ||
| log.Error("Error while committing states", "error", err) | ||
| return nil, nil, 0, err | ||
|
|
@@ -1371,7 +1409,7 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *typ | |
| StateSyncData: stateSyncData, | ||
| }) | ||
| body.Transactions = append(body.Transactions, stateSyncTx) | ||
| receipts = insertStateSyncTransactionAndCalculateReceipt(stateSyncTx, header, body, state, receipts) | ||
| receipts = insertStateSyncTransactionAndCalculateReceipt(stateSyncTx, header, body, state, receipts, vmCfg) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make the tracer lifecycle symmetric between |
||
| } else { | ||
| // set state sync | ||
| bc := chain.(core.BorStateSyncer) | ||
|
|
@@ -1573,6 +1611,7 @@ func (c *Bor) checkAndCommitSpan( | |
| state vm.StateDB, | ||
| header *types.Header, | ||
| chain core.ChainContext, | ||
| vmCfg vm.Config, | ||
| ) error { | ||
| var ctx = context.Background() | ||
| headerNumber := header.Number.Uint64() | ||
|
|
@@ -1589,7 +1628,7 @@ func (c *Bor) checkAndCommitSpan( | |
| tempState.IntermediateRoot(false) | ||
|
|
||
| if c.needToCommitSpan(span, headerNumber) { | ||
| return c.FetchAndCommitSpan(ctx, span.Id+1, state, header, chain) | ||
| return c.FetchAndCommitSpan(ctx, span.Id+1, state, header, chain, vmCfg) | ||
| } | ||
|
|
||
| return nil | ||
|
|
@@ -1628,6 +1667,7 @@ func (c *Bor) FetchAndCommitSpan( | |
| state vm.StateDB, | ||
| header *types.Header, | ||
| chain core.ChainContext, | ||
| vmCfg vm.Config, | ||
| ) error { | ||
| var ( | ||
| minSpan borTypes.Span | ||
|
|
@@ -1691,14 +1731,15 @@ func (c *Bor) FetchAndCommitSpan( | |
| ) | ||
| } | ||
|
|
||
| return c.spanner.CommitSpan(ctx, minSpan, validators, producers, state, header, chain) | ||
| return c.spanner.CommitSpan(ctx, minSpan, validators, producers, state, header, chain, vmCfg) | ||
| } | ||
|
|
||
| // CommitStates commit states | ||
| func (c *Bor) CommitStates( | ||
| state vm.StateDB, | ||
| header *types.Header, | ||
| chain statefull.ChainContext, | ||
| vmCfg vm.Config, | ||
| ) ([]*types.StateSyncData, error) { | ||
| fetchStart := time.Now() | ||
| number := header.Number.Uint64() | ||
|
|
@@ -1812,7 +1853,7 @@ func (c *Bor) CommitStates( | |
| // we expect that this call MUST emit an event, otherwise we wouldn't make a receipt | ||
| // if the receiver address is not a contract then we'll skip the most of the execution and emitting an event as well | ||
| // https://github.com/0xPolygon/genesis-contracts/blob/master/contracts/StateReceiver.sol#L27 | ||
| gasUsed, err = c.GenesisContractsClient.CommitState(eventRecord, state, header, chain) | ||
| gasUsed, err = c.GenesisContractsClient.CommitState(eventRecord, state, header, chain, vmCfg) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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
jsandnativetracers here? IIUC, you're only leveragingLiveDirectoryin this PR.