Skip to content

Commit 668fc8e

Browse files
committed
deal with reorg
chore
1 parent b78a743 commit 668fc8e

File tree

7 files changed

+24
-9
lines changed

7 files changed

+24
-9
lines changed

core/blockchain.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,8 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash) (*typ
944944
}
945945
// Recover if the target state if it's not available yet.
946946
if !bc.HasState(head.Root) {
947-
if err := bc.triedb.Recover(head.Root); err != nil {
947+
curPeriod := head.Number.Uint64() / state.NumBlocksPerPeriod // TODO(weiihann): deal with this later
948+
if err := bc.triedb.Recover(head.Root, curPeriod); err != nil {
948949
log.Error("Failed to rollback state, resetting to genesis", "err", err)
949950
return bc.genesisBlock.Header(), rootNumber
950951
}
@@ -2324,7 +2325,8 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator, ma
23242325
parent := it.previous()
23252326
for parent != nil && !bc.HasState(parent.Root) {
23262327
if bc.stateRecoverable(parent.Root) {
2327-
if err := bc.triedb.Recover(parent.Root); err != nil {
2328+
curPeriod := parent.Number.Uint64() / state.NumBlocksPerPeriod // TODO(weiihann): deal with this later
2329+
if err := bc.triedb.Recover(parent.Root, curPeriod); err != nil {
23282330
return nil, 0, err
23292331
}
23302332
break
@@ -2386,7 +2388,8 @@ func (bc *BlockChain) recoverAncestors(block *types.Block, makeWitness bool) (co
23862388
)
23872389
for parent != nil && !bc.HasState(parent.Root()) {
23882390
if bc.stateRecoverable(parent.Root()) {
2389-
if err := bc.triedb.Recover(parent.Root()); err != nil {
2391+
curPeriod := parent.NumberU64() / state.NumBlocksPerPeriod // TODO(weiihann): deal with this later
2392+
if err := bc.triedb.Recover(parent.Root(), curPeriod); err != nil {
23902393
return common.Hash{}, err
23912394
}
23922395
break

triedb/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ func (db *Database) Dereference(root common.Hash) error {
272272
// supported as the rollback destination only if it's canonical state and the
273273
// corresponding trie histories are existent. It's only supported by path-based
274274
// database and will return an error for others.
275-
func (db *Database) Recover(target common.Hash) error {
275+
func (db *Database) Recover(target common.Hash, period uint64) error {
276276
pdb, ok := db.backend.(*pathdb.Database)
277277
if !ok {
278278
return errors.New("not supported")
279279
}
280-
return pdb.Recover(target)
280+
return pdb.Recover(target, period)
281281
}
282282

283283
// Recoverable returns the indicator if the specified state is enabled to be

triedb/pathdb/database.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ func (db *Database) Enable(root common.Hash) error {
460460
// canonical state and the corresponding trie histories are existent.
461461
//
462462
// The supplied root must be a valid trie hash value.
463-
func (db *Database) Recover(root common.Hash) error {
463+
func (db *Database) Recover(root common.Hash, period uint64) error {
464464
db.lock.Lock()
465465
defer db.lock.Unlock()
466466

@@ -485,6 +485,7 @@ func (db *Database) Recover(root common.Hash) error {
485485
if err != nil {
486486
return err
487487
}
488+
h.meta.period = period
488489
dl, err = dl.revert(h)
489490
if err != nil {
490491
return err

triedb/pathdb/database_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ func TestDatabaseRollback(t *testing.T) {
597597
if i > 0 {
598598
parent = tester.roots[i-1]
599599
}
600-
if err := tester.db.Recover(parent); err != nil {
600+
if err := tester.db.Recover(parent, 0); err != nil {
601601
t.Fatalf("Failed to revert db, err: %v", err)
602602
}
603603
if i > 0 {
@@ -703,7 +703,7 @@ func TestExecuteRollback(t *testing.T) {
703703
}
704704
}
705705

706-
if err := tester.db.Recover(h.meta.parent); err != nil {
706+
if err := tester.db.Recover(h.meta.parent, h.meta.period); err != nil {
707707
t.Fatalf("Failed to recover db, err: %v", err)
708708
}
709709
}

triedb/pathdb/disklayer.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@ func (dl *diskLayer) revert(h *stateHistory) (*diskLayer, error) {
494494
if err != nil {
495495
return nil, err
496496
}
497+
498+
// Populate the period for the nodes
499+
for _, subset := range nodes {
500+
for _, n := range subset {
501+
n.Period = h.meta.period
502+
}
503+
}
504+
497505
// Derive the state modification set from the history, keyed by the hash
498506
// of the account address and the storage key.
499507
accounts, storages := h.stateSet()

triedb/pathdb/history_state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ type meta struct {
203203
parent common.Hash // prev-state root before the state transition
204204
root common.Hash // post-state root after the state transition
205205
block uint64 // associated block number
206+
period uint64 // associated period, not persisted to disk
206207
}
207208

208209
// encode packs the meta object into byte stream.

triedb/pathdb/nodes.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ func newNodeSet(nodes map[common.Hash]map[string]*trienode.Node, period uint64)
6464
}
6565
}
6666
s.computeSize()
67-
s.setPeriod(period)
67+
if period != 0 {
68+
s.setPeriod(period)
69+
}
6870
return s
6971
}
7072

0 commit comments

Comments
 (0)