Skip to content

Commit 451ffdb

Browse files
zsfelfoldikaralabe
authored andcommitted
core/bloombits: use general filters instead of addresses and topics
1 parent 6ff2c02 commit 451ffdb

3 files changed

Lines changed: 33 additions & 46 deletions

File tree

‎core/bloombits/matcher.go‎

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"sync/atomic"
2525
"time"
2626

27-
"github.com/ethereum/go-ethereum/common"
2827
"github.com/ethereum/go-ethereum/common/bitutil"
2928
"github.com/ethereum/go-ethereum/crypto"
3029
)
@@ -68,8 +67,7 @@ type Retrieval struct {
6867
type Matcher struct {
6968
sectionSize uint64 // Size of the data batches to filter on
7069

71-
addresses []bloomIndexes // Addresses the system is filtering for
72-
topics [][]bloomIndexes // Topics the system is filtering for
70+
filters [][]bloomIndexes // Filter the system is matching for
7371
schedulers map[uint]*scheduler // Retrieval schedulers for loading bloom bits
7472

7573
retrievers chan chan uint // Retriever processes waiting for bit allocations
@@ -82,7 +80,8 @@ type Matcher struct {
8280

8381
// NewMatcher creates a new pipeline for retrieving bloom bit streams and doing
8482
// address and topic filtering on them.
85-
func NewMatcher(sectionSize uint64, addresses []common.Address, topics [][]common.Hash) *Matcher {
83+
func NewMatcher(sectionSize uint64, filters [][][]byte) *Matcher {
84+
// Create the matcher instance
8685
m := &Matcher{
8786
sectionSize: sectionSize,
8887
schedulers: make(map[uint]*scheduler),
@@ -91,48 +90,25 @@ func NewMatcher(sectionSize uint64, addresses []common.Address, topics [][]commo
9190
retrievals: make(chan chan *Retrieval),
9291
deliveries: make(chan *Retrieval),
9392
}
94-
m.setAddresses(addresses)
95-
m.setTopics(topics)
96-
return m
97-
}
93+
// Calculate the bloom bit indexes for the groups we're interested in
94+
m.filters = nil
9895

99-
// setAddresses configures the matcher to only return logs that are generated
100-
// from addresses that are included in the given list.
101-
func (m *Matcher) setAddresses(addresses []common.Address) {
102-
// Calculate the bloom bit indexes for the addresses we're interested in
103-
m.addresses = make([]bloomIndexes, len(addresses))
104-
for i, address := range addresses {
105-
m.addresses[i] = calcBloomIndexes(address.Bytes())
106-
}
107-
// For every bit, create a scheduler to load/download the bit vectors
108-
for _, bloomIndexList := range m.addresses {
109-
for _, bloomIndex := range bloomIndexList {
110-
m.addScheduler(bloomIndex)
96+
for _, filter := range filters {
97+
bloomBits := make([]bloomIndexes, len(filter))
98+
for i, clause := range filter {
99+
bloomBits[i] = calcBloomIndexes(clause)
111100
}
112-
}
113-
}
114-
115-
// setTopics configures the matcher to only return logs that have topics matching
116-
// the given list.
117-
func (m *Matcher) setTopics(topicsList [][]common.Hash) {
118-
// Calculate the bloom bit indexes for the topics we're interested in
119-
m.topics = nil
120-
121-
for _, topics := range topicsList {
122-
bloomBits := make([]bloomIndexes, len(topics))
123-
for i, topic := range topics {
124-
bloomBits[i] = calcBloomIndexes(topic.Bytes())
125-
}
126-
m.topics = append(m.topics, bloomBits)
101+
m.filters = append(m.filters, bloomBits)
127102
}
128103
// For every bit, create a scheduler to load/download the bit vectors
129-
for _, bloomIndexLists := range m.topics {
104+
for _, bloomIndexLists := range m.filters {
130105
for _, bloomIndexList := range bloomIndexLists {
131106
for _, bloomIndex := range bloomIndexList {
132107
m.addScheduler(bloomIndex)
133108
}
134109
}
135110
}
111+
return m
136112
}
137113

138114
// addScheduler adds a bit stream retrieval scheduler for the given bit index if
@@ -250,14 +226,10 @@ func (m *Matcher) run(begin, end uint64, buffer int, session *MatcherSession) ch
250226
}
251227
}()
252228
// Assemble the daisy-chained filtering pipeline
253-
blooms := m.topics
254-
if len(m.addresses) > 0 {
255-
blooms = append([][]bloomIndexes{m.addresses}, blooms...)
256-
}
257229
next := source
258230
dist := make(chan *request, buffer)
259231

260-
for _, bloom := range blooms {
232+
for _, bloom := range m.filters {
261233
next = m.subMatch(next, dist, bloom, session)
262234
}
263235
// Start the request distribution

‎core/bloombits/matcher_test.go‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ func testMatcherBothModes(t *testing.T, filter [][]bloomIndexes, blocks uint64,
9494
// number of requests made for cross validation between different modes.
9595
func testMatcher(t *testing.T, filter [][]bloomIndexes, blocks uint64, intermittent bool, retrievals uint32, maxReqCount int) uint32 {
9696
// Create a new matcher an simulate our explicit random bitsets
97-
matcher := NewMatcher(testSectionSize, nil, nil)
98-
99-
matcher.addresses = filter[0]
100-
matcher.topics = filter[1:]
97+
matcher := NewMatcher(testSectionSize, nil)
98+
matcher.filters = filter
10199

102100
for _, rule := range filter {
103101
for _, topic := range rule {

‎eth/filters/filter.go‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ type Filter struct {
6060
// New creates a new filter which uses a bloom filter on blocks to figure out whether
6161
// a particular block is interesting or not.
6262
func New(backend Backend, begin, end int64, addresses []common.Address, topics [][]common.Hash) *Filter {
63+
// Flatten the address and topic filter clauses into a single filter system
64+
var filters [][][]byte
65+
if len(addresses) > 0 {
66+
filter := make([][]byte, len(addresses))
67+
for i, address := range addresses {
68+
filter[i] = address.Bytes()
69+
}
70+
filters = append(filters, filter)
71+
}
72+
for _, topicList := range topics {
73+
filter := make([][]byte, len(topicList))
74+
for i, topic := range topicList {
75+
filter[i] = topic.Bytes()
76+
}
77+
filters = append(filters, filter)
78+
}
79+
// Assemble and return the filter
6380
size, _ := backend.BloomStatus()
6481

6582
return &Filter{
@@ -69,7 +86,7 @@ func New(backend Backend, begin, end int64, addresses []common.Address, topics [
6986
addresses: addresses,
7087
topics: topics,
7188
db: backend.ChainDb(),
72-
matcher: bloombits.NewMatcher(size, addresses, topics),
89+
matcher: bloombits.NewMatcher(size, filters),
7390
}
7491
}
7592

0 commit comments

Comments
 (0)