github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/core/bloom_indexer.go (about)

     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  
    17  package core
    18  
    19  import (
    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  
    31  const (
    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.
    39  type 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.
    49  func 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.
    61  func (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, executes an Or operation on header.bloom and private bloom
    68  // (header.bloom | private bloom) and adds to index
    69  func (b *BloomIndexer) Process(ctx context.Context, header *types.Header) error {
    70  	publicBloom := header.Bloom
    71  	privateBloom := rawdb.GetPrivateBlockBloom(b.db, header.Number.Uint64())
    72  	publicBloom.OrBloom(privateBloom.Bytes())
    73  
    74  	b.gen.AddBloom(uint(header.Number.Uint64()-b.section*b.size), publicBloom)
    75  	b.head = header.Hash()
    76  	return nil
    77  }
    78  
    79  // Commit implements core.ChainIndexerBackend, finalizing the bloom section and
    80  // writing it out into the database.
    81  func (b *BloomIndexer) Commit() error {
    82  	batch := b.db.NewBatch()
    83  	for i := 0; i < types.BloomBitLength; i++ {
    84  		bits, err := b.gen.Bitset(uint(i))
    85  		if err != nil {
    86  			return err
    87  		}
    88  		rawdb.WriteBloomBits(batch, uint(i), b.section, b.head, bitutil.CompressBytes(bits))
    89  	}
    90  	return batch.Write()
    91  }
    92  
    93  // Prune returns an empty error since we don't support pruning here.
    94  func (b *BloomIndexer) Prune(threshold uint64) error {
    95  	return nil
    96  }