Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #390 from ethereumproject/ensure-valid-bc
Browse files Browse the repository at this point in the history
core: db stability and recoverability
  • Loading branch information
whilei authored Nov 1, 2017
2 parents b2af41f + 04f6596 commit f7204f1
Show file tree
Hide file tree
Showing 44 changed files with 945 additions and 362 deletions.
7 changes: 3 additions & 4 deletions accounts/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
package accounts

import (
"time"
"github.com/ethereumproject/go-ethereum/common"
"fmt"
"github.com/ethereumproject/go-ethereum/common"
"os"
"strings"
"time"
)

// Minimum amount of time between cache reloads. This limit applies if the platform does
Expand Down Expand Up @@ -75,7 +75,6 @@ type caching interface {
close()
}


func skipKeyFile(fi os.FileInfo) bool {
// Skip editor backups and UNIX-style hidden files.
if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
Expand All @@ -89,4 +88,4 @@ func skipKeyFile(fi os.FileInfo) bool {
return true
}
return false
}
}
2 changes: 1 addition & 1 deletion accounts/vendor/github.com/boltdb/bolt/bolt_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 37 additions & 37 deletions accounts/vendor/github.com/mailru/easyjson/parser/parser_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/geth/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

var (
AccountsIndexFlag = cli.BoolFlag{
Name: "index-accounts,indexaccounts",
Name: "index-accounts,indexaccounts",
Usage: "Enable key-value db store for indexing large amounts of key files",
}
walletCommand = cli.Command{
Expand Down
21 changes: 16 additions & 5 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"time"
"strings"
"bufio"
"time"

"github.com/ethereumproject/go-ethereum/common"
"github.com/ethereumproject/go-ethereum/console"
Expand Down Expand Up @@ -104,14 +104,25 @@ var (
}
resetCommand = cli.Command{
Action: resetChaindata,
Name: "reset",
Usage: "Reset the chain database",
Name: "reset",
Usage: "Reset the chain database",
Description: `
Reset does a hard reset of the entire chain database.
This is a drastic and irreversible command, and should be used with caution.
The command will require user confirmation before any action is taken.
`,
}
recoverCommand = cli.Command{
Action: recoverChaindata,
Name: "recover",
Usage: "Attempt blockchain data recovery in case of data corruption",
Description: `
Recover scans and health-checks all available blockchain data in order
to recover all consistent and healthy block data. It will remove invalid or
corrupt block data that may have been caused by hard killing, system failure,
space limitations, or attack.
`,
}
)

func importChain(ctx *cli.Context) error {
Expand Down Expand Up @@ -287,7 +298,7 @@ func dump(ctx *cli.Context) error {
if sorted {
err = state.SortedDump(addresses, prefix, indent, out)
} else {
err = state.UnsortedDump(addresses,prefix,indent,out)
err = state.UnsortedDump(addresses, prefix, indent, out)
}

if err != nil {
Expand Down
102 changes: 84 additions & 18 deletions cmd/geth/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,33 @@
package main

import (
"fmt"
"io"
"os"
"os/signal"
"runtime"

"strings"

"bufio"
"errors"
"io/ioutil"
"math/big"
"path/filepath"
"strconv"
"syscall"
"time"

"fmt"
"github.com/ethereumproject/ethash"
"github.com/ethereumproject/go-ethereum/core"
"github.com/ethereumproject/go-ethereum/core/state"
"github.com/ethereumproject/go-ethereum/core/types"
"github.com/ethereumproject/go-ethereum/eth"
"github.com/ethereumproject/go-ethereum/eth/downloader"
"github.com/ethereumproject/go-ethereum/event"
"github.com/ethereumproject/go-ethereum/logger"
"github.com/ethereumproject/go-ethereum/logger/glog"
"github.com/ethereumproject/go-ethereum/node"
"github.com/ethereumproject/go-ethereum/pow"
"github.com/ethereumproject/go-ethereum/rlp"
"gopkg.in/urfave/cli.v1"
"bufio"
"io"
"io/ioutil"
"math/big"
"os"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"
)

const (
Expand Down Expand Up @@ -573,7 +572,9 @@ func rollback(ctx *cli.Context) error {

glog.Warning("Rolling back blockchain...")

bc.SetHead(blockIndex)
if err := bc.SetHead(blockIndex); err != nil {
glog.V(logger.Warn).Infof("error setting head: %v", err)
}

// Check if *neither* block nor fastblock numbers match desired head number
nowCurrentHead := bc.CurrentBlock().Number().Uint64()
Expand Down Expand Up @@ -945,6 +946,9 @@ func stringInSlice(s string, sl []string) bool {
return false
}

// makeMLogDocumentation creates markdown documentation text for, eg. wiki
// That's why it uses 'fmt' instead of glog or log; output with prefixes (glog and log)
// will break markdown.
func makeMLogDocumentation(ctx *cli.Context) error {
wantComponents := ctx.Args()

Expand Down Expand Up @@ -1002,6 +1006,68 @@ func makeMLogDocumentation(ctx *cli.Context) error {
return nil
}

func recoverChaindata(ctx *cli.Context) error {

// Congruent to MakeChain(), but uses special NewBlockChainDryrun. Avoids a one-off function in flags.go.
var err error
sconf := mustMakeSufficientChainConfig(ctx)
bcdb := MakeChainDatabase(ctx)
defer bcdb.Close()

pow := pow.PoW(core.FakePow{})
if !ctx.GlobalBool(aliasableName(FakePoWFlag.Name, ctx)) {
pow = ethash.New()
} else {
glog.V(logger.Info).Info("Consensus: fake")
}

bc, err := core.NewBlockChainDryrun(bcdb, sconf.ChainConfig, pow, new(event.TypeMux))
if err != nil {
glog.Fatal("Could not start chain manager: ", err)
}

if blockchainLoadError := bc.LoadLastState(true); blockchainLoadError != nil {
glog.V(logger.Error).Infof("! Found error while loading blockchain: %v", blockchainLoadError)
// but do not return
}

header := bc.CurrentHeader()
currentBlock := bc.CurrentBlock()
currentFastBlock := bc.CurrentFastBlock()

glog.V(logger.Error).Infoln("Current status (before recovery attempt):")
if header != nil {
glog.V(logger.Error).Infof("Last header: #%d\n", header.Number.Uint64())
if currentBlock != nil {
glog.V(logger.Error).Infof("Last block: #%d\n", currentBlock.Number())
} else {
glog.V(logger.Error).Infoln("! Last block: nil")
}
if currentFastBlock != nil {
glog.V(logger.Error).Infof("Last fast block: #%d\n", currentFastBlock.Number())
} else {
glog.V(logger.Error).Infoln("! Last fast block: nil")
}
} else {
glog.V(logger.Error).Infoln("! Last header: nil")
}

glog.V(logger.Error).Infoln(glog.Separator("-"))

glog.V(logger.Error).Infoln("Checking db validity and recoverable data...")
checkpoint := bc.Recovery(1, 2048)
glog.V(logger.Error).Infof("Found last recoverable checkpoint=#%d\n", checkpoint)

glog.V(logger.Error).Infoln(glog.Separator("-"))

glog.V(logger.Error).Infoln("Setting blockchain db head to last safe checkpoint...")
if setHeadErr := bc.SetHead(checkpoint); setHeadErr != nil {
glog.V(logger.Error).Infof("Error setting head: %v\n", setHeadErr)
return setHeadErr
}
return nil
}

// https://gist.github.com/r0l1/3dcbb0c8f6cfe9c66ab8008f55f8f28b
// askForConfirmation asks the user for confirmation. A user must type in "yes" or "no" and
// then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as
Expand Down Expand Up @@ -1050,4 +1116,4 @@ func resetChaindata(ctx *cli.Context) error {
glog.V(logger.Error).Infoln("Leaving chaindata untouched. As you were.")
}
return nil
}
}
4 changes: 2 additions & 2 deletions cmd/geth/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var (
devModeDataDirPath = filepath.Join(os.TempDir(), "/ethereum_dev_mode")

cacheChainIdentity string
cacheChainConfig *core.SufficientChainConfig
cacheChainConfig *core.SufficientChainConfig
)

// chainIsMorden allows either
Expand Down Expand Up @@ -151,7 +151,7 @@ func mustMakeChainIdentity(ctx *cli.Context) (identity string) {
if cacheChainIdentity != "" {
return cacheChainIdentity
}
defer func () {
defer func() {
cacheChainIdentity = identity
}()

Expand Down
Loading

0 comments on commit f7204f1

Please sign in to comment.