forked from tendermint/tendermint
/
main.go
242 lines (208 loc) · 6.27 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/viper"
cmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/cmd/tendermint/commands/debug"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
tmflags "github.com/tendermint/tendermint/libs/cli/flags"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/p2p"
cs "github.com/tendermint/tendermint/test/maverick/consensus"
nd "github.com/tendermint/tendermint/test/maverick/node"
"github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
)
var (
config = cfg.DefaultConfig()
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
misbehaviorFlag = ""
)
func init() {
registerFlagsRootCmd(RootCmd)
}
func registerFlagsRootCmd(command *cobra.Command) {
command.PersistentFlags().String("log_level", config.LogLevel, "Log level")
}
func ParseConfig() (*cfg.Config, error) {
conf := cfg.DefaultConfig()
err := viper.Unmarshal(conf)
if err != nil {
return nil, err
}
conf.SetRoot(conf.RootDir)
cfg.EnsureRoot(conf.RootDir)
if err = conf.ValidateBasic(); err != nil {
return nil, fmt.Errorf("error in config file: %v", err)
}
return conf, err
}
// RootCmd is the root command for Tendermint core.
var RootCmd = &cobra.Command{
Use: "maverick",
Short: "Tendermint Maverick Node",
Long: "Tendermint Maverick Node for testing with faulty consensus misbehaviors in a testnet. Contains " +
"all the functionality of a normal node but custom misbehaviors can be injected when running the node " +
"through a flag. See maverick node --help for how the misbehavior flag is constructured",
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
fmt.Printf("use: %v, args: %v", cmd.Use, cmd.Args)
config, err = ParseConfig()
if err != nil {
return err
}
if config.LogFormat == cfg.LogFormatJSON {
logger = log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout))
}
logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel)
if err != nil {
return err
}
if viper.GetBool(cli.TraceFlag) {
logger = log.NewTracingLogger(logger)
}
logger = logger.With("module", "main")
return nil
},
}
func main() {
rootCmd := RootCmd
rootCmd.AddCommand(
ListMisbehaviorCmd,
cmd.GenValidatorCmd,
InitFilesCmd,
cmd.ProbeUpnpCmd,
cmd.ReplayCmd,
cmd.ReplayConsoleCmd,
cmd.ResetAllCmd,
cmd.ResetPrivValidatorCmd,
cmd.ShowValidatorCmd,
cmd.ShowNodeIDCmd,
cmd.GenNodeKeyCmd,
cmd.VersionCmd,
debug.DebugCmd,
cli.NewCompletionCmd(rootCmd, true),
)
nodeCmd := &cobra.Command{
Use: "node",
Short: "Run the maverick node",
RunE: func(command *cobra.Command, args []string) error {
return startNode(config, logger, misbehaviorFlag)
},
}
cmd.AddNodeFlags(nodeCmd)
// Create & start node
rootCmd.AddCommand(nodeCmd)
// add special flag for misbehaviors
nodeCmd.Flags().StringVar(
&misbehaviorFlag,
"misbehaviors",
"",
"Select the misbehaviors of the node (comma-separated, no spaces in between): \n"+
"e.g. --misbehaviors double-prevote,3\n"+
"You can also have multiple misbehaviors: e.g. double-prevote,3,no-vote,5")
cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv(filepath.Join("$HOME", cfg.DefaultTendermintDir)))
if err := cmd.Execute(); err != nil {
panic(err)
}
}
func startNode(config *cfg.Config, logger log.Logger, misbehaviorFlag string) error {
misbehaviors, err := nd.ParseMisbehaviors(misbehaviorFlag)
if err != nil {
return err
}
node, err := nd.DefaultNewNode(config, logger, misbehaviors)
if err != nil {
return fmt.Errorf("failed to create node: %w", err)
}
if err := node.Start(); err != nil {
return fmt.Errorf("failed to start node: %w", err)
}
logger.Info("Started node", "nodeInfo", node.Switch().NodeInfo())
// Stop upon receiving SIGTERM or CTRL-C.
tmos.TrapSignal(logger, func() {
if node.IsRunning() {
if err := node.Stop(); err != nil {
logger.Error("unable to stop the node", "error", err)
}
}
})
// Run forever.
select {}
}
var InitFilesCmd = &cobra.Command{
Use: "init",
Short: "Initialize Tendermint",
RunE: initFiles,
}
func initFiles(cmd *cobra.Command, args []string) error {
return initFilesWithConfig(config)
}
func initFilesWithConfig(config *cfg.Config) error {
// private validator
privValKeyFile := config.PrivValidatorKeyFile()
privValStateFile := config.PrivValidatorStateFile()
var pv *nd.FilePV
if tmos.FileExists(privValKeyFile) {
pv = nd.LoadFilePV(privValKeyFile, privValStateFile)
logger.Info("Found private validator", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
} else {
pv = nd.GenFilePV(privValKeyFile, privValStateFile)
pv.Save()
logger.Info("Generated private validator", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
}
nodeKeyFile := config.NodeKeyFile()
if tmos.FileExists(nodeKeyFile) {
logger.Info("Found node key", "path", nodeKeyFile)
} else {
if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil {
return err
}
logger.Info("Generated node key", "path", nodeKeyFile)
}
// genesis file
genFile := config.GenesisFile()
if tmos.FileExists(genFile) {
logger.Info("Found genesis file", "path", genFile)
} else {
genDoc := types.GenesisDoc{
ChainID: fmt.Sprintf("test-chain-%v", tmrand.Str(6)),
GenesisTime: tmtime.Now(),
ConsensusParams: types.DefaultConsensusParams(),
}
pubKey, err := pv.GetPubKey()
if err != nil {
return fmt.Errorf("can't get pubkey: %w", err)
}
genDoc.Validators = []types.GenesisValidator{{
Address: pubKey.Address(),
PubKey: pubKey,
Power: 10,
}}
if err := genDoc.SaveAs(genFile); err != nil {
return err
}
logger.Info("Generated genesis file", "path", genFile)
}
return nil
}
var ListMisbehaviorCmd = &cobra.Command{
Use: "misbehaviors",
Short: "Lists possible misbehaviors",
RunE: listMisbehaviors,
}
func listMisbehaviors(cmd *cobra.Command, args []string) error {
str := "Currently registered misbehaviors: \n"
for key := range cs.MisbehaviorList {
str += fmt.Sprintf("- %s\n", key)
}
fmt.Println(str)
return nil
}