eth/filters: check hist only mode

Signed-off-by: Delweng <delweng@gmail.com>
This commit is contained in:
Delweng 2023-06-13 18:11:14 +08:00 committed by jsvisa
parent f50087ebc7
commit 8c429c95b3
2 changed files with 13 additions and 10 deletions

View file

@ -398,16 +398,19 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notifier, rpcSub *rpc.S
return api.liveLogs(ctx, notifier, rpcSub, crit) 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() { go func() {
// do historical sync first // do historical sync first
head, err := api.histLogs(ctx, notifier, rpcSub, crit) _, err := api.histLogs(ctx, notifier, rpcSub, crit)
if err != nil { if err != nil {
return 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 // then subscribe from the header
api.liveLogs(ctx, notifier, rpcSub, crit) api.liveLogs(ctx, notifier, rpcSub, crit)
}() }()

View file

@ -799,11 +799,11 @@ func TestLogsSubscription(t *testing.T) {
FilterCriteria{FromBlock: big.NewInt(1), ToBlock: pendingBlockNumber}, FilterCriteria{FromBlock: big.NewInt(1), ToBlock: pendingBlockNumber},
allLogs, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, allLogs, newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, 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)},
allLogs[0:3], newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil, // allLogs[0:3], newMockNotifier(), &rpc.Subscription{ID: rpc.NewID()}, nil,
}, // },
} }
// subscribe logs // subscribe logs