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 (
2018-11-12 10:54:18 -08:00
coinPos = sdk . NewInt64Coin ( DefaultBondDenom , 1000 )
coinZero = sdk . NewInt64Coin ( DefaultBondDenom , 0 )
2018-06-26 19:00:12 -07:00
)
// test ValidateBasic for MsgCreateValidator
func TestMsgCreateValidator ( t * testing . T ) {
2018-09-24 15:23:58 -07:00
commission1 := NewCommissionMsg ( sdk . ZeroDec ( ) , sdk . ZeroDec ( ) , sdk . ZeroDec ( ) )
commission2 := NewCommissionMsg ( sdk . NewDec ( 5 ) , sdk . NewDec ( 5 ) , sdk . NewDec ( 5 ) )
2018-06-26 19:00:12 -07:00
tests := [ ] struct {
name , moniker , identity , website , details string
2018-09-24 15:23:58 -07:00
commissionMsg CommissionMsg
2019-02-08 12:44:19 -08:00
minSelfDelegation sdk . Int
2018-08-30 21:06:44 -07:00
validatorAddr sdk . ValAddress
2018-06-26 19:00:12 -07:00
pubkey crypto . PubKey
bond sdk . Coin
expectPass bool
} {
2019-02-08 12:44:19 -08:00
{ "basic good" , "a" , "b" , "c" , "d" , commission1 , sdk . OneInt ( ) , addr1 , pk1 , coinPos , true } ,
{ "partial description" , "" , "" , "c" , "" , commission1 , sdk . OneInt ( ) , addr1 , pk1 , coinPos , true } ,
{ "empty description" , "" , "" , "" , "" , commission2 , sdk . OneInt ( ) , addr1 , pk1 , coinPos , false } ,
{ "empty address" , "a" , "b" , "c" , "d" , commission2 , sdk . OneInt ( ) , emptyAddr , pk1 , coinPos , false } ,
{ "empty pubkey" , "a" , "b" , "c" , "d" , commission1 , sdk . OneInt ( ) , addr1 , emptyPubkey , coinPos , true } ,
{ "empty bond" , "a" , "b" , "c" , "d" , commission2 , sdk . OneInt ( ) , addr1 , pk1 , coinZero , false } ,
{ "zero min self delegation" , "a" , "b" , "c" , "d" , commission1 , sdk . ZeroInt ( ) , addr1 , pk1 , coinPos , false } ,
{ "negative min self delegation" , "a" , "b" , "c" , "d" , commission1 , sdk . NewInt ( - 1 ) , addr1 , pk1 , coinPos , false } ,
{ "delegation less than min self delegation" , "a" , "b" , "c" , "d" , commission1 , coinPos . Amount . Add ( sdk . OneInt ( ) ) , addr1 , pk1 , coinPos , false } ,
2018-06-26 19:00:12 -07:00
}
for _ , tc := range tests {
description := NewDescription ( tc . moniker , tc . identity , tc . website , tc . details )
2019-02-08 12:44:19 -08:00
msg := NewMsgCreateValidator ( tc . validatorAddr , tc . pubkey , tc . bond , description , tc . commissionMsg , 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 {
name , moniker , identity , website , details string
2018-08-30 21:06:44 -07:00
validatorAddr sdk . ValAddress
2018-06-26 19:00:12 -07:00
expectPass bool
} {
{ "basic good" , "a" , "b" , "c" , "d" , addr1 , true } ,
{ "partial description" , "" , "" , "c" , "" , addr1 , true } ,
{ "empty description" , "" , "" , "" , "" , addr1 , false } ,
{ "empty address" , "a" , "b" , "c" , "d" , emptyAddr , false } ,
}
for _ , tc := range tests {
description := NewDescription ( tc . moniker , tc . identity , tc . website , 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
}
}
}
2018-07-10 17:16:37 -07:00
// test ValidateBasic and GetSigners for MsgCreateValidatorOnBehalfOf
func TestMsgCreateValidatorOnBehalfOf ( t * testing . T ) {
2018-09-24 15:23:58 -07:00
commission1 := NewCommissionMsg ( sdk . ZeroDec ( ) , sdk . ZeroDec ( ) , sdk . ZeroDec ( ) )
commission2 := NewCommissionMsg ( sdk . NewDec ( 5 ) , sdk . NewDec ( 5 ) , sdk . NewDec ( 5 ) )
2018-07-10 17:16:37 -07:00
tests := [ ] struct {
name , moniker , identity , website , details string
2018-09-24 15:23:58 -07:00
commissionMsg CommissionMsg
2019-02-08 12:44:19 -08:00
minSelfDelegation sdk . Int
2018-07-10 17:16:37 -07:00
delegatorAddr sdk . AccAddress
2018-08-30 21:06:44 -07:00
validatorAddr sdk . ValAddress
2018-07-10 17:16:37 -07:00
validatorPubKey crypto . PubKey
bond sdk . Coin
expectPass bool
} {
2019-02-08 12:44:19 -08:00
{ "basic good" , "a" , "b" , "c" , "d" , commission2 , sdk . OneInt ( ) , sdk . AccAddress ( addr1 ) , addr2 , pk2 , coinPos , true } ,
{ "partial description" , "" , "" , "c" , "" , commission2 , sdk . OneInt ( ) , sdk . AccAddress ( addr1 ) , addr2 , pk2 , coinPos , true } ,
{ "empty description" , "" , "" , "" , "" , commission1 , sdk . OneInt ( ) , sdk . AccAddress ( addr1 ) , addr2 , pk2 , coinPos , false } ,
{ "empty delegator address" , "a" , "b" , "c" , "d" , commission1 , sdk . OneInt ( ) , sdk . AccAddress ( emptyAddr ) , addr2 , pk2 , coinPos , false } ,
{ "empty validator address" , "a" , "b" , "c" , "d" , commission2 , sdk . OneInt ( ) , sdk . AccAddress ( addr1 ) , emptyAddr , pk2 , coinPos , false } ,
{ "empty pubkey" , "a" , "b" , "c" , "d" , commission1 , sdk . OneInt ( ) , sdk . AccAddress ( addr1 ) , addr2 , emptyPubkey , coinPos , true } ,
{ "empty bond" , "a" , "b" , "c" , "d" , commission2 , sdk . OneInt ( ) , sdk . AccAddress ( addr1 ) , addr2 , pk2 , coinZero , false } ,
{ "zero min self delegation" , "a" , "b" , "c" , "d" , commission2 , sdk . ZeroInt ( ) , sdk . AccAddress ( addr1 ) , addr2 , pk2 , coinPos , false } ,
{ "negative min self delegation" , "" , "" , "c" , "" , commission2 , sdk . NewInt ( - 1 ) , sdk . AccAddress ( addr1 ) , addr2 , pk2 , coinPos , false } ,
{ "delegation less than min self delegation" , "a" , "b" , "c" , "d" , commission2 , coinPos . Amount . Add ( sdk . OneInt ( ) ) , sdk . AccAddress ( addr1 ) , addr2 , pk2 , coinPos , false } ,
2018-07-10 17:16:37 -07:00
}
for _ , tc := range tests {
description := NewDescription ( tc . moniker , tc . identity , tc . website , tc . details )
2018-09-24 15:23:58 -07:00
msg := NewMsgCreateValidatorOnBehalfOf (
2019-02-08 12:44:19 -08:00
tc . delegatorAddr , tc . validatorAddr , tc . validatorPubKey , tc . bond , description , tc . commissionMsg , tc . minSelfDelegation ,
2018-09-24 15:23:58 -07:00
)
2018-07-10 17:16:37 -07:00
if tc . expectPass {
require . Nil ( t , msg . ValidateBasic ( ) , "test: %v" , tc . name )
} else {
require . NotNil ( t , msg . ValidateBasic ( ) , "test: %v" , tc . name )
}
}
2019-02-08 12:44:19 -08:00
msg := NewMsgCreateValidator ( addr1 , pk1 , coinPos , Description { } , CommissionMsg { } , sdk . OneInt ( ) )
2018-07-10 17:16:37 -07:00
addrs := msg . GetSigners ( )
2018-08-30 21:06:44 -07:00
require . Equal ( t , [ ] sdk . AccAddress { sdk . AccAddress ( addr1 ) } , addrs , "Signers on default msg is wrong" )
2018-07-10 17:16:37 -07:00
2019-02-08 12:44:19 -08:00
msg = NewMsgCreateValidatorOnBehalfOf ( sdk . AccAddress ( addr2 ) , addr1 , pk1 , coinPos , Description { } , CommissionMsg { } , sdk . OneInt ( ) )
2018-07-10 17:16:37 -07:00
addrs = msg . GetSigners ( )
2018-08-30 21:06:44 -07:00
require . Equal ( t , [ ] sdk . AccAddress { sdk . AccAddress ( addr2 ) , sdk . AccAddress ( addr1 ) } , addrs , "Signers for onbehalfof msg is wrong" )
2018-07-10 17:16:37 -07:00
}
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
} {
2018-08-30 21:06:44 -07:00
{ "basic good" , sdk . AccAddress ( addr1 ) , addr2 , coinPos , true } ,
{ "self bond" , sdk . AccAddress ( addr1 ) , addr1 , coinPos , true } ,
{ "empty delegator" , sdk . AccAddress ( emptyAddr ) , addr1 , coinPos , false } ,
{ "empty validator" , sdk . AccAddress ( addr1 ) , emptyAddr , coinPos , false } ,
{ "empty bond" , sdk . AccAddress ( addr1 ) , addr2 , 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
2018-08-14 17:15:02 -07:00
sharesAmount sdk . Dec
2018-06-26 19:00:12 -07:00
expectPass bool
} {
2018-08-30 21:06:44 -07:00
{ "regular" , sdk . AccAddress ( addr1 ) , addr2 , addr3 , sdk . NewDecWithPrec ( 1 , 1 ) , true } ,
{ "negative decimal" , sdk . AccAddress ( addr1 ) , addr2 , addr3 , sdk . NewDecWithPrec ( - 1 , 1 ) , false } ,
{ "zero amount" , sdk . AccAddress ( addr1 ) , addr2 , addr3 , sdk . ZeroDec ( ) , false } ,
{ "empty delegator" , sdk . AccAddress ( emptyAddr ) , addr1 , addr3 , sdk . NewDecWithPrec ( 1 , 1 ) , false } ,
{ "empty source validator" , sdk . AccAddress ( addr1 ) , emptyAddr , addr3 , sdk . NewDecWithPrec ( 1 , 1 ) , false } ,
{ "empty destination validator" , sdk . AccAddress ( addr1 ) , addr2 , emptyAddr , sdk . NewDecWithPrec ( 1 , 1 ) , false } ,
2018-06-26 19:00:12 -07:00
}
for _ , tc := range tests {
msg := NewMsgBeginRedelegate ( tc . delegatorAddr , tc . validatorSrcAddr , tc . validatorDstAddr , tc . sharesAmount )
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
2018-08-14 17:15:02 -07:00
sharesAmount sdk . Dec
2018-06-26 19:00:12 -07:00
expectPass bool
} {
2018-08-30 21:06:44 -07:00
{ "regular" , sdk . AccAddress ( addr1 ) , addr2 , sdk . NewDecWithPrec ( 1 , 1 ) , true } ,
{ "negative decimal" , sdk . AccAddress ( addr1 ) , addr2 , sdk . NewDecWithPrec ( - 1 , 1 ) , false } ,
{ "zero amount" , sdk . AccAddress ( addr1 ) , addr2 , sdk . ZeroDec ( ) , false } ,
{ "empty delegator" , sdk . AccAddress ( emptyAddr ) , addr1 , sdk . NewDecWithPrec ( 1 , 1 ) , false } ,
{ "empty validator" , sdk . AccAddress ( addr1 ) , emptyAddr , sdk . NewDecWithPrec ( 1 , 1 ) , false } ,
2018-06-26 19:00:12 -07:00
}
for _ , tc := range tests {
2019-01-17 09:53:22 -08:00
msg := NewMsgUndelegate ( tc . delegatorAddr , tc . validatorAddr , tc . sharesAmount )
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 )
}
}
}