Skip to content

Commit 951494e

Browse files
Merge commit 'upstream/master:f3314bb6' into merge-v1.12.1-pre
rpc/handler.go: conflict with upstream's PR (ethereum/go-ethereum#26681) to add batch request and response size limits, and our own implementation of it. Removed our implementation (#198, which was moved inside batchCallBuffer.pushResponse in the v1.11.2 merge #205) in favor of upstream.
2 parents 0f1170e + f3314bb commit 951494e

22 files changed

+558
-249
lines changed

cmd/clef/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ func signer(c *cli.Context) error {
732732
cors := utils.SplitAndTrim(c.String(utils.HTTPCORSDomainFlag.Name))
733733

734734
srv := rpc.NewServer()
735+
srv.SetBatchLimits(node.DefaultConfig.BatchRequestLimit, node.DefaultConfig.BatchResponseMaxSize)
735736
err := node.RegisterApis(rpcAPI, []string{"account"}, srv)
736737
if err != nil {
737738
utils.Fatalf("Could not register API: %w", err)

cmd/geth/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ var (
168168
utils.RPCGlobalEVMTimeoutFlag,
169169
utils.RPCGlobalTxFeeCapFlag,
170170
utils.AllowUnprotectedTxs,
171+
utils.BatchRequestLimit,
172+
utils.BatchResponseMaxSize,
171173
}
172174

173175
metricsFlags = []cli.Flag{

cmd/utils/flags.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,18 @@ var (
713713
Usage: "Allow for unprotected (non EIP155 signed) transactions to be submitted via RPC",
714714
Category: flags.APICategory,
715715
}
716+
BatchRequestLimit = &cli.IntFlag{
717+
Name: "rpc.batch-request-limit",
718+
Usage: "Maximum number of requests in a batch",
719+
Value: node.DefaultConfig.BatchRequestLimit,
720+
Category: flags.APICategory,
721+
}
722+
BatchResponseMaxSize = &cli.IntFlag{
723+
Name: "rpc.batch-response-max-size",
724+
Usage: "Maximum number of bytes returned from a batched call",
725+
Value: node.DefaultConfig.BatchResponseMaxSize,
726+
Category: flags.APICategory,
727+
}
716728
EnablePersonal = &cli.BoolFlag{
717729
Name: "rpc.enabledeprecatedpersonal",
718730
Usage: "Enables the (deprecated) personal namespace",
@@ -1130,6 +1142,14 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) {
11301142
if ctx.IsSet(AllowUnprotectedTxs.Name) {
11311143
cfg.AllowUnprotectedTxs = ctx.Bool(AllowUnprotectedTxs.Name)
11321144
}
1145+
1146+
if ctx.IsSet(BatchRequestLimit.Name) {
1147+
cfg.BatchRequestLimit = ctx.Int(BatchRequestLimit.Name)
1148+
}
1149+
1150+
if ctx.IsSet(BatchResponseMaxSize.Name) {
1151+
cfg.BatchResponseMaxSize = ctx.Int(BatchResponseMaxSize.Name)
1152+
}
11331153
}
11341154

11351155
// setGraphQL creates the GraphQL listener interface string from the set

node/api.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func (api *adminAPI) StartHTTP(host *string, port *int, cors *string, apis *stri
176176
CorsAllowedOrigins: api.node.config.HTTPCors,
177177
Vhosts: api.node.config.HTTPVirtualHosts,
178178
Modules: api.node.config.HTTPModules,
179+
rpcEndpointConfig: rpcEndpointConfig{
180+
batchItemLimit: api.node.config.BatchRequestLimit,
181+
batchResponseSizeLimit: api.node.config.BatchResponseMaxSize,
182+
},
179183
}
180184
if cors != nil {
181185
config.CorsAllowedOrigins = nil
@@ -250,6 +254,10 @@ func (api *adminAPI) StartWS(host *string, port *int, allowedOrigins *string, ap
250254
Modules: api.node.config.WSModules,
251255
Origins: api.node.config.WSOrigins,
252256
// ExposeAll: api.node.config.WSExposeAll,
257+
rpcEndpointConfig: rpcEndpointConfig{
258+
batchItemLimit: api.node.config.BatchRequestLimit,
259+
batchResponseSizeLimit: api.node.config.BatchResponseMaxSize,
260+
},
253261
}
254262
if apis != nil {
255263
config.Modules = nil

node/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ type Config struct {
197197
// AllowUnprotectedTxs allows non EIP-155 protected transactions to be send over RPC.
198198
AllowUnprotectedTxs bool `toml:",omitempty"`
199199

200+
// BatchRequestLimit is the maximum number of requests in a batch.
201+
BatchRequestLimit int `toml:",omitempty"`
202+
203+
// BatchResponseMaxSize is the maximum number of bytes returned from a batched rpc call.
204+
BatchResponseMaxSize int `toml:",omitempty"`
205+
200206
// JWTSecret is the path to the hex-encoded jwt secret.
201207
JWTSecret string `toml:",omitempty"`
202208

node/defaults.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,19 @@ var (
4646

4747
// DefaultConfig contains reasonable default settings.
4848
var DefaultConfig = Config{
49-
DataDir: DefaultDataDir(),
50-
HTTPPort: DefaultHTTPPort,
51-
AuthAddr: DefaultAuthHost,
52-
AuthPort: DefaultAuthPort,
53-
AuthVirtualHosts: DefaultAuthVhosts,
54-
HTTPModules: []string{"net", "web3"},
55-
HTTPVirtualHosts: []string{"localhost"},
56-
HTTPTimeouts: rpc.DefaultHTTPTimeouts,
57-
WSPort: DefaultWSPort,
58-
WSModules: []string{"net", "web3"},
59-
GraphQLVirtualHosts: []string{"localhost"},
49+
DataDir: DefaultDataDir(),
50+
HTTPPort: DefaultHTTPPort,
51+
AuthAddr: DefaultAuthHost,
52+
AuthPort: DefaultAuthPort,
53+
AuthVirtualHosts: DefaultAuthVhosts,
54+
HTTPModules: []string{"net", "web3"},
55+
HTTPVirtualHosts: []string{"localhost"},
56+
HTTPTimeouts: rpc.DefaultHTTPTimeouts,
57+
WSPort: DefaultWSPort,
58+
WSModules: []string{"net", "web3"},
59+
BatchRequestLimit: 1000,
60+
BatchResponseMaxSize: 25 * 1000 * 1000,
61+
GraphQLVirtualHosts: []string{"localhost"},
6062
P2P: p2p.Config{
6163
ListenAddr: ":30303",
6264
MaxPeers: 50,

node/node.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ func New(conf *Config) (*Node, error) {
105105
if strings.HasSuffix(conf.Name, ".ipc") {
106106
return nil, errors.New(`Config.Name cannot end in ".ipc"`)
107107
}
108-
108+
server := rpc.NewServer()
109+
server.SetBatchLimits(conf.BatchRequestLimit, conf.BatchResponseMaxSize)
109110
node := &Node{
110111
config: conf,
111-
inprocHandler: rpc.NewServer(),
112+
inprocHandler: server,
112113
eventmux: new(event.TypeMux),
113114
log: conf.Logger,
114115
stop: make(chan struct{}),
@@ -409,6 +410,11 @@ func (n *Node) startRPC() error {
409410
openAPIs, allAPIs = n.getAPIs()
410411
)
411412

413+
rpcConfig := rpcEndpointConfig{
414+
batchItemLimit: n.config.BatchRequestLimit,
415+
batchResponseSizeLimit: n.config.BatchResponseMaxSize,
416+
}
417+
412418
initHttp := func(server *httpServer, port int) error {
413419
if err := server.setListenAddr(n.config.HTTPHost, port); err != nil {
414420
return err
@@ -418,6 +424,7 @@ func (n *Node) startRPC() error {
418424
Vhosts: n.config.HTTPVirtualHosts,
419425
Modules: n.config.HTTPModules,
420426
prefix: n.config.HTTPPathPrefix,
427+
rpcEndpointConfig: rpcConfig,
421428
}); err != nil {
422429
return err
423430
}
@@ -431,9 +438,10 @@ func (n *Node) startRPC() error {
431438
return err
432439
}
433440
if err := server.enableWS(openAPIs, wsConfig{
434-
Modules: n.config.WSModules,
435-
Origins: n.config.WSOrigins,
436-
prefix: n.config.WSPathPrefix,
441+
Modules: n.config.WSModules,
442+
Origins: n.config.WSOrigins,
443+
prefix: n.config.WSPathPrefix,
444+
rpcEndpointConfig: rpcConfig,
437445
}); err != nil {
438446
return err
439447
}
@@ -447,26 +455,29 @@ func (n *Node) startRPC() error {
447455
if err := server.setListenAddr(n.config.AuthAddr, port); err != nil {
448456
return err
449457
}
458+
sharedConfig := rpcConfig
459+
sharedConfig.jwtSecret = secret
450460
if err := server.enableRPC(allAPIs, httpConfig{
451461
CorsAllowedOrigins: DefaultAuthCors,
452462
Vhosts: n.config.AuthVirtualHosts,
453463
Modules: DefaultAuthModules,
454464
prefix: DefaultAuthPrefix,
455-
jwtSecret: secret,
465+
rpcEndpointConfig: sharedConfig,
456466
}); err != nil {
457467
return err
458468
}
459469
servers = append(servers, server)
470+
460471
// Enable auth via WS
461472
server = n.wsServerForPort(port, true)
462473
if err := server.setListenAddr(n.config.AuthAddr, port); err != nil {
463474
return err
464475
}
465476
if err := server.enableWS(allAPIs, wsConfig{
466-
Modules: DefaultAuthModules,
467-
Origins: DefaultAuthOrigins,
468-
prefix: DefaultAuthPrefix,
469-
jwtSecret: secret,
477+
Modules: DefaultAuthModules,
478+
Origins: DefaultAuthOrigins,
479+
prefix: DefaultAuthPrefix,
480+
rpcEndpointConfig: sharedConfig,
470481
}); err != nil {
471482
return err
472483
}

node/rpcstack.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,21 @@ type httpConfig struct {
4141
CorsAllowedOrigins []string
4242
Vhosts []string
4343
prefix string // path prefix on which to mount http handler
44-
jwtSecret []byte // optional JWT secret
44+
rpcEndpointConfig
4545
}
4646

4747
// wsConfig is the JSON-RPC/Websocket configuration
4848
type wsConfig struct {
49-
Origins []string
50-
Modules []string
51-
prefix string // path prefix on which to mount ws handler
52-
jwtSecret []byte // optional JWT secret
49+
Origins []string
50+
Modules []string
51+
prefix string // path prefix on which to mount ws handler
52+
rpcEndpointConfig
53+
}
54+
55+
type rpcEndpointConfig struct {
56+
jwtSecret []byte // optional JWT secret
57+
batchItemLimit int
58+
batchResponseSizeLimit int
5359
}
5460

5561
type rpcHandler struct {
@@ -302,6 +308,7 @@ func (h *httpServer) enableRPC(apis []rpc.API, config httpConfig) error {
302308

303309
// Create RPC server and handler.
304310
srv := rpc.NewServer()
311+
srv.SetBatchLimits(config.batchItemLimit, config.batchResponseSizeLimit)
305312
if err := RegisterApis(apis, config.Modules, srv); err != nil {
306313
return err
307314
}
@@ -339,6 +346,7 @@ func (h *httpServer) enableWS(apis []rpc.API, config wsConfig) error {
339346
}
340347
// Create RPC server and handler.
341348
srv := rpc.NewServer()
349+
srv.SetBatchLimits(config.batchItemLimit, config.batchResponseSizeLimit)
342350
if err := RegisterApis(apis, config.Modules, srv); err != nil {
343351
return err
344352
}

node/rpcstack_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,10 @@ func TestJWT(t *testing.T) {
339339
ss, _ := jwt.NewWithClaims(method, testClaim(input)).SignedString(secret)
340340
return ss
341341
}
342-
srv := createAndStartServer(t, &httpConfig{jwtSecret: []byte("secret")},
343-
true, &wsConfig{Origins: []string{"*"}, jwtSecret: []byte("secret")}, nil)
342+
cfg := rpcEndpointConfig{jwtSecret: []byte("secret")}
343+
httpcfg := &httpConfig{rpcEndpointConfig: cfg}
344+
wscfg := &wsConfig{Origins: []string{"*"}, rpcEndpointConfig: cfg}
345+
srv := createAndStartServer(t, httpcfg, true, wscfg, nil)
344346
wsUrl := fmt.Sprintf("ws://%v", srv.listenAddr())
345347
htUrl := fmt.Sprintf("http://%v", srv.listenAddr())
346348

0 commit comments

Comments
 (0)