|
|
@ -22,16 +22,22 @@ import ( |
|
|
|
"github.com/ethereum/go-ethereum/core/types" |
|
|
|
"github.com/ethereum/go-ethereum/core/types" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// errSectionOutOfBounds is returned if the user tried to add more bloom filters
|
|
|
|
var ( |
|
|
|
// to the batch than available space, or if tries to retrieve above the capacity,
|
|
|
|
// errSectionOutOfBounds is returned if the user tried to add more bloom filters
|
|
|
|
var errSectionOutOfBounds = errors.New("section out of bounds") |
|
|
|
// to the batch than available space, or if tries to retrieve above the capacity.
|
|
|
|
|
|
|
|
errSectionOutOfBounds = errors.New("section out of bounds") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// errBloomBitOutOfBounds is returned if the user tried to retrieve specified
|
|
|
|
|
|
|
|
// bit bloom above the capacity.
|
|
|
|
|
|
|
|
errBloomBitOutOfBounds = errors.New("bloom bit out of bounds") |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// Generator takes a number of bloom filters and generates the rotated bloom bits
|
|
|
|
// Generator takes a number of bloom filters and generates the rotated bloom bits
|
|
|
|
// to be used for batched filtering.
|
|
|
|
// to be used for batched filtering.
|
|
|
|
type Generator struct { |
|
|
|
type Generator struct { |
|
|
|
blooms [types.BloomBitLength][]byte // Rotated blooms for per-bit matching
|
|
|
|
blooms [types.BloomBitLength][]byte // Rotated blooms for per-bit matching
|
|
|
|
sections uint // Number of sections to batch together
|
|
|
|
sections uint // Number of sections to batch together
|
|
|
|
nextBit uint // Next bit to set when adding a bloom
|
|
|
|
nextSec uint // Next section to set when adding a bloom
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewGenerator creates a rotated bloom generator that can iteratively fill a
|
|
|
|
// NewGenerator creates a rotated bloom generator that can iteratively fill a
|
|
|
@ -51,15 +57,15 @@ func NewGenerator(sections uint) (*Generator, error) { |
|
|
|
// in memory accordingly.
|
|
|
|
// in memory accordingly.
|
|
|
|
func (b *Generator) AddBloom(index uint, bloom types.Bloom) error { |
|
|
|
func (b *Generator) AddBloom(index uint, bloom types.Bloom) error { |
|
|
|
// Make sure we're not adding more bloom filters than our capacity
|
|
|
|
// Make sure we're not adding more bloom filters than our capacity
|
|
|
|
if b.nextBit >= b.sections { |
|
|
|
if b.nextSec >= b.sections { |
|
|
|
return errSectionOutOfBounds |
|
|
|
return errSectionOutOfBounds |
|
|
|
} |
|
|
|
} |
|
|
|
if b.nextBit != index { |
|
|
|
if b.nextSec != index { |
|
|
|
return errors.New("bloom filter with unexpected index") |
|
|
|
return errors.New("bloom filter with unexpected index") |
|
|
|
} |
|
|
|
} |
|
|
|
// Rotate the bloom and insert into our collection
|
|
|
|
// Rotate the bloom and insert into our collection
|
|
|
|
byteIndex := b.nextBit / 8 |
|
|
|
byteIndex := b.nextSec / 8 |
|
|
|
bitMask := byte(1) << byte(7-b.nextBit%8) |
|
|
|
bitMask := byte(1) << byte(7-b.nextSec%8) |
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < types.BloomBitLength; i++ { |
|
|
|
for i := 0; i < types.BloomBitLength; i++ { |
|
|
|
bloomByteIndex := types.BloomByteLength - 1 - i/8 |
|
|
|
bloomByteIndex := types.BloomByteLength - 1 - i/8 |
|
|
@ -69,7 +75,7 @@ func (b *Generator) AddBloom(index uint, bloom types.Bloom) error { |
|
|
|
b.blooms[i][byteIndex] |= bitMask |
|
|
|
b.blooms[i][byteIndex] |= bitMask |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
b.nextBit++ |
|
|
|
b.nextSec++ |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
@ -77,11 +83,11 @@ func (b *Generator) AddBloom(index uint, bloom types.Bloom) error { |
|
|
|
// Bitset returns the bit vector belonging to the given bit index after all
|
|
|
|
// Bitset returns the bit vector belonging to the given bit index after all
|
|
|
|
// blooms have been added.
|
|
|
|
// blooms have been added.
|
|
|
|
func (b *Generator) Bitset(idx uint) ([]byte, error) { |
|
|
|
func (b *Generator) Bitset(idx uint) ([]byte, error) { |
|
|
|
if b.nextBit != b.sections { |
|
|
|
if b.nextSec != b.sections { |
|
|
|
return nil, errors.New("bloom not fully generated yet") |
|
|
|
return nil, errors.New("bloom not fully generated yet") |
|
|
|
} |
|
|
|
} |
|
|
|
if idx >= b.sections { |
|
|
|
if idx >= types.BloomBitLength { |
|
|
|
return nil, errSectionOutOfBounds |
|
|
|
return nil, errBloomBitOutOfBounds |
|
|
|
} |
|
|
|
} |
|
|
|
return b.blooms[idx], nil |
|
|
|
return b.blooms[idx], nil |
|
|
|
} |
|
|
|
} |
|
|
|