2019-07-19 06:43:38 -07:00
|
|
|
// nolint
|
|
|
|
// DONTCOVER
|
2018-06-10 17:55:54 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-11-12 10:54:18 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2019-03-01 09:12:28 -08:00
|
|
|
"github.com/tendermint/tendermint/crypto/secp256k1"
|
2018-11-12 10:54:18 -08:00
|
|
|
|
2018-06-10 17:55:54 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2018-07-03 21:21:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2019-06-28 13:11:27 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
2018-06-10 17:55:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-03-01 09:12:28 -08:00
|
|
|
priv1 = secp256k1.GenPrivKey()
|
2018-07-06 00:06:53 -07:00
|
|
|
addr1 = sdk.AccAddress(priv1.PubKey().Address())
|
2018-07-30 17:09:50 -07:00
|
|
|
coins = sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}
|
2018-06-10 17:55:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// initialize the mock application for this module
|
2019-01-11 12:08:01 -08:00
|
|
|
func getMockApp(t *testing.T) (*mock.App, staking.Keeper, Keeper) {
|
2018-06-10 17:55:54 -07:00
|
|
|
mapp := mock.NewApp()
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
RegisterCodec(mapp.Cdc)
|
2019-02-05 21:30:48 -08:00
|
|
|
staking.RegisterCodec(mapp.Cdc)
|
2019-06-28 13:11:27 -07:00
|
|
|
supply.RegisterCodec(mapp.Cdc)
|
2019-02-04 15:35:12 -08:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
keyStaking := sdk.NewKVStoreKey(staking.StoreKey)
|
|
|
|
tkeyStaking := sdk.NewTransientStoreKey(staking.TStoreKey)
|
2018-12-19 11:58:30 -08:00
|
|
|
keySlashing := sdk.NewKVStoreKey(StoreKey)
|
2019-06-28 13:11:27 -07:00
|
|
|
keySupply := sdk.NewKVStoreKey(supply.StoreKey)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2019-01-28 19:06:48 -08:00
|
|
|
bankKeeper := bank.NewBaseKeeper(mapp.AccountKeeper, mapp.ParamsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace)
|
2019-07-06 12:42:06 -07:00
|
|
|
maccPerms := map[string][]string{
|
2019-07-16 07:02:36 -07:00
|
|
|
auth.FeeCollectorName: nil,
|
2019-07-22 08:26:42 -07:00
|
|
|
staking.NotBondedPoolName: {supply.Burner, supply.Staking},
|
|
|
|
staking.BondedPoolName: {supply.Burner, supply.Staking},
|
2019-07-06 12:42:06 -07:00
|
|
|
}
|
|
|
|
supplyKeeper := supply.NewKeeper(mapp.Cdc, keySupply, mapp.AccountKeeper, bankKeeper, supply.DefaultCodespace, maccPerms)
|
2019-06-28 13:11:27 -07:00
|
|
|
stakingKeeper := staking.NewKeeper(mapp.Cdc, keyStaking, tkeyStaking, supplyKeeper, mapp.ParamsKeeper.Subspace(staking.DefaultParamspace), staking.DefaultCodespace)
|
2019-03-01 16:04:37 -08:00
|
|
|
keeper := NewKeeper(mapp.Cdc, keySlashing, stakingKeeper, mapp.ParamsKeeper.Subspace(DefaultParamspace), DefaultCodespace)
|
2019-01-11 12:08:01 -08:00
|
|
|
mapp.Router().AddRoute(staking.RouterKey, staking.NewHandler(stakingKeeper))
|
2018-12-19 11:58:30 -08:00
|
|
|
mapp.Router().AddRoute(RouterKey, NewHandler(keeper))
|
2019-03-01 16:04:37 -08:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
mapp.SetEndBlocker(getEndBlocker(stakingKeeper))
|
2019-06-28 13:11:27 -07:00
|
|
|
mapp.SetInitChainer(getInitChainer(mapp, stakingKeeper, mapp.AccountKeeper, supplyKeeper))
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
require.NoError(t, mapp.CompleteSetup(keyStaking, tkeyStaking, keySupply, keySlashing))
|
2018-06-10 17:55:54 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
return mapp, stakingKeeper, keeper
|
2018-06-10 17:55:54 -07:00
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
// staking endblocker
|
|
|
|
func getEndBlocker(keeper staking.Keeper) sdk.EndBlocker {
|
2019-04-05 13:20:58 -07:00
|
|
|
return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
|
2019-06-26 09:03:25 -07:00
|
|
|
validatorUpdates := staking.EndBlocker(ctx, keeper)
|
2018-06-10 17:55:54 -07:00
|
|
|
return abci.ResponseEndBlock{
|
|
|
|
ValidatorUpdates: validatorUpdates,
|
2019-04-05 13:20:58 -07:00
|
|
|
}
|
2018-06-10 17:55:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite the mock init chainer
|
2019-06-28 13:11:27 -07:00
|
|
|
func getInitChainer(mapp *mock.App, keeper staking.Keeper, accountKeeper types.AccountKeeper, supplyKeeper types.SupplyKeeper) sdk.InitChainer {
|
2018-06-10 17:55:54 -07:00
|
|
|
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
2019-06-28 13:11:27 -07:00
|
|
|
// set module accounts
|
2019-07-16 07:02:36 -07:00
|
|
|
feeCollector := supply.NewEmptyModuleAccount(auth.FeeCollectorName)
|
2019-07-06 12:42:06 -07:00
|
|
|
notBondedPool := supply.NewEmptyModuleAccount(types.NotBondedPoolName, supply.Burner, supply.Staking)
|
|
|
|
bondPool := supply.NewEmptyModuleAccount(types.BondedPoolName, supply.Burner, supply.Staking)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
supplyKeeper.SetModuleAccount(ctx, feeCollector)
|
|
|
|
supplyKeeper.SetModuleAccount(ctx, bondPool)
|
|
|
|
supplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
|
|
|
|
2018-06-10 17:55:54 -07:00
|
|
|
mapp.InitChainer(ctx, req)
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingGenesis := staking.DefaultGenesisState()
|
2019-06-28 13:11:27 -07:00
|
|
|
validators := staking.InitGenesis(ctx, keeper, accountKeeper, supplyKeeper, stakingGenesis)
|
2018-07-18 23:39:40 -07:00
|
|
|
return abci.ResponseInitChain{
|
|
|
|
Validators: validators,
|
|
|
|
}
|
2018-06-10 17:55:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
func checkValidator(t *testing.T, mapp *mock.App, keeper staking.Keeper,
|
|
|
|
addr sdk.AccAddress, expFound bool) staking.Validator {
|
2018-06-10 17:55:54 -07:00
|
|
|
ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{})
|
2018-08-30 21:06:44 -07:00
|
|
|
validator, found := keeper.GetValidator(ctxCheck, sdk.ValAddress(addr1))
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, expFound, found)
|
2018-06-10 17:55:54 -07:00
|
|
|
return validator
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkValidatorSigningInfo(t *testing.T, mapp *mock.App, keeper Keeper,
|
2018-08-31 17:01:23 -07:00
|
|
|
addr sdk.ConsAddress, expFound bool) ValidatorSigningInfo {
|
2018-06-10 17:55:54 -07:00
|
|
|
ctxCheck := mapp.BaseApp.NewContext(true, abci.Header{})
|
2019-07-19 06:43:38 -07:00
|
|
|
signingInfo, found := keeper.GetValidatorSigningInfo(ctxCheck, addr)
|
2018-06-29 18:10:15 -07:00
|
|
|
require.Equal(t, expFound, found)
|
2018-06-10 17:55:54 -07:00
|
|
|
return signingInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSlashingMsgs(t *testing.T) {
|
2019-01-11 12:08:01 -08:00
|
|
|
mapp, stakingKeeper, keeper := getMockApp(t)
|
2018-06-10 17:55:54 -07:00
|
|
|
|
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-10 17:55:54 -07:00
|
|
|
|
|
|
|
acc1 := &auth.BaseAccount{
|
|
|
|
Address: addr1,
|
|
|
|
Coins: sdk.Coins{genCoin},
|
|
|
|
}
|
2018-06-10 18:15:48 -07:00
|
|
|
accs := []auth.Account{acc1}
|
2018-06-10 17:55:54 -07:00
|
|
|
mock.SetGenesis(mapp, accs)
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
description := staking.NewDescription("foo_moniker", "", "", "")
|
2019-06-03 14:14:21 -07:00
|
|
|
commission := staking.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
createValidatorMsg := staking.NewMsgCreateValidator(
|
2019-02-08 12:44:19 -08:00
|
|
|
sdk.ValAddress(addr1), priv1.PubKey(), bondCoin, description, commission, sdk.OneInt(),
|
2018-06-10 17:55:54 -07:00
|
|
|
)
|
2019-03-07 21:52:19 -08:00
|
|
|
|
|
|
|
header := abci.Header{Height: mapp.LastBlockHeight() + 1}
|
|
|
|
mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{createValidatorMsg}, []uint64{0}, []uint64{0}, true, true, priv1)
|
2019-02-21 09:35:55 -08:00
|
|
|
mock.CheckBalance(t, mapp, addr1, sdk.Coins{genCoin.Sub(bondCoin)})
|
2019-03-07 21:52:19 -08:00
|
|
|
|
|
|
|
header = abci.Header{Height: mapp.LastBlockHeight() + 1}
|
|
|
|
mapp.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2018-06-10 17:55:54 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
validator := checkValidator(t, mapp, stakingKeeper, 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-09-08 01:44:58 -07:00
|
|
|
unjailMsg := MsgUnjail{ValidatorAddr: sdk.ValAddress(validator.ConsPubKey.Address())}
|
2018-06-10 17:55:54 -07:00
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
// no signing info yet
|
2018-08-31 17:01:23 -07:00
|
|
|
checkValidatorSigningInfo(t, mapp, keeper, sdk.ConsAddress(addr1), false)
|
2018-06-10 17:55:54 -07:00
|
|
|
|
2018-08-22 08:56:13 -07:00
|
|
|
// unjail should fail with unknown validator
|
2019-03-07 21:52:19 -08:00
|
|
|
header = abci.Header{Height: mapp.LastBlockHeight() + 1}
|
|
|
|
res := mock.SignCheckDeliver(t, mapp.Cdc, mapp.BaseApp, header, []sdk.Msg{unjailMsg}, []uint64{0}, []uint64{1}, false, false, priv1)
|
2018-11-16 09:12:24 -08:00
|
|
|
require.EqualValues(t, CodeValidatorNotJailed, res.Code)
|
|
|
|
require.EqualValues(t, DefaultCodespace, res.Codespace)
|
2018-06-10 17:55:54 -07:00
|
|
|
}
|