Skip to content

Commit

Permalink
state/stateobject: skip lcoal revive in snap expired;
Browse files Browse the repository at this point in the history
rpc: using jsoniter as json encode/decoder;
  • Loading branch information
0xbundler committed Nov 10, 2023
1 parent 6b2bea2 commit a45c0a9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"sync/atomic"
"time"

jsoniter "github.com/json-iterator/go"

"github.com/ethereum/go-ethereum/log"
)

Expand Down Expand Up @@ -371,6 +373,7 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str
if result == nil {
return nil
}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
return json.Unmarshal(resp.Result, result)
}
}
Expand Down Expand Up @@ -453,6 +456,7 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
case resp.Result == nil:
elem.Error = ErrNoResult
default:
var json = jsoniter.ConfigCompatibleWithStandardLibrary
elem.Error = json.Unmarshal(resp.Result, elem.Result)
}
}
Expand Down Expand Up @@ -549,6 +553,7 @@ func (c *Client) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMes
msg := &jsonrpcMessage{Version: vsn, ID: c.nextID(), Method: method}
if paramsIn != nil { // prevent sending "params":null
var err error
var json = jsoniter.ConfigCompatibleWithStandardLibrary
if msg.Params, err = json.Marshal(paramsIn); err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"sync"
"time"

jsoniter "github.com/json-iterator/go"

"github.com/ethereum/go-ethereum/common/gopool"

"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -409,6 +411,7 @@ func (h *handler) handleResponses(batch []*jsonrpcMessage, handleCall func(*json
if msg.Error != nil {
op.err = msg.Error
} else {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
op.err = json.Unmarshal(msg.Result, &op.sub.subid)
if op.err == nil {
go op.sub.run()
Expand Down Expand Up @@ -450,6 +453,7 @@ func (h *handler) handleResponses(batch []*jsonrpcMessage, handleCall func(*json
// handleSubscriptionResult processes subscription notifications.
func (h *handler) handleSubscriptionResult(msg *jsonrpcMessage) {
var result subscriptionResult
var json = jsoniter.ConfigCompatibleWithStandardLibrary
if err := json.Unmarshal(msg.Params, &result); err != nil {
h.log.Debug("Dropping invalid subscription message")
return
Expand Down
9 changes: 8 additions & 1 deletion rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package rpc
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -30,6 +29,8 @@ import (
"strconv"
"sync"
"time"

jsoniter "github.com/json-iterator/go"
)

const (
Expand Down Expand Up @@ -178,6 +179,7 @@ func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) e

var resp jsonrpcMessage
batch := [1]*jsonrpcMessage{&resp}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
if err := json.NewDecoder(respBody).Decode(&resp); err != nil {
return err
}
Expand All @@ -194,6 +196,7 @@ func (c *Client) sendBatchHTTP(ctx context.Context, op *requestOp, msgs []*jsonr
defer respBody.Close()

var respmsgs []*jsonrpcMessage
var json = jsoniter.ConfigCompatibleWithStandardLibrary
if err := json.NewDecoder(respBody).Decode(&respmsgs); err != nil {
return err
}
Expand All @@ -202,6 +205,7 @@ func (c *Client) sendBatchHTTP(ctx context.Context, op *requestOp, msgs []*jsonr
}

func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadCloser, error) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
body, err := json.Marshal(msg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -258,6 +262,7 @@ func newHTTPServerConn(r *http.Request, w http.ResponseWriter) ServerCodec {

encoder := func(v any, isErrorResponse bool) error {
if !isErrorResponse {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
return json.NewEncoder(conn).Encode(v)
}

Expand All @@ -267,6 +272,7 @@ func newHTTPServerConn(r *http.Request, w http.ResponseWriter) ServerCodec {
// server's write timeout occurs. So we need to flush the response. The
// Content-Length header also needs to be set to ensure the client knows
// when it has the full response.
var json = jsoniter.ConfigCompatibleWithStandardLibrary
encdata, err := json.Marshal(v)
if err != nil {
return err
Expand All @@ -287,6 +293,7 @@ func newHTTPServerConn(r *http.Request, w http.ResponseWriter) ServerCodec {
return err
}

var json = jsoniter.ConfigCompatibleWithStandardLibrary
dec := json.NewDecoder(conn)
dec.UseNumber()

Expand Down
6 changes: 6 additions & 0 deletions rpc/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"strings"
"sync"
"time"

jsoniter "github.com/json-iterator/go"
)

const (
Expand Down Expand Up @@ -91,6 +93,7 @@ func (msg *jsonrpcMessage) namespace() string {
}

func (msg *jsonrpcMessage) String() string {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
b, _ := json.Marshal(msg)
return string(b)
}
Expand All @@ -102,6 +105,7 @@ func (msg *jsonrpcMessage) errorResponse(err error) *jsonrpcMessage {
}

func (msg *jsonrpcMessage) response(result interface{}) *jsonrpcMessage {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
enc, err := json.Marshal(result)
if err != nil {
return msg.errorResponse(&internalServerError{errcodeMarshalError, err.Error()})
Expand Down Expand Up @@ -199,6 +203,7 @@ func NewFuncCodec(conn deadlineCloser, encode encodeFunc, decode decodeFunc) Ser
// NewCodec creates a codec on the given connection. If conn implements ConnRemoteAddr, log
// messages will use it to include the remote address of the connection.
func NewCodec(conn Conn) ServerCodec {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
enc := json.NewEncoder(conn)
dec := json.NewDecoder(conn)
dec.UseNumber()
Expand Down Expand Up @@ -267,6 +272,7 @@ func (c *jsonCodec) closed() <-chan interface{} {
func parseMessage(raw json.RawMessage) ([]*jsonrpcMessage, bool) {
if !isBatch(raw) {
msgs := []*jsonrpcMessage{{}}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
json.Unmarshal(raw, &msgs[0])
return msgs, false
}
Expand Down
5 changes: 5 additions & 0 deletions rpc/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"strings"
"sync"
"time"

jsoniter "github.com/json-iterator/go"
)

var (
Expand Down Expand Up @@ -182,6 +184,7 @@ func (n *Notifier) activate() error {
}

func (n *Notifier) send(sub *Subscription, data json.RawMessage) error {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
params, _ := json.Marshal(&subscriptionResult{ID: string(sub.ID), Result: data})
ctx := context.Background()

Expand All @@ -208,6 +211,7 @@ func (s *Subscription) Err() <-chan error {

// MarshalJSON marshals a subscription as its ID.
func (s *Subscription) MarshalJSON() ([]byte, error) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(s.ID)
}

Expand Down Expand Up @@ -376,6 +380,7 @@ func (sub *ClientSubscription) forward() (unsubscribeServer bool, err error) {

func (sub *ClientSubscription) unmarshal(result json.RawMessage) (interface{}, error) {
val := reflect.New(sub.etype)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(result, val.Interface())
return val.Elem().Interface(), err
}
Expand Down
4 changes: 3 additions & 1 deletion rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package rpc

import (
"context"
"encoding/json"
"fmt"
"math"
"strconv"
"strings"

jsoniter "github.com/json-iterator/go"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
Expand Down Expand Up @@ -152,6 +153,7 @@ type BlockNumberOrHash struct {
func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error {
type erased BlockNumberOrHash
e := erased{}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(data, &e)
if err == nil {
if e.BlockNumber != nil && e.BlockHash != nil {
Expand Down

0 comments on commit a45c0a9

Please sign in to comment.