Skip to content

Commit 4025070

Browse files
authored
Add limit to filter logs (#1558)
1 parent 2df8107 commit 4025070

File tree

8 files changed

+59
-6
lines changed

8 files changed

+59
-6
lines changed

cmd/seid/cmd/root.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,15 @@ checktx_timeout = "{{ .EVM.CheckTxTimeout }}"
496496
# controls whether to have txns go through one by one
497497
slow = {{ .EVM.Slow }}
498498
499+
# Deny list defines list of methods that EVM RPC should fail fast
500+
deny_list = {{ .EVM.DenyList }}
501+
502+
# max number of logs returned if block range is open-ended
503+
max_log_no_block = {{ .EVM.MaxLogNoBlock }}
504+
505+
# max number of blocks to query logs for
506+
max_blocks_for_log = {{ .EVM.MaxBlocksForLog }}
507+
499508
[eth_replay]
500509
eth_replay_enabled = {{ .ETHReplay.Enabled }}
501510
eth_rpc = "{{ .ETHReplay.EthRPC }}"

evmrpc/config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ type Config struct {
7373

7474
// Deny list defines list of methods that EVM RPC should fail fast
7575
DenyList []string `mapstructure:"deny_list"`
76+
77+
// max number of logs returned if block range is open-ended
78+
MaxLogNoBlock int64 `mapstructure:"max_log_no_block"`
79+
80+
// max number of blocks to query logs for
81+
MaxBlocksForLog int64 `mapstructure:"max_blocks_for_log"`
7682
}
7783

7884
var DefaultConfig = Config{
@@ -93,6 +99,8 @@ var DefaultConfig = Config{
9399
MaxTxPoolTxs: 1000,
94100
Slow: false,
95101
DenyList: make([]string, 0),
102+
MaxLogNoBlock: 10000,
103+
MaxBlocksForLog: 2000,
96104
}
97105

98106
const (
@@ -113,6 +121,8 @@ const (
113121
flagCheckTxTimeout = "evm.checktx_timeout"
114122
flagSlow = "evm.slow"
115123
flagDenyList = "evm.deny_list"
124+
flagMaxLogNoBlock = "evm.max_log_no_block"
125+
flagMaxBlocksForLog = "evm.max_blocks_for_log"
116126
)
117127

118128
func ReadConfig(opts servertypes.AppOptions) (Config, error) {
@@ -203,5 +213,15 @@ func ReadConfig(opts servertypes.AppOptions) (Config, error) {
203213
return cfg, err
204214
}
205215
}
216+
if v := opts.Get(flagMaxLogNoBlock); v != nil {
217+
if cfg.MaxLogNoBlock, err = cast.ToInt64E(v); err != nil {
218+
return cfg, err
219+
}
220+
}
221+
if v := opts.Get(flagMaxBlocksForLog); v != nil {
222+
if cfg.MaxBlocksForLog, err = cast.ToInt64E(v); err != nil {
223+
return cfg, err
224+
}
225+
}
206226
return cfg, nil
207227
}

evmrpc/config_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type opts struct {
2626
maxTxPoolTxs interface{}
2727
slow interface{}
2828
denyList interface{}
29+
maxLogNoBlock interface{}
30+
maxBlocksForLog interface{}
2931
}
3032

3133
func (o *opts) Get(k string) interface{} {
@@ -80,6 +82,12 @@ func (o *opts) Get(k string) interface{} {
8082
if k == "evm.deny_list" {
8183
return o.denyList
8284
}
85+
if k == "evm.max_log_no_block" {
86+
return o.maxLogNoBlock
87+
}
88+
if k == "evm.max_blocks_for_log" {
89+
return o.maxBlocksForLog
90+
}
8391
panic("unknown key")
8492
}
8593

@@ -102,6 +110,8 @@ func TestReadConfig(t *testing.T) {
102110
1000,
103111
false,
104112
make([]string, 0),
113+
20000,
114+
1000,
105115
}
106116
_, err := evmrpc.ReadConfig(&goodOpts)
107117
require.Nil(t, err)

evmrpc/filter.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ type FilterAPI struct {
5151
}
5252

5353
type FilterConfig struct {
54-
timeout time.Duration
54+
timeout time.Duration
55+
maxLog int64
56+
maxBlock int64
5557
}
5658

5759
type EventItemDataWrapper struct {
@@ -60,6 +62,7 @@ type EventItemDataWrapper struct {
6062
}
6163

6264
func NewFilterAPI(tmClient rpcclient.Client, logFetcher *LogFetcher, filterConfig *FilterConfig) *FilterAPI {
65+
logFetcher.filterConfig = filterConfig
6366
filters := make(map[ethrpc.ID]filter)
6467
api := &FilterAPI{
6568
tmClient: tmClient,
@@ -261,9 +264,10 @@ func (a *FilterAPI) UninstallFilter(
261264
}
262265

263266
type LogFetcher struct {
264-
tmClient rpcclient.Client
265-
k *keeper.Keeper
266-
ctxProvider func(int64) sdk.Context
267+
tmClient rpcclient.Client
268+
k *keeper.Keeper
269+
ctxProvider func(int64) sdk.Context
270+
filterConfig *FilterConfig
267271
}
268272

269273
func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCriteria, lastToHeight int64) ([]*ethtypes.Log, int64, error) {
@@ -275,6 +279,7 @@ func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCr
275279
}
276280
return f.GetLogsForBlock(ctx, block, crit, bloomIndexes), block.Block.Height, nil
277281
}
282+
applyOpenEndedLogLimit := f.filterConfig.maxLog > 0 && (crit.FromBlock == nil || crit.ToBlock == nil)
278283
latest := f.ctxProvider(LatestCtxHeight).BlockHeight()
279284
begin, end := latest, latest
280285
if crit.FromBlock != nil {
@@ -290,6 +295,9 @@ func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCr
290295
if lastToHeight > begin {
291296
begin = lastToHeight
292297
}
298+
if !applyOpenEndedLogLimit && f.filterConfig.maxBlock > 0 && end >= (begin+f.filterConfig.maxBlock) {
299+
end = begin + f.filterConfig.maxBlock - 1
300+
}
293301
// begin should always be <= end block at this point
294302
if begin > end {
295303
return nil, 0, fmt.Errorf("fromBlock %d is after toBlock %d", begin, end)
@@ -303,6 +311,10 @@ func (f *LogFetcher) GetLogsByFilters(ctx context.Context, crit filters.FilterCr
303311
return nil, 0, err
304312
}
305313
res = append(res, f.GetLogsForBlock(ctx, block, crit, bloomIndexes)...)
314+
if applyOpenEndedLogLimit && int64(len(res)) >= f.filterConfig.maxLog {
315+
res = res[:int(f.filterConfig.maxLog)]
316+
break
317+
}
306318
}
307319

308320
return res, end, nil

evmrpc/filter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func TestFilterGetFilterChanges(t *testing.T) {
246246

247247
resObj = sendRequest(t, TestPort, "getFilterChanges", filterId)
248248
logs := resObj["result"].([]interface{})
249-
require.Equal(t, 5, len(logs))
249+
require.Equal(t, 4, len(logs)) // limited by MaxLogNoBlock config to 4
250250
logObj := logs[0].(map[string]interface{})
251251
require.Equal(t, "0x2", logObj["blockNumber"].(string))
252252

evmrpc/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func NewEVMHTTPServer(
7272
},
7373
{
7474
Namespace: "eth",
75-
Service: NewFilterAPI(tmClient, &LogFetcher{tmClient: tmClient, k: k, ctxProvider: ctxProvider}, &FilterConfig{timeout: config.FilterTimeout}),
75+
Service: NewFilterAPI(tmClient, &LogFetcher{tmClient: tmClient, k: k, ctxProvider: ctxProvider}, &FilterConfig{timeout: config.FilterTimeout, maxLog: config.MaxLogNoBlock, maxBlock: config.MaxBlocksForLog}),
7676
},
7777
{
7878
Namespace: "sei",

evmrpc/setup_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ func init() {
407407
goodConfig.HTTPPort = TestPort
408408
goodConfig.WSPort = TestWSPort
409409
goodConfig.FilterTimeout = 500 * time.Millisecond
410+
goodConfig.MaxLogNoBlock = 4
410411
infoLog, err := log.NewDefaultLogger("text", "info")
411412
if err != nil {
412413
panic(err)

evmrpc/subscribe.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type SubscriptionConfig struct {
3333
}
3434

3535
func NewSubscriptionAPI(tmClient rpcclient.Client, logFetcher *LogFetcher, subscriptionConfig *SubscriptionConfig) *SubscriptionAPI {
36+
logFetcher.filterConfig = &FilterConfig{}
3637
return &SubscriptionAPI{
3738
tmClient: tmClient,
3839
subscriptionManager: NewSubscriptionManager(tmClient),

0 commit comments

Comments
 (0)