From e8161541e23dfe1dd6fae922fc55d21b43d6efc7 Mon Sep 17 00:00:00 2001 From: ledgerwatch Date: Tue, 27 Apr 2021 09:23:04 +0100 Subject: [PATCH] Use RLP encoding instead of json for subscriptions (#1825) Co-authored-by: Alexey Sharp --- cmd/rpcdaemon/filters/filters.go | 8 ++++---- ethdb/remote/remotedbserver/ethbackend.go | 22 ++++++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/cmd/rpcdaemon/filters/filters.go b/cmd/rpcdaemon/filters/filters.go index 87dac12c9..45dc301d7 100644 --- a/cmd/rpcdaemon/filters/filters.go +++ b/cmd/rpcdaemon/filters/filters.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "crypto/rand" - "encoding/json" "errors" "fmt" "io" @@ -123,7 +122,8 @@ func (ff *Filters) OnNewEvent(event *remote.SubscribeReply) { case remote.Event_HEADER: payload := event.Data var header types.Header - err := json.Unmarshal(payload, &header) + + err := rlp.Decode(bytes.NewReader(payload), &header) if err != nil { // ignoring what we can't unmarshal log.Warn("rpc filters, unprocessable payload", "err", err) @@ -135,7 +135,7 @@ func (ff *Filters) OnNewEvent(event *remote.SubscribeReply) { case remote.Event_PENDING_LOGS: payload := event.Data var logs types.Logs - err := json.Unmarshal(payload, &logs) + err := rlp.Decode(bytes.NewReader(payload), &logs) if err != nil { // ignoring what we can't unmarshal log.Warn("rpc filters, unprocessable payload", "err", err) @@ -147,7 +147,7 @@ func (ff *Filters) OnNewEvent(event *remote.SubscribeReply) { case remote.Event_PENDING_BLOCK: payload := event.Data var block types.Block - err := json.Unmarshal(payload, &block) + err := rlp.Decode(bytes.NewReader(payload), &block) if err != nil { // ignoring what we can't unmarshal log.Warn("rpc filters, unprocessable payload", "err", err) diff --git a/ethdb/remote/remotedbserver/ethbackend.go b/ethdb/remote/remotedbserver/ethbackend.go index 39043f686..322231c2a 100644 --- a/ethdb/remote/remotedbserver/ethbackend.go +++ b/ethdb/remote/remotedbserver/ethbackend.go @@ -3,7 +3,6 @@ package remotedbserver import ( "bytes" "context" - "encoding/json" "errors" "github.com/ledgerwatch/turbo-geth/common" @@ -74,13 +73,14 @@ func (s *EthBackendServer) Subscribe(r *remote.SubscribeRequest, subscribeServer default: } - payload, err := json.Marshal(h) - if err != nil { + var buf bytes.Buffer + if err := rlp.Encode(&buf, h); err != nil { log.Warn("error while marshaling a header", "err", err) return err } + payload := buf.Bytes() - err = subscribeServer.Send(&remote.SubscribeReply{ + err := subscribeServer.Send(&remote.SubscribeReply{ Type: remote.Event_HEADER, Data: payload, }) @@ -135,13 +135,14 @@ func (s *EthBackendServer) Subscribe(r *remote.SubscribeRequest, subscribeServer default: } - payload, err := json.Marshal(data) - if err != nil { + var buf bytes.Buffer + if err := rlp.Encode(&buf, data); err != nil { log.Warn("error while marshaling a pending logs", "err", err) return err } + payload := buf.Bytes() - err = subscribeServer.Send(&remote.SubscribeReply{ + err := subscribeServer.Send(&remote.SubscribeReply{ Type: remote.Event_PENDING_LOGS, Data: payload, }) @@ -164,13 +165,14 @@ func (s *EthBackendServer) Subscribe(r *remote.SubscribeRequest, subscribeServer default: } - payload, err := json.Marshal(data) - if err != nil { + var buf bytes.Buffer + if err := rlp.Encode(&buf, data); err != nil { log.Warn("error while marshaling a pending block", "err", err) return err } + payload := buf.Bytes() - err = subscribeServer.Send(&remote.SubscribeReply{ + err := subscribeServer.Send(&remote.SubscribeReply{ Type: remote.Event_PENDING_BLOCK, Data: payload, })