/
doctor.go
518 lines (493 loc) · 16.4 KB
/
doctor.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
sdkserver "github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
"github.com/spf13/cobra"
"github.com/spf13/viper"
tmcfg "github.com/tendermint/tendermint/config"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/privval"
tmtypes "github.com/tendermint/tendermint/types"
tmversion "github.com/tendermint/tendermint/version"
"github.com/functionx/fx-core/v7/client/grpc"
"github.com/functionx/fx-core/v7/server"
fxcfg "github.com/functionx/fx-core/v7/server/config"
fxtypes "github.com/functionx/fx-core/v7/types"
)
const SPACE = " "
type blockchain interface {
GetChainId() (string, error)
GetBlockHeight() (int64, error)
GetSyncing() (bool, error)
GetNodeInfo() (*tmservice.VersionInfo, error)
CurrentPlan() (*upgradetypes.Plan, error)
GetConsensusValidators() ([]*tmservice.Validator, error)
}
func doctorCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "doctor",
Short: "Check your system for potential problems",
RunE: func(cmd *cobra.Command, _ []string) error {
serverCtx := sdkserver.GetServerContextFromCmd(cmd)
clientCtx := client.GetClientContextFromCmd(cmd)
printPrompt()
printOSInfo(serverCtx.Config.RootDir)
printSelfInfo()
chainId, err := checkGenesis(serverCtx.Config.GenesisFile())
if err != nil {
return err
}
if err := checkUpgradeInfo(serverCtx.Config.RootDir); err != nil {
return err
}
bc, err := getBlockchain(clientCtx, serverCtx)
if err != nil {
return err
}
needUpgrade, err := checkBlockchainData(bc, chainId, serverCtx.Config.PrivValidatorKeyFile())
if err != nil {
return err
}
if err = checkAppConfig(serverCtx.Viper); err != nil {
return err
}
if err = checkTmConfig(serverCtx.Config, needUpgrade); err != nil {
return err
}
if err = checkCosmovisor(serverCtx.Config.RootDir, bc); err != nil {
return err
}
return nil
},
}
cmd.Flags().String(flags.FlagHome, fxtypes.GetDefaultNodeHome(), "The application home directory")
cmd.Flags().String(flags.FlagNode, "tcp://localhost:26657", "<host>:<port> to Tendermint RPC interface for this chain")
cmd.Flags().String(flags.FlagGRPC, "", "the gRPC endpoint to use for this chain")
cmd.Flags().Bool(flags.FlagGRPCInsecure, false, "allow gRPC over insecure channels, if not TLS the server must use TLS")
return cmd
}
func printPrompt() {
fmt.Printf(`
Please note that these warnings are just used to help the fxCore maintainers
If everything you use "fxcored" for is working fine: please don't worry;
just ignore this. Thanks!
`)
fmt.Printf("\n")
}
func printOSInfo(home string) {
fmt.Printf("Computer Info:\n")
fmt.Printf("%sOS/Arch: %s/%s\n", SPACE, runtime.GOOS, runtime.GOARCH)
fmt.Printf("%sCPU: %d\n", SPACE, runtime.NumCPU())
memory, err := mem.VirtualMemory()
if err != nil {
return
}
fmt.Printf("%sMemory Total: %.2f GB, Available: %.2f GB, UsedPercent: %.2f%%\n",
SPACE, float64(memory.Total)/1024/1024/1024, float64(memory.Available)/1024/1024/1024, memory.UsedPercent)
usage, err := disk.Usage(home)
if err != nil {
return
}
fmt.Printf("%sDisk Total: %.2f GB, Free: %.2f GB, UsedPercent: %.2f%%\n",
SPACE, float64(usage.Total)/1024/1024/1024, float64(usage.Free)/1024/1024/1024, usage.UsedPercent)
fmt.Printf("%s%sPath: %s\n", SPACE, SPACE, usage.Path)
}
func printSelfInfo() {
fmt.Printf("fxcored Info:\n")
info := version.NewInfo()
fmt.Printf("%sVersion: %s\n", SPACE, info.Version)
fmt.Printf("%sGit Commit: %s\n", SPACE, info.GitCommit)
fmt.Printf("%sBuild Tags: %s\n", SPACE, info.BuildTags)
fmt.Printf("%sGo Version: %s\n", SPACE, runtime.Version())
fmt.Printf("%sCosmos SDK Version: %s\n", SPACE, info.CosmosSdkVersion)
fmt.Printf("%sTendermint Version: %s\n", SPACE, tmversion.TMCoreSemVer)
}
func checkGenesis(genesisFile string) (string, error) {
fmt.Printf("Genesis:\n")
fmt.Printf("%sFile: %s\n", SPACE, genesisFile)
genesisSha256, err := getGenesisSha256(genesisFile)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("%sWarning: Not found Genesis file!\n", SPACE)
return "", nil
}
return "", err
}
switch genesisSha256 {
case fxtypes.MainnetGenesisHash:
fmt.Printf("%sNetwork: Mainnet\n", SPACE)
return fxtypes.MainnetChainId, nil
case fxtypes.TestnetGenesisHash:
fmt.Printf("%sNetwork: Testnet\n", SPACE)
return fxtypes.TestnetChainId, nil
default:
fmt.Printf("%sWarning: Unknown Network!\n", SPACE)
return "Unknown", nil
}
}
func checkUpgradeInfo(homeDir string) error {
file := filepath.Join(homeDir, "data", upgradetypes.UpgradeInfoFilename)
data, err := os.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("read upgrade info error: %s", err.Error())
}
upgradeInfo := new(upgradetypes.Plan)
if err := json.Unmarshal(data, upgradeInfo); err != nil {
return err
}
fmt.Printf("Upgrade Info:\n")
fmt.Printf("%sFile: %s\n", SPACE, file)
fmt.Printf("%sName: %s\n", SPACE, upgradeInfo.Name)
fmt.Printf("%sHeight: %d\n", SPACE, upgradeInfo.Height)
return nil
}
func getBlockchain(cliCtx client.Context, serverCtx *sdkserver.Context) (blockchain, error) {
fmt.Printf("Blockchain Data:\n")
grpcAddr := serverCtx.Viper.GetString(flags.FlagGRPC)
newClient := grpc.NewClient(cliCtx)
_, err := newClient.GetBlockHeight()
if err == nil {
fmt.Printf("%sRemote Node: %s %s\n", SPACE, cliCtx.NodeURI, grpcAddr)
return newClient, nil
}
if len(grpcAddr) > 0 {
return nil, err
}
if len(serverCtx.Config.RootDir) == 0 {
fmt.Printf("%sWarning: Not found root dir\n", SPACE)
return nil, nil
}
database, err := server.NewDatabase(serverCtx.Config, cliCtx.Codec)
if err != nil {
return nil, err
}
if database == nil {
fmt.Printf("%sWarning: Not found data file!\n", SPACE)
return nil, nil
}
fmt.Printf("%sData Dir: %s/data\n", SPACE, serverCtx.Config.RootDir)
return database, nil
}
//gocyclo:ignore
func checkBlockchainData(bc blockchain, genesisId, privValidatorKeyFile string) (bool, error) {
if bc == nil {
return false, nil
}
chainId, err := bc.GetChainId()
if err != nil {
fmt.Printf("%sWarning: %s\n", SPACE, err.Error())
return false, nil
}
if len(chainId) > 0 {
fmt.Printf("%sChain ID: %s\n", SPACE, chainId)
}
blockHeight, err := bc.GetBlockHeight()
if err != nil {
return false, err
}
if blockHeight > 0 {
fmt.Printf("%sBlock Height: %d\n", SPACE, blockHeight)
}
syncing, err := bc.GetSyncing()
if err != nil {
return false, nil
}
fmt.Printf("%sSyncing: %v\n", SPACE, syncing)
pvKey := privval.FilePVKey{}
keyJSONBytes, err := os.ReadFile(privValidatorKeyFile)
if err != nil {
return false, err
}
if err = tmjson.Unmarshal(keyJSONBytes, &pvKey); err != nil {
return false, err
}
validators, err := bc.GetConsensusValidators()
if err != nil {
return false, err
}
for _, validator := range validators {
if strings.EqualFold(
sdk.ConsAddress(pvKey.Address.Bytes()).String(),
validator.GetAddress(),
) {
fmt.Printf("%sNode Type: This node is a validator\n", SPACE)
}
}
app, err := bc.GetNodeInfo()
if err != nil {
return false, err
}
fmt.Printf("%sNode Info: \n", SPACE)
if len(app.Version) > 0 {
fmt.Printf("%s%sVersion: %s\n", SPACE, SPACE, app.Version)
}
if len(app.GitCommit) > 0 {
fmt.Printf("%s%sGit Commit: %s\n", SPACE, SPACE, app.GitCommit)
}
if len(app.BuildTags) > 0 {
fmt.Printf("%s%sBuild Tags: %s\n", SPACE, SPACE, app.BuildTags)
}
if len(app.GoVersion) > 0 {
fmt.Printf("%s%sGo Version: %s\n", SPACE, SPACE, app.GoVersion)
}
if len(app.CosmosSdkVersion) > 0 {
fmt.Printf("%s%sCosmos SDK Version: %s\n", SPACE, SPACE, app.CosmosSdkVersion)
}
plan, err := bc.CurrentPlan()
if err != nil {
return false, err
}
if plan != nil {
fmt.Printf("%sCurrent Upgrade Plan:\n", SPACE)
fmt.Printf("%s%sName: %s\n", SPACE, SPACE, plan.Name)
fmt.Printf("%s%sHeight: %d\n", SPACE, SPACE, plan.Height)
}
if len(chainId) > 0 && chainId != genesisId {
fmt.Printf("%s%sWarning: The remote node chainId(%s) does not match the local genesis chainId(%s)\n", SPACE, SPACE, chainId, genesisId)
return false, nil
}
if chainId == fxtypes.MainnetChainId {
if blockHeight < fxtypes.MainnetBlockHeightV2 {
fmt.Printf("%sVersion: V1\n", SPACE)
} else if blockHeight < fxtypes.MainnetBlockHeightV3 {
fmt.Printf("%sVersion: V2\n", SPACE)
} else if blockHeight < fxtypes.MainnetBlockHeightV4 {
fmt.Printf("%sVersion: v3\n", SPACE)
} else if blockHeight < fxtypes.MainnetBlockHeightV5 {
fmt.Printf("%sVersion: V4\n", SPACE)
} else if blockHeight < fxtypes.MainnetBlockHeightV6 {
fmt.Printf("%sVersion: V5\n", SPACE)
}
}
if chainId == fxtypes.TestnetChainId {
if blockHeight < fxtypes.TestnetBlockHeightV2 {
fmt.Printf("%sVersion: V1\n", SPACE)
} else if blockHeight < fxtypes.TestnetBlockHeightV3 {
fmt.Printf("%sVersion: V2\n", SPACE)
} else if blockHeight < fxtypes.TestnetBlockHeightV4 {
fmt.Printf("%sVersion: V3\n", SPACE)
} else if blockHeight < fxtypes.TestnetBlockHeightV41 {
fmt.Printf("%sVersion: V4\n", SPACE)
} else if blockHeight < fxtypes.TestnetBlockHeightV42 {
fmt.Printf("%sVersion: V4.1\n", SPACE)
} else if blockHeight < fxtypes.TestnetBlockHeightV5 {
fmt.Printf("%sVersion: V4.2\n", SPACE)
} else if blockHeight < fxtypes.TestnetBlockHeightV6 {
fmt.Printf("%sVersion: V5.0\n", SPACE)
} else if blockHeight < fxtypes.TestnetBlockHeightV71 {
fmt.Printf("%sVersion: V6.0\n", SPACE)
}
}
return plan != nil, nil
}
func checkAppConfig(viper *viper.Viper) error {
fmt.Printf("App Config:\n")
fmt.Printf("%sFile: %s\n", SPACE, viper.ConfigFileUsed())
config.SetConfigTemplate(fxcfg.DefaultConfigTemplate())
appConfig := fxcfg.DefaultConfig()
if err := viper.Unmarshal(appConfig); err != nil {
return err
}
if err := appConfig.ValidateBasic(); err != nil {
fmt.Printf("%sWarning: %s\n", SPACE, err.Error())
}
if appConfig.TLS.KeyPath == "" && appConfig.TLS.CertificatePath != "" {
fmt.Printf("%sWarning: certificate_path is set, but key_path is not set\n", SPACE)
}
if appConfig.TLS.KeyPath != "" && appConfig.TLS.CertificatePath == "" {
fmt.Printf("%sWarning: key_path is set, but certificate_path is not set\n", SPACE)
}
if appConfig.IAVLCacheSize > 781250 {
fmt.Printf("%sWarning: if the node does not require high API query, you can appropriately reduce iavl_cache_size to reduce memory usage\n", SPACE)
}
return nil
}
func checkTmConfig(config *tmcfg.Config, needUpgrade bool) error {
fmt.Printf("Tendermint Config:\n")
fmt.Printf("%sFile: %s/config/config.toml\n", SPACE, config.RootDir)
if err := config.ValidateBasic(); err != nil {
fmt.Printf("%sWarning: ", err.Error())
}
if needUpgrade && config.Consensus.DoubleSignCheckHeight > 0 {
fmt.Printf("%sWarning: double_sign_check_height is greater than 0\n", SPACE)
fmt.Printf("%s%sPlease check the upgrade plan and set double_sign_check_height to 0\n", SPACE, SPACE)
fmt.Printf("%s%sIf you are sure that the upgrade has been completed, you can ignore this warning\n", SPACE, SPACE)
}
if config.P2P.Seeds == "" {
fmt.Printf("%sWarning: seeds is empty\n", SPACE)
}
if config.RPC.TLSKeyFile == "" && config.RPC.TLSCertFile != "" {
fmt.Printf("%sWarning: tls_cert_file is not empty, but tls_key_file is empty\n", SPACE)
}
if config.RPC.TLSKeyFile != "" && config.RPC.TLSCertFile == "" {
fmt.Printf("%sWarning: tls_key_file is not empty, but tls_cert_file is empty\n", SPACE)
}
return nil
}
//gocyclo:ignore
func checkCosmovisor(rootPath string, bc blockchain) error {
cosmovisorPath := filepath.Join(rootPath, "cosmovisor")
if _, err := os.Stat(cosmovisorPath); err != nil {
if os.IsNotExist(err) {
fmt.Printf("Cosmovisor: %s\n", "not found")
return nil
}
return err
}
defer func() {
_ = printDirectory(cosmovisorPath, 0, []bool{false}, SPACE)
}()
fmt.Printf("Cosmovisor:\n")
for _, dir := range []string{"current", "genesis"} {
fmt.Printf("%s%s%s:\n", SPACE, strings.ToUpper(dir[:1]), dir[1:])
fxcored := filepath.Join(cosmovisorPath, dir, "bin/fxcored")
fmt.Printf("%s%sBinary File: %s\n", SPACE, SPACE, fxcored)
output, err := exec.Command(fxcored, "version").Output()
if err != nil {
fmt.Printf("%s%sWarning: %s\n", SPACE, SPACE, err.Error())
return nil
}
v := string(bytes.Trim(output, "\n"))
fmt.Printf("%s%sfxcored version: %s\n", SPACE, SPACE, v)
}
upgradesPath := filepath.Join(cosmovisorPath, "upgrades")
fmt.Printf("%sUpgrades:\n", SPACE)
fmt.Printf("%s%sPath: %s\n", SPACE, SPACE, upgradesPath)
entries, err := os.ReadDir(upgradesPath)
if err != nil {
fmt.Printf("%s%sWarning: %s\n", SPACE, SPACE, err.Error())
return nil
}
plan, _ := bc.CurrentPlan()
var planVersion bool
for _, entry := range entries {
if !entry.IsDir() {
continue
}
if plan != nil && plan.Name == entry.Name() {
planVersion = true
}
fmt.Printf("%s%s%s:\n", SPACE, SPACE, entry.Name())
fxcored := filepath.Join(upgradesPath, entry.Name(), "bin/fxcored")
fmt.Printf("%s%s%sBinary File: %s\n", SPACE, SPACE, SPACE, fxcored)
output, err := exec.Command(fxcored, "version").Output()
if err != nil {
fmt.Printf("%s%s%sWarning: %s\n", SPACE, SPACE, SPACE, err.Error())
continue
}
v := string(bytes.Trim(output, "\n"))
fmt.Printf("%s%s%sfxcored version: %s\n", SPACE, SPACE, SPACE, v)
if !(strings.HasPrefix(v, "release/v"+entry.Name()[len(entry.Name())-1:]) || strings.HasPrefix(v, "release/"+entry.Name())) {
fmt.Printf("%s%s%sWarning: fxcored version is not match upgrade plan\n", SPACE, SPACE, SPACE)
}
upgradeInfoFile := filepath.Join(upgradesPath, entry.Name(), upgradetypes.UpgradeInfoFilename)
upgradeInfo, err := os.ReadFile(upgradeInfoFile)
if err != nil {
if !os.IsNotExist(err) {
fmt.Printf("%s%s%sWarning: %s\n", SPACE, SPACE, SPACE, err.Error())
}
continue
}
fmt.Printf("%s%s%sUpgrade Info File: %s\n", SPACE, SPACE, SPACE, upgradeInfoFile)
var plan upgradetypes.Plan
if err := json.Unmarshal(upgradeInfo, &plan); err != nil {
fmt.Printf("%s%s%sWarning: %s\n", SPACE, SPACE, SPACE, err.Error())
continue
}
fmt.Printf("%s%s%sUpgrade Plan: %s %d\n", SPACE, SPACE, SPACE, plan.Name, plan.Height)
if plan.Name != entry.Name() {
fmt.Printf("%s%s%sWarning: fxcored version is not match upgrade plan\n", SPACE, SPACE, SPACE)
}
}
if plan != nil && !planVersion {
fmt.Printf("%s%sWarning: current upgrade plan is not found in cosmovisor\n", SPACE, SPACE)
}
return nil
}
func printDirectory(path string, depth int, last []bool, tab string) error {
printPath := path
if depth > 0 {
printPath = filepath.Base(path)
}
printTree(printPath, depth, last, tab)
entries, err := os.ReadDir(path)
if err != nil {
return err
}
for idx, entry := range entries {
currentLast := idx == len(entries)-1
info, err := entry.Info()
if err != nil {
return err
}
if (info.Mode() & os.ModeSymlink) == os.ModeSymlink {
fullPath, err := os.Readlink(filepath.Join(path, entry.Name()))
if err != nil {
return err
} else {
printTree(entry.Name()+" -> "+fullPath, depth+1, append(last, currentLast), tab)
}
} else if entry.IsDir() {
if err = printDirectory(filepath.Join(path, entry.Name()), depth+1, append(last, currentLast), tab); err != nil {
return err
}
} else {
printTree(entry.Name(), depth+1, append(last, currentLast), tab)
}
}
return nil
}
func printTree(entry string, depth int, last []bool, tab string) {
if depth == 0 {
fmt.Printf("%s%s\n", tab, entry)
} else {
indent := ""
newLast := last[1:]
for i := 0; i < len(newLast)-1; i++ {
if newLast[i] {
indent = fmt.Sprintf("%s ", indent)
} else {
indent = fmt.Sprintf("%s│ ", indent)
}
}
sepStr := "├── "
if last[len(last)-1] {
sepStr = "└── "
}
fmt.Printf("%s%s%s%s\n", tab, indent, sepStr, entry)
}
}
func getGenesisSha256(genesisFile string) (string, error) {
genesisFileData, err := os.ReadFile(genesisFile)
if err != nil {
return "", err
}
genesisDoc, err := tmtypes.GenesisDocFromJSON(genesisFileData)
if err != nil {
return "", err
}
genesisBytes, err := tmjson.Marshal(genesisDoc)
if err != nil {
return "", err
}
return fxtypes.Sha256Hex(genesisBytes), nil
}