Skip to content

Commit

Permalink
Merge pull request #1792 from CortexFoundation/dev
Browse files Browse the repository at this point in the history
fast exit for invalid block range
  • Loading branch information
ucwong authored Nov 8, 2023
2 parents 8451ab3 + 6cf3989 commit 70176e1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ctxc/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion ctxc/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions ctxc/filters/filter_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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()
Expand Down

0 comments on commit 70176e1

Please sign in to comment.