2020-02-14 07:30:51 -08:00
|
|
|
package slashing_test
|
2018-06-10 17:55:54 -07:00
|
|
|
|
|
|
|
import (
|
2019-12-27 09:57:54 -08:00
|
|
|
"errors"
|
2018-06-10 17:55:54 -07:00
|
|
|
"testing"
|
|
|
|
|
2018-11-12 10:54:18 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2018-11-12 10:54:18 -08:00
|
|
|
|
2020-09-25 01:41:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
2020-08-28 09:02:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
2020-02-14 07:30:51 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2018-06-10 17:55:54 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-05-20 12:21:00 -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:25:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
2020-06-12 07:54:37 -07:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
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())
|
2020-08-14 10:58:53 -07:00
|
|
|
|
|
|
|
valKey = ed25519.GenPrivKey()
|
|
|
|
valAddr = sdk.AccAddress(valKey.PubKey().Address())
|
2018-06-10 17:55:54 -07:00
|
|
|
)
|
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
func checkValidator(t *testing.T, app *simapp.SimApp, _ sdk.AccAddress, expFound bool) stakingtypes.Validator {
|
2020-08-14 10:58:53 -07:00
|
|
|
ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{})
|
2020-02-14 07:30:51 -08:00
|
|
|
validator, found := app.StakingKeeper.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
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:25:12 -07:00
|
|
|
func checkValidatorSigningInfo(t *testing.T, app *simapp.SimApp, addr sdk.ConsAddress, expFound bool) types.ValidatorSigningInfo {
|
2020-08-14 10:58:53 -07:00
|
|
|
ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{})
|
2020-02-14 07:30:51 -08:00
|
|
|
signingInfo, found := app.SlashingKeeper.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-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
|
|
|
|
2020-06-17 11:42:27 -07:00
|
|
|
acc1 := &authtypes.BaseAccount{
|
2020-09-25 03:25:37 -07:00
|
|
|
Address: addr1.String(),
|
2018-06-10 17:55:54 -07:00
|
|
|
}
|
2020-05-20 12:21:00 -07:00
|
|
|
accs := authtypes.GenesisAccounts{acc1}
|
2020-06-14 16:06:16 -07:00
|
|
|
balances := []banktypes.Balance{
|
2020-02-14 07:30:51 -08:00
|
|
|
{
|
2020-09-25 03:25:37 -07:00
|
|
|
Address: addr1.String(),
|
2020-01-30 13:31:16 -08:00
|
|
|
Coins: sdk.Coins{genCoin},
|
|
|
|
},
|
|
|
|
}
|
2020-02-14 07:30:51 -08:00
|
|
|
|
|
|
|
app := simapp.SetupWithGenesisAccounts(accs, balances...)
|
|
|
|
simapp.CheckBalance(t, app, addr1, sdk.Coins{genCoin})
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2020-06-12 07:54:37 -07:00
|
|
|
description := stakingtypes.NewDescription("foo_moniker", "", "", "", "")
|
|
|
|
commission := stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
|
2018-09-24 15:23:58 -07:00
|
|
|
|
2020-10-19 06:04:55 -07:00
|
|
|
createValidatorMsg, err := stakingtypes.NewMsgCreateValidator(
|
2020-08-14 10:58:53 -07:00
|
|
|
sdk.ValAddress(addr1), valKey.PubKey(), bondCoin, description, commission, sdk.OneInt(),
|
2018-06-10 17:55:54 -07:00
|
|
|
)
|
2020-10-19 06:04:55 -07:00
|
|
|
require.NoError(t, err)
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
header := tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-10-27 04:33:48 -07:00
|
|
|
txGen := simapp.MakeTestEncodingConfig().TxConfig
|
2020-10-19 06:04:55 -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-08-14 10:58:53 -07:00
|
|
|
header = tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-02-14 07:30:51 -08:00
|
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: header})
|
2018-06-10 17:55:54 -07:00
|
|
|
|
2020-02-14 07:30:51 -08:00
|
|
|
validator := checkValidator(t, app, addr1, true)
|
2020-09-25 03:25:37 -07:00
|
|
|
require.Equal(t, sdk.ValAddress(addr1).String(), validator.OperatorAddress)
|
2020-10-12 06:56:02 -07:00
|
|
|
require.Equal(t, stakingtypes.Bonded, validator.Status)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.True(sdk.IntEq(t, bondTokens, validator.BondedTokens()))
|
2020-09-25 03:25:37 -07:00
|
|
|
unjailMsg := &types.MsgUnjail{ValidatorAddr: sdk.ValAddress(addr1).String()}
|
2018-06-10 17:55:54 -07:00
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
checkValidatorSigningInfo(t, app, sdk.ConsAddress(valAddr), true)
|
2018-06-10 17:55:54 -07:00
|
|
|
|
2018-08-22 08:56:13 -07:00
|
|
|
// unjail should fail with unknown validator
|
2020-08-14 10:58:53 -07:00
|
|
|
header = tmproto.Header{Height: app.LastBlockHeight() + 1}
|
2020-08-20 15:05:41 -07:00
|
|
|
_, res, err := simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{unjailMsg}, "", []uint64{0}, []uint64{1}, false, false, priv1)
|
2019-12-27 09:57:54 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, res)
|
2020-06-12 07:25:12 -07:00
|
|
|
require.True(t, errors.Is(types.ErrValidatorNotJailed, err))
|
2018-06-10 17:55:54 -07:00
|
|
|
}
|