2022-05-06 19:28:42 +08:00
|
|
|
// Copyright 2022 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 rawdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// freezerRecheckInterval is the frequency to check the key-value database for
|
|
|
|
// chain progression that might permit new blocks to be frozen into immutable
|
|
|
|
// storage.
|
|
|
|
freezerRecheckInterval = time.Minute
|
|
|
|
|
|
|
|
// freezerBatchLimit is the maximum number of blocks to freeze in one batch
|
|
|
|
// before doing an fsync and deleting it from the key-value store.
|
|
|
|
freezerBatchLimit = 30000
|
|
|
|
)
|
|
|
|
|
|
|
|
// chainFreezer is a wrapper of freezer with additional chain freezing feature.
|
|
|
|
// The background thread will keep moving ancient chain segments from key-value
|
|
|
|
// database to flat files for saving space on live database.
|
|
|
|
type chainFreezer struct {
|
2023-03-21 19:10:23 +08:00
|
|
|
threshold atomic.Uint64 // Number of recent blocks not to freeze (params.FullImmutabilityThreshold apart from tests)
|
2022-05-06 19:28:42 +08:00
|
|
|
|
|
|
|
*Freezer
|
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
trigger chan chan struct{} // Manual blocking freeze trigger, test determinism
|
|
|
|
}
|
|
|
|
|
|
|
|
// newChainFreezer initializes the freezer for ancient chain data.
|
2023-02-21 13:10:01 +02:00
|
|
|
func newChainFreezer(datadir string, namespace string, readonly bool) (*chainFreezer, error) {
|
|
|
|
freezer, err := NewChainFreezer(datadir, namespace, readonly)
|
2022-05-06 19:28:42 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-21 19:10:23 +08:00
|
|
|
cf := chainFreezer{
|
|
|
|
Freezer: freezer,
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
trigger: make(chan chan struct{}),
|
|
|
|
}
|
|
|
|
cf.threshold.Store(params.FullImmutabilityThreshold)
|
|
|
|
return &cf, nil
|
2022-05-06 19:28:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the chain freezer instance and terminates the background thread.
|
|
|
|
func (f *chainFreezer) Close() error {
|
|
|
|
select {
|
|
|
|
case <-f.quit:
|
|
|
|
default:
|
|
|
|
close(f.quit)
|
|
|
|
}
|
|
|
|
f.wg.Wait()
|
2023-01-16 03:57:27 -05:00
|
|
|
return f.Freezer.Close()
|
2022-05-06 19:28:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// freeze is a background thread that periodically checks the blockchain for any
|
|
|
|
// import progress and moves ancient data from the fast database into the freezer.
|
|
|
|
//
|
|
|
|
// This functionality is deliberately broken off from block importing to avoid
|
|
|
|
// incurring additional data shuffling delays on block propagation.
|
|
|
|
func (f *chainFreezer) freeze(db ethdb.KeyValueStore) {
|
|
|
|
var (
|
|
|
|
backoff bool
|
|
|
|
triggered chan struct{} // Used in tests
|
2023-01-13 18:55:50 +08:00
|
|
|
nfdb = &nofreezedb{KeyValueStore: db}
|
2022-05-06 19:28:42 +08:00
|
|
|
)
|
2022-09-16 17:33:48 +08:00
|
|
|
timer := time.NewTimer(freezerRecheckInterval)
|
|
|
|
defer timer.Stop()
|
2023-01-13 18:55:50 +08:00
|
|
|
|
2022-05-06 19:28:42 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-f.quit:
|
|
|
|
log.Info("Freezer shutting down")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if backoff {
|
|
|
|
// If we were doing a manual trigger, notify it
|
|
|
|
if triggered != nil {
|
|
|
|
triggered <- struct{}{}
|
|
|
|
triggered = nil
|
|
|
|
}
|
|
|
|
select {
|
2022-09-16 17:33:48 +08:00
|
|
|
case <-timer.C:
|
2022-05-06 19:28:42 +08:00
|
|
|
backoff = false
|
2022-09-16 17:33:48 +08:00
|
|
|
timer.Reset(freezerRecheckInterval)
|
2022-05-06 19:28:42 +08:00
|
|
|
case triggered = <-f.trigger:
|
|
|
|
backoff = false
|
|
|
|
case <-f.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Retrieve the freezing threshold.
|
|
|
|
hash := ReadHeadBlockHash(nfdb)
|
|
|
|
if hash == (common.Hash{}) {
|
|
|
|
log.Debug("Current full block hash unavailable") // new chain, empty database
|
|
|
|
backoff = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
number := ReadHeaderNumber(nfdb, hash)
|
2023-03-21 19:10:23 +08:00
|
|
|
threshold := f.threshold.Load()
|
|
|
|
frozen := f.frozen.Load()
|
2022-05-06 19:28:42 +08:00
|
|
|
switch {
|
|
|
|
case number == nil:
|
|
|
|
log.Error("Current full block number unavailable", "hash", hash)
|
|
|
|
backoff = true
|
|
|
|
continue
|
|
|
|
|
|
|
|
case *number < threshold:
|
2023-12-20 09:23:48 -07:00
|
|
|
log.Debug("Current full block not old enough to freeze", "number", *number, "hash", hash, "delay", threshold)
|
2022-05-06 19:28:42 +08:00
|
|
|
backoff = true
|
|
|
|
continue
|
|
|
|
|
|
|
|
case *number-threshold <= frozen:
|
|
|
|
log.Debug("Ancient blocks frozen already", "number", *number, "hash", hash, "frozen", frozen)
|
|
|
|
backoff = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
head := ReadHeader(nfdb, hash, *number)
|
|
|
|
if head == nil {
|
|
|
|
log.Error("Current full block unavailable", "number", *number, "hash", hash)
|
|
|
|
backoff = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seems we have data ready to be frozen, process in usable batches
|
|
|
|
var (
|
|
|
|
start = time.Now()
|
|
|
|
first, _ = f.Ancients()
|
|
|
|
limit = *number - threshold
|
|
|
|
)
|
|
|
|
if limit-first > freezerBatchLimit {
|
|
|
|
limit = first + freezerBatchLimit
|
|
|
|
}
|
|
|
|
ancients, err := f.freezeRange(nfdb, first, limit)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error in block freeze operation", "err", err)
|
|
|
|
backoff = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Batch of blocks have been frozen, flush them before wiping from leveldb
|
|
|
|
if err := f.Sync(); err != nil {
|
|
|
|
log.Crit("Failed to flush frozen tables", "err", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wipe out all data from the active database
|
|
|
|
batch := db.NewBatch()
|
|
|
|
for i := 0; i < len(ancients); i++ {
|
|
|
|
// Always keep the genesis block in active database
|
|
|
|
if first+uint64(i) != 0 {
|
|
|
|
DeleteBlockWithoutNumber(batch, ancients[i], first+uint64(i))
|
|
|
|
DeleteCanonicalHash(batch, first+uint64(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
log.Crit("Failed to delete frozen canonical blocks", "err", err)
|
|
|
|
}
|
|
|
|
batch.Reset()
|
|
|
|
|
|
|
|
// Wipe out side chains also and track dangling side chains
|
|
|
|
var dangling []common.Hash
|
2023-03-21 19:10:23 +08:00
|
|
|
frozen = f.frozen.Load() // Needs reload after during freezeRange
|
2022-05-06 19:28:42 +08:00
|
|
|
for number := first; number < frozen; number++ {
|
|
|
|
// Always keep the genesis block in active database
|
|
|
|
if number != 0 {
|
|
|
|
dangling = ReadAllHashes(db, number)
|
|
|
|
for _, hash := range dangling {
|
|
|
|
log.Trace("Deleting side chain", "number", number, "hash", hash)
|
|
|
|
DeleteBlock(batch, hash, number)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
log.Crit("Failed to delete frozen side blocks", "err", err)
|
|
|
|
}
|
|
|
|
batch.Reset()
|
|
|
|
|
2023-09-29 09:52:22 +02:00
|
|
|
// Step into the future and delete any dangling side chains
|
2022-05-06 19:28:42 +08:00
|
|
|
if frozen > 0 {
|
|
|
|
tip := frozen
|
|
|
|
for len(dangling) > 0 {
|
|
|
|
drop := make(map[common.Hash]struct{})
|
|
|
|
for _, hash := range dangling {
|
|
|
|
log.Debug("Dangling parent from Freezer", "number", tip-1, "hash", hash)
|
|
|
|
drop[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
children := ReadAllHashes(db, tip)
|
|
|
|
for i := 0; i < len(children); i++ {
|
|
|
|
// Dig up the child and ensure it's dangling
|
|
|
|
child := ReadHeader(nfdb, children[i], tip)
|
|
|
|
if child == nil {
|
|
|
|
log.Error("Missing dangling header", "number", tip, "hash", children[i])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := drop[child.ParentHash]; !ok {
|
|
|
|
children = append(children[:i], children[i+1:]...)
|
|
|
|
i--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Delete all block data associated with the child
|
|
|
|
log.Debug("Deleting dangling block", "number", tip, "hash", children[i], "parent", child.ParentHash)
|
|
|
|
DeleteBlock(batch, children[i], tip)
|
|
|
|
}
|
|
|
|
dangling = children
|
|
|
|
tip++
|
|
|
|
}
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
log.Crit("Failed to delete dangling side blocks", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log something friendly for the user
|
|
|
|
context := []interface{}{
|
|
|
|
"blocks", frozen - first, "elapsed", common.PrettyDuration(time.Since(start)), "number", frozen - 1,
|
|
|
|
}
|
|
|
|
if n := len(ancients); n > 0 {
|
|
|
|
context = append(context, []interface{}{"hash", ancients[n-1]}...)
|
|
|
|
}
|
2022-08-01 15:13:25 +03:00
|
|
|
log.Debug("Deep froze chain segment", context...)
|
2022-05-06 19:28:42 +08:00
|
|
|
|
|
|
|
// Avoid database thrashing with tiny writes
|
|
|
|
if frozen-first < freezerBatchLimit {
|
|
|
|
backoff = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *chainFreezer) freezeRange(nfdb *nofreezedb, number, limit uint64) (hashes []common.Hash, err error) {
|
|
|
|
hashes = make([]common.Hash, 0, limit-number)
|
|
|
|
|
|
|
|
_, err = f.ModifyAncients(func(op ethdb.AncientWriteOp) error {
|
|
|
|
for ; number <= limit; number++ {
|
|
|
|
// Retrieve all the components of the canonical block.
|
|
|
|
hash := ReadCanonicalHash(nfdb, number)
|
|
|
|
if hash == (common.Hash{}) {
|
|
|
|
return fmt.Errorf("canonical hash missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
header := ReadHeaderRLP(nfdb, hash, number)
|
|
|
|
if len(header) == 0 {
|
|
|
|
return fmt.Errorf("block header missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
body := ReadBodyRLP(nfdb, hash, number)
|
|
|
|
if len(body) == 0 {
|
|
|
|
return fmt.Errorf("block body missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
receipts := ReadReceiptsRLP(nfdb, hash, number)
|
|
|
|
if len(receipts) == 0 {
|
|
|
|
return fmt.Errorf("block receipts missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
td := ReadTdRLP(nfdb, hash, number)
|
|
|
|
if len(td) == 0 {
|
|
|
|
return fmt.Errorf("total difficulty missing, can't freeze block %d", number)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the batch.
|
2023-02-21 12:17:34 +02:00
|
|
|
if err := op.AppendRaw(ChainFreezerHashTable, number, hash[:]); err != nil {
|
2022-05-06 19:28:42 +08:00
|
|
|
return fmt.Errorf("can't write hash to Freezer: %v", err)
|
|
|
|
}
|
2023-02-21 12:17:34 +02:00
|
|
|
if err := op.AppendRaw(ChainFreezerHeaderTable, number, header); err != nil {
|
2022-05-06 19:28:42 +08:00
|
|
|
return fmt.Errorf("can't write header to Freezer: %v", err)
|
|
|
|
}
|
2023-02-21 12:17:34 +02:00
|
|
|
if err := op.AppendRaw(ChainFreezerBodiesTable, number, body); err != nil {
|
2022-05-06 19:28:42 +08:00
|
|
|
return fmt.Errorf("can't write body to Freezer: %v", err)
|
|
|
|
}
|
2023-02-21 12:17:34 +02:00
|
|
|
if err := op.AppendRaw(ChainFreezerReceiptTable, number, receipts); err != nil {
|
2022-05-06 19:28:42 +08:00
|
|
|
return fmt.Errorf("can't write receipts to Freezer: %v", err)
|
|
|
|
}
|
2023-02-21 12:17:34 +02:00
|
|
|
if err := op.AppendRaw(ChainFreezerDifficultyTable, number, td); err != nil {
|
2022-05-06 19:28:42 +08:00
|
|
|
return fmt.Errorf("can't write td to Freezer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hashes = append(hashes, hash)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return hashes, err
|
|
|
|
}
|