github.com/MetalBlockchain/subnet-evm@v0.4.9/core/bloombits/generator.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2017 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package bloombits 28 29 import ( 30 "errors" 31 32 "github.com/MetalBlockchain/subnet-evm/core/types" 33 ) 34 35 var ( 36 // errSectionOutOfBounds is returned if the user tried to add more bloom filters 37 // to the batch than available space, or if tries to retrieve above the capacity. 38 errSectionOutOfBounds = errors.New("section out of bounds") 39 40 // errBloomBitOutOfBounds is returned if the user tried to retrieve specified 41 // bit bloom above the capacity. 42 errBloomBitOutOfBounds = errors.New("bloom bit out of bounds") 43 ) 44 45 // Generator takes a number of bloom filters and generates the rotated bloom bits 46 // to be used for batched filtering. 47 type Generator struct { 48 blooms [types.BloomBitLength][]byte // Rotated blooms for per-bit matching 49 sections uint // Number of sections to batch together 50 nextSec uint // Next section to set when adding a bloom 51 } 52 53 // NewGenerator creates a rotated bloom generator that can iteratively fill a 54 // batched bloom filter's bits. 55 func NewGenerator(sections uint) (*Generator, error) { 56 if sections%8 != 0 { 57 return nil, errors.New("section count not multiple of 8") 58 } 59 b := &Generator{sections: sections} 60 for i := 0; i < types.BloomBitLength; i++ { 61 b.blooms[i] = make([]byte, sections/8) 62 } 63 return b, nil 64 } 65 66 // AddBloom takes a single bloom filter and sets the corresponding bit column 67 // in memory accordingly. 68 func (b *Generator) AddBloom(index uint, bloom types.Bloom) error { 69 // Make sure we're not adding more bloom filters than our capacity 70 if b.nextSec >= b.sections { 71 return errSectionOutOfBounds 72 } 73 if b.nextSec != index { 74 return errors.New("bloom filter with unexpected index") 75 } 76 // Rotate the bloom and insert into our collection 77 byteIndex := b.nextSec / 8 78 bitIndex := byte(7 - b.nextSec%8) 79 for byt := 0; byt < types.BloomByteLength; byt++ { 80 bloomByte := bloom[types.BloomByteLength-1-byt] 81 if bloomByte == 0 { 82 continue 83 } 84 base := 8 * byt 85 b.blooms[base+7][byteIndex] |= ((bloomByte >> 7) & 1) << bitIndex 86 b.blooms[base+6][byteIndex] |= ((bloomByte >> 6) & 1) << bitIndex 87 b.blooms[base+5][byteIndex] |= ((bloomByte >> 5) & 1) << bitIndex 88 b.blooms[base+4][byteIndex] |= ((bloomByte >> 4) & 1) << bitIndex 89 b.blooms[base+3][byteIndex] |= ((bloomByte >> 3) & 1) << bitIndex 90 b.blooms[base+2][byteIndex] |= ((bloomByte >> 2) & 1) << bitIndex 91 b.blooms[base+1][byteIndex] |= ((bloomByte >> 1) & 1) << bitIndex 92 b.blooms[base][byteIndex] |= (bloomByte & 1) << bitIndex 93 } 94 b.nextSec++ 95 return nil 96 } 97 98 // Bitset returns the bit vector belonging to the given bit index after all 99 // blooms have been added. 100 func (b *Generator) Bitset(idx uint) ([]byte, error) { 101 if b.nextSec != b.sections { 102 return nil, errors.New("bloom not fully generated yet") 103 } 104 if idx >= types.BloomBitLength { 105 return nil, errBloomBitOutOfBounds 106 } 107 return b.blooms[idx], nil 108 }