bloombits.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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"
  20. "github.com/ethereum/go-ethereum/common/bitutil"
  21. "github.com/ethereum/go-ethereum/core"
  22. "github.com/ethereum/go-ethereum/core/bloombits"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. const (
  28. // bloomServiceThreads is the number of goroutines used globally by an Ethereum
  29. // instance to service bloombits lookups for all running filters.
  30. bloomServiceThreads = 16
  31. // bloomFilterThreads is the number of goroutines used locally per filter to
  32. // multiplex requests onto the global servicing goroutines.
  33. bloomFilterThreads = 3
  34. // bloomRetrievalBatch is the maximum number of bloom bit retrievals to service
  35. // in a single batch.
  36. bloomRetrievalBatch = 16
  37. // bloomRetrievalWait is the maximum time to wait for enough bloom bit requests
  38. // to accumulate request an entire batch (avoiding hysteresis).
  39. bloomRetrievalWait = time.Duration(0)
  40. )
  41. // startBloomHandlers starts a batch of goroutines to accept bloom bit database
  42. // retrievals from possibly a range of filters and serving the data to satisfy.
  43. func (eth *Ethereum) startBloomHandlers() {
  44. for i := 0; i < bloomServiceThreads; i++ {
  45. go func() {
  46. for {
  47. select {
  48. case <-eth.shutdownChan:
  49. return
  50. case request := <-eth.bloomRequests:
  51. task := <-request
  52. task.Bitsets = make([][]byte, len(task.Sections))
  53. for i, section := range task.Sections {
  54. head := core.GetCanonicalHash(eth.chainDb, (section+1)*params.BloomBitsBlocks-1)
  55. blob, err := bitutil.DecompressBytes(core.GetBloomBits(eth.chainDb, task.Bit, section, head), int(params.BloomBitsBlocks)/8)
  56. if err != nil {
  57. panic(err)
  58. }
  59. task.Bitsets[i] = blob
  60. }
  61. request <- task
  62. }
  63. }
  64. }()
  65. }
  66. }
  67. const (
  68. // bloomConfirms is the number of confirmation blocks before a bloom section is
  69. // considered probably final and its rotated bits are calculated.
  70. bloomConfirms = 256
  71. // bloomThrottling is the time to wait between processing two consecutive index
  72. // sections. It's useful during chain upgrades to prevent disk overload.
  73. bloomThrottling = 100 * time.Millisecond
  74. )
  75. // BloomIndexer implements a core.ChainIndexer, building up a rotated bloom bits index
  76. // for the Ethereum header bloom filters, permitting blazing fast filtering.
  77. type BloomIndexer struct {
  78. size uint64 // section size to generate bloombits for
  79. db ethdb.Database // database instance to write index data and metadata into
  80. gen *bloombits.Generator // generator to rotate the bloom bits crating the bloom index
  81. section uint64 // Section is the section number being processed currently
  82. head common.Hash // Head is the hash of the last header processed
  83. }
  84. // NewBloomIndexer returns a chain indexer that generates bloom bits data for the
  85. // canonical chain for fast logs filtering.
  86. func NewBloomIndexer(db ethdb.Database, size uint64) *core.ChainIndexer {
  87. backend := &BloomIndexer{
  88. db: db,
  89. size: size,
  90. }
  91. table := ethdb.NewTable(db, string(core.BloomBitsIndexPrefix))
  92. return core.NewChainIndexer(db, table, backend, size, bloomConfirms, bloomThrottling, "bloombits")
  93. }
  94. // Reset implements core.ChainIndexerBackend, starting a new bloombits index
  95. // section.
  96. func (b *BloomIndexer) Reset(section uint64) {
  97. gen, err := bloombits.NewGenerator(uint(b.size))
  98. if err != nil {
  99. panic(err)
  100. }
  101. b.gen, b.section, b.head = gen, section, common.Hash{}
  102. }
  103. // Process implements core.ChainIndexerBackend, adding a new header's bloom into
  104. // the index.
  105. func (b *BloomIndexer) Process(header *types.Header) {
  106. b.gen.AddBloom(header.Bloom)
  107. b.head = header.Hash()
  108. }
  109. // Commit implements core.ChainIndexerBackend, finalizing the bloom section and
  110. // writing it out into the database.
  111. func (b *BloomIndexer) Commit() error {
  112. batch := b.db.NewBatch()
  113. for i := 0; i < types.BloomBitLength; i++ {
  114. bits, err := b.gen.Bitset(uint(i))
  115. if err != nil {
  116. return err
  117. }
  118. core.WriteBloomBits(batch, uint(i), b.section, b.head, bitutil.CompressBytes(bits))
  119. }
  120. return batch.Write()
  121. }