cosmos-sdk/x/staking/keeper/common_test.go

85 lines
2.5 KiB
Go
Raw Normal View History

package keeper_test
import (
"bytes"
"encoding/hex"
"strconv"
"testing"
2020-02-21 09:54:10 -08:00
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
2020-02-21 09:49:54 -08:00
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
cdc "github.com/cosmos/cosmos-sdk/simapp/codec"
2020-02-21 09:54:10 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2020-02-21 09:49:54 -08:00
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
2020-02-21 09:49:54 -08:00
abci "github.com/tendermint/tendermint/abci/types"
)
var (
2020-02-24 07:14:16 -08:00
PKs = createTestPubKeys(500)
)
// nolint: unparam
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
for i := 100; i < (numPubKeys + 100); i++ {
numString := strconv.Itoa(i)
buffer.WriteString("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AF") //base pubkey string
buffer.WriteString(numString) //adding on final two digits to make pubkeys unique
publicKeys = append(publicKeys, NewPubKey(buffer.String()))
buffer.Reset()
}
2020-02-19 01:53:43 -08:00
return publicKeys
}
func NewPubKey(pk string) (res crypto.PubKey) {
pkBytes, err := hex.DecodeString(pk)
if err != nil {
panic(err)
}
//res, err = crypto.PubKeyFromBytes(pkBytes)
var pkEd ed25519.PubKeyEd25519
copy(pkEd[:], pkBytes)
return pkEd
}
2020-02-21 09:49:54 -08:00
// getBaseSimappWithCustomKeeper Returns a simapp with custom StakingKeeper
// to avoid messing with the hooks.
func getBaseSimappWithCustomKeeper() (*codec.Codec, *simapp.SimApp, sdk.Context) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})
appCodec := cdc.NewAppCodec(codec.New())
2020-02-21 09:49:54 -08:00
app.StakingKeeper = keeper.NewKeeper(
appCodec,
2020-02-21 09:49:54 -08:00
app.GetKey(staking.StoreKey),
app.BankKeeper,
app.SupplyKeeper,
app.GetSubspace(staking.ModuleName),
)
return codec.New(), app, ctx
2020-02-21 09:49:54 -08:00
}
// intended to be used with require/assert: require.True(ValEq(...))
func ValEq(t *testing.T, exp, got types.Validator) (*testing.T, bool, string, types.Validator, types.Validator) {
return t, exp.MinEqual(got), "expected:\n%v\ngot:\n%v", exp, got
}
// generateAddresses generates numAddrs of normal AccAddrs and ValAddrs
func generateAddresses(app *simapp.SimApp, ctx sdk.Context, numAddrs int) ([]sdk.AccAddress, []sdk.ValAddress) {
addrDels := simapp.AddTestAddrsIncremental(app, ctx, numAddrs, sdk.NewInt(10000))
addrVals := simapp.ConvertAddrsToValAddrs(addrDels)
return addrDels, addrVals
}