refactor old genesis_test to use simapp

This commit is contained in:
Jonathan Gimeno 2020-02-26 10:47:39 +01:00
parent 660ffc8be5
commit 399ea763ee
3 changed files with 1565 additions and 1496 deletions

View File

@ -1,11 +1,21 @@
package staking_test
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/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
@ -17,6 +27,8 @@ var (
addr2 = sdk.AccAddress(priv2.PubKey().Address())
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 {
@ -29,3 +41,57 @@ func NewTestMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amt sdk.
amount := sdk.NewCoin(sdk.DefaultBondDenom, amt)
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
}

View File

@ -1,27 +1,49 @@
package staking
package staking_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/stretchr/testify/require"
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"
"github.com/cosmos/cosmos-sdk/x/staking"
"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) {
ctx, accKeeper, bk, keeper, supplyKeeper := CreateTestInput(t, false, 1000)
app, ctx, addrs, _ := bootstrapGenesisTest(t, 1000, 10)
valTokens := sdk.TokensFromConsensusPower(1)
params := keeper.GetParams(ctx)
validators := make([]Validator, 2)
var delegations []Delegation
params := app.StakingKeeper.GetParams(ctx)
validators := make([]types.Validator, 2)
var delegations []types.Delegation
pk0, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, PKs[0])
require.NoError(t, err)
@ -30,33 +52,33 @@ func TestInitGenesis(t *testing.T) {
require.NoError(t, err)
// initialize the validators
validators[0].OperatorAddress = sdk.ValAddress(Addrs[0])
validators[0].OperatorAddress = sdk.ValAddress(addrs[0])
validators[0].ConsensusPubkey = pk0
validators[0].Description = NewDescription("hoop", "", "", "", "")
validators[0].Description = types.NewDescription("hoop", "", "", "", "")
validators[0].Status = sdk.Bonded
validators[0].Tokens = valTokens
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].Description = NewDescription("bloop", "", "", "", "")
validators[1].Description = types.NewDescription("bloop", "", "", "", "")
validators[1].Status = sdk.Bonded
validators[1].Tokens = valTokens
validators[1].DelegatorShares = valTokens.ToDec()
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.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
resVal, found := keeper.GetValidator(ctx, sdk.ValAddress(Addrs[0]))
resVal, found := app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addrs[0]))
require.True(t, found)
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.Equal(t, sdk.Bonded, resVal.Status)
@ -72,15 +94,15 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
size := 200
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)
delegations := []Delegation{}
validators := make([]Validator, size)
params := app.StakingKeeper.GetParams(ctx)
delegations := []types.Delegation{}
validators := make([]types.Validator, size)
for i := range validators {
validators[i] = NewValidator(sdk.ValAddress(Addrs[i]),
PKs[i], NewDescription(fmt.Sprintf("#%d", i), "", "", "", ""))
validators[i] = types.NewValidator(sdk.ValAddress(addrs[i]),
PKs[i], types.NewDescription(fmt.Sprintf("#%d", i), "", "", "", ""))
validators[i].Status = sdk.Bonded
@ -93,7 +115,7 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
}
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)
for i, val := range validators[:100] {
@ -138,9 +160,9 @@ func TestValidateGenesis(t *testing.T) {
genesisState := types.DefaultGenesisState()
tt.mutate(&genesisState)
if tt.wantErr {
assert.Error(t, ValidateGenesis(genesisState))
assert.Error(t, staking.ValidateGenesis(genesisState))
} else {
assert.NoError(t, ValidateGenesis(genesisState))
assert.NoError(t, staking.ValidateGenesis(genesisState))
}
})
}

File diff suppressed because it is too large Load Diff