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

core: improve snapshot journal recovery #21594

Merged
merged 19 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
core, tests: fix tests and special recovery case
  • Loading branch information
rjl493456442 committed Oct 28, 2020
commit d05d8954aadb46fe7474a2eb41481c94838ab379
8 changes: 4 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,15 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
return nil, err
}
// Make sure the state associated with the block is available
head := bc.CurrentBlock()
head, recoverSnapshot := bc.CurrentBlock(), false
if _, err := state.New(head.Root(), bc.stateCache, bc.snaps); err != nil {
// Head state is missing, before the state recovery, find out the
// disk layer point of snapshot(if it's enabled). Make sure the
// rewound point is lower then disk layer.
var diskRoot common.Hash
if bc.cacheConfig.SnapshotLimit > 0 {
diskRoot = rawdb.ReadSnapshotRoot(bc.db)
recoverSnapshot = true
}
log.Warn("Head state missing, repairing", "number", head.Number(), "hash", head.Hash())
if diskRoot != (common.Hash{}) {
Expand Down Expand Up @@ -357,7 +358,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
}
// Load any existing snapshot, regenerating it if loading failed
if bc.cacheConfig.SnapshotLimit > 0 {
bc.snaps = snapshot.New(bc.db, bc.stateCache.TrieDB(), bc.cacheConfig.SnapshotLimit, bc.CurrentBlock().Root(), !bc.cacheConfig.SnapshotWait)
bc.snaps = snapshot.New(bc.db, bc.stateCache.TrieDB(), bc.cacheConfig.SnapshotLimit, bc.CurrentBlock().Root(), !bc.cacheConfig.SnapshotWait, recoverSnapshot)
}
// Take ownership of this particular state
go bc.update()
Expand Down Expand Up @@ -496,10 +497,9 @@ func (bc *BlockChain) setHead(head uint64, onDelete func(block *types.Block, can
} else {
log.Trace("Rewind passed pivot, aiming genesis", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash(), "pivot", *pivot)
newHeadBlock = bc.genesisBlock
canStop = true
}
}
if canStop {
if canStop || newHeadBlock.NumberU64() == 0 {
log.Debug("Rewound to block with state", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash())
break
}
Expand Down
14 changes: 10 additions & 4 deletions core/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package core

import (
"fmt"
"io/ioutil"
"math/big"
"os"
Expand Down Expand Up @@ -1556,7 +1557,7 @@ func TestLongReorgedFastSyncingDeepRepair(t *testing.T) {
func testRepair(t *testing.T, tt *rewindTest) {
// It's hard to follow the test case, visualize the input
//log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
//fmt.Println(tt.dump(true))
fmt.Println(tt.dump(true))

// Create a temporary persistent database
datadir, err := ioutil.TempDir("", "")
Expand All @@ -1573,10 +1574,15 @@ func testRepair(t *testing.T, tt *rewindTest) {

// Initialize a fresh chain
var (
genesis = new(Genesis).MustCommit(db)
engine = ethash.NewFullFaker()
genesis = new(Genesis).MustCommit(db)
engine = ethash.NewFullFaker()
cacheConfig = defaultCacheConfig
)
chain, err := NewBlockChain(db, nil, params.AllEthashProtocolChanges, engine, vm.Config{}, nil, nil)
if !tt.enableSnapshot {
cacheConfig.SnapshotLimit = 0
cacheConfig.SnapshotWait = false
}
chain, err := NewBlockChain(db, cacheConfig, params.AllEthashProtocolChanges, engine, vm.Config{}, nil, nil)
if err != nil {
t.Fatalf("Failed to create chain: %v", err)
}
Expand Down
17 changes: 12 additions & 5 deletions core/blockchain_sethead_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

// rewindTest is a test case for chain rollback upon user request.
type rewindTest struct {
enableSnapshot bool // Flag whether the snapshot is enabled
canonicalBlocks int // Number of blocks to generate for the canonical chain (heavier)
sidechainBlocks int // Number of blocks to generate for the side chain (lighter)
freezeThreshold uint64 // Block number until which to move things into the freezer
Expand All @@ -55,7 +56,8 @@ type rewindTest struct {
func (tt *rewindTest) dump(crash bool) string {
buffer := new(strings.Builder)

fmt.Fprint(buffer, "Chain:\n G")
fmt.Fprint(buffer, fmt.Sprintf("Chain<snapshot = %t>:", tt.enableSnapshot))
fmt.Fprint(buffer, "\n G")
for i := 0; i < tt.canonicalBlocks; i++ {
fmt.Fprintf(buffer, "->C%d", i+1)
}
Expand Down Expand Up @@ -1747,7 +1749,7 @@ func TestLongReorgedFastSyncingDeepSetHead(t *testing.T) {
func testSetHead(t *testing.T, tt *rewindTest) {
// It's hard to follow the test case, visualize the input
//log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
//fmt.Println(tt.dump(false))
fmt.Println(tt.dump(false))

// Create a temporary persistent database
datadir, err := ioutil.TempDir("", "")
Expand All @@ -1764,10 +1766,15 @@ func testSetHead(t *testing.T, tt *rewindTest) {

// Initialize a fresh chain
var (
genesis = new(Genesis).MustCommit(db)
engine = ethash.NewFullFaker()
genesis = new(Genesis).MustCommit(db)
engine = ethash.NewFullFaker()
cacheConfig = defaultCacheConfig
)
chain, err := NewBlockChain(db, nil, params.AllEthashProtocolChanges, engine, vm.Config{}, nil, nil)
if !tt.enableSnapshot {
cacheConfig.SnapshotLimit = 0
cacheConfig.SnapshotWait = false
}
chain, err := NewBlockChain(db, cacheConfig, params.AllEthashProtocolChanges, engine, vm.Config{}, nil, nil)
if err != nil {
t.Fatalf("Failed to create chain: %v", err)
}
Expand Down
11 changes: 9 additions & 2 deletions core/state/snapshot/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func loadAndParseJournal(db ethdb.KeyValueStore, base *diskLayer) (snapshot, jou
}

// loadSnapshot loads a pre-existing state snapshot backed by a key-value store.
func loadSnapshot(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash) (snapshot, error) {
func loadSnapshot(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash, recovery bool) (snapshot, error) {
// Retrieve the block number and hash of the snapshot, failing if no snapshot
// is present in the database (or crashed mid-update).
baseRoot := rawdb.ReadSnapshotRoot(diskdb)
Expand Down Expand Up @@ -175,9 +175,16 @@ func loadSnapshot(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int,
// which is below the snapshot. In this case the snapshot can be recovered
// by re-executing blocks but right now it's unavailable.
if head := snapshot.Root(); head != root {
if legacy {
// If it's legacy snapshot, or it's new-format snapshot but
// it's not in recovery mode, returns the error here for
// rebuilding the entire snapshot forcibly.
if legacy || !recovery {
return nil, fmt.Errorf("head doesn't match snapshot: have %#x, want %#x", head, root)
}
// It's in snapshot recovery, the assumption is held that
// the disk layer is higher than chain head. It can be
// eventually recovered when the chain head is beyond the
// disk layer.
log.Warn("Snapshot is not continous with chain", "snaproot", head, "chainroot", root)
}
// Everything loaded correctly, resume any suspended operations
Expand Down
7 changes: 4 additions & 3 deletions core/state/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ type Tree struct {
// If the snapshot is missing or the disk layer is broken, the entire is deleted
// and will be reconstructed from scratch based on the tries in the key-value
// store, on a background thread. If the memory layers from the journal is not
// continuous with disk layer or the journal is missing, all diffs will be discarded.
func New(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash, async bool) *Tree {
// continuous with disk layer or the journal is missing, all diffs will be discarded
// iff it's in "recovery" mode, otherwise rebuild is mandatory.
func New(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash, async bool, recovery bool) *Tree {
// Create a new, empty snapshot tree
snap := &Tree{
diskdb: diskdb,
Expand All @@ -185,7 +186,7 @@ func New(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root comm
defer snap.waitBuild()
}
// Attempt to load a previously persisted snapshot and rebuild one if failed
head, err := loadSnapshot(diskdb, triedb, cache, root)
head, err := loadSnapshot(diskdb, triedb, cache, root, recovery)
if err != nil {
log.Warn("Failed to load snapshot, regenerating", "err", err)
snap.Rebuild(root)
Expand Down
2 changes: 1 addition & 1 deletion tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func MakePreState(db ethdb.Database, accounts core.GenesisAlloc, snapshotter boo

var snaps *snapshot.Tree
if snapshotter {
snaps = snapshot.New(db, sdb.TrieDB(), 1, root, false)
snaps = snapshot.New(db, sdb.TrieDB(), 1, root, false, false)
}
statedb, _ = state.New(root, sdb, snaps)
return snaps, statedb
Expand Down