Skip to content

Commit

Permalink
Merge pull request #596 from HaoyangLiu/develop
Browse files Browse the repository at this point in the history
WIP: Fix replay issue
  • Loading branch information
HaoyangLiu authored Nov 14, 2018
2 parents ad55c23 + 8321fe9 commit eb92b33
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 126 deletions.
4 changes: 3 additions & 1 deletion Gopkg.lock

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

45 changes: 2 additions & 43 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package app

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"sort"
"strings"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
Expand All @@ -24,21 +22,16 @@ import (
"github.com/irisnet/irishub/iparam"
"github.com/irisnet/irishub/modules/gov"
"github.com/irisnet/irishub/modules/gov/params"

"github.com/irisnet/irishub/modules/record"
"github.com/irisnet/irishub/modules/service"
"github.com/irisnet/irishub/modules/service/params"
"github.com/irisnet/irishub/modules/record"
"github.com/irisnet/irishub/modules/upgrade"
"github.com/irisnet/irishub/modules/upgrade/params"
"github.com/spf13/viper"
abci "github.com/tendermint/tendermint/abci/types"
bc "github.com/tendermint/tendermint/blockchain"
tmcli "github.com/tendermint/tendermint/libs/cli"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/node"
sm "github.com/tendermint/tendermint/state"
tmtypes "github.com/tendermint/tendermint/types"
)

Expand Down Expand Up @@ -126,7 +119,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio

var lastHeight int64
if viper.GetBool(FlagReplay) {
lastHeight = app.replay()
lastHeight = bam.Replay(app.Logger)
}

// define the AccountKeeper
Expand Down Expand Up @@ -502,40 +495,6 @@ func (app *IrisApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode bam.RunTxMode)
return result
}

func (app *IrisApp) replay() 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(errors.New("there is no block now, can't replay"))
}
var loadHeight int64
if blockStore.Height() == curState.LastBlockHeight {
sm.SaveState(stateDB, preState)
loadHeight = preState.LastBlockHeight
} else if blockStore.Height() == curState.LastBlockHeight+1 {
loadHeight = curState.LastBlockHeight
} else {
panic(errors.New("tendermint block store height should be at most one ahead of the its state height"))
}

return loadHeight
}

//______________________________________________________________________________________________

// Combined Staking Hooks
Expand Down
54 changes: 54 additions & 0 deletions baseapp/replay.go
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
}
42 changes: 1 addition & 41 deletions examples/irishub-bugfix-2/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package app

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"sort"
"strings"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
Expand All @@ -32,13 +30,9 @@ import (
"github.com/irisnet/irishub/modules/upgrade/params"
"github.com/spf13/viper"
abci "github.com/tendermint/tendermint/abci/types"
bc "github.com/tendermint/tendermint/blockchain"
tmcli "github.com/tendermint/tendermint/libs/cli"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/node"
sm "github.com/tendermint/tendermint/state"
tmtypes "github.com/tendermint/tendermint/types"
)

Expand Down Expand Up @@ -127,7 +121,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio

var lastHeight int64
if viper.GetBool(FlagReplay) {
lastHeight = app.replay()
lastHeight = bam.Replay(app.Logger)
}

// define the AccountKeeper
Expand Down Expand Up @@ -507,40 +501,6 @@ func (app *IrisApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode bam.RunTxMode)
return result
}

func (app *IrisApp) replay() 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(errors.New("there is no block now, can't replay"))
}
var loadHeight int64
if blockStore.Height() == curState.LastBlockHeight {
sm.SaveState(stateDB, preState)
loadHeight = preState.LastBlockHeight
} else if blockStore.Height() == curState.LastBlockHeight+1 {
loadHeight = curState.LastBlockHeight
} else {
panic(errors.New("tendermint block store height should be at most one ahead of the its state height"))
}

return loadHeight
}

//______________________________________________________________________________________________

// Combined Staking Hooks
Expand Down
42 changes: 1 addition & 41 deletions examples/irishub1/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package app

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"sort"
"strings"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
Expand All @@ -32,13 +30,9 @@ import (
"github.com/irisnet/irishub/modules/upgrade/params"
"github.com/spf13/viper"
abci "github.com/tendermint/tendermint/abci/types"
bc "github.com/tendermint/tendermint/blockchain"
tmcli "github.com/tendermint/tendermint/libs/cli"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/node"
sm "github.com/tendermint/tendermint/state"
tmtypes "github.com/tendermint/tendermint/types"
)

Expand Down Expand Up @@ -127,7 +121,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio

var lastHeight int64
if viper.GetBool(FlagReplay) {
lastHeight = app.replay()
lastHeight = bam.Replay(app.Logger)
}

// define the AccountKeeper
Expand Down Expand Up @@ -507,40 +501,6 @@ func (app *IrisApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode bam.RunTxMode)
return result
}

func (app *IrisApp) replay() 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(errors.New("there is no block now, can't replay"))
}
var loadHeight int64
if blockStore.Height() == curState.LastBlockHeight {
sm.SaveState(stateDB, preState)
loadHeight = preState.LastBlockHeight
} else if blockStore.Height() == curState.LastBlockHeight+1 {
loadHeight = curState.LastBlockHeight
} else {
panic(errors.New("tendermint block store height should be at most one ahead of the its state height"))
}

return loadHeight
}

//______________________________________________________________________________________________

// Combined Staking Hooks
Expand Down

0 comments on commit eb92b33

Please sign in to comment.