2018-05-28 14:39:57 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-08-31 12:21:12 -07:00
|
|
|
"time"
|
2018-05-28 14:39:57 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2018-07-25 13:43:37 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
2018-07-02 13:34:06 -07:00
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2018-05-28 14:39:57 -07:00
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-05-28 14:39:57 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2018-07-13 17:12:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2018-05-28 14:39:57 -07:00
|
|
|
)
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
// TODO remove dependencies on staking (should only refer to validator set type from sdk)
|
|
|
|
|
2018-05-28 14:39:57 -07:00
|
|
|
var (
|
|
|
|
pks = []crypto.PubKey{
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
|
2018-06-04 16:42:01 -07:00
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
|
2018-05-28 14:39:57 -07:00
|
|
|
}
|
2018-08-31 17:01:23 -07:00
|
|
|
addrs = []sdk.ValAddress{
|
|
|
|
sdk.ValAddress(pks[0].Address()),
|
|
|
|
sdk.ValAddress(pks[1].Address()),
|
|
|
|
sdk.ValAddress(pks[2].Address()),
|
2018-07-03 22:32:49 -07:00
|
|
|
}
|
2019-02-13 15:01:50 -08:00
|
|
|
initCoins = sdk.TokensFromTendermintPower(200)
|
2018-05-28 14:39:57 -07:00
|
|
|
)
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
func createTestCodec() *codec.Codec {
|
|
|
|
cdc := codec.New()
|
|
|
|
sdk.RegisterCodec(cdc)
|
|
|
|
auth.RegisterCodec(cdc)
|
|
|
|
bank.RegisterCodec(cdc)
|
2019-01-11 12:08:01 -08:00
|
|
|
staking.RegisterCodec(cdc)
|
2018-09-13 11:17:32 -07:00
|
|
|
codec.RegisterCrypto(cdc)
|
2018-05-28 14:39:57 -07:00
|
|
|
return cdc
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, staking.Keeper, params.Subspace, Keeper) {
|
2018-12-19 11:58:30 -08:00
|
|
|
keyAcc := sdk.NewKVStoreKey(auth.StoreKey)
|
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)
|
|
|
|
keyParams := sdk.NewKVStoreKey(params.StoreKey)
|
|
|
|
tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey)
|
2018-05-28 14:39:57 -07:00
|
|
|
db := dbm.NewMemDB()
|
|
|
|
ms := store.NewCommitMultiStore(db)
|
|
|
|
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
|
2019-01-11 12:08:01 -08:00
|
|
|
ms.MountStoreWithDB(tkeyStaking, sdk.StoreTypeTransient, nil)
|
|
|
|
ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db)
|
2018-05-28 14:39:57 -07:00
|
|
|
ms.MountStoreWithDB(keySlashing, sdk.StoreTypeIAVL, db)
|
2018-07-13 17:12:23 -07:00
|
|
|
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
|
2018-08-31 02:03:43 -07:00
|
|
|
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
|
2018-05-28 14:39:57 -07:00
|
|
|
err := ms.LoadLatestVersion()
|
|
|
|
require.Nil(t, err)
|
2018-08-31 12:21:12 -07:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewTMLogger(os.Stdout))
|
2018-05-28 14:39:57 -07:00
|
|
|
cdc := createTestCodec()
|
2018-12-20 11:09:43 -08:00
|
|
|
paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams)
|
|
|
|
accountKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2019-01-28 19:06:48 -08:00
|
|
|
ck := bank.NewBaseKeeper(accountKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace)
|
2019-01-11 12:08:01 -08:00
|
|
|
sk := staking.NewKeeper(cdc, keyStaking, tkeyStaking, ck, paramsKeeper.Subspace(staking.DefaultParamspace), staking.DefaultCodespace)
|
|
|
|
genesis := staking.DefaultGenesisState()
|
2018-07-13 13:46:14 -07:00
|
|
|
|
2019-02-05 21:30:48 -08:00
|
|
|
genesis.Pool.NotBondedTokens = initCoins.MulRaw(int64(len(addrs)))
|
2018-07-13 13:46:14 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
_, err = staking.InitGenesis(ctx, sk, genesis)
|
2018-07-09 19:51:13 -07:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2018-05-28 14:39:57 -07:00
|
|
|
for _, addr := range addrs {
|
2019-03-01 16:04:37 -08:00
|
|
|
_, _, err = ck.AddCoins(ctx, sdk.AccAddress(addr), sdk.Coins{
|
|
|
|
{sk.GetParams(ctx).BondDenom, initCoins},
|
|
|
|
})
|
2018-05-28 14:39:57 -07:00
|
|
|
}
|
2019-03-01 16:04:37 -08:00
|
|
|
require.Nil(t, err)
|
2018-10-10 13:01:30 -07:00
|
|
|
paramstore := paramsKeeper.Subspace(DefaultParamspace)
|
2019-03-01 16:04:37 -08:00
|
|
|
keeper := NewKeeper(cdc, keySlashing, &sk, paramstore, DefaultCodespace)
|
2018-11-04 22:11:03 -08:00
|
|
|
sk.SetHooks(keeper.Hooks())
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2018-09-10 05:10:06 -07:00
|
|
|
require.NotPanics(t, func() {
|
2019-02-13 15:01:50 -08:00
|
|
|
InitGenesis(ctx, keeper, GenesisState{defaults, nil, nil}, genesis.Validators.ToSDKValidators())
|
2018-09-10 05:10:06 -07:00
|
|
|
})
|
2018-08-31 02:03:43 -07:00
|
|
|
|
|
|
|
return ctx, ck, sk, paramstore, keeper
|
2018-05-28 14:39:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPubKey(pk string) (res crypto.PubKey) {
|
|
|
|
pkBytes, err := hex.DecodeString(pk)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-25 13:43:37 -07:00
|
|
|
var pkEd ed25519.PubKeyEd25519
|
2018-05-28 14:39:57 -07:00
|
|
|
copy(pkEd[:], pkBytes[:])
|
|
|
|
return pkEd
|
|
|
|
}
|
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
func testAddr(addr string) sdk.AccAddress {
|
2018-05-29 12:46:38 -07:00
|
|
|
res := []byte(addr)
|
2018-05-28 14:39:57 -07:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
func NewTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) staking.MsgCreateValidator {
|
|
|
|
commission := staking.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
|
2019-01-17 09:53:22 -08:00
|
|
|
return staking.NewMsgCreateValidator(
|
2019-02-13 15:01:50 -08:00
|
|
|
address, pubKey, sdk.NewCoin(sdk.DefaultBondDenom, amt),
|
2019-02-08 12:44:19 -08:00
|
|
|
staking.Description{}, commission, sdk.OneInt(),
|
2019-01-17 09:53:22 -08:00
|
|
|
)
|
2018-05-28 14:39:57 -07:00
|
|
|
}
|
2018-08-31 12:21:12 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
func newTestMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, delAmount sdk.Int) staking.MsgDelegate {
|
2019-02-13 15:01:50 -08:00
|
|
|
amount := sdk.NewCoin(sdk.DefaultBondDenom, delAmount)
|
2019-01-17 09:53:22 -08:00
|
|
|
return staking.NewMsgDelegate(delAddr, valAddr, amount)
|
2018-08-31 12:21:12 -07:00
|
|
|
}
|