[state] Fix bug: infinite loop problem to rollback block state
@@ -38,6 +38,7 @@ type BlockInfo struct {
BlockHash types.BlockID
PrevHash types.BlockID
}
+
type StateEntry struct {
State *types.State
Undo *types.State
@@ -214,8 +215,9 @@ func (sdb *ChainStateDB) Rollback(blockNo types.BlockNo) error {
sdb.Lock()
defer sdb.Unlock()
- for sdb.latest.BlockNo > blockNo {
- bs, err := sdb.loadBlockState(sdb.latest.BlockHash)
+ target := sdb.latest
+ for target.BlockNo > blockNo {
+ bs, err := sdb.loadBlockState(target.BlockHash)
if err != nil {
return err
}
@@ -231,6 +233,10 @@ func (sdb *ChainStateDB) Rollback(blockNo types.BlockNo) error {
}
// logger.Debugf("- trie.root: %v", base64.StdEncoding.EncodeToString(sdb.GetHash()))
sdb.latest = &bs.BlockInfo
+ target = &BlockInfo{
+ BlockNo: sdb.latest.BlockNo - 1,
+ BlockHash: sdb.latest.PrevHash,
+ }
}
err := sdb.saveStateDB()
return err
