Skip to content
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

fix: Keep streaming blocks until subscription is closed. #110

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 61 additions & 59 deletions eth/filters/trace_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,73 +143,75 @@ func (api *FilterAPI) NewFullBlocksWithTrace(ctx context.Context, tracerOptsJSON
}

var hashes []common.Hash
select {
case r := <-reorgs:
// Reverse the added blocks in the reorgs, excluding the latest block
// as it will be emitted on the newHeads channels.
hashes = make([]common.Hash, 0, len(r.Added)-1)
for i := len(r.Added) - 1; i > 0; i-- {
hashes = append(hashes, r.Added[i])
}
case h := <-headers:
hashes = []common.Hash{h.Hash()}
case <-headersSub.Err():
return
case <-reorgSub.Err():
return
case <-notifier.Closed():
return
}

for _, hash := range hashes {
block, err := api.sys.backend.BlockByHash(ctx, hash)
if err != nil {
log.Error("failed to get block", "err", err, "hash", hash)
continue
}

marshalBlock, err := RPCMarshalBlock(block, true, true, api.sys.backend.ChainConfig())
if err != nil {
continue
for {
select {
case r := <-reorgs:
// Reverse the added blocks in the reorgs, excluding the latest block
// as it will be emitted on the newHeads channels.
hashes = make([]common.Hash, 0, len(r.Added)-1)
for i := len(r.Added) - 1; i > 0; i-- {
hashes = append(hashes, r.Added[i])
}
case h := <-headers:
hashes = []common.Hash{h.Hash()}
case <-headersSub.Err():
return
case <-reorgSub.Err():
return
case <-notifier.Closed():
return
}

trace, err := traceBlock(block, chainConfig, api.sys.chain, tracerOpts)
if err != nil {
log.Error("failed to trace block", "err", err, "block", block.Number())
continue
}
marshalBlock["trace"] = trace
for _, hash := range hashes {
block, err := api.sys.backend.BlockByHash(ctx, hash)
if err != nil {
log.Error("failed to get block", "err", err, "hash", hash)
continue
}

marshalReceipts := make(map[common.Hash]map[string]interface{})
receipts, err := api.sys.backend.GetReceipts(ctx, hash)
if err != nil {
continue
}
for index, receipt := range receipts {
fields := map[string]interface{}{
"transactionIndex": hexutil.Uint64(index),
"gasUsed": hexutil.Uint64(receipt.GasUsed),
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
"contractAddress": nil,
"logs": receipt.Logs,
"logsBloom": receipt.Bloom,
"status": hexutil.Uint64(receipt.Status),
marshalBlock, err := RPCMarshalBlock(block, true, true, api.sys.backend.ChainConfig())
if err != nil {
continue
}
if receipt.Logs == nil {
fields["logs"] = [][]*types.Log{}

trace, err := traceBlock(block, chainConfig, api.sys.chain, tracerOpts)
if err != nil {
log.Error("failed to trace block", "err", err, "block", block.Number())
continue
}
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
if receipt.ContractAddress != (common.Address{}) {
fields["contractAddress"] = receipt.ContractAddress
marshalBlock["trace"] = trace

marshalReceipts := make(map[common.Hash]map[string]interface{})
receipts, err := api.sys.backend.GetReceipts(ctx, hash)
if err != nil {
continue
}
if reason, ok := core.GetRevertReason(receipt.TxHash, hash); ok {
fields["revertReason"] = reason
for index, receipt := range receipts {
fields := map[string]interface{}{
"transactionIndex": hexutil.Uint64(index),
"gasUsed": hexutil.Uint64(receipt.GasUsed),
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
"contractAddress": nil,
"logs": receipt.Logs,
"logsBloom": receipt.Bloom,
"status": hexutil.Uint64(receipt.Status),
}
if receipt.Logs == nil {
fields["logs"] = [][]*types.Log{}
}
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
if receipt.ContractAddress != (common.Address{}) {
fields["contractAddress"] = receipt.ContractAddress
}
if reason, ok := core.GetRevertReason(receipt.TxHash, hash); ok {
fields["revertReason"] = reason
}
marshalReceipts[receipt.TxHash] = fields
}
marshalReceipts[receipt.TxHash] = fields
}
marshalBlock["receipts"] = marshalReceipts
marshalBlock["receipts"] = marshalReceipts

notifier.Notify(rpcSub.ID, marshalBlock)
notifier.Notify(rpcSub.ID, marshalBlock)
}
}
}()

Expand Down