2018-02-14 15:49:11 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-10-24 16:19:09 +03:00
|
|
|
// 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 light
|
|
|
|
|
|
|
|
import (
|
2020-07-13 12:02:54 +03:00
|
|
|
"bytes"
|
2018-08-15 23:25:46 +03:00
|
|
|
"context"
|
2017-10-24 16:19:09 +03:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2018-08-15 23:25:46 +03:00
|
|
|
"fmt"
|
2017-10-24 16:19:09 +03:00
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/bitutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-05-07 14:35:06 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2017-10-24 16:19:09 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2023-05-09 10:11:04 +03:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2017-10-24 16:19:09 +03:00
|
|
|
)
|
|
|
|
|
2018-08-28 10:08:16 +03:00
|
|
|
// IndexerConfig includes a set of configs for chain indexers.
|
|
|
|
type IndexerConfig struct {
|
|
|
|
// The block frequency for creating CHTs.
|
|
|
|
ChtSize uint64
|
2018-02-11 15:57:46 +03:00
|
|
|
|
2018-08-28 10:08:16 +03:00
|
|
|
// The number of confirmations needed to generate/accept a canonical hash help trie.
|
|
|
|
ChtConfirms uint64
|
|
|
|
|
|
|
|
// The block frequency for creating new bloom bits.
|
|
|
|
BloomSize uint64
|
|
|
|
|
|
|
|
// The number of confirmation needed before a bloom section is considered probably final and its rotated bits
|
|
|
|
// are calculated.
|
|
|
|
BloomConfirms uint64
|
|
|
|
|
|
|
|
// The block frequency for creating BloomTrie.
|
|
|
|
BloomTrieSize uint64
|
|
|
|
|
|
|
|
// The number of confirmations needed to generate/accept a bloom trie.
|
|
|
|
BloomTrieConfirms uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultServerIndexerConfig wraps a set of configs as a default indexer config for server side.
|
|
|
|
DefaultServerIndexerConfig = &IndexerConfig{
|
2019-04-05 18:40:03 +03:00
|
|
|
ChtSize: params.CHTFrequency,
|
2018-08-28 10:08:16 +03:00
|
|
|
ChtConfirms: params.HelperTrieProcessConfirmations,
|
|
|
|
BloomSize: params.BloomBitsBlocks,
|
|
|
|
BloomConfirms: params.BloomConfirms,
|
|
|
|
BloomTrieSize: params.BloomTrieFrequency,
|
|
|
|
BloomTrieConfirms: params.HelperTrieProcessConfirmations,
|
|
|
|
}
|
|
|
|
// DefaultClientIndexerConfig wraps a set of configs as a default indexer config for client side.
|
|
|
|
DefaultClientIndexerConfig = &IndexerConfig{
|
2019-04-05 18:40:03 +03:00
|
|
|
ChtSize: params.CHTFrequency,
|
2018-08-28 10:08:16 +03:00
|
|
|
ChtConfirms: params.HelperTrieConfirmations,
|
|
|
|
BloomSize: params.BloomBitsBlocksClient,
|
|
|
|
BloomConfirms: params.HelperTrieConfirmations,
|
|
|
|
BloomTrieSize: params.BloomTrieFrequency,
|
|
|
|
BloomTrieConfirms: params.HelperTrieConfirmations,
|
|
|
|
}
|
|
|
|
// TestServerIndexerConfig wraps a set of configs as a test indexer config for server side.
|
|
|
|
TestServerIndexerConfig = &IndexerConfig{
|
2019-11-07 00:09:37 +03:00
|
|
|
ChtSize: 128,
|
|
|
|
ChtConfirms: 1,
|
|
|
|
BloomSize: 16,
|
|
|
|
BloomConfirms: 1,
|
|
|
|
BloomTrieSize: 128,
|
|
|
|
BloomTrieConfirms: 1,
|
2018-08-28 10:08:16 +03:00
|
|
|
}
|
|
|
|
// TestClientIndexerConfig wraps a set of configs as a test indexer config for client side.
|
|
|
|
TestClientIndexerConfig = &IndexerConfig{
|
2019-11-07 00:09:37 +03:00
|
|
|
ChtSize: 128,
|
|
|
|
ChtConfirms: 8,
|
|
|
|
BloomSize: 128,
|
|
|
|
BloomConfirms: 8,
|
|
|
|
BloomTrieSize: 128,
|
|
|
|
BloomTrieConfirms: 8,
|
2018-08-28 10:08:16 +03:00
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-21 12:29:34 +03:00
|
|
|
errNoTrustedCht = errors.New("no trusted canonical hash trie")
|
|
|
|
errNoTrustedBloomTrie = errors.New("no trusted bloom trie")
|
|
|
|
errNoHeader = errors.New("header not found")
|
2017-10-24 16:19:09 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// ChtNode structures are stored in the Canonical Hash Trie in an RLP encoded format
|
|
|
|
type ChtNode struct {
|
|
|
|
Hash common.Hash
|
|
|
|
Td *big.Int
|
|
|
|
}
|
|
|
|
|
2018-08-28 10:08:16 +03:00
|
|
|
// GetChtRoot reads the CHT root associated to the given section from the database
|
2017-10-24 16:19:09 +03:00
|
|
|
func GetChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash {
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
|
2022-10-19 10:53:09 +03:00
|
|
|
data, _ := db.Get(append(append(rawdb.ChtPrefix, encNumber[:]...), sectionHead.Bytes()...))
|
2017-10-24 16:19:09 +03:00
|
|
|
return common.BytesToHash(data)
|
|
|
|
}
|
|
|
|
|
2018-08-28 10:08:16 +03:00
|
|
|
// StoreChtRoot writes the CHT root associated to the given section into the database
|
2017-10-24 16:19:09 +03:00
|
|
|
func StoreChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash) {
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
|
2022-10-19 10:53:09 +03:00
|
|
|
db.Put(append(append(rawdb.ChtPrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes())
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-28 10:08:16 +03:00
|
|
|
// ChtIndexerBackend implements core.ChainIndexerBackend.
|
2017-10-24 16:19:09 +03:00
|
|
|
type ChtIndexerBackend struct {
|
2020-07-13 12:02:54 +03:00
|
|
|
disablePruning bool
|
2018-08-15 23:25:46 +03:00
|
|
|
diskdb, trieTable ethdb.Database
|
|
|
|
odr OdrBackend
|
2018-02-05 19:40:32 +03:00
|
|
|
triedb *trie.Database
|
2017-10-24 16:19:09 +03:00
|
|
|
section, sectionSize uint64
|
|
|
|
lastHash common.Hash
|
|
|
|
trie *trie.Trie
|
2023-05-09 10:11:04 +03:00
|
|
|
originRoot common.Hash
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-28 10:08:16 +03:00
|
|
|
// NewChtIndexer creates a Cht chain indexer
|
2020-07-13 12:02:54 +03:00
|
|
|
func NewChtIndexer(db ethdb.Database, odr OdrBackend, size, confirms uint64, disablePruning bool) *core.ChainIndexer {
|
2022-10-19 10:53:09 +03:00
|
|
|
trieTable := rawdb.NewTable(db, string(rawdb.ChtTablePrefix))
|
2018-02-05 19:40:32 +03:00
|
|
|
backend := &ChtIndexerBackend{
|
2020-07-13 12:02:54 +03:00
|
|
|
diskdb: db,
|
|
|
|
odr: odr,
|
|
|
|
trieTable: trieTable,
|
2020-11-18 12:51:33 +03:00
|
|
|
triedb: trie.NewDatabaseWithConfig(trieTable, &trie.Config{Cache: 1}), // Use a tiny cache only to keep memory down
|
2020-07-13 12:02:54 +03:00
|
|
|
sectionSize: size,
|
|
|
|
disablePruning: disablePruning,
|
2018-02-05 19:40:32 +03:00
|
|
|
}
|
2022-10-19 10:53:09 +03:00
|
|
|
return core.NewChainIndexer(db, rawdb.NewTable(db, string(rawdb.ChtIndexTablePrefix)), backend, size, confirms, time.Millisecond*100, "cht")
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-15 23:25:46 +03:00
|
|
|
// fetchMissingNodes tries to retrieve the last entry of the latest trusted CHT from the
|
|
|
|
// ODR backend in order to be able to add new entries and calculate subsequent root hashes
|
|
|
|
func (c *ChtIndexerBackend) fetchMissingNodes(ctx context.Context, section uint64, root common.Hash) error {
|
|
|
|
batch := c.trieTable.NewBatch()
|
2018-08-28 10:08:16 +03:00
|
|
|
r := &ChtRequest{ChtRoot: root, ChtNum: section - 1, BlockNum: section*c.sectionSize - 1, Config: c.odr.IndexerConfig()}
|
2018-08-15 23:25:46 +03:00
|
|
|
for {
|
|
|
|
err := c.odr.Retrieve(ctx, r)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
r.Proof.Store(batch)
|
|
|
|
return batch.Write()
|
|
|
|
case ErrNoPeers:
|
|
|
|
// if there are no peers to serve, retry later
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
// stay in the loop and try again
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
// Reset implements core.ChainIndexerBackend
|
2018-08-15 23:25:46 +03:00
|
|
|
func (c *ChtIndexerBackend) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error {
|
2023-05-11 10:19:42 +03:00
|
|
|
root := types.EmptyRootHash
|
2017-10-24 16:19:09 +03:00
|
|
|
if section > 0 {
|
2018-02-05 19:40:32 +03:00
|
|
|
root = GetChtRoot(c.diskdb, section-1, lastSectionHead)
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
var err error
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
c.trie, err = trie.New(trie.TrieID(root), c.triedb)
|
2018-08-15 23:25:46 +03:00
|
|
|
|
|
|
|
if err != nil && c.odr != nil {
|
|
|
|
err = c.fetchMissingNodes(ctx, section, root)
|
|
|
|
if err == nil {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
c.trie, err = trie.New(trie.TrieID(root), c.triedb)
|
2018-08-15 23:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
c.section = section
|
2023-05-09 10:11:04 +03:00
|
|
|
c.originRoot = root
|
2017-10-24 16:19:09 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process implements core.ChainIndexerBackend
|
2018-08-15 23:25:46 +03:00
|
|
|
func (c *ChtIndexerBackend) Process(ctx context.Context, header *types.Header) error {
|
2017-10-24 16:19:09 +03:00
|
|
|
hash, num := header.Hash(), header.Number.Uint64()
|
|
|
|
c.lastHash = hash
|
|
|
|
|
2018-05-07 14:35:06 +03:00
|
|
|
td := rawdb.ReadTd(c.diskdb, hash, num)
|
2017-10-24 16:19:09 +03:00
|
|
|
if td == nil {
|
|
|
|
panic(nil)
|
|
|
|
}
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], num)
|
|
|
|
data, _ := rlp.EncodeToBytes(ChtNode{hash, td})
|
2023-04-20 13:57:24 +03:00
|
|
|
return c.trie.Update(encNumber[:], data)
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit implements core.ChainIndexerBackend
|
|
|
|
func (c *ChtIndexerBackend) Commit() error {
|
2023-06-27 15:36:38 +03:00
|
|
|
root, nodes, err := c.trie.Commit(false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-04 11:03:20 +03:00
|
|
|
// Commit trie changes into trie database in case it's not nil.
|
|
|
|
if nodes != nil {
|
2023-07-24 13:22:09 +03:00
|
|
|
if err := c.triedb.Update(root, c.originRoot, 0, trienode.NewWithNodeSet(nodes), nil); err != nil {
|
2022-08-04 11:03:20 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-02-08 14:14:34 +03:00
|
|
|
if err := c.triedb.Commit(root, false); err != nil {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-08-04 11:03:20 +03:00
|
|
|
}
|
|
|
|
// Re-create trie with newly generated root and updated database.
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
c.trie, err = trie.New(trie.TrieID(root), c.triedb)
|
2017-10-24 16:19:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
// Pruning historical trie nodes if necessary.
|
|
|
|
if !c.disablePruning {
|
|
|
|
it := c.trieTable.NewIterator(nil, nil)
|
|
|
|
defer it.Release()
|
|
|
|
|
|
|
|
var (
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
deleted int
|
|
|
|
batch = c.trieTable.NewBatch()
|
|
|
|
t = time.Now()
|
2020-07-13 12:02:54 +03:00
|
|
|
)
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
hashes := make(map[common.Hash]struct{})
|
|
|
|
if nodes != nil {
|
|
|
|
for _, hash := range nodes.Hashes() {
|
|
|
|
hashes[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
for it.Next() {
|
2022-10-19 10:53:09 +03:00
|
|
|
trimmed := bytes.TrimPrefix(it.Key(), rawdb.ChtTablePrefix)
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
if len(trimmed) == common.HashLength {
|
|
|
|
if _, ok := hashes[common.BytesToHash(trimmed)]; !ok {
|
|
|
|
batch.Delete(trimmed)
|
|
|
|
deleted += 1
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
}
|
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("Prune historical CHT trie nodes", "deleted", deleted, "remaining", len(hashes), "elapsed", common.PrettyDuration(time.Since(t)))
|
2020-07-13 12:02:54 +03:00
|
|
|
}
|
2019-04-05 18:40:03 +03:00
|
|
|
log.Info("Storing CHT", "section", c.section, "head", fmt.Sprintf("%064x", c.lastHash), "root", fmt.Sprintf("%064x", root))
|
2018-02-05 19:40:32 +03:00
|
|
|
StoreChtRoot(c.diskdb, c.section, c.lastHash, root)
|
2017-10-24 16:19:09 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-06 18:14:55 +03:00
|
|
|
// Prune implements core.ChainIndexerBackend which deletes all chain data
|
|
|
|
// (except hash<->number mappings) older than the specified threshold.
|
2020-07-13 12:02:54 +03:00
|
|
|
func (c *ChtIndexerBackend) Prune(threshold uint64) error {
|
|
|
|
// Short circuit if the light pruning is disabled.
|
|
|
|
if c.disablePruning {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
t := time.Now()
|
|
|
|
// Always keep genesis header in database.
|
|
|
|
start, end := uint64(1), (threshold+1)*c.sectionSize
|
|
|
|
|
|
|
|
var batch = c.diskdb.NewBatch()
|
|
|
|
for {
|
|
|
|
numbers, hashes := rawdb.ReadAllCanonicalHashes(c.diskdb, start, end, 10240)
|
|
|
|
if len(numbers) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for i := 0; i < len(numbers); i++ {
|
|
|
|
// Keep hash<->number mapping in database otherwise the hash based
|
|
|
|
// API(e.g. GetReceipt, GetLogs) will be broken.
|
|
|
|
//
|
|
|
|
// Storage size wise, the size of a mapping is ~41bytes. For one
|
|
|
|
// section is about 1.3MB which is acceptable.
|
|
|
|
//
|
|
|
|
// In order to totally get rid of this index, we need an additional
|
|
|
|
// flag to specify how many historical data light client can serve.
|
|
|
|
rawdb.DeleteCanonicalHash(batch, numbers[i])
|
|
|
|
rawdb.DeleteBlockWithoutNumber(batch, hashes[i], numbers[i])
|
|
|
|
}
|
|
|
|
if batch.ValueSize() > ethdb.IdealBatchSize {
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
batch.Reset()
|
|
|
|
}
|
|
|
|
start = numbers[len(numbers)-1] + 1
|
|
|
|
}
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("Prune history headers", "threshold", threshold, "elapsed", common.PrettyDuration(time.Since(t)))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-19 09:00:21 +03:00
|
|
|
// GetBloomTrieRoot reads the BloomTrie root associated to the given section from the database
|
2017-10-24 16:19:09 +03:00
|
|
|
func GetBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash {
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
|
2022-10-19 10:53:09 +03:00
|
|
|
data, _ := db.Get(append(append(rawdb.BloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...))
|
2017-10-24 16:19:09 +03:00
|
|
|
return common.BytesToHash(data)
|
|
|
|
}
|
|
|
|
|
2022-08-19 09:00:21 +03:00
|
|
|
// StoreBloomTrieRoot writes the BloomTrie root associated to the given section into the database
|
2017-10-24 16:19:09 +03:00
|
|
|
func StoreBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash) {
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
|
2022-10-19 10:53:09 +03:00
|
|
|
db.Put(append(append(rawdb.BloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes())
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// BloomTrieIndexerBackend implements core.ChainIndexerBackend
|
|
|
|
type BloomTrieIndexerBackend struct {
|
2020-07-13 12:02:54 +03:00
|
|
|
disablePruning bool
|
2018-08-28 10:08:16 +03:00
|
|
|
diskdb, trieTable ethdb.Database
|
|
|
|
triedb *trie.Database
|
|
|
|
odr OdrBackend
|
|
|
|
section uint64
|
|
|
|
parentSize uint64
|
|
|
|
size uint64
|
|
|
|
bloomTrieRatio uint64
|
|
|
|
trie *trie.Trie
|
2023-05-09 10:11:04 +03:00
|
|
|
originRoot common.Hash
|
2018-08-28 10:08:16 +03:00
|
|
|
sectionHeads []common.Hash
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBloomTrieIndexer creates a BloomTrie chain indexer
|
2020-07-13 12:02:54 +03:00
|
|
|
func NewBloomTrieIndexer(db ethdb.Database, odr OdrBackend, parentSize, size uint64, disablePruning bool) *core.ChainIndexer {
|
2022-10-19 10:53:09 +03:00
|
|
|
trieTable := rawdb.NewTable(db, string(rawdb.BloomTrieTablePrefix))
|
2018-02-05 19:40:32 +03:00
|
|
|
backend := &BloomTrieIndexerBackend{
|
2020-07-13 12:02:54 +03:00
|
|
|
diskdb: db,
|
|
|
|
odr: odr,
|
|
|
|
trieTable: trieTable,
|
2020-11-18 12:51:33 +03:00
|
|
|
triedb: trie.NewDatabaseWithConfig(trieTable, &trie.Config{Cache: 1}), // Use a tiny cache only to keep memory down
|
2020-07-13 12:02:54 +03:00
|
|
|
parentSize: parentSize,
|
|
|
|
size: size,
|
|
|
|
disablePruning: disablePruning,
|
2018-02-05 19:40:32 +03:00
|
|
|
}
|
2018-08-28 10:08:16 +03:00
|
|
|
backend.bloomTrieRatio = size / parentSize
|
2017-10-24 16:19:09 +03:00
|
|
|
backend.sectionHeads = make([]common.Hash, backend.bloomTrieRatio)
|
2022-10-19 10:53:09 +03:00
|
|
|
return core.NewChainIndexer(db, rawdb.NewTable(db, string(rawdb.BloomTrieIndexPrefix)), backend, size, 0, time.Millisecond*100, "bloomtrie")
|
2018-08-15 23:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetchMissingNodes tries to retrieve the last entries of the latest trusted bloom trie from the
|
|
|
|
// ODR backend in order to be able to add new entries and calculate subsequent root hashes
|
|
|
|
func (b *BloomTrieIndexerBackend) fetchMissingNodes(ctx context.Context, section uint64, root common.Hash) error {
|
|
|
|
indexCh := make(chan uint, types.BloomBitLength)
|
|
|
|
type res struct {
|
|
|
|
nodes *NodeSet
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
resCh := make(chan res, types.BloomBitLength)
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
go func() {
|
|
|
|
for bitIndex := range indexCh {
|
2018-09-20 14:11:14 +03:00
|
|
|
r := &BloomRequest{BloomTrieRoot: root, BloomTrieNum: section - 1, BitIdx: bitIndex, SectionIndexList: []uint64{section - 1}, Config: b.odr.IndexerConfig()}
|
2018-08-15 23:25:46 +03:00
|
|
|
for {
|
|
|
|
if err := b.odr.Retrieve(ctx, r); err == ErrNoPeers {
|
|
|
|
// if there are no peers to serve, retry later
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
resCh <- res{nil, ctx.Err()}
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second * 10):
|
|
|
|
// stay in the loop and try again
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resCh <- res{r.Proofs, err}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
for i := uint(0); i < types.BloomBitLength; i++ {
|
|
|
|
indexCh <- i
|
|
|
|
}
|
|
|
|
close(indexCh)
|
|
|
|
batch := b.trieTable.NewBatch()
|
|
|
|
for i := uint(0); i < types.BloomBitLength; i++ {
|
|
|
|
res := <-resCh
|
|
|
|
if res.err != nil {
|
|
|
|
return res.err
|
|
|
|
}
|
|
|
|
res.nodes.Store(batch)
|
|
|
|
}
|
|
|
|
return batch.Write()
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset implements core.ChainIndexerBackend
|
2018-08-15 23:25:46 +03:00
|
|
|
func (b *BloomTrieIndexerBackend) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error {
|
2023-05-11 10:19:42 +03:00
|
|
|
root := types.EmptyRootHash
|
2017-10-24 16:19:09 +03:00
|
|
|
if section > 0 {
|
2018-02-05 19:40:32 +03:00
|
|
|
root = GetBloomTrieRoot(b.diskdb, section-1, lastSectionHead)
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
var err error
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
b.trie, err = trie.New(trie.TrieID(root), b.triedb)
|
2018-08-15 23:25:46 +03:00
|
|
|
if err != nil && b.odr != nil {
|
|
|
|
err = b.fetchMissingNodes(ctx, section, root)
|
|
|
|
if err == nil {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
b.trie, err = trie.New(trie.TrieID(root), b.triedb)
|
2018-08-15 23:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
b.section = section
|
2023-05-09 10:11:04 +03:00
|
|
|
b.originRoot = root
|
2017-10-24 16:19:09 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process implements core.ChainIndexerBackend
|
2018-08-15 23:25:46 +03:00
|
|
|
func (b *BloomTrieIndexerBackend) Process(ctx context.Context, header *types.Header) error {
|
2018-08-28 10:08:16 +03:00
|
|
|
num := header.Number.Uint64() - b.section*b.size
|
|
|
|
if (num+1)%b.parentSize == 0 {
|
|
|
|
b.sectionHeads[num/b.parentSize] = header.Hash()
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
2018-08-15 23:25:46 +03:00
|
|
|
return nil
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit implements core.ChainIndexerBackend
|
|
|
|
func (b *BloomTrieIndexerBackend) Commit() error {
|
|
|
|
var compSize, decompSize uint64
|
|
|
|
|
|
|
|
for i := uint(0); i < types.BloomBitLength; i++ {
|
|
|
|
var encKey [10]byte
|
|
|
|
binary.BigEndian.PutUint16(encKey[0:2], uint16(i))
|
|
|
|
binary.BigEndian.PutUint64(encKey[2:10], b.section)
|
|
|
|
var decomp []byte
|
|
|
|
for j := uint64(0); j < b.bloomTrieRatio; j++ {
|
2018-05-07 14:35:06 +03:00
|
|
|
data, err := rawdb.ReadBloomBits(b.diskdb, i, b.section*b.bloomTrieRatio+j, b.sectionHeads[j])
|
2017-10-24 16:19:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-28 10:08:16 +03:00
|
|
|
decompData, err2 := bitutil.DecompressBytes(data, int(b.parentSize/8))
|
2017-10-24 16:19:09 +03:00
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
decomp = append(decomp, decompData...)
|
|
|
|
}
|
|
|
|
comp := bitutil.CompressBytes(decomp)
|
|
|
|
|
|
|
|
decompSize += uint64(len(decomp))
|
|
|
|
compSize += uint64(len(comp))
|
2023-04-20 13:57:24 +03:00
|
|
|
|
|
|
|
var terr error
|
2017-10-24 16:19:09 +03:00
|
|
|
if len(comp) > 0 {
|
2023-04-20 13:57:24 +03:00
|
|
|
terr = b.trie.Update(encKey[:], comp)
|
2017-10-24 16:19:09 +03:00
|
|
|
} else {
|
2023-04-20 13:57:24 +03:00
|
|
|
terr = b.trie.Delete(encKey[:])
|
|
|
|
}
|
|
|
|
if terr != nil {
|
|
|
|
return terr
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-27 15:36:38 +03:00
|
|
|
root, nodes, err := b.trie.Commit(false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-04 11:03:20 +03:00
|
|
|
// Commit trie changes into trie database in case it's not nil.
|
|
|
|
if nodes != nil {
|
2023-07-24 13:22:09 +03:00
|
|
|
if err := b.triedb.Update(root, b.originRoot, 0, trienode.NewWithNodeSet(nodes), nil); err != nil {
|
2022-08-04 11:03:20 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-02-08 14:14:34 +03:00
|
|
|
if err := b.triedb.Commit(root, false); err != nil {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-08-04 11:03:20 +03:00
|
|
|
}
|
|
|
|
// Re-create trie with newly generated root and updated database.
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
b.trie, err = trie.New(trie.TrieID(root), b.triedb)
|
2017-10-24 16:19:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
// Pruning historical trie nodes if necessary.
|
|
|
|
if !b.disablePruning {
|
|
|
|
it := b.trieTable.NewIterator(nil, nil)
|
|
|
|
defer it.Release()
|
|
|
|
|
|
|
|
var (
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
deleted int
|
|
|
|
batch = b.trieTable.NewBatch()
|
|
|
|
t = time.Now()
|
2020-07-13 12:02:54 +03:00
|
|
|
)
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
hashes := make(map[common.Hash]struct{})
|
|
|
|
if nodes != nil {
|
|
|
|
for _, hash := range nodes.Hashes() {
|
|
|
|
hashes[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
for it.Next() {
|
2022-10-19 10:53:09 +03:00
|
|
|
trimmed := bytes.TrimPrefix(it.Key(), rawdb.BloomTrieTablePrefix)
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
if len(trimmed) == common.HashLength {
|
|
|
|
if _, ok := hashes[common.BytesToHash(trimmed)]; !ok {
|
|
|
|
batch.Delete(trimmed)
|
|
|
|
deleted += 1
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
}
|
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 11:01:02 +03:00
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("Prune historical bloom trie nodes", "deleted", deleted, "remaining", len(hashes), "elapsed", common.PrettyDuration(time.Since(t)))
|
2020-07-13 12:02:54 +03:00
|
|
|
}
|
2018-02-05 19:40:32 +03:00
|
|
|
sectionHead := b.sectionHeads[b.bloomTrieRatio-1]
|
|
|
|
StoreBloomTrieRoot(b.diskdb, b.section, sectionHead, root)
|
2020-07-13 12:02:54 +03:00
|
|
|
log.Info("Storing bloom trie", "section", b.section, "head", fmt.Sprintf("%064x", sectionHead), "root", fmt.Sprintf("%064x", root), "compression", float64(compSize)/float64(decompSize))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prune implements core.ChainIndexerBackend which deletes all
|
|
|
|
// bloombits which older than the specified threshold.
|
|
|
|
func (b *BloomTrieIndexerBackend) Prune(threshold uint64) error {
|
|
|
|
// Short circuit if the light pruning is disabled.
|
|
|
|
if b.disablePruning {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
start := time.Now()
|
|
|
|
for i := uint(0); i < types.BloomBitLength; i++ {
|
|
|
|
rawdb.DeleteBloombits(b.diskdb, i, 0, threshold*b.bloomTrieRatio+b.bloomTrieRatio)
|
|
|
|
}
|
|
|
|
log.Debug("Prune history bloombits", "threshold", threshold, "elapsed", common.PrettyDuration(time.Since(start)))
|
2017-10-24 16:19:09 +03:00
|
|
|
return nil
|
|
|
|
}
|