2018-06-26 19:00:12 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2018-08-30 21:06:44 -07:00
|
|
|
"bytes"
|
2018-06-29 13:30:12 -07:00
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-12-27 09:57:54 -08:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
|
|
|
|
2019-01-23 00:46:41 -08:00
|
|
|
var (
|
|
|
|
_ sdk.Msg = &MsgCreateValidator{}
|
|
|
|
_ sdk.Msg = &MsgEditValidator{}
|
|
|
|
_ sdk.Msg = &MsgDelegate{}
|
|
|
|
_ sdk.Msg = &MsgUndelegate{}
|
|
|
|
_ sdk.Msg = &MsgBeginRedelegate{}
|
|
|
|
)
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// NewMsgCreateValidator creates a new MsgCreateValidator instance.
|
|
|
|
// Delegator address and validator address are the same.
|
2019-03-05 08:55:46 -08:00
|
|
|
func NewMsgCreateValidator(
|
|
|
|
valAddr sdk.ValAddress, pubKey crypto.PubKey, selfDelegation sdk.Coin,
|
2019-06-03 14:14:21 -07:00
|
|
|
description Description, commission CommissionRates, minSelfDelegation sdk.Int,
|
2019-03-05 08:55:46 -08:00
|
|
|
) MsgCreateValidator {
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2020-02-06 11:21:02 -08:00
|
|
|
var pkStr string
|
|
|
|
if pubKey != nil {
|
|
|
|
pkStr = sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, pubKey)
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:16:37 -07:00
|
|
|
return MsgCreateValidator{
|
2019-02-08 12:44:19 -08:00
|
|
|
Description: description,
|
2019-03-05 08:55:46 -08:00
|
|
|
DelegatorAddress: sdk.AccAddress(valAddr),
|
2019-02-25 07:16:52 -08:00
|
|
|
ValidatorAddress: valAddr,
|
2020-02-06 11:21:02 -08:00
|
|
|
Pubkey: pkStr,
|
2019-03-05 08:55:46 -08:00
|
|
|
Value: selfDelegation,
|
2019-02-08 12:44:19 -08:00
|
|
|
Commission: commission,
|
|
|
|
MinSelfDelegation: minSelfDelegation,
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// Route implements the sdk.Msg interface.
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgCreateValidator) Route() string { return RouterKey }
|
2018-07-10 17:16:37 -07:00
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// 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.
|
2018-07-06 00:06:53 -07:00
|
|
|
func (msg MsgCreateValidator) GetSigners() []sdk.AccAddress {
|
2018-07-10 17:16:37 -07:00
|
|
|
// delegator is first signer so delegator pays fees
|
2019-02-25 07:16:52 -08:00
|
|
|
addrs := []sdk.AccAddress{msg.DelegatorAddress}
|
2018-08-30 21:06:44 -07:00
|
|
|
|
2019-02-25 07:16:52 -08:00
|
|
|
if !bytes.Equal(msg.DelegatorAddress.Bytes(), msg.ValidatorAddress.Bytes()) {
|
|
|
|
addrs = append(addrs, sdk.AccAddress(msg.ValidatorAddress))
|
2018-07-10 17:16:37 -07:00
|
|
|
}
|
|
|
|
return addrs
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-01-23 00:46:41 -08:00
|
|
|
// GetSignBytes returns the message bytes to sign over.
|
|
|
|
func (msg MsgCreateValidator) GetSignBytes() []byte {
|
2019-05-16 08:25:32 -07:00
|
|
|
bz := ModuleCdc.MustMarshalJSON(msg)
|
2019-01-23 00:46:41 -08:00
|
|
|
return sdk.MustSortJSON(bz)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// ValidateBasic implements the sdk.Msg interface.
|
2019-12-27 09:57:54 -08:00
|
|
|
func (msg MsgCreateValidator) ValidateBasic() error {
|
2019-02-07 17:41:23 -08:00
|
|
|
// note that unmarshaling from bech32 ensures either empty or valid
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.DelegatorAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyDelegatorAddr
|
2018-07-10 17:16:37 -07:00
|
|
|
}
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.ValidatorAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyValidatorAddr
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-03-05 08:55:46 -08:00
|
|
|
if !sdk.AccAddress(msg.ValidatorAddress).Equals(msg.DelegatorAddress) {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrBadValidatorAddr
|
2019-03-05 08:55:46 -08:00
|
|
|
}
|
2020-02-06 11:21:02 -08:00
|
|
|
if msg.Pubkey == "" {
|
|
|
|
return ErrEmptyValidatorPubKey
|
|
|
|
}
|
2019-12-06 10:18:00 -08:00
|
|
|
if !msg.Value.Amount.IsPositive() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrBadDelegationAmount
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2018-09-24 15:23:58 -07:00
|
|
|
if msg.Description == (Description{}) {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "empty description")
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-06-03 14:14:21 -07:00
|
|
|
if msg.Commission == (CommissionRates{}) {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "empty commission")
|
2018-09-24 15:23:58 -07:00
|
|
|
}
|
2019-06-03 14:14:21 -07:00
|
|
|
if err := msg.Commission.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-14 13:05:42 -07:00
|
|
|
if !msg.MinSelfDelegation.IsPositive() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrMinSelfDelegationInvalid
|
2019-02-08 12:44:19 -08:00
|
|
|
}
|
|
|
|
if msg.Value.Amount.LT(msg.MinSelfDelegation) {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrSelfDelegationBelowMinimum
|
2019-02-08 12:44:19 -08:00
|
|
|
}
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// NewMsgEditValidator creates a new MsgEditValidator instance
|
2019-02-08 12:44:19 -08:00
|
|
|
func NewMsgEditValidator(valAddr sdk.ValAddress, description Description, newRate *sdk.Dec, newMinSelfDelegation *sdk.Int) MsgEditValidator {
|
2018-06-26 19:00:12 -07:00
|
|
|
return MsgEditValidator{
|
2019-02-08 12:44:19 -08:00
|
|
|
Description: description,
|
|
|
|
CommissionRate: newRate,
|
2019-02-25 07:16:52 -08:00
|
|
|
ValidatorAddress: valAddr,
|
2019-02-08 12:44:19 -08:00
|
|
|
MinSelfDelegation: newMinSelfDelegation,
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// Route implements the sdk.Msg interface.
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgEditValidator) Route() string { return RouterKey }
|
2019-12-03 03:14:18 -08:00
|
|
|
|
|
|
|
// Type implements the sdk.Msg interface.
|
|
|
|
func (msg MsgEditValidator) Type() string { return "edit_validator" }
|
|
|
|
|
|
|
|
// GetSigners implements the sdk.Msg interface.
|
2018-07-06 00:06:53 -07:00
|
|
|
func (msg MsgEditValidator) GetSigners() []sdk.AccAddress {
|
2019-02-25 07:16:52 -08:00
|
|
|
return []sdk.AccAddress{sdk.AccAddress(msg.ValidatorAddress)}
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// GetSignBytes implements the sdk.Msg interface.
|
2018-06-26 19:00:12 -07:00
|
|
|
func (msg MsgEditValidator) GetSignBytes() []byte {
|
2019-05-16 08:25:32 -07:00
|
|
|
bz := ModuleCdc.MustMarshalJSON(msg)
|
2019-01-23 00:46:41 -08:00
|
|
|
return sdk.MustSortJSON(bz)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// ValidateBasic implements the sdk.Msg interface.
|
2019-12-27 09:57:54 -08:00
|
|
|
func (msg MsgEditValidator) ValidateBasic() error {
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.ValidatorAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyValidatorAddr
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2018-09-24 15:23:58 -07:00
|
|
|
if msg.Description == (Description{}) {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "empty description")
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-10-14 13:05:42 -07:00
|
|
|
if msg.MinSelfDelegation != nil && !msg.MinSelfDelegation.IsPositive() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrMinSelfDelegationInvalid
|
2019-02-08 12:44:19 -08:00
|
|
|
}
|
2019-02-07 17:41:23 -08:00
|
|
|
if msg.CommissionRate != nil {
|
2019-10-14 13:05:42 -07:00
|
|
|
if msg.CommissionRate.GT(sdk.OneDec()) || msg.CommissionRate.IsNegative() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "commission rate must be between 0 and 1 (inclusive)")
|
2019-02-07 17:41:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// NewMsgDelegate creates a new MsgDelegate instance.
|
2019-03-25 14:13:02 -07:00
|
|
|
func NewMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) MsgDelegate {
|
2018-06-26 19:00:12 -07:00
|
|
|
return MsgDelegate{
|
2019-02-25 07:16:52 -08:00
|
|
|
DelegatorAddress: delAddr,
|
|
|
|
ValidatorAddress: valAddr,
|
2019-03-25 14:13:02 -07:00
|
|
|
Amount: amount,
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// Route implements the sdk.Msg interface.
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgDelegate) Route() string { return RouterKey }
|
2019-12-03 03:14:18 -08:00
|
|
|
|
|
|
|
// Type implements the sdk.Msg interface.
|
|
|
|
func (msg MsgDelegate) Type() string { return "delegate" }
|
|
|
|
|
|
|
|
// GetSigners implements the sdk.Msg interface.
|
2018-07-06 00:06:53 -07:00
|
|
|
func (msg MsgDelegate) GetSigners() []sdk.AccAddress {
|
2019-02-25 07:16:52 -08:00
|
|
|
return []sdk.AccAddress{msg.DelegatorAddress}
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// GetSignBytes implements the sdk.Msg interface.
|
2018-06-26 19:00:12 -07:00
|
|
|
func (msg MsgDelegate) GetSignBytes() []byte {
|
2019-05-16 08:25:32 -07:00
|
|
|
bz := ModuleCdc.MustMarshalJSON(msg)
|
2019-01-23 00:46:41 -08:00
|
|
|
return sdk.MustSortJSON(bz)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// ValidateBasic implements the sdk.Msg interface.
|
2019-12-27 09:57:54 -08:00
|
|
|
func (msg MsgDelegate) ValidateBasic() error {
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.DelegatorAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyDelegatorAddr
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.ValidatorAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyValidatorAddr
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-12-06 10:18:00 -08:00
|
|
|
if !msg.Amount.Amount.IsPositive() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrBadDelegationAmount
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// NewMsgBeginRedelegate creates a new MsgBeginRedelegate instance.
|
|
|
|
func NewMsgBeginRedelegate(
|
|
|
|
delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress, amount sdk.Coin,
|
|
|
|
) MsgBeginRedelegate {
|
2018-06-26 19:00:12 -07:00
|
|
|
return MsgBeginRedelegate{
|
2019-02-25 07:16:52 -08:00
|
|
|
DelegatorAddress: delAddr,
|
|
|
|
ValidatorSrcAddress: valSrcAddr,
|
|
|
|
ValidatorDstAddress: valDstAddr,
|
2019-03-25 14:13:02 -07:00
|
|
|
Amount: amount,
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// Route implements the sdk.Msg interface.
|
2018-12-19 11:58:30 -08:00
|
|
|
func (msg MsgBeginRedelegate) Route() string { return RouterKey }
|
2019-12-03 03:14:18 -08:00
|
|
|
|
|
|
|
// Type implements the sdk.Msg interface
|
|
|
|
func (msg MsgBeginRedelegate) Type() string { return "begin_redelegate" }
|
|
|
|
|
|
|
|
// GetSigners implements the sdk.Msg interface
|
2018-07-06 00:06:53 -07:00
|
|
|
func (msg MsgBeginRedelegate) GetSigners() []sdk.AccAddress {
|
2019-02-25 07:16:52 -08:00
|
|
|
return []sdk.AccAddress{msg.DelegatorAddress}
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// GetSignBytes implements the sdk.Msg interface.
|
2018-06-26 19:00:12 -07:00
|
|
|
func (msg MsgBeginRedelegate) GetSignBytes() []byte {
|
2019-05-16 08:25:32 -07:00
|
|
|
bz := ModuleCdc.MustMarshalJSON(msg)
|
2019-01-23 00:46:41 -08:00
|
|
|
return sdk.MustSortJSON(bz)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// ValidateBasic implements the sdk.Msg interface.
|
2019-12-27 09:57:54 -08:00
|
|
|
func (msg MsgBeginRedelegate) ValidateBasic() error {
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.DelegatorAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyDelegatorAddr
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.ValidatorSrcAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyValidatorAddr
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.ValidatorDstAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyValidatorAddr
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-12-06 10:18:00 -08:00
|
|
|
if !msg.Amount.Amount.IsPositive() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrBadSharesAmount
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// NewMsgUndelegate creates a new MsgUndelegate instance.
|
2019-03-25 14:13:02 -07:00
|
|
|
func NewMsgUndelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin) MsgUndelegate {
|
2019-01-17 09:53:22 -08:00
|
|
|
return MsgUndelegate{
|
2019-02-25 07:16:52 -08:00
|
|
|
DelegatorAddress: delAddr,
|
|
|
|
ValidatorAddress: valAddr,
|
2019-03-25 14:13:02 -07:00
|
|
|
Amount: amount,
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// 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.
|
2019-02-25 07:16:52 -08:00
|
|
|
func (msg MsgUndelegate) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.DelegatorAddress} }
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// GetSignBytes implements the sdk.Msg interface.
|
2019-01-17 09:53:22 -08:00
|
|
|
func (msg MsgUndelegate) GetSignBytes() []byte {
|
2019-05-16 08:25:32 -07:00
|
|
|
bz := ModuleCdc.MustMarshalJSON(msg)
|
2019-01-23 00:46:41 -08:00
|
|
|
return sdk.MustSortJSON(bz)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:14:18 -08:00
|
|
|
// ValidateBasic implements the sdk.Msg interface.
|
2019-12-27 09:57:54 -08:00
|
|
|
func (msg MsgUndelegate) ValidateBasic() error {
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.DelegatorAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyDelegatorAddr
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-02-25 07:16:52 -08:00
|
|
|
if msg.ValidatorAddress.Empty() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrEmptyValidatorAddr
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
2019-12-06 10:18:00 -08:00
|
|
|
if !msg.Amount.Amount.IsPositive() {
|
2019-12-27 09:57:54 -08:00
|
|
|
return ErrBadSharesAmount
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|