From daec607285795a6484f88e51b06f05e5dce6b608 Mon Sep 17 00:00:00 2001 From: Tyler Smith Date: Fri, 15 Sep 2023 02:51:00 -0400 Subject: [PATCH] fix: Keep streaming blocks until subscription is closed. --- eth/filters/trace_api.go | 120 ++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/eth/filters/trace_api.go b/eth/filters/trace_api.go index 37f7392ade9b..349c8cf5fa05 100644 --- a/eth/filters/trace_api.go +++ b/eth/filters/trace_api.go @@ -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) + } } }()