From 89cce937ab3ef5d8781bc24b8ce75a5c0b42895a Mon Sep 17 00:00:00 2001 From: jsvisa Date: Tue, 27 Jun 2023 00:00:51 +0800 Subject: [PATCH] eth/filters: add notifier interface doc Signed-off-by: jsvisa --- eth/filters/api.go | 64 ++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/eth/filters/api.go b/eth/filters/api.go index 5791a8e703..076117296b 100644 --- a/eth/filters/api.go +++ b/eth/filters/api.go @@ -261,6 +261,8 @@ func (api *FilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) { return rpcSub, nil } +// notifier is used for broadcasting data(eg: logs) to rpc receivers +// used in unit testing type notifier interface { Notify(id rpc.ID, data interface{}) error Closed() <-chan interface{} @@ -277,6 +279,37 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc return rpcSub, err } +// logs is the inner implmention of logs filter +func (api *FilterAPI) logs(ctx context.Context, notifier notifier, rpcSub *rpc.Subscription, crit FilterCriteria) error { + isLiveFilter := true + if crit.FromBlock != nil { + isLiveFilter = crit.FromBlock.Sign() < 0 + } + + // do live filter only + if isLiveFilter { + 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 + _, err := api.histLogs(ctx, notifier, rpcSub, crit) + if err != nil { + return + } + // then subscribe from the header + api.liveLogs(ctx, notifier, rpcSub, crit) + }() + return nil +} + // liveLogs only retrieves live logs func (api *FilterAPI) liveLogs(ctx context.Context, notify notifier, rpcSub *rpc.Subscription, crit FilterCriteria) error { matchedLogs := make(chan []*types.Log) @@ -386,37 +419,6 @@ func (api *FilterAPI) histLogs(ctx context.Context, notifier notifier, rpcSub *r } } -// logs is the inner implmention of logs filter -func (api *FilterAPI) logs(ctx context.Context, notifier notifier, rpcSub *rpc.Subscription, crit FilterCriteria) error { - isLiveFilter := true - if crit.FromBlock != nil { - isLiveFilter = crit.FromBlock.Sign() < 0 - } - - // do live filter only - if isLiveFilter { - 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 - _, err := api.histLogs(ctx, notifier, rpcSub, crit) - if err != nil { - return - } - // then subscribe from the header - api.liveLogs(ctx, notifier, rpcSub, crit) - }() - return nil -} - // FilterCriteria represents a request to create a new filter. // Same as ethereum.FilterQuery but with UnmarshalJSON() method. type FilterCriteria ethereum.FilterQuery