feat(filter): implement a simple rlp.Encode/Decoder

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2024-08-17 11:52:59 +00:00
parent 978112678c
commit cbd1e10e9e

View file

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"os" "os"
"path" "path"
"strconv" "strconv"
@ -20,6 +21,7 @@ import (
"github.com/ethereum/go-ethereum/eth/tracers/native" "github.com/ethereum/go-ethereum/eth/tracers/native"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
@ -37,6 +39,26 @@ type traceResult struct {
Error string `json:"error,omitempty"` // Trace failure 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})
}
// 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 { type filter struct {
backend tracers.Backend backend tracers.Backend
kvdb ethdb.Database kvdb ethdb.Database
@ -249,7 +271,7 @@ func (f *filter) OnBlockEnd(err error) {
number := f.latest.Load() number := f.latest.Load()
hash := f.hash hash := f.hash
for name, traces := range f.traces { for name, traces := range f.traces {
data, err := json.Marshal(traces) data, err := rlp.EncodeToBytes(traces)
if err != nil { if err != nil {
log.Error("Failed to marshal traces", "error", err) log.Error("Failed to marshal traces", "error", err)
break break
@ -301,7 +323,7 @@ func (f *filter) readBlockTraces(ctx context.Context, name string, blknum uint64
} }
var traces []*traceResult var traces []*traceResult
err = json.Unmarshal(data, &traces) err = rlp.DecodeBytes(data, &traces)
return traces, err return traces, err
} }