2016-04-14 09:18:24 -07:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-08-30 01:04:59 -07:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
// Package filters implements an ethereum filtering system for block,
|
2015-08-30 01:04:59 -07:00
|
|
|
// transactions and log events.
|
|
|
|
package filters
|
|
|
|
|
|
|
|
import (
|
2017-03-22 10:20:33 -07:00
|
|
|
"context"
|
2016-07-27 08:47:46 -07:00
|
|
|
"errors"
|
2016-02-12 16:40:44 -08:00
|
|
|
"fmt"
|
2015-08-30 01:04:59 -07:00
|
|
|
"sync"
|
2015-10-12 05:04:38 -07:00
|
|
|
"time"
|
2015-08-30 01:04:59 -07:00
|
|
|
|
2018-01-05 02:39:24 -08:00
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
2016-07-27 08:47:46 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-08-30 01:04:59 -07:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-05-07 04:35:06 -07:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2016-07-27 08:47:46 -07:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-08-30 01:04:59 -07:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2018-05-09 01:29:25 -07:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-07-27 08:47:46 -07:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2015-08-30 01:04:59 -07:00
|
|
|
)
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
// Type determines the kind of filter and is used to put the filter in to
|
2016-02-12 16:40:44 -08:00
|
|
|
// the correct bucket when added.
|
2016-07-27 08:47:46 -07:00
|
|
|
type Type byte
|
2016-02-12 16:40:44 -08:00
|
|
|
|
|
|
|
const (
|
2017-01-06 09:44:35 -08:00
|
|
|
// UnknownSubscription indicates an unknown subscription type
|
2016-07-27 08:47:46 -07:00
|
|
|
UnknownSubscription Type = iota
|
|
|
|
// LogsSubscription queries for new or removed (chain reorg) logs
|
|
|
|
LogsSubscription
|
2016-11-28 05:59:06 -08:00
|
|
|
// PendingLogsSubscription queries for logs in pending blocks
|
2016-07-27 08:47:46 -07:00
|
|
|
PendingLogsSubscription
|
2016-11-28 05:59:06 -08:00
|
|
|
// MinedAndPendingLogsSubscription queries for logs in mined and pending blocks.
|
|
|
|
MinedAndPendingLogsSubscription
|
2016-07-27 08:47:46 -07:00
|
|
|
// PendingTransactionsSubscription queries tx hashes for pending
|
|
|
|
// transactions entering the pending state
|
|
|
|
PendingTransactionsSubscription
|
|
|
|
// BlocksSubscription queries hashes for blocks that are imported
|
|
|
|
BlocksSubscription
|
2016-11-28 05:59:06 -08:00
|
|
|
// LastSubscription keeps track of the last index
|
|
|
|
LastIndexSubscription
|
2016-02-12 16:40:44 -08:00
|
|
|
)
|
|
|
|
|
2017-08-18 03:58:36 -07:00
|
|
|
const (
|
|
|
|
|
2018-05-18 01:45:52 -07:00
|
|
|
// txChanSize is the size of channel listening to NewTxsEvent.
|
2017-08-18 03:58:36 -07:00
|
|
|
// The number is referenced from the size of tx pool.
|
|
|
|
txChanSize = 4096
|
|
|
|
// rmLogsChanSize is the size of channel listening to RemovedLogsEvent.
|
|
|
|
rmLogsChanSize = 10
|
|
|
|
// logsChanSize is the size of channel listening to LogsEvent.
|
|
|
|
logsChanSize = 10
|
|
|
|
// chainEvChanSize is the size of channel listening to ChainEvent.
|
|
|
|
chainEvChanSize = 10
|
|
|
|
)
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
var (
|
|
|
|
ErrInvalidSubscriptionID = errors.New("invalid id")
|
|
|
|
)
|
|
|
|
|
|
|
|
type subscription struct {
|
|
|
|
id rpc.ID
|
|
|
|
typ Type
|
|
|
|
created time.Time
|
2018-01-05 02:39:24 -08:00
|
|
|
logsCrit ethereum.FilterQuery
|
2017-01-05 05:03:50 -08:00
|
|
|
logs chan []*types.Log
|
2018-05-10 00:04:45 -07:00
|
|
|
hashes chan []common.Hash
|
2016-07-27 08:47:46 -07:00
|
|
|
headers chan *types.Header
|
|
|
|
installed chan struct{} // closed when the filter is installed
|
|
|
|
err chan error // closed when the filter is uninstalled
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
// EventSystem creates subscriptions, processes events and broadcasts them to the
|
|
|
|
// subscription which match the subscription criteria.
|
|
|
|
type EventSystem struct {
|
|
|
|
mux *event.TypeMux
|
2016-10-13 20:51:29 -07:00
|
|
|
backend Backend
|
|
|
|
lightMode bool
|
|
|
|
lastHead *types.Header
|
2018-05-09 01:29:25 -07:00
|
|
|
|
|
|
|
// Subscriptions
|
2018-05-10 00:04:45 -07:00
|
|
|
txsSub event.Subscription // Subscription for new transaction event
|
2018-05-09 01:29:25 -07:00
|
|
|
logsSub event.Subscription // Subscription for new log event
|
|
|
|
rmLogsSub event.Subscription // Subscription for removed log event
|
|
|
|
chainSub event.Subscription // Subscription for new chain event
|
|
|
|
pendingLogSub *event.TypeMuxSubscription // Subscription for pending log event
|
|
|
|
|
|
|
|
// Channels
|
|
|
|
install chan *subscription // install filter for event notification
|
|
|
|
uninstall chan *subscription // remove filter for event notification
|
2018-05-18 01:45:52 -07:00
|
|
|
txsCh chan core.NewTxsEvent // Channel to receive new transactions event
|
2018-05-09 01:29:25 -07:00
|
|
|
logsCh chan []*types.Log // Channel to receive new log event
|
|
|
|
rmLogsCh chan core.RemovedLogsEvent // Channel to receive removed log event
|
|
|
|
chainCh chan core.ChainEvent // Channel to receive new chain event
|
2016-07-27 08:47:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEventSystem creates a new manager that listens for event on the given mux,
|
|
|
|
// parses and filters them. It uses the all map to retrieve filter changes. The
|
|
|
|
// work loop holds its own index that is used to forward events to filters.
|
|
|
|
//
|
|
|
|
// The returned manager has a loop that needs to be stopped with the Stop function
|
|
|
|
// or by stopping the given mux.
|
2016-10-13 20:51:29 -07:00
|
|
|
func NewEventSystem(mux *event.TypeMux, backend Backend, lightMode bool) *EventSystem {
|
2016-07-27 08:47:46 -07:00
|
|
|
m := &EventSystem{
|
|
|
|
mux: mux,
|
2016-10-13 20:51:29 -07:00
|
|
|
backend: backend,
|
|
|
|
lightMode: lightMode,
|
2016-07-27 08:47:46 -07:00
|
|
|
install: make(chan *subscription),
|
|
|
|
uninstall: make(chan *subscription),
|
2018-05-18 01:45:52 -07:00
|
|
|
txsCh: make(chan core.NewTxsEvent, txChanSize),
|
2018-05-09 01:29:25 -07:00
|
|
|
logsCh: make(chan []*types.Log, logsChanSize),
|
|
|
|
rmLogsCh: make(chan core.RemovedLogsEvent, rmLogsChanSize),
|
|
|
|
chainCh: make(chan core.ChainEvent, chainEvChanSize),
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
2016-07-27 08:47:46 -07:00
|
|
|
|
2018-05-09 01:29:25 -07:00
|
|
|
// Subscribe events
|
2018-05-18 01:45:52 -07:00
|
|
|
m.txsSub = m.backend.SubscribeNewTxsEvent(m.txsCh)
|
2018-05-09 01:29:25 -07:00
|
|
|
m.logsSub = m.backend.SubscribeLogsEvent(m.logsCh)
|
|
|
|
m.rmLogsSub = m.backend.SubscribeRemovedLogsEvent(m.rmLogsCh)
|
|
|
|
m.chainSub = m.backend.SubscribeChainEvent(m.chainCh)
|
|
|
|
// TODO(rjl493456442): use feed to subscribe pending log event
|
|
|
|
m.pendingLogSub = m.mux.Subscribe(core.PendingLogsEvent{})
|
|
|
|
|
|
|
|
// Make sure none of the subscriptions are empty
|
2018-05-10 00:04:45 -07:00
|
|
|
if m.txsSub == nil || m.logsSub == nil || m.rmLogsSub == nil || m.chainSub == nil ||
|
2018-05-09 01:29:25 -07:00
|
|
|
m.pendingLogSub.Closed() {
|
|
|
|
log.Crit("Subscribe for event system failed")
|
|
|
|
}
|
2016-07-27 08:47:46 -07:00
|
|
|
|
2018-05-09 01:29:25 -07:00
|
|
|
go m.eventLoop()
|
2016-07-27 08:47:46 -07:00
|
|
|
return m
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
// Subscription is created when the client registers itself for a particular event.
|
|
|
|
type Subscription struct {
|
|
|
|
ID rpc.ID
|
|
|
|
f *subscription
|
|
|
|
es *EventSystem
|
|
|
|
unsubOnce sync.Once
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
// Err returns a channel that is closed when unsubscribed.
|
|
|
|
func (sub *Subscription) Err() <-chan error {
|
|
|
|
return sub.f.err
|
2016-06-17 00:53:54 -07:00
|
|
|
}
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
// Unsubscribe uninstalls the subscription from the event broadcast loop.
|
|
|
|
func (sub *Subscription) Unsubscribe() {
|
|
|
|
sub.unsubOnce.Do(func() {
|
|
|
|
uninstallLoop:
|
|
|
|
for {
|
|
|
|
// write uninstall request and consume logs/hashes. This prevents
|
|
|
|
// the eventLoop broadcast method to deadlock when writing to the
|
|
|
|
// filter event channel while the subscription loop is waiting for
|
|
|
|
// this method to return (and thus not reading these events).
|
|
|
|
select {
|
|
|
|
case sub.es.uninstall <- sub.f:
|
|
|
|
break uninstallLoop
|
|
|
|
case <-sub.f.logs:
|
|
|
|
case <-sub.f.hashes:
|
|
|
|
case <-sub.f.headers:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for filter to be uninstalled in work loop before returning
|
|
|
|
// this ensures that the manager won't use the event channel which
|
|
|
|
// will probably be closed by the client asap after this method returns.
|
|
|
|
<-sub.Err()
|
|
|
|
})
|
2016-06-17 00:53:54 -07:00
|
|
|
}
|
2016-02-12 16:40:44 -08:00
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
// subscribe installs the subscription in the event broadcast loop.
|
|
|
|
func (es *EventSystem) subscribe(sub *subscription) *Subscription {
|
|
|
|
es.install <- sub
|
|
|
|
<-sub.installed
|
|
|
|
return &Subscription{ID: sub.id, f: sub, es: es}
|
|
|
|
}
|
2016-02-12 16:40:44 -08:00
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
// SubscribeLogs creates a subscription that will write all logs matching the
|
2016-11-28 05:59:06 -08:00
|
|
|
// given criteria to the given logs channel. Default value for the from and to
|
|
|
|
// block is "latest". If the fromBlock > toBlock an error is returned.
|
2018-01-05 02:39:24 -08:00
|
|
|
func (es *EventSystem) SubscribeLogs(crit ethereum.FilterQuery, logs chan []*types.Log) (*Subscription, error) {
|
2016-11-28 05:59:06 -08:00
|
|
|
var from, to rpc.BlockNumber
|
|
|
|
if crit.FromBlock == nil {
|
|
|
|
from = rpc.LatestBlockNumber
|
|
|
|
} else {
|
|
|
|
from = rpc.BlockNumber(crit.FromBlock.Int64())
|
|
|
|
}
|
|
|
|
if crit.ToBlock == nil {
|
|
|
|
to = rpc.LatestBlockNumber
|
|
|
|
} else {
|
|
|
|
to = rpc.BlockNumber(crit.ToBlock.Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
// only interested in pending logs
|
|
|
|
if from == rpc.PendingBlockNumber && to == rpc.PendingBlockNumber {
|
|
|
|
return es.subscribePendingLogs(crit, logs), nil
|
|
|
|
}
|
|
|
|
// only interested in new mined logs
|
|
|
|
if from == rpc.LatestBlockNumber && to == rpc.LatestBlockNumber {
|
|
|
|
return es.subscribeLogs(crit, logs), nil
|
|
|
|
}
|
|
|
|
// only interested in mined logs within a specific block range
|
|
|
|
if from >= 0 && to >= 0 && to >= from {
|
|
|
|
return es.subscribeLogs(crit, logs), nil
|
|
|
|
}
|
|
|
|
// interested in mined logs from a specific block number, new logs and pending logs
|
|
|
|
if from >= rpc.LatestBlockNumber && to == rpc.PendingBlockNumber {
|
|
|
|
return es.subscribeMinedPendingLogs(crit, logs), nil
|
|
|
|
}
|
|
|
|
// interested in logs from a specific block number to new mined blocks
|
|
|
|
if from >= 0 && to == rpc.LatestBlockNumber {
|
|
|
|
return es.subscribeLogs(crit, logs), nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid from and to block combination: from > to")
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscribeMinedPendingLogs creates a subscription that returned mined and
|
|
|
|
// pending logs that match the given criteria.
|
2018-01-05 02:39:24 -08:00
|
|
|
func (es *EventSystem) subscribeMinedPendingLogs(crit ethereum.FilterQuery, logs chan []*types.Log) *Subscription {
|
2016-07-27 08:47:46 -07:00
|
|
|
sub := &subscription{
|
|
|
|
id: rpc.NewID(),
|
2016-11-28 05:59:06 -08:00
|
|
|
typ: MinedAndPendingLogsSubscription,
|
2016-07-27 08:47:46 -07:00
|
|
|
logsCrit: crit,
|
|
|
|
created: time.Now(),
|
|
|
|
logs: logs,
|
2018-05-10 00:04:45 -07:00
|
|
|
hashes: make(chan []common.Hash),
|
2016-07-27 08:47:46 -07:00
|
|
|
headers: make(chan *types.Header),
|
|
|
|
installed: make(chan struct{}),
|
|
|
|
err: make(chan error),
|
2016-02-12 16:40:44 -08:00
|
|
|
}
|
2016-07-27 08:47:46 -07:00
|
|
|
return es.subscribe(sub)
|
|
|
|
}
|
2015-08-30 01:04:59 -07:00
|
|
|
|
2016-11-28 05:59:06 -08:00
|
|
|
// subscribeLogs creates a subscription that will write all logs matching the
|
|
|
|
// given criteria to the given logs channel.
|
2018-01-05 02:39:24 -08:00
|
|
|
func (es *EventSystem) subscribeLogs(crit ethereum.FilterQuery, logs chan []*types.Log) *Subscription {
|
2016-07-27 08:47:46 -07:00
|
|
|
sub := &subscription{
|
|
|
|
id: rpc.NewID(),
|
2016-11-28 05:59:06 -08:00
|
|
|
typ: LogsSubscription,
|
2016-07-27 08:47:46 -07:00
|
|
|
logsCrit: crit,
|
|
|
|
created: time.Now(),
|
|
|
|
logs: logs,
|
2018-05-10 00:04:45 -07:00
|
|
|
hashes: make(chan []common.Hash),
|
2016-07-27 08:47:46 -07:00
|
|
|
headers: make(chan *types.Header),
|
|
|
|
installed: make(chan struct{}),
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
return es.subscribe(sub)
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
|
|
|
|
2016-11-28 05:59:06 -08:00
|
|
|
// subscribePendingLogs creates a subscription that writes transaction hashes for
|
2016-07-27 08:47:46 -07:00
|
|
|
// transactions that enter the transaction pool.
|
2018-01-05 02:39:24 -08:00
|
|
|
func (es *EventSystem) subscribePendingLogs(crit ethereum.FilterQuery, logs chan []*types.Log) *Subscription {
|
2016-07-27 08:47:46 -07:00
|
|
|
sub := &subscription{
|
|
|
|
id: rpc.NewID(),
|
2016-11-28 05:59:06 -08:00
|
|
|
typ: PendingLogsSubscription,
|
|
|
|
logsCrit: crit,
|
2016-07-27 08:47:46 -07:00
|
|
|
created: time.Now(),
|
2016-11-28 05:59:06 -08:00
|
|
|
logs: logs,
|
2018-05-10 00:04:45 -07:00
|
|
|
hashes: make(chan []common.Hash),
|
2016-07-27 08:47:46 -07:00
|
|
|
headers: make(chan *types.Header),
|
|
|
|
installed: make(chan struct{}),
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
return es.subscribe(sub)
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
// SubscribeNewHeads creates a subscription that writes the header of a block that is
|
|
|
|
// imported in the chain.
|
|
|
|
func (es *EventSystem) SubscribeNewHeads(headers chan *types.Header) *Subscription {
|
|
|
|
sub := &subscription{
|
|
|
|
id: rpc.NewID(),
|
|
|
|
typ: BlocksSubscription,
|
|
|
|
created: time.Now(),
|
2017-01-05 05:03:50 -08:00
|
|
|
logs: make(chan []*types.Log),
|
2018-05-10 00:04:45 -07:00
|
|
|
hashes: make(chan []common.Hash),
|
2016-07-27 08:47:46 -07:00
|
|
|
headers: headers,
|
|
|
|
installed: make(chan struct{}),
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
return es.subscribe(sub)
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
|
|
|
|
2018-05-18 01:45:52 -07:00
|
|
|
// SubscribePendingTxs creates a subscription that writes transaction hashes for
|
2016-11-28 05:59:06 -08:00
|
|
|
// transactions that enter the transaction pool.
|
2018-05-18 01:45:52 -07:00
|
|
|
func (es *EventSystem) SubscribePendingTxs(hashes chan []common.Hash) *Subscription {
|
2016-11-28 05:59:06 -08:00
|
|
|
sub := &subscription{
|
|
|
|
id: rpc.NewID(),
|
|
|
|
typ: PendingTransactionsSubscription,
|
|
|
|
created: time.Now(),
|
2017-01-05 05:03:50 -08:00
|
|
|
logs: make(chan []*types.Log),
|
2016-11-28 05:59:06 -08:00
|
|
|
hashes: hashes,
|
|
|
|
headers: make(chan *types.Header),
|
|
|
|
installed: make(chan struct{}),
|
|
|
|
err: make(chan error),
|
|
|
|
}
|
|
|
|
return es.subscribe(sub)
|
|
|
|
}
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
type filterIndex map[Type]map[rpc.ID]*subscription
|
|
|
|
|
|
|
|
// broadcast event to filters that match criteria.
|
2017-08-18 03:58:36 -07:00
|
|
|
func (es *EventSystem) broadcast(filters filterIndex, ev interface{}) {
|
2016-07-27 08:47:46 -07:00
|
|
|
if ev == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-18 03:58:36 -07:00
|
|
|
switch e := ev.(type) {
|
2017-01-05 05:03:50 -08:00
|
|
|
case []*types.Log:
|
2016-07-27 08:47:46 -07:00
|
|
|
if len(e) > 0 {
|
|
|
|
for _, f := range filters[LogsSubscription] {
|
2017-08-18 03:58:36 -07:00
|
|
|
if matchedLogs := filterLogs(e, f.logsCrit.FromBlock, f.logsCrit.ToBlock, f.logsCrit.Addresses, f.logsCrit.Topics); len(matchedLogs) > 0 {
|
|
|
|
f.logs <- matchedLogs
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
2015-10-29 05:28:00 -07:00
|
|
|
}
|
2016-07-27 08:47:46 -07:00
|
|
|
}
|
|
|
|
case core.RemovedLogsEvent:
|
|
|
|
for _, f := range filters[LogsSubscription] {
|
2017-08-18 03:58:36 -07:00
|
|
|
if matchedLogs := filterLogs(e.Logs, f.logsCrit.FromBlock, f.logsCrit.ToBlock, f.logsCrit.Addresses, f.logsCrit.Topics); len(matchedLogs) > 0 {
|
|
|
|
f.logs <- matchedLogs
|
2015-10-29 05:28:00 -07:00
|
|
|
}
|
2016-07-27 08:47:46 -07:00
|
|
|
}
|
2017-08-18 03:58:36 -07:00
|
|
|
case *event.TypeMuxEvent:
|
|
|
|
switch muxe := e.Data.(type) {
|
|
|
|
case core.PendingLogsEvent:
|
|
|
|
for _, f := range filters[PendingLogsSubscription] {
|
|
|
|
if e.Time.After(f.created) {
|
|
|
|
if matchedLogs := filterLogs(muxe.Logs, nil, f.logsCrit.ToBlock, f.logsCrit.Addresses, f.logsCrit.Topics); len(matchedLogs) > 0 {
|
|
|
|
f.logs <- matchedLogs
|
|
|
|
}
|
2016-01-05 05:55:28 -08:00
|
|
|
}
|
|
|
|
}
|
2016-07-27 08:47:46 -07:00
|
|
|
}
|
2018-05-18 01:45:52 -07:00
|
|
|
case core.NewTxsEvent:
|
|
|
|
hashes := make([]common.Hash, 0, len(e.Txs))
|
2018-05-10 00:04:45 -07:00
|
|
|
for _, tx := range e.Txs {
|
|
|
|
hashes = append(hashes, tx.Hash())
|
|
|
|
}
|
2016-07-27 08:47:46 -07:00
|
|
|
for _, f := range filters[PendingTransactionsSubscription] {
|
2018-05-10 00:04:45 -07:00
|
|
|
f.hashes <- hashes
|
2016-07-27 08:47:46 -07:00
|
|
|
}
|
|
|
|
case core.ChainEvent:
|
|
|
|
for _, f := range filters[BlocksSubscription] {
|
2017-08-18 03:58:36 -07:00
|
|
|
f.headers <- e.Block.Header()
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
2016-10-13 20:51:29 -07:00
|
|
|
if es.lightMode && len(filters[LogsSubscription]) > 0 {
|
|
|
|
es.lightFilterNewHead(e.Block.Header(), func(header *types.Header, remove bool) {
|
|
|
|
for _, f := range filters[LogsSubscription] {
|
2017-08-18 03:58:36 -07:00
|
|
|
if matchedLogs := es.lightFilterLogs(header, f.logsCrit.Addresses, f.logsCrit.Topics, remove); len(matchedLogs) > 0 {
|
|
|
|
f.logs <- matchedLogs
|
2016-10-13 20:51:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *EventSystem) lightFilterNewHead(newHeader *types.Header, callBack func(*types.Header, bool)) {
|
|
|
|
oldh := es.lastHead
|
|
|
|
es.lastHead = newHeader
|
|
|
|
if oldh == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newh := newHeader
|
|
|
|
// find common ancestor, create list of rolled back and new block hashes
|
|
|
|
var oldHeaders, newHeaders []*types.Header
|
|
|
|
for oldh.Hash() != newh.Hash() {
|
2016-11-08 16:20:49 -08:00
|
|
|
if oldh.Number.Uint64() >= newh.Number.Uint64() {
|
2016-10-13 20:51:29 -07:00
|
|
|
oldHeaders = append(oldHeaders, oldh)
|
2018-05-07 04:35:06 -07:00
|
|
|
oldh = rawdb.ReadHeader(es.backend.ChainDb(), oldh.ParentHash, oldh.Number.Uint64()-1)
|
2016-10-13 20:51:29 -07:00
|
|
|
}
|
2016-11-08 16:20:49 -08:00
|
|
|
if oldh.Number.Uint64() < newh.Number.Uint64() {
|
2016-10-13 20:51:29 -07:00
|
|
|
newHeaders = append(newHeaders, newh)
|
2018-05-07 04:35:06 -07:00
|
|
|
newh = rawdb.ReadHeader(es.backend.ChainDb(), newh.ParentHash, newh.Number.Uint64()-1)
|
2016-10-13 20:51:29 -07:00
|
|
|
if newh == nil {
|
|
|
|
// happens when CHT syncing, nothing to do
|
|
|
|
newh = oldh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// roll back old blocks
|
|
|
|
for _, h := range oldHeaders {
|
|
|
|
callBack(h, true)
|
|
|
|
}
|
|
|
|
// check new blocks (array is in reverse order)
|
|
|
|
for i := len(newHeaders) - 1; i >= 0; i-- {
|
|
|
|
callBack(newHeaders[i], false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter logs of a single header in light client mode
|
2017-01-05 05:03:50 -08:00
|
|
|
func (es *EventSystem) lightFilterLogs(header *types.Header, addresses []common.Address, topics [][]common.Hash, remove bool) []*types.Log {
|
2016-10-13 20:51:29 -07:00
|
|
|
if bloomFilter(header.Bloom, addresses, topics) {
|
|
|
|
// Get the logs of the block
|
2017-03-22 10:20:33 -07:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
|
|
defer cancel()
|
2018-02-22 02:48:14 -08:00
|
|
|
logsList, err := es.backend.GetLogs(ctx, header.Hash())
|
2016-10-13 20:51:29 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-05 05:03:50 -08:00
|
|
|
var unfiltered []*types.Log
|
2018-02-22 02:48:14 -08:00
|
|
|
for _, logs := range logsList {
|
|
|
|
for _, log := range logs {
|
2016-12-04 10:07:24 -08:00
|
|
|
logcopy := *log
|
|
|
|
logcopy.Removed = remove
|
|
|
|
unfiltered = append(unfiltered, &logcopy)
|
2016-10-13 20:51:29 -07:00
|
|
|
}
|
|
|
|
}
|
2016-11-28 05:59:06 -08:00
|
|
|
logs := filterLogs(unfiltered, nil, nil, addresses, topics)
|
2018-02-22 02:48:14 -08:00
|
|
|
if len(logs) > 0 && logs[0].TxHash == (common.Hash{}) {
|
|
|
|
// We have matching but non-derived logs
|
|
|
|
receipts, err := es.backend.GetReceipts(ctx, header.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
unfiltered = unfiltered[:0]
|
|
|
|
for _, receipt := range receipts {
|
|
|
|
for _, log := range receipt.Logs {
|
|
|
|
logcopy := *log
|
|
|
|
logcopy.Removed = remove
|
|
|
|
unfiltered = append(unfiltered, &logcopy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logs = filterLogs(unfiltered, nil, nil, addresses, topics)
|
|
|
|
}
|
2016-10-13 20:51:29 -07:00
|
|
|
return logs
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
2016-10-13 20:51:29 -07:00
|
|
|
return nil
|
2015-08-30 01:04:59 -07:00
|
|
|
}
|
2016-07-27 08:47:46 -07:00
|
|
|
|
|
|
|
// eventLoop (un)installs filters and processes mux events.
|
|
|
|
func (es *EventSystem) eventLoop() {
|
2018-05-09 01:29:25 -07:00
|
|
|
// Ensure all subscriptions get cleaned up
|
|
|
|
defer func() {
|
|
|
|
es.pendingLogSub.Unsubscribe()
|
2018-05-10 00:04:45 -07:00
|
|
|
es.txsSub.Unsubscribe()
|
2018-05-09 01:29:25 -07:00
|
|
|
es.logsSub.Unsubscribe()
|
|
|
|
es.rmLogsSub.Unsubscribe()
|
|
|
|
es.chainSub.Unsubscribe()
|
|
|
|
}()
|
|
|
|
|
|
|
|
index := make(filterIndex)
|
2016-11-28 05:59:06 -08:00
|
|
|
for i := UnknownSubscription; i < LastIndexSubscription; i++ {
|
|
|
|
index[i] = make(map[rpc.ID]*subscription)
|
|
|
|
}
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
for {
|
|
|
|
select {
|
2017-08-18 03:58:36 -07:00
|
|
|
// Handle subscribed events
|
2018-05-10 00:04:45 -07:00
|
|
|
case ev := <-es.txsCh:
|
2017-08-18 03:58:36 -07:00
|
|
|
es.broadcast(index, ev)
|
2018-05-09 01:29:25 -07:00
|
|
|
case ev := <-es.logsCh:
|
2017-08-18 03:58:36 -07:00
|
|
|
es.broadcast(index, ev)
|
2018-05-09 01:29:25 -07:00
|
|
|
case ev := <-es.rmLogsCh:
|
2017-08-18 03:58:36 -07:00
|
|
|
es.broadcast(index, ev)
|
2018-05-09 01:29:25 -07:00
|
|
|
case ev := <-es.chainCh:
|
|
|
|
es.broadcast(index, ev)
|
|
|
|
case ev, active := <-es.pendingLogSub.Chan():
|
|
|
|
if !active { // system stopped
|
|
|
|
return
|
|
|
|
}
|
2017-08-18 03:58:36 -07:00
|
|
|
es.broadcast(index, ev)
|
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
case f := <-es.install:
|
2016-11-28 05:59:06 -08:00
|
|
|
if f.typ == MinedAndPendingLogsSubscription {
|
|
|
|
// the type are logs and pending logs subscriptions
|
|
|
|
index[LogsSubscription][f.id] = f
|
|
|
|
index[PendingLogsSubscription][f.id] = f
|
|
|
|
} else {
|
|
|
|
index[f.typ][f.id] = f
|
2016-07-27 08:47:46 -07:00
|
|
|
}
|
|
|
|
close(f.installed)
|
2018-05-09 01:29:25 -07:00
|
|
|
|
2016-07-27 08:47:46 -07:00
|
|
|
case f := <-es.uninstall:
|
2016-11-28 05:59:06 -08:00
|
|
|
if f.typ == MinedAndPendingLogsSubscription {
|
|
|
|
// the type are logs and pending logs subscriptions
|
|
|
|
delete(index[LogsSubscription], f.id)
|
|
|
|
delete(index[PendingLogsSubscription], f.id)
|
|
|
|
} else {
|
|
|
|
delete(index[f.typ], f.id)
|
|
|
|
}
|
2016-07-27 08:47:46 -07:00
|
|
|
close(f.err)
|
2017-08-18 03:58:36 -07:00
|
|
|
|
|
|
|
// System stopped
|
2018-05-10 00:04:45 -07:00
|
|
|
case <-es.txsSub.Err():
|
2017-08-18 03:58:36 -07:00
|
|
|
return
|
2018-05-09 01:29:25 -07:00
|
|
|
case <-es.logsSub.Err():
|
2017-08-18 03:58:36 -07:00
|
|
|
return
|
2018-05-09 01:29:25 -07:00
|
|
|
case <-es.rmLogsSub.Err():
|
2017-08-18 03:58:36 -07:00
|
|
|
return
|
2018-05-09 01:29:25 -07:00
|
|
|
case <-es.chainSub.Err():
|
2017-08-18 03:58:36 -07:00
|
|
|
return
|
2016-07-27 08:47:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|