Skip to content

Commit

Permalink
fix: support custom p2p port for gentx/validator creation (cosmos#12075)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored May 30, 2022
1 parent dc110e6 commit 3504dd3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (CLI) [#12075](https://github.com/cosmos/cosmos-sdk/pull/12075) Add `p2p-port` to the `gentx` and `create-validator` CLI commands to support custom P2P ports.
* [#11969](https://github.com/cosmos/cosmos-sdk/pull/11969) Fix the panic error in `x/upgrade` when `AppVersion` is not set.
* (tests) [\#11940](https://github.com/cosmos/cosmos-sdk/pull/11940) Fix some client tests in the `x/gov` module
* [\#11772](https://github.com/cosmos/cosmos-sdk/pull/11772) Limit types.Dec length to avoid overflow.
Expand Down
1 change: 1 addition & 0 deletions x/staking/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
FlagGenesisFormat = "genesis-format"
FlagNodeID = "node-id"
FlagIP = "ip"
FlagP2PPort = "p2p-port"
)

// common flagsets to add to various functions
Expand Down
41 changes: 28 additions & 13 deletions x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,11 @@ func newBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl
genOnly, _ := fs.GetBool(flags.FlagGenerateOnly)
if genOnly {
ip, _ := fs.GetString(FlagIP)
p2pPort, _ := fs.GetUint(FlagP2PPort)
nodeID, _ := fs.GetString(FlagNodeID)

if nodeID != "" && ip != "" {
txf = txf.WithMemo(fmt.Sprintf("%s@%s:26656", nodeID, ip))
if nodeID != "" && ip != "" && p2pPort > 0 {
txf = txf.WithMemo(fmt.Sprintf("%s@%s:%d", nodeID, ip, p2pPort))
}
}

Expand All @@ -411,7 +412,8 @@ func newBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl
// this is anticipated to be used with the gen-tx
func CreateValidatorMsgFlagSet(ipDefault string) (fs *flag.FlagSet, defaultsDesc string) {
fsCreateValidator := flag.NewFlagSet("", flag.ContinueOnError)
fsCreateValidator.String(FlagIP, ipDefault, "The node's public IP")
fsCreateValidator.String(FlagIP, ipDefault, "The node's public P2P IP")
fsCreateValidator.Uint(FlagP2PPort, 26656, "The node's public P2P port")
fsCreateValidator.String(FlagNodeID, "", "The node's NodeID")
fsCreateValidator.String(FlagMoniker, "", "The validator's (optional) moniker")
fsCreateValidator.String(FlagWebsite, "", "The validator's (optional) website")
Expand Down Expand Up @@ -451,6 +453,7 @@ type TxCreateValidatorConfig struct {
PubKey cryptotypes.PubKey

IP string
P2PPort uint
Website string
SecurityContact string
Details string
Expand All @@ -464,35 +467,35 @@ func PrepareConfigForTxCreateValidator(flagSet *flag.FlagSet, moniker, nodeID, c
if err != nil {
return c, err
}

if ip == "" {
_, _ = fmt.Fprintf(os.Stderr, "couldn't retrieve an external IP; "+
"the tx's memo field will be unset")
_, _ = fmt.Fprintf(os.Stderr, "failed to retrieve an external IP; the tx's memo field will be unset")
}

p2pPort, err := flagSet.GetUint(FlagP2PPort)
if err != nil {
return c, err
}
c.IP = ip

website, err := flagSet.GetString(FlagWebsite)
if err != nil {
return c, err
}
c.Website = website

securityContact, err := flagSet.GetString(FlagSecurityContact)
if err != nil {
return c, err
}
c.SecurityContact = securityContact

details, err := flagSet.GetString(FlagDetails)
if err != nil {
return c, err
}
c.SecurityContact = details

identity, err := flagSet.GetString(FlagIdentity)
if err != nil {
return c, err
}
c.Identity = identity

c.Amount, err = flagSet.GetString(FlagAmount)
if err != nil {
Expand All @@ -519,6 +522,11 @@ func PrepareConfigForTxCreateValidator(flagSet *flag.FlagSet, moniker, nodeID, c
return c, err
}

c.IP = ip
c.P2PPort = p2pPort
c.Website = website
c.SecurityContact = securityContact
c.Identity = identity
c.NodeID = nodeID
c.PubKey = valPubKey
c.Website = website
Expand Down Expand Up @@ -586,17 +594,24 @@ func BuildCreateValidatorMsg(clientCtx client.Context, config TxCreateValidatorC
}

msg, err := types.NewMsgCreateValidator(
sdk.ValAddress(valAddr), config.PubKey, amount, description, commissionRates, minSelfDelegation,
sdk.ValAddress(valAddr),
config.PubKey,
amount,
description,
commissionRates,
minSelfDelegation,
)
if err != nil {
return txBldr, msg, err
}

if generateOnly {
ip := config.IP
p2pPort := config.P2PPort
nodeID := config.NodeID

if nodeID != "" && ip != "" {
txBldr = txBldr.WithMemo(fmt.Sprintf("%s@%s:26656", nodeID, ip))
if nodeID != "" && ip != "" && p2pPort > 0 {
txBldr = txBldr.WithMemo(fmt.Sprintf("%s@%s:%d", nodeID, ip, p2pPort))
}
}

Expand Down
1 change: 1 addition & 0 deletions x/staking/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
IP: ip,
ChainID: chainID,
NodeID: nodeID,
P2PPort: 26656,
PubKey: valPubKey,
Moniker: moniker,
Amount: amount,
Expand Down

0 comments on commit 3504dd3

Please sign in to comment.