cosmos-sdk/x/staking/app_test.go

117 lines
4.3 KiB
Go
Raw Normal View History

2020-02-14 07:30:51 -08:00
package staking_test
2018-06-07 20:55:14 -07:00
import (
"testing"
"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"
"github.com/cosmos/cosmos-sdk/x/auth"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
2018-06-07 20:55:14 -07:00
"github.com/cosmos/cosmos-sdk/x/bank"
2020-02-14 07:30:51 -08:00
"github.com/cosmos/cosmos-sdk/x/staking"
2018-06-07 20:55:14 -07:00
)
2020-02-14 07:30:51 -08:00
func checkValidator(t *testing.T, app *simapp.SimApp, addr sdk.ValAddress, expFound bool) staking.Validator {
ctxCheck := app.BaseApp.NewContext(true, abci.Header{})
validator, found := app.StakingKeeper.GetValidator(ctxCheck, addr)
require.Equal(t, expFound, found)
return validator
}
func checkDelegation(
2020-02-14 07:30:51 -08:00
t *testing.T, app *simapp.SimApp, delegatorAddr sdk.AccAddress,
validatorAddr sdk.ValAddress, expFound bool, expShares sdk.Dec,
) {
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)
if expFound {
require.True(t, found)
require.True(sdk.DecEq(t, expShares, delegation.Shares))
return
}
require.False(t, found)
}
2019-01-11 12:08:01 -08:00
func TestStakingMsgs(t *testing.T) {
genTokens := sdk.TokensFromConsensusPower(42)
bondTokens := sdk.TokensFromConsensusPower(10)
genCoin := sdk.NewCoin(sdk.DefaultBondDenom, genTokens)
bondCoin := sdk.NewCoin(sdk.DefaultBondDenom, bondTokens)
2018-06-07 20:55:14 -07:00
2020-01-30 13:31:16 -08:00
acc1 := &auth.BaseAccount{Address: addr1}
acc2 := &auth.BaseAccount{Address: addr2}
2020-02-14 07:30:51 -08:00
accs := authexported.GenesisAccounts{acc1, acc2}
balances := []bank.Balance{
{
2020-01-30 13:31:16 -08:00
Address: addr1,
Coins: sdk.Coins{genCoin},
},
{
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
// create validator
2020-02-14 07:30:51 -08:00
description := staking.NewDescription("foo_moniker", "", "", "", "")
createValidatorMsg := staking.NewMsgCreateValidator(
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}
simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{createValidatorMsg}, []uint64{0}, []uint64{0}, true, true, priv1)
simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin.Sub(bondCoin)})
2020-02-14 07:30:51 -08:00
header = abci.Header{Height: app.LastBlockHeight() + 1}
app.BeginBlock(abci.RequestBeginBlock{Header: header})
2020-02-14 07:30:51 -08:00
validator := checkValidator(t, app, sdk.ValAddress(addr1), true)
require.Equal(t, sdk.ValAddress(addr1), validator.OperatorAddress)
require.Equal(t, sdk.Bonded, validator.Status)
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})
// edit the validator
2020-02-14 07:30:51 -08:00
description = staking.NewDescription("bar_moniker", "", "", "", "")
editValidatorMsg := staking.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}
simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{editValidatorMsg}, []uint64{0}, []uint64{1}, true, true, priv1)
2020-02-14 07:30:51 -08:00
validator = checkValidator(t, app, sdk.ValAddress(addr1), true)
require.Equal(t, description, validator.Description)
2018-06-07 20:55:14 -07:00
// delegate
2020-02-14 07:30:51 -08:00
simapp.CheckBalance(t, app, addr2, sdk.Coins{genCoin})
delegateMsg := staking.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}
simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{delegateMsg}, []uint64{1}, []uint64{0}, true, true, priv2)
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
// begin unbonding
2020-02-14 07:30:51 -08:00
beginUnbondingMsg := staking.NewMsgUndelegate(addr2, sdk.ValAddress(addr1), bondCoin)
header = abci.Header{Height: app.LastBlockHeight() + 1}
simapp.SignCheckDeliver(t, app.Codec(), app.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, []uint64{1}, []uint64{1}, true, true, priv2)
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
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{})
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
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
}