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
|
return rpcSub, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type notify interface {
|
type notifier interface {
|
||||||
Notify(id rpc.ID, data interface{}) error
|
Notify(id rpc.ID, data interface{}) error
|
||||||
Closed() <-chan interface{}
|
Closed() <-chan interface{}
|
||||||
}
|
}
|
||||||
|
|
@ -277,8 +277,8 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
|
||||||
return rpcSub, err
|
return rpcSub, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *FilterAPI) logs(ctx context.Context, notifier notify, rpcSub *rpc.Subscription, crit FilterCriteria) error {
|
// liveLogs only retrieves live logs
|
||||||
filterLiveLogs := func() error {
|
func (api *FilterAPI) liveLogs(ctx context.Context, 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 {
|
||||||
|
|
@ -291,12 +291,12 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notify, rpcSub *rpc.Sub
|
||||||
case logs := <-matchedLogs:
|
case logs := <-matchedLogs:
|
||||||
for _, log := range logs {
|
for _, log := range logs {
|
||||||
log := log
|
log := log
|
||||||
notifier.Notify(rpcSub.ID, &log)
|
notify.Notify(rpcSub.ID, &log)
|
||||||
}
|
}
|
||||||
case <-rpcSub.Err(): // client send an unsubscribe request
|
case <-rpcSub.Err(): // client send an unsubscribe request
|
||||||
logsSub.Unsubscribe()
|
logsSub.Unsubscribe()
|
||||||
return
|
return
|
||||||
case <-notifier.Closed(): // connection dropped
|
case <-notify.Closed(): // connection dropped
|
||||||
logsSub.Unsubscribe()
|
logsSub.Unsubscribe()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -305,22 +305,14 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notify, rpcSub *rpc.Sub
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
isLiveFilter := true
|
// histLogs only retrieves the logs older than current header
|
||||||
if crit.FromBlock != nil {
|
func (api *FilterAPI) histLogs(ctx context.Context, notifier notifier, rpcSub *rpc.Subscription, crit FilterCriteria) (int64, error) {
|
||||||
isLiveFilter = crit.FromBlock.Sign() < 0
|
|
||||||
}
|
|
||||||
// do live filter only
|
|
||||||
if isLiveFilter {
|
|
||||||
err := filterLiveLogs()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
filterHistLogs := func(n int64) (int64, error) {
|
|
||||||
// the ctx will be canceled after this callback(filter.Logs) is called
|
// 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
|
// and we are run in a background goroutine, use a new context instead
|
||||||
var (
|
var (
|
||||||
cctx = context.Background()
|
cctx = context.Background()
|
||||||
rmLogsCh = make(chan []*types.Log)
|
rmLogsCh = make(chan []*types.Log)
|
||||||
|
n = crit.FromBlock.Int64()
|
||||||
)
|
)
|
||||||
for {
|
for {
|
||||||
// get the latest block header
|
// get the latest block header
|
||||||
|
|
@ -390,10 +382,21 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notify, rpcSub *rpc.Sub
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n := crit.FromBlock.Int64()
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// do historical sync first
|
// do historical sync first
|
||||||
head, err := filterHistLogs(n)
|
head, err := api.histLogs(ctx, notifier, rpcSub, crit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -402,7 +405,7 @@ func (api *FilterAPI) logs(ctx context.Context, notifier notify, rpcSub *rpc.Sub
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// then subscribe from the header
|
// then subscribe from the header
|
||||||
filterLiveLogs()
|
api.liveLogs(ctx, notifier, rpcSub, crit)
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue