2019-01-11 12:08:01 -08:00
|
|
|
package staking
|
2018-02-23 15:57:31 -08:00
|
|
|
|
2018-03-29 07:49:18 -07:00
|
|
|
import (
|
|
|
|
"testing"
|
2018-08-12 00:33:48 -07:00
|
|
|
"time"
|
2018-03-29 07:49:18 -07:00
|
|
|
|
2018-07-09 17:42:57 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-03-29 07:49:18 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-11-29 09:21:45 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/crypto/secp256k1"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2018-03-29 07:49:18 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-01-11 12:08:01 -08:00
|
|
|
keep "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-03-29 07:49:18 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
//______________________________________________________________________
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
// retrieve params which are instant
|
|
|
|
func setInstantUnbondPeriod(keeper keep.Keeper, ctx sdk.Context) types.Params {
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
params.UnbondingTime = 0
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
2018-04-03 19:26:39 -07:00
|
|
|
//______________________________________________________________________
|
|
|
|
|
2018-06-13 00:12:57 -07:00
|
|
|
func TestValidatorByPowerIndex(t *testing.T) {
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr, validatorAddr3 := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1])
|
2018-06-13 00:12:57 -07:00
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
initPower := int64(1000000)
|
2019-02-13 15:01:50 -08:00
|
|
|
initBond := sdk.TokensFromTendermintPower(initPower)
|
2019-02-05 21:30:48 -08:00
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, initPower)
|
2018-06-26 19:00:12 -07:00
|
|
|
_ = setInstantUnbondPeriod(keeper, ctx)
|
2018-06-13 00:12:57 -07:00
|
|
|
|
|
|
|
// create validator
|
2018-10-09 10:58:59 -07:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], initBond)
|
2018-06-13 00:12:57 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, got.IsOK(), "expected create-validator to be ok, got %v", got)
|
2018-06-13 00:12:57 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// must end-block
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 1, len(updates))
|
|
|
|
|
2018-06-13 00:12:57 -07:00
|
|
|
// verify the self-delegation exists
|
2018-08-30 21:06:44 -07:00
|
|
|
bond, found := keeper.GetDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
|
2018-06-13 00:12:57 -07:00
|
|
|
require.True(t, found)
|
2019-02-05 21:30:48 -08:00
|
|
|
gotBond := bond.Shares.RoundInt()
|
|
|
|
require.Equal(t, initBond, gotBond)
|
2018-06-13 00:12:57 -07:00
|
|
|
|
|
|
|
// verify that the by power index exists
|
|
|
|
validator, found := keeper.GetValidator(ctx, validatorAddr)
|
|
|
|
require.True(t, found)
|
2018-12-07 16:04:52 -08:00
|
|
|
power := keep.GetValidatorsByPowerIndexKey(validator)
|
2018-06-26 19:00:12 -07:00
|
|
|
require.True(t, keep.ValidatorByPowerIndexExists(ctx, keeper, power))
|
2018-06-13 00:12:57 -07:00
|
|
|
|
|
|
|
// create a second validator keep it bonded
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator = NewTestMsgCreateValidator(validatorAddr3, keep.PKs[2], initBond)
|
2018-06-13 00:12:57 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, got.IsOK(), "expected create-validator to be ok, got %v", got)
|
2018-06-13 00:12:57 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// must end-block
|
|
|
|
updates = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 1, len(updates))
|
|
|
|
|
2018-08-22 08:56:13 -07:00
|
|
|
// slash and jail the first validator
|
2018-09-24 21:09:31 -07:00
|
|
|
consAddr0 := sdk.ConsAddress(keep.PKs[0].Address())
|
2019-02-05 21:30:48 -08:00
|
|
|
keeper.Slash(ctx, consAddr0, 0, initPower, sdk.NewDecWithPrec(5, 1))
|
2018-09-24 21:09:31 -07:00
|
|
|
keeper.Jail(ctx, consAddr0)
|
2018-10-03 09:37:06 -07:00
|
|
|
keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
2018-06-13 00:12:57 -07:00
|
|
|
validator, found = keeper.GetValidator(ctx, validatorAddr)
|
|
|
|
require.True(t, found)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, sdk.Unbonding, validator.Status) // ensure is unbonding
|
2019-02-21 09:35:55 -08:00
|
|
|
require.Equal(t, initBond.QuoRaw(2), validator.Tokens) // ensure tokens slashed
|
2018-10-03 09:37:06 -07:00
|
|
|
keeper.Unjail(ctx, consAddr0)
|
2018-06-13 00:12:57 -07:00
|
|
|
|
|
|
|
// the old power record should have been deleted as the power changed
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, keep.ValidatorByPowerIndexExists(ctx, keeper, power))
|
2018-06-13 00:12:57 -07:00
|
|
|
|
|
|
|
// but the new power record should have been created
|
|
|
|
validator, found = keeper.GetValidator(ctx, validatorAddr)
|
|
|
|
require.True(t, found)
|
2018-12-07 16:04:52 -08:00
|
|
|
power2 := GetValidatorsByPowerIndexKey(validator)
|
2018-06-26 19:00:12 -07:00
|
|
|
require.True(t, keep.ValidatorByPowerIndexExists(ctx, keeper, power2))
|
2018-06-13 00:12:57 -07:00
|
|
|
|
|
|
|
// now the new record power index should be the same as the original record
|
2018-12-07 16:04:52 -08:00
|
|
|
power3 := GetValidatorsByPowerIndexKey(validator)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, power2, power3)
|
2018-06-13 00:12:57 -07:00
|
|
|
|
|
|
|
// unbond self-delegation
|
2019-02-21 00:05:31 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(sdk.AccAddress(validatorAddr), validatorAddr, initBond.ToDec())
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2018-06-26 19:00:12 -07:00
|
|
|
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
2018-10-07 21:43:47 -07:00
|
|
|
var finishTime time.Time
|
2018-11-04 18:28:38 -08:00
|
|
|
types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(finishTime)
|
|
|
|
EndBlocker(ctx, keeper)
|
2018-06-13 00:12:57 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
2018-06-13 00:12:57 -07:00
|
|
|
// verify that by power key nolonger exists
|
|
|
|
_, found = keeper.GetValidator(ctx, validatorAddr)
|
|
|
|
require.False(t, found)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, keep.ValidatorByPowerIndexExists(ctx, keeper, power3))
|
2018-06-13 00:12:57 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:22:46 -07:00
|
|
|
func TestDuplicatesMsgCreateValidator(t *testing.T) {
|
2018-06-26 19:00:12 -07:00
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
addr1, addr2 := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1])
|
2018-07-09 17:42:57 -07:00
|
|
|
pk1, pk2 := keep.PKs[0], keep.PKs[1]
|
|
|
|
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator1 := NewTestMsgCreateValidator(addr1, pk1, valTokens)
|
2018-07-09 17:42:57 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator1, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, got.IsOK(), "%v", got)
|
2018-06-13 00:12:57 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
|
|
|
|
validator, found := keeper.GetValidator(ctx, addr1)
|
2018-04-03 19:26:39 -07:00
|
|
|
require.True(t, found)
|
2018-07-13 13:46:14 -07:00
|
|
|
assert.Equal(t, sdk.Bonded, validator.Status)
|
2019-02-25 07:16:52 -08:00
|
|
|
assert.Equal(t, addr1, validator.OperatorAddress)
|
2018-09-08 01:44:58 -07:00
|
|
|
assert.Equal(t, pk1, validator.ConsPubKey)
|
2019-02-05 21:30:48 -08:00
|
|
|
assert.Equal(t, valTokens, validator.BondedTokens())
|
2019-02-21 00:05:31 -08:00
|
|
|
assert.Equal(t, valTokens.ToDec(), validator.DelegatorShares)
|
2018-07-09 17:42:57 -07:00
|
|
|
assert.Equal(t, Description{}, validator.Description)
|
|
|
|
|
2018-08-16 13:47:59 -07:00
|
|
|
// two validators can't have the same operator address
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator2 := NewTestMsgCreateValidator(addr1, pk2, valTokens)
|
2018-07-09 17:42:57 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator2, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, got.IsOK(), "%v", got)
|
2018-07-09 17:42:57 -07:00
|
|
|
|
|
|
|
// two validators can't have the same pubkey
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator3 := NewTestMsgCreateValidator(addr2, pk1, valTokens)
|
2018-07-09 17:42:57 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator3, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, got.IsOK(), "%v", got)
|
2018-07-09 17:42:57 -07:00
|
|
|
|
2018-08-16 13:47:59 -07:00
|
|
|
// must have different pubkey and operator
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator4 := NewTestMsgCreateValidator(addr2, pk2, valTokens)
|
2018-07-09 17:42:57 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator4, keeper)
|
|
|
|
require.True(t, got.IsOK(), "%v", got)
|
2018-10-03 09:37:06 -07:00
|
|
|
|
|
|
|
// must end-block
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 1, len(updates))
|
|
|
|
|
2018-07-09 17:42:57 -07:00
|
|
|
validator, found = keeper.GetValidator(ctx, addr2)
|
|
|
|
|
|
|
|
require.True(t, found)
|
2018-07-13 13:46:14 -07:00
|
|
|
assert.Equal(t, sdk.Bonded, validator.Status)
|
2019-02-25 07:16:52 -08:00
|
|
|
assert.Equal(t, addr2, validator.OperatorAddress)
|
2018-09-08 01:44:58 -07:00
|
|
|
assert.Equal(t, pk2, validator.ConsPubKey)
|
2019-02-05 21:30:48 -08:00
|
|
|
assert.True(sdk.IntEq(t, valTokens, validator.Tokens))
|
2019-02-21 00:05:31 -08:00
|
|
|
assert.True(sdk.DecEq(t, valTokens.ToDec(), validator.DelegatorShares))
|
2018-07-09 17:42:57 -07:00
|
|
|
assert.Equal(t, Description{}, validator.Description)
|
2018-03-29 07:49:18 -07:00
|
|
|
}
|
|
|
|
|
2018-11-29 09:21:45 -08:00
|
|
|
func TestInvalidPubKeyTypeMsgCreateValidator(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
|
|
|
|
|
|
|
addr := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
invalidPk := secp256k1.GenPrivKey().PubKey()
|
|
|
|
|
|
|
|
// invalid pukKey type should not be allowed
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(addr, invalidPk, sdk.NewInt(10))
|
2018-11-29 09:21:45 -08:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.False(t, got.IsOK(), "%v", got)
|
|
|
|
|
|
|
|
ctx = ctx.WithConsensusParams(&abci.ConsensusParams{
|
|
|
|
Validator: &abci.ValidatorParams{PubKeyTypes: []string{tmtypes.ABCIPubKeyTypeSecp256k1}},
|
|
|
|
})
|
|
|
|
|
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "%v", got)
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:16:37 -07:00
|
|
|
func TestDuplicatesMsgCreateValidatorOnBehalfOf(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr := sdk.ValAddress(keep.Addrs[0])
|
2018-07-10 17:16:37 -07:00
|
|
|
delegatorAddr := keep.Addrs[1]
|
|
|
|
pk := keep.PKs[0]
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidatorOnBehalfOf := NewTestMsgCreateValidatorOnBehalfOf(delegatorAddr, validatorAddr, pk, valTokens)
|
2018-07-10 17:16:37 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidatorOnBehalfOf, keeper)
|
|
|
|
require.True(t, got.IsOK(), "%v", got)
|
2018-10-03 09:37:06 -07:00
|
|
|
|
|
|
|
// must end-block
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 1, len(updates))
|
|
|
|
|
2018-07-10 17:16:37 -07:00
|
|
|
validator, found := keeper.GetValidator(ctx, validatorAddr)
|
|
|
|
|
|
|
|
require.True(t, found)
|
2018-07-13 13:46:14 -07:00
|
|
|
assert.Equal(t, sdk.Bonded, validator.Status)
|
2019-02-25 07:16:52 -08:00
|
|
|
assert.Equal(t, validatorAddr, validator.OperatorAddress)
|
2018-09-08 01:44:58 -07:00
|
|
|
assert.Equal(t, pk, validator.ConsPubKey)
|
2019-02-05 21:30:48 -08:00
|
|
|
assert.True(sdk.IntEq(t, valTokens, validator.Tokens))
|
2019-02-21 00:05:31 -08:00
|
|
|
assert.True(sdk.DecEq(t, valTokens.ToDec(), validator.DelegatorShares))
|
2018-07-13 13:46:14 -07:00
|
|
|
assert.Equal(t, Description{}, validator.Description)
|
2018-07-10 17:16:37 -07:00
|
|
|
|
|
|
|
// one validator cannot be created twice even from different delegator
|
2019-02-25 07:16:52 -08:00
|
|
|
msgCreateValidatorOnBehalfOf.DelegatorAddress = keep.Addrs[2]
|
2018-07-10 17:16:37 -07:00
|
|
|
msgCreateValidatorOnBehalfOf.PubKey = keep.PKs[1]
|
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidatorOnBehalfOf, keeper)
|
|
|
|
require.False(t, got.IsOK(), "%v", got)
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:21:12 -07:00
|
|
|
func TestLegacyValidatorDelegations(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, int64(1000))
|
|
|
|
setInstantUnbondPeriod(keeper, ctx)
|
|
|
|
|
2019-02-13 15:01:50 -08:00
|
|
|
bondAmount := sdk.TokensFromTendermintPower(10)
|
2018-09-24 21:09:31 -07:00
|
|
|
valAddr := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
valConsPubKey, valConsAddr := keep.PKs[0], sdk.ConsAddress(keep.PKs[0].Address())
|
2018-08-31 12:21:12 -07:00
|
|
|
delAddr := keep.Addrs[1]
|
|
|
|
|
|
|
|
// create validator
|
2018-10-09 10:58:59 -07:00
|
|
|
msgCreateVal := NewTestMsgCreateValidator(valAddr, valConsPubKey, bondAmount)
|
2018-08-31 12:21:12 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateVal, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected create validator msg to be ok, got %v", got)
|
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// must end-block
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 1, len(updates))
|
|
|
|
|
2018-08-31 12:21:12 -07:00
|
|
|
// verify the validator exists and has the correct attributes
|
|
|
|
validator, found := keeper.GetValidator(ctx, valAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Equal(t, sdk.Bonded, validator.Status)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount, validator.DelegatorShares.RoundInt())
|
|
|
|
require.Equal(t, bondAmount, validator.BondedTokens())
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
// delegate tokens to the validator
|
2018-10-09 10:58:59 -07:00
|
|
|
msgDelegate := NewTestMsgDelegate(delAddr, valAddr, bondAmount)
|
2018-08-31 12:21:12 -07:00
|
|
|
got = handleMsgDelegate(ctx, msgDelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected delegation to be ok, got %v", got)
|
|
|
|
|
|
|
|
// verify validator bonded shares
|
|
|
|
validator, found = keeper.GetValidator(ctx, valAddr)
|
|
|
|
require.True(t, found)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount.MulRaw(2), validator.DelegatorShares.RoundInt())
|
|
|
|
require.Equal(t, bondAmount.MulRaw(2), validator.BondedTokens())
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
// unbond validator total self-delegations (which should jail the validator)
|
2019-02-13 15:01:50 -08:00
|
|
|
unbondShares := sdk.TokensFromTendermintPower(10)
|
2019-02-21 00:05:31 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(sdk.AccAddress(valAddr), valAddr, unbondShares.ToDec())
|
2018-08-31 12:21:12 -07:00
|
|
|
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2018-08-31 12:21:12 -07:00
|
|
|
require.True(t, got.IsOK(), "expected begin unbonding validator msg to be ok, got %v", got)
|
|
|
|
|
2018-10-07 21:43:47 -07:00
|
|
|
var finishTime time.Time
|
2018-11-04 18:28:38 -08:00
|
|
|
types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(finishTime)
|
|
|
|
EndBlocker(ctx, keeper)
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
// verify the validator record still exists, is jailed, and has correct tokens
|
|
|
|
validator, found = keeper.GetValidator(ctx, valAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.True(t, validator.Jailed)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount, validator.Tokens)
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
// verify delegation still exists
|
|
|
|
bond, found := keeper.GetDelegation(ctx, delAddr, valAddr)
|
|
|
|
require.True(t, found)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount, bond.Shares.RoundInt())
|
|
|
|
require.Equal(t, bondAmount, validator.DelegatorShares.RoundInt())
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
// verify the validator can still self-delegate
|
2018-10-09 10:58:59 -07:00
|
|
|
msgSelfDelegate := NewTestMsgDelegate(sdk.AccAddress(valAddr), valAddr, bondAmount)
|
2018-08-31 12:21:12 -07:00
|
|
|
got = handleMsgDelegate(ctx, msgSelfDelegate, keeper)
|
2019-02-07 17:41:23 -08:00
|
|
|
require.True(t, got.IsOK(), "expected delegation to be ok, got %v", got)
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
// verify validator bonded shares
|
|
|
|
validator, found = keeper.GetValidator(ctx, valAddr)
|
|
|
|
require.True(t, found)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount.MulRaw(2), validator.DelegatorShares.RoundInt())
|
|
|
|
require.Equal(t, bondAmount.MulRaw(2), validator.Tokens)
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
// unjail the validator now that is has non-zero self-delegated shares
|
2018-09-24 21:09:31 -07:00
|
|
|
keeper.Unjail(ctx, valConsAddr)
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
// verify the validator can now accept delegations
|
2018-10-09 10:58:59 -07:00
|
|
|
msgDelegate = NewTestMsgDelegate(delAddr, valAddr, bondAmount)
|
2018-08-31 12:21:12 -07:00
|
|
|
got = handleMsgDelegate(ctx, msgDelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected delegation to be ok, got %v", got)
|
|
|
|
|
|
|
|
// verify validator bonded shares
|
|
|
|
validator, found = keeper.GetValidator(ctx, valAddr)
|
|
|
|
require.True(t, found)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount.MulRaw(3), validator.DelegatorShares.RoundInt())
|
|
|
|
require.Equal(t, bondAmount.MulRaw(3), validator.Tokens)
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
// verify new delegation
|
|
|
|
bond, found = keeper.GetDelegation(ctx, delAddr, valAddr)
|
|
|
|
require.True(t, found)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount.MulRaw(2), bond.Shares.RoundInt())
|
|
|
|
require.Equal(t, bondAmount.MulRaw(3), validator.DelegatorShares.RoundInt())
|
2018-08-31 12:21:12 -07:00
|
|
|
}
|
|
|
|
|
2018-03-29 07:49:18 -07:00
|
|
|
func TestIncrementsMsgDelegate(t *testing.T) {
|
2019-02-05 21:30:48 -08:00
|
|
|
initPower := int64(1000)
|
2019-02-13 15:01:50 -08:00
|
|
|
initBond := sdk.TokensFromTendermintPower(initPower)
|
2019-02-05 21:30:48 -08:00
|
|
|
ctx, accMapper, keeper := keep.CreateTestInput(t, false, initPower)
|
2018-04-01 09:05:58 -07:00
|
|
|
params := keeper.GetParams(ctx)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
2019-02-13 15:01:50 -08:00
|
|
|
bondAmount := sdk.TokensFromTendermintPower(10)
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr, delegatorAddr := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1]
|
2018-04-01 09:05:58 -07:00
|
|
|
|
2018-05-31 12:22:46 -07:00
|
|
|
// first create validator
|
2018-10-09 10:58:59 -07:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], bondAmount)
|
2018-05-31 12:22:46 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, got.IsOK(), "expected create validator msg to be ok, got %v", got)
|
2018-04-03 19:26:39 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// apply TM updates
|
|
|
|
keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
|
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-07-13 13:46:14 -07:00
|
|
|
require.Equal(t, sdk.Bonded, validator.Status)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount, validator.DelegatorShares.RoundInt())
|
|
|
|
require.Equal(t, bondAmount, validator.BondedTokens(), "validator: %v", validator)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
2018-05-15 20:32:18 -07:00
|
|
|
_, found = keeper.GetDelegation(ctx, delegatorAddr, validatorAddr)
|
|
|
|
require.False(t, found)
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
bond, found := keeper.GetDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
|
2018-05-15 20:32:18 -07:00
|
|
|
require.True(t, found)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount, bond.Shares.RoundInt())
|
2018-05-15 20:32:18 -07:00
|
|
|
|
|
|
|
pool := keeper.GetPool(ctx)
|
2018-07-13 13:46:14 -07:00
|
|
|
exRate := validator.DelegatorShareExRate()
|
2018-08-14 17:15:02 -07:00
|
|
|
require.True(t, exRate.Equal(sdk.OneDec()), "expected exRate 1 got %v", exRate)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, bondAmount, pool.BondedTokens)
|
2018-05-15 20:32:18 -07:00
|
|
|
|
2018-03-29 07:49:18 -07:00
|
|
|
// just send the same msgbond multiple times
|
2018-10-09 10:58:59 -07:00
|
|
|
msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, bondAmount)
|
2018-04-23 09:41:36 -07:00
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
for i := int64(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)
|
|
|
|
|
2018-07-13 13:46:14 -07:00
|
|
|
exRate := validator.DelegatorShareExRate()
|
2018-08-14 17:15:02 -07:00
|
|
|
require.True(t, exRate.Equal(sdk.OneDec()), "expected exRate 1 got %v, i = %v", exRate, i)
|
2018-05-15 20:32:18 -07:00
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
expBond := bondAmount.MulRaw(i + 1)
|
|
|
|
expDelegatorShares := bondAmount.MulRaw(i + 2) // (1 self delegation)
|
|
|
|
expDelegatorAcc := initBond.Sub(expBond)
|
2018-04-03 19:26:39 -07:00
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
gotBond := bond.Shares.RoundInt()
|
|
|
|
gotDelegatorShares := validator.DelegatorShares.RoundInt()
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 12:44:19 -08:00
|
|
|
func TestEditValidatorDecreaseMinSelfDelegation(t *testing.T) {
|
|
|
|
validatorAddr := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
|
|
|
|
initPower := int64(100)
|
2019-02-13 15:01:50 -08:00
|
|
|
initBond := sdk.TokensFromTendermintPower(100)
|
2019-02-08 12:44:19 -08:00
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, initPower)
|
|
|
|
_ = setInstantUnbondPeriod(keeper, ctx)
|
|
|
|
|
|
|
|
// create validator
|
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], initBond)
|
|
|
|
msgCreateValidator.MinSelfDelegation = sdk.NewInt(2)
|
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected create-validator to be ok, got %v", got)
|
|
|
|
|
|
|
|
// must end-block
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 1, len(updates))
|
|
|
|
|
|
|
|
// verify the self-delegation exists
|
|
|
|
bond, found := keeper.GetDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
gotBond := bond.Shares.RoundInt()
|
|
|
|
require.Equal(t, initBond, gotBond,
|
|
|
|
"initBond: %v\ngotBond: %v\nbond: %v\n",
|
|
|
|
initBond, gotBond, bond)
|
|
|
|
|
|
|
|
newMinSelfDelegation := sdk.OneInt()
|
|
|
|
msgEditValidator := NewMsgEditValidator(validatorAddr, Description{}, nil, &newMinSelfDelegation)
|
|
|
|
got = handleMsgEditValidator(ctx, msgEditValidator, keeper)
|
|
|
|
require.False(t, got.IsOK(), "should not be able to decrease minSelfDelegation")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) {
|
|
|
|
validatorAddr := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
|
|
|
|
initPower := int64(100)
|
2019-02-13 15:01:50 -08:00
|
|
|
initBond := sdk.TokensFromTendermintPower(100)
|
2019-02-08 12:44:19 -08:00
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, initPower)
|
|
|
|
_ = setInstantUnbondPeriod(keeper, ctx)
|
|
|
|
|
|
|
|
// create validator
|
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], initBond)
|
|
|
|
msgCreateValidator.MinSelfDelegation = sdk.NewInt(2)
|
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected create-validator to be ok, got %v", got)
|
|
|
|
|
|
|
|
// must end-block
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 1, len(updates))
|
|
|
|
|
|
|
|
// verify the self-delegation exists
|
|
|
|
bond, found := keeper.GetDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
gotBond := bond.Shares.RoundInt()
|
|
|
|
require.Equal(t, initBond, gotBond,
|
|
|
|
"initBond: %v\ngotBond: %v\nbond: %v\n",
|
|
|
|
initBond, gotBond, bond)
|
|
|
|
|
|
|
|
newMinSelfDelegation := initBond.Add(sdk.OneInt())
|
|
|
|
msgEditValidator := NewMsgEditValidator(validatorAddr, Description{}, nil, &newMinSelfDelegation)
|
|
|
|
got = handleMsgEditValidator(ctx, msgEditValidator, keeper)
|
|
|
|
require.False(t, got.IsOK(), "should not be able to increase minSelfDelegation above current self delegation")
|
|
|
|
}
|
|
|
|
|
2018-03-29 07:49:18 -07:00
|
|
|
func TestIncrementsMsgUnbond(t *testing.T) {
|
2019-02-05 21:30:48 -08:00
|
|
|
initPower := int64(1000)
|
2019-02-13 15:01:50 -08:00
|
|
|
initBond := sdk.TokensFromTendermintPower(initPower)
|
2019-02-05 21:30:48 -08:00
|
|
|
ctx, accMapper, keeper := keep.CreateTestInput(t, false, initPower)
|
2018-06-26 19:00:12 -07:00
|
|
|
params := setInstantUnbondPeriod(keeper, ctx)
|
2018-07-12 16:38:35 -07:00
|
|
|
denom := params.BondDenom
|
2018-04-01 09:05:58 -07:00
|
|
|
|
2018-05-31 12:22:46 -07:00
|
|
|
// create validator, delegate
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr, delegatorAddr := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1]
|
2018-04-03 19:26:39 -07:00
|
|
|
|
2018-10-09 10:58:59 -07:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], initBond)
|
2018-05-31 12:22:46 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, got.IsOK(), "expected create-validator to be ok, got %v", got)
|
2018-04-03 19:26:39 -07:00
|
|
|
|
2018-07-12 16:38:35 -07:00
|
|
|
// initial balance
|
|
|
|
amt1 := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(denom)
|
|
|
|
|
2018-10-09 10:58:59 -07:00
|
|
|
msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, initBond)
|
2018-04-01 09:05:58 -07:00
|
|
|
got = handleMsgDelegate(ctx, msgDelegate, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, got.IsOK(), "expected delegation to be ok, got %v", got)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
2018-07-12 16:38:35 -07:00
|
|
|
// balance should have been subtracted after delegation
|
|
|
|
amt2 := accMapper.GetAccount(ctx, delegatorAddr).GetCoins().AmountOf(denom)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.True(sdk.IntEq(t, amt1.Sub(initBond), amt2))
|
2018-07-12 16:38:35 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// apply TM updates
|
|
|
|
keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
|
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)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, initBond.MulRaw(2), validator.DelegatorShares.RoundInt())
|
|
|
|
require.Equal(t, initBond.MulRaw(2), validator.BondedTokens())
|
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-08-14 17:15:02 -07:00
|
|
|
unbondShares := sdk.NewDec(10)
|
2019-01-17 09:53:22 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(delegatorAddr, validatorAddr, unbondShares)
|
2019-02-05 21:30:48 -08:00
|
|
|
numUnbonds := int64(5)
|
|
|
|
for i := int64(0); i < numUnbonds; i++ {
|
2019-01-16 02:35:18 -08:00
|
|
|
|
2019-01-17 09:53:22 -08:00
|
|
|
got := handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2018-06-26 19:00:12 -07:00
|
|
|
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
|
2018-10-07 21:43:47 -07:00
|
|
|
var finishTime time.Time
|
2018-11-04 18:28:38 -08:00
|
|
|
types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(finishTime)
|
|
|
|
EndBlocker(ctx, keeper)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
2019-01-16 02:35:18 -08: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
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
expBond := initBond.Sub(unbondShares.MulInt64(i + 1).RoundInt())
|
|
|
|
expDelegatorShares := (initBond.MulRaw(2)).Sub(unbondShares.MulInt64(i + 1).RoundInt())
|
|
|
|
expDelegatorAcc := initBond.Sub(expBond)
|
2018-04-03 19:26:39 -07:00
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
gotBond := bond.Shares.RoundInt()
|
|
|
|
gotDelegatorShares := validator.DelegatorShares.RoundInt()
|
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
|
2019-02-05 21:30:48 -08:00
|
|
|
errorCases := []sdk.Int{
|
|
|
|
//1<<64 - 1, // more than int64 power
|
|
|
|
//1<<63 + 1, // more than int64 power
|
2019-02-13 15:01:50 -08:00
|
|
|
sdk.TokensFromTendermintPower(1<<63 - 1),
|
|
|
|
sdk.TokensFromTendermintPower(1 << 31),
|
2018-06-26 19:00:12 -07:00
|
|
|
initBond,
|
2018-03-29 07:49:18 -07:00
|
|
|
}
|
2019-02-05 21:30:48 -08:00
|
|
|
for i, c := range errorCases {
|
2019-02-21 00:05:31 -08:00
|
|
|
unbondShares := c.ToDec()
|
2019-01-17 09:53:22 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(delegatorAddr, validatorAddr, unbondShares)
|
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.False(t, got.IsOK(), "expected unbond msg to fail, index: %v", i)
|
2018-03-29 07:49:18 -07:00
|
|
|
}
|
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
leftBonded := initBond.Sub(unbondShares.MulInt64(numUnbonds).RoundInt())
|
2018-03-29 07:49:18 -07:00
|
|
|
|
|
|
|
// should be unable to unbond one more than we have
|
2019-02-21 00:05:31 -08:00
|
|
|
unbondShares = leftBonded.AddRaw(1).ToDec()
|
2019-01-17 09:53:22 -08:00
|
|
|
msgUndelegate = NewMsgUndelegate(delegatorAddr, validatorAddr, unbondShares)
|
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, got.IsOK(),
|
2019-01-17 09:53:22 -08:00
|
|
|
"got: %v\nmsgUnbond: %v\nshares: %v\nleftBonded: %v\n", got, msgUndelegate, unbondShares.String(), leftBonded)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
|
|
|
// should be able to unbond just what we have
|
2019-02-21 00:05:31 -08:00
|
|
|
unbondShares = leftBonded.ToDec()
|
2019-01-17 09:53:22 -08:00
|
|
|
msgUndelegate = NewMsgUndelegate(delegatorAddr, validatorAddr, unbondShares)
|
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, got.IsOK(),
|
2019-01-17 09:53:22 -08:00
|
|
|
"got: %v\nmsgUnbond: %v\nshares: %v\nleftBonded: %v\n", got, msgUndelegate, unbondShares, leftBonded)
|
2018-03-29 07:49:18 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 12:22:46 -07:00
|
|
|
func TestMultipleMsgCreateValidator(t *testing.T) {
|
2019-02-05 21:30:48 -08:00
|
|
|
initPower := int64(1000)
|
2019-02-13 15:01:50 -08:00
|
|
|
initTokens := sdk.TokensFromTendermintPower(initPower)
|
2019-02-05 21:30:48 -08:00
|
|
|
ctx, accMapper, keeper := keep.CreateTestInput(t, false, initPower)
|
2018-06-26 19:00:12 -07:00
|
|
|
params := setInstantUnbondPeriod(keeper, ctx)
|
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
validatorAddrs := []sdk.ValAddress{
|
|
|
|
sdk.ValAddress(keep.Addrs[0]),
|
|
|
|
sdk.ValAddress(keep.Addrs[1]),
|
|
|
|
sdk.ValAddress(keep.Addrs[2]),
|
|
|
|
}
|
|
|
|
delegatorAddrs := []sdk.AccAddress{
|
|
|
|
keep.Addrs[3],
|
|
|
|
keep.Addrs[4],
|
|
|
|
keep.Addrs[5],
|
|
|
|
}
|
2018-03-29 07:49:18 -07:00
|
|
|
|
|
|
|
// bond them all
|
2018-05-09 21:01:58 -07:00
|
|
|
for i, validatorAddr := range validatorAddrs {
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidatorOnBehalfOf := NewTestMsgCreateValidatorOnBehalfOf(
|
|
|
|
delegatorAddrs[i], validatorAddr, keep.PKs[i], valTokens)
|
2018-07-10 17:16:37 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidatorOnBehalfOf, 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]
|
2019-02-05 21:30:48 -08:00
|
|
|
balanceExpd := initTokens.Sub(valTokens)
|
2018-07-10 17:16:37 -07:00
|
|
|
balanceGot := accMapper.GetAccount(ctx, delegatorAddrs[i]).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)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, valTokens, val.DelegatorShares.RoundInt(), "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
|
|
|
}
|
|
|
|
|
2018-08-22 08:56:13 -07:00
|
|
|
// unbond them all by removing delegation
|
2018-05-09 21:01:58 -07:00
|
|
|
for i, validatorAddr := range validatorAddrs {
|
2018-07-10 17:16:37 -07:00
|
|
|
_, found := keeper.GetValidator(ctx, validatorAddr)
|
2018-04-01 09:05:58 -07:00
|
|
|
require.True(t, found)
|
2019-02-13 15:01:50 -08:00
|
|
|
unbondingTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-21 00:05:31 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(delegatorAddrs[i], validatorAddr, unbondingTokens.ToDec()) // remove delegation
|
2019-01-17 09:53:22 -08:00
|
|
|
got := handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2018-06-26 19:00:12 -07:00
|
|
|
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
|
2018-10-07 21:43:47 -07:00
|
|
|
var finishTime time.Time
|
2018-11-04 22:11:03 -08:00
|
|
|
// Jump to finishTime for unbonding period and remove from unbonding queue
|
2018-11-04 18:28:38 -08:00
|
|
|
types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(finishTime)
|
|
|
|
EndBlocker(ctx, keeper)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
2018-11-04 22:11:03 -08:00
|
|
|
// Check that the validator is deleted from state
|
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)
|
|
|
|
|
2018-07-10 17:16:37 -07:00
|
|
|
gotBalance := accMapper.GetAccount(ctx, delegatorAddrs[i]).GetCoins().AmountOf(params.BondDenom)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, initTokens, gotBalance, "expected account to have %d, got %d", initTokens, gotBalance)
|
2018-03-29 07:49:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleMsgDelegate(t *testing.T) {
|
2018-06-26 19:00:12 -07:00
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr, delegatorAddrs := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1:]
|
2018-06-26 19:00:12 -07:00
|
|
|
_ = setInstantUnbondPeriod(keeper, ctx)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
2018-05-09 21:01:58 -07:00
|
|
|
//first make a validator
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], sdk.NewInt(10))
|
2018-05-31 12:22:46 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
2018-04-01 09:05:58 -07:00
|
|
|
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 {
|
2019-02-05 21:30:48 -08:00
|
|
|
msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, sdk.NewInt(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 {
|
2019-01-17 09:53:22 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(delegatorAddr, validatorAddr, sdk.NewDec(10))
|
|
|
|
got := handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2018-06-26 19:00:12 -07:00
|
|
|
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
|
2018-10-07 21:43:47 -07:00
|
|
|
var finishTime time.Time
|
2018-11-04 18:28:38 -08:00
|
|
|
types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(finishTime)
|
|
|
|
EndBlocker(ctx, keeper)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 08:56:13 -07:00
|
|
|
func TestJailValidator(t *testing.T) {
|
2018-06-26 19:00:12 -07:00
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr, delegatorAddr := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1]
|
2018-06-26 19:00:12 -07:00
|
|
|
_ = setInstantUnbondPeriod(keeper, ctx)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
2018-05-09 21:01:58 -07:00
|
|
|
// create the validator
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], sdk.NewInt(10))
|
2018-05-31 12:22:46 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
2018-03-29 07:49:18 -07:00
|
|
|
|
|
|
|
// bond a delegator
|
2019-02-05 21:30:48 -08:00
|
|
|
msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, sdk.NewInt(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
|
2019-01-17 09:53:22 -08:00
|
|
|
msgUndelegateValidator := NewMsgUndelegate(sdk.AccAddress(validatorAddr), validatorAddr, sdk.NewDec(10))
|
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegateValidator, keeper)
|
2018-10-03 09:37:06 -07:00
|
|
|
require.True(t, got.IsOK(), "expected no error: %v", got)
|
2018-10-07 21:43:47 -07:00
|
|
|
var finishTime time.Time
|
2018-11-04 18:28:38 -08:00
|
|
|
types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(finishTime)
|
|
|
|
EndBlocker(ctx, keeper)
|
2018-06-26 19:00:12 -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-08-22 08:56:13 -07:00
|
|
|
require.True(t, validator.Jailed, "%v", validator)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
|
|
|
// test that the delegator can still withdraw their bonds
|
2019-01-17 09:53:22 -08:00
|
|
|
msgUndelegateDelegator := NewMsgUndelegate(delegatorAddr, validatorAddr, sdk.NewDec(10))
|
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegateDelegator, keeper)
|
2018-06-26 19:00:12 -07:00
|
|
|
require.True(t, got.IsOK(), "expected no error")
|
2018-11-04 18:28:38 -08:00
|
|
|
types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(finishTime)
|
|
|
|
EndBlocker(ctx, keeper)
|
2018-03-29 07:49:18 -07:00
|
|
|
|
|
|
|
// verify that the pubkey can now be reused
|
2018-05-31 12:22:46 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, got.IsOK(), "expected ok, got %v", got)
|
2018-03-29 07:49:18 -07:00
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-10-14 17:37:06 -07:00
|
|
|
func TestValidatorQueue(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
|
|
|
validatorAddr, delegatorAddr := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1]
|
|
|
|
|
|
|
|
// set the unbonding time
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
params.UnbondingTime = 7 * time.Second
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
|
|
|
|
// create the validator
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], valTokens)
|
2018-10-14 17:37:06 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
|
|
|
// bond a delegator
|
2019-02-13 15:01:50 -08:00
|
|
|
delTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, delTokens)
|
2018-10-14 17:37:06 -07:00
|
|
|
got = handleMsgDelegate(ctx, msgDelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected ok, got %v", got)
|
|
|
|
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
|
|
|
// unbond the all self-delegation to put validator in unbonding state
|
2019-02-05 21:30:48 -08:00
|
|
|
msgUndelegateValidator := NewMsgUndelegate(sdk.AccAddress(validatorAddr),
|
2019-02-21 00:05:31 -08:00
|
|
|
validatorAddr, delTokens.ToDec())
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegateValidator, keeper)
|
2018-10-14 17:37:06 -07:00
|
|
|
require.True(t, got.IsOK(), "expected no error: %v", got)
|
|
|
|
var finishTime time.Time
|
2018-11-04 18:28:38 -08:00
|
|
|
types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
|
2018-10-14 17:37:06 -07:00
|
|
|
ctx = ctx.WithBlockTime(finishTime)
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
origHeader := ctx.BlockHeader()
|
|
|
|
|
|
|
|
validator, found := keeper.GetValidator(ctx, validatorAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.True(t, validator.GetStatus() == sdk.Unbonding, "%v", validator)
|
|
|
|
|
|
|
|
// should still be unbonding at time 6 seconds later
|
|
|
|
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 6))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
validator, found = keeper.GetValidator(ctx, validatorAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.True(t, validator.GetStatus() == sdk.Unbonding, "%v", validator)
|
|
|
|
|
|
|
|
// should be in unbonded state at time 7 seconds later
|
|
|
|
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 7))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
validator, found = keeper.GetValidator(ctx, validatorAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.True(t, validator.GetStatus() == sdk.Unbonded, "%v", validator)
|
|
|
|
}
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
func TestUnbondingPeriod(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr := sdk.ValAddress(keep.Addrs[0])
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// set the unbonding time
|
|
|
|
params := keeper.GetParams(ctx)
|
2018-08-12 00:33:48 -07:00
|
|
|
params.UnbondingTime = 7 * time.Second
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
|
|
|
|
// create the validator
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], valTokens)
|
2018-06-26 19:00:12 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
2018-10-07 21:43:47 -07:00
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
// begin unbonding
|
2019-02-13 15:01:50 -08:00
|
|
|
unbondingTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(sdk.AccAddress(validatorAddr),
|
2019-02-21 00:05:31 -08:00
|
|
|
validatorAddr, unbondingTokens.ToDec())
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2018-06-26 19:00:12 -07:00
|
|
|
require.True(t, got.IsOK(), "expected no error")
|
2018-10-07 21:43:47 -07:00
|
|
|
origHeader := ctx.BlockHeader()
|
|
|
|
|
|
|
|
_, found := keeper.GetUnbondingDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
|
|
|
|
require.True(t, found, "should not have unbonded")
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// cannot complete unbonding at same time
|
2018-10-07 21:43:47 -07:00
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
_, found = keeper.GetUnbondingDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
|
|
|
|
require.True(t, found, "should not have unbonded")
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// cannot complete unbonding at time 6 seconds later
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 6))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
_, found = keeper.GetUnbondingDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
|
|
|
|
require.True(t, found, "should not have unbonded")
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// can complete unbonding at time 7 seconds later
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 7))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
_, found = keeper.GetUnbondingDelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
|
|
|
|
require.False(t, found, "should have unbonded")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnbondingFromUnbondingValidator(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
|
|
|
validatorAddr, delegatorAddr := sdk.ValAddress(keep.Addrs[0]), keep.Addrs[1]
|
|
|
|
|
|
|
|
// create the validator
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], sdk.NewInt(10))
|
2018-10-07 21:43:47 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
|
|
|
// bond a delegator
|
2019-02-05 21:30:48 -08:00
|
|
|
msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, sdk.NewInt(10))
|
2018-10-07 21:43:47 -07:00
|
|
|
got = handleMsgDelegate(ctx, msgDelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected ok, got %v", got)
|
|
|
|
|
|
|
|
// unbond the validators bond portion
|
2019-01-17 09:53:22 -08:00
|
|
|
msgUndelegateValidator := NewMsgUndelegate(sdk.AccAddress(validatorAddr), validatorAddr, sdk.NewDec(10))
|
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegateValidator, keeper)
|
2018-10-07 21:43:47 -07:00
|
|
|
require.True(t, got.IsOK(), "expected no error")
|
|
|
|
|
|
|
|
// change the ctx to Block Time one second before the validator would have unbonded
|
|
|
|
var finishTime time.Time
|
2018-11-04 18:28:38 -08:00
|
|
|
types.MsgCdc.MustUnmarshalBinaryLengthPrefixed(got.Data, &finishTime)
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(finishTime.Add(time.Second * -1))
|
|
|
|
|
|
|
|
// unbond the delegator from the validator
|
2019-01-17 09:53:22 -08:00
|
|
|
msgUndelegateDelegator := NewMsgUndelegate(delegatorAddr, validatorAddr, sdk.NewDec(10))
|
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegateDelegator, keeper)
|
2018-06-26 19:00:12 -07:00
|
|
|
require.True(t, got.IsOK(), "expected no error")
|
2018-10-07 21:43:47 -07:00
|
|
|
|
|
|
|
// move the Block time forward by one second
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(time.Second * 1))
|
|
|
|
|
|
|
|
// Run the EndBlocker
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
|
|
|
// Check to make sure that the unbonding delegation is no longer in state
|
|
|
|
// (meaning it was deleted in the above EndBlocker)
|
|
|
|
_, found := keeper.GetUnbondingDelegation(ctx, delegatorAddr, validatorAddr)
|
|
|
|
require.False(t, found, "should be removed from state")
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedelegationPeriod(t *testing.T) {
|
2018-07-12 16:38:35 -07:00
|
|
|
ctx, AccMapper, keeper := keep.CreateTestInput(t, false, 1000)
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr, validatorAddr2 := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1])
|
2018-07-12 16:38:35 -07:00
|
|
|
denom := keeper.GetParams(ctx).BondDenom
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// set the unbonding time
|
|
|
|
params := keeper.GetParams(ctx)
|
2018-08-12 00:33:48 -07:00
|
|
|
params.UnbondingTime = 7 * time.Second
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
|
|
|
|
// create the validators
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], sdk.NewInt(10))
|
2018-07-12 16:38:35 -07:00
|
|
|
|
|
|
|
// initial balance
|
2018-08-30 21:06:44 -07:00
|
|
|
amt1 := AccMapper.GetAccount(ctx, sdk.AccAddress(validatorAddr)).GetCoins().AmountOf(denom)
|
2018-07-12 16:38:35 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
2018-07-12 16:38:35 -07:00
|
|
|
// balance should have been subtracted after creation
|
2018-08-30 21:06:44 -07:00
|
|
|
amt2 := AccMapper.GetAccount(ctx, sdk.AccAddress(validatorAddr)).GetCoins().AmountOf(denom)
|
2018-07-12 16:38:35 -07:00
|
|
|
require.Equal(t, amt1.Sub(sdk.NewInt(10)).Int64(), amt2.Int64(), "expected coins to be subtracted")
|
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator = NewTestMsgCreateValidator(validatorAddr2, keep.PKs[1], sdk.NewInt(10))
|
2018-06-26 19:00:12 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
bal1 := AccMapper.GetAccount(ctx, sdk.AccAddress(validatorAddr)).GetCoins()
|
2018-07-12 16:38:35 -07:00
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
// begin redelegate
|
2018-08-30 21:06:44 -07:00
|
|
|
msgBeginRedelegate := NewMsgBeginRedelegate(sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2, sdk.NewDec(10))
|
2018-06-26 19:00:12 -07:00
|
|
|
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error, %v", got)
|
|
|
|
|
2018-07-12 16:38:35 -07:00
|
|
|
// origin account should not lose tokens as with a regular delegation
|
2018-08-30 21:06:44 -07:00
|
|
|
bal2 := AccMapper.GetAccount(ctx, sdk.AccAddress(validatorAddr)).GetCoins()
|
2018-07-12 16:38:35 -07:00
|
|
|
require.Equal(t, bal1, bal2)
|
|
|
|
|
2018-10-07 21:43:47 -07:00
|
|
|
origHeader := ctx.BlockHeader()
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
// cannot complete redelegation at same time
|
2018-10-07 21:43:47 -07:00
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
_, found := keeper.GetRedelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2)
|
|
|
|
require.True(t, found, "should not have unbonded")
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// cannot complete redelegation at time 6 seconds later
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 6))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
_, found = keeper.GetRedelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2)
|
|
|
|
require.True(t, found, "should not have unbonded")
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// can complete redelegation at time 7 seconds later
|
2018-10-07 21:43:47 -07:00
|
|
|
ctx = ctx.WithBlockTime(origHeader.Time.Add(time.Second * 7))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
_, found = keeper.GetRedelegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2)
|
|
|
|
require.False(t, found, "should have unbonded")
|
2018-06-26 19:00:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransitiveRedelegation(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
validatorAddr2 := sdk.ValAddress(keep.Addrs[1])
|
|
|
|
validatorAddr3 := sdk.ValAddress(keep.Addrs[2])
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// set the unbonding time
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
params.UnbondingTime = 0
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
|
|
|
|
// create the validators
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], sdk.NewInt(10))
|
2018-06-26 19:00:12 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator = NewTestMsgCreateValidator(validatorAddr2, keep.PKs[1], sdk.NewInt(10))
|
2018-06-26 19:00:12 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator = NewTestMsgCreateValidator(validatorAddr3, keep.PKs[2], sdk.NewInt(10))
|
2018-06-26 19:00:12 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
|
|
|
// begin redelegate
|
2018-08-30 21:06:44 -07:00
|
|
|
msgBeginRedelegate := NewMsgBeginRedelegate(sdk.AccAddress(validatorAddr), validatorAddr, validatorAddr2, sdk.NewDec(10))
|
2018-06-26 19:00:12 -07:00
|
|
|
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error, %v", got)
|
|
|
|
|
|
|
|
// cannot redelegation to next validator while first delegation exists
|
2018-08-30 21:06:44 -07:00
|
|
|
msgBeginRedelegate = NewMsgBeginRedelegate(sdk.AccAddress(validatorAddr), validatorAddr2, validatorAddr3, sdk.NewDec(10))
|
2018-06-26 19:00:12 -07:00
|
|
|
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
|
|
|
|
require.True(t, !got.IsOK(), "expected an error, msg: %v", msgBeginRedelegate)
|
|
|
|
|
|
|
|
// complete first redelegation
|
2018-10-07 21:43:47 -07:00
|
|
|
EndBlocker(ctx, keeper)
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// now should be able to redelegate from the second validator to the third
|
|
|
|
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error")
|
|
|
|
}
|
2018-06-29 20:34:55 -07:00
|
|
|
|
2019-01-16 02:35:18 -08:00
|
|
|
func TestMultipleRedelegationAtSameTime(t *testing.T) {
|
2018-10-15 18:06:31 -07:00
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
2019-01-16 02:35:18 -08:00
|
|
|
valAddr := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
valAddr2 := sdk.ValAddress(keep.Addrs[1])
|
2018-10-15 18:06:31 -07:00
|
|
|
|
|
|
|
// set the unbonding time
|
|
|
|
params := keeper.GetParams(ctx)
|
2019-01-16 02:35:18 -08:00
|
|
|
params.UnbondingTime = 1 * time.Second
|
2018-10-15 18:06:31 -07:00
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
|
|
|
|
// create the validators
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(valAddr, keep.PKs[0], valTokens)
|
2018-10-15 18:06:31 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator = NewTestMsgCreateValidator(valAddr2, keep.PKs[1], valTokens)
|
2018-10-15 18:06:31 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
|
|
|
// end block to bond them
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
2019-01-16 02:35:18 -08:00
|
|
|
// begin a redelegate
|
|
|
|
selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator)
|
|
|
|
msgBeginRedelegate := NewMsgBeginRedelegate(selfDelAddr,
|
2019-02-21 09:35:55 -08:00
|
|
|
valAddr, valAddr2, valTokens.QuoRaw(2).ToDec())
|
2018-10-15 18:06:31 -07:00
|
|
|
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error, %v", got)
|
|
|
|
|
2019-01-16 02:35:18 -08:00
|
|
|
// there should only be one entry in the redelegation object
|
|
|
|
rd, found := keeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Len(t, rd.Entries, 1)
|
|
|
|
|
|
|
|
// start a second redelegation at this same time as the first
|
2018-10-15 18:06:31 -07:00
|
|
|
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
|
2019-01-16 02:35:18 -08:00
|
|
|
require.True(t, got.IsOK(), "expected no error, msg: %v", msgBeginRedelegate)
|
2018-10-15 18:06:31 -07:00
|
|
|
|
2019-01-16 02:35:18 -08:00
|
|
|
// now there should be two entries
|
|
|
|
rd, found = keeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Len(t, rd.Entries, 2)
|
2018-10-15 18:06:31 -07:00
|
|
|
|
2019-01-16 02:35:18 -08:00
|
|
|
// move forward in time, should complete both redelegations
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(1 * time.Second))
|
2018-10-15 18:06:31 -07:00
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
2019-01-16 02:35:18 -08:00
|
|
|
rd, found = keeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2)
|
|
|
|
require.False(t, found)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleRedelegationAtUniqueTimes(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
|
|
|
valAddr := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
valAddr2 := sdk.ValAddress(keep.Addrs[1])
|
|
|
|
|
|
|
|
// set the unbonding time
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
params.UnbondingTime = 10 * time.Second
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
|
|
|
|
// create the validators
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(valAddr, keep.PKs[0], valTokens)
|
2019-01-16 02:35:18 -08:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator = NewTestMsgCreateValidator(valAddr2, keep.PKs[1], valTokens)
|
2019-01-16 02:35:18 -08:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
|
|
|
// end block to bond them
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
|
|
|
// begin a redelegate
|
|
|
|
selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator)
|
|
|
|
msgBeginRedelegate := NewMsgBeginRedelegate(selfDelAddr,
|
2019-02-21 09:35:55 -08:00
|
|
|
valAddr, valAddr2, valTokens.QuoRaw(2).ToDec())
|
2018-10-15 18:06:31 -07:00
|
|
|
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
|
2019-01-16 02:35:18 -08:00
|
|
|
require.True(t, got.IsOK(), "expected no error, %v", got)
|
|
|
|
|
|
|
|
// move forward in time and start a second redelegation
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
|
|
|
|
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error, msg: %v", msgBeginRedelegate)
|
|
|
|
|
|
|
|
// now there should be two entries
|
|
|
|
rd, found := keeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Len(t, rd.Entries, 2)
|
|
|
|
|
|
|
|
// move forward in time, should complete the first redelegation, but not the second
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
rd, found = keeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Len(t, rd.Entries, 1)
|
|
|
|
|
|
|
|
// move forward in time, should complete the second redelegation
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
rd, found = keeper.GetRedelegation(ctx, selfDelAddr, valAddr, valAddr2)
|
|
|
|
require.False(t, found)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
|
|
|
valAddr := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
|
|
|
|
// set the unbonding time
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
params.UnbondingTime = 1 * time.Second
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
|
|
|
|
// create the validator
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(valAddr, keep.PKs[0], valTokens)
|
2019-01-16 02:35:18 -08:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
|
|
|
// end block to bond
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
|
|
|
// begin an unbonding delegation
|
|
|
|
selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator)
|
2019-02-21 09:35:55 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(selfDelAddr, valAddr, valTokens.QuoRaw(2).ToDec())
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2019-01-16 02:35:18 -08:00
|
|
|
require.True(t, got.IsOK(), "expected no error, %v", got)
|
|
|
|
|
|
|
|
// there should only be one entry in the ubd object
|
|
|
|
ubd, found := keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Len(t, ubd.Entries, 1)
|
|
|
|
|
|
|
|
// start a second ubd at this same time as the first
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error, msg: %v", msgUndelegate)
|
2019-01-16 02:35:18 -08:00
|
|
|
|
|
|
|
// now there should be two entries
|
|
|
|
ubd, found = keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Len(t, ubd.Entries, 2)
|
|
|
|
|
|
|
|
// move forwaubd in time, should complete both ubds
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(1 * time.Second))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
|
|
|
ubd, found = keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
|
|
|
|
require.False(t, found)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
|
|
|
valAddr := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
|
|
|
|
// set the unbonding time
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
params.UnbondingTime = 10 * time.Second
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
|
|
|
|
// create the validator
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(valAddr, keep.PKs[0], valTokens)
|
2019-01-16 02:35:18 -08:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
|
|
|
// end block to bond
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
|
|
|
// begin an unbonding delegation
|
|
|
|
selfDelAddr := sdk.AccAddress(valAddr) // (the validator is it's own delegator)
|
2019-02-21 09:35:55 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(selfDelAddr, valAddr, valTokens.QuoRaw(2).ToDec())
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
2019-01-16 02:35:18 -08:00
|
|
|
require.True(t, got.IsOK(), "expected no error, %v", got)
|
|
|
|
|
|
|
|
// there should only be one entry in the ubd object
|
|
|
|
ubd, found := keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Len(t, ubd.Entries, 1)
|
|
|
|
|
|
|
|
// move forwaubd in time and start a second redelegation
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error, msg: %v", msgUndelegate)
|
2019-01-16 02:35:18 -08:00
|
|
|
|
|
|
|
// now there should be two entries
|
|
|
|
ubd, found = keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Len(t, ubd.Entries, 2)
|
|
|
|
|
|
|
|
// move forwaubd in time, should complete the first redelegation, but not the second
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
ubd, found = keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Len(t, ubd.Entries, 1)
|
|
|
|
|
|
|
|
// move forwaubd in time, should complete the second redelegation
|
|
|
|
ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(5 * time.Second))
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
ubd, found = keeper.GetUnbondingDelegation(ctx, selfDelAddr, valAddr)
|
|
|
|
require.False(t, found)
|
2018-10-15 18:06:31 -07:00
|
|
|
}
|
|
|
|
|
2018-07-06 15:41:04 -07:00
|
|
|
func TestUnbondingWhenExcessValidators(t *testing.T) {
|
2018-07-03 15:08:37 -07:00
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr1 := sdk.ValAddress(keep.Addrs[0])
|
|
|
|
validatorAddr2 := sdk.ValAddress(keep.Addrs[1])
|
|
|
|
validatorAddr3 := sdk.ValAddress(keep.Addrs[2])
|
2018-07-03 15:08:37 -07:00
|
|
|
|
2018-07-06 15:41:04 -07:00
|
|
|
// set the unbonding time
|
|
|
|
params := keeper.GetParams(ctx)
|
|
|
|
params.UnbondingTime = 0
|
|
|
|
params.MaxValidators = 2
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
|
|
|
|
// add three validators
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens1 := sdk.TokensFromTendermintPower(50)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr1, keep.PKs[0], valTokens1)
|
2018-07-06 15:41:04 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
2018-10-03 09:37:06 -07:00
|
|
|
// apply TM updates
|
|
|
|
keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
2018-10-21 15:26:58 -07:00
|
|
|
require.Equal(t, 1, len(keeper.GetLastValidators(ctx)))
|
2018-07-06 15:41:04 -07:00
|
|
|
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens2 := sdk.TokensFromTendermintPower(30)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator = NewTestMsgCreateValidator(validatorAddr2, keep.PKs[1], valTokens2)
|
2018-07-06 15:41:04 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
2018-10-03 09:37:06 -07:00
|
|
|
// apply TM updates
|
|
|
|
keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
2018-10-21 15:26:58 -07:00
|
|
|
require.Equal(t, 2, len(keeper.GetLastValidators(ctx)))
|
2018-07-06 15:41:04 -07:00
|
|
|
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens3 := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator = NewTestMsgCreateValidator(validatorAddr3, keep.PKs[2], valTokens3)
|
2018-07-06 15:41:04 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
2018-10-03 09:37:06 -07:00
|
|
|
// apply TM updates
|
|
|
|
keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
2018-10-21 15:26:58 -07:00
|
|
|
require.Equal(t, 2, len(keeper.GetLastValidators(ctx)))
|
2018-07-06 15:41:04 -07:00
|
|
|
|
|
|
|
// unbond the valdator-2
|
2019-02-21 00:05:31 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(sdk.AccAddress(validatorAddr2), validatorAddr2, valTokens2.ToDec())
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgUndelegate")
|
2018-07-06 16:53:07 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// apply TM updates
|
|
|
|
keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
|
2018-07-06 16:53:07 -07:00
|
|
|
// because there are extra validators waiting to get in, the queued
|
|
|
|
// validator (aka. validator-1) should make it into the bonded group, thus
|
|
|
|
// the total number of validators should stay the same
|
2018-10-21 15:26:58 -07:00
|
|
|
vals := keeper.GetLastValidators(ctx)
|
2018-07-06 17:37:59 -07:00
|
|
|
require.Equal(t, 2, len(vals), "vals %v", vals)
|
2018-07-06 16:53:07 -07:00
|
|
|
val1, found := keeper.GetValidator(ctx, validatorAddr1)
|
|
|
|
require.True(t, found)
|
2018-07-13 13:46:14 -07:00
|
|
|
require.Equal(t, sdk.Bonded, val1.Status, "%v", val1)
|
2018-07-06 15:41:04 -07:00
|
|
|
}
|
|
|
|
|
2018-06-29 20:34:55 -07:00
|
|
|
func TestBondUnbondRedelegateSlashTwice(t *testing.T) {
|
|
|
|
ctx, _, keeper := keep.CreateTestInput(t, false, 1000)
|
2018-08-30 21:06:44 -07:00
|
|
|
valA, valB, del := sdk.ValAddress(keep.Addrs[0]), sdk.ValAddress(keep.Addrs[1]), keep.Addrs[2]
|
2018-09-24 21:09:31 -07:00
|
|
|
consAddr0 := sdk.ConsAddress(keep.PKs[0].Address())
|
2018-06-29 20:34:55 -07:00
|
|
|
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(10)
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator := NewTestMsgCreateValidator(valA, keep.PKs[0], valTokens)
|
2018-06-29 20:34:55 -07:00
|
|
|
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
msgCreateValidator = NewTestMsgCreateValidator(valB, keep.PKs[1], valTokens)
|
2018-06-29 20:34:55 -07:00
|
|
|
got = handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
|
|
|
|
|
|
|
|
// delegate 10 stake
|
2019-02-05 21:30:48 -08:00
|
|
|
msgDelegate := NewTestMsgDelegate(del, valA, valTokens)
|
2018-06-29 20:34:55 -07:00
|
|
|
got = handleMsgDelegate(ctx, msgDelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgDelegate")
|
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// apply Tendermint updates
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 2, len(updates))
|
|
|
|
|
2018-06-29 20:34:55 -07:00
|
|
|
// a block passes
|
|
|
|
ctx = ctx.WithBlockHeight(1)
|
|
|
|
|
|
|
|
// begin unbonding 4 stake
|
2019-02-13 15:01:50 -08:00
|
|
|
ubdTokens := sdk.TokensFromTendermintPower(4)
|
2019-02-21 00:05:31 -08:00
|
|
|
msgUndelegate := NewMsgUndelegate(del, valA, ubdTokens.ToDec())
|
2019-01-17 09:53:22 -08:00
|
|
|
got = handleMsgUndelegate(ctx, msgUndelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgUndelegate")
|
2018-06-29 20:34:55 -07:00
|
|
|
|
|
|
|
// begin redelegate 6 stake
|
2019-02-13 15:01:50 -08:00
|
|
|
rdTokens := sdk.TokensFromTendermintPower(6)
|
2019-02-21 00:05:31 -08:00
|
|
|
msgBeginRedelegate := NewMsgBeginRedelegate(del, valA, valB, rdTokens.ToDec())
|
2018-06-29 20:34:55 -07:00
|
|
|
got = handleMsgBeginRedelegate(ctx, msgBeginRedelegate, keeper)
|
|
|
|
require.True(t, got.IsOK(), "expected no error on runMsgBeginRedelegate")
|
|
|
|
|
|
|
|
// destination delegation should have 6 shares
|
|
|
|
delegation, found := keeper.GetDelegation(ctx, del, valB)
|
|
|
|
require.True(t, found)
|
2019-02-21 00:05:31 -08:00
|
|
|
require.Equal(t, rdTokens.ToDec(), delegation.Shares)
|
2018-06-29 20:34:55 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// must apply validator updates
|
|
|
|
updates = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 2, len(updates))
|
|
|
|
|
2018-06-29 20:34:55 -07:00
|
|
|
// slash the validator by half
|
2018-09-24 21:09:31 -07:00
|
|
|
keeper.Slash(ctx, consAddr0, 0, 20, sdk.NewDecWithPrec(5, 1))
|
2018-06-29 20:34:55 -07:00
|
|
|
|
|
|
|
// unbonding delegation should have been slashed by half
|
2019-01-16 02:35:18 -08:00
|
|
|
ubd, found := keeper.GetUnbondingDelegation(ctx, del, valA)
|
2018-06-29 20:34:55 -07:00
|
|
|
require.True(t, found)
|
2019-01-16 02:35:18 -08:00
|
|
|
require.Len(t, ubd.Entries, 1)
|
2019-02-21 09:35:55 -08:00
|
|
|
require.Equal(t, ubdTokens.QuoRaw(2), ubd.Entries[0].Balance)
|
2018-06-29 20:34:55 -07:00
|
|
|
|
|
|
|
// redelegation should have been slashed by half
|
|
|
|
redelegation, found := keeper.GetRedelegation(ctx, del, valA, valB)
|
|
|
|
require.True(t, found)
|
2019-01-16 02:35:18 -08:00
|
|
|
require.Len(t, redelegation.Entries, 1)
|
2018-06-29 20:34:55 -07:00
|
|
|
|
|
|
|
// destination delegation should have been slashed by half
|
|
|
|
delegation, found = keeper.GetDelegation(ctx, del, valB)
|
|
|
|
require.True(t, found)
|
2019-02-21 09:35:55 -08:00
|
|
|
require.Equal(t, rdTokens.QuoRaw(2).ToDec(), delegation.Shares)
|
2018-06-29 20:34:55 -07:00
|
|
|
|
|
|
|
// validator power should have been reduced by half
|
|
|
|
validator, found := keeper.GetValidator(ctx, valA)
|
|
|
|
require.True(t, found)
|
2019-02-21 09:35:55 -08:00
|
|
|
require.Equal(t, valTokens.QuoRaw(2), validator.GetBondedTokens())
|
2018-06-29 20:34:55 -07:00
|
|
|
|
|
|
|
// slash the validator for an infraction committed after the unbonding and redelegation begin
|
|
|
|
ctx = ctx.WithBlockHeight(3)
|
2018-09-24 21:09:31 -07:00
|
|
|
keeper.Slash(ctx, consAddr0, 2, 10, sdk.NewDecWithPrec(5, 1))
|
2018-06-29 20:34:55 -07:00
|
|
|
|
|
|
|
// unbonding delegation should be unchanged
|
2019-01-16 02:35:18 -08:00
|
|
|
ubd, found = keeper.GetUnbondingDelegation(ctx, del, valA)
|
2018-06-29 20:34:55 -07:00
|
|
|
require.True(t, found)
|
2019-01-16 02:35:18 -08:00
|
|
|
require.Len(t, ubd.Entries, 1)
|
2019-02-21 09:35:55 -08:00
|
|
|
require.Equal(t, ubdTokens.QuoRaw(2), ubd.Entries[0].Balance)
|
2018-06-29 20:34:55 -07:00
|
|
|
|
|
|
|
// redelegation should be unchanged
|
|
|
|
redelegation, found = keeper.GetRedelegation(ctx, del, valA, valB)
|
|
|
|
require.True(t, found)
|
2019-01-16 02:35:18 -08:00
|
|
|
require.Len(t, redelegation.Entries, 1)
|
2018-06-29 20:34:55 -07:00
|
|
|
|
|
|
|
// destination delegation should be unchanged
|
|
|
|
delegation, found = keeper.GetDelegation(ctx, del, valB)
|
|
|
|
require.True(t, found)
|
2019-02-21 09:35:55 -08:00
|
|
|
require.Equal(t, rdTokens.QuoRaw(2).ToDec(), delegation.Shares)
|
2018-06-29 20:34:55 -07:00
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// end blocker
|
|
|
|
EndBlocker(ctx, keeper)
|
|
|
|
|
2018-06-29 20:34:55 -07:00
|
|
|
// validator power should have been reduced to zero
|
2018-11-04 22:11:03 -08:00
|
|
|
// validator should be in unbonding state
|
|
|
|
validator, _ = keeper.GetValidator(ctx, valA)
|
|
|
|
require.Equal(t, validator.GetStatus(), sdk.Unbonding)
|
2018-06-29 20:34:55 -07:00
|
|
|
}
|