2018-01-25 12:11:58 -08:00
|
|
|
package stake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2018-01-31 18:56:46 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-01-25 12:11:58 -08:00
|
|
|
|
2018-03-13 11:27:52 -07:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2018-01-25 12:11:58 -08:00
|
|
|
crypto "github.com/tendermint/go-crypto"
|
2018-01-31 18:56:46 -08:00
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
2018-04-23 03:35:09 -07:00
|
|
|
"github.com/tendermint/tmlibs/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-01-25 12:11:58 -08:00
|
|
|
)
|
|
|
|
|
2018-03-26 07:48:15 -07:00
|
|
|
// dummy addresses used for testing
|
|
|
|
var (
|
|
|
|
addrs = []sdk.Address{
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6160"),
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6161"),
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6162"),
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6163"),
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6164"),
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6165"),
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6166"),
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6167"),
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6168"),
|
|
|
|
testAddr("A58856F0FD53BF058B4909A21AEC019107BA6169"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// dummy pubkeys used for testing
|
|
|
|
pks = []crypto.PubKey{
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB50"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB51"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB52"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB53"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB54"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB55"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB56"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB57"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB58"),
|
|
|
|
newPubKey("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AFB59"),
|
|
|
|
}
|
|
|
|
|
|
|
|
emptyAddr sdk.Address
|
|
|
|
emptyPubkey crypto.PubKey
|
|
|
|
)
|
|
|
|
|
2018-04-06 17:25:08 -07:00
|
|
|
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-04-06 16:20:14 -07:00
|
|
|
cdc.RegisterConcrete(MsgDeclareCandidacy{}, "test/stake/DeclareCandidacy", nil)
|
|
|
|
cdc.RegisterConcrete(MsgEditCandidacy{}, "test/stake/EditCandidacy", nil)
|
|
|
|
cdc.RegisterConcrete(MsgUnbond{}, "test/stake/Unbond", nil)
|
|
|
|
|
|
|
|
// Register AppAccount
|
|
|
|
cdc.RegisterInterface((*sdk.Account)(nil), nil)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func paramsNoInflation() Params {
|
|
|
|
return 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-04-01 09:05:58 -07:00
|
|
|
func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context, sdk.AccountMapper, Keeper) {
|
2018-02-02 08:38:40 -08:00
|
|
|
db := dbm.NewMemDB()
|
2018-03-17 11:18:04 -07:00
|
|
|
keyStake := sdk.NewKVStoreKey("stake")
|
2018-03-20 14:21:18 -07:00
|
|
|
keyMain := keyStake //sdk.NewKVStoreKey("main") //TODO fix multistore
|
2018-03-13 11:27:52 -07:00
|
|
|
|
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-02-02 08:38:40 -08:00
|
|
|
err := ms.LoadLatestVersion()
|
|
|
|
require.Nil(t, err)
|
2018-03-13 11:27:52 -07:00
|
|
|
|
2018-04-23 03:35:09 -07:00
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil, log.NewNopLogger())
|
2018-03-17 11:18:04 -07:00
|
|
|
cdc := makeTestCodec()
|
2018-04-07 00:02:00 -07:00
|
|
|
accountMapper := auth.NewAccountMapper(
|
|
|
|
cdc, // amino codec
|
2018-03-17 11:18:04 -07:00
|
|
|
keyMain, // target store
|
|
|
|
&auth.BaseAccount{}, // prototype
|
2018-04-22 23:36:15 -07:00
|
|
|
)
|
2018-04-18 21:49:24 -07:00
|
|
|
ck := bank.NewKeeper(accountMapper)
|
2018-04-18 09:36:55 -07:00
|
|
|
keeper := NewKeeper(cdc, keyStake, ck, DefaultCodespace)
|
2018-04-03 11:50:31 -07:00
|
|
|
keeper.setPool(ctx, initialPool())
|
|
|
|
keeper.setParams(ctx, defaultParams())
|
2018-03-20 14:21:18 -07:00
|
|
|
|
2018-03-19 08:53:20 -07:00
|
|
|
// fill all the addresses with some coins
|
|
|
|
for _, addr := range addrs {
|
2018-03-29 05:27:35 -07:00
|
|
|
ck.AddCoins(ctx, addr, sdk.Coins{
|
|
|
|
{keeper.GetParams(ctx).BondDenom, initCoins},
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func newPubKey(pk string) (res crypto.PubKey) {
|
|
|
|
pkBytes, err := hex.DecodeString(pk)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
//res, err = crypto.PubKeyFromBytes(pkBytes)
|
|
|
|
var pkEd crypto.PubKeyEd25519
|
|
|
|
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
|
|
|
|
func testAddr(addr string) sdk.Address {
|
2018-03-19 08:53:20 -07:00
|
|
|
res, err := sdk.GetAddress(addr)
|
2018-03-17 11:18:04 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|