Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] add limit batch request for web3 API #3822

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ type Config struct {
GasStation gasstation.Config `yaml:"gasStation"`
RangeQueryLimit uint64 `yaml:"rangeQueryLimit"`
Tracer tracer.Config `yaml:"tracer"`
// BatchRequestLimit is the maximum number of requests in a batch.
BatchRequestLimit int `yaml:"batchRequestLimit"`
}

// DefaultConfig is the default config
var DefaultConfig = Config{
UseRDS: false,
GRPCPort: 14014,
HTTPPort: 15014,
WebSocketPort: 16014,
TpsWindow: 10,
GasStation: gasstation.DefaultConfig,
RangeQueryLimit: 1000,
UseRDS: false,
GRPCPort: 14014,
HTTPPort: 15014,
WebSocketPort: 16014,
TpsWindow: 10,
GasStation: gasstation.DefaultConfig,
RangeQueryLimit: 1000,
BatchRequestLimit: _defaultBatchRequestLimit,
}
2 changes: 1 addition & 1 deletion api/serverV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewServerV2(
if err != nil {
return nil, err
}
web3Handler := NewWeb3Handler(coreAPI, cfg.RedisCacheURL)
web3Handler := NewWeb3Handler(coreAPI, cfg.RedisCacheURL, cfg.BatchRequestLimit)

tp, err := tracer.NewProvider(
tracer.WithServiceName(cfg.Tracer.ServiceName),
Expand Down
2 changes: 1 addition & 1 deletion api/serverV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestServerV2(t *testing.T) {
defer ctrl.Finish()
core := mock_apicoreservice.NewMockCoreService(ctrl)
// TODO: mock web3handler
web3Handler := NewWeb3Handler(core, "")
web3Handler := NewWeb3Handler(core, "", _defaultBatchRequestLimit)
svr := &ServerV2{
core: core,
grpcServer: NewGRPCServer(core, testutil.RandomPort()),
Expand Down
27 changes: 21 additions & 6 deletions api/web3server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (

const (
_metamaskBalanceContractAddr = "io1k8uw2hrlvnfq8s2qpwwc24ws2ru54heenx8chr"
// _defaultBatchRequestLimit is the default maximum number of items in a batch.
_defaultBatchRequestLimit = 100 // Maximum number of items in a batch.
)

type (
Expand All @@ -42,8 +44,9 @@ type (
}

web3Handler struct {
coreService CoreService
cache apiCache
coreService CoreService
cache apiCache
batchRequestLimit int
}
)

Expand Down Expand Up @@ -71,6 +74,7 @@ var (
errInvalidFilterID = errors.New("filter not found")
errInvalidBlock = errors.New("invalid block")
errUnsupportedAction = errors.New("the type of action is not supported")
errMsgBatchTooLarge = errors.New("batch too large")

_pendingBlockNumber = "pending"
_latestBlockNumber = "latest"
Expand All @@ -82,10 +86,11 @@ func init() {
}

// NewWeb3Handler creates a handle to process web3 requests
func NewWeb3Handler(core CoreService, cacheURL string) Web3Handler {
func NewWeb3Handler(core CoreService, cacheURL string, batchRequestLimit int) Web3Handler {
return &web3Handler{
coreService: core,
cache: newAPICache(15*time.Minute, cacheURL),
coreService: core,
cache: newAPICache(15*time.Minute, cacheURL),
batchRequestLimit: batchRequestLimit,
}
}

Expand All @@ -102,8 +107,18 @@ func (svr *web3Handler) HandlePOSTReq(ctx context.Context, reader io.Reader, wri
if !web3Reqs.IsArray() {
return svr.handleWeb3Req(ctx, &web3Reqs, writer)
}
batchWriter := apitypes.NewBatchWriter(writer)
web3ReqArr := web3Reqs.Array()
if len(web3ReqArr) > int(svr.batchRequestLimit) {
err := errors.Wrapf(
errMsgBatchTooLarge,
"batch size %d exceeds the limit %d",
len(web3ReqArr),
svr.batchRequestLimit,
)
span.RecordError(err)
return writer.Write(&web3Response{err: err})
}
batchWriter := apitypes.NewBatchWriter(writer)
for i := range web3ReqArr {
if err := svr.handleWeb3Req(ctx, &web3ReqArr[i], batchWriter); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion api/web3server_integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestWeb3ServerIntegrity(t *testing.T) {
ctx := context.Background()
web3svr.Start(ctx)
defer web3svr.Stop(ctx)
handler := newHTTPHandler(NewWeb3Handler(svr.core, ""))
handler := newHTTPHandler(NewWeb3Handler(svr.core, "", _defaultBatchRequestLimit))

// send request
t.Run("eth_gasPrice", func(t *testing.T) {
Expand Down
Loading