aboutsummaryrefslogtreecommitdiffstats
path: root/core/bloombits/generator.go
blob: 04a7f5146ce51cb68db29bbf7a397cb5e0b7ab0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 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/>.

package bloombits

import (
    "errors"

    "github.com/ethereum/go-ethereum/core/types"
)

// errSectionOutOfBounds is returned if the user tried to add more bloom filters
// to the batch than available space, or if tries to retrieve above the capacity,
var errSectionOutOfBounds = errors.New("section out of bounds")

// Generator takes a number of bloom filters and generates the rotated bloom bits
// to be used for batched filtering.
type Generator struct {
    blooms   [types.BloomBitLength][]byte // Rotated blooms for per-bit matching
    sections uint                         // Number of sections to batch together
    nextBit  uint                         // Next bit to set when adding a bloom
}

// NewGenerator creates a rotated bloom generator that can iteratively fill a
// batched bloom filter's bits.
func NewGenerator(sections uint) (*Generator, error) {
    if sections%8 != 0 {
        return nil, errors.New("section count not multiple of 8")
    }
    b := &Generator{sections: sections}
    for i := 0; i < types.BloomBitLength; i++ {
        b.blooms[i] = make([]byte, sections/8)
    }
    return b, nil
}

// AddBloom takes a single bloom filter and sets the corresponding bit column
// in memory accordingly.
func (b *Generator) AddBloom(bloom types.Bloom) error {
    // Make sure we're not adding more bloom filters than our capacity
    if b.nextBit >= b.sections {
        return errSectionOutOfBounds
    }
    // Rotate the bloom and insert into our collection
    byteMask := b.nextBit / 8
    bitMask := byte(1) << byte(7-b.nextBit%8)

    for i := 0; i < types.BloomBitLength; i++ {
        bloomByteMask := types.BloomByteLength - 1 - i/8
        bloomBitMask := byte(1) << byte(i%8)

        if (bloom[bloomByteMask] & bloomBitMask) != 0 {
            b.blooms[i][byteMask] |= bitMask
        }
    }
    b.nextBit++

    return nil
}

// Bitset returns the bit vector belonging to the given bit index after all
// blooms have been added.
func (b *Generator) Bitset(idx uint) ([]byte, error) {
    if b.nextBit != b.sections {
        return nil, errors.New("bloom not fully generated yet")
    }
    if idx >= b.sections {
        return nil, errSectionOutOfBounds
    }
    return b.blooms[idx], nil
}