Skip to content

Commit

Permalink
Migrate Keeper staking tests to use simapp (#5683)
Browse files Browse the repository at this point in the history
* use simapp into keeper

* refactor TestHistoricalInfo to use simapp

* refactor historical info test

* temporal commit on querier test, it passes

* commit end of day refactoring

* rename and temp commit

* fix test query validators

* make TestQueryDelegations pass with simapp :D

* rename function

* make TestQueryRedelegations pass with simapp

* finish keeper refactor for querier in staking using simapp

* refactor delegation test TestUnbondDelegation to use simapp

* make all test pass temporary

* avoid usage of historicals

* refactor creation of the simapp

* refactor creation of simapp

* comment before creating new way to generate accounts

* make TestDelegation pass with generated accounts

* refactor to use accounts

* refactor test unbondingdelegationsmax entries

* refactor TestUndelegateSelfDelegationBelowMinSelfDelegation to use simapp

* update TestUndelegate from unbonding validator and fix bug

* refactor TestUndelegateFromUnbondedValidator to use simapp

* TestUnbondingAllDelegationFromValidator to use simapp

* refactor TestGetRedelegationsFromSrcValidator to use simapp

* refactor TestRedelegation to use simapp

* refactor TestRedelegateToSameValidator to use simapp

* refacotr TestRedelegationMaxEntries to use simapp

* refactor delegation test

* refactor TestRedelegateFromUnbondingValidator to use simapp

* finish refactor delegation test

* refactor and remove unused code

* migrate revocation of old slash test

* fix TestSlashUnbondingDelegation test to use simapp

* refactor TestSlashRedelegation to use simapp

* refactor TestSlashAtFutureHeight test

* test TestSlashAtNegativeHeight migrated to simapp

* migrated two tests from slash_test to use simapp

* refactor TestSlashWithRedelegation

* end refactoring slash_test

* refactor first test validation to simapp

* refacor TestUpdateValidatorByPowerIndex to use simapp

* refactor TestUpdateBondedValidatorsDecreaseCliff to simapp

* refactor TestSlashToZeroPowerRemoved

* TestValidatorBasics

* refactro TestGetValidatorSortingUnmixed to simapp

* refactor TestGetValidatorSortingMixed test to simap

* refctor TestGetValidatorsEdgeCases to use simapp

* make test TestValidatorBondHeight pass

* refactor TestFullValidatorSetPowerChange test

* end refactoring validator_test

* clean code

* move methods

* rename

* rename commont test

* git remove unused vars

* refactor ordering functions

* refactor old genesis_test to use simapp

* refactor TestValidatorByPowerIndex

* refactor TestDuplicatesMsgCreateValidator

* refactor TestInvalidPubKeyTypeMsgCreateValidator

* temporary commit

* refactor TestLegacyValidatorDelegations to use simapp

* refactor TestIncrementsMsgDelegate

* refactor next

* refactor TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond

* refactor TestIncrementsMsgUnbond

* refator TestMultipleMsgCreateValidator

* refactor TestMultipleMsgDelegate

* refactor TestJailValidator

* refactor TestUnbondingPeriod and TestValidatorQueue

* refactor TestUnbondingFromUnbondingValidator and TestRedelegationPeriod

* refactor TestTransitiveRedelegation and TestMultipleRedelegationAtSameTime

* refactor TestMultipleRedelegationAtUniqueTimes and TestMultipleUnbondingDelegationAtSameTime

* refactor TestMultipleUnbondingDelegationAtUniqueTimes and TestUnbondingWhenExcessValidators

* end refactor handler_test

* remove test_common

* remove create test public keys

* fix based on PR comments

* use prealloc array for ConvertAddrsToValAddrs

* fix lint errors

* fix lint errors 2

* remove duplicated func

* rename function names

* Update simapp/test_helpers.go

* Update x/staking/keeper/keeper.go

* Update simapp/test_helpers.go

* Update simapp/test_helpers.go

* add last touches to the PR

* edit text

Co-authored-by: Alexander Bezobchuk <[email protected]>
Co-authored-by: Alessio Treglia <[email protected]>
  • Loading branch information
alexanderbez and alessio authored Feb 28, 2020
2 parents 7080949 + 52c6e5e commit e358089
Show file tree
Hide file tree
Showing 17 changed files with 1,651 additions and 1,518 deletions.
111 changes: 108 additions & 3 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package simapp

import (
"bytes"
"encoding/hex"
"fmt"
"strconv"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -77,15 +81,56 @@ func SetupWithGenesisAccounts(genAccs []authexported.GenesisAccount, balances ..
return app
}

// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt
func AddTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress {
type GenerateAccountStrategy func(int) []sdk.AccAddress

// createRandomAccounts is a strategy used by addTestAddrs() in order to generated addresses in random order.
func createRandomAccounts(accNum int) []sdk.AccAddress {
testAddrs := make([]sdk.AccAddress, accNum)
for i := 0; i < accNum; i++ {
pk := ed25519.GenPrivKey().PubKey()
testAddrs[i] = sdk.AccAddress(pk.Address())
}

return testAddrs
}

// createIncrementalAccounts is a strategy used by addTestAddrs() in order to generated addresses in ascending order.
func createIncrementalAccounts(accNum 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 < (accNum + 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()
addr, _ := TestAddr(buffer.String(), bech)

addresses = append(addresses, addr)
buffer.Reset()
}

return addresses
}

// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress {
return addTestAddrs(app, ctx, accNum, accAmt, createRandomAccounts)
}

// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrsIncremental(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress {
return addTestAddrs(app, ctx, accNum, accAmt, createIncrementalAccounts)
}

func addTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int, strategy GenerateAccountStrategy) []sdk.AccAddress {
testAddrs := strategy(accNum)

initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))
totalSupply := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt.MulRaw(int64(len(testAddrs)))))
prevSupply := app.SupplyKeeper.GetSupply(ctx)
Expand All @@ -104,6 +149,38 @@ func AddTestAddrs(app *SimApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sd
return testAddrs
}

