Skip to content

Commit f20b334

Browse files
authored
eth/filters: eth_getLogs fast exit for invalid block range (#28386)
1 parent 97ae324 commit f20b334

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

eth/filters/api.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ import (
3434
)
3535

3636
var (
37-
errInvalidTopic = errors.New("invalid topic(s)")
38-
errFilterNotFound = errors.New("filter not found")
37+
errInvalidTopic = errors.New("invalid topic(s)")
38+
errFilterNotFound = errors.New("filter not found")
39+
errInvalidBlockRange = errors.New("invalid block range params")
3940
)
4041

4142
// filter is a helper struct that holds meta information over the filter type
@@ -347,6 +348,9 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*type
347348
if crit.ToBlock != nil {
348349
end = crit.ToBlock.Int64()
349350
}
351+
if begin > 0 && end > 0 && begin > end {
352+
return nil, errInvalidBlockRange
353+
}
350354
// Construct the range filter
351355
filter = api.sys.NewRangeFilter(begin, end, crit.Addresses, crit.Topics)
352356
}

eth/filters/filter_system.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package filters
2020

2121
import (
2222
"context"
23-
"errors"
2423
"fmt"
2524
"sync"
2625
"sync/atomic"
@@ -332,7 +331,7 @@ func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*typ
332331
if from >= 0 && to == rpc.LatestBlockNumber {
333332
return es.subscribeLogs(crit, logs), nil
334333
}
335-
return nil, errors.New("invalid from and to block combination: from > to")
334+
return nil, errInvalidBlockRange
336335
}
337336

338337
// subscribeMinedPendingLogs creates a subscription that returned mined and

eth/filters/filter_system_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,10 @@ func TestInvalidLogFilterCreation(t *testing.T) {
429429
}
430430
}
431431

432+
// TestLogFilterUninstall tests invalid getLogs requests
432433
func TestInvalidGetLogsRequest(t *testing.T) {
434+
t.Parallel()
435+
433436
var (
434437
db = rawdb.NewMemoryDatabase()
435438
_, sys = newTestFilterSystem(t, db, Config{})
@@ -451,6 +454,21 @@ func TestInvalidGetLogsRequest(t *testing.T) {
451454
}
452455
}
453456

457+
// TestInvalidGetRangeLogsRequest tests getLogs with invalid block range
458+
func TestInvalidGetRangeLogsRequest(t *testing.T) {
459+
t.Parallel()
460+
461+
var (
462+
db = rawdb.NewMemoryDatabase()
463+
_, sys = newTestFilterSystem(t, db, Config{})
464+
api = NewFilterAPI(sys, false)
465+
)
466+
467+
if _, err := api.GetLogs(context.Background(), FilterCriteria{FromBlock: big.NewInt(2), ToBlock: big.NewInt(1)}); err != errInvalidBlockRange {
468+
t.Errorf("Expected Logs for invalid range return error, but got: %v", err)
469+
}
470+
}
471+
454472
// TestLogFilter tests whether log filters match the correct logs that are posted to the event feed.
455473
func TestLogFilter(t *testing.T) {
456474
t.Parallel()

0 commit comments

Comments
 (0)