github.com/elastos/Elastos.ELA.SideChain.ETH@v0.2.2/eth/bloombits.go (about)

     1  // Copyright 2017 The Elastos.ELA.SideChain.ESC Authors
     2  // This file is part of the Elastos.ELA.SideChain.ESC library.
     3  //
     4  // The Elastos.ELA.SideChain.ESC 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 Elastos.ELA.SideChain.ESC 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 Elastos.ELA.SideChain.ESC library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package eth
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/elastos/Elastos.ELA.SideChain.ESC/common"
    24  	"github.com/elastos/Elastos.ELA.SideChain.ESC/common/bitutil"
    25  	"github.com/elastos/Elastos.ELA.SideChain.ESC/core"
    26  	"github.com/elastos/Elastos.ELA.SideChain.ESC/core/bloombits"
    27  	"github.com/elastos/Elastos.ELA.SideChain.ESC/core/rawdb"
    28  	"github.com/elastos/Elastos.ELA.SideChain.ESC/core/types"
    29  	"github.com/elastos/Elastos.ELA.SideChain.ESC/ethdb"
    30  )
    31  
    32  const (
    33  	// bloomServiceThreads is the number of goroutines used globally by an Ethereum
    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 (eth *Ethereum) startBloomHandlers(sectionSize uint64) {
    53  	for i := 0; i < bloomServiceThreads; i++ {
    54  		go func() {
    55  			for {
    56  				select {
    57  				case <-eth.shutdownChan:
    58  					return
    59  
    60  				case request := <-eth.bloomRequests:
    61  					task := <-request
    62  					task.Bitsets = make([][]byte, len(task.Sections))
    63  					for i, section := range task.Sections {
    64  						head := rawdb.ReadCanonicalHash(eth.chainDb, (section+1)*sectionSize-1)
    65  						if compVector, err := rawdb.ReadBloomBits(eth.chainDb, task.Bit, section, head); err == nil {
    66  							if blob, err := bitutil.DecompressBytes(compVector, int(sectionSize/8)); err == nil {
    67  								task.Bitsets[i] = blob
    68  							} else {
    69  								task.Error = err
    70  							}
    71  						} else {
    72  							task.Error = err
    73  						}
    74  					}
    75  					request <- task
    76  				}
    77  			}
    78  		}()
    79  	}
    80  }
    81  
    82  const (
    83  	// bloomThrottling is the time to wait between processing two consecutive index
    84  	// sections. It's useful during chain upgrades to prevent disk overload.
    85  	bloomThrottling = 100 * time.Millisecond
    86  )
    87  
    88  // BloomIndexer implements a core.ChainIndexer, building up a rotated bloom bits index
    89  // for the Ethereum header bloom filters, permitting blazing fast filtering.
    90  type BloomIndexer struct {
    91  	size    uint64               // section size to generate bloombits for
    92  	db      ethdb.Database       // database instance to write index data and metadata into
    93  	gen     *bloombits.Generator // generator to rotate the bloom bits crating the bloom index
    94  	section uint64               // Section is the section number being processed currently
    95  	head    common.Hash          // Head is the hash of the last header processed
    96  }
    97  
    98  // NewBloomIndexer returns a chain indexer that generates bloom bits data for the
    99  // canonical chain for fast logs filtering.
   100  func NewBloomIndexer(db ethdb.Database, size, confirms uint64) *core.ChainIndexer {
   101  	backend := &BloomIndexer{
   102  		db:   db,
   103  		size: size,
   104  	}
   105  	table := rawdb.NewTable(db, string(rawdb.BloomBitsIndexPrefix))
   106  
   107  	return core.NewChainIndexer(db, table, backend, size, confirms, bloomThrottling, "bloombits")
   108  }
   109  
   110  // Reset implements core.ChainIndexerBackend, starting a new bloombits index
   111  // section.
   112  func (b *BloomIndexer) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error {
   113  	gen, err := bloombits.NewGenerator(uint(b.size))
   114  	b.gen, b.section, b.head = gen, section, common.Hash{}
   115  	return err
   116  }
   117  
   118  // Process implements core.ChainIndexerBackend, adding a new header's bloom into
   119  // the index.
   120  func (b *BloomIndexer) Process(ctx context.Context, header *types.Header) error {
   121  	b.gen.AddBloom(uint(header.Number.Uint64()-b.section*b.size), header.Bloom)
   122  	b.head = header.Hash()
   123  	return nil
   124  }
   125  
   126  // Commit implements core.ChainIndexerBackend, finalizing the bloom section and
   127  // writing it out into the database.
   128  func (b *BloomIndexer) Commit() error {
   129  	batch := b.db.NewBatch()
   130  	for i := 0; i < types.BloomBitLength; i++ {
   131  		bits, err := b.gen.Bitset(uint(i))
   132  		if err != nil {
   133  			return err
   134  		}
   135  		rawdb.WriteBloomBits(batch, uint(i), b.section, b.head, bitutil.CompressBytes(bits))
   136  	}
   137  	return batch.Write()
   138  }