Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
EVM-437 Batch calls over websockets not working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Jun 6, 2023
1 parent 2c2c45d commit 243c55f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions jsonrpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Request struct {
Params json.RawMessage `json:"params,omitempty"`
}

type BatchRequest []Request

// Response is a jsonrpc response interface
type Response interface {
GetID() interface{}
Expand Down
34 changes: 34 additions & 0 deletions jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,45 @@ func (d *Dispatcher) RemoveFilterByWs(conn wsConn) {
}

func (d *Dispatcher) HandleWs(reqBody []byte, conn wsConn) ([]byte, error) {
// first try to unmarshal to batch request
// if there is an error try to unmarshal to single request
var batchReq BatchRequest
if err := json.Unmarshal(reqBody, &batchReq); err == nil {
const (
openSquareBracket = 91 // [
closeSquareBracket = 93 // ]
comma = 44 // ,
)

result := []byte{openSquareBracket}

for i, req := range batchReq {
bytes, err := d.handleWs(req, conn)
if err != nil {
return nil, err
}

if i > 0 {
result = append(result, comma)
}

result = append(result, bytes...)
}

// batch output should look like
// [ { "requestId": "1", "status": 200 }, { "requestId": "2", "status": 200 } ]
return append(result, closeSquareBracket), nil
}

var req Request
if err := json.Unmarshal(reqBody, &req); err != nil {
return NewRPCResponse(req.ID, "2.0", nil, NewInvalidRequestError("Invalid json request")).Bytes()
}

return d.handleWs(req, conn)
}

func (d *Dispatcher) handleWs(req Request, conn wsConn) ([]byte, error) {
// if the request method is eth_subscribe we need to create a
// new filter with ws connection
if req.Method == "eth_subscribe" {
Expand Down

0 comments on commit 243c55f

Please sign in to comment.