176 lines
5.2 KiB
Go
176 lines
5.2 KiB
Go
package stake
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/abci/types"
|
|
crypto "github.com/tendermint/go-crypto"
|
|
dbm "github.com/tendermint/tmlibs/db"
|
|
|
|
"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"
|
|
)
|
|
|
|
// 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),
|
|
}
|
|
}
|
|
|
|
// get raw genesis raw message for testing
|
|
func GetDefaultGenesisState() GenesisState {
|
|
return GenesisState{
|
|
Pool: initialPool(),
|
|
Params: defaultParams(),
|
|
}
|
|
}
|
|
|
|
// XXX reference the common declaration of this function
|
|
func subspace(prefix []byte) (start, end []byte) {
|
|
end = make([]byte, len(prefix))
|
|
copy(end, prefix)
|
|
end[len(end)-1]++
|
|
return prefix, end
|
|
}
|
|
|
|
func makeTestCodec() *wire.Codec {
|
|
var cdc = wire.NewCodec()
|
|
|
|
// Register Msgs
|
|
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
|
|
cdc.RegisterConcrete(bank.MsgSend{}, "test/stake/Send", nil)
|
|
cdc.RegisterConcrete(bank.MsgIssue{}, "test/stake/Issue", nil)
|
|
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)
|
|
wire.RegisterCrypto(cdc)
|
|
|
|
return cdc
|
|
}
|
|
|
|
func paramsNoInflation() Params {
|
|
return Params{
|
|
InflationRateChange: sdk.ZeroRat,
|
|
InflationMax: sdk.ZeroRat,
|
|
InflationMin: sdk.ZeroRat,
|
|
GoalBonded: sdk.NewRat(67, 100),
|
|
MaxValidators: 100,
|
|
BondDenom: "fermion",
|
|
}
|
|
}
|
|
|
|
// hogpodge of all sorts of input required for testing
|
|
func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context, sdk.AccountMapper, Keeper) {
|
|
db := dbm.NewMemDB()
|
|
keyStake := sdk.NewKVStoreKey("stake")
|
|
keyMain := keyStake //sdk.NewKVStoreKey("main") //TODO fix multistore
|
|
|
|
ms := store.NewCommitMultiStore(db)
|
|
ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db)
|
|
err := ms.LoadLatestVersion()
|
|
require.Nil(t, err)
|
|
|
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, nil)
|
|
cdc := makeTestCodec()
|
|
accountMapper := auth.NewAccountMapper(
|
|
cdc, // amino codec
|
|
keyMain, // target store
|
|
&auth.BaseAccount{}, // prototype
|
|
)
|
|
ck := bank.NewKeeper(accountMapper)
|
|
keeper := NewKeeper(cdc, keyStake, ck, DefaultCodespace)
|
|
keeper.setPool(ctx, initialPool())
|
|
keeper.setParams(ctx, defaultParams())
|
|
|
|
// fill all the addresses with some coins
|
|
for _, addr := range addrs {
|
|
ck.AddCoins(ctx, addr, sdk.Coins{
|
|
{keeper.GetParams(ctx).BondDenom, initCoins},
|
|
})
|
|
}
|
|
|
|
return ctx, accountMapper, keeper
|
|
}
|
|
|
|
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[:])
|
|
return pkEd
|
|
}
|
|
|
|
// for incode address generation
|
|
func testAddr(addr string) sdk.Address {
|
|
res, err := sdk.GetAddress(addr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return res
|
|
}
|