From 0d24d2b2cbc6c8e7183b7152271c1705739091b8 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Thu, 20 Feb 2020 16:09:25 +0100 Subject: [PATCH] fix test query validators --- simapp/test_helpers.go | 30 +++++++++++++ x/staking/keeper/old_test_common.go | 3 +- x/staking/keeper/querier_test.go | 70 ++++++++++++++++------------- 3 files changed, 70 insertions(+), 33 deletions(-) diff --git a/simapp/test_helpers.go b/simapp/test_helpers.go index abc87ed63..31b4db3b8 100644 --- a/simapp/test_helpers.go +++ b/simapp/test_helpers.go @@ -1,6 +1,9 @@ package simapp import ( + "bytes" + "encoding/hex" + "strconv" "testing" "github.com/stretchr/testify/require" @@ -187,3 +190,30 @@ func incrementAllSequenceNumbers(initSeqNums []uint64) { initSeqNums[i]++ } } + +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 +} diff --git a/x/staking/keeper/old_test_common.go b/x/staking/keeper/old_test_common.go index 005cfbfb1..8e4cd6b9a 100644 --- a/x/staking/keeper/old_test_common.go +++ b/x/staking/keeper/old_test_common.go @@ -83,9 +83,10 @@ func CreateTestInput(t *testing.T, isCheckTx bool, initPower int64) (sdk.Context keyAcc := sdk.NewKVStoreKey(auth.StoreKey) bankKey := sdk.NewKVStoreKey(bank.StoreKey) keyParams := sdk.NewKVStoreKey(params.StoreKey) - tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey) keySupply := sdk.NewKVStoreKey(supply.StoreKey) + tkeyParams := sdk.NewTransientStoreKey(params.TStoreKey) + db := dbm.NewMemDB() ms := store.NewCommitMultiStore(db) ms.MountStoreWithDB(keyStaking, sdk.StoreTypeIAVL, db) diff --git a/x/staking/keeper/querier_test.go b/x/staking/keeper/querier_test.go index 4f348a429..333b3b28e 100644 --- a/x/staking/keeper/querier_test.go +++ b/x/staking/keeper/querier_test.go @@ -141,8 +141,7 @@ func TestQueryValidators(t *testing.T) { params := app.StakingKeeper.GetParams(ctx) querier := staking.NewQuerier(app.StakingKeeper) - addrs := simapp.AddTestAddrs(app, ctx, 500, sdk.NewInt(10000)) - addrVal1, _ := sdk.ValAddress(addrs[0]), sdk.ValAddress(addrs[1]) + addrs := simapp.AddTestAddrs(app, ctx, 500, sdk.TokensFromConsensusPower(10000)) // Create Validators amts := []sdk.Int{sdk.NewInt(9), sdk.NewInt(8), sdk.NewInt(7)} @@ -160,6 +159,7 @@ func TestQueryValidators(t *testing.T) { // Query Validators queriedValidators := app.StakingKeeper.GetValidators(ctx, params.MaxValidators) + require.Len(t, queriedValidators, 3) for i, s := range status { queryValsParams := types.NewQueryValidatorsParams(1, int(params.MaxValidators), s.String()) @@ -180,26 +180,27 @@ func TestQueryValidators(t *testing.T) { require.Equal(t, 1, len(validatorsResp)) require.ElementsMatch(t, validators[i].OperatorAddress, validatorsResp[0].OperatorAddress) - } // Query each validator - queryParams := types.NewQueryValidatorParams(addrVal1) - bz, err := cdc.MarshalJSON(queryParams) - require.NoError(t, err) + for _, validator := range validators { + queryParams := types.NewQueryValidatorParams(validator.OperatorAddress) + bz, err := cdc.MarshalJSON(queryParams) + require.NoError(t, err) - query := abci.RequestQuery{ - Path: "/custom/staking/validator", - Data: bz, + query := abci.RequestQuery{ + Path: "/custom/staking/validator", + Data: bz, + } + res, err := querier(ctx, []string{types.QueryValidator}, query) + require.NoError(t, err) + + var queriedValidator types.Validator + err = cdc.UnmarshalJSON(res, &queriedValidator) + require.NoError(t, err) + + require.Equal(t, validator, queriedValidator) } - res, err := querier(ctx, []string{types.QueryValidator}, query) - require.NoError(t, err) - - var validator types.Validator - err = cdc.UnmarshalJSON(res, &validator) - require.NoError(t, err) - - require.Equal(t, queriedValidators[0], validator) } func TestQueryDelegation(t *testing.T) { @@ -209,8 +210,12 @@ func TestQueryDelegation(t *testing.T) { params := app.StakingKeeper.GetParams(ctx) querier := staking.NewQuerier(app.StakingKeeper) - addrs := simapp.AddTestAddrs(app, ctx, 500, sdk.NewInt(10000)) + addrs := simapp.AddTestAddrs(app, ctx, 500, sdk.TokensFromConsensusPower(10000)) + addrAcc1, addrAcc2 := addrs[0], addrs[1] + addrVal1, addrVal2 := sdk.ValAddress(addrAcc1), sdk.ValAddress(addrAcc2) + pubKeys := simapp.CreateTestPubKeys(2) + pk1, pk2 := pubKeys[0], pubKeys[1] // Create Validators and Delegation val1 := types.NewValidator(addrVal1, pk1, types.Description{}) @@ -222,7 +227,8 @@ func TestQueryDelegation(t *testing.T) { app.StakingKeeper.SetValidatorByPowerIndex(ctx, val2) delTokens := sdk.TokensFromConsensusPower(20) - app.StakingKeeper.Delegate(ctx, addrAcc2, delTokens, sdk.Unbonded, val1, true) + _, err := app.StakingKeeper.Delegate(ctx, addrAcc2, delTokens, sdk.Unbonded, val1, true) + require.NoError(t, err) // apply TM updates app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) @@ -414,19 +420,19 @@ func TestQueryDelegation(t *testing.T) { Data: bz, } -// res, err = querier(ctx, []string{types.QueryRedelegations}, query) -// require.NoError(t, err) -// -// var redelRes types.RedelegationResponses -// errRes = cdc.UnmarshalJSON(res, &redelRes) -// require.NoError(t, errRes) -// require.Len(t, redelRes, 1) -// require.Equal(t, redel.DelegatorAddress, redelRes[0].DelegatorAddress) -// require.Equal(t, redel.ValidatorSrcAddress, redelRes[0].ValidatorSrcAddress) -// require.Equal(t, redel.ValidatorDstAddress, redelRes[0].ValidatorDstAddress) -// require.Len(t, redel.Entries, len(redelRes[0].Entries)) -//} -// + res, err = querier(ctx, []string{types.QueryRedelegations}, query) + require.NoError(t, err) + + var redelRes types.RedelegationResponses + errRes = cdc.UnmarshalJSON(res, &redelRes) + require.NoError(t, errRes) + require.Len(t, redelRes, 1) + require.Equal(t, redel.DelegatorAddress, redelRes[0].DelegatorAddress) + require.Equal(t, redel.ValidatorSrcAddress, redelRes[0].ValidatorSrcAddress) + require.Equal(t, redel.ValidatorDstAddress, redelRes[0].ValidatorDstAddress) + require.Len(t, redel.Entries, len(redelRes[0].Entries)) +} + //func TestQueryRedelegations(t *testing.T) { // cdc := codec.New() // ctx, _, _, keeper, _ := CreateTestInput(t, false, 10000)