Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
Sina Mahmoodi 2023-06-29 18:04:05 +02:00 committed by jsvisa
parent 6419fa7f38
commit 8f71da4d06

View file

@ -276,7 +276,7 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notifier, rpcSub *rpc.S
return errInvalidToBlock return errInvalidToBlock
} }
if crit.FromBlock == nil { if crit.FromBlock == nil {
return api.liveLogs(ctx, notifier, rpcSub, crit) return api.liveLogs(notifier, rpcSub, crit)
} }
from := rpc.BlockNumber(crit.FromBlock.Int64()) from := rpc.BlockNumber(crit.FromBlock.Int64())
switch from { switch from {
@ -294,18 +294,18 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notifier, rpcSub *rpc.S
} }
go func() { go func() {
// do historical sync first // do historical sync first
_, err := api.histLogs(ctx, notifier, rpcSub, int64(from), crit) _, err := api.histLogs(notifier, rpcSub, int64(from), crit)
if err != nil { if err != nil {
return return
} }
// then subscribe from the header // then subscribe from the header
api.liveLogs(ctx, notifier, rpcSub, crit) api.liveLogs(notifier, rpcSub, crit)
}() }()
return nil return nil
} }
// liveLogs only retrieves live logs // liveLogs only retrieves live logs
func (api *FilterAPI) liveLogs(ctx context.Context, notify notifier, rpcSub *rpc.Subscription, crit FilterCriteria) error { func (api *FilterAPI) liveLogs(notify notifier, rpcSub *rpc.Subscription, crit FilterCriteria) error {
matchedLogs := make(chan []*types.Log) matchedLogs := make(chan []*types.Log)
logsSub, err := api.events.SubscribeLogs(ethereum.FilterQuery(crit), matchedLogs) logsSub, err := api.events.SubscribeLogs(ethereum.FilterQuery(crit), matchedLogs)
if err != nil { if err != nil {
@ -332,31 +332,31 @@ func (api *FilterAPI) liveLogs(ctx context.Context, notify notifier, rpcSub *rpc
return nil return nil
} }
// histLogs only retrieves the logs older than current header // histLogs retrieves the logs older than current header.
func (api *FilterAPI) histLogs(ctx context.Context, notifier notifier, rpcSub *rpc.Subscription, n int64, crit FilterCriteria) (int64, error) { func (api *FilterAPI) histLogs(notifier notifier, rpcSub *rpc.Subscription, from int64, crit FilterCriteria) (int64, error) {
// the ctx will be canceled after this callback(filter.Logs) is called // The original request ctx will be canceled as soon as the parent goroutine
// and we are run in a background goroutine, use a new context instead // returns a subscription. Use a new context instead.
var ( var (
cctx = context.Background() ctx = context.Background()
rmLogsCh = make(chan []*types.Log) rmLogsCh = make(chan []*types.Log)
) )
for { for {
// get the latest block header // Get the latest block header.
header := api.sys.backend.CurrentHeader() header := api.sys.backend.CurrentHeader()
if header == nil { if header == nil {
return 0, errors.New("header is null") return 0, errors.New("unexpected error: no header block found")
} }
head := header.Number.Int64() head := header.Number.Int64()
if n >= head { if from >= head {
return head, nil return head, nil
} }
// do historical sync from n to head // Do historical sync beginning at `from` to head.
f := api.sys.NewRangeFilter(n, head, crit.Addresses, crit.Topics) f := api.sys.NewRangeFilter(from, head, crit.Addresses, crit.Topics)
logChan, errChan := f.rangeLogsAsync(cctx) logChan, errChan := f.rangeLogsAsync(ctx)
// subscribe rmLogs // subscribe rmLogs
query := ethereum.FilterQuery{FromBlock: big.NewInt(n), ToBlock: big.NewInt(head), Addresses: crit.Addresses, Topics: crit.Topics} query := ethereum.FilterQuery{FromBlock: big.NewInt(from), ToBlock: big.NewInt(head), Addresses: crit.Addresses, Topics: crit.Topics}
rmLogsSub, err := api.events.SubscribeLogs(query, rmLogsCh) rmLogsSub, err := api.events.SubscribeLogs(query, rmLogsCh)
if err != nil { if err != nil {
return 0, err return 0, err
@ -404,7 +404,7 @@ func (api *FilterAPI) histLogs(ctx context.Context, notifier notifier, rpcSub *r
} }
// move forward to the next batch // move forward to the next batch
n = head + 1 from = head + 1
} }
} }