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