refactor old genesis_test to use simapp
This commit is contained in:
parent
660ffc8be5
commit
399ea763ee
|
@ -1,11 +1,21 @@
|
||||||
package staking_test
|
package staking_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/hex"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
"github.com/cosmos/cosmos-sdk/simapp"
|
||||||
|
cdc "github.com/cosmos/cosmos-sdk/simapp/codec"
|
||||||
|
abci "github.com/tendermint/tendermint/abci/types"
|
||||||
"github.com/tendermint/tendermint/crypto"
|
"github.com/tendermint/tendermint/crypto"
|
||||||
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||||
"github.com/tendermint/tendermint/crypto/secp256k1"
|
"github.com/tendermint/tendermint/crypto/secp256k1"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/staking"
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,6 +27,8 @@ var (
|
||||||
addr2 = sdk.AccAddress(priv2.PubKey().Address())
|
addr2 = sdk.AccAddress(priv2.PubKey().Address())
|
||||||
|
|
||||||
commissionRates = staking.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
|
commissionRates = staking.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
|
||||||
|
|
||||||
|
PKs = createTestPubKeys(500)
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) staking.MsgCreateValidator {
|
func NewTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) staking.MsgCreateValidator {
|
||||||
|
@ -29,3 +41,57 @@ func NewTestMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amt sdk.
|
||||||
amount := sdk.NewCoin(sdk.DefaultBondDenom, amt)
|
amount := sdk.NewCoin(sdk.DefaultBondDenom, amt)
|
||||||
return staking.NewMsgDelegate(delAddr, valAddr, amount)
|
return staking.NewMsgDelegate(delAddr, valAddr, amount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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())
|
||||||
|
|
||||||
|
app.StakingKeeper = keeper.NewKeeper(
|
||||||
|
appCodec,
|
||||||
|
app.GetKey(staking.StoreKey),
|
||||||
|
app.BankKeeper,
|
||||||
|
app.SupplyKeeper,
|
||||||
|
app.GetSubspace(staking.ModuleName),
|
||||||
|
)
|
||||||
|
|
||||||
|
return codec.New(), app, ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
|
@ -1,27 +1,49 @@
|
||||||
package staking
|
package staking_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
abci "github.com/tendermint/tendermint/abci/types"
|
abci "github.com/tendermint/tendermint/abci/types"
|
||||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/simapp"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
||||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func bootstrapGenesisTest(t *testing.T, power int64, numAddrs int) (*simapp.SimApp, sdk.Context, []sdk.AccAddress, []sdk.ValAddress) {
|
||||||
|
_, app, ctx := getBaseSimappWithCustomKeeper()
|
||||||
|
|
||||||
|
addrDels, addrVals := generateAddresses(app, ctx, numAddrs)
|
||||||
|
|
||||||
|
amt := sdk.TokensFromConsensusPower(power)
|
||||||
|
totalSupply := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), amt.MulRaw(int64(len(addrDels)))))
|
||||||
|
|
||||||
|
notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx)
|
||||||
|
err := app.BankKeeper.SetBalances(ctx, notBondedPool.GetAddress(), totalSupply)
|
||||||
|
require.NoError(t, err)
|
||||||
|
app.SupplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
||||||
|
|
||||||
|
app.SupplyKeeper.SetSupply(ctx, supply.NewSupply(totalSupply))
|
||||||
|
|
||||||
|
return app, ctx, addrDels, addrVals
|
||||||
|
}
|
||||||
|
|
||||||
func TestInitGenesis(t *testing.T) {
|
func TestInitGenesis(t *testing.T) {
|
||||||
ctx, accKeeper, bk, keeper, supplyKeeper := CreateTestInput(t, false, 1000)
|
app, ctx, addrs, _ := bootstrapGenesisTest(t, 1000, 10)
|
||||||
|
|
||||||
valTokens := sdk.TokensFromConsensusPower(1)
|
valTokens := sdk.TokensFromConsensusPower(1)
|
||||||
|
|
||||||
params := keeper.GetParams(ctx)
|
params := app.StakingKeeper.GetParams(ctx)
|
||||||
validators := make([]Validator, 2)
|
validators := make([]types.Validator, 2)
|
||||||
var delegations []Delegation
|
var delegations []types.Delegation
|
||||||
|
|
||||||
pk0, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, PKs[0])
|
pk0, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, PKs[0])
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -30,33 +52,33 @@ func TestInitGenesis(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// initialize the validators
|
// initialize the validators
|
||||||
validators[0].OperatorAddress = sdk.ValAddress(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 = types.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(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 = types.NewDescription("bloop", "", "", "", "")
|
||||||
validators[1].Status = sdk.Bonded
|
validators[1].Status = sdk.Bonded
|
||||||
validators[1].Tokens = valTokens
|
validators[1].Tokens = valTokens
|
||||||
validators[1].DelegatorShares = valTokens.ToDec()
|
validators[1].DelegatorShares = valTokens.ToDec()
|
||||||
|
|
||||||
genesisState := types.NewGenesisState(params, validators, delegations)
|
genesisState := types.NewGenesisState(params, validators, delegations)
|
||||||
vals := InitGenesis(ctx, keeper, accKeeper, bk, supplyKeeper, genesisState)
|
vals := staking.InitGenesis(ctx, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.SupplyKeeper, genesisState)
|
||||||
|
|
||||||
actualGenesis := ExportGenesis(ctx, keeper)
|
actualGenesis := staking.ExportGenesis(ctx, app.StakingKeeper)
|
||||||
require.Equal(t, genesisState.Params, actualGenesis.Params)
|
require.Equal(t, genesisState.Params, actualGenesis.Params)
|
||||||
require.Equal(t, genesisState.Delegations, actualGenesis.Delegations)
|
require.Equal(t, genesisState.Delegations, actualGenesis.Delegations)
|
||||||
require.EqualValues(t, keeper.GetAllValidators(ctx), actualGenesis.Validators)
|
require.EqualValues(t, app.StakingKeeper.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(Addrs[0]))
|
resVal, found := app.StakingKeeper.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(Addrs[1]))
|
resVal, found = app.StakingKeeper.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)
|
||||||
|
|
||||||
|
@ -72,15 +94,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 := CreateTestInput(t, false, 1000)
|
app, ctx, addrs, _ := bootstrapGenesisTest(t, 1000, 200)
|
||||||
|
|
||||||
params := keeper.GetParams(ctx)
|
params := app.StakingKeeper.GetParams(ctx)
|
||||||
delegations := []Delegation{}
|
delegations := []types.Delegation{}
|
||||||
validators := make([]Validator, size)
|
validators := make([]types.Validator, size)
|
||||||
|
|
||||||
for i := range validators {
|
for i := range validators {
|
||||||
validators[i] = NewValidator(sdk.ValAddress(Addrs[i]),
|
validators[i] = types.NewValidator(sdk.ValAddress(addrs[i]),
|
||||||
PKs[i], NewDescription(fmt.Sprintf("#%d", i), "", "", "", ""))
|
PKs[i], types.NewDescription(fmt.Sprintf("#%d", i), "", "", "", ""))
|
||||||
|
|
||||||
validators[i].Status = sdk.Bonded
|
validators[i].Status = sdk.Bonded
|
||||||
|
|
||||||
|
@ -93,7 +115,7 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
genesisState := types.NewGenesisState(params, validators, delegations)
|
genesisState := types.NewGenesisState(params, validators, delegations)
|
||||||
vals := InitGenesis(ctx, keeper, accKeeper, bk, supplyKeeper, genesisState)
|
vals := staking.InitGenesis(ctx, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.SupplyKeeper, genesisState)
|
||||||
|
|
||||||
abcivals := make([]abci.ValidatorUpdate, 100)
|
abcivals := make([]abci.ValidatorUpdate, 100)
|
||||||
for i, val := range validators[:100] {
|
for i, val := range validators[:100] {
|
||||||
|
@ -138,9 +160,9 @@ func TestValidateGenesis(t *testing.T) {
|
||||||
genesisState := types.DefaultGenesisState()
|
genesisState := types.DefaultGenesisState()
|
||||||
tt.mutate(&genesisState)
|
tt.mutate(&genesisState)
|
||||||
if tt.wantErr {
|
if tt.wantErr {
|
||||||
assert.Error(t, ValidateGenesis(genesisState))
|
assert.Error(t, staking.ValidateGenesis(genesisState))
|
||||||
} else {
|
} else {
|
||||||
assert.NoError(t, ValidateGenesis(genesisState))
|
assert.NoError(t, staking.ValidateGenesis(genesisState))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue