github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/les/bloombits.go (about)

     1  package les
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/quickchainproject/quickchain/common/bitutil"
     7  	"github.com/quickchainproject/quickchain/light"
     8  )
     9  
    10  const (
    11  	// bloomServiceThreads is the number of goroutines used globally by an Ethereum
    12  	// instance to service bloombits lookups for all running filters.
    13  	bloomServiceThreads = 16
    14  
    15  	// bloomFilterThreads is the number of goroutines used locally per filter to
    16  	// multiplex requests onto the global servicing goroutines.
    17  	bloomFilterThreads = 3
    18  
    19  	// bloomRetrievalBatch is the maximum number of bloom bit retrievals to service
    20  	// in a single batch.
    21  	bloomRetrievalBatch = 16
    22  
    23  	// bloomRetrievalWait is the maximum time to wait for enough bloom bit requests
    24  	// to accumulate request an entire batch (avoiding hysteresis).
    25  	bloomRetrievalWait = time.Microsecond * 100
    26  )
    27  
    28  // startBloomHandlers starts a batch of goroutines to accept bloom bit database
    29  // retrievals from possibly a range of filters and serving the data to satisfy.
    30  func (eth *LightEthereum) startBloomHandlers() {
    31  	for i := 0; i < bloomServiceThreads; i++ {
    32  		go func() {
    33  			for {
    34  				select {
    35  				case <-eth.shutdownChan:
    36  					return
    37  
    38  				case request := <-eth.bloomRequests:
    39  					task := <-request
    40  					task.Bitsets = make([][]byte, len(task.Sections))
    41  					compVectors, err := light.GetBloomBits(task.Context, eth.odr, task.Bit, task.Sections)
    42  					if err == nil {
    43  						for i := range task.Sections {
    44  							if blob, err := bitutil.DecompressBytes(compVectors[i], int(light.BloomTrieFrequency/8)); err == nil {
    45  								task.Bitsets[i] = blob
    46  							} else {
    47  								task.Error = err
    48  							}
    49  						}
    50  					} else {
    51  						task.Error = err
    52  					}
    53  					request <- task
    54  				}
    55  			}
    56  		}()
    57  	}
    58  }