cosmos-sdk/x/slashing/test_common.go

127 lines
4.2 KiB
Go
Raw Normal View History

2018-05-28 14:39:57 -07:00
package slashing
import (
"encoding/hex"
"os"
"testing"
"time"
2018-05-28 14:39:57 -07:00
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
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/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"
"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
)
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
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
}
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
}
initCoins = staking.TokensFromTendermintPower(200)
2018-05-28 14:39:57 -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)
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) {
keyAcc := sdk.NewKVStoreKey(auth.StoreKey)
2019-01-11 12:08:01 -08:00
keyStaking := sdk.NewKVStoreKey(staking.StoreKey)
tkeyStaking := sdk.NewTransientStoreKey(staking.TStoreKey)
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)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
2018-05-28 14:39:57 -07:00
err := ms.LoadLatestVersion()
require.Nil(t, err)
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)
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()
genesis.Pool.NotBondedTokens = initCoins.MulRaw(int64(len(addrs)))
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 {
_, _, err = ck.AddCoins(ctx, sdk.AccAddress(addr), sdk.Coins{
2018-05-28 14:39:57 -07:00
{sk.GetParams(ctx).BondDenom, initCoins},
})
}
require.Nil(t, err)
paramstore := paramsKeeper.Subspace(DefaultParamspace)
keeper := NewKeeper(cdc, keySlashing, &sk, paramstore, DefaultCodespace)
sk.SetHooks(keeper.Hooks())
2018-09-10 05:10:06 -07:00
require.NotPanics(t, func() {
2019-01-10 17:22:49 -08:00
InitGenesis(ctx, keeper, GenesisState{defaults, nil, nil}, genesis)
2018-09-10 05:10:06 -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)
}
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(
address, pubKey, sdk.NewCoin(staking.DefaultBondDenom, amt),
2019-01-17 09:53:22 -08:00
staking.Description{}, commission,
)
2018-05-28 14:39:57 -07:00
}
2019-01-11 12:08:01 -08:00
func newTestMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, delAmount sdk.Int) staking.MsgDelegate {
2019-01-17 09:53:22 -08:00
amount := sdk.NewCoin(staking.DefaultBondDenom, delAmount)
return staking.NewMsgDelegate(delAddr, valAddr, amount)
}