bloom_indexer.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, adding a new header's bloom into
  59. // the index.
  60. func (b *BloomIndexer) Process(ctx context.Context, header *types.Header) error {
  61. b.gen.AddBloom(uint(header.Number.Uint64()-b.section*b.size), header.Bloom)
  62. b.head = header.Hash()
  63. return nil
  64. }
  65. // Commit implements core.ChainIndexerBackend, finalizing the bloom section and
  66. // writing it out into the database.
  67. func (b *BloomIndexer) Commit() error {
  68. batch := b.db.NewBatch()
  69. for i := 0; i < types.BloomBitLength; i++ {
  70. bits, err := b.gen.Bitset(uint(i))
  71. if err != nil {
  72. return err
  73. }
  74. rawdb.WriteBloomBits(batch, uint(i), b.section, b.head, bitutil.CompressBytes(bits))
  75. }
  76. return batch.Write()
  77. }
  78. // Prune returns an empty error since we don't support pruning here.
  79. func (b *BloomIndexer) Prune(threshold uint64) error {
  80. return nil
  81. }