Skip to content

Commit 4dfcd60

Browse files
committed
Merge pull request bnb-chain#1392 from bas-vk/ipcpipelining
Several bugfixes to IPC channel
2 parents 546c0f6 + e8c1399 commit 4dfcd60

4 files changed

Lines changed: 181 additions & 46 deletions

File tree

‎cmd/utils/flags.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,17 +432,17 @@ func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
432432
func IpcSocketPath(ctx *cli.Context) (ipcpath string) {
433433
if common.IsWindows() {
434434
ipcpath = common.DefaultIpcPath()
435-
if ipcpath != ctx.GlobalString(IPCPathFlag.Name) {
435+
if ctx.GlobalIsSet(IPCPathFlag.Name) {
436436
ipcpath = ctx.GlobalString(IPCPathFlag.Name)
437437
}
438438
} else {
439439
ipcpath = common.DefaultIpcPath()
440-
if ctx.GlobalString(IPCPathFlag.Name) != common.DefaultIpcPath() {
441-
ipcpath = ctx.GlobalString(IPCPathFlag.Name)
442-
} else if ctx.GlobalString(DataDirFlag.Name) != "" &&
443-
ctx.GlobalString(DataDirFlag.Name) != common.DefaultDataDir() {
440+
if ctx.GlobalIsSet(DataDirFlag.Name) {
444441
ipcpath = filepath.Join(ctx.GlobalString(DataDirFlag.Name), "geth.ipc")
445442
}
443+
if ctx.GlobalIsSet(IPCPathFlag.Name) {
444+
ipcpath = ctx.GlobalString(IPCPathFlag.Name)
445+
}
446446
}
447447

448448
return

‎rpc/codec/json.go‎

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,51 @@ import (
1010
)
1111

1212
const (
13-
READ_TIMEOUT = 15 // read timeout in seconds
13+
READ_TIMEOUT = 60 // in seconds
1414
MAX_REQUEST_SIZE = 1024 * 1024
1515
MAX_RESPONSE_SIZE = 1024 * 1024
1616
)
1717

1818
// Json serialization support
1919
type JsonCodec struct {
2020
c net.Conn
21+
d *json.Decoder
2122
}
2223

2324
// Create new JSON coder instance
2425
func NewJsonCoder(conn net.Conn) ApiCoder {
2526
return &JsonCodec{
2627
c: conn,
28+
d: json.NewDecoder(conn),
2729
}
2830
}
2931

30-
// Serialize obj to JSON and write it to conn
32+
// Read incoming request and parse it to RPC request
3133
func (self *JsonCodec) ReadRequest() (requests []*shared.Request, isBatch bool, err error) {
32-
bytesInBuffer := 0
33-
buf := make([]byte, MAX_REQUEST_SIZE)
34-
3534
deadline := time.Now().Add(READ_TIMEOUT * time.Second)
3635
if err := self.c.SetDeadline(deadline); err != nil {
3736
return nil, false, err
3837
}
3938

40-
for {
41-
n, err := self.c.Read(buf[bytesInBuffer:])
42-
if err != nil {
43-
self.c.Close()
44-
return nil, false, err
45-
}
46-
47-
bytesInBuffer += n
48-
49-
singleRequest := shared.Request{}
50-
err = json.Unmarshal(buf[:bytesInBuffer], &singleRequest)
51-
if err == nil {
52-
requests := make([]*shared.Request, 1)
53-
requests[0] = &singleRequest
54-
return requests, false, nil
55-
}
56-
57-
requests = make([]*shared.Request, 0)
58-
err = json.Unmarshal(buf[:bytesInBuffer], &requests)
59-
if err == nil {
60-
return requests, true, nil
39+
var incoming json.RawMessage
40+
err = self.d.Decode(&incoming)
41+
if err == nil {
42+
isBatch = incoming[0] == '['
43+
if isBatch {
44+
requests = make([]*shared.Request, 0)
45+
err = json.Unmarshal(incoming, &requests)
46+
} else {
47+
requests = make([]*shared.Request, 1)
48+
var singleRequest shared.Request
49+
if err = json.Unmarshal(incoming, &singleRequest); err == nil {
50+
requests[0] = &singleRequest
51+
}
6152
}
53+
return
6254
}
6355

64-
self.c.Close() // timeout
65-
return nil, false, fmt.Errorf("Unable to read response")
56+
self.c.Close()
57+
return nil, false, err
6658
}
6759

6860
func (self *JsonCodec) ReadResponse() (interface{}, error) {
@@ -81,15 +73,15 @@ func (self *JsonCodec) ReadResponse() (interface{}, error) {
8173
}
8274
bytesInBuffer += n
8375

76+
var failure shared.ErrorResponse
77+
if err = json.Unmarshal(buf[:bytesInBuffer], &failure); err == nil && failure.Error != nil {
78+
return failure, fmt.Errorf(failure.Error.Message)
79+
}
80+
8481
var success shared.SuccessResponse
8582
if err = json.Unmarshal(buf[:bytesInBuffer], &success); err == nil {
8683
return success, nil
8784
}
88-
89-
var failure shared.ErrorResponse
90-
if err = json.Unmarshal(buf[:bytesInBuffer], &failure); err == nil && failure.Error != nil {
91-
return failure, nil
92-
}
9385
}
9486

9587
self.c.Close()

‎rpc/codec/json_test.go‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package codec
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"net"
7+
"testing"
8+
"time"
9+
)
10+
11+
type jsonTestConn struct {
12+
buffer *bytes.Buffer
13+
}
14+
15+
func newJsonTestConn(data []byte) *jsonTestConn {
16+
return &jsonTestConn{
17+
buffer: bytes.NewBuffer(data),
18+
}
19+
}
20+
21+
func (self *jsonTestConn) Read(p []byte) (n int, err error) {
22+
return self.buffer.Read(p)
23+
}
24+
25+
func (self *jsonTestConn) Write(p []byte) (n int, err error) {
26+
return self.buffer.Write(p)
27+
}
28+
29+
func (self *jsonTestConn) Close() error {
30+
// not implemented
31+
return nil
32+
}
33+
34+
func (self *jsonTestConn) LocalAddr() net.Addr {
35+
// not implemented
36+
return nil
37+
}
38+
39+
func (self *jsonTestConn) RemoteAddr() net.Addr {
40+
// not implemented
41+
return nil
42+
}
43+
44+
func (self *jsonTestConn) SetDeadline(t time.Time) error {
45+
return nil
46+
}
47+
48+
func (self *jsonTestConn) SetReadDeadline(t time.Time) error {
49+
return nil
50+
}
51+
52+
func (self *jsonTestConn) SetWriteDeadline(t time.Time) error {
53+
return nil
54+
}
55+
56+
func TestJsonDecoderWithValidRequest(t *testing.T) {
57+
reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","params":[],"id":64}`)
58+
decoder := newJsonTestConn(reqdata)
59+
60+
jsonDecoder := NewJsonCoder(decoder)
61+
requests, batch, err := jsonDecoder.ReadRequest()
62+
63+
if err != nil {
64+
t.Errorf("Read valid request failed - %v", err)
65+
}
66+
67+
if len(requests) != 1 {
68+
t.Errorf("Expected to get a single request but got %d", len(requests))
69+
}
70+
71+
if batch {
72+
t.Errorf("Got batch indication while expecting single request")
73+
}
74+
75+
if requests[0].Id != float64(64) {
76+
t.Errorf("Expected req.Id == 64 but got %v", requests[0].Id)
77+
}
78+
79+
if requests[0].Method != "modules" {
80+
t.Errorf("Expected req.Method == 'modules' got '%s'", requests[0].Method)
81+
}
82+
}
83+
84+
func TestJsonDecoderWithValidBatchRequest(t *testing.T) {
85+
reqdata := []byte(`[{"jsonrpc":"2.0","method":"modules","params":[],"id":64},
86+
{"jsonrpc":"2.0","method":"modules","params":[],"id":64}]`)
87+
decoder := newJsonTestConn(reqdata)
88+
89+
jsonDecoder := NewJsonCoder(decoder)
90+
requests, batch, err := jsonDecoder.ReadRequest()
91+
92+
if err != nil {
93+
t.Errorf("Read valid batch request failed - %v", err)
94+
}
95+
96+
if len(requests) != 2 {
97+
t.Errorf("Expected to get two requests but got %d", len(requests))
98+
}
99+
100+
if !batch {
101+
t.Errorf("Got no batch indication while expecting batch request")
102+
}
103+
104+
for i := 0; i < len(requests); i++ {
105+
if requests[i].Id != float64(64) {
106+
t.Errorf("Expected req.Id == 64 but got %v", requests[i].Id)
107+
}
108+
109+
if requests[i].Method != "modules" {
110+
t.Errorf("Expected req.Method == 'modules' got '%s'", requests[i].Method)
111+
}
112+
}
113+
}
114+
115+
func TestJsonDecoderWithInvalidIncompleteMessage(t *testing.T) {
116+
reqdata := []byte(`{"jsonrpc":"2.0","method":"modules","pa`)
117+
decoder := newJsonTestConn(reqdata)
118+
119+
jsonDecoder := NewJsonCoder(decoder)
120+
requests, batch, err := jsonDecoder.ReadRequest()
121+
122+
if err != io.ErrUnexpectedEOF {
123+
t.Errorf("Expected to read an incomplete request err but got %v", err)
124+
}
125+
126+
// remaining message
127+
decoder.Write([]byte(`rams":[],"id:64"}`))
128+
requests, batch, err = jsonDecoder.ReadRequest()
129+
130+
if err == nil {
131+
t.Errorf("Expected an error but got nil")
132+
}
133+
134+
if len(requests) != 0 {
135+
t.Errorf("Expected to get no requests but got %d", len(requests))
136+
}
137+
138+
if batch {
139+
t.Errorf("Got batch indication while expecting non batch")
140+
}
141+
}

‎rpc/jeth.go‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package rpc
33
import (
44
"encoding/json"
55

6+
"fmt"
7+
68
"github.com/ethereum/go-ethereum/jsre"
79
"github.com/ethereum/go-ethereum/rpc/comms"
810
"github.com/ethereum/go-ethereum/rpc/shared"
@@ -20,14 +22,13 @@ func NewJeth(ethApi shared.EthereumApi, re *jsre.JSRE, client comms.EthereumClie
2022
}
2123

2224
func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id interface{}) (response otto.Value) {
23-
rpcerr := &shared.ErrorObject{code, msg}
24-
call.Otto.Set("ret_jsonrpc", shared.JsonRpcVersion)
25-
call.Otto.Set("ret_id", id)
26-
call.Otto.Set("ret_error", rpcerr)
27-
response, _ = call.Otto.Run(`
28-
ret_response = { jsonrpc: ret_jsonrpc, id: ret_id, error: ret_error };
29-
`)
30-
return
25+
errObj := fmt.Sprintf("{\"message\": \"%s\", \"code\": %d}", msg, code)
26+
retResponse := fmt.Sprintf("ret_response = JSON.parse('{\"jsonrpc\": \"%s\", \"id\": %v, \"error\": %s}');", shared.JsonRpcVersion, id, errObj)
27+
28+
call.Otto.Run("ret_error = " + errObj)
29+
res, _ := call.Otto.Run(retResponse)
30+
31+
return res
3132
}
3233

3334
func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) {
@@ -56,6 +57,7 @@ func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) {
5657
return self.err(call, -32603, err.Error(), req.Id)
5758
}
5859
respif, err = self.client.Recv()
60+
5961
if err != nil {
6062
return self.err(call, -32603, err.Error(), req.Id)
6163
}

0 commit comments

Comments
 (0)