Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

eth: Add --historymode flag for configurable blockchain history pruning FIX #31277 #31325

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ if one is set. Otherwise it prints the genesis from the datadir.`,
Flags: slices.Concat([]cli.Flag{
utils.CacheFlag,
utils.SyncModeFlag,
utils.StateSchemeFlag,
utils.GCModeFlag,
utils.HistoryModeFlag,
utils.SnapshotFlag,
utils.CacheDatabaseFlag,
utils.CacheGCFlag,
Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ var (
utils.SyncModeFlag,
utils.SyncTargetFlag,
utils.ExitWhenSyncedFlag,
utils.StateSchemeFlag,
utils.GCModeFlag,
utils.HistoryModeFlag,
utils.SnapshotFlag,
utils.TxLookupLimitFlag, // deprecated
utils.TransactionHistoryFlag,
Expand Down
34 changes: 34 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ var (
Value: "full",
Category: flags.StateCategory,
}
HistoryModeFlag = &cli.StringFlag{
Name: "historymode",
Usage: `Blockchain history pruning mode ("full" keeps all history, "pruned" keeps only recent history according to StateHistory and TransactionHistory settings)`,
Value: "full",
Category: flags.StateCategory,
}
StateSchemeFlag = &cli.StringFlag{
Name: "state.scheme",
Usage: "Scheme to use for storing ethereum state ('hash' or 'path')",
Expand Down Expand Up @@ -1587,6 +1593,34 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if ctx.IsSet(GCModeFlag.Name) {
cfg.NoPruning = ctx.String(GCModeFlag.Name) == "archive"
}
if historymode := ctx.String(HistoryModeFlag.Name); historymode != "full" && historymode != "pruned" {
Fatalf("--%s must be either 'full' or 'pruned'", HistoryModeFlag.Name)
}
if ctx.IsSet(HistoryModeFlag.Name) {
if ctx.String(HistoryModeFlag.Name) == "full" {
cfg.HistoryMode = ethconfig.AllHistory
} else {
cfg.HistoryMode = ethconfig.PrunedHistory
}
} else {
// Set the default value if not specified
// Future versions of Geth will default to pruned, but for now we use full
cfg.HistoryMode = ethconfig.AllHistory
}

// If gcmode is set to archive, ensure historymode is set to full
if ctx.IsSet(GCModeFlag.Name) && ctx.String(GCModeFlag.Name) == "archive" {
cfg.HistoryMode = ethconfig.AllHistory
log.Info("Setting historymode to 'full' because gcmode is 'archive'")
}

// Conversely, if historymode is set to pruned, gcmode can't be archive
if ctx.IsSet(HistoryModeFlag.Name) && ctx.String(HistoryModeFlag.Name) == "pruned" && ctx.String(GCModeFlag.Name) == "archive" {
log.Warn("Conflicting flags: historymode=pruned and gcmode=archive cannot be used together")
log.Warn("Setting gcmode to 'full' to be compatible with historymode=pruned")
cfg.NoPruning = false
}

if ctx.IsSet(CacheNoPrefetchFlag.Name) {
cfg.NoPrefetch = ctx.Bool(CacheNoPrefetchFlag.Name)
}
Expand Down
36 changes: 34 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,25 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
TrieTimeLimit: config.TrieTimeout,
SnapshotLimit: config.SnapshotCache,
Preimages: config.Preimages,
StateHistory: config.StateHistory,
StateScheme: scheme,
}
)

// Configure state history based on history mode
if config.HistoryMode == ethconfig.AllHistory {
// In all history mode, retain all state history if specified as 0,
// otherwise use the configured value
cacheConfig.StateHistory = config.StateHistory
} else if config.HistoryMode == ethconfig.PrunedHistory {
// In pruned history mode, ensure state history is limited
if config.StateHistory == 0 {
// If it's set to 0 (unlimited), use a default value for pruned mode
cacheConfig.StateHistory = 2350000 // Same default as TransactionHistory
} else {
cacheConfig.StateHistory = config.StateHistory
}
}

if config.VMTrace != "" {
traceConfig := json.RawMessage("{}")
if config.VMTraceJsonConfig != "" {
Expand All @@ -220,7 +235,24 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
if config.OverrideVerkle != nil {
overrides.OverrideVerkle = config.OverrideVerkle
}
eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, &config.TransactionHistory)

// Set transaction history based on history mode
var txHistoryLimit *uint64
if config.HistoryMode == ethconfig.AllHistory {
// In all history mode, use the configured transaction history
txHistoryLimit = &config.TransactionHistory
} else if config.HistoryMode == ethconfig.PrunedHistory {
// In pruned history mode, ensure transaction history is limited
// Make a copy to avoid modifying the original config
limit := config.TransactionHistory
if limit == 0 {
// If it's set to 0 (unlimited), use a reasonable default for pruned mode
limit = 2350000 // Default value in ethconfig.Defaults
}
txHistoryLimit = &limit
}

eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, txHistoryLimit)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var FullNodeGPO = gasprice.Config{
// Defaults contains default settings for use on the Ethereum main net.
var Defaults = Config{
SyncMode: SnapSync,
HistoryMode: AllHistory,
NetworkId: 0, // enable auto configuration of networkID == chainID
TxLookupLimit: 2350000,
TransactionHistory: 2350000,
Expand Down Expand Up @@ -80,6 +81,8 @@ type Config struct {
// zero, the chain ID is used as network ID.
NetworkId uint64
SyncMode SyncMode
// HistoryMode defines the pruning mode for historical blockchain data
HistoryMode HistoryMode

// This can be set to list of enrtree:// URLs which will be queried for
// nodes to connect to.
Expand Down
66 changes: 66 additions & 0 deletions eth/ethconfig/historymode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2023 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 ethconfig

import "fmt"

// HistoryMode represents the blockchain history mode for pruning.
type HistoryMode uint32

const (
AllHistory HistoryMode = iota // Keep all history
PrunedHistory // Prune history beyond StateHistory and TransactionHistory
)

func (mode HistoryMode) IsValid() bool {
return mode == AllHistory || mode == PrunedHistory
}

// String implements the stringer interface.
func (mode HistoryMode) String() string {
switch mode {
case AllHistory:
return "full"
case PrunedHistory:
return "pruned"
default:
return "unknown"
}
}

func (mode HistoryMode) MarshalText() ([]byte, error) {
switch mode {
case AllHistory:
return []byte("full"), nil
case PrunedHistory:
return []byte("pruned"), nil
default:
return nil, fmt.Errorf("unknown history mode %d", mode)
}
}

func (mode *HistoryMode) UnmarshalText(text []byte) error {
switch string(text) {
case "full":
*mode = AllHistory
case "pruned":
*mode = PrunedHistory
default:
return fmt.Errorf(`unknown history mode %q, want "full" or "pruned"`, text)
}
return nil
}