cosmos-sdk/x/slashing/test_common.go

120 lines
3.5 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/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/params"
2018-05-28 14:39:57 -07:00
"github.com/cosmos/cosmos-sdk/x/stake"
)
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
}
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
}
initCoins = 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, params.Setter, Keeper) {
2018-05-28 14:39:57 -07:00
keyAcc := sdk.NewKVStoreKey("acc")
keyStake := sdk.NewKVStoreKey("stake")
keySlashing := sdk.NewKVStoreKey("slashing")
keyParams := sdk.NewKVStoreKey("params")
2018-05-28 14:39:57 -07:00
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)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, 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()
accountMapper := auth.NewAccountMapper(cdc, keyAcc, auth.ProtoBaseAccount)
2018-05-28 14:39:57 -07:00
ck := bank.NewKeeper(accountMapper)
params := params.NewKeeper(cdc, keyParams)
2018-05-28 14:39:57 -07:00
sk := stake.NewKeeper(cdc, keyStake, ck, stake.DefaultCodespace)
genesis := stake.DefaultGenesisState()
genesis.Pool.LooseTokens = sdk.NewDec(initCoins.MulRaw(int64(len(addrs))).Int64())
_, 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 {
_, _, err = ck.AddCoins(ctx, addr, sdk.Coins{
2018-05-28 14:39:57 -07:00
{sk.GetParams(ctx).BondDenom, initCoins},
})
}
require.Nil(t, err)
keeper := NewKeeper(cdc, keySlashing, sk, params.Getter(), DefaultCodespace)
return ctx, ck, sk, params.Setter(), 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
}
func newTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) stake.MsgCreateValidator {
2018-05-31 12:22:46 -07:00
return stake.MsgCreateValidator{
Description: stake.Description{},
DelegatorAddr: sdk.AccAddress(address),
ValidatorAddr: address,
PubKey: pubKey,
Delegation: sdk.Coin{"steak", amt},
2018-05-28 14:39:57 -07:00
}
}
func newTestMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, delAmount sdk.Int) stake.MsgDelegate {
return stake.MsgDelegate{
DelegatorAddr: delAddr,
ValidatorAddr: valAddr,
Delegation: sdk.Coin{"steak", delAmount},
}
}