|
| 1 | +// Copyright 2014 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +// package filters implements an ethereum filtering system for block, |
| 18 | +// transactions and log events. |
| 19 | +package filters |
| 20 | + |
| 21 | +import ( |
| 22 | + "sync" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/core" |
| 25 | + "github.com/ethereum/go-ethereum/core/state" |
| 26 | + "github.com/ethereum/go-ethereum/event" |
| 27 | +) |
| 28 | + |
| 29 | +// FilterSystem manages filters that filter specific events such as |
| 30 | +// block, transaction and log events. The Filtering system can be used to listen |
| 31 | +// for specific LOG events fires by the EVM (Ethereum Virtual Machine). |
| 32 | +type FilterSystem struct { |
| 33 | + eventMux *event.TypeMux |
| 34 | + |
| 35 | + filterMu sync.RWMutex |
| 36 | + filterId int |
| 37 | + filters map[int]*Filter |
| 38 | + |
| 39 | + quit chan struct{} |
| 40 | +} |
| 41 | + |
| 42 | +// NewFilterSystem returns a newly allocated filter manager |
| 43 | +func NewFilterSystem(mux *event.TypeMux) *FilterSystem { |
| 44 | + fs := &FilterSystem{ |
| 45 | + eventMux: mux, |
| 46 | + filters: make(map[int]*Filter), |
| 47 | + } |
| 48 | + go fs.filterLoop() |
| 49 | + return fs |
| 50 | +} |
| 51 | + |
| 52 | +// Stop quits the filter loop required for polling events |
| 53 | +func (fs *FilterSystem) Stop() { |
| 54 | + close(fs.quit) |
| 55 | +} |
| 56 | + |
| 57 | +// Add adds a filter to the filter manager |
| 58 | +func (fs *FilterSystem) Add(filter *Filter) (id int) { |
| 59 | + fs.filterMu.Lock() |
| 60 | + defer fs.filterMu.Unlock() |
| 61 | + id = fs.filterId |
| 62 | + fs.filters[id] = filter |
| 63 | + fs.filterId++ |
| 64 | + |
| 65 | + return id |
| 66 | +} |
| 67 | + |
| 68 | +// Remove removes a filter by filter id |
| 69 | +func (fs *FilterSystem) Remove(id int) { |
| 70 | + fs.filterMu.Lock() |
| 71 | + defer fs.filterMu.Unlock() |
| 72 | + if _, ok := fs.filters[id]; ok { |
| 73 | + delete(fs.filters, id) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Get retrieves a filter installed using Add The filter may not be modified. |
| 78 | +func (fs *FilterSystem) Get(id int) *Filter { |
| 79 | + fs.filterMu.RLock() |
| 80 | + defer fs.filterMu.RUnlock() |
| 81 | + return fs.filters[id] |
| 82 | +} |
| 83 | + |
| 84 | +// filterLoop waits for specific events from ethereum and fires their handlers |
| 85 | +// when the filter matches the requirements. |
| 86 | +func (fs *FilterSystem) filterLoop() { |
| 87 | + // Subscribe to events |
| 88 | + events := fs.eventMux.Subscribe( |
| 89 | + //core.PendingBlockEvent{}, |
| 90 | + core.ChainEvent{}, |
| 91 | + core.TxPreEvent{}, |
| 92 | + state.Logs(nil)) |
| 93 | + |
| 94 | +out: |
| 95 | + for { |
| 96 | + select { |
| 97 | + case <-fs.quit: |
| 98 | + break out |
| 99 | + case event := <-events.Chan(): |
| 100 | + switch event := event.(type) { |
| 101 | + case core.ChainEvent: |
| 102 | + fs.filterMu.RLock() |
| 103 | + for _, filter := range fs.filters { |
| 104 | + if filter.BlockCallback != nil { |
| 105 | + filter.BlockCallback(event.Block, event.Logs) |
| 106 | + } |
| 107 | + } |
| 108 | + fs.filterMu.RUnlock() |
| 109 | + |
| 110 | + case core.TxPreEvent: |
| 111 | + fs.filterMu.RLock() |
| 112 | + for _, filter := range fs.filters { |
| 113 | + if filter.TransactionCallback != nil { |
| 114 | + filter.TransactionCallback(event.Tx) |
| 115 | + } |
| 116 | + } |
| 117 | + fs.filterMu.RUnlock() |
| 118 | + |
| 119 | + case state.Logs: |
| 120 | + fs.filterMu.RLock() |
| 121 | + for _, filter := range fs.filters { |
| 122 | + if filter.LogsCallback != nil { |
| 123 | + msgs := filter.FilterLogs(event) |
| 124 | + if len(msgs) > 0 { |
| 125 | + filter.LogsCallback(msgs) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + fs.filterMu.RUnlock() |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments