-
Notifications
You must be signed in to change notification settings - Fork 20
/
rollback.go
52 lines (47 loc) · 1.37 KB
/
rollback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cmd
import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/store"
dbm "github.com/tendermint/tm-db"
)
// RollbackCmd update state last results hash from block
func RollbackCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "rollback",
Short: "rollback when app crash",
Long: "nibirud rollback",
RunE: func(cmd *cobra.Command, args []string) error {
config := cfg.DefaultBaseConfig()
dbType := dbm.BackendType(config.DBBackend)
// Get BlockStore
clientCtx := client.GetClientContextFromCmd(cmd)
config.RootDir = clientCtx.HomeDir
blockStoreDB, err := dbm.NewDB("blockstore", dbType, config.DBDir())
if err != nil {
return err
}
blockStore := store.NewBlockStore(blockStoreDB)
storeBlockHeight := blockStore.Height()
// Get StateStore
stateDB, err := dbm.NewDB("state", dbType, config.DBDir())
if err != nil {
return err
}
stateStore := state.NewStore(stateDB)
state, err := stateStore.Load()
if err != nil {
return err
}
block := blockStore.LoadBlock(storeBlockHeight)
state.LastResultsHash = block.LastResultsHash
err = stateStore.Save(state)
return err
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}