move methods
This commit is contained in:
parent
8f779b12b8
commit
632e65f575
|
@ -11,12 +11,11 @@ import (
|
||||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
keep "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInitGenesis(t *testing.T) {
|
func TestInitGenesis(t *testing.T) {
|
||||||
ctx, accKeeper, bk, keeper, supplyKeeper := keep.CreateTestInput(t, false, 1000)
|
ctx, accKeeper, bk, keeper, supplyKeeper := CreateTestInput(t, false, 1000)
|
||||||
|
|
||||||
valTokens := sdk.TokensFromConsensusPower(1)
|
valTokens := sdk.TokensFromConsensusPower(1)
|
||||||
|
|
||||||
|
@ -24,20 +23,20 @@ func TestInitGenesis(t *testing.T) {
|
||||||
validators := make([]Validator, 2)
|
validators := make([]Validator, 2)
|
||||||
var delegations []Delegation
|
var delegations []Delegation
|
||||||
|
|
||||||
pk0, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, keep.PKs[0])
|
pk0, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, PKs[0])
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
pk1, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, keep.PKs[1])
|
pk1, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, PKs[1])
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// initialize the validators
|
// initialize the validators
|
||||||
validators[0].OperatorAddress = sdk.ValAddress(keep.Addrs[0])
|
validators[0].OperatorAddress = sdk.ValAddress(Addrs[0])
|
||||||
validators[0].ConsensusPubkey = pk0
|
validators[0].ConsensusPubkey = pk0
|
||||||
validators[0].Description = NewDescription("hoop", "", "", "", "")
|
validators[0].Description = NewDescription("hoop", "", "", "", "")
|
||||||
validators[0].Status = sdk.Bonded
|
validators[0].Status = sdk.Bonded
|
||||||
validators[0].Tokens = valTokens
|
validators[0].Tokens = valTokens
|
||||||
validators[0].DelegatorShares = valTokens.ToDec()
|
validators[0].DelegatorShares = valTokens.ToDec()
|
||||||
validators[1].OperatorAddress = sdk.ValAddress(keep.Addrs[1])
|
validators[1].OperatorAddress = sdk.ValAddress(Addrs[1])
|
||||||
validators[1].ConsensusPubkey = pk1
|
validators[1].ConsensusPubkey = pk1
|
||||||
validators[1].Description = NewDescription("bloop", "", "", "", "")
|
validators[1].Description = NewDescription("bloop", "", "", "", "")
|
||||||
validators[1].Status = sdk.Bonded
|
validators[1].Status = sdk.Bonded
|
||||||
|
@ -53,11 +52,11 @@ func TestInitGenesis(t *testing.T) {
|
||||||
require.EqualValues(t, keeper.GetAllValidators(ctx), actualGenesis.Validators)
|
require.EqualValues(t, keeper.GetAllValidators(ctx), actualGenesis.Validators)
|
||||||
|
|
||||||
// now make sure the validators are bonded and intra-tx counters are correct
|
// now make sure the validators are bonded and intra-tx counters are correct
|
||||||
resVal, found := keeper.GetValidator(ctx, sdk.ValAddress(keep.Addrs[0]))
|
resVal, found := keeper.GetValidator(ctx, sdk.ValAddress(Addrs[0]))
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
require.Equal(t, sdk.Bonded, resVal.Status)
|
require.Equal(t, sdk.Bonded, resVal.Status)
|
||||||
|
|
||||||
resVal, found = keeper.GetValidator(ctx, sdk.ValAddress(keep.Addrs[1]))
|
resVal, found = keeper.GetValidator(ctx, sdk.ValAddress(Addrs[1]))
|
||||||
require.True(t, found)
|
require.True(t, found)
|
||||||
require.Equal(t, sdk.Bonded, resVal.Status)
|
require.Equal(t, sdk.Bonded, resVal.Status)
|
||||||
|
|
||||||
|
@ -73,15 +72,15 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
|
||||||
size := 200
|
size := 200
|
||||||
require.True(t, size > 100)
|
require.True(t, size > 100)
|
||||||
|
|
||||||
ctx, accKeeper, bk, keeper, supplyKeeper := keep.CreateTestInput(t, false, 1000)
|
ctx, accKeeper, bk, keeper, supplyKeeper := CreateTestInput(t, false, 1000)
|
||||||
|
|
||||||
params := keeper.GetParams(ctx)
|
params := keeper.GetParams(ctx)
|
||||||
delegations := []Delegation{}
|
delegations := []Delegation{}
|
||||||
validators := make([]Validator, size)
|
validators := make([]Validator, size)
|
||||||
|
|
||||||
for i := range validators {
|
for i := range validators {
|
||||||
validators[i] = NewValidator(sdk.ValAddress(keep.Addrs[i]),
|
validators[i] = NewValidator(sdk.ValAddress(Addrs[i]),
|
||||||
keep.PKs[i], NewDescription(fmt.Sprintf("#%d", i), "", "", "", ""))
|
PKs[i], NewDescription(fmt.Sprintf("#%d", i), "", "", "", ""))
|
||||||
|
|
||||||
validators[i].Status = sdk.Bonded
|
validators[i].Status = sdk.Bonded
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -2,220 +2,12 @@ package keeper // noalias
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
abci "github.com/tendermint/tendermint/abci/types"
|
|
||||||
"github.com/tendermint/tendermint/crypto"
|
|
||||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
|
||||||
"github.com/tendermint/tendermint/libs/log"
|
|
||||||
tmtypes "github.com/tendermint/tendermint/types"
|
|
||||||
dbm "github.com/tendermint/tm-db"
|
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
|
||||||
"github.com/cosmos/cosmos-sdk/store"
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
||||||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/params"
|
|
||||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/supply"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// dummy addresses used for testing
|
|
||||||
// nolint:unused, deadcode
|
|
||||||
var (
|
|
||||||
Addrs = createTestAddrs(500)
|
|
||||||
PKs = createTestPubKeys(500)
|
|
||||||
)
|
|
||||||
|
|
||||||
// create a codec used only for testing
|
|
||||||
func MakeTestCodec() *codec.Codec {
|
|
||||||
var cdc = codec.New()
|
|
||||||
|
|
||||||
// Register Msgs
|
|
||||||
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
|
|
||||||
cdc.RegisterConcrete(bank.MsgSend{}, "test/staking/Send", nil)
|
|
||||||
cdc.RegisterConcrete(types.MsgCreateValidator{}, "test/staking/CreateValidator", nil)
|
|
||||||
cdc.RegisterConcrete(types.MsgEditValidator{}, "test/staking/EditValidator", nil)
|
|
||||||
cdc.RegisterConcrete(types.MsgUndelegate{}, "test/staking/Undelegate", nil)
|
|
||||||
cdc.RegisterConcrete(types.MsgBeginRedelegate{}, "test/staking/BeginRedelegate", nil)
|
|
||||||
|
|
||||||
// Register AppAccount
|
|
||||||
cdc.RegisterInterface((*authexported.Account)(nil), nil)
|
|
||||||
cdc.RegisterConcrete(&auth.BaseAccount{}, "test/staking/BaseAccount", nil)
|
|
||||||
supply.RegisterCodec(cdc)
|
|
||||||
codec.RegisterCrypto(cdc)
|
|
||||||
|
|
||||||
return cdc
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hogpodge of all sorts of input required for testing.
|
|
||||||
// `initPower` is converted to an amount of tokens.
|
|
||||||
// If `initPower` is 0, no addrs get created.
|
|
||||||
func CreateTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context, auth.AccountKeeper, types.BankKeeper, Keeper, types.SupplyKeeper) {
|
|
||||||
keyStaking := sdk.NewKVStoreKey(types.StoreKey)
|
|
||||||
keyAcc := sdk.NewKVStoreKey(auth.StoreKey)
|
|
||||||
bankKey := sdk.NewKVStoreKey(bank.StoreKey)
|
|
||||||
keyParams := sdk.NewKVStoreKey(params.StoreKey)
|
|
||||||
keySupply := sdk.NewKVStoreKey(supply.StoreKey)
|
|
||||||
|
|
||||||
tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey)
|
|
||||||
|
|
||||||
db := dbm.NewMemDB()
|
|
||||||
ms := store.NewCommitMultiStore(db)
|
|
||||||
ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db)
|
|
||||||
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
|
|
||||||
ms.MountStoreWithDB(bankKey, sdk.StoreTypeIAVL, db)
|
|
||||||
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
|
|
||||||
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
|
|
||||||
ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db)
|
|
||||||
err := ms.LoadLatestVersion()
|
|
||||||
require.Nil(t, err)
|
|
||||||
|
|
||||||
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, log.NewNopLogger())
|
|
||||||
ctx = ctx.WithConsensusParams(
|
|
||||||
&abci.ConsensusParams{
|
|
||||||
Validator: &abci.ValidatorParams{
|
|
||||||
PubKeyTypes: []string{tmtypes.ABCIPubKeyTypeEd25519},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
cdc := MakeTestCodec()
|
|
||||||
|
|
||||||
feeCollectorAcc := supply.NewEmptyModuleAccount(auth.FeeCollectorName)
|
|
||||||
notBondedPool := supply.NewEmptyModuleAccount(types.NotBondedPoolName, supply.Burner, supply.Staking)
|
|
||||||
bondPool := supply.NewEmptyModuleAccount(types.BondedPoolName, supply.Burner, supply.Staking)
|
|
||||||
|
|
||||||
blacklistedAddrs := make(map[string]bool)
|
|
||||||
blacklistedAddrs[feeCollectorAcc.GetAddress().String()] = true
|
|
||||||
blacklistedAddrs[notBondedPool.GetAddress().String()] = true
|
|
||||||
blacklistedAddrs[bondPool.GetAddress().String()] = true
|
|
||||||
|
|
||||||
pk := params.NewKeeper(params.ModuleCdc, keyParams, tkeyParams)
|
|
||||||
|
|
||||||
accountKeeper := auth.NewAccountKeeper(
|
|
||||||
cdc, // amino codec
|
|
||||||
keyAcc, // target store
|
|
||||||
pk.Subspace(auth.DefaultParamspace),
|
|
||||||
auth.ProtoBaseAccount, // prototype
|
|
||||||
)
|
|
||||||
|
|
||||||
bk := bank.NewBaseKeeper(
|
|
||||||
cdc,
|
|
||||||
bankKey,
|
|
||||||
accountKeeper,
|
|
||||||
pk.Subspace(bank.DefaultParamspace),
|
|
||||||
blacklistedAddrs,
|
|
||||||
)
|
|
||||||
|
|
||||||
maccPerms := map[string][]string{
|
|
||||||
auth.FeeCollectorName: nil,
|
|
||||||
types.NotBondedPoolName: {supply.Burner, supply.Staking},
|
|
||||||
types.BondedPoolName: {supply.Burner, supply.Staking},
|
|
||||||
}
|
|
||||||
supplyKeeper := supply.NewKeeper(cdc, keySupply, accountKeeper, bk, maccPerms)
|
|
||||||
|
|
||||||
initTokens := sdk.TokensFromConsensusPower(initPower)
|
|
||||||
initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))
|
|
||||||
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens.MulRaw(int64(len(Addrs)))))
|
|
||||||
|
|
||||||
supplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply))
|
|
||||||
|
|
||||||
keeper := NewKeeper(types.ModuleCdc, keyStaking, bk, supplyKeeper, pk.Subspace(DefaultParamspace))
|
|
||||||
keeper.SetParams(ctx, types.DefaultParams())
|
|
||||||
|
|
||||||
// set module accounts
|
|
||||||
require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), totalSupply))
|
|
||||||
|
|
||||||
supplyKeeper.SetModuleAccount(ctx, feeCollectorAcc)
|
|
||||||
supplyKeeper.SetModuleAccount(ctx, bondPool)
|
|
||||||
supplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
|
||||||
|
|
||||||
// fill all the addresses with some coins, set the loose pool tokens simultaneously
|
|
||||||
for i, addr := range Addrs {
|
|
||||||
accountKeeper.SetAccount(ctx, auth.NewBaseAccount(addr, PKs[i], uint64(i), 0))
|
|
||||||
require.NoError(t, bk.SetBalances(ctx, addr, initCoins))
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctx, accountKeeper, bk, keeper, supplyKeeper
|
|
||||||
}
|
|
||||||
|
|
||||||
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 TestAddr(addr string, bech string) sdk.AccAddress {
|
|
||||||
|
|
||||||
res, err := sdk.AccAddressFromHex(addr)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// nolint: unparam
|
|
||||||
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, TestAddr(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()
|
|
||||||
}
|
|
||||||
return publicKeys
|
|
||||||
}
|
|
||||||
|
|
||||||
//_____________________________________________________________________________________
|
|
||||||
|
|
||||||
// does a certain by-power index record exist
|
// does a certain by-power index record exist
|
||||||
func ValidatorByPowerIndexExists(ctx sdk.Context, keeper Keeper, power []byte) bool {
|
func ValidatorByPowerIndexExists(ctx sdk.Context, keeper Keeper, power []byte) bool {
|
||||||
store := ctx.KVStore(keeper.storeKey)
|
store := ctx.KVStore(keeper.storeKey)
|
||||||
|
|
|
@ -0,0 +1,217 @@
|
||||||
|
package staking
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
abci "github.com/tendermint/tendermint/abci/types"
|
||||||
|
"github.com/tendermint/tendermint/crypto"
|
||||||
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||||
|
"github.com/tendermint/tendermint/libs/log"
|
||||||
|
tmtypes "github.com/tendermint/tendermint/types"
|
||||||
|
dbm "github.com/tendermint/tm-db"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
"github.com/cosmos/cosmos-sdk/store"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/params"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
||||||
|
)
|
||||||
|
|
||||||
|
// dummy addresses used for testing
|
||||||
|
// nolint:unused, deadcode
|
||||||
|
var (
|
||||||
|
Addrs = createTestAddrs(500)
|
||||||
|
PKs = createTestPubKeys(500)
|
||||||
|
)
|
||||||
|
|
||||||
|
// nolint: unparam
|
||||||
|
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, TestAddr(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()
|
||||||
|
}
|
||||||
|
return publicKeys
|
||||||
|
}
|
||||||
|
|
||||||
|
//_____________________________________________________________________________________
|
||||||
|
|
||||||
|
// Hogpodge of all sorts of input required for testing.
|
||||||
|
// `initPower` is converted to an amount of tokens.
|
||||||
|
// If `initPower` is 0, no addrs get created.
|
||||||
|
func CreateTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context, auth.AccountKeeper, types.BankKeeper, Keeper, types.SupplyKeeper) {
|
||||||
|
keyStaking := sdk.NewKVStoreKey(types.StoreKey)
|
||||||
|
keyAcc := sdk.NewKVStoreKey(auth.StoreKey)
|
||||||
|
bankKey := sdk.NewKVStoreKey(bank.StoreKey)
|
||||||
|
keyParams := sdk.NewKVStoreKey(params.StoreKey)
|
||||||
|
keySupply := sdk.NewKVStoreKey(supply.StoreKey)
|
||||||
|
|
||||||
|
tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey)
|
||||||
|
|
||||||
|
db := dbm.NewMemDB()
|
||||||
|
ms := store.NewCommitMultiStore(db)
|
||||||
|
ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db)
|
||||||
|
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
|
||||||
|
ms.MountStoreWithDB(bankKey, sdk.StoreTypeIAVL, db)
|
||||||
|
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
|
||||||
|
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
|
||||||
|
ms.MountStoreWithDB(keySupply, sdk.StoreTypeIAVL, db)
|
||||||
|
err := ms.LoadLatestVersion()
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, log.NewNopLogger())
|
||||||
|
ctx = ctx.WithConsensusParams(
|
||||||
|
&abci.ConsensusParams{
|
||||||
|
Validator: &abci.ValidatorParams{
|
||||||
|
PubKeyTypes: []string{tmtypes.ABCIPubKeyTypeEd25519},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
cdc := MakeTestCodec()
|
||||||
|
|
||||||
|
feeCollectorAcc := supply.NewEmptyModuleAccount(auth.FeeCollectorName)
|
||||||
|
notBondedPool := supply.NewEmptyModuleAccount(types.NotBondedPoolName, supply.Burner, supply.Staking)
|
||||||
|
bondPool := supply.NewEmptyModuleAccount(types.BondedPoolName, supply.Burner, supply.Staking)
|
||||||
|
|
||||||
|
blacklistedAddrs := make(map[string]bool)
|
||||||
|
blacklistedAddrs[feeCollectorAcc.GetAddress().String()] = true
|
||||||
|
blacklistedAddrs[notBondedPool.GetAddress().String()] = true
|
||||||
|
blacklistedAddrs[bondPool.GetAddress().String()] = true
|
||||||
|
|
||||||
|
pk := params.NewKeeper(params.ModuleCdc, keyParams, tkeyParams)
|
||||||
|
|
||||||
|
accountKeeper := auth.NewAccountKeeper(
|
||||||
|
cdc, // amino codec
|
||||||
|
keyAcc, // target store
|
||||||
|
pk.Subspace(auth.DefaultParamspace),
|
||||||
|
auth.ProtoBaseAccount, // prototype
|
||||||
|
)
|
||||||
|
|
||||||
|
bk := bank.NewBaseKeeper(
|
||||||
|
cdc,
|
||||||
|
bankKey,
|
||||||
|
accountKeeper,
|
||||||
|
pk.Subspace(bank.DefaultParamspace),
|
||||||
|
blacklistedAddrs,
|
||||||
|
)
|
||||||
|
|
||||||
|
maccPerms := map[string][]string{
|
||||||
|
auth.FeeCollectorName: nil,
|
||||||
|
types.NotBondedPoolName: {supply.Burner, supply.Staking},
|
||||||
|
types.BondedPoolName: {supply.Burner, supply.Staking},
|
||||||
|
}
|
||||||
|
supplyKeeper := supply.NewKeeper(cdc, keySupply, accountKeeper, bk, maccPerms)
|
||||||
|
|
||||||
|
initTokens := sdk.TokensFromConsensusPower(initPower)
|
||||||
|
initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))
|
||||||
|
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens.MulRaw(int64(len(Addrs)))))
|
||||||
|
|
||||||
|
supplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply))
|
||||||
|
|
||||||
|
keeper := NewKeeper(types.ModuleCdc, keyStaking, bk, supplyKeeper, pk.Subspace(DefaultParamspace))
|
||||||
|
keeper.SetParams(ctx, types.DefaultParams())
|
||||||
|
|
||||||
|
// set module accounts
|
||||||
|
require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), totalSupply))
|
||||||
|
|
||||||
|
supplyKeeper.SetModuleAccount(ctx, feeCollectorAcc)
|
||||||
|
supplyKeeper.SetModuleAccount(ctx, bondPool)
|
||||||
|
supplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
||||||
|
|
||||||
|
// fill all the addresses with some coins, set the loose pool tokens simultaneously
|
||||||
|
for i, addr := range Addrs {
|
||||||
|
accountKeeper.SetAccount(ctx, auth.NewBaseAccount(addr, PKs[i], uint64(i), 0))
|
||||||
|
require.NoError(t, bk.SetBalances(ctx, addr, initCoins))
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx, accountKeeper, bk, keeper, supplyKeeper
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a codec used only for testing
|
||||||
|
func MakeTestCodec() *codec.Codec {
|
||||||
|
var cdc = codec.New()
|
||||||
|
|
||||||
|
// Register Msgs
|
||||||
|
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
|
||||||
|
cdc.RegisterConcrete(bank.MsgSend{}, "test/staking/Send", nil)
|
||||||
|
cdc.RegisterConcrete(types.MsgCreateValidator{}, "test/staking/CreateValidator", nil)
|
||||||
|
cdc.RegisterConcrete(types.MsgEditValidator{}, "test/staking/EditValidator", nil)
|
||||||
|
cdc.RegisterConcrete(types.MsgUndelegate{}, "test/staking/Undelegate", nil)
|
||||||
|
cdc.RegisterConcrete(types.MsgBeginRedelegate{}, "test/staking/BeginRedelegate", nil)
|
||||||
|
|
||||||
|
// Register AppAccount
|
||||||
|
cdc.RegisterInterface((*authexported.Account)(nil), nil)
|
||||||
|
cdc.RegisterConcrete(&auth.BaseAccount{}, "test/staking/BaseAccount", nil)
|
||||||
|
supply.RegisterCodec(cdc)
|
||||||
|
codec.RegisterCrypto(cdc)
|
||||||
|
|
||||||
|
return cdc
|
||||||
|
}
|
||||||
|
|
||||||
|
// for incode address generation
|
||||||
|
func TestAddr(addr string, bech string) sdk.AccAddress {
|
||||||
|
|
||||||
|
res, err := sdk.AccAddressFromHex(addr)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue