2018-06-26 19:00:12 -07:00
|
|
|
package keeper
|
2018-01-25 12:11:58 -08:00
|
|
|
|
|
|
|
import (
|
2018-05-26 14:21:29 -07:00
|
|
|
"bytes"
|
2018-01-25 12:11:58 -08:00
|
|
|
"encoding/hex"
|
2018-06-01 02:51:38 -07:00
|
|
|
"strconv"
|
2018-01-31 18:56:46 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-01-25 12:11:58 -08:00
|
|
|
|
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-01-25 12:11:58 -08:00
|
|
|
|
2018-03-16 12:52:39 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
2018-01-25 12:11:58 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-03-17 11:18:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2018-06-26 19:00:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
2018-01-25 12:11:58 -08:00
|
|
|
)
|
|
|
|
|
2018-03-26 07:48:15 -07:00
|
|
|
// dummy addresses used for testing
|
|
|
|
var (
|
2018-07-18 23:39:40 -07:00
|
|
|
Addrs = createTestAddrs(500)
|
|
|
|
PKs = createTestPubKeys(500)
|
2018-07-06 00:06:53 -07:00
|
|
|
emptyAddr sdk.AccAddress
|
2018-03-26 07:48:15 -07:00
|
|
|
emptyPubkey crypto.PubKey
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2018-07-06 00:06:53 -07:00
|
|
|
addrDels = []sdk.AccAddress{
|
2018-06-26 19:00:12 -07:00
|
|
|
Addrs[0],
|
|
|
|
Addrs[1],
|
|
|
|
}
|
2018-07-06 00:06:53 -07:00
|
|
|
addrVals = []sdk.AccAddress{
|
2018-06-26 19:00:12 -07:00
|
|
|
Addrs[2],
|
|
|
|
Addrs[3],
|
|
|
|
Addrs[4],
|
|
|
|
Addrs[5],
|
|
|
|
Addrs[6],
|
|
|
|
}
|
2018-03-26 07:48:15 -07:00
|
|
|
)
|
|
|
|
|
2018-05-18 15:57:47 -07:00
|
|
|
//_______________________________________________________________________________________
|
|
|
|
|
|
|
|
// intended to be used with require/assert: require.True(ValEq(...))
|
2018-06-26 19:00:12 -07:00
|
|
|
func ValEq(t *testing.T, exp, got types.Validator) (*testing.T, bool, string, types.Validator, types.Validator) {
|
|
|
|
return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp, got
|
2018-05-18 15:57:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//_______________________________________________________________________________________
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
// create a codec used only for testing
|
|
|
|
func MakeTestCodec() *wire.Codec {
|
2018-04-06 16:20:14 -07:00
|
|
|
var cdc = wire.NewCodec()
|
|
|
|
|
|
|
|
// Register Msgs
|
|
|
|
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
|
2018-04-18 21:49:24 -07:00
|
|
|
cdc.RegisterConcrete(bank.MsgSend{}, "test/stake/Send", nil)
|
|
|
|
cdc.RegisterConcrete(bank.MsgIssue{}, "test/stake/Issue", nil)
|
2018-06-26 19:00:12 -07:00
|
|
|
cdc.RegisterConcrete(types.MsgCreateValidator{}, "test/stake/CreateValidator", nil)
|
|
|
|
cdc.RegisterConcrete(types.MsgEditValidator{}, "test/stake/EditValidator", nil)
|
|
|
|
cdc.RegisterConcrete(types.MsgBeginUnbonding{}, "test/stake/BeginUnbonding", nil)
|
|
|
|
cdc.RegisterConcrete(types.MsgCompleteUnbonding{}, "test/stake/CompleteUnbonding", nil)
|
|
|
|
cdc.RegisterConcrete(types.MsgBeginRedelegate{}, "test/stake/BeginRedelegate", nil)
|
|
|
|
cdc.RegisterConcrete(types.MsgCompleteRedelegate{}, "test/stake/CompleteRedelegate", nil)
|
2018-04-06 16:20:14 -07:00
|
|
|
|
|
|
|
// Register AppAccount
|
2018-05-23 19:26:54 -07:00
|
|
|
cdc.RegisterInterface((*auth.Account)(nil), nil)
|
2018-05-23 22:09:01 -07:00
|
|
|
cdc.RegisterConcrete(&auth.BaseAccount{}, "test/stake/Account", nil)
|
2018-04-07 00:02:00 -07:00
|
|
|
wire.RegisterCrypto(cdc)
|
2018-04-06 16:20:14 -07:00
|
|
|
|
2018-03-17 11:18:04 -07:00
|
|
|
return cdc
|
|
|
|
}
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
// default params without inflation
|
|
|
|
func ParamsNoInflation() types.Params {
|
|
|
|
return types.Params{
|
2018-04-30 14:21:14 -07:00
|
|
|
InflationRateChange: sdk.ZeroRat(),
|
|
|
|
InflationMax: sdk.ZeroRat(),
|
|
|
|
InflationMin: sdk.ZeroRat(),
|
2018-03-17 11:18:04 -07:00
|
|
|
GoalBonded: sdk.NewRat(67, 100),
|
2018-03-20 14:21:18 -07:00
|
|
|
MaxValidators: 100,
|
2018-04-30 16:24:46 -07:00
|
|
|
BondDenom: "steak",
|
2018-03-17 11:18:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hogpodge of all sorts of input required for testing
|
2018-06-26 19:00:12 -07:00
|
|
|
func CreateTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context, auth.AccountMapper, Keeper) {
|
2018-05-23 19:53:42 -07:00
|
|
|
|
2018-03-17 11:18:04 -07:00
|
|
|
keyStake := sdk.NewKVStoreKey("stake")
|
2018-05-04 18:29:12 -07:00
|
|
|
keyAcc := sdk.NewKVStoreKey("acc")
|
2018-03-13 11:27:52 -07:00
|
|
|
|
2018-05-22 12:13:03 -07:00
|
|
|
db := dbm.NewMemDB()
|
2018-01-31 18:56:46 -08:00
|
|
|
ms := store.NewCommitMultiStore(db)
|
2018-03-17 11:18:04 -07:00
|
|
|
ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db)
|
2018-05-04 18:29:12 -07:00
|
|
|
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
|
2018-02-02 08:38:40 -08:00
|
|
|
err := ms.LoadLatestVersion()
|
|
|
|
require.Nil(t, err)
|
2018-03-13 11:27:52 -07:00
|
|
|
|
2018-06-21 18:03:05 -07:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, log.NewNopLogger())
|
2018-06-26 19:00:12 -07:00
|
|
|
cdc := MakeTestCodec()
|
2018-04-07 00:02:00 -07:00
|
|
|
accountMapper := auth.NewAccountMapper(
|
2018-06-25 19:05:47 -07:00
|
|
|
cdc, // amino codec
|
|
|
|
keyAcc, // target store
|
|
|
|
auth.ProtoBaseAccount, // prototype
|
2018-04-22 23:36:15 -07:00
|
|
|
)
|
2018-04-18 21:49:24 -07:00
|
|
|
ck := bank.NewKeeper(accountMapper)
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper := NewKeeper(cdc, keyStake, ck, types.DefaultCodespace)
|
|
|
|
keeper.SetPool(ctx, types.InitialPool())
|
|
|
|
keeper.SetNewParams(ctx, types.DefaultParams())
|
|
|
|
keeper.InitIntraTxCounter(ctx)
|
|
|
|
|
|
|
|
// fill all the addresses with some coins, set the loose pool tokens simultaneously
|
|
|
|
for _, addr := range Addrs {
|
|
|
|
pool := keeper.GetPool(ctx)
|
2018-06-28 15:52:10 -07:00
|
|
|
_, _, err := ck.AddCoins(ctx, addr, sdk.Coins{
|
2018-06-26 19:00:12 -07:00
|
|
|
{keeper.GetParams(ctx).BondDenom, sdk.NewInt(initCoins)},
|
2018-03-29 05:27:35 -07:00
|
|
|
})
|
2018-06-28 15:52:10 -07:00
|
|
|
require.Nil(t, err)
|
2018-07-13 13:46:14 -07:00
|
|
|
pool.LooseTokens = pool.LooseTokens.Add(sdk.NewRat(initCoins))
|
2018-06-26 19:00:12 -07:00
|
|
|
keeper.SetPool(ctx, pool)
|
2018-03-19 08:53:20 -07:00
|
|
|
}
|
|
|
|
|
2018-03-20 14:21:18 -07:00
|
|
|
return ctx, accountMapper, keeper
|
2018-01-25 12:11:58 -08:00
|
|
|
}
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
func NewPubKey(pk string) (res crypto.PubKey) {
|
2018-01-25 12:11:58 -08:00
|
|
|
pkBytes, err := hex.DecodeString(pk)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
//res, err = crypto.PubKeyFromBytes(pkBytes)
|
2018-07-25 13:43:37 -07:00
|
|
|
var pkEd ed25519.PubKeyEd25519
|
2018-01-25 12:11:58 -08:00
|
|
|
copy(pkEd[:], pkBytes[:])
|
2018-04-06 17:25:08 -07:00
|
|
|
return pkEd
|
2018-01-25 12:11:58 -08:00
|
|
|
}
|
|
|
|
|
2018-03-17 11:18:04 -07:00
|
|
|
// for incode address generation
|
2018-07-06 00:06:53 -07:00
|
|
|
func TestAddr(addr string, bech string) sdk.AccAddress {
|
2018-05-26 14:21:29 -07:00
|
|
|
|
2018-07-09 16:06:05 -07:00
|
|
|
res, err := sdk.AccAddressFromHex(addr)
|
2018-05-26 14:21:29 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-07-05 13:36:51 -07:00
|
|
|
bechexpected := res.String()
|
2018-05-26 14:21:29 -07:00
|
|
|
if bech != bechexpected {
|
|
|
|
panic("Bech encoding doesn't match reference")
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:06:05 -07:00
|
|
|
bechres, err := sdk.AccAddressFromBech32(bech)
|
2018-05-26 14:21:29 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if bytes.Compare(bechres, res) != 0 {
|
|
|
|
panic("Bech decode and hex decode don't match")
|
|
|
|
}
|
|
|
|
|
2018-03-17 11:18:04 -07:00
|
|
|
return res
|
|
|
|
}
|
2018-06-01 02:51:38 -07:00
|
|
|
|
2018-06-29 15:22:24 -07:00
|
|
|
// nolint: unparam
|
2018-07-06 00:06:53 -07:00
|
|
|
func createTestAddrs(numAddrs int) []sdk.AccAddress {
|
|
|
|
var addresses []sdk.AccAddress
|
2018-06-01 02:51:38 -07:00
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
2018-06-01 03:39:23 -07:00
|
|
|
// start at 100 so we can make up to 999 test addresses with valid test addresses
|
|
|
|
for i := 100; i < (numAddrs + 100); i++ {
|
2018-06-01 02:51:38 -07:00
|
|
|
numString := strconv.Itoa(i)
|
2018-06-01 03:39:23 -07:00
|
|
|
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") //base address string
|
2018-06-01 02:51:38 -07:00
|
|
|
|
|
|
|
buffer.WriteString(numString) //adding on final two digits to make addresses unique
|
2018-07-09 16:06:05 -07:00
|
|
|
res, _ := sdk.AccAddressFromHex(buffer.String())
|
2018-07-05 13:36:51 -07:00
|
|
|
bech := res.String()
|
2018-06-26 19:00:12 -07:00
|
|
|
addresses = append(addresses, TestAddr(buffer.String(), bech))
|
2018-06-01 02:51:38 -07:00
|
|
|
buffer.Reset()
|
|
|
|
}
|
|
|
|
return addresses
|
|
|
|
}
|
|
|
|
|
2018-06-29 15:22:24 -07:00
|
|
|
// nolint: unparam
|
2018-06-01 02:51:38 -07:00
|
|
|
func createTestPubKeys(numPubKeys int) []crypto.PubKey {
|
|
|
|
var publicKeys []crypto.PubKey
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
//start at 10 to avoid changing 1 to 01, 2 to 02, etc
|
2018-06-01 03:39:23 -07:00
|
|
|
for i := 100; i < (numPubKeys + 100); i++ {
|
2018-06-01 02:51:38 -07:00
|
|
|
numString := strconv.Itoa(i)
|
2018-06-01 03:39:23 -07:00
|
|
|
buffer.WriteString("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AF") //base pubkey string
|
|
|
|
buffer.WriteString(numString) //adding on final two digits to make pubkeys unique
|
2018-06-26 19:00:12 -07:00
|
|
|
publicKeys = append(publicKeys, NewPubKey(buffer.String()))
|
2018-06-01 02:51:38 -07:00
|
|
|
buffer.Reset()
|
|
|
|
}
|
|
|
|
return publicKeys
|
|
|
|
}
|
2018-06-26 19:00:12 -07:00
|
|
|
|
|
|
|
//_____________________________________________________________________________________
|
|
|
|
|
|
|
|
// does a certain by-power index record exist
|
|
|
|
func ValidatorByPowerIndexExists(ctx sdk.Context, keeper Keeper, power []byte) bool {
|
|
|
|
store := ctx.KVStore(keeper.storeKey)
|
|
|
|
return store.Get(power) != nil
|
|
|
|
}
|