eth/filters: testing pending with latest

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2023-06-27 01:04:26 +08:00
parent 7f3b72fe10
commit 69a14cb0ae
2 changed files with 37 additions and 1 deletions

View file

@ -39,6 +39,10 @@ import (
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
var (
errInvalidFromTo = errors.New("invalid from and to block combination: from > to")
)
// Config represents the configuration of the filter system. // Config represents the configuration of the filter system.
type Config struct { type Config struct {
LogCacheSize int // maximum number of cached blocks (default: 32) LogCacheSize int // maximum number of cached blocks (default: 32)
@ -335,7 +339,24 @@ 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, errInvalidBlockRange return nil, errInvalidFromTo
}
// subscribeMinedPendingLogs creates a subscription that returned mined and
// pending logs that match the given criteria.
func (es *EventSystem) subscribeMinedPendingLogs(crit ethereum.FilterQuery, logs chan []*types.Log) *Subscription {
sub := &subscription{
id: rpc.NewID(),
typ: MinedAndPendingLogsSubscription,
logsCrit: crit,
created: time.Now(),
logs: logs,
txs: make(chan []*types.Transaction),
headers: make(chan *types.Header),
installed: make(chan struct{}),
err: make(chan error),
}
return es.subscribe(sub)
} }
// subscribeLogs creates a subscription that will write all logs matching the // subscribeLogs creates a subscription that will write all logs matching the

View file

@ -809,6 +809,21 @@ func TestLogsSubscription(t *testing.T) {
FilterCriteria{}, FilterCriteria{},
nil, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, nil, nil, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, nil,
}, },
// from pending to pending
{
FilterCriteria{FromBlock: big.NewInt(rpc.PendingBlockNumber.Int64()), ToBlock: big.NewInt(rpc.PendingBlockNumber.Int64())},
nil, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, nil,
},
// from latest to pending
{
FilterCriteria{FromBlock: big.NewInt(rpc.LatestBlockNumber.Int64()), ToBlock: big.NewInt(rpc.PendingBlockNumber.Int64())},
nil, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, nil,
},
// from pending to latest
{
FilterCriteria{FromBlock: big.NewInt(rpc.PendingBlockNumber.Int64()), ToBlock: big.NewInt(rpc.LatestBlockNumber.Int64())},
nil, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, errInvalidFromTo, nil,
},
// from 1 to 3 // from 1 to 3
{ {
FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(3)}, FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(3)},