cosmos-sdk/x/staking/keeper/test_common.go
Robert Zaremba 6bc8ff2dfe
Use any as validator pubkey (#7597)
* protobuf pubkey type update

* wip2

* wip3

* solving types.NewValidator issues

* remove bech32 from validator type assignment

* update Validator interface

* Changelog update

* wip4

* update genutil

* fix simapp & x/ibc/testing tests

* update staking

* changelog update

* fix import cycle in tests

* fix amino panic on TestValidatorMarshalUnmarshalJSON

* fix TestValidatorMarshalUnmarshalJSON consensus_pubkey check

* Add UnpackInterfaces to HistoricalInfo

* fix TestHistoricalInfo

* update todos

* fix: Expecting ed25519.PubKey to implement proto.Message

* fix linter issues

* Fix migrate test

* Update CHANGELOG.md

Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>

* review comments

* cosmetic changes

* add UnpackInterfaces got GenesisRandomized test

* Validator.Equal reuses Validator.MinEqual

* fix test

* use Validator.Equal in tests

* Fix staking simulation TestRandomizedGenState

* Remove TODO

* use HistoricalInfo.Equal

* use proto.Equal

* rename Validator.GetConsPubKey to TmConsPubKey

* prefer require.Equal over reflect.DeepEqual

* SetHistoricalInfo using a pointer

* Fix TestQueryDelegation test

* Fix TestQueryValidators test

* Fix TestSimulateMsgUnjail test

* experiement with LegacyAmino instances

* Register codecs in all simapp tests

* Fix cli_test compilation problems

* fix typo sdk -> std

* fix typo

* fix TestPlanStringer

* Rename to MakeEncodingConfig

* Remove RegisterCodecsTests

* Use gRPC in GetCmdQueryValidators

* Empty status

* fix info log check

* linter fixes

* rename simapparams to simappparams

* Update simapp/test_helpers.go

Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>

* comments updates

* use valAddr1 instead of sdk.ValAddress(pk1.Address().Bytes())

Co-authored-by: Cory Levinson <cjlevinson@gmail.com>
Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
2020-10-23 12:07:52 +00:00

70 lines
1.7 KiB
Go

package keeper // noalias
import (
"bytes"
"math/rand"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
// does a certain by-power index record exist
func ValidatorByPowerIndexExists(ctx sdk.Context, keeper Keeper, power []byte) bool {
store := ctx.KVStore(keeper.storeKey)
return store.Has(power)
}
// update validator for testing
func TestingUpdateValidator(keeper Keeper, ctx sdk.Context, validator types.Validator, apply bool) types.Validator {
keeper.SetValidator(ctx, validator)
// Remove any existing power key for validator.
store := ctx.KVStore(keeper.storeKey)
deleted := false
iterator := sdk.KVStorePrefixIterator(store, types.ValidatorsByPowerIndexKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
valAddr := types.ParseValidatorPowerRankKey(iterator.Key())
if bytes.Equal(valAddr, validator.GetOperator()) {
if deleted {
panic("found duplicate power index key")
} else {
deleted = true
}
store.Delete(iterator.Key())
}
}
keeper.SetValidatorByPowerIndex(ctx, validator)
if !apply {
ctx, _ = ctx.CacheContext()
}
_, err := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
panic(err)
}
validator, found := keeper.GetValidator(ctx, validator.GetOperator())
if !found {
panic("validator expected but not found")
}
return validator
}
// RandomValidator returns a random validator given access to the keeper and ctx
func RandomValidator(r *rand.Rand, keeper Keeper, ctx sdk.Context) (val types.Validator, ok bool) {
vals := keeper.GetAllValidators(ctx)
if len(vals) == 0 {
return types.Validator{}, false
}
i := r.Intn(len(vals))
return vals[i], true
}