github.com/gregpr07/bsc@v1.1.2/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/gregpr07/bsc/common/bitutil"
    23  	"github.com/gregpr07/bsc/common/gopool"
    24  	"github.com/gregpr07/bsc/core/rawdb"
    25  )
    26  
    27  const (
    28  	// bloomServiceThreads is the number of goroutines used globally by an Ethereum
    29  	// instance to service bloombits lookups for all running filters.
    30  	bloomServiceThreads = 16
    31  
    32  	// bloomFilterThreads is the number of goroutines used locally per filter to
    33  	// multiplex requests onto the global servicing goroutines.
    34  	bloomFilterThreads = 3
    35  
    36  	// bloomRetrievalBatch is the maximum number of bloom bit retrievals to service
    37  	// in a single batch.
    38  	bloomRetrievalBatch = 16
    39  
    40  	// bloomRetrievalWait is the maximum time to wait for enough bloom bit requests
    41  	// to accumulate request an entire batch (avoiding hysteresis).
    42  	bloomRetrievalWait = time.Duration(0)
    43  )
    44  
    45  // startBloomHandlers starts a batch of goroutines to accept bloom bit database
    46  // retrievals from possibly a range of filters and serving the data to satisfy.
    47  func (eth *Ethereum) startBloomHandlers(sectionSize uint64) {
    48  	for i := 0; i < bloomServiceThreads; i++ {
    49  		gopool.Submit(func() {
    50  			for {
    51  				select {
    52  				case <-eth.closeBloomHandler:
    53  					return
    54  
    55  				case request := <-eth.bloomRequests:
    56  					task := <-request
    57  					task.Bitsets = make([][]byte, len(task.Sections))
    58  					for i, section := range task.Sections {
    59  						head := rawdb.ReadCanonicalHash(eth.chainDb, (section+1)*sectionSize-1)
    60  						if compVector, err := rawdb.ReadBloomBits(eth.chainDb, task.Bit, section, head); err == nil {
    61  							if blob, err := bitutil.DecompressBytes(compVector, int(sectionSize/8)); err == nil {
    62  								task.Bitsets[i] = blob
    63  							} else {
    64  								task.Error = err
    65  							}
    66  						} else {
    67  							task.Error = err
    68  						}
    69  					}
    70  					request <- task
    71  				}
    72  			}
    73  		})
    74  	}
    75  }