Skip to content

Commit f7a7199

Browse files
committed
core, event/filter, xeth: refactored filter system
Moved the filtering system from `event` to `eth/filters` package and removed the `core.Filter` object. The `filters.Filter` object now requires a `common.Database` rather than a `eth.Backend` and invokes the `core.GetBlockByX` directly rather than thru a "manager".
1 parent 0d78f96 commit f7a7199

4 files changed

Lines changed: 162 additions & 158 deletions

File tree

core/filter.go renamed to eth/filters/filter.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package core
17+
package filters
1818

1919
import (
2020
"math"
2121

2222
"github.com/ethereum/go-ethereum/common"
23+
"github.com/ethereum/go-ethereum/core"
2324
"github.com/ethereum/go-ethereum/core/state"
2425
"github.com/ethereum/go-ethereum/core/types"
25-
"github.com/ethereum/go-ethereum/logger"
26-
"github.com/ethereum/go-ethereum/logger/glog"
2726
)
2827

2928
type AccountChange struct {
@@ -32,7 +31,7 @@ type AccountChange struct {
3231

3332
// Filtering interface
3433
type Filter struct {
35-
eth Backend
34+
db common.Database
3635
earliest int64
3736
latest int64
3837
skip int
@@ -47,8 +46,8 @@ type Filter struct {
4746

4847
// Create a new filter which uses a bloom filter on blocks to figure out whether a particular block
4948
// is interesting or not.
50-
func NewFilter(eth Backend) *Filter {
51-
return &Filter{eth: eth}
49+
func New(db common.Database) *Filter {
50+
return &Filter{db: db}
5251
}
5352

5453
// Set the earliest and latest block for filtering.
@@ -80,7 +79,7 @@ func (self *Filter) SetSkip(skip int) {
8079

8180
// Run filters logs with the current parameters set
8281
func (self *Filter) Find() state.Logs {
83-
earliestBlock := self.eth.ChainManager().CurrentBlock()
82+
earliestBlock := core.GetCurrentBlock(self.db)
8483
var earliestBlockNo uint64 = uint64(self.earliest)
8584
if self.earliest == -1 {
8685
earliestBlockNo = earliestBlock.NumberU64()
@@ -92,7 +91,7 @@ func (self *Filter) Find() state.Logs {
9291

9392
var (
9493
logs state.Logs
95-
block = self.eth.ChainManager().GetBlockByNumber(latestBlockNo)
94+
block = core.GetBlockByNumber(self.db, latestBlockNo)
9695
)
9796

9897
done:
@@ -111,17 +110,17 @@ done:
111110
// current parameters
112111
if self.bloomFilter(block) {
113112
// Get the logs of the block
114-
unfiltered, err := self.eth.BlockProcessor().GetLogs(block)
115-
if err != nil {
116-
glog.V(logger.Warn).Infoln("err: filter get logs ", err)
117-
118-
break
113+
var (
114+
receipts = core.GetBlockReceipts(self.db, block.Hash())
115+
unfiltered state.Logs
116+
)
117+
for _, receipt := range receipts {
118+
unfiltered = append(unfiltered, receipt.Logs()...)
119119
}
120-
121120
logs = append(logs, self.FilterLogs(unfiltered)...)
122121
}
123122

124-
block = self.eth.ChainManager().GetBlock(block.ParentHash())
123+
block = core.GetBlockByHash(self.db, block.ParentHash())
125124
}
126125

127126
skip := int(math.Min(float64(len(logs)), float64(self.skip)))

‎eth/filters/filter_system.go‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

‎event/filter/eth_filter.go‎

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)