2019-10-14 08:43:19 -07:00
|
|
|
// nolint:deadcode,unused
|
2019-07-19 06:43:38 -07:00
|
|
|
// DONTCOVER
|
|
|
|
// noalias
|
|
|
|
package keeper
|
2018-05-28 14:39:57 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"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
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2019-08-02 06:20:39 -07:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
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-07-19 06:43:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/internal/types"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2019-06-28 13:11:27 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
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 (
|
2019-07-19 06:43:38 -07:00
|
|
|
Pks = []crypto.PubKey{
|
2018-05-28 14:39:57 -07:00
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
|
2018-06-04 16:42:01 -07:00
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
|
2018-05-28 14:39:57 -07:00
|
|
|
}
|
2019-07-19 06:43:38 -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-07-19 06:43:38 -07:00
|
|
|
InitTokens = sdk.TokensFromConsensusPower(200)
|
|
|
|
initCoins = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, InitTokens))
|
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)
|
2019-06-28 13:11:27 -07:00
|
|
|
supply.RegisterCodec(cdc)
|
2018-09-13 11:17:32 -07:00
|
|
|
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-07-19 06:43:38 -07:00
|
|
|
func CreateTestInput(t *testing.T, defaults types.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)
|
2019-07-19 06:43:38 -07:00
|
|
|
keySlashing := sdk.NewKVStoreKey(types.StoreKey)
|
2019-06-28 13:11:27 -07:00
|
|
|
keySupply := sdk.NewKVStoreKey(supply.StoreKey)
|
2018-12-19 11:58:30 -08:00
|
|
|
keyParams := sdk.NewKVStoreKey(params.StoreKey)
|
|
|
|
tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey)
|
2019-07-31 08:39:02 -07:00
|
|
|
|
2018-05-28 14:39:57 -07:00
|
|
|
db := dbm.NewMemDB()
|
2019-07-31 08:39:02 -07:00
|
|
|
|
2018-05-28 14:39:57 -07:00
|
|
|
ms := store.NewCommitMultiStore(db)
|
|
|
|
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
|
2019-01-11 12:08:01 -08:00
|
|
|
ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db)
|
2019-06-28 13:11:27 -07:00
|
|
|
ms.MountStoreWithDB(keySupply, 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)
|
2019-07-31 08:39:02 -07:00
|
|
|
|
2018-05-28 14:39:57 -07:00
|
|
|
err := ms.LoadLatestVersion()
|
|
|
|
require.Nil(t, err)
|
2019-07-31 08:39:02 -07:00
|
|
|
|
2019-05-09 10:38:15 -07:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{Time: time.Unix(0, 0)}, false, log.NewNopLogger())
|
2018-05-28 14:39:57 -07:00
|
|
|
cdc := createTestCodec()
|
2019-07-31 08:39:02 -07:00
|
|
|
|
|
|
|
feeCollectorAcc := supply.NewEmptyModuleAccount(auth.FeeCollectorName)
|
|
|
|
notBondedPool := supply.NewEmptyModuleAccount(staking.NotBondedPoolName, supply.Burner, supply.Staking)
|
|
|
|
bondPool := supply.NewEmptyModuleAccount(staking.BondedPoolName, supply.Burner, supply.Staking)
|
|
|
|
|
|
|
|
blacklistedAddrs := make(map[string]bool)
|
2019-08-08 12:51:18 -07:00
|
|
|
blacklistedAddrs[feeCollectorAcc.GetAddress().String()] = true
|
|
|
|
blacklistedAddrs[notBondedPool.GetAddress().String()] = true
|
|
|
|
blacklistedAddrs[bondPool.GetAddress().String()] = true
|
2019-07-31 08:39:02 -07:00
|
|
|
|
2019-04-30 09:31:38 -07:00
|
|
|
paramsKeeper := params.NewKeeper(cdc, keyParams, tkeyParams, params.DefaultCodespace)
|
2018-12-20 11:09:43 -08:00
|
|
|
accountKeeper := auth.NewAccountKeeper(cdc, keyAcc, paramsKeeper.Subspace(auth.DefaultParamspace), auth.ProtoBaseAccount)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2019-07-31 08:39:02 -07:00
|
|
|
bk := bank.NewBaseKeeper(accountKeeper, paramsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace, blacklistedAddrs)
|
2019-07-06 12:42:06 -07:00
|
|
|
maccPerms := map[string][]string{
|
2019-07-16 07:02:36 -07:00
|
|
|
auth.FeeCollectorName: nil,
|
2019-07-22 08:26:42 -07:00
|
|
|
staking.NotBondedPoolName: {supply.Burner, supply.Staking},
|
|
|
|
staking.BondedPoolName: {supply.Burner, supply.Staking},
|
2019-07-06 12:42:06 -07:00
|
|
|
}
|
2019-08-01 11:56:50 -07:00
|
|
|
supplyKeeper := supply.NewKeeper(cdc, keySupply, accountKeeper, bk, maccPerms)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2019-07-19 06:43:38 -07:00
|
|
|
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, InitTokens.MulRaw(int64(len(Addrs)))))
|
2019-06-28 13:11:27 -07:00
|
|
|
supplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply))
|
|
|
|
|
2019-09-18 11:28:02 -07:00
|
|
|
sk := staking.NewKeeper(cdc, keyStaking, supplyKeeper, paramsKeeper.Subspace(staking.DefaultParamspace), staking.DefaultCodespace)
|
2019-01-11 12:08:01 -08:00
|
|
|
genesis := staking.DefaultGenesisState()
|
2018-07-13 13:46:14 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// set module accounts
|
|
|
|
supplyKeeper.SetModuleAccount(ctx, feeCollectorAcc)
|
|
|
|
supplyKeeper.SetModuleAccount(ctx, bondPool)
|
|
|
|
supplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
2018-07-13 13:46:14 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
_ = staking.InitGenesis(ctx, sk, accountKeeper, supplyKeeper, genesis)
|
2018-07-09 19:51:13 -07:00
|
|
|
|
2019-07-19 06:43:38 -07:00
|
|
|
for _, addr := range Addrs {
|
2019-06-28 13:11:27 -07:00
|
|
|
_, err = bk.AddCoins(ctx, sdk.AccAddress(addr), initCoins)
|
2018-05-28 14:39:57 -07:00
|
|
|
}
|
2019-03-01 16:04:37 -08:00
|
|
|
require.Nil(t, err)
|
2019-07-19 06:43:38 -07:00
|
|
|
paramstore := paramsKeeper.Subspace(types.DefaultParamspace)
|
|
|
|
keeper := NewKeeper(cdc, keySlashing, &sk, paramstore, types.DefaultCodespace)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2019-07-19 06:43:38 -07:00
|
|
|
keeper.SetParams(ctx, defaults)
|
|
|
|
sk.SetHooks(keeper.Hooks())
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
return ctx, bk, 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
|
2019-10-14 08:43:19 -07:00
|
|
|
copy(pkEd[:], pkBytes)
|
2018-05-28 14:39:57 -07:00
|
|
|
return pkEd
|
|
|
|
}
|
|
|
|
|
2019-07-19 06:43:38 -07:00
|
|
|
// Have to change these parameters for tests
|
|
|
|
// lest the tests take forever
|
|
|
|
func TestParams() types.Params {
|
|
|
|
params := types.DefaultParams()
|
|
|
|
params.SignedBlocksWindow = 1000
|
|
|
|
params.DowntimeJailDuration = 60 * 60
|
|
|
|
return params
|
2018-05-28 14:39:57 -07:00
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
func NewTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) staking.MsgCreateValidator {
|
2019-06-03 14:14:21 -07:00
|
|
|
commission := staking.NewCommissionRates(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-07-19 06:43:38 -07: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
|
|
|
}
|