2018-06-26 19:00:12 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
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"
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-02-13 15:01:50 -08:00
|
|
|
coinPos = sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000)
|
|
|
|
coinZero = sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)
|
2018-06-26 19:00:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// test ValidateBasic for MsgCreateValidator
|
|
|
|
func TestMsgCreateValidator(t *testing.T) {
|
2019-06-03 14:14:21 -07:00
|
|
|
commission1 := NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
|
|
|
|
commission2 := NewCommissionRates(sdk.NewDec(5), sdk.NewDec(5), sdk.NewDec(5))
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
tests := []struct {
|
2019-08-12 06:10:16 -07:00
|
|
|
name, moniker, identity, website, securityContact, details string
|
|
|
|
CommissionRates CommissionRates
|
|
|
|
minSelfDelegation sdk.Int
|
|
|
|
validatorAddr sdk.ValAddress
|
|
|
|
pubkey crypto.PubKey
|
|
|
|
bond sdk.Coin
|
|
|
|
expectPass bool
|
2018-06-26 19:00:12 -07:00
|
|
|
}{
|
2019-08-12 06:10:16 -07:00
|
|
|
{"basic good", "a", "b", "c", "d", "e", commission1, sdk.OneInt(), valAddr1, pk1, coinPos, true},
|
|
|
|
{"partial description", "", "", "c", "", "", commission1, sdk.OneInt(), valAddr1, pk1, coinPos, true},
|
|
|
|
{"empty description", "", "", "", "", "", commission2, sdk.OneInt(), valAddr1, pk1, coinPos, false},
|
|
|
|
{"empty address", "a", "b", "c", "d", "e", commission2, sdk.OneInt(), emptyAddr, pk1, coinPos, false},
|
2020-02-06 11:21:02 -08:00
|
|
|
{"empty pubkey", "a", "b", "c", "d", "e", commission1, sdk.OneInt(), valAddr1, emptyPubkey, coinPos, false},
|
2019-08-12 06:10:16 -07:00
|
|
|
{"empty bond", "a", "b", "c", "d", "e", commission2, sdk.OneInt(), valAddr1, pk1, coinZero, false},
|
|
|
|
{"zero min self delegation", "a", "b", "c", "d", "e", commission1, sdk.ZeroInt(), valAddr1, pk1, coinPos, false},
|
|
|
|
{"negative min self delegation", "a", "b", "c", "d", "e", commission1, sdk.NewInt(-1), valAddr1, pk1, coinPos, false},
|
|
|
|
{"delegation less than min self delegation", "a", "b", "c", "d", "e", commission1, coinPos.Amount.Add(sdk.OneInt()), valAddr1, pk1, coinPos, false},
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-08-12 06:10:16 -07:00
|
|
|
description := NewDescription(tc.moniker, tc.identity, tc.website, tc.securityContact, tc.details)
|
2019-06-03 14:14:21 -07:00
|
|
|
msg := NewMsgCreateValidator(tc.validatorAddr, tc.pubkey, tc.bond, description, tc.CommissionRates, tc.minSelfDelegation)
|
2018-06-26 19:00:12 -07:00
|
|
|
if tc.expectPass {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
2018-06-26 19:00:12 -07:00
|
|
|
} else {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.NotNil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test ValidateBasic for MsgEditValidator
|
|
|
|
func TestMsgEditValidator(t *testing.T) {
|
|
|
|
tests := []struct {
|
2019-08-12 06:10:16 -07:00
|
|
|
name, moniker, identity, website, securityContact, details string
|
|
|
|
validatorAddr sdk.ValAddress
|
|
|
|
expectPass bool
|
2018-06-26 19:00:12 -07:00
|
|
|
}{
|
2019-08-12 06:10:16 -07:00
|
|
|
{"basic good", "a", "b", "c", "d", "e", valAddr1, true},
|
|
|
|
{"partial description", "", "", "c", "", "", valAddr1, true},
|
|
|
|
{"empty description", "", "", "", "", "", valAddr1, false},
|
|
|
|
{"empty address", "a", "b", "c", "d", "e", emptyAddr, false},
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-08-12 06:10:16 -07:00
|
|
|
description := NewDescription(tc.moniker, tc.identity, tc.website, tc.securityContact, tc.details)
|
2018-09-24 15:23:58 -07:00
|
|
|
newRate := sdk.ZeroDec()
|
2019-02-08 12:44:19 -08:00
|
|
|
newMinSelfDelegation := sdk.OneInt()
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2019-02-08 12:44:19 -08:00
|
|
|
msg := NewMsgEditValidator(tc.validatorAddr, description, &newRate, &newMinSelfDelegation)
|
2018-06-26 19:00:12 -07:00
|
|
|
if tc.expectPass {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
2018-06-26 19:00:12 -07:00
|
|
|
} else {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.NotNil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test ValidateBasic for MsgDelegate
|
|
|
|
func TestMsgDelegate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2018-07-06 00:06:53 -07:00
|
|
|
delegatorAddr sdk.AccAddress
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr sdk.ValAddress
|
2018-06-26 19:00:12 -07:00
|
|
|
bond sdk.Coin
|
|
|
|
expectPass bool
|
|
|
|
}{
|
2019-05-28 01:44:04 -07:00
|
|
|
{"basic good", sdk.AccAddress(valAddr1), valAddr2, coinPos, true},
|
|
|
|
{"self bond", sdk.AccAddress(valAddr1), valAddr1, coinPos, true},
|
|
|
|
{"empty delegator", sdk.AccAddress(emptyAddr), valAddr1, coinPos, false},
|
|
|
|
{"empty validator", sdk.AccAddress(valAddr1), emptyAddr, coinPos, false},
|
|
|
|
{"empty bond", sdk.AccAddress(valAddr1), valAddr2, coinZero, false},
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
msg := NewMsgDelegate(tc.delegatorAddr, tc.validatorAddr, tc.bond)
|
|
|
|
if tc.expectPass {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
2018-06-26 19:00:12 -07:00
|
|
|
} else {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.NotNil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test ValidateBasic for MsgUnbond
|
|
|
|
func TestMsgBeginRedelegate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2018-07-06 00:06:53 -07:00
|
|
|
delegatorAddr sdk.AccAddress
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorSrcAddr sdk.ValAddress
|
|
|
|
validatorDstAddr sdk.ValAddress
|
2019-03-25 14:13:02 -07:00
|
|
|
amount sdk.Coin
|
2018-06-26 19:00:12 -07:00
|
|
|
expectPass bool
|
|
|
|
}{
|
2019-05-28 01:44:04 -07:00
|
|
|
{"regular", sdk.AccAddress(valAddr1), valAddr2, valAddr3, sdk.NewInt64Coin(sdk.DefaultBondDenom, 1), true},
|
|
|
|
{"zero amount", sdk.AccAddress(valAddr1), valAddr2, valAddr3, sdk.NewInt64Coin(sdk.DefaultBondDenom, 0), false},
|
|
|
|
{"empty delegator", sdk.AccAddress(emptyAddr), valAddr1, valAddr3, sdk.NewInt64Coin(sdk.DefaultBondDenom, 1), false},
|
|
|
|
{"empty source validator", sdk.AccAddress(valAddr1), emptyAddr, valAddr3, sdk.NewInt64Coin(sdk.DefaultBondDenom, 1), false},
|
|
|
|
{"empty destination validator", sdk.AccAddress(valAddr1), valAddr2, emptyAddr, sdk.NewInt64Coin(sdk.DefaultBondDenom, 1), false},
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-03-25 14:13:02 -07:00
|
|
|
msg := NewMsgBeginRedelegate(tc.delegatorAddr, tc.validatorSrcAddr, tc.validatorDstAddr, tc.amount)
|
2018-06-26 19:00:12 -07:00
|
|
|
if tc.expectPass {
|
|
|
|
require.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test ValidateBasic for MsgUnbond
|
2019-01-17 09:53:22 -08:00
|
|
|
func TestMsgUndelegate(t *testing.T) {
|
2018-06-26 19:00:12 -07:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2018-07-06 00:06:53 -07:00
|
|
|
delegatorAddr sdk.AccAddress
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr sdk.ValAddress
|
2019-03-25 14:13:02 -07:00
|
|
|
amount sdk.Coin
|
2018-06-26 19:00:12 -07:00
|
|
|
expectPass bool
|
|
|
|
}{
|
2019-05-28 01:44:04 -07:00
|
|
|
{"regular", sdk.AccAddress(valAddr1), valAddr2, sdk.NewInt64Coin(sdk.DefaultBondDenom, 1), true},
|
|
|
|
{"zero amount", sdk.AccAddress(valAddr1), valAddr2, sdk.NewInt64Coin(sdk.DefaultBondDenom, 0), false},
|
|
|
|
{"empty delegator", sdk.AccAddress(emptyAddr), valAddr1, sdk.NewInt64Coin(sdk.DefaultBondDenom, 1), false},
|
|
|
|
{"empty validator", sdk.AccAddress(valAddr1), emptyAddr, sdk.NewInt64Coin(sdk.DefaultBondDenom, 1), false},
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2019-03-25 14:13:02 -07:00
|
|
|
msg := NewMsgUndelegate(tc.delegatorAddr, tc.validatorAddr, tc.amount)
|
2018-06-26 19:00:12 -07:00
|
|
|
if tc.expectPass {
|
|
|
|
require.Nil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, msg.ValidateBasic(), "test: %v", tc.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|