2017-08-18 22:52:20 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// 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/>.
|
2017-08-29 14:13:11 +03:00
|
|
|
|
2017-08-18 22:52:20 +03:00
|
|
|
package bloombits
|
|
|
|
|
|
|
|
import (
|
2017-10-24 16:19:09 +03:00
|
|
|
"context"
|
2017-08-18 22:52:20 +03:00
|
|
|
"math/rand"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2017-09-27 13:14:52 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-08-18 22:52:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const testSectionSize = 4096
|
|
|
|
|
2017-09-27 13:14:52 +03:00
|
|
|
// Tests that wildcard filter rules (nil) can be specified and are handled well.
|
|
|
|
func TestMatcherWildcards(t *testing.T) {
|
2020-12-15 20:52:51 +03:00
|
|
|
t.Parallel()
|
2017-09-27 13:14:52 +03:00
|
|
|
matcher := NewMatcher(testSectionSize, [][][]byte{
|
2017-11-08 13:45:52 +03:00
|
|
|
{common.Address{}.Bytes(), common.Address{0x01}.Bytes()}, // Default address is not a wildcard
|
|
|
|
{common.Hash{}.Bytes(), common.Hash{0x01}.Bytes()}, // Default hash is not a wildcard
|
|
|
|
{common.Hash{0x01}.Bytes()}, // Plain rule, sanity check
|
|
|
|
{common.Hash{0x01}.Bytes(), nil}, // Wildcard suffix, drop rule
|
|
|
|
{nil, common.Hash{0x01}.Bytes()}, // Wildcard prefix, drop rule
|
|
|
|
{nil, nil}, // Wildcard combo, drop rule
|
|
|
|
{}, // Inited wildcard rule, drop rule
|
|
|
|
nil, // Proper wildcard rule, drop rule
|
2017-09-27 13:14:52 +03:00
|
|
|
})
|
|
|
|
if len(matcher.filters) != 3 {
|
|
|
|
t.Fatalf("filter system size mismatch: have %d, want %d", len(matcher.filters), 3)
|
|
|
|
}
|
|
|
|
if len(matcher.filters[0]) != 2 {
|
|
|
|
t.Fatalf("address clause size mismatch: have %d, want %d", len(matcher.filters[0]), 2)
|
|
|
|
}
|
|
|
|
if len(matcher.filters[1]) != 2 {
|
|
|
|
t.Fatalf("combo topic clause size mismatch: have %d, want %d", len(matcher.filters[1]), 2)
|
|
|
|
}
|
|
|
|
if len(matcher.filters[2]) != 1 {
|
|
|
|
t.Fatalf("singletone topic clause size mismatch: have %d, want %d", len(matcher.filters[2]), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// Tests the matcher pipeline on a single continuous workflow without interrupts.
|
|
|
|
func TestMatcherContinuous(t *testing.T) {
|
2020-12-15 20:52:51 +03:00
|
|
|
t.Parallel()
|
2017-11-15 14:54:40 +03:00
|
|
|
testMatcherDiffBatches(t, [][]bloomIndexes{{{10, 20, 30}}}, 0, 100000, false, 75)
|
|
|
|
testMatcherDiffBatches(t, [][]bloomIndexes{{{32, 3125, 100}}, {{40, 50, 10}}}, 0, 100000, false, 81)
|
|
|
|
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)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// Tests the matcher pipeline on a constantly interrupted and resumed work pattern
|
|
|
|
// with the aim of ensuring data items are requested only once.
|
|
|
|
func TestMatcherIntermittent(t *testing.T) {
|
2020-12-15 20:52:51 +03:00
|
|
|
t.Parallel()
|
2017-11-15 14:54:40 +03:00
|
|
|
testMatcherDiffBatches(t, [][]bloomIndexes{{{10, 20, 30}}}, 0, 100000, true, 75)
|
|
|
|
testMatcherDiffBatches(t, [][]bloomIndexes{{{32, 3125, 100}}, {{40, 50, 10}}}, 0, 100000, true, 81)
|
|
|
|
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)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// Tests the matcher pipeline on random input to hopefully catch anomalies.
|
|
|
|
func TestMatcherRandom(t *testing.T) {
|
2020-12-15 20:52:51 +03:00
|
|
|
t.Parallel()
|
2017-08-29 14:13:11 +03:00
|
|
|
for i := 0; i < 10; i++ {
|
2017-11-15 14:54:40 +03:00
|
|
|
testMatcherBothModes(t, makeRandomIndexes([]int{1}, 50), 0, 10000, 0)
|
|
|
|
testMatcherBothModes(t, makeRandomIndexes([]int{3}, 50), 0, 10000, 0)
|
|
|
|
testMatcherBothModes(t, makeRandomIndexes([]int{2, 2, 2}, 20), 0, 10000, 0)
|
|
|
|
testMatcherBothModes(t, makeRandomIndexes([]int{5, 5, 5}, 50), 0, 10000, 0)
|
|
|
|
testMatcherBothModes(t, makeRandomIndexes([]int{4, 4, 4}, 20), 0, 10000, 0)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 14:54:40 +03:00
|
|
|
// Tests that the matcher can properly find matches if the starting block is
|
|
|
|
// shifter from a multiple of 8. This is needed to cover an optimisation with
|
|
|
|
// bitset matching https://github.com/ethereum/go-ethereum/issues/15309.
|
|
|
|
func TestMatcherShifted(t *testing.T) {
|
2020-12-15 20:52:51 +03:00
|
|
|
t.Parallel()
|
2017-11-15 14:54:40 +03:00
|
|
|
// Block 0 always matches in the tests, skip ahead of first 8 blocks with the
|
|
|
|
// start to get a potential zero byte in the matcher bitset.
|
|
|
|
|
|
|
|
// To keep the second bitset byte zero, the filter must only match for the first
|
|
|
|
// time in block 16, so doing an all-16 bit filter should suffice.
|
|
|
|
|
|
|
|
// To keep the starting block non divisible by 8, block number 9 is the first
|
|
|
|
// that would introduce a shift and not match block 0.
|
|
|
|
testMatcherBothModes(t, [][]bloomIndexes{{{16, 16, 16}}}, 9, 64, 0)
|
|
|
|
}
|
|
|
|
|
2017-09-06 11:12:53 +03:00
|
|
|
// Tests that matching on everything doesn't crash (special case internally).
|
|
|
|
func TestWildcardMatcher(t *testing.T) {
|
2020-12-15 20:52:51 +03:00
|
|
|
t.Parallel()
|
2017-11-15 14:54:40 +03:00
|
|
|
testMatcherBothModes(t, nil, 0, 10000, 0)
|
2017-09-06 11:12:53 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// makeRandomIndexes generates a random filter system, composed on multiple filter
|
2017-10-20 12:34:43 +03:00
|
|
|
// criteria, each having one bloom list component for the address and arbitrarily
|
2017-08-29 14:13:11 +03:00
|
|
|
// many topic bloom list components.
|
|
|
|
func makeRandomIndexes(lengths []int, max int) [][]bloomIndexes {
|
|
|
|
res := make([][]bloomIndexes, len(lengths))
|
|
|
|
for i, topics := range lengths {
|
|
|
|
res[i] = make([]bloomIndexes, topics)
|
|
|
|
for j := 0; j < topics; j++ {
|
|
|
|
for k := 0; k < len(res[i][j]); k++ {
|
|
|
|
res[i][j][k] = uint(rand.Intn(max-1) + 2)
|
|
|
|
}
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:13:11 +03:00
|
|
|
return res
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// testMatcherDiffBatches runs the given matches test in single-delivery and also
|
|
|
|
// in batches delivery mode, verifying that all kinds of deliveries are handled
|
2022-06-10 18:47:06 +03:00
|
|
|
// correctly within.
|
2017-11-15 14:54:40 +03:00
|
|
|
func testMatcherDiffBatches(t *testing.T, filter [][]bloomIndexes, start, blocks uint64, intermittent bool, retrievals uint32) {
|
|
|
|
singleton := testMatcher(t, filter, start, blocks, intermittent, retrievals, 1)
|
|
|
|
batched := testMatcher(t, filter, start, blocks, intermittent, retrievals, 16)
|
2017-08-29 14:13:11 +03:00
|
|
|
|
|
|
|
if singleton != batched {
|
2022-06-10 18:47:06 +03:00
|
|
|
t.Errorf("filter = %v blocks = %v intermittent = %v: request count mismatch, %v in singleton vs. %v in batched mode", filter, blocks, intermittent, singleton, batched)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// testMatcherBothModes runs the given matcher test in both continuous as well as
|
|
|
|
// in intermittent mode, verifying that the request counts match each other.
|
2017-11-15 14:54:40 +03:00
|
|
|
func testMatcherBothModes(t *testing.T, filter [][]bloomIndexes, start, blocks uint64, retrievals uint32) {
|
|
|
|
continuous := testMatcher(t, filter, start, blocks, false, retrievals, 16)
|
|
|
|
intermittent := testMatcher(t, filter, start, blocks, true, retrievals, 16)
|
2017-08-29 14:13:11 +03:00
|
|
|
|
|
|
|
if continuous != intermittent {
|
|
|
|
t.Errorf("filter = %v blocks = %v: request count mismatch, %v in continuous vs. %v in intermittent mode", filter, blocks, continuous, intermittent)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// testMatcher is a generic tester to run the given matcher test and return the
|
|
|
|
// number of requests made for cross validation between different modes.
|
2017-11-15 14:54:40 +03:00
|
|
|
func testMatcher(t *testing.T, filter [][]bloomIndexes, start, blocks uint64, intermittent bool, retrievals uint32, maxReqCount int) uint32 {
|
2017-08-29 14:13:11 +03:00
|
|
|
// Create a new matcher an simulate our explicit random bitsets
|
2017-09-06 03:33:10 +03:00
|
|
|
matcher := NewMatcher(testSectionSize, nil)
|
|
|
|
matcher.filters = filter
|
2017-08-29 14:13:11 +03:00
|
|
|
|
|
|
|
for _, rule := range filter {
|
|
|
|
for _, topic := range rule {
|
|
|
|
for _, bit := range topic {
|
|
|
|
matcher.addScheduler(bit)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:13:11 +03:00
|
|
|
// Track the number of retrieval requests made
|
|
|
|
var requested uint32
|
2017-08-18 22:52:20 +03:00
|
|
|
|
2018-07-24 11:24:27 +03:00
|
|
|
// Start the matching session for the filter and the retriever goroutines
|
2017-08-29 14:13:11 +03:00
|
|
|
quit := make(chan struct{})
|
|
|
|
matches := make(chan uint64, 16)
|
2017-08-18 22:52:20 +03:00
|
|
|
|
2017-11-15 14:54:40 +03:00
|
|
|
session, err := matcher.Start(context.Background(), start, blocks-1, matches)
|
2017-08-29 14:13:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to stat matcher session: %v", err)
|
|
|
|
}
|
|
|
|
startRetrievers(session, quit, &requested, maxReqCount)
|
2017-08-18 22:52:20 +03:00
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// Iterate over all the blocks and verify that the pipeline produces the correct matches
|
2017-11-15 14:54:40 +03:00
|
|
|
for i := start; i < blocks; i++ {
|
2017-08-29 14:13:11 +03:00
|
|
|
if expMatch3(filter, i) {
|
|
|
|
match, ok := <-matches
|
2017-08-18 22:52:20 +03:00
|
|
|
if !ok {
|
2017-08-29 14:13:11 +03:00
|
|
|
t.Errorf("filter = %v blocks = %v intermittent = %v: expected #%v, results channel closed", filter, blocks, intermittent, i)
|
2017-08-18 22:52:20 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if match != i {
|
2017-08-29 14:13:11 +03:00
|
|
|
t.Errorf("filter = %v blocks = %v intermittent = %v: expected #%v, got #%v", filter, blocks, intermittent, i, match)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
2017-08-29 14:13:11 +03:00
|
|
|
// If we're testing intermittent mode, abort and restart the pipeline
|
|
|
|
if intermittent {
|
2017-10-24 16:19:09 +03:00
|
|
|
session.Close()
|
2017-08-29 14:13:11 +03:00
|
|
|
close(quit)
|
|
|
|
|
|
|
|
quit = make(chan struct{})
|
|
|
|
matches = make(chan uint64, 16)
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
session, err = matcher.Start(context.Background(), i+1, blocks-1, matches)
|
2017-08-29 14:13:11 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to stat matcher session: %v", err)
|
|
|
|
}
|
|
|
|
startRetrievers(session, quit, &requested, maxReqCount)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:13:11 +03:00
|
|
|
// Ensure the result channel is torn down after the last block
|
|
|
|
match, ok := <-matches
|
2017-08-18 22:52:20 +03:00
|
|
|
if ok {
|
2017-08-29 14:13:11 +03:00
|
|
|
t.Errorf("filter = %v blocks = %v intermittent = %v: expected closed channel, got #%v", filter, blocks, intermittent, match)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
2017-08-29 14:13:11 +03:00
|
|
|
// Clean up the session and ensure we match the expected retrieval count
|
2017-10-24 16:19:09 +03:00
|
|
|
session.Close()
|
2017-08-29 14:13:11 +03:00
|
|
|
close(quit)
|
2017-08-18 22:52:20 +03:00
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
if retrievals != 0 && requested != retrievals {
|
|
|
|
t.Errorf("filter = %v blocks = %v intermittent = %v: request count mismatch, have #%v, want #%v", filter, blocks, intermittent, requested, retrievals)
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
2017-08-29 14:13:11 +03:00
|
|
|
return requested
|
|
|
|
}
|
|
|
|
|
|
|
|
// startRetrievers starts a batch of goroutines listening for section requests
|
|
|
|
// and serving them.
|
|
|
|
func startRetrievers(session *MatcherSession, quit chan struct{}, retrievals *uint32, batch int) {
|
|
|
|
requests := make(chan chan *Retrieval)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
// Start a multiplexer to test multiple threaded execution
|
|
|
|
go session.Multiplex(batch, 100*time.Microsecond, requests)
|
2017-08-18 22:52:20 +03:00
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// Start a services to match the above multiplexer
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
// Wait for a service request or a shutdown
|
|
|
|
select {
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
|
|
|
|
case request := <-requests:
|
|
|
|
task := <-request
|
|
|
|
|
|
|
|
task.Bitsets = make([][]byte, len(task.Sections))
|
|
|
|
for i, section := range task.Sections {
|
|
|
|
if rand.Int()%4 != 0 { // Handle occasional missing deliveries
|
|
|
|
task.Bitsets[i] = generateBitset(task.Bit, section)
|
|
|
|
atomic.AddUint32(retrievals, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request <- task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
// generateBitset generates the rotated bitset for the given bloom bit and section
|
|
|
|
// numbers.
|
|
|
|
func generateBitset(bit uint, section uint64) []byte {
|
|
|
|
bitset := make([]byte, testSectionSize/8)
|
|
|
|
for i := 0; i < len(bitset); i++ {
|
|
|
|
for b := 0; b < 8; b++ {
|
|
|
|
blockIdx := section*testSectionSize + uint64(i*8+b)
|
|
|
|
bitset[i] += bitset[i]
|
|
|
|
if (blockIdx % uint64(bit)) == 0 {
|
|
|
|
bitset[i]++
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:13:11 +03:00
|
|
|
return bitset
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
func expMatch1(filter bloomIndexes, i uint64) bool {
|
|
|
|
for _, ii := range filter {
|
|
|
|
if (i % uint64(ii)) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
func expMatch2(filter []bloomIndexes, i uint64) bool {
|
|
|
|
for _, ii := range filter {
|
|
|
|
if expMatch1(ii, i) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:13:11 +03:00
|
|
|
func expMatch3(filter [][]bloomIndexes, i uint64) bool {
|
|
|
|
for _, ii := range filter {
|
|
|
|
if !expMatch2(ii, i) {
|
|
|
|
return false
|
|
|
|
}
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|
2017-08-29 14:13:11 +03:00
|
|
|
return true
|
2017-08-18 22:52:20 +03:00
|
|
|
}
|