* Cherry picked commits from prev branch * Added new keepers for querier functionalities * Renaming * Fixed gov errors and messages * Added Querier to stake and app * Update delegation keepers * REST Queriers not working * Fix marshalling error * Querier tests working * Pool and params working * sdk.NewCoin for test handler * Refactor and renaming * Update LCD queries and added more tests for queriers * use sdk.NewCoin * Delegator summary query and tests * Added more tests for keeper * Update PENDING.md * Update stake rest query * Format and replaced panics for sdk.Error * Refactor and addressed comments from Sunny and Aleks * Fixed some of the errors produced by addr type change * Fixed remaining errors * Updated and fixed lite tests * JSON Header and consistency on errors * Increased cov for genesis * Added comment for maxRetrieve param in keepers * Comment on DelegationWithoutDec * Bech32Validator Keepers * Changed Bech validator * Updated remaining tests and bech32 validator * Addressed most of Rigel's comments * Updated tests and types * Make codec to be unexported from keeper * Moved logic to query_utils and updated tests * Fix linter err and PENDING * Fix err * Fix err * Fixed tests * Update PENDING description * Update UpdateBondedValidatorsFull * Update iterator * defer iterator.Close() * delete comment * Address some of Aleks comments, need to fix tests * export querier * Fixed tests * Address Rigel's comments * More tests * return error for GetDelegatorValidator * Fixed conflicts * Fix linter warnings * Address @rigelrozanski comments * Delete comments * wire ––> codec
102 lines
3.4 KiB
Go
102 lines
3.4 KiB
Go
package keeper
|
|
|
|
import (
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
|
)
|
|
|
|
// Return all validators that a delegator is bonded to. If maxRetrieve is supplied, the respective amount will be returned.
|
|
func (k Keeper) GetDelegatorValidators(ctx sdk.Context, delegatorAddr sdk.AccAddress,
|
|
maxRetrieve uint16) (validators []types.Validator) {
|
|
validators = make([]types.Validator, maxRetrieve)
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
delegatorPrefixKey := GetDelegationsKey(delegatorAddr)
|
|
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest
|
|
defer iterator.Close()
|
|
|
|
i := 0
|
|
for ; iterator.Valid() && i < int(maxRetrieve); iterator.Next() {
|
|
addr := iterator.Key()
|
|
delegation := types.MustUnmarshalDelegation(k.cdc, addr, iterator.Value())
|
|
|
|
validator, found := k.GetValidator(ctx, delegation.ValidatorAddr)
|
|
if !found {
|
|
panic(types.ErrNoValidatorFound(types.DefaultCodespace))
|
|
}
|
|
validators[i] = validator
|
|
i++
|
|
}
|
|
return validators[:i] // trim
|
|
}
|
|
|
|
// return a validator that a delegator is bonded to
|
|
func (k Keeper) GetDelegatorValidator(ctx sdk.Context, delegatorAddr sdk.AccAddress,
|
|
validatorAddr sdk.ValAddress) (validator types.Validator, err sdk.Error) {
|
|
|
|
delegation, found := k.GetDelegation(ctx, delegatorAddr, validatorAddr)
|
|
if !found {
|
|
return validator, types.ErrNoDelegation(types.DefaultCodespace)
|
|
}
|
|
|
|
validator, found = k.GetValidator(ctx, delegation.ValidatorAddr)
|
|
if !found {
|
|
panic(types.ErrNoValidatorFound(types.DefaultCodespace))
|
|
}
|
|
return
|
|
}
|
|
|
|
//_____________________________________________________________________________________
|
|
|
|
// return all delegations for a delegator
|
|
func (k Keeper) GetAllDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddress) (
|
|
delegations []types.Delegation) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
delegatorPrefixKey := GetDelegationsKey(delegator)
|
|
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest
|
|
defer iterator.Close()
|
|
|
|
i := 0
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Key(), iterator.Value())
|
|
delegations = append(delegations, delegation)
|
|
i++
|
|
}
|
|
return delegations
|
|
}
|
|
|
|
// return all unbonding-delegations for a delegator
|
|
func (k Keeper) GetAllUnbondingDelegations(ctx sdk.Context, delegator sdk.AccAddress) (
|
|
unbondingDelegations []types.UnbondingDelegation) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
delegatorPrefixKey := GetUBDsKey(delegator)
|
|
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest
|
|
defer iterator.Close()
|
|
|
|
i := 0
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
unbondingDelegation := types.MustUnmarshalUBD(k.cdc, iterator.Key(), iterator.Value())
|
|
unbondingDelegations = append(unbondingDelegations, unbondingDelegation)
|
|
i++
|
|
}
|
|
return unbondingDelegations
|
|
}
|
|
|
|
// return all redelegations for a delegator
|
|
func (k Keeper) GetAllRedelegations(ctx sdk.Context, delegator sdk.AccAddress) (redelegations []types.Redelegation) {
|
|
store := ctx.KVStore(k.storeKey)
|
|
delegatorPrefixKey := GetREDsKey(delegator)
|
|
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest
|
|
defer iterator.Close()
|
|
|
|
i := 0
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
redelegation := types.MustUnmarshalRED(k.cdc, iterator.Key(), iterator.Value())
|
|
redelegations = append(redelegations, redelegation)
|
|
i++
|
|
}
|
|
return redelegations
|
|
}
|