/
config_options.go
194 lines (172 loc) · 6.22 KB
/
config_options.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
package commands
import (
"fmt"
"net"
"strings"
cli "github.com/jawher/mow.cli"
"github.com/klyed/hivesmartchain/config"
"github.com/klyed/hivesmartchain/crypto"
)
type configOptions struct {
configFileOpt *string
genesisFileOpt *string
validatorIndexOpt *int
accountIndexOpt *int
initAddressOpt *string
initPassphraseOpt *string
initMonikerOpt *string
externalAddressOpt *string
grpcAddressOpt *string
}
const configFileSpec = "[--config=<config file>]"
var configFileOption = cli.StringOpt{
Name: "c config",
Desc: "Use the specified Hive Smart Chain config file",
EnvVar: "HSC_CONFIG_FILE",
}
const genesisFileSpec = "[--genesis=<genesis json file>]"
var genesisFileOption = cli.StringOpt{
Name: "g genesis",
Desc: "Use the specified genesis JSON file rather than a key in the main config, use - to read from STDIN",
EnvVar: "HSC_GENESIS_FILE",
}
func addConfigOptions(cmd *cli.Cmd) *configOptions {
spec := "[--moniker=<human readable moniker>] " +
"[--index=<index of account in GenesisDoc> " +
"|--validator=<index of validator in GenesisDoc> " +
"|--address=<address of signing key>] " +
"[--passphrase=<secret passphrase to unlock key>] " +
"[--external-address=<hostname:port>] " +
"[--grpc-address=<hostname:port>] " +
configFileSpec + " " + genesisFileSpec
cmd.Spec = strings.Join([]string{cmd.Spec, spec}, " ")
return &configOptions{
accountIndexOpt: cmd.Int(cli.IntOpt{
Name: "i index",
Desc: "Account index (in accounts list - GenesisSpec or GenesisDoc) from which to set Address",
Value: -1,
EnvVar: "HSC_ACCOUNT_INDEX",
}),
validatorIndexOpt: cmd.Int(cli.IntOpt{
Name: "v validator",
Desc: "Validator index (in validators list - GenesisSpec or GenesisDoc) from which to set Address",
Value: -1,
EnvVar: "HSC_VALIDATOR_INDEX",
}),
initAddressOpt: cmd.String(cli.StringOpt{
Name: "a address",
Desc: "The address of the signing key of this node",
EnvVar: "HSC_ADDRESS",
}),
initPassphraseOpt: cmd.String(cli.StringOpt{
Name: "p passphrase",
Desc: "The passphrase of the signing key of this node (currently unimplemented but planned for future version of our KeyClient interface)",
EnvVar: "HSC_PASSPHRASE",
}),
initMonikerOpt: cmd.String(cli.StringOpt{
Name: "m moniker",
Desc: "An optional human-readable moniker to identify this node amongst Tendermint peers in logs and status queries",
EnvVar: "HSC_NODE_MONIKER",
}),
externalAddressOpt: cmd.String(cli.StringOpt{
Name: "x external-address",
Desc: "An external address or host name provided with the port that this node will broadcast over gossip in order for other nodes to connect",
EnvVar: "HSC_EXTERNAL_ADDRESS",
}),
grpcAddressOpt: cmd.String(cli.StringOpt{
Name: "grpc-address",
Desc: "GRPC listen address",
EnvVar: "HSC_GRPC_ADDRESS",
}),
configFileOpt: cmd.String(configFileOption),
genesisFileOpt: cmd.String(genesisFileOption),
}
}
func (opts *configOptions) obtainBurrowConfig() (*config.BurrowConfig, error) {
conf, err := obtainDefaultConfig(*opts.configFileOpt, *opts.genesisFileOpt)
if err != nil {
return nil, err
}
// Which account am I?
conf.ValidatorAddress, err = accountAddress(conf, *opts.initAddressOpt, *opts.accountIndexOpt, *opts.validatorIndexOpt)
if err != nil {
return nil, err
}
if *opts.initPassphraseOpt != "" {
conf.Passphrase = opts.initPassphraseOpt
}
if *opts.initMonikerOpt == "" {
chainIDHeader := ""
if conf.GenesisDoc != nil && conf.GenesisDoc.ChainID != "" {
chainIDHeader = conf.GenesisDoc.ChainID
}
if conf.ValidatorAddress != nil {
// Set a default moniker... since we can at this stage of config completion and it is required for start
conf.Tendermint.Moniker = fmt.Sprintf("%sNode_%s", chainIDHeader, conf.ValidatorAddress)
}
/*
chainIDHeader := "69"
if conf.GenesisDoc != nil && conf.GenesisDoc.ChainID() != "" {
chainIDHeader = fmt.Sprintf("%s", chainIDHeader)
}
if conf.ValidatorAddress != nil {
// Set a default moniker... since we can at this stage of config completion and it is required for start
conf.Tendermint.Moniker = fmt.Sprintf("%s", conf.Tendermint.Moniker)
}
*/
} else {
conf.Tendermint.Moniker = *opts.initMonikerOpt
}
if *opts.externalAddressOpt != "" {
conf.Tendermint.ExternalAddress = *opts.externalAddressOpt
}
if *opts.grpcAddressOpt != "" {
host, port, err := net.SplitHostPort(*opts.grpcAddressOpt)
if err != nil {
return nil, fmt.Errorf("could not parse GRPC listen addres: %w", err)
}
conf.RPC.GRPC.ListenHost = host
conf.RPC.GRPC.ListenPort = port
}
return conf, nil
}
// address is sourced in the following order:
// 1. explicitly from cli
// 2. genesis accounts (by index)
// 3. genesis validators (by index)
// 4. config
// 5. genesis validator (if only one)
func accountAddress(conf *config.BurrowConfig, addressIn string, accIndex, valIndex int) (*crypto.Address, error) {
if addressIn != "" {
address, err := crypto.AddressFromHexString(addressIn)
if err != nil {
return nil, fmt.Errorf("could not read address for account in '%s': %v", addressIn, err)
}
return &address, nil
} else if accIndex > -1 {
if conf.GenesisDoc == nil {
return nil, fmt.Errorf("unable to set Address from provided index since no " +
"GenesisDoc/GenesisSpec provided")
}
if accIndex >= len(conf.GenesisDoc.Accounts) {
return nil, fmt.Errorf("index of %v given but only %v accounts specified in GenesisDoc",
accIndex, len(conf.GenesisDoc.Accounts))
}
return &conf.GenesisDoc.Accounts[accIndex].Address, nil
} else if valIndex > -1 {
if conf.GenesisDoc == nil {
return nil, fmt.Errorf("unable to set Address from provided validator since no " +
"GenesisDoc/GenesisSpec provided")
}
if valIndex >= len(conf.GenesisDoc.Validators) {
return nil, fmt.Errorf("validator index of %v given but only %v validators specified in GenesisDoc",
valIndex, len(conf.GenesisDoc.Validators))
}
return &conf.GenesisDoc.Validators[valIndex].Address, nil
} else if conf.ValidatorAddress != nil {
return conf.ValidatorAddress, nil
} else if conf.GenesisDoc != nil && len(conf.GenesisDoc.Validators) == 1 {
return &conf.GenesisDoc.Validators[0].Address, nil
}
return nil, nil
}