Skip to content

Commit ba6c5e8

Browse files
committed
initia custom
1 parent 36b2371 commit ba6c5e8

File tree

13 files changed

+1574
-6
lines changed

13 files changed

+1574
-6
lines changed

core/bloombits/docs.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
17+
// Package bloombits implements bloom filtering on batches of data.
18+
package bloombits

core/bloombits/generator.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
17+
package bloombits
18+
19+
import (
20+
"errors"
21+
22+
"github.com/ethereum/go-ethereum/core/types"
23+
)
24+
25+
var (
26+
// errSectionOutOfBounds is returned if the user tried to add more bloom filters
27+
// to the batch than available space, or if tries to retrieve above the capacity.
28+
errSectionOutOfBounds = errors.New("section out of bounds")
29+
30+
// errBloomBitOutOfBounds is returned if the user tried to retrieve specified
31+
// bit bloom above the capacity.
32+
errBloomBitOutOfBounds = errors.New("bloom bit out of bounds")
33+
)
34+
35+
// Generator takes a number of bloom filters and generates the rotated bloom bits
36+
// to be used for batched filtering.
37+
type Generator struct {
38+
blooms [types.BloomBitLength][]byte // Rotated blooms for per-bit matching
39+
sections uint // Number of sections to batch together
40+
nextSec uint // Next section to set when adding a bloom
41+
}
42+
43+
// NewGenerator creates a rotated bloom generator that can iteratively fill a
44+
// batched bloom filter's bits.
45+
func NewGenerator(sections uint) (*Generator, error) {
46+
if sections%8 != 0 {
47+
return nil, errors.New("section count not multiple of 8")
48+
}
49+
b := &Generator{sections: sections}
50+
for i := 0; i < types.BloomBitLength; i++ {
51+
b.blooms[i] = make([]byte, sections/8)
52+
}
53+
return b, nil
54+
}
55+
56+
// AddBloom takes a single bloom filter and sets the corresponding bit column
57+
// in memory accordingly.
58+
func (b *Generator) AddBloom(index uint, bloom types.Bloom) error {
59+
// Make sure we're not adding more bloom filters than our capacity
60+
if b.nextSec >= b.sections {
61+
return errSectionOutOfBounds
62+
}
63+
if b.nextSec != index {
64+
return errors.New("bloom filter with unexpected index")
65+
}
66+
// Rotate the bloom and insert into our collection
67+
byteIndex := b.nextSec / 8
68+
bitIndex := byte(7 - b.nextSec%8)
69+
for byt := 0; byt < types.BloomByteLength; byt++ {
70+
bloomByte := bloom[types.BloomByteLength-1-byt]
71+
if bloomByte == 0 {
72+
continue
73+
}
74+
base := 8 * byt
75+
b.blooms[base+7][byteIndex] |= ((bloomByte >> 7) & 1) << bitIndex
76+
b.blooms[base+6][byteIndex] |= ((bloomByte >> 6) & 1) << bitIndex
77+
b.blooms[base+5][byteIndex] |= ((bloomByte >> 5) & 1) << bitIndex
78+
b.blooms[base+4][byteIndex] |= ((bloomByte >> 4) & 1) << bitIndex
79+
b.blooms[base+3][byteIndex] |= ((bloomByte >> 3) & 1) << bitIndex
80+
b.blooms[base+2][byteIndex] |= ((bloomByte >> 2) & 1) << bitIndex
81+
b.blooms[base+1][byteIndex] |= ((bloomByte >> 1) & 1) << bitIndex
82+
b.blooms[base][byteIndex] |= (bloomByte & 1) << bitIndex
83+
}
84+
b.nextSec++
85+
return nil
86+
}
87+
88+
// Bitset returns the bit vector belonging to the given bit index after all
89+
// blooms have been added.
90+
func (b *Generator) Bitset(idx uint) ([]byte, error) {
91+
if b.nextSec != b.sections {
92+
return nil, errors.New("bloom not fully generated yet")
93+
}
94+
if idx >= types.BloomBitLength {
95+
return nil, errBloomBitOutOfBounds
96+
}
97+
return b.blooms[idx], nil
98+
}

core/bloombits/generator_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
17+
package bloombits
18+
19+
import (
20+
"bytes"
21+
crand "crypto/rand"
22+
"math/rand"
23+
"testing"
24+
25+
"github.com/ethereum/go-ethereum/core/types"
26+
)
27+
28+
// Tests that batched bloom bits are correctly rotated from the input bloom
29+
// filters.
30+
func TestGenerator(t *testing.T) {
31+
// Generate the input and the rotated output
32+
var input, output [types.BloomBitLength][types.BloomByteLength]byte
33+
34+
for i := 0; i < types.BloomBitLength; i++ {
35+
for j := 0; j < types.BloomBitLength; j++ {
36+
bit := byte(rand.Int() % 2)
37+
38+
input[i][j/8] |= bit << byte(7-j%8)
39+
output[types.BloomBitLength-1-j][i/8] |= bit << byte(7-i%8)
40+
}
41+
}
42+
// Crunch the input through the generator and verify the result
43+
gen, err := NewGenerator(types.BloomBitLength)
44+
if err != nil {
45+
t.Fatalf("failed to create bloombit generator: %v", err)
46+
}
47+
for i, bloom := range input {
48+
if err := gen.AddBloom(uint(i), bloom); err != nil {
49+
t.Fatalf("bloom %d: failed to add: %v", i, err)
50+
}
51+
}
52+
for i, want := range output {
53+
have, err := gen.Bitset(uint(i))
54+
if err != nil {
55+
t.Fatalf("output %d: failed to retrieve bits: %v", i, err)
56+
}
57+
if !bytes.Equal(have, want[:]) {
58+
t.Errorf("output %d: bit vector mismatch have %x, want %x", i, have, want)
59+
}
60+
}
61+
}
62+
63+
func BenchmarkGenerator(b *testing.B) {
64+
var input [types.BloomBitLength][types.BloomByteLength]byte
65+
b.Run("empty", func(b *testing.B) {
66+
b.ReportAllocs()
67+
b.ResetTimer()
68+
for i := 0; i < b.N; i++ {
69+
// Crunch the input through the generator and verify the result
70+
gen, err := NewGenerator(types.BloomBitLength)
71+
if err != nil {
72+
b.Fatalf("failed to create bloombit generator: %v", err)
73+
}
74+
for j, bloom := range &input {
75+
if err := gen.AddBloom(uint(j), bloom); err != nil {
76+
b.Fatalf("bloom %d: failed to add: %v", i, err)
77+
}
78+
}
79+
}
80+
})
81+
for i := 0; i < types.BloomBitLength; i++ {
82+
crand.Read(input[i][:])
83+
}
84+
b.Run("random", func(b *testing.B) {
85+
b.ReportAllocs()
86+
b.ResetTimer()
87+
for i := 0; i < b.N; i++ {
88+
// Crunch the input through the generator and verify the result
89+
gen, err := NewGenerator(types.BloomBitLength)
90+
if err != nil {
91+
b.Fatalf("failed to create bloombit generator: %v", err)
92+
}
93+
for j, bloom := range &input {
94+
if err := gen.AddBloom(uint(j), bloom); err != nil {
95+
b.Fatalf("bloom %d: failed to add: %v", i, err)
96+
}
97+
}
98+
}
99+
})
100+
}

0 commit comments

Comments
 (0)