eth/tracers: nopersist txhash and flatten parity trace

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2024-09-27 02:49:44 +00:00
parent f08d9a058b
commit 92a0bc003c
2 changed files with 33 additions and 13 deletions

View file

@ -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)

View file

@ -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