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"
|
2018-05-28 14:39:57 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
2018-07-17 13:59:06 -07:00
|
|
|
initCoins = sdk.NewInt(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)
|
|
|
|
stake.RegisterCodec(cdc)
|
|
|
|
codec.RegisterCrypto(cdc)
|
2018-05-28 14:39:57 -07:00
|
|
|
return cdc
|
|
|
|
}
|
|
|
|
|
2018-10-10 13:01:30 -07:00
|
|
|
func createTestInput(t *testing.T, defaults Params) (sdk.Context, bank.Keeper, stake.Keeper, params.Subspace, Keeper) {
|
2018-05-28 14:39:57 -07:00
|
|
|
keyAcc := sdk.NewKVStoreKey("acc")
|
|
|
|
keyStake := sdk.NewKVStoreKey("stake")
|
2018-09-12 21:53:55 -07:00
|
|
|
tkeyStake := sdk.NewTransientStoreKey("transient_stake")
|
2018-05-28 14:39:57 -07:00
|
|
|
keySlashing := sdk.NewKVStoreKey("slashing")
|
2018-07-13 17:12:23 -07:00
|
|
|
keyParams := sdk.NewKVStoreKey("params")
|
2018-08-31 02:03:43 -07:00
|
|
|
tkeyParams := sdk.NewTransientStoreKey("transient_params")
|
2018-05-28 14:39:57 -07:00
|
|
|
db := dbm.NewMemDB()
|
|
|
|
ms := store.NewCommitMultiStore(db)
|
|
|
|
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
|
2018-09-12 21:53:55 -07:00
|
|
|
ms.MountStoreWithDB(tkeyStake, sdk.StoreTypeTransient, nil)
|
2018-05-28 14:39:57 -07:00
|
|
|
ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db)
|
|
|
|
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-06-25 19:05:47 -07:00
|
|
|
accountMapper := auth.NewAccountMapper(cdc, keyAcc, auth.ProtoBaseAccount)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2018-09-10 11:25:34 -07:00
|
|
|
ck := bank.NewBaseKeeper(accountMapper)
|
2018-10-06 08:32:41 -07:00
|
|
|
paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams)
|
2018-10-10 13:01:30 -07:00
|
|
|
sk := stake.NewKeeper(cdc, keyStake, tkeyStake, ck, paramsKeeper.Subspace(stake.DefaultParamspace), stake.DefaultCodespace)
|
2018-05-28 14:39:57 -07:00
|
|
|
genesis := stake.DefaultGenesisState()
|
2018-07-13 13:46:14 -07:00
|
|
|
|
2018-08-14 17:15:02 -07:00
|
|
|
genesis.Pool.LooseTokens = sdk.NewDec(initCoins.MulRaw(int64(len(addrs))).Int64())
|
2018-07-13 13:46:14 -07:00
|
|
|
|
2018-07-18 23:39:40 -07:00
|
|
|
_, err = stake.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 {
|
2018-08-31 17:01:23 -07:00
|
|
|
_, _, err = ck.AddCoins(ctx, sdk.AccAddress(addr), sdk.Coins{
|
2018-05-28 14:39:57 -07:00
|
|
|
{sk.GetParams(ctx).BondDenom, initCoins},
|
|
|
|
})
|
|
|
|
}
|
2018-06-28 15:52:10 -07:00
|
|
|
require.Nil(t, err)
|
2018-10-10 13:01:30 -07:00
|
|
|
paramstore := paramsKeeper.Subspace(DefaultParamspace)
|
2018-08-31 02:03:43 -07:00
|
|
|
keeper := NewKeeper(cdc, keySlashing, sk, paramstore, DefaultCodespace)
|
2018-10-11 13:48:00 -07:00
|
|
|
sk = sk.WithHooks(keeper.Hooks())
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2018-09-10 05:10:06 -07:00
|
|
|
require.NotPanics(t, func() {
|
|
|
|
InitGenesis(ctx, keeper, GenesisState{defaults}, genesis)
|
|
|
|
})
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:58:59 -07:00
|
|
|
func NewTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) stake.MsgCreateValidator {
|
2018-09-24 15:23:58 -07:00
|
|
|
commission := stake.NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
|
2018-05-31 12:22:46 -07:00
|
|
|
return stake.MsgCreateValidator{
|
2018-07-10 17:16:37 -07:00
|
|
|
Description: stake.Description{},
|
2018-09-24 15:23:58 -07:00
|
|
|
Commission: commission,
|
2018-08-30 21:06:44 -07:00
|
|
|
DelegatorAddr: sdk.AccAddress(address),
|
2018-07-10 17:16:37 -07:00
|
|
|
ValidatorAddr: address,
|
|
|
|
PubKey: pubKey,
|
2018-09-24 15:23:58 -07:00
|
|
|
Delegation: sdk.NewCoin("steak", amt),
|
2018-05-28 14:39:57 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 12:21:12 -07:00
|
|
|
|
|
|
|
func newTestMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, delAmount sdk.Int) stake.MsgDelegate {
|
|
|
|
return stake.MsgDelegate{
|
|
|
|
DelegatorAddr: delAddr,
|
|
|
|
ValidatorAddr: valAddr,
|
2018-09-24 15:23:58 -07:00
|
|
|
Delegation: sdk.NewCoin("steak", delAmount),
|
2018-08-31 12:21:12 -07:00
|
|
|
}
|
|
|
|
}
|