eth/filter: check begin/end inside rangeFilter

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2023-10-20 12:46:40 +08:00
parent 1ad1f59b06
commit ed3b1e61cf
3 changed files with 20 additions and 10 deletions

View file

@ -34,6 +34,8 @@ import (
) )
var ( var (
ErrInvalidBlockRange = errors.New("invalid from and to block combination: from > to")
errInvalidTopic = errors.New("invalid topic(s)") errInvalidTopic = errors.New("invalid topic(s)")
errFilterNotFound = errors.New("filter not found") errFilterNotFound = errors.New("filter not found")
) )
@ -347,12 +349,16 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit FilterCriteria) ([]*type
if crit.ToBlock != nil { if crit.ToBlock != nil {
end = crit.ToBlock.Int64() end = crit.ToBlock.Int64()
} }
// Fast exit if from > to // Construct the range filter
if begin > 0 && end > 0 && begin > end { var err error
filter, err = api.sys.NewRangeFilter(begin, end, crit.Addresses, crit.Topics)
if err != nil {
// Compatible with old implement
if err == ErrInvalidBlockRange {
return nil, nil return nil, nil
} }
// Construct the range filter return nil, err
filter = api.sys.NewRangeFilter(begin, end, crit.Addresses, crit.Topics) }
} }
// Run the filter and return all the logs // Run the filter and return all the logs
logs, err := filter.Logs(ctx) logs, err := filter.Logs(ctx)
@ -403,7 +409,7 @@ func (api *FilterAPI) GetFilterLogs(ctx context.Context, id rpc.ID) ([]*types.Lo
end = f.crit.ToBlock.Int64() end = f.crit.ToBlock.Int64()
} }
// Construct the range filter // Construct the range filter
filter = api.sys.NewRangeFilter(begin, end, f.crit.Addresses, f.crit.Topics) filter, _ = api.sys.NewRangeFilter(begin, end, f.crit.Addresses, f.crit.Topics)
} }
// Run the filter and return all the logs // Run the filter and return all the logs
logs, err := filter.Logs(ctx) logs, err := filter.Logs(ctx)

View file

@ -42,7 +42,12 @@ type Filter struct {
// NewRangeFilter creates a new filter which uses a bloom filter on blocks to // NewRangeFilter creates a new filter which uses a bloom filter on blocks to
// figure out whether a particular block is interesting or not. // figure out whether a particular block is interesting or not.
func (sys *FilterSystem) NewRangeFilter(begin, end int64, addresses []common.Address, topics [][]common.Hash) *Filter { func (sys *FilterSystem) NewRangeFilter(begin, end int64, addresses []common.Address, topics [][]common.Hash) (*Filter, error) {
// Fast exit if from > to
if begin > 0 && end > 0 && begin > end {
return nil, ErrInvalidBlockRange
}
// Flatten the address and topic filter clauses into a single bloombits filter // Flatten the address and topic filter clauses into a single bloombits filter
// system. Since the bloombits are not positional, nil topics are permitted, // system. Since the bloombits are not positional, nil topics are permitted,
// which get flattened into a nil byte slice. // which get flattened into a nil byte slice.
@ -70,7 +75,7 @@ func (sys *FilterSystem) NewRangeFilter(begin, end int64, addresses []common.Add
filter.begin = begin filter.begin = begin
filter.end = end filter.end = end
return filter return filter, nil
} }
// NewBlockFilter creates a new filter which directly inspects the contents of // NewBlockFilter creates a new filter which directly inspects the contents of

View file

@ -20,7 +20,6 @@ package filters
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -332,7 +331,7 @@ func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*typ
if from >= 0 && to == rpc.LatestBlockNumber { if from >= 0 && to == rpc.LatestBlockNumber {
return es.subscribeLogs(crit, logs), nil return es.subscribeLogs(crit, logs), nil
} }
return nil, errors.New("invalid from and to block combination: from > to") return nil, ErrInvalidBlockRange
} }
// subscribeMinedPendingLogs creates a subscription that returned mined and // subscribeMinedPendingLogs creates a subscription that returned mined and