forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollback.go
85 lines (69 loc) · 1.97 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package app
import (
"context"
"fmt"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/log"
"github.com/omni-network/omni/lib/netconf"
cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
dbm "github.com/cosmos/cosmos-db"
)
func DefaultRollbackConfig() RollbackConfig {
return RollbackConfig{
// RemoveCometBlock (--hard=true) rollback doesn't add value in most use-cases.
// Blocks cannot be re-proposed/re-consensused since that would result in validator slashing.
RemoveCometBlock: false,
}
}
type RollbackConfig struct {
RemoveCometBlock bool
}
func Rollback(ctx context.Context, cfg Config, rCfg RollbackConfig) error {
db, err := dbm.NewDB("application", cfg.BackendType(), cfg.DataDir())
if err != nil {
return errors.Wrap(err, "create db")
}
baseAppOpts, err := makeBaseAppOpts(cfg)
if err != nil {
return errors.Wrap(err, "make base app opts")
}
engineCl, err := newEngineClient(ctx, cfg, cfg.Network, nil)
if err != nil {
return err
}
privVal, err := loadPrivVal(cfg)
if err != nil {
return errors.Wrap(err, "load validator key")
}
voter, err := newVoterLoader(privVal.Key.PrivKey)
if err != nil {
return errors.Wrap(err, "new voter loader")
}
//nolint:contextcheck // False positive.
app, err := newApp(
newSDKLogger(ctx),
db,
engineCl,
voter,
netconf.ChainVersionNamer(cfg.Network),
netconf.ChainNamer(cfg.Network),
burnEVMFees{},
serverAppOptsFromCfg(cfg),
make(chan<- error, 1),
baseAppOpts...,
)
if err != nil {
return errors.Wrap(err, "new app")
}
// Rollback CometBFT state
height, hash, err := cmtcmd.RollbackState(&cfg.Comet, rCfg.RemoveCometBlock)
if err != nil {
return errors.Wrap(err, "rollback comet state")
}
// Rollback the multistore
if err := app.CommitMultiStore().RollbackToVersion(height); err != nil {
return errors.Wrap(err, "rollback to height")
}
log.Info(ctx, "Rolled back consensus state", "height", height, "hash", fmt.Sprintf("%X", hash))
return nil
}