cosmos-sdk/x/stake/handler_test.go

332 lines
13 KiB
Go
Raw Normal View History

2018-02-23 15:57:31 -08:00
package stake
2018-03-29 07:49:18 -07:00
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
crypto "github.com/tendermint/go-crypto"
sdk "github.com/cosmos/cosmos-sdk/types"
)
//______________________________________________________________________
func newTestMsgDeclareCandidacy(address sdk.Address, pubKey crypto.PubKey, amt int64) MsgDeclareCandidacy {
return MsgDeclareCandidacy{
Description: Description{},
2018-05-09 21:01:58 -07:00
ValidatorAddr: address,
2018-03-29 07:49:18 -07:00
PubKey: pubKey,
2018-05-04 18:29:12 -07:00
Bond: sdk.Coin{"steak", amt},
2018-03-29 07:49:18 -07:00
}
}
2018-05-09 21:01:58 -07:00
func newTestMsgDelegate(delegatorAddr, validatorAddr sdk.Address, amt int64) MsgDelegate {
2018-03-29 07:49:18 -07:00
return MsgDelegate{
DelegatorAddr: delegatorAddr,
2018-05-09 21:01:58 -07:00
ValidatorAddr: validatorAddr,
2018-04-30 16:24:46 -07:00
Bond: sdk.Coin{"steak", amt},
2018-03-29 07:49:18 -07:00
}
}
2018-04-03 19:26:39 -07:00
//______________________________________________________________________
2018-03-29 07:49:18 -07:00
func TestDuplicatesMsgDeclareCandidacy(t *testing.T) {
2018-04-01 09:05:58 -07:00
ctx, _, keeper := createTestInput(t, false, 1000)
2018-03-29 07:49:18 -07:00
2018-05-09 21:01:58 -07:00
validatorAddr := addrs[0]
2018-04-03 19:26:39 -07:00
pk := pks[0]
2018-05-09 21:01:58 -07:00
msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pk, 10)
2018-03-30 10:28:51 -07:00
got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper)
assert.True(t, got.IsOK(), "%v", got)
2018-05-09 21:01:58 -07:00
validator, found := keeper.GetValidator(ctx, validatorAddr)
2018-04-03 19:26:39 -07:00
require.True(t, found)
2018-05-12 12:45:40 -07:00
assert.Equal(t, sdk.Bonded, validator.Status)
assert.Equal(t, validatorAddr, validator.Owner)
2018-05-09 21:01:58 -07:00
assert.Equal(t, pk, validator.PubKey)
2018-05-18 15:57:47 -07:00
assert.Equal(t, sdk.NewRat(10), validator.PoolShares.Bonded())
2018-05-09 21:01:58 -07:00
assert.Equal(t, sdk.NewRat(10), validator.DelegatorShares)
assert.Equal(t, Description{}, validator.Description)
// one validator cannot bond twice
2018-03-30 10:28:51 -07:00
msgDeclareCandidacy.PubKey = pks[1]
got = handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper)
assert.False(t, got.IsOK(), "%v", got)
2018-03-29 07:49:18 -07:00
}
func TestIncrementsMsgDelegate(t *testing.T) {
2018-04-01 09:05:58 -07:00
initBond := int64(1000)
ctx, accMapper, keeper := createTestInput(t, false, initBond)
params := keeper.GetParams(ctx)
2018-03-29 07:49:18 -07:00
bondAmount := int64(10)
2018-05-09 21:01:58 -07:00
validatorAddr, delegatorAddr := addrs[0], addrs[1]
2018-04-01 09:05:58 -07:00
// first declare candidacy
2018-05-09 21:01:58 -07:00
msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[0], bondAmount)
2018-04-01 09:05:58 -07:00
got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper)
assert.True(t, got.IsOK(), "expected declare candidacy msg to be ok, got %v", got)
2018-04-03 19:26:39 -07:00
2018-05-09 21:01:58 -07:00
validator, found := keeper.GetValidator(ctx, validatorAddr)
2018-04-03 19:26:39 -07:00
require.True(t, found)
require.Equal(t, sdk.Bonded, validator.Status)
2018-05-09 21:01:58 -07:00
assert.Equal(t, bondAmount, validator.DelegatorShares.Evaluate())
2018-05-18 15:57:47 -07:00
assert.Equal(t, bondAmount, validator.PoolShares.Bonded().Evaluate(), "validator: %v", validator)
2018-03-29 07:49:18 -07:00
_, found = keeper.GetDelegation(ctx, delegatorAddr, validatorAddr)
require.False(t, found)
bond, found := keeper.GetDelegation(ctx, validatorAddr, validatorAddr)
require.True(t, found)
assert.Equal(t, bondAmount, bond.Shares.Evaluate())
pool := keeper.GetPool(ctx)
exRate := validator.DelegatorShareExRate(pool)
require.True(t, exRate.Equal(sdk.OneRat()), "expected exRate 1 got %v", exRate)
assert.Equal(t, bondAmount, pool.BondedShares.Evaluate())
assert.Equal(t, bondAmount, pool.BondedTokens)
2018-03-29 07:49:18 -07:00
// just send the same msgbond multiple times
2018-05-09 21:01:58 -07:00
msgDelegate := newTestMsgDelegate(delegatorAddr, validatorAddr, bondAmount)
2018-04-23 09:41:36 -07:00
2018-03-29 07:49:18 -07:00
for i := 0; i < 5; i++ {
2018-04-23 09:41:36 -07:00
ctx = ctx.WithBlockHeight(int64(i))
2018-04-01 09:05:58 -07:00
got := handleMsgDelegate(ctx, msgDelegate, keeper)
2018-04-03 12:15:08 -07:00
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
2018-03-29 07:49:18 -07:00
//Check that the accounts and the bond account have the appropriate values
2018-05-09 21:01:58 -07:00
validator, found := keeper.GetValidator(ctx, validatorAddr)
2018-04-03 19:26:39 -07:00
require.True(t, found)
2018-05-09 21:01:58 -07:00
bond, found := keeper.GetDelegation(ctx, delegatorAddr, validatorAddr)
2018-04-03 19:26:39 -07:00
require.True(t, found)
pool := keeper.GetPool(ctx)
exRate := validator.DelegatorShareExRate(pool)
require.True(t, exRate.Equal(sdk.OneRat()), "expected exRate 1 got %v, i = %v", exRate, i)
2018-04-03 19:26:39 -07:00
expBond := int64(i+1) * bondAmount
2018-05-04 12:38:25 -07:00
expDelegatorShares := int64(i+2) * bondAmount // (1 self delegation)
2018-04-03 19:26:39 -07:00
expDelegatorAcc := initBond - expBond
2018-04-23 09:41:36 -07:00
require.Equal(t, bond.Height, int64(i), "Incorrect bond height")
2018-04-03 19:26:39 -07:00
gotBond := bond.Shares.Evaluate()
2018-05-09 21:01:58 -07:00
gotDelegatorShares := validator.DelegatorShares.Evaluate()
2018-04-03 19:26:39 -07:00
gotDelegatorAcc := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(params.BondDenom)
require.Equal(t, expBond, gotBond,
2018-05-09 21:01:58 -07:00
"i: %v\nexpBond: %v\ngotBond: %v\nvalidator: %v\nbond: %v\n",
i, expBond, gotBond, validator, bond)
2018-05-04 12:38:25 -07:00
require.Equal(t, expDelegatorShares, gotDelegatorShares,
2018-05-09 21:01:58 -07:00
"i: %v\nexpDelegatorShares: %v\ngotDelegatorShares: %v\nvalidator: %v\nbond: %v\n",
i, expDelegatorShares, gotDelegatorShares, validator, bond)
2018-04-03 19:26:39 -07:00
require.Equal(t, expDelegatorAcc, gotDelegatorAcc,
2018-05-09 21:01:58 -07:00
"i: %v\nexpDelegatorAcc: %v\ngotDelegatorAcc: %v\nvalidator: %v\nbond: %v\n",
i, expDelegatorAcc, gotDelegatorAcc, validator, bond)
2018-03-29 07:49:18 -07:00
}
}
func TestIncrementsMsgUnbond(t *testing.T) {
initBond := int64(1000)
2018-04-01 09:05:58 -07:00
ctx, accMapper, keeper := createTestInput(t, false, initBond)
params := keeper.GetParams(ctx)
// declare candidacy, delegate
2018-05-09 21:01:58 -07:00
validatorAddr, delegatorAddr := addrs[0], addrs[1]
2018-04-03 19:26:39 -07:00
2018-05-09 21:01:58 -07:00
msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[0], initBond)
2018-04-01 09:05:58 -07:00
got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper)
assert.True(t, got.IsOK(), "expected declare-candidacy to be ok, got %v", got)
2018-04-03 19:26:39 -07:00
2018-05-09 21:01:58 -07:00
msgDelegate := newTestMsgDelegate(delegatorAddr, validatorAddr, initBond)
2018-04-01 09:05:58 -07:00
got = handleMsgDelegate(ctx, msgDelegate, keeper)
assert.True(t, got.IsOK(), "expected delegation to be ok, got %v", got)
2018-03-29 07:49:18 -07:00
2018-05-09 21:01:58 -07:00
validator, found := keeper.GetValidator(ctx, validatorAddr)
2018-04-03 19:26:39 -07:00
require.True(t, found)
2018-05-09 21:01:58 -07:00
assert.Equal(t, initBond*2, validator.DelegatorShares.Evaluate())
2018-05-18 15:57:47 -07:00
assert.Equal(t, initBond*2, validator.PoolShares.Bonded().Evaluate())
2018-04-03 19:26:39 -07:00
2018-04-01 09:05:58 -07:00
// just send the same msgUnbond multiple times
// TODO use decimals here
2018-03-29 07:49:18 -07:00
unbondShares, unbondSharesStr := int64(10), "10"
2018-05-09 21:01:58 -07:00
msgUnbond := NewMsgUnbond(delegatorAddr, validatorAddr, unbondSharesStr)
2018-04-01 09:05:58 -07:00
numUnbonds := 5
for i := 0; i < numUnbonds; i++ {
got := handleMsgUnbond(ctx, msgUnbond, keeper)
2018-04-03 12:15:08 -07:00
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
2018-03-29 07:49:18 -07:00
//Check that the accounts and the bond account have the appropriate values
2018-05-09 21:01:58 -07:00
validator, found = keeper.GetValidator(ctx, validatorAddr)
2018-04-03 19:26:39 -07:00
require.True(t, found)
2018-05-09 21:01:58 -07:00
bond, found := keeper.GetDelegation(ctx, delegatorAddr, validatorAddr)
2018-04-01 09:05:58 -07:00
require.True(t, found)
2018-03-29 07:49:18 -07:00
2018-04-03 19:26:39 -07:00
expBond := initBond - int64(i+1)*unbondShares
2018-05-04 12:38:25 -07:00
expDelegatorShares := 2*initBond - int64(i+1)*unbondShares
2018-04-03 19:26:39 -07:00
expDelegatorAcc := initBond - expBond
gotBond := bond.Shares.Evaluate()
2018-05-09 21:01:58 -07:00
gotDelegatorShares := validator.DelegatorShares.Evaluate()
2018-04-03 19:26:39 -07:00
gotDelegatorAcc := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(params.BondDenom)
require.Equal(t, expBond, gotBond,
2018-05-09 21:01:58 -07:00
"i: %v\nexpBond: %v\ngotBond: %v\nvalidator: %v\nbond: %v\n",
i, expBond, gotBond, validator, bond)
2018-05-04 12:38:25 -07:00
require.Equal(t, expDelegatorShares, gotDelegatorShares,
2018-05-09 21:01:58 -07:00
"i: %v\nexpDelegatorShares: %v\ngotDelegatorShares: %v\nvalidator: %v\nbond: %v\n",
i, expDelegatorShares, gotDelegatorShares, validator, bond)
2018-04-03 19:26:39 -07:00
require.Equal(t, expDelegatorAcc, gotDelegatorAcc,
2018-05-09 21:01:58 -07:00
"i: %v\nexpDelegatorAcc: %v\ngotDelegatorAcc: %v\nvalidator: %v\nbond: %v\n",
i, expDelegatorAcc, gotDelegatorAcc, validator, bond)
2018-03-29 07:49:18 -07:00
}
// these are more than we have bonded now
errorCases := []int64{
//1<<64 - 1, // more than int64
//1<<63 + 1, // more than int64
1<<63 - 1,
1 << 31,
initBond,
}
for _, c := range errorCases {
unbondShares := strconv.Itoa(int(c))
2018-05-09 21:01:58 -07:00
msgUnbond := NewMsgUnbond(delegatorAddr, validatorAddr, unbondShares)
2018-04-01 09:05:58 -07:00
got = handleMsgUnbond(ctx, msgUnbond, keeper)
2018-04-03 12:15:08 -07:00
require.False(t, got.IsOK(), "expected unbond msg to fail")
2018-03-29 07:49:18 -07:00
}
2018-04-01 09:05:58 -07:00
leftBonded := initBond - unbondShares*int64(numUnbonds)
2018-03-29 07:49:18 -07:00
// should be unable to unbond one more than we have
2018-04-03 19:26:39 -07:00
unbondSharesStr = strconv.Itoa(int(leftBonded) + 1)
2018-05-09 21:01:58 -07:00
msgUnbond = NewMsgUnbond(delegatorAddr, validatorAddr, unbondSharesStr)
2018-04-01 09:05:58 -07:00
got = handleMsgUnbond(ctx, msgUnbond, keeper)
2018-04-03 19:26:39 -07:00
assert.False(t, got.IsOK(),
"got: %v\nmsgUnbond: %v\nshares: %v\nleftBonded: %v\n", got, msgUnbond, unbondSharesStr, leftBonded)
2018-03-29 07:49:18 -07:00
// should be able to unbond just what we have
2018-04-03 19:26:39 -07:00
unbondSharesStr = strconv.Itoa(int(leftBonded))
2018-05-09 21:01:58 -07:00
msgUnbond = NewMsgUnbond(delegatorAddr, validatorAddr, unbondSharesStr)
2018-04-01 09:05:58 -07:00
got = handleMsgUnbond(ctx, msgUnbond, keeper)
2018-04-03 19:26:39 -07:00
assert.True(t, got.IsOK(),
"got: %v\nmsgUnbond: %v\nshares: %v\nleftBonded: %v\n", got, msgUnbond, unbondSharesStr, leftBonded)
2018-03-29 07:49:18 -07:00
}
func TestMultipleMsgDeclareCandidacy(t *testing.T) {
2018-04-01 09:05:58 -07:00
initBond := int64(1000)
ctx, accMapper, keeper := createTestInput(t, false, initBond)
params := keeper.GetParams(ctx)
2018-05-09 21:01:58 -07:00
validatorAddrs := []sdk.Address{addrs[0], addrs[1], addrs[2]}
2018-03-29 07:49:18 -07:00
// bond them all
2018-05-09 21:01:58 -07:00
for i, validatorAddr := range validatorAddrs {
msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[i], 10)
2018-04-01 09:05:58 -07:00
got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper)
2018-04-03 12:15:08 -07:00
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
2018-03-29 07:49:18 -07:00
//Check that the account is bonded
2018-05-09 21:01:58 -07:00
validators := keeper.GetValidators(ctx, 100)
require.Equal(t, (i + 1), len(validators))
val := validators[i]
2018-04-01 09:05:58 -07:00
balanceExpd := initBond - 10
balanceGot := accMapper.GetAccount(ctx, val.Owner).GetCoins().AmountOf(params.BondDenom)
2018-05-09 21:01:58 -07:00
require.Equal(t, i+1, len(validators), "expected %d validators got %d, validators: %v", i+1, len(validators), validators)
2018-05-04 12:38:25 -07:00
require.Equal(t, 10, int(val.DelegatorShares.Evaluate()), "expected %d shares, got %d", 10, val.DelegatorShares)
2018-04-03 12:15:08 -07:00
require.Equal(t, balanceExpd, balanceGot, "expected account to have %d, got %d", balanceExpd, balanceGot)
2018-03-29 07:49:18 -07:00
}
// unbond them all
2018-05-09 21:01:58 -07:00
for i, validatorAddr := range validatorAddrs {
validatorPre, found := keeper.GetValidator(ctx, validatorAddr)
2018-04-01 09:05:58 -07:00
require.True(t, found)
2018-05-09 21:01:58 -07:00
msgUnbond := NewMsgUnbond(validatorAddr, validatorAddr, "10") // self-delegation
2018-04-01 09:05:58 -07:00
got := handleMsgUnbond(ctx, msgUnbond, keeper)
2018-04-03 12:15:08 -07:00
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
2018-03-29 07:49:18 -07:00
//Check that the account is unbonded
2018-05-09 21:01:58 -07:00
validators := keeper.GetValidators(ctx, 100)
require.Equal(t, len(validatorAddrs)-(i+1), len(validators),
"expected %d validators got %d", len(validatorAddrs)-(i+1), len(validators))
2018-04-01 09:05:58 -07:00
2018-05-09 21:01:58 -07:00
_, found = keeper.GetValidator(ctx, validatorAddr)
2018-04-03 19:26:39 -07:00
require.False(t, found)
expBalance := initBond
gotBalance := accMapper.GetAccount(ctx, validatorPre.Owner).GetCoins().AmountOf(params.BondDenom)
2018-04-03 19:26:39 -07:00
require.Equal(t, expBalance, gotBalance, "expected account to have %d, got %d", expBalance, gotBalance)
2018-03-29 07:49:18 -07:00
}
}
func TestMultipleMsgDelegate(t *testing.T) {
2018-04-03 19:26:39 -07:00
ctx, _, keeper := createTestInput(t, false, 1000)
2018-05-09 21:01:58 -07:00
validatorAddr, delegatorAddrs := addrs[0], addrs[1:]
2018-03-29 07:49:18 -07:00
2018-05-09 21:01:58 -07:00
//first make a validator
msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[0], 10)
2018-04-01 09:05:58 -07:00
got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper)
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
2018-03-29 07:49:18 -07:00
// delegate multiple parties
2018-04-01 09:05:58 -07:00
for i, delegatorAddr := range delegatorAddrs {
2018-05-09 21:01:58 -07:00
msgDelegate := newTestMsgDelegate(delegatorAddr, validatorAddr, 10)
2018-04-01 09:05:58 -07:00
got := handleMsgDelegate(ctx, msgDelegate, keeper)
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
2018-03-29 07:49:18 -07:00
//Check that the account is bonded
2018-05-09 21:01:58 -07:00
bond, found := keeper.GetDelegation(ctx, delegatorAddr, validatorAddr)
2018-04-01 09:05:58 -07:00
require.True(t, found)
2018-04-03 12:15:08 -07:00
require.NotNil(t, bond, "expected delegatee bond %d to exist", bond)
2018-03-29 07:49:18 -07:00
}
// unbond them all
2018-04-01 09:05:58 -07:00
for i, delegatorAddr := range delegatorAddrs {
2018-05-09 21:01:58 -07:00
msgUnbond := NewMsgUnbond(delegatorAddr, validatorAddr, "10")
2018-04-01 09:05:58 -07:00
got := handleMsgUnbond(ctx, msgUnbond, keeper)
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
2018-03-29 07:49:18 -07:00
//Check that the account is unbonded
2018-05-09 21:01:58 -07:00
_, found := keeper.GetDelegation(ctx, delegatorAddr, validatorAddr)
2018-04-01 09:05:58 -07:00
require.False(t, found)
2018-03-29 07:49:18 -07:00
}
}
func TestVoidCandidacy(t *testing.T) {
2018-04-03 19:26:39 -07:00
ctx, _, keeper := createTestInput(t, false, 1000)
2018-05-09 21:01:58 -07:00
validatorAddr, delegatorAddr := addrs[0], addrs[1]
2018-03-29 07:49:18 -07:00
2018-05-09 21:01:58 -07:00
// create the validator
msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[0], 10)
2018-04-01 09:05:58 -07:00
got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper)
require.True(t, got.IsOK(), "expected no error on runMsgDeclareCandidacy")
2018-03-29 07:49:18 -07:00
// bond a delegator
2018-05-09 21:01:58 -07:00
msgDelegate := newTestMsgDelegate(delegatorAddr, validatorAddr, 10)
2018-04-01 09:05:58 -07:00
got = handleMsgDelegate(ctx, msgDelegate, keeper)
require.True(t, got.IsOK(), "expected ok, got %v", got)
2018-03-29 07:49:18 -07:00
2018-05-09 21:01:58 -07:00
// unbond the validators bond portion
msgUnbondValidator := NewMsgUnbond(validatorAddr, validatorAddr, "10")
got = handleMsgUnbond(ctx, msgUnbondValidator, keeper)
2018-04-01 09:05:58 -07:00
require.True(t, got.IsOK(), "expected no error on runMsgDeclareCandidacy")
2018-05-09 21:01:58 -07:00
validator, found := keeper.GetValidator(ctx, validatorAddr)
2018-04-03 19:26:39 -07:00
require.True(t, found)
2018-05-20 14:56:43 -07:00
require.True(t, validator.Revoked)
2018-03-29 07:49:18 -07:00
2018-04-03 19:26:39 -07:00
// test that this address cannot yet be bonded too because is revoked
2018-04-01 09:05:58 -07:00
got = handleMsgDelegate(ctx, msgDelegate, keeper)
assert.False(t, got.IsOK(), "expected error, got %v", got)
2018-03-29 07:49:18 -07:00
// test that the delegator can still withdraw their bonds
2018-05-09 21:01:58 -07:00
msgUnbondDelegator := NewMsgUnbond(delegatorAddr, validatorAddr, "10")
2018-04-03 19:26:39 -07:00
got = handleMsgUnbond(ctx, msgUnbondDelegator, keeper)
2018-04-01 09:05:58 -07:00
require.True(t, got.IsOK(), "expected no error on runMsgDeclareCandidacy")
2018-03-29 07:49:18 -07:00
// verify that the pubkey can now be reused
2018-04-01 09:05:58 -07:00
got = handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper)
assert.True(t, got.IsOK(), "expected ok, got %v", got)
2018-03-29 07:49:18 -07:00
}