From 8c429c95b34cdbb311197e1b78aa261c8724a4a1 Mon Sep 17 00:00:00 2001 From: Delweng Date: Tue, 13 Jun 2023 18:11:14 +0800 Subject: [PATCH] eth/filters: check hist only mode Signed-off-by: Delweng --- eth/filters/api.go | 13 ++++++++----- eth/filters/filter_system_test.go | 10 +++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/eth/filters/api.go b/eth/filters/api.go index 589abed99b..5791a8e703 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -398,16 +398,19 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notifier, rpcSub *rpc.S return api.liveLogs(ctx, notifier, rpcSub, crit) } + // if toBlock is limited and handled, no need to subscribe live logs anymore + if toBlock := crit.ToBlock; toBlock != nil { + if header := api.sys.backend.CurrentHeader(); header != nil && toBlock.Sign() > 0 && toBlock.Cmp(header.Number) <= 0 { + return errors.New("historical only log subscription is not supported") + } + } + go func() { // do historical sync first - head, err := api.histLogs(ctx, notifier, rpcSub, crit) + _, err := api.histLogs(ctx, notifier, rpcSub, crit) if err != nil { return } - // if toBlock is limited and handled, no need to subscribe live logs anymore - if toBlock := crit.ToBlock; toBlock != nil && toBlock.Sign() > 0 && toBlock.Cmp(big.NewInt(head)) <= 0 { - return - } // then subscribe from the header api.liveLogs(ctx, notifier, rpcSub, crit) }() diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index cac36c0ef9..f5b4f2565b 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -799,11 +799,11 @@ func TestLogsSubscription(t *testing.T) { FilterCriteria{FromBlock: big.NewInt(1), ToBlock: pendingBlockNumber}, allLogs, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, }, - // from 1 to 3 - { - FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(3)}, - allLogs[0:3], newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, - }, + // // from 1 to 3 + // { + // FilterCriteria{FromBlock: big.NewInt(1), ToBlock: big.NewInt(3)}, + // allLogs[0:3], newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, + // }, } // subscribe logs