mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 21:26:42 +00:00
eth/filters: wip
Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
parent
5035064421
commit
92c980f355
1 changed files with 64 additions and 14 deletions
|
|
@ -273,28 +273,78 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
|
||||||
matchedLogs = make(chan []*types.Log)
|
matchedLogs = make(chan []*types.Log)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
syncHistLogs := func(n int64) error {
|
||||||
|
for {
|
||||||
|
// Get latest head block num
|
||||||
|
head := api.sys.backend.CurrentHeader().Number.Int64()
|
||||||
|
if n >= head {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Do historical sync from n to head
|
||||||
|
f := api.sys.NewRangeFilter(n, head, crit.Addresses, crit.Topics)
|
||||||
|
logChan, errChan := f.rangeLogsAsync(ctx)
|
||||||
|
select {
|
||||||
|
case <-notifier.Closed():
|
||||||
|
return errors.New("connection dropped")
|
||||||
|
case log := <-logChan:
|
||||||
|
notifier.Notify(rpcSub.ID, &log)
|
||||||
|
case err := <-errChan:
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Check if any logs from n to head have been reorged
|
||||||
|
// send the reorged logs
|
||||||
|
|
||||||
|
// Head is stable, move n to current head
|
||||||
|
n = head
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
syncLiveLogs := func() error {
|
||||||
logsSub, err := api.events.SubscribeLogs(ethereum.FilterQuery(crit), matchedLogs)
|
logsSub, err := api.events.SubscribeLogs(ethereum.FilterQuery(crit), matchedLogs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer logsSub.Unsubscribe()
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case logs := <-matchedLogs:
|
case logs := <-matchedLogs:
|
||||||
for _, log := range logs {
|
for _, log := range logs {
|
||||||
|
log := log
|
||||||
notifier.Notify(rpcSub.ID, &log)
|
notifier.Notify(rpcSub.ID, &log)
|
||||||
}
|
}
|
||||||
case <-rpcSub.Err(): // client send an unsubscribe request
|
case <-rpcSub.Err(): // client send an unsubscribe request
|
||||||
|
logsSub.Unsubscribe()
|
||||||
|
return
|
||||||
|
case <-notifier.Closed(): // connection dropped
|
||||||
|
logsSub.Unsubscribe()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if crit.FromBlock != nil {
|
||||||
|
n := crit.FromBlock.Int64()
|
||||||
|
go func() {
|
||||||
|
// do historical sync first
|
||||||
|
if err := syncHistLogs(n); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// subscribe from latest
|
||||||
|
syncLiveLogs()
|
||||||
|
}()
|
||||||
return rpcSub, nil
|
return rpcSub, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err := syncLiveLogs()
|
||||||
|
return rpcSub, err
|
||||||
|
}
|
||||||
|
|
||||||
// FilterCriteria represents a request to create a new filter.
|
// FilterCriteria represents a request to create a new filter.
|
||||||
// Same as ethereum.FilterQuery but with UnmarshalJSON() method.
|
// Same as ethereum.FilterQuery but with UnmarshalJSON() method.
|
||||||
type FilterCriteria ethereum.FilterQuery
|
type FilterCriteria ethereum.FilterQuery
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue