From 1ad1f59b064b3690be066a6c4b98d0ac0430e6ca Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 20 Oct 2023 12:22:29 +0800 Subject: [PATCH] eth/filters: getLogs fast exit if from>to Signed-off-by: jsvisa --- eth/filters/api.go | 4 ++++ eth/filters/filter_system_test.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/eth/filters/api.go b/eth/filters/api.go index cc08b442e8..e4ce927b17 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -347,6 +347,10 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*type if crit.ToBlock != nil { end = crit.ToBlock.Int64() } + // Fast exit if from > to + if begin > 0 && end > 0 && begin > end { + return nil, nil + } // Construct the range filter filter = api.sys.NewRangeFilter(begin, end, crit.Addresses, crit.Topics) } diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index f7e5327c56..d4bbec62f2 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -429,7 +429,10 @@ func TestInvalidLogFilterCreation(t *testing.T) { } } +// TestLogFilterUninstall tests invalid getLogs requests func TestInvalidGetLogsRequest(t *testing.T) { + t.Parallel() + var ( db = rawdb.NewMemoryDatabase() _, sys = newTestFilterSystem(t, db, Config{}) @@ -451,6 +454,21 @@ func TestInvalidGetLogsRequest(t *testing.T) { } } +// TestInvalidGetRangeLogsRequest tests getLogs with invalid block range +func TestInvalidGetRangeLogsRequest(t *testing.T) { + t.Parallel() + + var ( + db = rawdb.NewMemoryDatabase() + _, sys = newTestFilterSystem(t, db, Config{}) + api = NewFilterAPI(sys, false) + ) + + if _, err := api.GetLogs(context.Background(), FilterCriteria{FromBlock: big.NewInt(2), ToBlock: big.NewInt(1)}); err != nil { + t.Errorf("Expected Logs for invalid range succeed, but got: %v", err) + } +} + // TestLogFilter tests whether log filters match the correct logs that are posted to the event feed. func TestLogFilter(t *testing.T) { t.Parallel()