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(sectionSize uint64) {
  39. for i := 0; i < bloomServiceThreads; i++ {
  40. go func() {
  41. defer eth.wg.Done()
  42. for {
  43. select {
  44. case <-eth.closeCh:
  45. return
  46. case request := <-eth.bloomRequests:
  47. task := <-request
  48. task.Bitsets = make([][]byte, len(task.Sections))
  49. compVectors, err := light.GetBloomBits(task.Context, eth.odr, task.Bit, task.Sections)
  50. if err == nil {
  51. for i := range task.Sections {
  52. if blob, err := bitutil.DecompressBytes(compVectors[i], int(sectionSize/8)); err == nil {
  53. task.Bitsets[i] = blob
  54. } else {
  55. task.Error = err
  56. }
  57. }
  58. } else {
  59. task.Error = err
  60. }
  61. request <- task
  62. }
  63. }
  64. }()
  65. }
  66. }