github.com/fxsjy/go-ethereum@v1.8.4-0.20180410143526-2e247705cd27/les/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 les
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/ethereum/go-ethereum/common/bitutil"
    23  	"github.com/ethereum/go-ethereum/light"
    24  )
    25  
    26  const (
    27  	// bloomServiceThreads is the number of goroutines used globally by an Ethereum
    28  	// instance to service bloombits lookups for all running filters.
    29  	bloomServiceThreads = 16
    30  
    31  	// bloomFilterThreads is the number of goroutines used locally per filter to
    32  	// multiplex requests onto the global servicing goroutines.
    33  	bloomFilterThreads = 3
    34  
    35  	// bloomRetrievalBatch is the maximum number of bloom bit retrievals to service
    36  	// in a single batch.
    37  	bloomRetrievalBatch = 16
    38  
    39  	// bloomRetrievalWait is the maximum time to wait for enough bloom bit requests
    40  	// to accumulate request an entire batch (avoiding hysteresis).
    41  	bloomRetrievalWait = time.Microsecond * 100
    42  )
    43  
    44  // startBloomHandlers starts a batch of goroutines to accept bloom bit database
    45  // retrievals from possibly a range of filters and serving the data to satisfy.
    46  func (eth *LightEthereum) startBloomHandlers() {
    47  	for i := 0; i < bloomServiceThreads; i++ {
    48  		go func() {
    49  			for {
    50  				select {
    51  				case <-eth.shutdownChan:
    52  					return
    53  
    54  				case request := <-eth.bloomRequests:
    55  					task := <-request
    56  					task.Bitsets = make([][]byte, len(task.Sections))
    57  					compVectors, err := light.GetBloomBits(task.Context, eth.odr, task.Bit, task.Sections)
    58  					if err == nil {
    59  						for i := range task.Sections {
    60  							if blob, err := bitutil.DecompressBytes(compVectors[i], int(light.BloomTrieFrequency/8)); err == nil {
    61  								task.Bitsets[i] = blob
    62  							} else {
    63  								task.Error = err
    64  							}
    65  						}
    66  					} else {
    67  						task.Error = err
    68  					}
    69  					request <- task
    70  				}
    71  			}
    72  		}()
    73  	}
    74  }
    75  
    76  const (
    77  	// bloomConfirms is the number of confirmation blocks before a bloom section is
    78  	// considered probably final and its rotated bits are calculated.
    79  	bloomConfirms = 256
    80  
    81  	// bloomThrottling is the time to wait between processing two consecutive index
    82  	// sections. It's useful during chain upgrades to prevent disk overload.
    83  	bloomThrottling = 100 * time.Millisecond
    84  )