From 62dbd8bcee9d53d49449432e4a059db52c2ae41e Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Mon, 24 Feb 2020 12:46:14 +0100 Subject: [PATCH] TestUnbondingAllDelegationFromValidator to use simapp --- x/staking/keeper/delegation_test.go | 147 ++++++++++++------------ x/staking/keeper/old_delegation_test.go | 72 ------------ 2 files changed, 75 insertions(+), 144 deletions(-) diff --git a/x/staking/keeper/delegation_test.go b/x/staking/keeper/delegation_test.go index 63e8e8fd72..3250b0c219 100644 --- a/x/staking/keeper/delegation_test.go +++ b/x/staking/keeper/delegation_test.go @@ -547,78 +547,81 @@ func TestUndelegateFromUnbondedValidator(t *testing.T) { require.False(t, found, "%v", validator) } -//func TestUnbondingAllDelegationFromValidator(t *testing.T) { -// ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) -// delTokens := sdk.TokensFromConsensusPower(10) -// delCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), delTokens)) -// -// // add bonded tokens to pool for delegations -// notBondedPool := keeper.GetNotBondedPool(ctx) -// oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) -// err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(delCoins...)) -// require.NoError(t, err) -// keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) -// -// //create a validator with a self-delegation -// validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) -// -// valTokens := sdk.TokensFromConsensusPower(10) -// validator, issuedShares := validator.AddTokensFromDel(valTokens) -// require.Equal(t, valTokens, issuedShares.RoundInt()) -// -// validator = TestingUpdateValidator(keeper, ctx, validator, true) -// require.True(t, validator.IsBonded()) -// val0AccAddr := sdk.AccAddress(addrVals[0].Bytes()) -// -// selfDelegation := types.NewDelegation(val0AccAddr, addrVals[0], issuedShares) -// keeper.SetDelegation(ctx, selfDelegation) -// -// // create a second delegation to this validator -// keeper.DeleteValidatorByPowerIndex(ctx, validator) -// validator, issuedShares = validator.AddTokensFromDel(delTokens) -// require.Equal(t, delTokens, issuedShares.RoundInt()) -// -// bondedPool := keeper.GetBondedPool(ctx) -// oldBonded := bk.GetAllBalances(ctx, bondedPool.GetAddress()) -// err = bk.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) -// require.NoError(t, err) -// keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) -// -// validator = TestingUpdateValidator(keeper, ctx, validator, true) -// require.True(t, validator.IsBonded()) -// -// delegation := types.NewDelegation(addrDels[0], addrVals[0], issuedShares) -// keeper.SetDelegation(ctx, delegation) -// -// ctx = ctx.WithBlockHeight(10) -// ctx = ctx.WithBlockTime(time.Unix(333, 0)) -// -// // unbond the all self-delegation to put validator in unbonding state -// _, err = keeper.Undelegate(ctx, val0AccAddr, addrVals[0], valTokens.ToDec()) -// require.NoError(t, err) -// -// // end block -// updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx) -// require.Equal(t, 1, len(updates)) -// -// // unbond all the remaining delegation -// _, err = keeper.Undelegate(ctx, addrDels[0], addrVals[0], delTokens.ToDec()) -// require.NoError(t, err) -// -// // validator should still be in state and still be in unbonding state -// validator, found := keeper.GetValidator(ctx, addrVals[0]) -// require.True(t, found) -// require.Equal(t, validator.Status, sdk.Unbonding) -// -// // unbond the validator -// ctx = ctx.WithBlockTime(validator.UnbondingTime) -// keeper.UnbondAllMatureValidatorQueue(ctx) -// -// // validator should now be deleted from state -// _, found = keeper.GetValidator(ctx, addrVals[0]) -// require.False(t, found) -//} -// +func TestUnbondingAllDelegationFromValidator(t *testing.T) { + _, app, ctx := getBaseSimappWithCustomKeeper() + delTokens := sdk.TokensFromConsensusPower(10) + delCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), delTokens)) + + addrDels := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.NewInt(0)) + addrVals := simapp.ConvertAddrsToValAddrs(addrDels) + + // add bonded tokens to pool for delegations + notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx) + oldNotBonded := app.BankKeeper.GetAllBalances(ctx, notBondedPool.GetAddress()) + err := app.BankKeeper.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(delCoins...)) + require.NoError(t, err) + app.SupplyKeeper.SetModuleAccount(ctx, notBondedPool) + + //create a validator with a self-delegation + validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) + + valTokens := sdk.TokensFromConsensusPower(10) + validator, issuedShares := validator.AddTokensFromDel(valTokens) + require.Equal(t, valTokens, issuedShares.RoundInt()) + + validator = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validator, true) + require.True(t, validator.IsBonded()) + val0AccAddr := sdk.AccAddress(addrVals[0].Bytes()) + + selfDelegation := types.NewDelegation(val0AccAddr, addrVals[0], issuedShares) + app.StakingKeeper.SetDelegation(ctx, selfDelegation) + + // create a second delegation to this validator + app.StakingKeeper.DeleteValidatorByPowerIndex(ctx, validator) + validator, issuedShares = validator.AddTokensFromDel(delTokens) + require.Equal(t, delTokens, issuedShares.RoundInt()) + + bondedPool := app.StakingKeeper.GetBondedPool(ctx) + oldBonded := app.BankKeeper.GetAllBalances(ctx, bondedPool.GetAddress()) + err = app.BankKeeper.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) + require.NoError(t, err) + app.SupplyKeeper.SetModuleAccount(ctx, bondedPool) + + validator = keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validator, true) + require.True(t, validator.IsBonded()) + + delegation := types.NewDelegation(addrDels[1], addrVals[0], issuedShares) + app.StakingKeeper.SetDelegation(ctx, delegation) + + ctx = ctx.WithBlockHeight(10) + ctx = ctx.WithBlockTime(time.Unix(333, 0)) + + // unbond the all self-delegation to put validator in unbonding state + _, err = app.StakingKeeper.Undelegate(ctx, val0AccAddr, addrVals[0], valTokens.ToDec()) + require.NoError(t, err) + + // end block + updates := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) + require.Equal(t, 1, len(updates)) + + // unbond all the remaining delegation + _, err = app.StakingKeeper.Undelegate(ctx, addrDels[1], addrVals[0], delTokens.ToDec()) + require.NoError(t, err) + + // validator should still be in state and still be in unbonding state + validator, found := app.StakingKeeper.GetValidator(ctx, addrVals[0]) + require.True(t, found) + require.Equal(t, validator.Status, sdk.Unbonding) + + // unbond the validator + ctx = ctx.WithBlockTime(validator.UnbondingTime) + app.StakingKeeper.UnbondAllMatureValidatorQueue(ctx) + + // validator should now be deleted from state + _, found = app.StakingKeeper.GetValidator(ctx, addrVals[0]) + require.False(t, found) +} + //// Make sure that that the retrieving the delegations doesn't affect the state //func TestGetRedelegationsFromSrcValidator(t *testing.T) { // ctx, _, _, keeper, _ := CreateTestInput(t, false, 0) diff --git a/x/staking/keeper/old_delegation_test.go b/x/staking/keeper/old_delegation_test.go index 7b31fc5cb9..03ec0f233c 100644 --- a/x/staking/keeper/old_delegation_test.go +++ b/x/staking/keeper/old_delegation_test.go @@ -11,78 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -func TestUnbondingAllDelegationFromValidator(t *testing.T) { - ctx, _, bk, keeper, _ := CreateTestInput(t, false, 0) - delTokens := sdk.TokensFromConsensusPower(10) - delCoins := sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), delTokens)) - - // add bonded tokens to pool for delegations - notBondedPool := keeper.GetNotBondedPool(ctx) - oldNotBonded := bk.GetAllBalances(ctx, notBondedPool.GetAddress()) - err := bk.SetBalances(ctx, notBondedPool.GetAddress(), oldNotBonded.Add(delCoins...)) - require.NoError(t, err) - keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool) - - //create a validator with a self-delegation - validator := types.NewValidator(addrVals[0], PKs[0], types.Description{}) - - valTokens := sdk.TokensFromConsensusPower(10) - validator, issuedShares := validator.AddTokensFromDel(valTokens) - require.Equal(t, valTokens, issuedShares.RoundInt()) - - validator = TestingUpdateValidator(keeper, ctx, validator, true) - require.True(t, validator.IsBonded()) - val0AccAddr := sdk.AccAddress(addrVals[0].Bytes()) - - selfDelegation := types.NewDelegation(val0AccAddr, addrVals[0], issuedShares) - keeper.SetDelegation(ctx, selfDelegation) - - // create a second delegation to this validator - keeper.DeleteValidatorByPowerIndex(ctx, validator) - validator, issuedShares = validator.AddTokensFromDel(delTokens) - require.Equal(t, delTokens, issuedShares.RoundInt()) - - bondedPool := keeper.GetBondedPool(ctx) - oldBonded := bk.GetAllBalances(ctx, bondedPool.GetAddress()) - err = bk.SetBalances(ctx, bondedPool.GetAddress(), oldBonded.Add(delCoins...)) - require.NoError(t, err) - keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool) - - validator = TestingUpdateValidator(keeper, ctx, validator, true) - require.True(t, validator.IsBonded()) - - delegation := types.NewDelegation(addrDels[0], addrVals[0], issuedShares) - keeper.SetDelegation(ctx, delegation) - - ctx = ctx.WithBlockHeight(10) - ctx = ctx.WithBlockTime(time.Unix(333, 0)) - - // unbond the all self-delegation to put validator in unbonding state - _, err = keeper.Undelegate(ctx, val0AccAddr, addrVals[0], valTokens.ToDec()) - require.NoError(t, err) - - // end block - updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx) - require.Equal(t, 1, len(updates)) - - // unbond all the remaining delegation - _, err = keeper.Undelegate(ctx, addrDels[0], addrVals[0], delTokens.ToDec()) - require.NoError(t, err) - - // validator should still be in state and still be in unbonding state - validator, found := keeper.GetValidator(ctx, addrVals[0]) - require.True(t, found) - require.Equal(t, validator.Status, sdk.Unbonding) - - // unbond the validator - ctx = ctx.WithBlockTime(validator.UnbondingTime) - keeper.UnbondAllMatureValidatorQueue(ctx) - - // validator should now be deleted from state - _, found = keeper.GetValidator(ctx, addrVals[0]) - require.False(t, found) -} - // Make sure that that the retrieving the delegations doesn't affect the state func TestGetRedelegationsFromSrcValidator(t *testing.T) { ctx, _, _, keeper, _ := CreateTestInput(t, false, 0)