Skip to content

Commit b7aecfe

Browse files
authored
Merge pull request #849 from onflow/mpeter/remove-custom-filter-criteria-type
Replace custom-defined `FilterCriteria` type with the relevant type from Geth
2 parents a6a9a0a + 51c7965 commit b7aecfe

File tree

9 files changed

+427
-152
lines changed

9 files changed

+427
-152
lines changed

api/api.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,6 @@ func (b *BlockChainAPI) GetLogs(
581581
return nil, err
582582
}
583583

584-
filter := logs.FilterCriteria{
585-
Addresses: criteria.Addresses,
586-
Topics: criteria.Topics,
587-
}
588-
589584
// if filter provided specific block ID
590585
if criteria.BlockHash != nil {
591586
// Check if the block exists, and return an error if not.
@@ -598,7 +593,7 @@ func (b *BlockChainAPI) GetLogs(
598593
return []*types.Log{}, nil
599594
}
600595

601-
f, err := logs.NewIDFilter(*criteria.BlockHash, filter, b.blocks, b.receipts)
596+
f, err := logs.NewIDFilter(criteria, b.blocks, b.receipts)
602597
if err != nil {
603598
return handleError[[]*types.Log](err, l, b.collector)
604599
}
@@ -641,7 +636,7 @@ func (b *BlockChainAPI) GetLogs(
641636
to = latest
642637
}
643638

644-
f, err := logs.NewRangeFilter(from.Uint64(), to.Uint64(), filter, b.receipts)
639+
f, err := logs.NewRangeFilter(from.Uint64(), to.Uint64(), criteria, b.receipts)
645640
if err != nil {
646641
return handleError[[]*types.Log](err, l, b.collector)
647642
}

api/pull.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ func newTransactionsFilter(expiry time.Duration, latestHeight uint64, fullTx boo
112112
// Criteria parameter filters the logs according to the criteria values.
113113
type logsFilter struct {
114114
*baseFilter
115-
criteria *filters.FilterCriteria
115+
criteria filters.FilterCriteria
116116
}
117117

118118
func newLogsFilter(
119119
expiry time.Duration,
120120
latestHeight uint64,
121-
criteria *filters.FilterCriteria,
121+
criteria filters.FilterCriteria,
122122
) *logsFilter {
123123
return &logsFilter{
124124
newBaseFilter(expiry, latestHeight),
@@ -272,7 +272,7 @@ func (api *PullAPI) NewFilter(ctx context.Context, criteria filters.FilterCriter
272272
// todo we should check for max range of from-to heights
273273
}
274274

275-
f := newLogsFilter(api.config.FilterExpiry, latest, &criteria)
275+
f := newLogsFilter(api.config.FilterExpiry, latest, criteria)
276276

277277
api.logger.Debug().
278278
Str("id", string(f.id())).
@@ -483,10 +483,6 @@ func (api *PullAPI) getTransactions(latestHeight uint64, filter *transactionsFil
483483

484484
func (api *PullAPI) getLogs(latestHeight uint64, filter *logsFilter) (any, error) {
485485
nextHeight := filter.next()
486-
criteria := logs.FilterCriteria{
487-
Addresses: filter.criteria.Addresses,
488-
Topics: filter.criteria.Topics,
489-
}
490486

491487
to := filter.criteria.ToBlock
492488
// we use latest as default for end range
@@ -506,7 +502,7 @@ func (api *PullAPI) getLogs(latestHeight uint64, filter *logsFilter) (any, error
506502
return []*gethTypes.Log{}, nil
507503
}
508504

509-
f, err := logs.NewRangeFilter(start, end, criteria, api.receipts)
505+
f, err := logs.NewRangeFilter(start, end, filter.criteria, api.receipts)
510506
if err != nil {
511507
return nil, fmt.Errorf("could not create range filter from %d to %d: %w", start, end, err)
512508
}

api/stream.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ func (s *StreamAPI) NewPendingTransactions(ctx context.Context, fullTx *bool) (*
9292

9393
// Logs creates a subscription that fires for all new log that match the given filter criteria.
9494
func (s *StreamAPI) Logs(ctx context.Context, criteria filters.FilterCriteria) (*rpc.Subscription, error) {
95-
logCriteria, err := logs.NewFilterCriteria(criteria.Addresses, criteria.Topics)
96-
if err != nil {
97-
return nil, fmt.Errorf("failed to create log subscription filter: %w", err)
98-
}
99-
10095
return newSubscription(
10196
ctx,
10297
s.logger,
@@ -106,7 +101,7 @@ func (s *StreamAPI) Logs(ctx context.Context, criteria filters.FilterCriteria) (
106101
for _, log := range allLogs {
107102
// todo we could optimize this matching for cases where we have multiple subscriptions
108103
// using the same filter criteria, we could only filter once and stream to all subscribers
109-
if !logs.ExactMatch(log, logCriteria) {
104+
if !logs.ExactMatch(log, criteria) {
110105
continue
111106
}
112107

services/logs/filter.go

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,22 @@ import (
88
"github.com/onflow/flow-evm-gateway/storage"
99
"github.com/onflow/go-ethereum/common"
1010
gethTypes "github.com/onflow/go-ethereum/core/types"
11+
"github.com/onflow/go-ethereum/eth/filters"
1112
)
1213

13-
// The maximum number of topic criteria allowed
14-
const maxTopics = 4
15-
16-
// The maximum number of addresses allowed
17-
const maxAddresses = 6
18-
19-
// FilterCriteria for log filtering.
20-
// Address of the contract emitting the log.
21-
// Topics that match the log topics, following the format:
22-
// [] “anything”
23-
// [A] “A in first position (and anything after)”
24-
// [null, B] “anything in first position AND B in second position (and anything after)”
25-
// [A, B] “A in first position AND B in second position (and anything after)”
26-
// [[A, B], [A, B]] “(A OR B) in first position AND (A OR B) in second position (and anything after)”
27-
type FilterCriteria struct {
28-
Addresses []common.Address
29-
Topics [][]common.Hash
30-
}
31-
32-
func NewFilterCriteria(addresses []common.Address, topics [][]common.Hash) (*FilterCriteria, error) {
33-
if len(topics) > maxTopics {
34-
return nil, fmt.Errorf("max topics exceeded, only %d allowed, got %d", maxTopics, len(topics))
35-
}
36-
if len(addresses) > maxAddresses {
37-
return nil, fmt.Errorf("max addresses exceeded, only %d allowed, got %d", maxAddresses, len(addresses))
38-
}
39-
40-
return &FilterCriteria{
41-
Addresses: addresses,
42-
Topics: topics,
43-
}, nil
44-
}
45-
4614
// RangeFilter matches all the indexed logs within the range defined as
4715
// start and end block height. The start must be strictly smaller or equal than end value.
4816
type RangeFilter struct {
4917
start, end uint64
50-
criteria *FilterCriteria
18+
criteria filters.FilterCriteria
5119
receipts storage.ReceiptIndexer
5220
}
5321

5422
func NewRangeFilter(
5523
start, end uint64,
56-
criteria FilterCriteria,
24+
criteria filters.FilterCriteria,
5725
receipts storage.ReceiptIndexer,
5826
) (*RangeFilter, error) {
59-
if len(criteria.Topics) > maxTopics {
60-
return nil, fmt.Errorf("max topics exceeded, only %d allowed, got %d", maxTopics, len(criteria.Topics))
61-
}
62-
6327
// make sure that beginning number is not bigger than end
6428
if start > end {
6529
return nil, fmt.Errorf(
@@ -73,7 +37,7 @@ func NewRangeFilter(
7337
return &RangeFilter{
7438
start: start,
7539
end: end,
76-
criteria: &criteria,
40+
criteria: criteria,
7741
receipts: receipts,
7842
}, nil
7943
}
@@ -126,24 +90,23 @@ func (r *RangeFilter) Match() ([]*gethTypes.Log, error) {
12690
// by the provided block ID.
12791
type IDFilter struct {
12892
id common.Hash
129-
criteria *FilterCriteria
93+
criteria filters.FilterCriteria
13094
blocks storage.BlockIndexer
13195
receipts storage.ReceiptIndexer
13296
}
13397

13498
func NewIDFilter(
135-
id common.Hash,
136-
criteria FilterCriteria,
99+
criteria filters.FilterCriteria,
137100
blocks storage.BlockIndexer,
138101
receipts storage.ReceiptIndexer,
139102
) (*IDFilter, error) {
140-
if len(criteria.Topics) > maxTopics {
141-
return nil, fmt.Errorf("max topics exceeded, only %d allowed, got %d", maxTopics, len(criteria.Topics))
103+
if criteria.BlockHash == nil {
104+
return nil, fmt.Errorf("filter criteria should have a non-nil block hash")
142105
}
143106

144107
return &IDFilter{
145-
id: id,
146-
criteria: &criteria,
108+
id: *criteria.BlockHash,
109+
criteria: criteria,
147110
blocks: blocks,
148111
receipts: receipts,
149112
}, nil
@@ -173,7 +136,7 @@ func (i *IDFilter) Match() ([]*gethTypes.Log, error) {
173136
}
174137

175138
// ExactMatch checks the topic and address values of the log match the filter exactly.
176-
func ExactMatch(log *gethTypes.Log, criteria *FilterCriteria) bool {
139+
func ExactMatch(log *gethTypes.Log, criteria filters.FilterCriteria) bool {
177140
// check criteria doesn't have more topics than the log, but it can have less due to wildcards
178141
if len(criteria.Topics) > len(log.Topics) {
179142
return false
@@ -203,7 +166,7 @@ func ExactMatch(log *gethTypes.Log, criteria *FilterCriteria) bool {
203166
// If true is returned we should further check against the exactMatch to really make sure the log is matched.
204167
//
205168
// source: https://github.com/ethereum/go-ethereum/blob/8d1db1601d3a9e4fd067558a49db6f0b879c9b48/eth/filters/filter.go#L395
206-
func bloomMatch(bloom gethTypes.Bloom, criteria *FilterCriteria) bool {
169+
func bloomMatch(bloom gethTypes.Bloom, criteria filters.FilterCriteria) bool {
207170
if len(criteria.Addresses) > 0 {
208171
var included bool
209172
for _, addr := range criteria.Addresses {

0 commit comments

Comments
 (0)