bloom_indexer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2021 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 core
  17. import (
  18. "context"
  19. "time"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/bitutil"
  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. )
  27. const (
  28. // bloomThrottling is the time to wait between processing two consecutive index
  29. // sections. It's useful during chain upgrades to prevent disk overload.
  30. bloomThrottling = 100 * time.Millisecond
  31. )
  32. // BloomIndexer implements a core.ChainIndexer, building up a rotated bloom bits index
  33. // for the Ethereum header bloom filters, permitting blazing fast filtering.
  34. type BloomIndexer struct {
  35. size uint64 // section size to generate bloombits for
  36. db ethdb.Database // database instance to write index data and metadata into
  37. gen *bloombits.Generator // generator to rotate the bloom bits crating the bloom index
  38. section uint64 // Section is the section number being processed currently
  39. head common.Hash // Head is the hash of the last header processed
  40. }
  41. // NewBloomIndexer returns a chain indexer that generates bloom bits data for the
  42. // canonical chain for fast logs filtering.
  43. func NewBloomIndexer(db ethdb.Database, size, confirms uint64) *ChainIndexer {
  44. backend := &BloomIndexer{
  45. db: db,
  46. size: size,
  47. }
  48. table := rawdb.NewTable(db, string(rawdb.BloomBitsIndexPrefix))
  49. return NewChainIndexer(db, table, backend, size, confirms, bloomThrottling, "bloombits")
  50. }
  51. // Reset implements core.ChainIndexerBackend, starting a new bloombits index
  52. // section.
  53. func (b *BloomIndexer) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error {
  54. gen, err := bloombits.NewGenerator(uint(b.size))
  55. b.gen, b.section, b.head = gen, section, common.Hash{}
  56. return err
  57. }
  58. // Process implements core.ChainIndexerBackend, executes an Or operation on header.bloom and private bloom
  59. // (header.bloom | private bloom) and adds to index
  60. func (b *BloomIndexer) Process(ctx context.Context, header *types.Header) error {
  61. publicBloom := header.Bloom
  62. privateBloom := rawdb.GetPrivateBlockBloom(b.db, header.Number.Uint64())
  63. publicBloom.OrBloom(privateBloom.Bytes())
  64. b.gen.AddBloom(uint(header.Number.Uint64()-b.section*b.size), publicBloom)
  65. b.head = header.Hash()
  66. return nil
  67. }
  68. // Commit implements core.ChainIndexerBackend, finalizing the bloom section and
  69. // writing it out into the database.
  70. func (b *BloomIndexer) Commit() error {
  71. batch := b.db.NewBatch()
  72. for i := 0; i < types.BloomBitLength; i++ {
  73. bits, err := b.gen.Bitset(uint(i))
  74. if err != nil {
  75. return err
  76. }
  77. rawdb.WriteBloomBits(batch, uint(i), b.section, b.head, bitutil.CompressBytes(bits))
  78. }
  79. return batch.Write()
  80. }
  81. // Prune returns an empty error since we don't support pruning here.
  82. func (b *BloomIndexer) Prune(threshold uint64) error {
  83. return nil
  84. }