From 92a0bc003c9e890a906b3e678a7549c230b6c550 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 27 Sep 2024 02:49:44 +0000 Subject: [PATCH] eth/tracers: nopersist txhash and flatten parity trace Signed-off-by: jsvisa --- eth/tracers/live/filter.go | 12 +++++------- eth/tracers/live/filter_api.go | 34 ++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/eth/tracers/live/filter.go b/eth/tracers/live/filter.go index bb495a38c8..8268f00ebe 100644 --- a/eth/tracers/live/filter.go +++ b/eth/tracers/live/filter.go @@ -34,27 +34,25 @@ const ( ) type traceResult struct { - TxHash common.Hash `json:"txHash,omitempty"` // transaction hash - Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer - Error string `json:"error,omitempty"` // Trace failure produced by the tracer + TxHash *common.Hash `json:"txHash,omitempty"` // Transaction hash generated from block + Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer + Error string `json:"error,omitempty"` // Trace failure produced by the tracer } // EncodeRLP implments rlp.Encoder func (tr *traceResult) EncodeRLP(w io.Writer) error { - return rlp.Encode(w, []interface{}{tr.TxHash, tr.Result, tr.Error}) + return rlp.Encode(w, []interface{}{tr.Result, tr.Error}) } // DecodeRLP implements rlp.Decoder func (tr *traceResult) DecodeRLP(s *rlp.Stream) error { var temp struct { - TxHash common.Hash Result []byte Error string } if err := s.Decode(&temp); err != nil { return err } - tr.TxHash = temp.TxHash tr.Error = temp.Error return json.Unmarshal(temp.Result, &tr.Result) } @@ -250,7 +248,7 @@ func (f *filter) OnTxEnd(receipt *types.Receipt, err error) { f.tracer.OnTxEnd(receipt, err) for name, tt := range f.tracer.Tracers() { - trace := &traceResult{TxHash: receipt.TxHash} + trace := &traceResult{} result, err := tt.GetResult() if err != nil { log.Error("Failed to get tracer results", "number", f.latest.Load(), "error", err) diff --git a/eth/tracers/live/filter_api.go b/eth/tracers/live/filter_api.go index 9c398a524d..b56f22a80a 100644 --- a/eth/tracers/live/filter_api.go +++ b/eth/tracers/live/filter_api.go @@ -45,13 +45,35 @@ func (api *filterAPI) Block(ctx context.Context, blockNr rpc.BlockNumber, cfg *t if err != nil { return nil, err } - results := make([]interface{}, len(traces)) - for i, trace := range traces { - if tracer == "parityTracer" { - results[i] = trace.Result - } else { - results[i] = trace + + results := make([]interface{}, 0, len(traces)) + if tracer == "parityTracer" { + // Convert from []interface{} to []traceResult + for _, trace := range traces { + if parityTraces, ok := trace.Result.([]interface{}); ok { + results = append(results, parityTraces...) + } else { + return nil, errors.New("unexpected trace result type") + } } + return results, nil + } + + txHashes := make([]common.Hash, 0) + block, err := api.backend.BlockByNumber(ctx, blockNr) + if err != nil { + return nil, err + } + for _, tx := range block.Transactions() { + txHashes = append(txHashes, tx.Hash()) + } + if len(traces) != len(txHashes) { + return nil, errors.New("traces and transactions mismatch") + } + + for i, trace := range traces { + trace.TxHash = &txHashes[i] + results = append(results, trace) } return results, nil