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-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-02-14 07:30:51 -08:00
|
|
|
ctxCheck := app.BaseApp.NewContext(true, abci.Header{})
|
|
|
|
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-02-14 07:30:51 -08:00
|
|
|
ctxCheck := app.BaseApp.NewContext(true, abci.Header{})
|
|
|
|
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) {
|
2019-06-12 08:57:47 -07:00
|
|
|
genTokens := sdk.TokensFromConsensusPower(42)
|
|
|
|
bondTokens := sdk.TokensFromConsensusPower(10)
|
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-06-17 11:42:27 -07:00
|
|
|
acc1 := &authtypes.BaseAccount{Address: addr1}
|
|
|
|
acc2 := &authtypes.BaseAccount{Address: addr2}
|
|
|
|
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-01-30 13:31:16 -08:00
|
|
|
Address: addr1,
|
|
|
|
Coins: sdk.Coins{genCoin},
|
|
|
|
},
|
2020-02-16 11:06:45 -08:00
|
|
|
{
|
2020-01-30 13:31:16 -08:00
|
|
|
Address: addr2,
|
|
|
|
Coins: sdk.Coins{genCoin},
|
|
|
|
},
|
|
|
|
}
|
2018-06-07 20:55:14 -07:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
app := simapp.SetupWithGenesisAccounts(accs, balances...)
|
|
|
|
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", "", "", "", "")
|
|
|
|
createValidatorMsg := types.NewMsgCreateValidator(
|
2019-06-03 14:14:21 -07:00
|
|
|
sdk.ValAddress(addr1), priv1.PubKey(), bondCoin, description, commissionRates, sdk.OneInt(),
|
2018-06-07 20:55:14 -07:00
|
|
|
)
|
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
header := abci.Header{Height: app.LastBlockHeight() + 1}
|
2020-07-20 05:30:12 -07:00
|
|
|
txGen := simapp.MakeEncodingConfig().TxConfig
|
2020-07-14 10:33:31 -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-02-14 07:30:51 -08:00
|
|
|
header = abci.Header{Height: app.LastBlockHeight() + 1}
|
|
|
|
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)
|
2019-02-25 07:16:52 -08:00
|
|
|
require.Equal(t, sdk.ValAddress(addr1), validator.OperatorAddress)
|
2018-07-13 13:46:14 -07:00
|
|
|
require.Equal(t, sdk.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-02-14 07:30:51 -08:00
|
|
|
header = abci.Header{Height: app.LastBlockHeight() + 1}
|
|
|
|
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-02-14 07:30:51 -08:00
|
|
|
header = abci.Header{Height: app.LastBlockHeight() + 1}
|
2020-07-14 10:33:31 -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-02-14 07:30:51 -08:00
|
|
|
header = abci.Header{Height: app.LastBlockHeight() + 1}
|
2020-07-14 10:33:31 -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-02-14 07:30:51 -08:00
|
|
|
header = abci.Header{Height: app.LastBlockHeight() + 1}
|
2020-07-14 10:33:31 -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
|
|
|
}
|