2018-02-24 05:19:32 -08:00
|
|
|
package stake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
crypto "github.com/tendermint/go-crypto"
|
|
|
|
wire "github.com/tendermint/go-wire"
|
2018-02-25 15:45:20 -08:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-02-24 05:19:32 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-03-13 11:27:52 -07:00
|
|
|
validator = []byte("addressvalidator1")
|
|
|
|
empty sdk.Address
|
2018-02-24 05:19:32 -08:00
|
|
|
|
2018-02-25 15:45:20 -08:00
|
|
|
coinPos = sdk.Coin{"fermion", 1000}
|
|
|
|
coinZero = sdk.Coin{"fermion", 0}
|
|
|
|
coinNeg = sdk.Coin{"fermion", -10000}
|
|
|
|
coinPosNotAtoms = sdk.Coin{"foo", 10000}
|
|
|
|
coinZeroNotAtoms = sdk.Coin{"foo", 0}
|
|
|
|
coinNegNotAtoms = sdk.Coin{"foo", -10000}
|
2018-02-24 05:19:32 -08:00
|
|
|
)
|
|
|
|
|
2018-03-14 11:42:50 -07:00
|
|
|
func TestMsgAddrValidateBasic(t *testing.T) {
|
2018-02-24 05:19:32 -08:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2018-03-14 11:42:50 -07:00
|
|
|
address sdk.Address
|
2018-02-24 05:19:32 -08:00
|
|
|
wantErr bool
|
|
|
|
}{
|
2018-03-14 11:42:50 -07:00
|
|
|
{"basic good", pks[0], false},
|
|
|
|
{"empty delegator", crypto.PubKey{}, true},
|
2018-02-24 05:19:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2018-03-14 11:42:50 -07:00
|
|
|
tx := NewMsgAddr(tc.address)
|
2018-02-24 05:19:32 -08:00
|
|
|
assert.Equal(t, tc.wantErr, tx.ValidateBasic() != nil,
|
|
|
|
"test: %v, tx.ValidateBasic: %v", tc.name, tx.ValidateBasic())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 11:42:50 -07:00
|
|
|
func TestValidateCoin(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
coin sdk.Coin
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"basic good", coinPos, false},
|
|
|
|
{"zero coin", coinZero, true},
|
|
|
|
{"neg coin", coinNeg, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
assert.Equal(t, tc.wantErr, tx.validateCoin(tc.coin) != nil,
|
|
|
|
"test: %v, tx.ValidateBasic: %v", tc.name, tx.ValidateBasic())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 05:19:32 -08:00
|
|
|
func TestAllAreTx(t *testing.T) {
|
|
|
|
|
|
|
|
// make sure all types construct properly
|
|
|
|
pubKey := newPubKey("1234567890")
|
|
|
|
bondAmt := 1234321
|
2018-02-25 15:45:20 -08:00
|
|
|
bond := sdk.Coin{Denom: "ATOM", Amount: int64(bondAmt)}
|
2018-02-24 05:19:32 -08:00
|
|
|
|
2018-03-14 11:42:50 -07:00
|
|
|
txDelegate := NewMsgDelegate(bond, pubKey)
|
|
|
|
_, ok := txDelegate.(MsgDelegate)
|
2018-02-25 15:45:20 -08:00
|
|
|
assert.True(t, ok, "%#v", txDelegate)
|
2018-02-24 05:19:32 -08:00
|
|
|
|
2018-03-14 11:42:50 -07:00
|
|
|
txUnbond := NewMsgUnbond(strconv.Itoa(bondAmt), pubKey)
|
|
|
|
_, ok = txUnbond.(MsgUnbond)
|
2018-02-25 15:45:20 -08:00
|
|
|
assert.True(t, ok, "%#v", txUnbond)
|
2018-02-24 05:19:32 -08:00
|
|
|
|
2018-03-14 11:42:50 -07:00
|
|
|
txDecl := NewMsgDeclareCandidacy(bond, pubKey, Description{})
|
|
|
|
_, ok = txDecl.(MsgDeclareCandidacy)
|
2018-02-25 15:45:20 -08:00
|
|
|
assert.True(t, ok, "%#v", txDecl)
|
2018-02-24 05:19:32 -08:00
|
|
|
|
2018-03-14 11:42:50 -07:00
|
|
|
txEditCan := NewMsgEditCandidacy(pubKey, Description{})
|
|
|
|
_, ok = txEditCan.(MsgEditCandidacy)
|
2018-02-25 15:45:20 -08:00
|
|
|
assert.True(t, ok, "%#v", txEditCan)
|
2018-02-24 05:19:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSerializeTx(t *testing.T) {
|
|
|
|
|
|
|
|
// make sure all types construct properly
|
|
|
|
pubKey := newPubKey("1234567890")
|
|
|
|
bondAmt := 1234321
|
2018-02-25 15:45:20 -08:00
|
|
|
bond := sdk.Coin{Denom: "ATOM", Amount: int64(bondAmt)}
|
2018-02-24 05:19:32 -08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
tx sdk.Tx
|
|
|
|
}{
|
2018-03-14 11:42:50 -07:00
|
|
|
{NewMsgUnbond(strconv.Itoa(bondAmt), pubKey)},
|
|
|
|
{NewMsgDeclareCandidacy(bond, pubKey, Description{})},
|
|
|
|
{NewMsgDeclareCandidacy(bond, pubKey, Description{})},
|
2018-02-24 05:19:32 -08:00
|
|
|
// {NewTxRevokeCandidacy(pubKey)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
|
|
|
var tx sdk.Tx
|
|
|
|
bs := wire.BinaryBytes(tc.tx)
|
|
|
|
err := wire.ReadBinaryBytes(bs, &tx)
|
2018-02-25 15:45:20 -08:00
|
|
|
if assert.NoError(t, err, "%d", i) {
|
|
|
|
assert.Equal(t, tc.tx, tx, "%d", i)
|
2018-02-24 05:19:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|