github.com/m3shine/gochain@v2.2.26+incompatible/eth/bloombits.go (about)

     1  // Copyright 2017 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 eth
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/gochain-io/gochain/common"
    23  	"github.com/gochain-io/gochain/common/bitutil"
    24  	"github.com/gochain-io/gochain/core"
    25  	"github.com/gochain-io/gochain/core/bloombits"
    26  	"github.com/gochain-io/gochain/core/rawdb"
    27  	"github.com/gochain-io/gochain/core/types"
    28  	"github.com/gochain-io/gochain/log"
    29  	"github.com/gochain-io/gochain/params"
    30  )
    31  
    32  const (
    33  	// bloomServiceThreads is the number of goroutines used globally by an GoChain
    34  	// instance to service bloombits lookups for all running filters.
    35  	bloomServiceThreads = 16
    36  
    37  	// bloomFilterThreads is the number of goroutines used locally per filter to
    38  	// multiplex requests onto the global servicing goroutines.
    39  	bloomFilterThreads = 3
    40  
    41  	// bloomRetrievalBatch is the maximum number of bloom bit retrievals to service
    42  	// in a single batch.
    43  	bloomRetrievalBatch = 16
    44  
    45  	// bloomRetrievalWait is the maximum time to wait for enough bloom bit requests
    46  	// to accumulate request an entire batch (avoiding hysteresis).
    47  	bloomRetrievalWait = time.Duration(0)
    48  )
    49  
    50  // startBloomHandlers starts a batch of goroutines to accept bloom bit database
    51  // retrievals from possibly a range of filters and serving the data to satisfy.
    52  func (gc *GoChain) startBloomHandlers() {
    53  	for i := 0; i < bloomServiceThreads; i++ {
    54  		go func() {
    55  			for {
    56  				select {
    57  				case <-gc.shutdownChan:
    58  					return
    59  
    60  				case request := <-gc.bloomRequests:
    61  					task := <-request
    62  					task.Bitsets = make([][]byte, len(task.Sections))
    63  					for i, section := range task.Sections {
    64  						head := rawdb.ReadCanonicalHash(gc.chainDb, (section+1)*params.BloomBitsBlocks-1)
    65  						compVector := rawdb.ReadBloomBits(gc.chainDb.GlobalTable(), task.Bit, section, head)
    66  						if blob, err := bitutil.DecompressBytes(compVector, int(params.BloomBitsBlocks)/8); err == nil {
    67  							task.Bitsets[i] = blob
    68  						} else {
    69  							task.Error = err
    70  						}
    71  					}
    72  					request <- task
    73  				}
    74  			}
    75  		}()
    76  	}
    77  }
    78  
    79  const (
    80  	// bloomConfirms is the number of confirmation blocks before a bloom section is
    81  	// considered probably final and its rotated bits are calculated.
    82  	bloomConfirms = 256
    83  
    84  	// bloomThrottling is the time to wait between processing two consecutive index
    85  	// sections. It's useful during chain upgrades to prevent disk overload.
    86  	bloomThrottling = 100 * time.Millisecond
    87  )
    88  
    89  // BloomIndexer implements a core.ChainIndexer, building up a rotated bloom bits index
    90  // for the GoChain header bloom filters, permitting blazing fast filtering.
    91  type BloomIndexer struct {
    92  	size uint64 // section size to generate bloombits for
    93  
    94  	db  common.Database      // database instance to write index data and metadata into
    95  	gen *bloombits.Generator // generator to rotate the bloom bits crating the bloom index
    96  
    97  	section uint64      // Section is the section number being processed currently
    98  	head    common.Hash // Head is the hash of the last header processed
    99  }
   100  
   101  // NewBloomIndexer returns a chain indexer that generates bloom bits data for the
   102  // canonical chain for fast logs filtering.
   103  func NewBloomIndexer(db common.Database, size uint64) *core.ChainIndexer {
   104  	backend := &BloomIndexer{
   105  		db:   db,
   106  		size: size,
   107  	}
   108  	table := common.NewTablePrefixer(db.GlobalTable(), string(rawdb.BloomBitsIndexPrefix))
   109  
   110  	return core.NewChainIndexer(db, table, backend, size, bloomConfirms, bloomThrottling, "bloombits")
   111  }
   112  
   113  // Reset implements core.ChainIndexerBackend, starting a new bloombits index
   114  // section.
   115  func (b *BloomIndexer) Reset(section uint64, lastSectionHead common.Hash) error {
   116  	gen, err := bloombits.NewGenerator(uint(b.size))
   117  	b.gen, b.section, b.head = gen, section, common.Hash{}
   118  	return err
   119  }
   120  
   121  // Process implements core.ChainIndexerBackend, adding a new header's bloom into
   122  // the index.
   123  func (b *BloomIndexer) Process(header *types.Header) {
   124  	if err := b.gen.AddBloom(uint(header.Number.Uint64()-b.section*b.size), header.Bloom); err != nil {
   125  		log.Error("Cannot add bloom to indexer")
   126  	}
   127  	b.head = header.Hash()
   128  }
   129  
   130  // Commit implements core.ChainIndexerBackend, finalizing the bloom section and
   131  // writing it out into the database.
   132  func (b *BloomIndexer) Commit() error {
   133  	batch := b.db.GlobalTable().NewBatch()
   134  
   135  	for i := 0; i < types.BloomBitLength; i++ {
   136  		bits, err := b.gen.Bitset(uint(i))
   137  		if err != nil {
   138  			return err
   139  		}
   140  		rawdb.WriteBloomBits(batch, uint(i), b.section, b.head, bitutil.CompressBytes(bits))
   141  	}
   142  	rawdb.Must("write bloom bits batch", batch.Write)
   143  	return nil
   144  }