From cbd1e10e9eecac0c3f316099a6200e5056c09896 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Sat, 17 Aug 2024 11:52:59 +0000 Subject: [PATCH] feat(filter): implement a simple rlp.Encode/Decoder Signed-off-by: jsvisa --- eth/tracers/live/filter.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/eth/tracers/live/filter.go b/eth/tracers/live/filter.go index 2a70e4f9ec..0fafb4d9ff 100644 --- a/eth/tracers/live/filter.go +++ b/eth/tracers/live/filter.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "os" "path" "strconv" @@ -20,6 +21,7 @@ import ( "github.com/ethereum/go-ethereum/eth/tracers/native" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" ) @@ -37,6 +39,26 @@ type traceResult struct { 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}) +} + +// 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) +} + type filter struct { backend tracers.Backend kvdb ethdb.Database @@ -249,7 +271,7 @@ func (f *filter) OnBlockEnd(err error) { number := f.latest.Load() hash := f.hash for name, traces := range f.traces { - data, err := json.Marshal(traces) + data, err := rlp.EncodeToBytes(traces) if err != nil { log.Error("Failed to marshal traces", "error", err) break @@ -301,7 +323,7 @@ func (f *filter) readBlockTraces(ctx context.Context, name string, blknum uint64 } var traces []*traceResult - err = json.Unmarshal(data, &traces) + err = rlp.DecodeBytes(data, &traces) return traces, err }