bloombits.go 5.0 KB

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