func (k Keeper) InsertValidatorQueue(ctx sdk.Context, val types.Validator) {
	timeSlice := k.GetValidatorQueueTimeSlice(ctx, val.UnbondingTime)
	timeSlice = append(timeSlice, val.OperatorAccount)
	k.SetValidatorQueueTimeSlice(ctx, val.UnbondingTime, timeSlice)
}
Insert an validator address to the appropriate timeslice in the validator queue
package keeper
import (
	"bytes"
	"fmt"
	"time"
	gogotypes "github.com/gogo/protobuf/types"
	"github.com/KuChainNetwork/kuchain/x/staking/types"
	sdk "github.com/cosmos/cosmos-sdk/types"
)
type cachedValidator struct {
	val        types.Validator
	marshalled string  
}
func newCachedValidator(val types.Validator, marshalled string) cachedValidator {
	return cachedValidator{
		val:        val,
		marshalled: marshalled,
	}
}
func (k Keeper) GetValidator(ctx sdk.Context, acc types.AccountID) (validator types.Validator, found bool) {
	store := ctx.KVStore(k.storeKey)
	value := store.Get(types.GetValidatorKey(acc))
	if value == nil {
		return validator, false
	}
	strValue := string(value)
	if val, ok := k.validatorCache[strValue]; ok {
		valToReturn := val.val
		valToReturn.OperatorAccount = acc
		return valToReturn, true
	}
	validator = types.MustUnmarshalValidator(k.cdc, value)
	cachedVal := newCachedValidator(validator, strValue)
	k.validatorCache[strValue] = newCachedValidator(validator, strValue)
	k.validatorCacheList.PushBack(cachedVal)
	if k.validatorCacheList.Len() > aminoCacheSize {
		valToRemove := k.validatorCacheList.Remove(k.validatorCacheList.Front()).(cachedValidator)
		delete(k.validatorCache, valToRemove.marshalled)
	}
	validator = types.MustUnmarshalValidator(k.cdc, value)
	return validator, true
}
func (k Keeper) mustGetValidator(ctx sdk.Context, acc types.AccountID) types.Validator {
	validator, found := k.GetValidator(ctx, acc)
	if !found {
		panic(fmt.Sprintf("validator record not found for address: %X\n", acc))
	}
	return validator
}
func (k Keeper) GetValidatorByConsAddr(ctx sdk.Context, consAcc sdk.ConsAddress) (validator types.Validator, found bool) {
	store := ctx.KVStore(k.storeKey)
	opAccBytes := store.Get(types.GetValidatorByConsAddrKey(consAcc))
	if opAccBytes == nil {
		return validator, false
	}
	return k.GetValidator(ctx, types.NewAccountIDFromByte(opAccBytes))
}
func (k Keeper) mustGetValidatorByConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) types.Validator {
	validator, found := k.GetValidatorByConsAddr(ctx, consAddr)
	if !found {
		panic(fmt.Errorf("validator with consensus-Address %s not found", consAddr))
	}
	return validator
}
func (k Keeper) SetValidator(ctx sdk.Context, validator types.Validator) {
	store := ctx.KVStore(k.storeKey)
	bz := types.MustMarshalValidator(k.cdc, validator)
	store.Set(types.GetValidatorKey(validator.OperatorAccount), bz)
}
func (k Keeper) SetValidatorByConsAddr(ctx sdk.Context, validator types.Validator) {
	store := ctx.KVStore(k.storeKey)
	store.Set(types.GetValidatorByConsAddrKey(validator.GetConsAccount()), validator.OperatorAccount.StoreKey())
}
func (k Keeper) SetValidatorByPowerIndex(ctx sdk.Context, validator types.Validator) {
	if validator.Jailed {
		return
	}
	store := ctx.KVStore(k.storeKey)
	store.Set(types.GetValidatorsByPowerIndexKey(validator), validator.OperatorAccount.Value)
}
func (k Keeper) DeleteValidatorByPowerIndex(ctx sdk.Context, validator types.Validator) {
	store := ctx.KVStore(k.storeKey)
	store.Delete(types.GetValidatorsByPowerIndexKey(validator))
}
func (k Keeper) HasValidatorByPowerIndex(ctx sdk.Context, power []byte) bool {
	store := ctx.KVStore(k.storeKey)
	return store.Has(power)
}
func (k Keeper) SetNewValidatorByPowerIndex(ctx sdk.Context, validator types.Validator) {
	store := ctx.KVStore(k.storeKey)
	store.Set(types.GetValidatorsByPowerIndexKey(validator), validator.OperatorAccount.Value)
}
func (k Keeper) AddValidatorTokensAndShares(ctx sdk.Context, validator types.Validator,
	tokensToAdd sdk.Int) (valOut types.Validator, addedShares sdk.Dec) {
	k.DeleteValidatorByPowerIndex(ctx, validator)
	validator, addedShares = validator.AddTokensFromDel(tokensToAdd)
	k.SetValidator(ctx, validator)
	k.SetValidatorByPowerIndex(ctx, validator)
	return validator, addedShares
}
func (k Keeper) RemoveValidatorTokensAndShares(ctx sdk.Context, validator types.Validator,
	sharesToRemove sdk.Dec) (valOut types.Validator, removedTokens sdk.Int) {
	k.DeleteValidatorByPowerIndex(ctx, validator)
	validator, removedTokens = validator.RemoveDelShares(sharesToRemove)
	k.SetValidator(ctx, validator)
	k.SetValidatorByPowerIndex(ctx, validator)
	return validator, removedTokens
}
func (k Keeper) RemoveValidatorTokens(ctx sdk.Context,
	validator types.Validator, tokensToRemove sdk.Int) types.Validator {
	k.DeleteValidatorByPowerIndex(ctx, validator)
	validator = validator.RemoveTokens(tokensToRemove)
	k.SetValidator(ctx, validator)
	k.SetValidatorByPowerIndex(ctx, validator)
	return validator
}
func (k Keeper) UpdateValidatorCommission(ctx sdk.Context,
	validator types.Validator, newRate sdk.Dec) (types.Commission, error) {
	commission := validator.Commission
	blockTime := ctx.BlockHeader().Time
	if err := commission.ValidateNewRate(newRate, blockTime); err != nil {
		return commission, err
	}
	commission.Rate = newRate
	commission.UpdateTime = blockTime
	return commission, nil
}
func (k Keeper) RemoveValidator(ctx sdk.Context, address types.AccountID) {
	validator, found := k.GetValidator(ctx, address)
	if !found {
		return
	}
	if !validator.IsUnbonded() {
		panic("cannot call RemoveValidator on bonded or unbonding validators")
	}
	if validator.Tokens.IsPositive() {
		panic("attempting to remove a validator which still contains tokens")
	}
	if validator.Tokens.IsPositive() {
		panic("validator being removed should never have positive tokens")
	}
	valConsAddr := validator.GetConsAccount()
	store := ctx.KVStore(k.storeKey)
	store.Delete(types.GetValidatorKey(address))
	store.Delete(types.GetValidatorByConsAddrKey(valConsAddr))
	store.Delete(types.GetValidatorsByPowerIndexKey(validator))
	k.AfterValidatorRemoved(ctx, validator.GetConsAccount(), validator.OperatorAccount)
}
func (k Keeper) GetAllValidators(ctx sdk.Context) (validators []types.Validator) {
	store := ctx.KVStore(k.storeKey)
	iterator := sdk.KVStorePrefixIterator(store, types.ValidatorsKey)
	defer iterator.Close()
	for ; iterator.Valid(); iterator.Next() {
		validator := types.MustUnmarshalValidator(k.cdc, iterator.Value())
		validators = append(validators, validator)
	}
	return validators
}
func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve uint32) (validators []types.Validator) {
	store := ctx.KVStore(k.storeKey)
	validators = make([]types.Validator, maxRetrieve)
	iterator := sdk.KVStorePrefixIterator(store, types.ValidatorsKey)
	defer iterator.Close()
	i := 0
	for ; iterator.Valid() && i < int(maxRetrieve); iterator.Next() {
		validator := types.MustUnmarshalValidator(k.cdc, iterator.Value())
		validators[i] = validator
		i++
	}
	return validators[:i]  
}
func (k Keeper) GetBondedValidatorsByPower(ctx sdk.Context) []types.Validator {
	maxValidators := k.MaxValidators(ctx)
	validators := make([]types.Validator, maxValidators)
	iterator := k.ValidatorsPowerStoreIterator(ctx)
	defer iterator.Close()
	i := 0
	for ; iterator.Valid() && i < int(maxValidators); iterator.Next() {
		address := NewAccountIDFromByte(iterator.Value())
		validator := k.mustGetValidator(ctx, address)
		if validator.IsBonded() {
			validators[i] = validator
			i++
		}
	}
	return validators[:i]  
}
func (k Keeper) ValidatorsPowerStoreIterator(ctx sdk.Context) sdk.Iterator {
	store := ctx.KVStore(k.storeKey)
	return sdk.KVStoreReversePrefixIterator(store, types.ValidatorsByPowerIndexKey)
}
func (k Keeper) GetLastValidatorPower(ctx sdk.Context, operator types.AccountID) (power int64) {
	store := ctx.KVStore(k.storeKey)
	bz := store.Get(types.GetLastValidatorPowerKey(operator))
	if bz == nil {
		return 0
	}
	intV := gogotypes.Int64Value{}
	k.cdc.MustUnmarshalBinaryBare(bz, &intV)
	return intV.GetValue()
}
func (k Keeper) SetLastValidatorPower(ctx sdk.Context, operator types.AccountID, power int64) {
	store := ctx.KVStore(k.storeKey)
	bz := k.cdc.MustMarshalBinaryBare(&gogotypes.Int64Value{Value: power})
	store.Set(types.GetLastValidatorPowerKey(operator), bz)
}
func (k Keeper) DeleteLastValidatorPower(ctx sdk.Context, operator types.AccountID) {
	store := ctx.KVStore(k.storeKey)
	store.Delete(types.GetLastValidatorPowerKey(operator))
}
func (k Keeper) LastValidatorsIterator(ctx sdk.Context) (iterator sdk.Iterator) {
	store := ctx.KVStore(k.storeKey)
	iterator = sdk.KVStorePrefixIterator(store, types.LastValidatorPowerKey)
	return iterator
}
func (k Keeper) IterateLastValidatorPowers(ctx sdk.Context, handler func(operator types.AccountID, power int64) (stop bool)) {
	store := ctx.KVStore(k.storeKey)
	iter := sdk.KVStorePrefixIterator(store, types.LastValidatorPowerKey)
	defer iter.Close()
	for ; iter.Valid(); iter.Next() {
		addr := NewAccountIDFromByte(iter.Key()[len(types.LastValidatorPowerKey):])
		intV := &gogotypes.Int64Value{}
		k.cdc.MustUnmarshalBinaryBare(iter.Value(), intV)
		if handler(addr, intV.GetValue()) {
			break
		}
	}
}
func (k Keeper) GetLastValidators(ctx sdk.Context) (validators []types.Validator) {
	store := ctx.KVStore(k.storeKey)
	maxValidators := k.MaxValidators(ctx)
	validators = make([]types.Validator, maxValidators)
	iterator := sdk.KVStorePrefixIterator(store, types.LastValidatorPowerKey)
	defer iterator.Close()
	i := 0
	for ; iterator.Valid(); iterator.Next() {
		if i >= int(maxValidators) {
			panic("more validators than maxValidators found")
		}
		address := NewAccountIDFromByte(types.AddressFromLastValidatorPowerKey(iterator.Key()))
		validator := k.mustGetValidator(ctx, address)
		validators[i] = validator
		i++
	}
	return validators[:i]  
}
func (k Keeper) GetValidatorQueueTimeSlice(ctx sdk.Context, timestamp time.Time) []types.AccountID {
	store := ctx.KVStore(k.storeKey)
	bz := store.Get(types.GetValidatorQueueTimeKey(timestamp))
	if bz == nil {
		return []types.AccountID{}
	}
	va := types.AccountIDs{}
	k.cdc.MustUnmarshalBinaryBare(bz, &va)
	return va.Addresses
}
func (k Keeper) SetValidatorQueueTimeSlice(ctx sdk.Context, timestamp time.Time, keys []types.AccountID) {
	store := ctx.KVStore(k.storeKey)
	bz := k.cdc.MustMarshalBinaryBare(&types.AccountIDs{Addresses: keys})
	store.Set(types.GetValidatorQueueTimeKey(timestamp), bz)
}
func (k Keeper) DeleteValidatorQueueTimeSlice(ctx sdk.Context, timestamp time.Time) {
	store := ctx.KVStore(k.storeKey)
	store.Delete(types.GetValidatorQueueTimeKey(timestamp))
}