forked from hyperledger-archives/burrow
/
config_options.go
114 lines (103 loc) · 3.93 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
package commands
import (
"fmt"
"strings"
"github.com/hyperledger/burrow/config"
"github.com/hyperledger/burrow/crypto"
cli "github.com/jawher/mow.cli"
)
type configOptions struct {
validatorIndexOpt *int
validatorAddressOpt *string
validatorPassphraseOpt *string
validatorMonikerOpt *string
externalAddressOpt *string
}
func addConfigOptions(cmd *cli.Cmd) *configOptions {
spec := "[--validator-moniker=<human readable moniker>] " +
"[--validator-index=<index of validator in GenesisDoc> | --validator-address=<address of validator signing key>] " +
"[--validator-passphrase=<secret passphrase to unlock validator key>] " +
"[--external-address=<hostname:port>]"
cmd.Spec = strings.Join([]string{cmd.Spec, spec}, " ")
return &configOptions{
validatorIndexOpt: cmd.Int(cli.IntOpt{
Name: "v validator-index",
Desc: "Validator index (in validators list - GenesisSpec or GenesisDoc) from which to set ValidatorAddress",
Value: -1,
EnvVar: "BURROW_VALIDATOR_INDEX",
}),
validatorAddressOpt: cmd.String(cli.StringOpt{
Name: "a validator-address",
Desc: "The address of the the signing key of this validator",
EnvVar: "BURROW_VALIDATOR_ADDRESS",
}),
validatorPassphraseOpt: cmd.String(cli.StringOpt{
Name: "p validator-passphrase",
Desc: "The passphrase of the signing key of this validator (currently unimplemented but planned for future version of our KeyClient interface)",
EnvVar: "BURROW_VALIDATOR_PASSPHRASE",
}),
validatorMonikerOpt: cmd.String(cli.StringOpt{
Name: "m validator-moniker",
Desc: "An optional human-readable moniker to identify this validator amongst Tendermint peers in logs and status queries",
EnvVar: "BURROW_VALIDATOR_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",
}),
}
}
func (opts *configOptions) configure(conf *config.BurrowConfig) error {
var err error
// Which validator am I?
conf.ValidatorAddress, err = validatorAddress(conf, *opts.validatorAddressOpt, *opts.validatorIndexOpt)
if err != nil {
return err
}
if *opts.validatorPassphraseOpt != "" {
conf.ValidatorPassphrase = opts.validatorPassphraseOpt
}
if *opts.validatorMonikerOpt == "" {
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("%sValidator_%s", chainIDHeader, conf.ValidatorAddress)
}
} else {
conf.Tendermint.Moniker = *opts.validatorMonikerOpt
}
if *opts.externalAddressOpt != "" {
conf.Tendermint.ExternalAddress = *opts.externalAddressOpt
}
return nil
}
func validatorAddress(conf *config.BurrowConfig, addressString string, index int) (*crypto.Address, error) {
if addressString != "" {
address, err := crypto.AddressFromHexString(addressString)
if err != nil {
return nil, fmt.Errorf("could not read address for validator in '%s': %v", addressString, err)
}
return &address, nil
} else if index > -1 {
if conf.GenesisDoc == nil {
return nil, fmt.Errorf("unable to set ValidatorAddress from provided validator-index since no " +
"GenesisDoc/GenesisSpec provided")
}
if index >= len(conf.GenesisDoc.Validators) {
return nil, fmt.Errorf("validator-index of %v given but only %v validators specified in GenesisDoc",
index, len(conf.GenesisDoc.Validators))
}
return &conf.GenesisDoc.Validators[index].Address, nil
}
if conf.ValidatorAddress != nil {
return conf.ValidatorAddress, nil
}
if conf.GenesisDoc != nil && len(conf.GenesisDoc.Validators) == 1 {
return &conf.GenesisDoc.Validators[0].Address, nil
}
return nil, nil
}