Skip to content

Commit

Permalink
fix MsgEditValidator JSON tag (#5342)
Browse files Browse the repository at this point in the history
* add json tag to MsgEditValidator; closes #5336

* changelog

* Apply suggestions from code review

Co-Authored-By: Alessio Treglia <alessio@tendermint.com>

* format

* changelog minor fix
  • Loading branch information
fedekunze authored Dec 3, 2019
1 parent b862e27 commit b9c40ea
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ if the provided arguments are invalid.
* (rest) [\#4783](https://github.com/cosmos/cosmos-sdk/issues/4783) The balance field in the DelegationResponse type is now sdk.Coin instead of sdk.Int
* (x/auth) [\#5006](https://github.com/cosmos/cosmos-sdk/pull/5006) The gas required to pass the `AnteHandler` has
increased significantly due to modular `AnteHandler` support. Increase GasLimit accordingly.
* (rest) [\#5336](https://github.com/cosmos/cosmos-sdk/issues/5336) `MsgEditValidator` uses `description` instead of `Description` as a JSON key.

### Features

Expand Down
3 changes: 2 additions & 1 deletion x/staking/legacy/v0_36/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package v0_36
import (
"time"

"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
v034staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v0_34"
"github.com/tendermint/tendermint/crypto"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion x/staking/legacy/v0_38/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package v0_38
import (
"time"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
v034staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v0_34"
v036staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v0_36"
Expand Down
84 changes: 54 additions & 30 deletions x/staking/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type msgCreateValidatorJSON struct {
Value sdk.Coin `json:"value" yaml:"value"`
}

// Default way to create validator. Delegator address and validator address are the same
// NewMsgCreateValidator creates a new MsgCreateValidator instance.
// Delegator address and validator address are the same.
func NewMsgCreateValidator(
valAddr sdk.ValAddress, pubKey crypto.PubKey, selfDelegation sdk.Coin,
description Description, commission CommissionRates, minSelfDelegation sdk.Int,
Expand All @@ -59,18 +60,21 @@ func NewMsgCreateValidator(
}
}

//nolint
// Route implements the sdk.Msg interface.
func (msg MsgCreateValidator) Route() string { return RouterKey }
func (msg MsgCreateValidator) Type() string { return "create_validator" }

// Return address(es) that must sign over msg.GetSignBytes()
// Type implements the sdk.Msg interface.
func (msg MsgCreateValidator) Type() string { return "create_validator" }

// GetSigners implements the sdk.Msg interface. It returns the address(es) that
// must sign over msg.GetSignBytes().
// If the validator address is not same as delegator's, then the validator must
// sign the msg as well.
func (msg MsgCreateValidator) GetSigners() []sdk.AccAddress {
// delegator is first signer so delegator pays fees
addrs := []sdk.AccAddress{msg.DelegatorAddress}

if !bytes.Equal(msg.DelegatorAddress.Bytes(), msg.ValidatorAddress.Bytes()) {
// if validator addr is not same as delegator addr, validator must sign
// msg as well
addrs = append(addrs, sdk.AccAddress(msg.ValidatorAddress))
}
return addrs
Expand Down Expand Up @@ -113,7 +117,7 @@ func (msg *MsgCreateValidator) UnmarshalJSON(bz []byte) error {
return nil
}

// custom marshal yaml function due to consensus pubkey
// MarshalYAML implements a custom marshal yaml function due to consensus pubkey.
func (msg MsgCreateValidator) MarshalYAML() (interface{}, error) {
bs, err := yaml.Marshal(struct {
Description Description
Expand Down Expand Up @@ -146,7 +150,7 @@ func (msg MsgCreateValidator) GetSignBytes() []byte {
return sdk.MustSortJSON(bz)
}

// quick validity check
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgCreateValidator) ValidateBasic() sdk.Error {
// note that unmarshaling from bech32 ensures either empty or valid
if msg.DelegatorAddress.Empty() {
Expand Down Expand Up @@ -182,7 +186,7 @@ func (msg MsgCreateValidator) ValidateBasic() sdk.Error {

// MsgEditValidator - struct for editing a validator
type MsgEditValidator struct {
Description
Description Description `json:"description" yaml:"description"`
ValidatorAddress sdk.ValAddress `json:"address" yaml:"address"`

// We pass a reference to the new commission rate and min self delegation as it's not mandatory to
Expand All @@ -194,6 +198,7 @@ type MsgEditValidator struct {
MinSelfDelegation *sdk.Int `json:"min_self_delegation" yaml:"min_self_delegation"`
}

// NewMsgEditValidator creates a new MsgEditValidator instance
func NewMsgEditValidator(valAddr sdk.ValAddress, description Description, newRate *sdk.Dec, newMinSelfDelegation *sdk.Int) MsgEditValidator {
return MsgEditValidator{
Description: description,
Expand All @@ -203,20 +208,24 @@ func NewMsgEditValidator(valAddr sdk.ValAddress, description Description, newRat
}
}

//nolint
// Route implements the sdk.Msg interface.
func (msg MsgEditValidator) Route() string { return RouterKey }
func (msg MsgEditValidator) Type() string { return "edit_validator" }

// Type implements the sdk.Msg interface.
func (msg MsgEditValidator) Type() string { return "edit_validator" }

// GetSigners implements the sdk.Msg interface.
func (msg MsgEditValidator) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.ValidatorAddress)}
}

// get the bytes for the message signer to sign on
// GetSignBytes implements the sdk.Msg interface.
func (msg MsgEditValidator) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

// quick validity check
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgEditValidator) ValidateBasic() sdk.Error {
if msg.ValidatorAddress.Empty() {
return sdk.NewError(DefaultCodespace, CodeInvalidInput, "nil validator address")
Expand Down Expand Up @@ -246,6 +255,7 @@ type MsgDelegate struct {
Amount sdk.Coin `json:"amount" yaml:"amount"`
}

// NewMsgDelegate creates a new MsgDelegate instance.
func NewMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) MsgDelegate {
return MsgDelegate{
DelegatorAddress: delAddr,
Expand All @@ -254,20 +264,24 @@ func NewMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.C
}
}

//nolint
// Route implements the sdk.Msg interface.
func (msg MsgDelegate) Route() string { return RouterKey }
func (msg MsgDelegate) Type() string { return "delegate" }

// Type implements the sdk.Msg interface.
func (msg MsgDelegate) Type() string { return "delegate" }

// GetSigners implements the sdk.Msg interface.
func (msg MsgDelegate) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.DelegatorAddress}
}

// get the bytes for the message signer to sign on
// GetSignBytes implements the sdk.Msg interface.
func (msg MsgDelegate) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

// quick validity check
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgDelegate) ValidateBasic() sdk.Error {
if msg.DelegatorAddress.Empty() {
return ErrNilDelegatorAddr(DefaultCodespace)
Expand All @@ -283,17 +297,18 @@ func (msg MsgDelegate) ValidateBasic() sdk.Error {

//______________________________________________________________________

// MsgDelegate - struct for bonding transactions
// MsgBeginRedelegate defines the attributes of a bonding transaction.
type MsgBeginRedelegate struct {
DelegatorAddress sdk.AccAddress `json:"delegator_address" yaml:"delegator_address"`
ValidatorSrcAddress sdk.ValAddress `json:"validator_src_address" yaml:"validator_src_address"`
ValidatorDstAddress sdk.ValAddress `json:"validator_dst_address" yaml:"validator_dst_address"`
Amount sdk.Coin `json:"amount" yaml:"amount"`
}

func NewMsgBeginRedelegate(delAddr sdk.AccAddress, valSrcAddr,
valDstAddr sdk.ValAddress, amount sdk.Coin) MsgBeginRedelegate {

// NewMsgBeginRedelegate creates a new MsgBeginRedelegate instance.
func NewMsgBeginRedelegate(
delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress, amount sdk.Coin,
) MsgBeginRedelegate {
return MsgBeginRedelegate{
DelegatorAddress: delAddr,
ValidatorSrcAddress: valSrcAddr,
Expand All @@ -302,20 +317,24 @@ func NewMsgBeginRedelegate(delAddr sdk.AccAddress, valSrcAddr,
}
}

//nolint
// Route implements the sdk.Msg interface.
func (msg MsgBeginRedelegate) Route() string { return RouterKey }
func (msg MsgBeginRedelegate) Type() string { return "begin_redelegate" }

// Type implements the sdk.Msg interface
func (msg MsgBeginRedelegate) Type() string { return "begin_redelegate" }

// GetSigners implements the sdk.Msg interface
func (msg MsgBeginRedelegate) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.DelegatorAddress}
}

// get the bytes for the message signer to sign on
// GetSignBytes implements the sdk.Msg interface.
func (msg MsgBeginRedelegate) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

// quick validity check
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgBeginRedelegate) ValidateBasic() sdk.Error {
if msg.DelegatorAddress.Empty() {
return ErrNilDelegatorAddr(DefaultCodespace)
Expand All @@ -339,6 +358,7 @@ type MsgUndelegate struct {
Amount sdk.Coin `json:"amount" yaml:"amount"`
}

// NewMsgUndelegate creates a new MsgUndelegate instance.
func NewMsgUndelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) MsgUndelegate {
return MsgUndelegate{
DelegatorAddress: delAddr,
Expand All @@ -347,18 +367,22 @@ func NewMsgUndelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk
}
}

//nolint
func (msg MsgUndelegate) Route() string { return RouterKey }
func (msg MsgUndelegate) Type() string { return "begin_unbonding" }
// Route implements the sdk.Msg interface.
func (msg MsgUndelegate) Route() string { return RouterKey }

// Type implements the sdk.Msg interface.
func (msg MsgUndelegate) Type() string { return "begin_unbonding" }

// GetSigners implements the sdk.Msg interface.
func (msg MsgUndelegate) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.DelegatorAddress} }

// get the bytes for the message signer to sign on
// GetSignBytes implements the sdk.Msg interface.
func (msg MsgUndelegate) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

// quick validity check
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgUndelegate) ValidateBasic() sdk.Error {
if msg.DelegatorAddress.Empty() {
return ErrNilDelegatorAddr(DefaultCodespace)
Expand Down

0 comments on commit b9c40ea

Please sign in to comment.