Skip to content

Commit 47b8cac

Browse files
authored
rpc: block filters return every block since the last poll (#4683)
On nodes without the Autobahn `BlockHeaderNotifier`, `eth_getFilterChanges` on a block filter dropped blocks. It read the CometBFT event log, which returns 10 items newest first, and moved its cursor to the newest one, so a poll covering more than 10 blocks returned only the newest 10. The first poll also returned blocks from before the filter was created. The filter now keeps a block height as its cursor, starting at the block after the safe latest height, and `blockHashesFrom` reads hashes from the block store through `BlockchainInfo`, oldest first, up to `max_block_filter_hashes` heights per poll. Fixing the paging on the event log instead would not be enough: the log keeps only 30 seconds of events by default, less than the 120-second filter timeout. When pruning passes the cursor, the poll fails with an error naming the pruned blocks and the filter continues from the earliest block, as log filters already do. Both filter types now update their cursor through `advanceFilterCursor`, which never moves it backward. `eth_getFilterLogs` now rejects block filters, as geth does. The change affects the RPC only, not consensus or state.
1 parent 617f7b7 commit 47b8cac

11 files changed

Lines changed: 595 additions & 189 deletions

‎evmrpc/AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ EVM RPCs prefixed by `eth_` and `debug_` on Sei generally follows [Ethereum's sp
4343
- `eth_syncing`
4444
- `eth_getProof` — deprecated rather than permanently incompatible; the message directs callers who need proofs to the Sei team.
4545
- **Log filters poll from a cursor.** `eth_newFilter` fixes where its polls start, once: a numeric `fromBlock` (or `earliest`, which is block 0) as given, otherwise the block after the head at creation; a numeric `toBlock` below that start is rejected. Each `eth_getFilterChanges` scans from that cursor to the head, or to a numeric `toBlock` below it; a tag `toBlock` such as `latest` follows the head and never ends the filter. A numeric `fromBlock` above 0 that is below the earliest available block is rejected, while a `fromBlock` of 0 polls from that block. When pruning passes the cursor of a filter, its next poll fails with an error naming the pruned blocks, and the filter continues from the earliest block. A poll covers at most `max_blocks_for_log` blocks, and fewer when its logs exceed the log caps; later polls return the rest. A block whose logs alone exceed the caps fails the poll that reaches it with an error naming the block, and the filter or subscription continues after it. Unlike geth, a numeric `fromBlock` in the past makes the first poll return that history. `eth_getFilterLogs` resolves tags when called and never moves the cursor. `eth_subscribe("logs")` polls through the same cursor logic, and treats a `fromBlock` of 0 with a tag `toBlock`, which ethclient sends for an open-ended query, as an omitted `fromBlock`. A failed subscription poll notifies `{"error": message}` in place of logs, adding the skipped block's `blockNumber` when the subscription continues past an oversized block; any other failure, pruned blocks included, ends the subscription.
46+
- **Block filters.** Under Autobahn, `eth_newBlockFilter` collects hashes from the `BlockHeaderNotifier`, and a filter holding `max_block_filter_hashes` unpolled hashes is removed. Otherwise there is no notifier, and the filter polls the CometBFT block store from a cursor that starts at the block after the head at creation, so no poll returns a block from before the filter existed. Each `eth_getFilterChanges` covers at most `max_block_filter_hashes` heights from the cursor up to the head and returns their hashes oldest first; later polls cover the rest. A block store read failure leaves the cursor where it was, so the next poll retries the same heights. When pruning passes the cursor, before or during a poll, that poll fails with a `-32000` error naming the pruned blocks, and the filter continues from the earliest block. Concurrent polls of one filter may return the same hashes. `eth_getFilterLogs` rejects a block filter as not existing, as geth does.
4647

4748
## `sei_` prefixed endpoints
4849
The legacy `sei_` namespace contains address and Cosmos transaction helpers.

‎evmrpc/config/config.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ type Config struct {
131131
// maximum number of active eth_newFilter and eth_newBlockFilter filters
132132
MaxFilters uint64 `mapstructure:"max_filters"`
133133

134-
// maximum number of unpolled hashes retained by an eth_newBlockFilter filter
134+
// maximum number of block hashes an eth_newBlockFilter filter holds unpolled (Autobahn) or
135+
// block heights one poll covers (otherwise)
135136
MaxBlockFilterHashes uint64 `mapstructure:"max_block_filter_hashes"`
136137

137138
// checkTx timeout for sig verify
@@ -999,8 +1000,9 @@ filter_timeout = "{{ .EVM.FilterTimeout }}"
9991000
# filters cannot be added if there are too many active eth_newFilter and eth_newBlockFilter
10001001
max_filters = {{ .EVM.MaxFilters }}
10011002
1002-
# maximum number of unpolled hashes retained by an eth_newBlockFilter filter;
1003-
# if a filter has more than this many unpolled hashes the filter will be invalidated and removed.
1003+
# maximum number of block hashes an eth_newBlockFilter filter handles at once.
1004+
# Under Autobahn a filter holding this many unpolled hashes is invalidated and removed;
1005+
# otherwise one poll covers at most this many block heights and later polls cover the rest.
10041006
max_block_filter_hashes = {{ .EVM.MaxBlockFilterHashes }}
10051007
10061008
# checkTx timeout for sig verify

‎evmrpc/export_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"math/big"
77
"sync"
8+
"testing"
9+
"time"
810

911
gethcommon "github.com/ethereum/go-ethereum/common"
1012
"github.com/ethereum/go-ethereum/common/hexutil"
@@ -66,6 +68,16 @@ func NewFilterConfigForTest(cfg FilterConfigTest) *FilterConfig {
6668
return filterCfg
6769
}
6870

71+
// NewBlockFilterAPIForTest returns a FilterAPI without a blockHeaderNotifier that reads blocks
72+
// from tmClient and the earliest and latest heights from heightClient.
73+
func NewBlockFilterAPIForTest(t *testing.T, tmClient, heightClient cosmoclient.LocalClient) *FilterAPI {
74+
return newTestFilterAPIWithClient(t, nil, &FilterConfig{
75+
timeout: time.Hour,
76+
maxFilters: DefaultMaxFilters,
77+
maxBlockFilterHashes: DefaultMaxBlockFilterHashes,
78+
}, tmClient, heightClient, 1<<40)
79+
}
80+
6981
// LogFetcherTestDeps wires a LogFetcher for range-path integration tests.
7082
type LogFetcherTestDeps struct {
7183
TmClient cosmoclient.LocalClient

‎evmrpc/filter.go‎

Lines changed: 116 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"errors"
88
"fmt"
99
"log/slog"
10+
"math"
1011
"math/big"
1112
"runtime/debug"
13+
"slices"
1214
"sort"
1315
"sync"
1416
"time"
@@ -25,7 +27,6 @@ import (
2527
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
2628
"github.com/sei-protocol/sei-chain/sei-db/ledger_db/receipt"
2729
"github.com/sei-protocol/sei-chain/sei-tendermint/rpc/coretypes"
28-
tmtypes "github.com/sei-protocol/sei-chain/sei-tendermint/types"
2930
"github.com/sei-protocol/sei-chain/x/evm/keeper"
3031
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
3132
"github.com/sei-protocol/seilog"
@@ -218,7 +219,9 @@ type filter struct {
218219
lastAccess time.Time
219220

220221
// BlocksSubscription
221-
blockCursor string
222+
// blockCursor is the cursor blockHashesFrom takes: the first block height the filter's next
223+
// poll returns, when no blockHeaderNotifier feeds blockHashes.
224+
blockCursor int64
222225
blockHashes []common.Hash
223226

224227
// LogsSubscription
@@ -500,6 +503,28 @@ func (a *FilterAPI) updateFilterAccess(filterID ethrpc.ID) {
500503
}
501504
}
502505

506+
// advanceFilterCursor moves a polling filter's cursor forward to cursor, and never back. Callers
507+
// apply it to the cursor a poll returns before they check the poll's error: a poll that fails on
508+
// blocks it cannot serve returns a cursor past them. Concurrent polls of one filter read the same
509+
// cursor and may return the same blocks; keeping the furthest cursor stops a slower poll from
510+
// delivering them yet again.
511+
func (a *FilterAPI) advanceFilterCursor(filterID ethrpc.ID, cursor int64) {
512+
a.filtersMu.Lock()
513+
defer a.filtersMu.Unlock()
514+
515+
f, exists := a.filters[filterID]
516+
if !exists {
517+
return
518+
}
519+
switch f.typ {
520+
case BlocksSubscription:
521+
f.blockCursor = max(f.blockCursor, cursor)
522+
case LogsSubscription:
523+
f.logCursor = max(f.logCursor, cursor)
524+
}
525+
a.filters[filterID] = f
526+
}
527+
503528
const NewFilterMethod = "newFilter"
504529

505530
func (a *FilterAPI) NewFilter(
@@ -540,13 +565,23 @@ func (a *FilterAPI) NewBlockFilter(
540565
recordMetricsWithError(ctx, fmt.Sprintf("%s_newBlockFilter", a.namespace), a.connectionType, startTime, err, recover())
541566
}()
542567

568+
var start int64
569+
if a.blockHeaderNotifier == nil {
570+
earliest, _, head, err := a.logFetcher.watermarks.Watermarks(ctx)
571+
if err != nil {
572+
return "", err
573+
}
574+
// On a state-synced node the block store can begin above the safe latest height.
575+
start = max(head+1, earliest)
576+
}
577+
543578
_, cancel := context.WithCancel(a.shutdownCtx)
544579

545580
curFilterID, err := a.addFilter(filter{
546581
typ: BlocksSubscription,
547582
cancelFunc: cancel,
548583
lastAccess: time.Now(),
549-
blockCursor: "",
584+
blockCursor: start,
550585
})
551586
if err != nil {
552587
cancel()
@@ -595,34 +630,15 @@ func (a *FilterAPI) GetFilterChanges(
595630
if a.blockHeaderNotifier != nil {
596631
return a.takeBlockHashes(filterID)
597632
}
598-
hashes, cursor, err := a.getBlockHeadersAfter(ctx, filter.blockCursor)
633+
hashes, cursor, err := a.blockHashesFrom(ctx, filter.blockCursor)
634+
a.advanceFilterCursor(filterID, cursor)
599635
if err != nil {
600636
return nil, err
601637
}
602-
603-
// Update filter with write lock
604-
a.filtersMu.Lock()
605-
if updatedFilter, exists := a.filters[filterID]; exists {
606-
updatedFilter.blockCursor = cursor
607-
a.filters[filterID] = updatedFilter
608-
}
609-
a.filtersMu.Unlock()
610-
611638
return hashes, nil
612639
case LogsSubscription:
613640
logs, cursor, err := a.logFetcher.pollLogs(ctx, filter.fc, filter.logCursor)
614-
615-
// Update filter with write lock, also when the poll failed, as a poll that fails on a
616-
// block it cannot serve moves the cursor past it. A concurrent poll of the same filter
617-
// may have advanced the cursor further, and moving it back would deliver those blocks
618-
// again.
619-
a.filtersMu.Lock()
620-
if updatedFilter, exists := a.filters[filterID]; exists && cursor > updatedFilter.logCursor {
621-
updatedFilter.logCursor = cursor
622-
a.filters[filterID] = updatedFilter
623-
}
624-
a.filtersMu.Unlock()
625-
641+
a.advanceFilterCursor(filterID, cursor)
626642
if err != nil {
627643
return nil, err
628644
}
@@ -651,7 +667,7 @@ func (a *FilterAPI) GetFilterLogs(
651667
filter, ok := a.filters[filterID]
652668
a.filtersMu.RUnlock()
653669

654-
if !ok {
670+
if !ok || filter.typ != LogsSubscription {
655671
return nil, errors.New("filter does not exist")
656672
}
657673

@@ -741,42 +757,84 @@ func (a *FilterAPI) GetLogs(ctx context.Context, crit filters.FilterCriteria) (r
741757
return logs, nil
742758
}
743759

744-
// get block headers after a certain cursor. Can use an empty string cursor
745-
// to get the latest block header.
746-
func (a *FilterAPI) getBlockHeadersAfter(
747-
ctx context.Context,
748-
cursor string,
749-
) ([]common.Hash, string, error) {
750-
q := NewBlockQueryBuilder()
751-
builtQuery := q.Build()
752-
hasMore := true
753-
headers := []common.Hash{}
754-
for hasMore {
755-
res, err := a.tmClient.Events(ctx, &coretypes.RequestEvents{
756-
Filter: &coretypes.EventFilter{Query: builtQuery},
757-
After: cursor,
758-
})
760+
// blockHashesFrom returns the hashes of the blocks from cursor on, oldest first, and the cursor the
761+
// next poll starts from, also when it returns an error. A poll covers at most maxBlockFilterHashes
762+
// heights up to the safe latest height, which must not exceed the block store's height. A cursor
763+
// below the earliest available block, found before or during the poll, fails it with an error
764+
// naming the pruned blocks, and the returned cursor is that block. It reads the CometBFT block
765+
// store, so it serves only nodes without a blockHeaderNotifier.
766+
func (a *FilterAPI) blockHashesFrom(ctx context.Context, cursor int64) ([]common.Hash, int64, error) {
767+
earliest, _, head, err := a.logFetcher.watermarks.Watermarks(ctx)
768+
if err != nil {
769+
return nil, cursor, err
770+
}
771+
if cursor < earliest {
772+
return nil, earliest, prunedBlocksError(cursor, earliest)
773+
}
774+
// #nosec G115 -- min with math.MaxInt32 fits the cap in an int64 with room for cursor+span-1 below
775+
span := int64(min(a.filterConfig.maxBlockFilterHashes, math.MaxInt32))
776+
to := min(head, cursor+span-1)
777+
if to < cursor {
778+
return []common.Hash{}, cursor, nil
779+
}
780+
hashes, err := a.blockHashesDescending(ctx, cursor, to)
781+
if errors.Is(err, coretypes.ErrInvalidRequest) {
782+
return a.prunedDuringPoll(ctx, cursor, err)
783+
}
784+
if err != nil {
785+
return nil, cursor, err
786+
}
787+
slices.Reverse(hashes)
788+
return hashes, to + 1, nil
789+
}
790+
791+
// prunedDuringPoll returns the result of a poll from cursor whose block store read failed with
792+
// err. When pruning passed the cursor during the poll, it fails with an error naming the pruned
793+
// blocks and returns the earliest available block as the next cursor, as a poll that found them
794+
// pruned beforehand does; otherwise it returns err and leaves the cursor where it was.
795+
func (a *FilterAPI) prunedDuringPoll(ctx context.Context, cursor int64, err error) ([]common.Hash, int64, error) {
796+
earliest, _, _, wmErr := a.logFetcher.watermarks.Watermarks(ctx)
797+
if wmErr != nil || cursor >= earliest {
798+
return nil, cursor, err
799+
}
800+
return nil, earliest, prunedBlocksError(cursor, earliest)
801+
}
802+
803+
// prunedBlocksError reports that the blocks from cursor up to earliest were pruned before a block
804+
// filter read them.
805+
func prunedBlocksError(cursor, earliest int64) error {
806+
return fmt.Errorf("blocks %d to %d were pruned before the filter read them", cursor, earliest-1)
807+
}
808+
809+
// blockHashesDescending returns the hashes of the blocks the block store holds from height from to
810+
// height to, newest first. It pages through BlockchainInfo by the heights each page returns.
811+
func (a *FilterAPI) blockHashesDescending(ctx context.Context, from, to int64) ([]common.Hash, error) {
812+
hashes := make([]common.Hash, 0, min(to-from+1, DefaultMaxBlockFilterHashes))
813+
for hi := to; hi >= from; {
814+
if err := ctx.Err(); err != nil {
815+
return nil, err
816+
}
817+
res, err := a.tmClient.BlockchainInfo(ctx, from, hi)
759818
if err != nil {
760-
return nil, "", err
819+
return nil, fmt.Errorf("fetching block headers %d to %d: %w", from, hi, err)
761820
}
762-
hasMore = res.More
763-
cursor = res.Newest
764-
765-
for _, item := range res.Items {
766-
wrapper := EventItemDataWrapper{}
767-
err := json.Unmarshal(item.Data, &wrapper)
768-
if err != nil {
769-
return nil, "", err
770-
}
771-
block := tmtypes.EventDataNewBlock{}
772-
err = json.Unmarshal(wrapper.Value, &block)
773-
if err != nil {
774-
return nil, "", err
775-
}
776-
headers = append(headers, common.BytesToHash(block.Block.Hash()))
821+
if len(res.BlockMetas) == 0 {
822+
// The CometBFT block store has no holes between its base and height, so this guards
823+
// another provider: the page below hi holds no block, and its size is the provider's
824+
// to choose, so only hi is known to be missing.
825+
hi--
826+
continue
777827
}
828+
for _, meta := range res.BlockMetas {
829+
hashes = append(hashes, common.BytesToHash(meta.BlockID.Hash))
830+
}
831+
lowest := res.BlockMetas[len(res.BlockMetas)-1].Header.Height
832+
if lowest > hi || lowest < from {
833+
return nil, fmt.Errorf("block headers %d to %d returned block %d", from, hi, lowest)
834+
}
835+
hi = lowest - 1
778836
}
779-
return headers, cursor, nil
837+
return hashes, nil
780838
}
781839

782840
func (a *FilterAPI) UninstallFilter(

0 commit comments

Comments
 (0)