// ConvertAddrsToValAddrs converts the provided addresses to ValAddress.
func ConvertAddrsToValAddrs(addrs []sdk.AccAddress) []sdk.ValAddress {
valAddrs := make([]sdk.ValAddress, len(addrs))

for i, addr := range addrs {
valAddrs[i] = sdk.ValAddress(addr)
}

return valAddrs
}

func TestAddr(addr string, bech string) (sdk.AccAddress, error) {
res, err := sdk.AccAddressFromHex(addr)
if err != nil {
return nil, err
}
bechexpected := res.String()
if bech != bechexpected {
return nil, fmt.Errorf("bech encoding doesn't match reference")
}

bechres, err := sdk.AccAddressFromBech32(bech)
if err != nil {
return nil, err
}
if !bytes.Equal(bechres, res) {
return nil, err
}

return res, nil
}

// CheckBalance checks the balance of an account.
func CheckBalance(t *testing.T, app *SimApp, addr sdk.AccAddress, balances sdk.Coins) {
ctxCheck := app.BaseApp.NewContext(true, abci.Header{})
Expand Down Expand Up @@ -187,3 +264,31 @@ func incrementAllSequenceNumbers(initSeqNums []uint64) {
initSeqNums[i]++
}
}

// CreateTestPubKeys returns a total of numPubKeys public keys in ascending order.
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, NewPubKeyFromHex(buffer.String()))
buffer.Reset()
}

return publicKeys
}

// NewPubKeyFromHex returns a PubKey from a hex string.
func NewPubKeyFromHex(pk string) (res crypto.PubKey) {
pkBytes, err := hex.DecodeString(pk)
if err != nil {
panic(err)
}
var pkEd ed25519.PubKeyEd25519
copy(pkEd[:], pkBytes)
return pkEd
}
44 changes: 35 additions & 9 deletions x/staking/common_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package staking_test

import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
cdc "github.com/cosmos/cosmos-sdk/simapp/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

Expand All @@ -16,16 +20,10 @@ var (
addr1 = sdk.AccAddress(priv1.PubKey().Address())
priv2 = secp256k1.GenPrivKey()
addr2 = sdk.AccAddress(priv2.PubKey().Address())
addr3 = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
priv4 = secp256k1.GenPrivKey()
addr4 = sdk.AccAddress(priv4.PubKey().Address())
coins = sdk.Coins{sdk.NewCoin("foocoin", sdk.NewInt(10))}
fee = auth.NewStdFee(
100000,
sdk.Coins{sdk.NewCoin("foocoin", sdk.NewInt(0))},
)

commissionRates = staking.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())

PKs = simapp.CreateTestPubKeys(500)
)

func NewTestMsgCreateValidator(address sdk.ValAddress, pubKey crypto.PubKey, amt sdk.Int) staking.MsgCreateValidator {
Expand All @@ -38,3 +36,31 @@ func NewTestMsgDelegate(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amt sdk.
amount := sdk.NewCoin(sdk.DefaultBondDenom, amt)
return staking.NewMsgDelegate(delAddr, valAddr, amount)
}

// 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),
)
app.StakingKeeper.SetParams(ctx, types.DefaultParams())

return codec.New(), app, ctx
}

// generateAddresses generates numAddrs of normal AccAddrs and ValAddrs
func generateAddresses(app *simapp.SimApp, ctx sdk.Context, numAddrs int, accAmount int64) ([]sdk.AccAddress, []sdk.ValAddress) {
addrDels := simapp.AddTestAddrsIncremental(app, ctx, numAddrs, sdk.NewInt(accAmount))
addrVals := simapp.ConvertAddrsToValAddrs(addrDels)

return addrDels, addrVals
}
77 changes: 48 additions & 29 deletions x/staking/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,83 @@
package staking
package staking_test

import (
"fmt"
"testing"

"github.com/tendermint/tendermint/crypto/ed25519"

"github.com/stretchr/testify/assert"

"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"
keep "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"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, 10000)

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 := keep.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, keep.PKs[0])
pk0, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, PKs[0])
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)

// initialize the validators
validators[0].OperatorAddress = sdk.ValAddress(keep.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(keep.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(keep.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(keep.Addrs[1]))
resVal, found = app.StakingKeeper.GetValidator(ctx, sdk.ValAddress(addrs[1]))
require.True(t, found)
require.Equal(t, sdk.Bonded, resVal.Status)

Expand All @@ -74,15 +93,15 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
size := 200
require.True(t, size > 100)

ctx, accKeeper, bk, keeper, supplyKeeper := keep.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(keep.Addrs[i]),
keep.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

Expand All @@ -95,7 +114,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] {
Expand Down Expand Up @@ -140,9 +159,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))
}
})
}
Expand Down
Loading

0 comments on commit e358089

Please sign in to comment.