-
Notifications
You must be signed in to change notification settings - Fork 319
/
staterecoverer.go
128 lines (111 loc) · 3.88 KB
/
staterecoverer.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
// This is a recovery tool that recovers a corrupted or missing state database.
// To use, run "make recover"
package main
import (
"context"
"flag"
"fmt"
glog "log"
"os"
"strings"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/blockchain/blockdao"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/util/fileutil"
"github.com/iotexproject/iotex-core/server/itx"
"github.com/iotexproject/iotex-core/state/factory"
)
// recoveryHeight is the blockchain height being recovered to
var recoveryHeight int
/**
* overwritePath is the path to the config file which overwrite default values
* secretPath is the path to the config file store secret values
*/
var (
genesisPath string
_overwritePath string
_secretPath string
_plugins strs
)
type strs []string
func (ss *strs) String() string {
return strings.Join(*ss, ",")
}
func (ss *strs) Set(str string) error {
*ss = append(*ss, str)
return nil
}
func init() {
flag.StringVar(&genesisPath, "genesis-path", "", "Genesis path")
flag.StringVar(&_overwritePath, "config-path", "", "Config path")
flag.StringVar(&_secretPath, "secret-path", "", "Secret path")
flag.Var(&_plugins, "plugin", "Plugin of the node")
flag.IntVar(&recoveryHeight, "recovery-height", 0, "Recovery height")
flag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr,
"usage: recover -config-path=[string]\n -recovery-height=[int]\n")
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
}
func main() {
genesisCfg, err := genesis.New(genesisPath)
if err != nil {
glog.Fatalln("Failed to new genesis config.", zap.Error(err))
}
cfg, err := config.New([]string{_overwritePath, _secretPath}, _plugins)
if err != nil {
glog.Fatalln("Failed to new config.", zap.Error(err))
}
cfg.Genesis = genesisCfg
log.S().Infof("Config in use: %+v", cfg)
// create server
svr, err := itx.NewServer(cfg)
if err != nil {
log.L().Fatal("Failed to create server.", zap.Error(err))
}
// recover chain and state
bc := svr.ChainService(cfg.Chain.ID).Blockchain()
sf := svr.ChainService(cfg.Chain.ID).StateFactory()
dao := svr.ChainService(cfg.Chain.ID).BlockDAO()
if err := bc.Start(context.Background()); err == nil {
log.L().Info("State DB status is normal.")
}
defer func() {
if err := bc.Stop(context.Background()); err != nil {
log.L().Fatal("Failed to stop blockchain")
}
}()
if err := recoverChainAndState(dao, sf, cfg, uint64(recoveryHeight)); err != nil {
log.L().Fatal("Failed to recover chain and state.", zap.Error(err))
} else {
log.S().Infof("Success to recover chain and state to target height %d", recoveryHeight)
}
}
// recoverChainAndState recovers the chain to target height and refresh state db if necessary
func recoverChainAndState(dao blockdao.BlockDAO, sf factory.Factory, cfg config.Config, targetHeight uint64) error {
// recover the blockchain to target height(blockDAO)
if err := dao.DeleteBlockToTarget(targetHeight); err != nil {
return errors.Wrapf(err, "failed to recover blockchain to target height %d", targetHeight)
}
stateHeight, err := sf.Height()
if err != nil {
return err
}
if targetHeight < stateHeight {
// delete existing state DB (build from scratch)
if fileutil.FileExists(cfg.Chain.TrieDBPath) && os.Remove(cfg.Chain.TrieDBPath) != nil {
return errors.New("failed to delete existing state DB")
}
}
return nil
}