-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #596 from HaoyangLiu/develop
WIP: Fix replay issue
- Loading branch information
Showing
5 changed files
with
61 additions
and
126 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package baseapp | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/server" | ||
"github.com/spf13/viper" | ||
bc "github.com/tendermint/tendermint/blockchain" | ||
tmcli "github.com/tendermint/tendermint/libs/cli" | ||
dbm "github.com/tendermint/tendermint/libs/db" | ||
"github.com/tendermint/tendermint/libs/log" | ||
"github.com/tendermint/tendermint/node" | ||
sm "github.com/tendermint/tendermint/state" | ||
) | ||
|
||
func Replay(logger log.Logger) int64 { | ||
ctx := server.NewDefaultContext() | ||
ctx.Config.RootDir = viper.GetString(tmcli.HomeFlag) | ||
dbContext := node.DBContext{"state", ctx.Config} | ||
dbType := dbm.DBBackendType(dbContext.Config.DBBackend) | ||
stateDB := dbm.NewDB(dbContext.ID, dbType, dbContext.Config.DBDir()) | ||
|
||
blockDBContext := node.DBContext{"blockstore", ctx.Config} | ||
blockStoreDB := dbm.NewDB(blockDBContext.ID, dbType, dbContext.Config.DBDir()) | ||
blockStore := bc.NewBlockStore(blockStoreDB) | ||
|
||
defer func() { | ||
stateDB.Close() | ||
blockStoreDB.Close() | ||
}() | ||
|
||
curState := sm.LoadState(stateDB) | ||
preState := sm.LoadPreState(stateDB) | ||
if curState.LastBlockHeight == preState.LastBlockHeight { | ||
panic(fmt.Errorf("there is no block now, can't replay")) | ||
} | ||
var loadHeight int64 | ||
if blockStore.Height() == curState.LastBlockHeight { | ||
logger.Info(fmt.Sprintf("blockstore height equals to current state height %d", curState.LastBlockHeight)) | ||
logger.Info("Just reset state DB to last height") | ||
sm.SaveState(stateDB, preState) | ||
loadHeight = preState.LastBlockHeight | ||
} else if blockStore.Height() == curState.LastBlockHeight+1 { | ||
logger.Info(fmt.Sprintf("blockstore height %d, current state height %d", blockStore.Height(), curState.LastBlockHeight)) | ||
logger.Info(fmt.Sprintf("Retreat block %d in block store and reset state DB to last height", blockStore.Height())) | ||
blockStore.RetreatLastBlock() | ||
sm.SaveState(stateDB, preState) | ||
loadHeight = preState.LastBlockHeight | ||
} else { | ||
panic(fmt.Errorf("tendermint block store height should be at most one ahead of the its state height")) | ||
} | ||
|
||
return loadHeight | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters