Skip to content

Commit bb57f1f

Browse files
authored
Merge pull request #15489 from karalabe/bloombits-shifted-start
core/bloombits: handle non 8-bit boundary section matches
2 parents bce5d83 + 4630141 commit bb57f1f

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

core/bloombits/matcher.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,12 @@ func (m *Matcher) Start(ctx context.Context, begin, end uint64, results chan uin
192192
}
193193
// Iterate over all the blocks in the section and return the matching ones
194194
for i := first; i <= last; i++ {
195-
// Skip the entire byte if no matches are found inside
195+
// Skip the entire byte if no matches are found inside (and we're processing an entire byte!)
196196
next := res.bitset[(i-sectionStart)/8]
197197
if next == 0 {
198-
i += 7
198+
if i%8 == 0 {
199+
i += 7
200+
}
199201
continue
200202
}
201203
// Some bit it set, do the actual submatching

core/bloombits/matcher_test.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,48 @@ func TestMatcherWildcards(t *testing.T) {
5656

5757
// Tests the matcher pipeline on a single continuous workflow without interrupts.
5858
func TestMatcherContinuous(t *testing.T) {
59-
testMatcherDiffBatches(t, [][]bloomIndexes{{{10, 20, 30}}}, 100000, false, 75)
60-
testMatcherDiffBatches(t, [][]bloomIndexes{{{32, 3125, 100}}, {{40, 50, 10}}}, 100000, false, 81)
61-
testMatcherDiffBatches(t, [][]bloomIndexes{{{4, 8, 11}, {7, 8, 17}}, {{9, 9, 12}, {15, 20, 13}}, {{18, 15, 15}, {12, 10, 4}}}, 10000, false, 36)
59+
testMatcherDiffBatches(t, [][]bloomIndexes{{{10, 20, 30}}}, 0, 100000, false, 75)
60+
testMatcherDiffBatches(t, [][]bloomIndexes{{{32, 3125, 100}}, {{40, 50, 10}}}, 0, 100000, false, 81)
61+
testMatcherDiffBatches(t, [][]bloomIndexes{{{4, 8, 11}, {7, 8, 17}}, {{9, 9, 12}, {15, 20, 13}}, {{18, 15, 15}, {12, 10, 4}}}, 0, 10000, false, 36)
6262
}
6363

6464
// Tests the matcher pipeline on a constantly interrupted and resumed work pattern
6565
// with the aim of ensuring data items are requested only once.
6666
func TestMatcherIntermittent(t *testing.T) {
67-
testMatcherDiffBatches(t, [][]bloomIndexes{{{10, 20, 30}}}, 100000, true, 75)
68-
testMatcherDiffBatches(t, [][]bloomIndexes{{{32, 3125, 100}}, {{40, 50, 10}}}, 100000, true, 81)
69-
testMatcherDiffBatches(t, [][]bloomIndexes{{{4, 8, 11}, {7, 8, 17}}, {{9, 9, 12}, {15, 20, 13}}, {{18, 15, 15}, {12, 10, 4}}}, 10000, true, 36)
67+
testMatcherDiffBatches(t, [][]bloomIndexes{{{10, 20, 30}}}, 0, 100000, true, 75)
68+
testMatcherDiffBatches(t, [][]bloomIndexes{{{32, 3125, 100}}, {{40, 50, 10}}}, 0, 100000, true, 81)
69+
testMatcherDiffBatches(t, [][]bloomIndexes{{{4, 8, 11}, {7, 8, 17}}, {{9, 9, 12}, {15, 20, 13}}, {{18, 15, 15}, {12, 10, 4}}}, 0, 10000, true, 36)
7070
}
7171

7272
// Tests the matcher pipeline on random input to hopefully catch anomalies.
7373
func TestMatcherRandom(t *testing.T) {
7474
for i := 0; i < 10; i++ {
75-
testMatcherBothModes(t, makeRandomIndexes([]int{1}, 50), 10000, 0)
76-
testMatcherBothModes(t, makeRandomIndexes([]int{3}, 50), 10000, 0)
77-
testMatcherBothModes(t, makeRandomIndexes([]int{2, 2, 2}, 20), 10000, 0)
78-
testMatcherBothModes(t, makeRandomIndexes([]int{5, 5, 5}, 50), 10000, 0)
79-
testMatcherBothModes(t, makeRandomIndexes([]int{4, 4, 4}, 20), 10000, 0)
75+
testMatcherBothModes(t, makeRandomIndexes([]int{1}, 50), 0, 10000, 0)
76+
testMatcherBothModes(t, makeRandomIndexes([]int{3}, 50), 0, 10000, 0)
77+
testMatcherBothModes(t, makeRandomIndexes([]int{2, 2, 2}, 20), 0, 10000, 0)
78+
testMatcherBothModes(t, makeRandomIndexes([]int{5, 5, 5}, 50), 0, 10000, 0)
79+
testMatcherBothModes(t, makeRandomIndexes([]int{4, 4, 4}, 20), 0, 10000, 0)
8080
}
8181
}
8282

83+
// Tests that the matcher can properly find matches if the starting block is
84+
// shifter from a multiple of 8. This is needed to cover an optimisation with
85+
// bitset matching https://github.com/ethereum/go-ethereum/issues/15309.
86+
func TestMatcherShifted(t *testing.T) {
87+
// Block 0 always matches in the tests, skip ahead of first 8 blocks with the
88+
// start to get a potential zero byte in the matcher bitset.
89+
90+
// To keep the second bitset byte zero, the filter must only match for the first
91+
// time in block 16, so doing an all-16 bit filter should suffice.
92+
93+
// To keep the starting block non divisible by 8, block number 9 is the first
94+
// that would introduce a shift and not match block 0.
95+
testMatcherBothModes(t, [][]bloomIndexes{{{16, 16, 16}}}, 9, 64, 0)
96+
}
97+
8398
// Tests that matching on everything doesn't crash (special case internally).
8499
func TestWildcardMatcher(t *testing.T) {
85-
testMatcherBothModes(t, nil, 10000, 0)
100+
testMatcherBothModes(t, nil, 0, 10000, 0)
86101
}
87102

88103
// makeRandomIndexes generates a random filter system, composed on multiple filter
@@ -104,9 +119,9 @@ func makeRandomIndexes(lengths []int, max int) [][]bloomIndexes {
104119
// testMatcherDiffBatches runs the given matches test in single-delivery and also
105120
// in batches delivery mode, verifying that all kinds of deliveries are handled
106121
// correctly withn.
107-
func testMatcherDiffBatches(t *testing.T, filter [][]bloomIndexes, blocks uint64, intermittent bool, retrievals uint32) {
108-
singleton := testMatcher(t, filter, blocks, intermittent, retrievals, 1)
109-
batched := testMatcher(t, filter, blocks, intermittent, retrievals, 16)
122+
func testMatcherDiffBatches(t *testing.T, filter [][]bloomIndexes, start, blocks uint64, intermittent bool, retrievals uint32) {
123+
singleton := testMatcher(t, filter, start, blocks, intermittent, retrievals, 1)
124+
batched := testMatcher(t, filter, start, blocks, intermittent, retrievals, 16)
110125

111126
if singleton != batched {
112127
t.Errorf("filter = %v blocks = %v intermittent = %v: request count mismatch, %v in signleton vs. %v in batched mode", filter, blocks, intermittent, singleton, batched)
@@ -115,9 +130,9 @@ func testMatcherDiffBatches(t *testing.T, filter [][]bloomIndexes, blocks uint64
115130

116131
// testMatcherBothModes runs the given matcher test in both continuous as well as
117132
// in intermittent mode, verifying that the request counts match each other.
118-
func testMatcherBothModes(t *testing.T, filter [][]bloomIndexes, blocks uint64, retrievals uint32) {
119-
continuous := testMatcher(t, filter, blocks, false, retrievals, 16)
120-
intermittent := testMatcher(t, filter, blocks, true, retrievals, 16)
133+
func testMatcherBothModes(t *testing.T, filter [][]bloomIndexes, start, blocks uint64, retrievals uint32) {
134+
continuous := testMatcher(t, filter, start, blocks, false, retrievals, 16)
135+
intermittent := testMatcher(t, filter, start, blocks, true, retrievals, 16)
121136

122137
if continuous != intermittent {
123138
t.Errorf("filter = %v blocks = %v: request count mismatch, %v in continuous vs. %v in intermittent mode", filter, blocks, continuous, intermittent)
@@ -126,7 +141,7 @@ func testMatcherBothModes(t *testing.T, filter [][]bloomIndexes, blocks uint64,
126141

127142
// testMatcher is a generic tester to run the given matcher test and return the
128143
// number of requests made for cross validation between different modes.
129-
func testMatcher(t *testing.T, filter [][]bloomIndexes, blocks uint64, intermittent bool, retrievals uint32, maxReqCount int) uint32 {
144+
func testMatcher(t *testing.T, filter [][]bloomIndexes, start, blocks uint64, intermittent bool, retrievals uint32, maxReqCount int) uint32 {
130145
// Create a new matcher an simulate our explicit random bitsets
131146
matcher := NewMatcher(testSectionSize, nil)
132147
matcher.filters = filter
@@ -145,14 +160,14 @@ func testMatcher(t *testing.T, filter [][]bloomIndexes, blocks uint64, intermitt
145160
quit := make(chan struct{})
146161
matches := make(chan uint64, 16)
147162

148-
session, err := matcher.Start(context.Background(), 0, blocks-1, matches)
163+
session, err := matcher.Start(context.Background(), start, blocks-1, matches)
149164
if err != nil {
150165
t.Fatalf("failed to stat matcher session: %v", err)
151166
}
152167
startRetrievers(session, quit, &requested, maxReqCount)
153168

154169
// Iterate over all the blocks and verify that the pipeline produces the correct matches
155-
for i := uint64(0); i < blocks; i++ {
170+
for i := start; i < blocks; i++ {
156171
if expMatch3(filter, i) {
157172
match, ok := <-matches
158173
if !ok {

eth/filters/filter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func (f *Filter) indexedLogs(ctx context.Context, end uint64) ([]*types.Log, err
158158
return logs, err
159159
}
160160
f.begin = int64(number) + 1
161+
161162
// Retrieve the suggested block and pull any truly matching logs
162163
header, err := f.backend.HeaderByNumber(ctx, rpc.BlockNumber(number))
163164
if header == nil || err != nil {

0 commit comments

Comments
 (0)