bloombits.go 2.5 KB

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