-
Notifications
You must be signed in to change notification settings - Fork 591
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
Changes from 17 commits
715c921
7b13a88
12e21b6
276a404
2b43baf
afa51c2
ee7c122
f1e065e
9e48c5e
8ddbaad
911e13f
fbbc506
61ffef5
8c27ab3
52a90b4
d7d8dca
c2d6e33
ce855ca
bd255d4
8304f4a
dfd2f16
1ac6590
ce6db80
e28eec5
cd0d9c7
5f16ec6
9d1096f
83b8a83
6f2062c
cddeedf
2a4d484
711f500
ec342cd
31e41da
f579f51
d8cb4ab
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 |
|---|---|---|
|
|
@@ -1123,6 +1123,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 { | ||
|
|
@@ -1139,20 +1152,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 { | ||
|
manav2401 marked this conversation as resolved.
Outdated
|
||
| 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.SystemAddress) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 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 | ||
|
|
@@ -1183,7 +1213,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 { | ||
|
|
@@ -1194,7 +1224,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 | ||
|
|
@@ -1227,6 +1257,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 | ||
|
|
@@ -1285,20 +1321,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 | ||
|
|
@@ -1324,7 +1362,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) | ||
|
manav2401 marked this conversation as resolved.
Outdated
|
||
| } else { | ||
| // set state sync | ||
| bc := chain.(core.BorStateSyncer) | ||
|
|
@@ -1526,6 +1564,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() | ||
|
|
@@ -1542,7 +1581,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 | ||
|
|
@@ -1581,6 +1620,7 @@ func (c *Bor) FetchAndCommitSpan( | |
| state vm.StateDB, | ||
| header *types.Header, | ||
| chain core.ChainContext, | ||
| vmCfg vm.Config, | ||
| ) error { | ||
| var ( | ||
| minSpan borTypes.Span | ||
|
|
@@ -1644,14 +1684,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() | ||
|
|
@@ -1765,7 +1806,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 | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.