mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 13:16:42 +00:00
eth/filters: move inner function as private method
Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
parent
964e801961
commit
2c0ca046b5
1 changed files with 110 additions and 107 deletions
|
|
@ -261,7 +261,7 @@ func (api *FilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
|
|||
return rpcSub, nil
|
||||
}
|
||||
|
||||
type notify interface {
|
||||
type notifier interface {
|
||||
Notify(id rpc.ID, data interface{}) error
|
||||
Closed() <-chan interface{}
|
||||
}
|
||||
|
|
@ -277,8 +277,8 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
|
|||
return rpcSub, err
|
||||
}
|
||||
|
||||
func (api *FilterAPI) logs(ctx context.Context, notifier notify, rpcSub *rpc.Subscription, crit FilterCriteria) error {
|
||||
filterLiveLogs := func() error {
|
||||
// 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)
|
||||
logsSub, err := api.events.SubscribeLogs(ethereum.FilterQuery(crit), matchedLogs)
|
||||
if err != nil {
|
||||
|
|
@ -291,36 +291,28 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notify, rpcSub *rpc.Sub
|
|||
case logs := <-matchedLogs:
|
||||
for _, log := range logs {
|
||||
log := log
|
||||
notifier.Notify(rpcSub.ID, &log)
|
||||
notify.Notify(rpcSub.ID, &log)
|
||||
}
|
||||
case <-rpcSub.Err(): // client send an unsubscribe request
|
||||
logsSub.Unsubscribe()
|
||||
return
|
||||
case <-notifier.Closed(): // connection dropped
|
||||
case <-notify.Closed(): // connection dropped
|
||||
logsSub.Unsubscribe()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
isLiveFilter := true
|
||||
if crit.FromBlock != nil {
|
||||
isLiveFilter = crit.FromBlock.Sign() < 0
|
||||
}
|
||||
// do live filter only
|
||||
if isLiveFilter {
|
||||
err := filterLiveLogs()
|
||||
return err
|
||||
}
|
||||
|
||||
filterHistLogs := func(n int64) (int64, error) {
|
||||
// histLogs only retrieves the logs older than current header
|
||||
func (api *FilterAPI) histLogs(ctx context.Context, notifier notifier, rpcSub *rpc.Subscription, crit FilterCriteria) (int64, error) {
|
||||
// the ctx will be canceled after this callback(filter.Logs) is called
|
||||
// and we are run in a background goroutine, use a new context instead
|
||||
var (
|
||||
cctx = context.Background()
|
||||
rmLogsCh = make(chan []*types.Log)
|
||||
n = crit.FromBlock.Int64()
|
||||
)
|
||||
for {
|
||||
// get the latest block header
|
||||
|
|
@ -388,12 +380,23 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notify, rpcSub *rpc.Sub
|
|||
// move forward to the next batch
|
||||
n = head + 1
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
n := crit.FromBlock.Int64()
|
||||
go func() {
|
||||
// do historical sync first
|
||||
head, err := filterHistLogs(n)
|
||||
head, err := api.histLogs(ctx, notifier, rpcSub, crit)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -402,7 +405,7 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notify, rpcSub *rpc.Sub
|
|||
return
|
||||
}
|
||||
// then subscribe from the header
|
||||
filterLiveLogs()
|
||||
api.liveLogs(ctx, notifier, rpcSub, crit)
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue