2020-02-14 07:30:51 -08:00
|
|
|
package staking_test
|
2018-06-07 20:55:14 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-11-12 10:54:18 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2018-11-12 10:54:18 -08:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2018-06-07 20:55:14 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-06-17 11:42:27 -07:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-06-14 16:06:16 -07:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2020-06-12 07:54:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-06-07 20:55:14 -07:00
|
|
|
)
|
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
func checkValidator(t *testing.T, app *simapp.SimApp, addr sdk.ValAddress, expFound bool) types.Validator {
|
2020-08-14 10:58:53 -07:00
|
|
|
ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{})
|
2020-02-14 07:30:51 -08:00
|
|
|
validator, found := app.StakingKeeper.GetValidator(ctxCheck, addr)
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, expFound, found)
|
2018-06-08 11:28:07 -07:00
|
|
|
return validator
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
func checkDelegation(
|
2020-02-14 07:30:51 -08:00
|
|
|
t *testing.T, app *simapp.SimApp, delegatorAddr sdk.AccAddress,
|
2018-08-30 21:06:44 -07:00
|
|
|
validatorAddr sdk.ValAddress, expFound bool, expShares sdk.Dec,
|
2018-07-03 21:21:36 -07:00
|
|
|
) {
|
2018-07-06 00:06:53 -07:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{})
|
2020-02-14 07:30:51 -08:00
|
|
|
delegation, found := app.StakingKeeper.GetDelegation(ctxCheck, delegatorAddr, validatorAddr)
|
2018-06-08 11:28:07 -07:00
|
|
|
if expFound {
|
2018-06-29 18:10:15 -07:00
|
|
|
require.True(t, found)
|
2018-08-14 17:15:02 -07:00
|
|
|
require.True(sdk.DecEq(t, expShares, delegation.Shares))
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-06-08 11:28:07 -07:00
|
|
|
return
|
|
|
|
}
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2018-06-29 18:10:15 -07:00
|
|
|
require.False(t, found)
|
2018-06-08 11:28:07 -07:00
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
func TestStakingMsgs(t *testing.T) {
|
2022-03-24 04:26:25 -07:00
|
|
|
genTokens := sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction)
|
|
|
|
bondTokens := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)
|
2019-02-13 15:01:50 -08:00
|
|
|
genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens)
|
|
|
|
bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens)
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
acc1 := &authtypes.BaseAccount{Address: addr1.String()}
|
|
|
|
acc2 := &authtypes.BaseAccount{Address: addr2.String()}
|
2020-06-17 11:42:27 -07:00
|
|
|
accs := authtypes.GenesisAccounts{acc1, acc2}
|
2020-06-14 16:06:16 -07:00
|
|
|
balances := []banktypes.Balance{
|
2020-02-16 11:06:45 -08:00
|
|
|
{
|
2020-09-25 03:25:37 -07:00
|
|
|
Address: addr1.String(),
|
2020-01-30 13:31:16 -08:00
|
|
|
Coins: sdk.Coins{genCoin},
|
|
|
|
},
|
2020-02-16 11:06:45 -08:00
|
|
|
{
|
2020-09-25 03:25:37 -07:00
|
|
|
Address: addr2.String(),
|
2020-01-30 13:31:16 -08:00
|
|
|
Coins: sdk.Coins{genCoin},
|
|
|
|
},
|
|
|
|
}
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2021-09-15 14:18:01 -07:00
|
|
|
app := simapp.SetupWithGenesisAccounts(t, accs, balances...)
|
2020-02-14 07:30:51 -08:00
|
|
|
simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin})
|
|
|
|
simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin})
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// create validator
|
2020-06-12 07:54:37 -07:00
|
|
|
description := types.NewDescription("foo_moniker", "", "", "", "")
|
2020-10-19 06:04:55 -07:00
|
|
|
createValidatorMsg, err := types.NewMsgCreateValidator(
|
2020-08-14 10:58:53 -07:00
|
|
|
sdk.ValAddress(addr1), valKey.PubKey(), bondCoin, description, commissionRates, sdk.OneInt(),
|
2018-06-07 20:55:14 -07:00
|
|
|
)
|
2020-10-19 06:04:55 -07:00
|
|
|
require.NoError(t, err)
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header := tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-10-27 04:33:48 -07:00
|
|
|
txGen := simapp.MakeTestEncodingConfig().TxConfig
|
2020-10-19 06:04:55 -07:00
|
|
|
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{createValidatorMsg}, "", []uint64{0}, []uint64{0}, true, true, priv1)
|
2020-03-20 12:24:18 -07:00
|
|
|
require.NoError(t, err)
|
2020-02-14 07:30:51 -08:00
|
|
|
simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin.Sub(bondCoin)})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header = tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-02-14 07:30:51 -08:00
|
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2018-07-03 21:21:36 -07:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
validator := checkValidator(t, app, sdk.ValAddress(addr1), true)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress)
|
2020-10-12 06:56:02 -07:00
|
|
|
require.Equal(t, types.Bonded, validator.Status)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.True(sdk.IntEq(t, bondTokens, validator.BondedTokens()))
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header = tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-02-14 07:30:51 -08:00
|
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2018-07-10 17:16:37 -07:00
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// edit the validator
|
2020-06-12 07:54:37 -07:00
|
|
|
description = types.NewDescription("bar_moniker", "", "", "", "")
|
|
|
|
editValidatorMsg := types.NewMsgEditValidator(sdk.ValAddress(addr1), description, nil, nil)
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header = tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-08-20 15:05:41 -07:00
|
|
|
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{editValidatorMsg}, "", []uint64{0}, []uint64{1}, true, true, priv1)
|
2020-03-20 12:24:18 -07:00
|
|
|
require.NoError(t, err)
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
validator = checkValidator(t, app, sdk.ValAddress(addr1), true)
|
2018-07-03 21:21:36 -07:00
|
|
|
require.Equal(t, description, validator.Description)
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// delegate
|
2020-02-14 07:30:51 -08:00
|
|
|
simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin})
|
2020-06-12 07:54:37 -07:00
|
|
|
delegateMsg := types.NewMsgDelegate(addr2, sdk.ValAddress(addr1), bondCoin)
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header = tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-08-20 15:05:41 -07:00
|
|
|
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{delegateMsg}, "", []uint64{1}, []uint64{0}, true, true, priv2)
|
2020-03-20 12:24:18 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin.Sub(bondCoin)})
|
|
|
|
checkDelegation(t, app, addr2, sdk.ValAddress(addr1), true, bondTokens.ToDec())
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2018-07-03 21:21:36 -07:00
|
|
|
// begin unbonding
|
2020-06-12 07:54:37 -07:00
|
|
|
beginUnbondingMsg := types.NewMsgUndelegate(addr2, sdk.ValAddress(addr1), bondCoin)
|
2020-08-14 10:58:53 -07:00
|
|
|
header = tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-08-20 15:05:41 -07:00
|
|
|
_, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, "", []uint64{1}, []uint64{1}, true, true, priv2)
|
2020-03-20 12:24:18 -07:00
|
|
|
require.NoError(t, err)
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// delegation should exist anymore
|
2020-02-14 07:30:51 -08:00
|
|
|
checkDelegation(t, app, addr2, sdk.ValAddress(addr1), false, sdk.Dec{})
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
// balance should be the same because bonding not yet complete
|
2020-02-14 07:30:51 -08:00
|
|
|
simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin.Sub(bondCoin)})
|
2018-06-07 20:55:14 -07:00
|
|
|
}
|