mirror of
https://github.com/luxfi/coreth.git
synced 2026-10-30 17:00:51 +00:00
123 lines
4.4 KiB
Go
123 lines
4.4 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
//
|
|
// This file is a derived work, based on the go-ethereum library whose original
|
|
// notices appear below.
|
|
//
|
|
// It is distributed under a license compatible with the licensing terms of the
|
|
// original code from which it is derived.
|
|
//
|
|
// Much love to the original authors for their work.
|
|
// **********
|
|
// Copyright 2021 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package core
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/luxfi/coreth/core/bloombits"
|
|
"github.com/luxfi/geth/common"
|
|
"github.com/luxfi/geth/core/rawdb"
|
|
"github.com/luxfi/geth/core/types"
|
|
"github.com/luxfi/geth/ethdb"
|
|
)
|
|
|
|
const (
|
|
// bloomThrottling is the time to wait between processing two consecutive index
|
|
// sections. It's useful during chain upgrades to prevent disk overload.
|
|
bloomThrottling = 100 * time.Millisecond
|
|
)
|
|
|
|
// bloomBitsIndexPrefix is the prefix for bloom bits index keys.
|
|
// This was removed from geth rawdb, so we define it here.
|
|
var bloomBitsIndexPrefix = []byte("iB")
|
|
|
|
// BloomIndexer implements a core.ChainIndexer, building up a rotated bloom bits index
|
|
// for the Ethereum header bloom filters, permitting blazing fast filtering.
|
|
type BloomIndexer struct {
|
|
size uint64 // section size to generate bloombits for
|
|
db ethdb.Database // database instance to write index data and metadata into
|
|
gen *bloombits.Generator // generator to rotate the bloom bits crating the bloom index
|
|
section uint64 // Section is the section number being processed currently
|
|
head common.Hash // Head is the hash of the last header processed
|
|
}
|
|
|
|
// NewBloomIndexer returns a chain indexer that generates bloom bits data for the
|
|
// canonical chain for fast logs filtering.
|
|
func NewBloomIndexer(db ethdb.Database, size, confirms uint64) *ChainIndexer {
|
|
backend := &BloomIndexer{
|
|
db: db,
|
|
size: size,
|
|
}
|
|
table := rawdb.NewTable(db, string(bloomBitsIndexPrefix))
|
|
|
|
return NewChainIndexer(db, table, backend, size, confirms, bloomThrottling, "bloombits")
|
|
}
|
|
|
|
// Reset implements core.ChainIndexerBackend, starting a new bloombits index
|
|
// section.
|
|
func (b *BloomIndexer) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error {
|
|
gen, err := bloombits.NewGenerator(uint(b.size))
|
|
b.gen, b.section, b.head = gen, section, common.Hash{}
|
|
return err
|
|
}
|
|
|
|
// Process implements core.ChainIndexerBackend, adding a new header's bloom into
|
|
// the index.
|
|
func (b *BloomIndexer) Process(ctx context.Context, header *types.Header) error {
|
|
b.gen.AddBloom(uint(header.Number.Uint64()-b.section*b.size), header.Bloom)
|
|
b.head = header.Hash()
|
|
return nil
|
|
}
|
|
|
|
// Commit implements core.ChainIndexerBackend, finalizing the bloom section and
|
|
// writing it out into the database.
|
|
func (b *BloomIndexer) Commit() error {
|
|
batch := b.db.NewBatchWithSize((int(b.size) / 8) * types.BloomBitLength)
|
|
for i := 0; i < types.BloomBitLength; i++ {
|
|
bits, err := b.gen.Bitset(uint(i))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
writeBloomBits(batch, uint(i), b.section, b.head, bits)
|
|
}
|
|
return batch.Write()
|
|
}
|
|
|
|
// writeBloomBits stores the bloom bits for a section.
|
|
func writeBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) {
|
|
key := append(bloomBitsIndexPrefix, make([]byte, 10)...)
|
|
key[2] = byte(bit >> 8)
|
|
key[3] = byte(bit)
|
|
key[4] = byte(section >> 56)
|
|
key[5] = byte(section >> 48)
|
|
key[6] = byte(section >> 40)
|
|
key[7] = byte(section >> 32)
|
|
key[8] = byte(section >> 24)
|
|
key[9] = byte(section >> 16)
|
|
key[10] = byte(section >> 8)
|
|
key[11] = byte(section)
|
|
db.Put(key, bits)
|
|
}
|
|
|
|
// Prune returns an empty error since we don't support pruning here.
|
|
func (b *BloomIndexer) Prune(threshold uint64) error {
|
|
return nil
|
|
}
|