-
Notifications
You must be signed in to change notification settings - Fork 30
/
oc_cmds.go
155 lines (136 loc) · 3.98 KB
/
oc_cmds.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
package server
// DONTCOVER
import (
"fmt"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
cfg "github.com/Finschia/ostracon/config"
osjson "github.com/Finschia/ostracon/libs/json"
ostos "github.com/Finschia/ostracon/libs/os"
"github.com/Finschia/ostracon/node"
"github.com/Finschia/ostracon/p2p"
pvm "github.com/Finschia/ostracon/privval"
"github.com/Finschia/ostracon/types"
ostversion "github.com/Finschia/ostracon/version"
sdk "github.com/Finschia/finschia-sdk/types"
)
// ShowNodeIDCmd - ported from Ostracon, dump node ID to stdout
func ShowNodeIDCmd() *cobra.Command {
return &cobra.Command{
Use: "show-node-id",
Short: "Show this node's ID",
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config
nodeKey, err := p2p.LoadNodeKey(cfg.NodeKeyFile())
if err != nil {
return err
}
fmt.Println(nodeKey.ID())
return nil
},
}
}
// ShowValidatorCmd - ported from Ostracon, show this node's validator info
func ShowValidatorCmd() *cobra.Command {
cmd := cobra.Command{
Use: "show-validator",
Short: "Show this node's ostracon validator info",
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config
return showValidator(cmd, cfg)
},
}
return &cmd
}
func showValidator(cmd *cobra.Command, config *cfg.Config) error {
var pv types.PrivValidator
if config.PrivValidatorListenAddr != "" {
chainID, err := loadChainID(config)
if err != nil {
return err
}
serverCtx := GetServerContextFromCmd(cmd)
log := serverCtx.Logger
pv, err = node.CreateAndStartPrivValidatorSocketClient(config.PrivValidatorListenAddr, chainID, log)
if err != nil {
return err
}
} else {
keyFilePath := config.PrivValidatorKeyFile()
if !ostos.FileExists(keyFilePath) {
return fmt.Errorf("private validator file %s does not exist", keyFilePath)
}
pv = pvm.LoadFilePV(keyFilePath, config.PrivValidatorStateFile())
}
pubKey, err := pv.GetPubKey()
if err != nil {
return fmt.Errorf("can't get pubkey: %w", err)
}
bz, err := osjson.Marshal(pubKey)
if err != nil {
return fmt.Errorf("failed to marshal private validator pubkey: %w", err)
}
fmt.Println(string(bz))
return nil
}
func loadChainID(config *cfg.Config) (string, error) {
stateDB, err := node.DefaultDBProvider(&node.DBContext{ID: "state", Config: config})
if err != nil {
return "", err
}
defer func() {
var _ = stateDB.Close()
}()
genesisDocProvider := node.DefaultGenesisDocProviderFunc(config)
_, genDoc, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genesisDocProvider)
if err != nil {
return "", err
}
return genDoc.ChainID, nil
}
// ShowAddressCmd - show this node's validator address
func ShowAddressCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show-address",
Short: "Shows this node's ostracon validator consensus address",
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config
privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
valConsAddr := (sdk.ConsAddress)(privValidator.GetAddress())
fmt.Println(valConsAddr.String())
return nil
},
}
return cmd
}
// VersionCmd prints ostracon and ABCI version numbers.
func VersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print ostracon libraries' version",
Long: `Print protocols' and libraries' version numbers
against which this app has been compiled.
`,
RunE: func(cmd *cobra.Command, args []string) error {
bs, err := yaml.Marshal(&struct {
Ostracon string
ABCI string
BlockProtocol uint64
P2PProtocol uint64
}{
Ostracon: ostversion.OCCoreSemVer,
ABCI: ostversion.ABCIVersion,
BlockProtocol: ostversion.BlockProtocol,
P2PProtocol: ostversion.P2PProtocol,
})
if err != nil {
return err
}
fmt.Println(string(bs))
return nil
},
}
}