2020-02-18 05:00:59 -08:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"strconv"
|
|
|
|
|
2020-02-21 09:49:54 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-02-18 07:44:23 -08:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2020-02-18 05:00:59 -08:00
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Addrs = createTestAddrs(500)
|
|
|
|
PKs = createTestPubKeys(500)
|
|
|
|
|
|
|
|
addrDels = []sdk.AccAddress{
|
|
|
|
Addrs[0],
|
|
|
|
Addrs[1],
|
|
|
|
}
|
|
|
|
addrVals = []sdk.ValAddress{
|
|
|
|
sdk.ValAddress(Addrs[2]),
|
|
|
|
sdk.ValAddress(Addrs[3]),
|
|
|
|
sdk.ValAddress(Addrs[4]),
|
|
|
|
sdk.ValAddress(Addrs[5]),
|
|
|
|
sdk.ValAddress(Addrs[6]),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func createTestAddrs(numAddrs int) []sdk.AccAddress {
|
|
|
|
var addresses []sdk.AccAddress
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
// start at 100 so we can make up to 999 test addresses with valid test addresses
|
|
|
|
for i := 100; i < (numAddrs + 100); i++ {
|
|
|
|
numString := strconv.Itoa(i)
|
|
|
|
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") //base address string
|
|
|
|
|
|
|
|
buffer.WriteString(numString) //adding on final two digits to make addresses unique
|
|
|
|
res, _ := sdk.AccAddressFromHex(buffer.String())
|
|
|
|
bech := res.String()
|
|
|
|
addresses = append(addresses, CreateTestAddr(buffer.String(), bech))
|
|
|
|
buffer.Reset()
|
|
|
|
}
|
|
|
|
return addresses
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-02-18 05:00:59 -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
|
|
|
|
}
|
|
|
|
|
|
|
|
// for incode address generation
|
|
|
|
func CreateTestAddr(addr string, bech string) sdk.AccAddress {
|
|
|
|
res, err := sdk.AccAddressFromHex(addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-02-19 01:53:43 -08:00
|
|
|
|
2020-02-18 05:00:59 -08:00
|
|
|
bechexpected := res.String()
|
|
|
|
if bech != bechexpected {
|
|
|
|
panic("Bech encoding doesn't match reference")
|
|
|
|
}
|
|
|
|
|
|
|
|
bechres, err := sdk.AccAddressFromBech32(bech)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(bechres, res) {
|
|
|
|
panic("Bech decode and hex decode don't match")
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
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) {
|
|
|
|
cdc := codec.New()
|
|
|
|
app := simapp.Setup(false)
|
|
|
|
ctx := app.BaseApp.NewContext(false, abci.Header{})
|
|
|
|
|
|
|
|
app.StakingKeeper = keeper.NewKeeper(
|
|
|
|
simapp.NewAppCodec().Staking,
|
|
|
|
app.GetKey(staking.StoreKey),
|
|
|
|
app.BankKeeper,
|
|
|
|
app.SupplyKeeper,
|
|
|
|
app.GetSubspace(staking.ModuleName),
|
|
|
|
)
|
|
|
|
|
|
|
|
return cdc, app, ctx
|
|
|
|
}
|