Skip to content

Commit e035369

Browse files
manav2401claude
andauthored
feat(evmonly): add the polling filter API to the EVM-only RPC (#4668)
This PR adds log filtering to EVM-only RPC. - `eth_newFilter`: installs a log filter and returns its ID. Polls start at a numeric fromBlock, or else at the block after the latest indexed one. - `eth_newBlockFilter`: installs a filter whose polls return the hashes of blocks committed since the previous poll. - `eth_getFilterChanges`: returns what a filter matched since its last poll, using the same cursor rules, range limits and log caps as the regular RPC. A block filter that falls more than max_block_filter_hashes blocks behind is removed. - `eth_getFilterLogs`: returns every log a log filter matches, without moving its poll cursor. - `eth_uninstallFilter`: removes a filter and reports whether it existed. - `eth_newPendingTransactionFilter`: returns the regular RPC's "not supported" error, since there's no pending-transaction feed to poll. Filters that aren't polled within `filter_timeout` expire. New `[giga.rpc]` keys use the regular RPC's defaults: - `filter_timeout` (120s) - `max_filters` (1000) - `max_block_filter_hashes` (1000). Log matching reuses the eth_getLogs code. Test coverage: table tests for the cursor rules, pruning, caps and expiry, an HTTP end-to-end test, and new checks in the Autobahn Basic suite. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 883f6e8 commit e035369

9 files changed

Lines changed: 931 additions & 7 deletions

File tree

‎giga/config/config.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ type RPCConfig struct {
9999
BatchResponseMaxSize int `mapstructure:"batch_response_max_size"`
100100
// MaxWSConnections caps simultaneously open WebSocket connections.
101101
MaxWSConnections int `mapstructure:"max_ws_connections"`
102+
// FilterTimeout is how long an eth_newFilter or eth_newBlockFilter filter lives without being
103+
// polled.
104+
FilterTimeout time.Duration `mapstructure:"filter_timeout"`
105+
// MaxFilters caps the installed eth_newFilter and eth_newBlockFilter filters.
106+
MaxFilters uint64 `mapstructure:"max_filters"`
107+
// MaxBlockFilterHashes caps the unpolled blocks an eth_newBlockFilter filter may fall behind
108+
// by before it is removed.
109+
MaxBlockFilterHashes uint64 `mapstructure:"max_block_filter_hashes"`
102110
// MaxConcurrentTraceCalls caps debug_trace* calls running at once; a call past it is refused.
103111
MaxConcurrentTraceCalls int `mapstructure:"max_concurrent_trace_calls"`
104112
// TraceTimeout bounds how long one debug_trace* call may run.
@@ -137,6 +145,9 @@ var DefaultConfig = Config{
137145
BatchRequestLimit: 100,
138146
BatchResponseMaxSize: 25 * 1000 * 1000,
139147
MaxWSConnections: 2000,
148+
FilterTimeout: 120 * time.Second,
149+
MaxFilters: 1000,
150+
MaxBlockFilterHashes: 1000,
140151
MaxConcurrentTraceCalls: 10,
141152
TraceTimeout: 30 * time.Second,
142153
MaxTraceStructLogBytes: 32 * 1024 * 1024,
@@ -186,6 +197,9 @@ const (
186197
FlagRPCBatchRequestLimit = "giga.rpc.batch_request_limit"
187198
FlagRPCBatchResponseMaxSize = "giga.rpc.batch_response_max_size"
188199
FlagRPCMaxWSConnections = "giga.rpc.max_ws_connections"
200+
FlagRPCFilterTimeout = "giga.rpc.filter_timeout"
201+
FlagRPCMaxFilters = "giga.rpc.max_filters"
202+
FlagRPCMaxBlockFilterHashes = "giga.rpc.max_block_filter_hashes"
189203
FlagRPCMaxConcurrentTraceCalls = "giga.rpc.max_concurrent_trace_calls"
190204
FlagRPCTraceTimeout = "giga.rpc.trace_timeout"
191205
FlagRPCMaxTraceStructLogBytes = "giga.rpc.max_trace_struct_log_bytes"
@@ -311,6 +325,21 @@ func ReadConfig(opts AppOptions) (Config, error) {
311325
return cfg, fmt.Errorf("%s: %w", FlagRPCMaxWSConnections, err)
312326
}
313327
}
328+
if v := opts.Get(FlagRPCFilterTimeout); v != nil {
329+
if cfg.RPC.FilterTimeout, err = cast.ToDurationE(v); err != nil {
330+
return cfg, fmt.Errorf("%s: %w", FlagRPCFilterTimeout, err)
331+
}
332+
}
333+
if v := opts.Get(FlagRPCMaxFilters); v != nil {
334+
if cfg.RPC.MaxFilters, err = cast.ToUint64E(v); err != nil {
335+
return cfg, fmt.Errorf("%s: %w", FlagRPCMaxFilters, err)
336+
}
337+
}
338+
if v := opts.Get(FlagRPCMaxBlockFilterHashes); v != nil {
339+
if cfg.RPC.MaxBlockFilterHashes, err = cast.ToUint64E(v); err != nil {
340+
return cfg, fmt.Errorf("%s: %w", FlagRPCMaxBlockFilterHashes, err)
341+
}
342+
}
314343
if v := opts.Get(FlagRPCMaxConcurrentTraceCalls); v != nil {
315344
if cfg.RPC.MaxConcurrentTraceCalls, err = cast.ToIntE(v); err != nil {
316345
return cfg, fmt.Errorf("%s: %w", FlagRPCMaxConcurrentTraceCalls, err)
@@ -404,6 +433,9 @@ func (c Config) Validate() error {
404433
if c.RPC.MaxWSConnections < 0 {
405434
return fmt.Errorf("%s: must be >= 0, got %d", FlagRPCMaxWSConnections, c.RPC.MaxWSConnections)
406435
}
436+
if c.RPC.FilterTimeout < 0 {
437+
return fmt.Errorf("%s: must be >= 0, got %s", FlagRPCFilterTimeout, c.RPC.FilterTimeout)
438+
}
407439
if c.RPC.MaxConcurrentTraceCalls < 0 {
408440
return fmt.Errorf("%s: must be >= 0, got %d", FlagRPCMaxConcurrentTraceCalls, c.RPC.MaxConcurrentTraceCalls)
409441
}
@@ -511,6 +543,17 @@ batch_response_max_size = {{ .Giga.RPC.BatchResponseMaxSize }}
511543
# max_ws_connections caps simultaneously open WebSocket connections.
512544
max_ws_connections = {{ .Giga.RPC.MaxWSConnections }}
513545
546+
# filter_timeout is how long an eth_newFilter or eth_newBlockFilter filter lives without being
547+
# polled.
548+
filter_timeout = "{{ .Giga.RPC.FilterTimeout }}"
549+
550+
# max_filters caps the installed eth_newFilter and eth_newBlockFilter filters.
551+
max_filters = {{ .Giga.RPC.MaxFilters }}
552+
553+
# max_block_filter_hashes caps the unpolled blocks an eth_newBlockFilter filter may fall behind
554+
# by before it is removed.
555+
max_block_filter_hashes = {{ .Giga.RPC.MaxBlockFilterHashes }}
556+
514557
# max_concurrent_trace_calls caps debug_trace* calls running at once; a call past it is refused.
515558
max_concurrent_trace_calls = {{ .Giga.RPC.MaxConcurrentTraceCalls }}
516559

‎giga/config/config_test.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func TestRPCDefaultsMatchTheV2EVMRPC(t *testing.T) {
5454
require.Equal(t, v2.TraceTimeout, r.TraceTimeout)
5555
require.Equal(t, v2.MaxTraceStructLogBytes, uint64(r.MaxTraceStructLogBytes))
5656
require.Equal(t, uint64(100), r.MaxSubscriptionsPerConnection)
57+
require.Equal(t, v2.FilterTimeout, r.FilterTimeout)
58+
require.Equal(t, v2.MaxFilters, r.MaxFilters)
59+
require.Equal(t, v2.MaxBlockFilterHashes, r.MaxBlockFilterHashes)
5760
}
5861

5962
// TestConfigTemplateRendersTheDefaults holds the app.toml template to the section: the rendered
@@ -94,6 +97,9 @@ func TestReadConfigReadsEveryKey(t *testing.T) {
9497
"giga.rpc.batch_request_limit": "10",
9598
"giga.rpc.batch_response_max_size": "1000",
9699
"giga.rpc.max_ws_connections": "7",
100+
"giga.rpc.filter_timeout": "45s",
101+
"giga.rpc.max_filters": "12",
102+
"giga.rpc.max_block_filter_hashes": "34",
97103
"giga.rpc.max_concurrent_trace_calls": "3",
98104
"giga.rpc.trace_timeout": "4s",
99105
"giga.rpc.max_trace_struct_log_bytes": "512",
@@ -128,6 +134,9 @@ func TestReadConfigReadsEveryKey(t *testing.T) {
128134
BatchRequestLimit: 10,
129135
BatchResponseMaxSize: 1000,
130136
MaxWSConnections: 7,
137+
FilterTimeout: 45 * time.Second,
138+
MaxFilters: 12,
139+
MaxBlockFilterHashes: 34,
131140
MaxConcurrentTraceCalls: 3,
132141
TraceTimeout: 4 * time.Second,
133142
MaxTraceStructLogBytes: 512,
@@ -176,6 +185,10 @@ func TestReadConfigRejectsUnusableValues(t *testing.T) {
176185
"non-numeric batch limit": {"giga.rpc.batch_request_limit": "many"},
177186
"non-numeric batch response size": {"giga.rpc.batch_response_max_size": "big"},
178187
"non-numeric ws connections": {"giga.rpc.max_ws_connections": "some"},
188+
"negative filter timeout": {"giga.rpc.filter_timeout": "-1s"},
189+
"non-duration filter timeout": {"giga.rpc.filter_timeout": "later"},
190+
"negative filter cap": {"giga.rpc.max_filters": "-1"},
191+
"non-numeric block filter hashes": {"giga.rpc.max_block_filter_hashes": "plenty"},
179192
"non-boolean state sync": {"giga.state_sync.enabled": "maybe"},
180193
} {
181194
_, err := gigaconfig.ReadConfig(opts)

‎giga/evmonly/rpc/filter.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func (e *invalidParamsError) Unwrap() error {
5454
type filterAPI struct {
5555
backend Backend
5656
store receiptpkg.ReceiptStore
57+
filters *filterRegistry
58+
// maxBlockFilterHashes caps the unpolled blocks a block filter may fall behind by; zero
59+
// disables the cap.
60+
maxBlockFilterHashes uint64
5761
}
5862

5963
// GetLogs returns the logs matching crit from finalized blocks. Open and tag
@@ -68,7 +72,12 @@ func (api *filterAPI) GetLogs(ctx context.Context, crit filters.FilterCriteria)
6872
if toBlock-fromBlock+1 > maxBlocksForLogs {
6973
return nil, &invalidParamsError{error: errLogRangeTooWide}
7074
}
75+
return api.rangeLogs(ctx, crit, fromBlock, toBlock)
76+
}
7177

78+
// rangeLogs returns the logs crit's addresses and topics match in the indexed blocks fromBlock
79+
// through toBlock, within the per-query log caps.
80+
func (api *filterAPI) rangeLogs(ctx context.Context, crit filters.FilterCriteria, fromBlock, toBlock uint64) ([]*ethtypes.Log, error) {
7281
budget := receiptpkg.NewLogBudget(maxLogsPerQuery, receiptpkg.DefaultMaxLogBytes)
7382
logs, err := api.store.FilterLogs(receiptContext(receiptpkg.WithStrictTopicCount(ctx)), fromBlock, toBlock, crit, budget)
7483
if err != nil {

0 commit comments

Comments
 (0)