cosmos-sdk/x/stake/test_common.go

176 lines
5.2 KiB
Go
Raw Normal View History

2018-01-25 12:11:58 -08:00
package stake
import (
"encoding/hex"
"testing"
"github.com/stretchr/testify/require"
2018-01-25 12:11:58 -08:00
abci "github.com/tendermint/abci/types"
2018-01-25 12:11:58 -08:00
crypto "github.com/tendermint/go-crypto"
dbm "github.com/tendermint/tmlibs/db"
2018-01-25 12:11:58 -08: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
)
// default params for testing
func defaultParams() Params {
return Params{
InflationRateChange: sdk.NewRat(13, 100),
InflationMax: sdk.NewRat(20, 100),
InflationMin: sdk.NewRat(7, 100),
GoalBonded: sdk.NewRat(67, 100),
MaxValidators: 100,
BondDenom: "fermion",
}
}
// initial pool for testing
func initialPool() Pool {
return Pool{
TotalSupply: 0,
BondedShares: sdk.ZeroRat,
UnbondedShares: sdk.ZeroRat,
BondedPool: 0,
UnbondedPool: 0,
InflationLastTime: 0,
Inflation: sdk.NewRat(7, 100),
}
}
2018-04-06 22:50:46 -07:00
// get raw genesis raw message for testing
func GetDefaultGenesisState() GenesisState {
return GenesisState{
Pool: initialPool(),
Params: defaultParams(),
}
2018-04-06 22:50:46 -07:00
}
2018-03-26 07:48:15 -07:00
// XXX reference the common declaration of this function
2018-01-25 13:11:40 -08:00
func subspace(prefix []byte) (start, end []byte) {
2018-02-02 08:51:08 -08:00
end = make([]byte, len(prefix))
copy(end, prefix)
2018-01-25 13:11:40 -08:00
end[len(end)-1]++
2018-02-02 08:51:08 -08:00
return prefix, end
2018-01-25 13:11:40 -08:00
}
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{
InflationRateChange: sdk.ZeroRat,
InflationMax: sdk.ZeroRat,
InflationMin: sdk.ZeroRat,
GoalBonded: sdk.NewRat(67, 100),
2018-03-20 14:21:18 -07:00
MaxValidators: 100,
2018-03-17 11:18:04 -07:00
BondDenom: "fermion",
}
}
// 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
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)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil)
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-07 00:02:00 -07:00
).Seal()
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
}