bloombits.go 2.5 KB

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