From 69a14cb0aeb4e8023d89df557318a8affe75dbfe Mon Sep 17 00:00:00 2001 From: jsvisa Date: Tue, 27 Jun 2023 01:04:26 +0800 Subject: [PATCH] eth/filters: testing pending with latest Signed-off-by: jsvisa --- eth/filters/filter_system.go | 23 ++++++++++++++++++++++- eth/filters/filter_system_test.go | 15 +++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/eth/filters/filter_system.go b/eth/filters/filter_system.go index 2ce12f2e94..c5f8318826 100644 --- a/eth/filters/filter_system.go +++ b/eth/filters/filter_system.go @@ -39,6 +39,10 @@ import ( "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. type Config struct { 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 { 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 diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index eb6746aed2..1d15c7dd98 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -809,6 +809,21 @@ func TestLogsSubscription(t *testing.T) { FilterCriteria{}, 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 { FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(3)},