Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add --pruneancient flag for snapshot prune-state cmd #1034

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions cmd/geth/snapshot.go
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/ethereum/go-ethereum/core/state/snapshot"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -70,6 +71,7 @@ var (
Flags: []cli.Flag{
utils.DataDirFlag,
utils.AncientFlag,
utils.PruneAncientDataFlag,
utils.RopstenFlag,
utils.SepoliaFlag,
utils.RinkebyFlag,
Expand Down Expand Up @@ -243,7 +245,7 @@ block is used.
)

func accessDb(ctx *cli.Context, stack *node.Node) (ethdb.Database, error) {
//The layer of tries trees that keep in memory.
// The layer of tries trees that keep in memory.
TriesInMemory := int(ctx.GlobalUint64(utils.TriesInMemoryFlag.Name))
chaindb := utils.MakeChainDatabase(ctx, stack, false, true)
defer chaindb.Close()
Expand All @@ -256,7 +258,7 @@ func accessDb(ctx *cli.Context, stack *node.Node) (ethdb.Database, error) {
return nil, errors.New("failed to load head block")
}
headHeader := headBlock.Header()
//Make sure the MPT and snapshot matches before pruning, otherwise the node can not start.
// Make sure the MPT and snapshot matches before pruning, otherwise the node can not start.
snaptree, err := snapshot.New(chaindb, trie.NewDatabase(chaindb), 256, TriesInMemory, headBlock.Root(), false, false, false, false)
if err != nil {
log.Error("snaptree error", "err", err)
Expand Down Expand Up @@ -416,7 +418,7 @@ func pruneBlock(ctx *cli.Context) error {

log.Info("backup block successfully")

//After backing up successfully, rename the new ancientdb name to the original one, and delete the old ancientdb
// After backing up successfully, rename the new ancientdb name to the original one, and delete the old ancientdb
if err := blockpruner.AncientDbReplacer(); err != nil {
return err
}
Expand All @@ -427,6 +429,12 @@ func pruneBlock(ctx *cli.Context) error {
}

func pruneState(ctx *cli.Context) error {
if ctx.GlobalIsSet(utils.PruneAncientDataFlag.Name) { // prune ancient only take effect on full sync.
if err := ctx.GlobalSet(utils.SyncModeFlag.Name, downloader.FullSync.String()); err != nil {
return err
}
}

stack, config := makeConfigNode(ctx)
defer stack.Close()

Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Expand Up @@ -2020,7 +2020,7 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node, readonly, disableFree
chainDb, err = stack.OpenDatabase(name, cache, handles, "", readonly)
} else {
name := "chaindata"
chainDb, err = stack.OpenDatabaseWithFreezer(name, cache, handles, ctx.GlobalString(AncientFlag.Name), "", readonly, disableFreeze, false, false)
chainDb, err = stack.OpenDatabaseWithFreezer(name, cache, handles, ctx.GlobalString(AncientFlag.Name), "", readonly, disableFreeze, false, ctx.GlobalIsSet(PruneAncientDataFlag.Name))
}
if err != nil {
Fatalf("Could not open database: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata