From 6cf39896a20def79fcbf0414cc2d6eb4b1eb6c8a Mon Sep 17 00:00:00 2001 From: ucwong Date: Tue, 7 Nov 2023 21:27:29 +0800 Subject: [PATCH] fast exit for invalid block range --- ctxc/filters/api.go | 9 +++++++++ ctxc/filters/filter_system.go | 2 +- ctxc/filters/filter_system_test.go | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/ctxc/filters/api.go b/ctxc/filters/api.go index 00a5227b4a..8e1f187a15 100644 --- a/ctxc/filters/api.go +++ b/ctxc/filters/api.go @@ -32,6 +32,12 @@ import ( "github.com/CortexFoundation/CortexTheseus/rpc" ) +var ( + errInvalidTopic = errors.New("invalid topic(s)") + errFilterNotFound = errors.New("filter not found") + errInvalidBlockRange = errors.New("invalid block range params") +) + // filter is a helper struct that holds meta information over the filter type // and associated subscription in the event system. type filter struct { @@ -331,6 +337,9 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*type if crit.ToBlock != nil { end = crit.ToBlock.Int64() } + if begin > 0 && end > 0 && begin > end { + return nil, errInvalidBlockRange + } // Construct the range filter filter = api.sys.NewRangeFilter(begin, end, crit.Addresses, crit.Topics) } diff --git a/ctxc/filters/filter_system.go b/ctxc/filters/filter_system.go index cb21021563..ea6e5bf960 100644 --- a/ctxc/filters/filter_system.go +++ b/ctxc/filters/filter_system.go @@ -295,7 +295,7 @@ func (es *EventSystem) SubscribeLogs(crit cortex.FilterQuery, logs chan []*types if from >= 0 && to == rpc.LatestBlockNumber { return es.subscribeLogs(crit, logs), nil } - return nil, fmt.Errorf("invalid from and to block combination: from > to") + return nil, errInvalidBlockRange } // subscribeMinedPendingLogs creates a subscription that returned mined and diff --git a/ctxc/filters/filter_system_test.go b/ctxc/filters/filter_system_test.go index 8b131d9c32..99e4bff3c6 100644 --- a/ctxc/filters/filter_system_test.go +++ b/ctxc/filters/filter_system_test.go @@ -294,7 +294,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{}) @@ -316,6 +319,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 != errInvalidBlockRange { + t.Errorf("Expected Logs for invalid range return error, 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()