test TestSlashAtNegativeHeight migrated to simapp

This commit is contained in:
Jonathan Gimeno 2020-02-24 19:01:55 +01:00
parent 7ee50eacf2
commit b36cdac3e3
2 changed files with 37 additions and 32 deletions

View File

@ -37,38 +37,6 @@ func setupHelper(t *testing.T, power int64) (sdk.Context, Keeper, types.Params)
//_________________________________________________________________________________
// test slash at a negative height
// this just represents pre-genesis and should have the same effect as slashing at height 0
func TestSlashAtNegativeHeight(t *testing.T) {
ctx, keeper, _ := setupHelper(t, 10)
consAddr := sdk.ConsAddress(PKs[0].Address())
fraction := sdk.NewDecWithPrec(5, 1)
bondedPool := keeper.GetBondedPool(ctx)
oldBondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress())
validator, found := keeper.GetValidatorByConsAddr(ctx, consAddr)
require.True(t, found)
keeper.Slash(ctx, consAddr, -2, 10, fraction)
// read updated state
validator, found = keeper.GetValidatorByConsAddr(ctx, consAddr)
require.True(t, found)
// end block
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
require.Equal(t, 1, len(updates), "cons addr: %v, updates: %v", []byte(consAddr), updates)
validator = keeper.mustGetValidator(ctx, validator.OperatorAddress)
// power decreased
require.Equal(t, int64(5), validator.GetConsensusPower())
// pool bonded shares decreased
newBondedPoolBalances := keeper.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress())
diffTokens := oldBondedPoolBalances.Sub(newBondedPoolBalances).AmountOf(keeper.BondDenom(ctx))
require.Equal(t, sdk.TokensFromConsensusPower(5).String(), diffTokens.String())
}
// tests Slash at the current height
func TestSlashValidatorAtCurrentHeight(t *testing.T) {
ctx, keeper, _ := setupHelper(t, 10)

View File

@ -4,6 +4,8 @@ import (
"testing"
"time"
"github.com/cosmos/cosmos-sdk/x/supply"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/simapp"
@ -35,6 +37,8 @@ func initConfig(t *testing.T, power int64) (*simapp.SimApp, sdk.Context, []sdk.A
require.NoError(t, err)
app.SupplyKeeper.SetModuleAccount(ctx, bondedPool)
app.SupplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply))
for i := int64(0); i < numVals; i++ {
validator := types.NewValidator(addrVals[i], PKs[i], types.Description{})
validator, _ = validator.AddTokensFromDel(amt)
@ -189,3 +193,36 @@ func TestSlashAtFutureHeight(t *testing.T) {
fraction := sdk.NewDecWithPrec(5, 1)
require.Panics(t, func() { app.StakingKeeper.Slash(ctx, consAddr, 1, 10, fraction) })
}
// test slash at a negative height
// this just represents pre-genesis and should have the same effect as slashing at height 0
func TestSlashAtNegativeHeight(t *testing.T) {
app, ctx, _, _ := initConfig(t, 10)
consAddr := sdk.ConsAddress(PKs[0].Address())
fraction := sdk.NewDecWithPrec(5, 1)
bondedPool := app.StakingKeeper.GetBondedPool(ctx)
oldBondedPoolBalances := app.BankKeeper.GetAllBalances(ctx, bondedPool.GetAddress())
validator, found := app.StakingKeeper.GetValidatorByConsAddr(ctx, consAddr)
require.True(t, found)
app.StakingKeeper.Slash(ctx, consAddr, -2, 10, fraction)
// read updated state
validator, found = app.StakingKeeper.GetValidatorByConsAddr(ctx, consAddr)
require.True(t, found)
// end block
updates := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
require.Equal(t, 1, len(updates), "cons addr: %v, updates: %v", []byte(consAddr), updates)
validator, found = app.StakingKeeper.GetValidator(ctx, validator.OperatorAddress)
require.True(t, found)
// power decreased
require.Equal(t, int64(5), validator.GetConsensusPower())
// pool bonded shares decreased
newBondedPoolBalances := app.BankKeeper.GetAllBalances(ctx, bondedPool.GetAddress())
diffTokens := oldBondedPoolBalances.Sub(newBondedPoolBalances).AmountOf(app.StakingKeeper.BondDenom(ctx))
require.Equal(t, sdk.TokensFromConsensusPower(5).String(), diffTokens.String())
}