bloombits.go 2.4 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 les
  17. import (
  18. "time"
  19. "github.com/ethereum/go-ethereum/common/bitutil"
  20. "github.com/ethereum/go-ethereum/light"
  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.Microsecond * 100
  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 *LightEthereum) startBloomHandlers() {
  39. for i := 0; i < bloomServiceThreads; i++ {
  40. go func() {
  41. for {
  42. select {
  43. case <-eth.shutdownChan:
  44. return
  45. case request := <-eth.bloomRequests:
  46. task := <-request
  47. task.Bitsets = make([][]byte, len(task.Sections))
  48. compVectors, err := light.GetBloomBits(task.Context, eth.odr, task.Bit, task.Sections)
  49. if err == nil {
  50. for i := range task.Sections {
  51. if blob, err := bitutil.DecompressBytes(compVectors[i], int(light.BloomTrieFrequency/8)); err == nil {
  52. task.Bitsets[i] = blob
  53. } else {
  54. task.Error = err
  55. }
  56. }
  57. } else {
  58. task.Error = err
  59. }
  60. request <- task
  61. }
  62. }
  63. }()
  64. }
  65. }