2018-05-28 14:39:57 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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-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
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
|
|
|
"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-07-06 00:06:53 -07:00
|
|
|
addrs = []sdk.AccAddress{
|
2018-07-09 01:47:38 -07:00
|
|
|
sdk.AccAddress(pks[0].Address()),
|
|
|
|
sdk.AccAddress(pks[1].Address()),
|
|
|
|
sdk.AccAddress(pks[2].Address()),
|
2018-07-03 22:32:49 -07:00
|
|
|
}
|
2018-06-15 14:16:45 -07:00
|
|
|
initCoins sdk.Int = sdk.NewInt(200)
|
2018-05-28 14:39:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func createTestCodec() *wire.Codec {
|
|
|
|
cdc := wire.NewCodec()
|
|
|
|
sdk.RegisterWire(cdc)
|
|
|
|
auth.RegisterWire(cdc)
|
|
|
|
bank.RegisterWire(cdc)
|
|
|
|
stake.RegisterWire(cdc)
|
|
|
|
wire.RegisterCrypto(cdc)
|
|
|
|
return cdc
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTestInput(t *testing.T) (sdk.Context, bank.Keeper, stake.Keeper, Keeper) {
|
|
|
|
keyAcc := sdk.NewKVStoreKey("acc")
|
|
|
|
keyStake := sdk.NewKVStoreKey("stake")
|
|
|
|
keySlashing := sdk.NewKVStoreKey("slashing")
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
ms := store.NewCommitMultiStore(db)
|
|
|
|
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
|
|
|
|
ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db)
|
|
|
|
ms.MountStoreWithDB(keySlashing, sdk.StoreTypeIAVL, db)
|
|
|
|
err := ms.LoadLatestVersion()
|
|
|
|
require.Nil(t, err)
|
2018-06-21 18:03:05 -07:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{}, false, log.NewTMLogger(os.Stdout))
|
2018-05-28 14:39:57 -07:00
|
|
|
cdc := createTestCodec()
|
|
|
|
accountMapper := auth.NewAccountMapper(cdc, keyAcc, &auth.BaseAccount{})
|
|
|
|
ck := bank.NewKeeper(accountMapper)
|
|
|
|
sk := stake.NewKeeper(cdc, keyStake, ck, stake.DefaultCodespace)
|
|
|
|
genesis := stake.DefaultGenesisState()
|
2018-06-26 19:00:12 -07:00
|
|
|
genesis.Pool.LooseTokens = initCoins.MulRaw(int64(len(addrs))).Int64()
|
2018-05-28 14:39:57 -07:00
|
|
|
stake.InitGenesis(ctx, sk, genesis)
|
|
|
|
for _, addr := range addrs {
|
2018-06-28 15:52:10 -07:00
|
|
|
_, _, err = ck.AddCoins(ctx, 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-05-28 14:39:57 -07:00
|
|
|
keeper := NewKeeper(cdc, keySlashing, sk, DefaultCodespace)
|
|
|
|
return ctx, ck, sk, keeper
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPubKey(pk string) (res crypto.PubKey) {
|
|
|
|
pkBytes, err := hex.DecodeString(pk)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var pkEd crypto.PubKeyEd25519
|
|
|
|
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-07-06 00:06:53 -07:00
|
|
|
func newTestMsgCreateValidator(address sdk.AccAddress, pubKey crypto.PubKey, amt sdk.Int) stake.MsgCreateValidator {
|
2018-05-31 12:22:46 -07:00
|
|
|
return stake.MsgCreateValidator{
|
2018-06-26 19:00:12 -07:00
|
|
|
Description: stake.Description{},
|
|
|
|
ValidatorAddr: address,
|
|
|
|
PubKey: pubKey,
|
|
|
|
SelfDelegation: sdk.Coin{"steak", amt},
|
2018-05-28 14:39:57 -07:00
|
|
|
}
|
|
|
|
}
|