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

Check + repair state on corrupted database #21650

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"os"
"runtime"
Expand Down Expand Up @@ -167,6 +168,48 @@ The export-preimages command export hash preimages to an RLP encoded stream`,
The arguments are interpreted as block numbers or hashes.
Use "ethereum dump 0" to dump the genesis block.`,
}
trieRepairCommand = cli.Command{
Action: utils.MigrateFlags(repairTrie),
Name: "repairtrie",
Usage: "Check and repair geth state database",
ArgsUsage: " ",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.CacheFlag,
utils.RopstenFlag,
utils.RinkebyFlag,
utils.GoerliFlag,
},
Category: "BLOCKCHAIN COMMANDS",
}
trieInspectCommand = cli.Command{
Action: utils.MigrateFlags(inspectTrie),
Name: "inspecttrie",
Usage: "Deep inspection of geth state database",
ArgsUsage: " ",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.CacheFlag,
utils.RopstenFlag,
utils.RinkebyFlag,
utils.GoerliFlag,
},
Category: "BLOCKCHAIN COMMANDS",
}
blockInspectCommand = cli.Command{
Action: utils.MigrateFlags(verifyBlocks),
Name: "inspectblocks",
Usage: "Check geth block database",
ArgsUsage: " ",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.CacheFlag,
utils.RopstenFlag,
utils.RinkebyFlag,
utils.GoerliFlag,
},
Category: "BLOCKCHAIN COMMANDS",
}
)

// initGenesis will initialise the given JSON format genesis file and writes it as
Expand Down Expand Up @@ -418,6 +461,69 @@ func dump(ctx *cli.Context) error {
return nil
}

func repairTrie(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()
chain, chainDb := utils.MakeChain(ctx, stack)
defer chainDb.Close()
block := chain.CurrentBlock()
if block == nil {
return errors.New("No block found!")
}
log.Info("State root", "blockhash", block.Hash(), "state root", block.Root())
state, err := state.New(block.Root(), state.NewDatabase(chainDb), nil)
if err != nil {
return err
}
if state.Repair(chainDb) {
fmt.Printf("Please restart the node in fast-sync mode, and hope that it works!")
}
return nil
}

func verifyBlocks(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()
chain, chainDb := utils.MakeChain(ctx, stack)
defer chainDb.Close()
head := chain.CurrentBlock()
if head == nil {
return errors.New("No block found!")
}

log.Info("Checking if chain is intact ")
var prev *types.Block
for num := uint64(0); num < head.NumberU64(); num++ {
block := chain.GetBlockByNumber(num)
if prev != nil {
if block == nil {
log.Info("Missing block", "number", num)
return errors.New("Missing blocks")
}
if block.ParentHash() != prev.Hash() {
log.Info("Non-contiguous chain", "block", block.NumberU64(),
"parentHash", block.ParentHash(), "block", prev.NumberU64(),
"hash", prev.Hash())
return errors.New("Non-contiguous chain")
}
prev = block
}
if num%500000 == 0 {
log.Info("Checking at", "number", num)
}
}
log.Info("All seems ok", "inspected", head.NumberU64())
return nil
}

func inspectTrie(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()
chainDb := utils.MakeChainDatabase(ctx, stack, true)
defer chainDb.Close()
return state.InspectDb(chainDb)
}

// hashish returns true for strings that look like hashes.
func hashish(x string) bool {
_, err := strconv.Atoi(x)
Expand Down
3 changes: 3 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ func init() {
removedbCommand,
dumpCommand,
dumpGenesisCommand,
trieRepairCommand,
trieInspectCommand,
blockInspectCommand,
// See accountcmd.go:
accountCommand,
walletCommand,
Expand Down
Loading