Skip to content

Commit 4ea4d2d

Browse files
zsfelfoldikaralabe
authored andcommitted
core, eth: add bloombit indexer, filter based on it
1 parent 1e67378 commit 4ea4d2d

23 files changed

Lines changed: 1590 additions & 483 deletions

‎core/blockchain.go‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -759,12 +759,6 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
759759
log.Crit("Failed to write block receipts", "err", err)
760760
return
761761
}
762-
if err := WriteMipmapBloom(bc.chainDb, block.NumberU64(), receipts); err != nil {
763-
errs[index] = fmt.Errorf("failed to write log blooms: %v", err)
764-
atomic.AddInt32(&failed, 1)
765-
log.Crit("Failed to write log blooms", "err", err)
766-
return
767-
}
768762
if err := WriteTxLookupEntries(bc.chainDb, block); err != nil {
769763
errs[index] = fmt.Errorf("failed to write lookup metadata: %v", err)
770764
atomic.AddInt32(&failed, 1)
@@ -1017,10 +1011,6 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
10171011
if err := WriteTxLookupEntries(bc.chainDb, block); err != nil {
10181012
return i, err
10191013
}
1020-
// Write map map bloom filters
1021-
if err := WriteMipmapBloom(bc.chainDb, block.NumberU64(), receipts); err != nil {
1022-
return i, err
1023-
}
10241014
// Write hash preimages
10251015
if err := WritePreimages(bc.chainDb, block.NumberU64(), state.Preimages()); err != nil {
10261016
return i, err
@@ -1178,11 +1168,6 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
11781168
if err := WriteTxLookupEntries(bc.chainDb, block); err != nil {
11791169
return err
11801170
}
1181-
// Write map map bloom filters
1182-
receipts := GetBlockReceipts(bc.chainDb, block.Hash(), block.NumberU64())
1183-
if err := WriteMipmapBloom(bc.chainDb, block.NumberU64(), receipts); err != nil {
1184-
return err
1185-
}
11861171
addedTxs = append(addedTxs, block.Transactions()...)
11871172
}
11881173

‎core/bloombits/fetcher_test.go‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2017 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+
package bloombits
17+
18+
import (
19+
"bytes"
20+
"encoding/binary"
21+
"math/rand"
22+
"sync"
23+
"sync/atomic"
24+
"testing"
25+
"time"
26+
)
27+
28+
const testFetcherReqCount = 5000
29+
30+
func fetcherTestVector(b uint, s uint64) []byte {
31+
r := make([]byte, 10)
32+
binary.BigEndian.PutUint16(r[0:2], uint16(b))
33+
binary.BigEndian.PutUint64(r[2:10], s)
34+
return r
35+
}
36+
37+
func TestFetcher(t *testing.T) {
38+
testFetcher(t, 1)
39+
}
40+
41+
func TestFetcherMultipleReaders(t *testing.T) {
42+
testFetcher(t, 10)
43+
}
44+
45+
func testFetcher(t *testing.T, cnt int) {
46+
f := &fetcher{
47+
requestMap: make(map[uint64]fetchRequest),
48+
}
49+
distCh := make(chan distRequest, channelCap)
50+
stop := make(chan struct{})
51+
var reqCount uint32
52+
53+
for i := 0; i < 10; i++ {
54+
go func() {
55+
for {
56+
req, ok := <-distCh
57+
if !ok {
58+
return
59+
}
60+
time.Sleep(time.Duration(rand.Intn(100000)))
61+
atomic.AddUint32(&reqCount, 1)
62+
f.deliver([]uint64{req.sectionIndex}, [][]byte{fetcherTestVector(req.bloomIndex, req.sectionIndex)})
63+
}
64+
}()
65+
}
66+
67+
var wg, wg2 sync.WaitGroup
68+
for cc := 0; cc < cnt; cc++ {
69+
wg.Add(1)
70+
in := make(chan uint64, channelCap)
71+
out := f.fetch(in, distCh, stop, &wg2)
72+
73+
time.Sleep(time.Millisecond * 10 * time.Duration(cc))
74+
go func() {
75+
for i := uint64(0); i < testFetcherReqCount; i++ {
76+
in <- i
77+
}
78+
}()
79+
80+
go func() {
81+
for i := uint64(0); i < testFetcherReqCount; i++ {
82+
bv := <-out
83+
if !bytes.Equal(bv, fetcherTestVector(0, i)) {
84+
if len(bv) != 10 {
85+
t.Errorf("Vector #%d length is %d, expected 10", i, len(bv))
86+
} else {
87+
j := binary.BigEndian.Uint64(bv[2:10])
88+
t.Errorf("Expected vector #%d, fetched #%d", i, j)
89+
}
90+
}
91+
}
92+
wg.Done()
93+
}()
94+
}
95+
96+
wg.Wait()
97+
close(stop)
98+
if reqCount != testFetcherReqCount {
99+
t.Errorf("Request count mismatch: expected %v, got %v", testFetcherReqCount, reqCount)
100+
}
101+
}

0 commit comments

Comments
 (0)