2018-09-13 14:23:44 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-09-13 14:23:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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() {
|
2019-01-07 12:42:52 -08:00
|
|
|
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value())
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
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() {
|
2019-01-07 12:42:52 -08:00
|
|
|
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value())
|
2018-09-13 14:23:44 -07:00
|
|
|
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() {
|
2019-01-07 12:42:52 -08:00
|
|
|
unbondingDelegation := types.MustUnmarshalUBD(k.cdc, iterator.Value())
|
2018-09-13 14:23:44 -07:00
|
|
|
unbondingDelegations = append(unbondingDelegations, unbondingDelegation)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return unbondingDelegations
|
|
|
|
}
|
|
|
|
|
|
|
|
// return all redelegations for a delegator
|
2018-12-18 13:43:41 -08:00
|
|
|
func (k Keeper) GetAllRedelegations(ctx sdk.Context, delegator sdk.AccAddress, srcValAddress, dstValAddress sdk.ValAddress) (redelegations []types.Redelegation) {
|
2018-09-13 14:23:44 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
delegatorPrefixKey := GetREDsKey(delegator)
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) //smallest to largest
|
|
|
|
defer iterator.Close()
|
|
|
|
|
2018-12-18 13:43:41 -08:00
|
|
|
srcValFilter := !(srcValAddress.Empty() || srcValAddress == nil)
|
|
|
|
dstValFilter := !(dstValAddress.Empty() || dstValAddress == nil)
|
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
2019-01-07 12:42:52 -08:00
|
|
|
redelegation := types.MustUnmarshalRED(k.cdc, iterator.Value())
|
2018-12-18 13:43:41 -08:00
|
|
|
if srcValFilter && !(srcValAddress.Equals(redelegation.ValidatorSrcAddr)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if dstValFilter && !(dstValAddress.Equals(redelegation.ValidatorDstAddr)) {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-13 14:23:44 -07:00
|
|
|
redelegations = append(redelegations, redelegation)
|
|
|
|
}
|
|
|
|
return redelegations
|
|
|
|
}
|