1// Copyright 2021 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 17package core 18 19import ( 20 "context" 21 "time" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/common/bitutil" 25 "github.com/ethereum/go-ethereum/core/bloombits" 26 "github.com/ethereum/go-ethereum/core/rawdb" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/ethdb" 29) 30 31const ( 32 // bloomThrottling is the time to wait between processing two consecutive index 33 // sections. It's useful during chain upgrades to prevent disk overload. 34 bloomThrottling = 100 * time.Millisecond 35) 36 37// BloomIndexer implements a core.ChainIndexer, building up a rotated bloom bits index 38// for the Ethereum header bloom filters, permitting blazing fast filtering. 39type BloomIndexer struct { 40 size uint64 // section size to generate bloombits for 41 db ethdb.Database // database instance to write index data and metadata into 42 gen *bloombits.Generator // generator to rotate the bloom bits crating the bloom index 43 section uint64 // Section is the section number being processed currently 44 head common.Hash // Head is the hash of the last header processed 45} 46 47// NewBloomIndexer returns a chain indexer that generates bloom bits data for the 48// canonical chain for fast logs filtering. 49func NewBloomIndexer(db ethdb.Database, size, confirms uint64) *ChainIndexer { 50 backend := &BloomIndexer{ 51 db: db, 52 size: size, 53 } 54 table := rawdb.NewTable(db, string(rawdb.BloomBitsIndexPrefix)) 55 56 return NewChainIndexer(db, table, backend, size, confirms, bloomThrottling, "bloombits") 57} 58 59// Reset implements core.ChainIndexerBackend, starting a new bloombits index 60// section. 61func (b *BloomIndexer) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error { 62 gen, err := bloombits.NewGenerator(uint(b.size)) 63 b.gen, b.section, b.head = gen, section, common.Hash{} 64 return err 65} 66 67// Process implements core.ChainIndexerBackend, adding a new header's bloom into 68// the index. 69func (b *BloomIndexer) Process(ctx context.Context, header *types.Header) error { 70 b.gen.AddBloom(uint(header.Number.Uint64()-b.section*b.size), header.Bloom) 71 b.head = header.Hash() 72 return nil 73} 74 75// Commit implements core.ChainIndexerBackend, finalizing the bloom section and 76// writing it out into the database. 77func (b *BloomIndexer) Commit() error { 78 batch := b.db.NewBatch() 79 for i := 0; i < types.BloomBitLength; i++ { 80 bits, err := b.gen.Bitset(uint(i)) 81 if err != nil { 82 return err 83 } 84 rawdb.WriteBloomBits(batch, uint(i), b.section, b.head, bitutil.CompressBytes(bits)) 85 } 86 return batch.Write() 87} 88 89// Prune returns an empty error since we don't support pruning here. 90func (b *BloomIndexer) Prune(threshold uint64) error { 91 return nil 92} 93