This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
config_options.go
164 lines (146 loc) · 5.39 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
package commands
import (
"fmt"
"strings"
"github.com/hyperledger/burrow/config"
"github.com/hyperledger/burrow/crypto"
cli "github.com/jawher/mow.cli"
)
type configOptions struct {
configFileOpt *string
genesisFileOpt *string
validatorIndexOpt *int
accountIndexOpt *int
initAddressOpt *string
initPassphraseOpt *string
initMonikerOpt *string
externalAddressOpt *string
}
const configFileSpec = "[--config=<config file>]"
var configFileOption = cli.StringOpt{
Name: "c config",
Desc: "Use the specified burrow config file",
EnvVar: "BURROW_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: "BURROW_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>] " +
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: "BURROW_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: "BURROW_VALIDATOR_INDEX",
}),
initAddressOpt: cmd.String(cli.StringOpt{
Name: "a address",
Desc: "The address of the signing key of this node",
EnvVar: "BURROW_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: "BURROW_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: "BURROW_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: "BURROW_EXTERNAL_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)
}
} else {
conf.Tendermint.Moniker = *opts.initMonikerOpt
}
if *opts.externalAddressOpt != "" {
conf.Tendermint.ExternalAddress = *opts.externalAddressOpt
}
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
}