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.
|
2020-02-06 11:21:02 -08:00
|
|
|
func (k Keeper) GetDelegatorValidators(
|
|
|
|
ctx sdk.Context, delegatorAddr sdk.AccAddress, maxRetrieve uint32,
|
2020-10-23 05:07:52 -07:00
|
|
|
) types.Validators {
|
2020-02-06 11:21:02 -08:00
|
|
|
validators := make([]types.Validator, maxRetrieve)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2019-05-28 01:44:04 -07:00
|
|
|
delegatorPrefixKey := types.GetDelegationsKey(delegatorAddr)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
|
2018-09-13 14:23:44 -07:00
|
|
|
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
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
validator, found := k.GetValidator(ctx, delegation.GetValidatorAddr())
|
2018-09-13 14:23:44 -07:00
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
panic(types.ErrNoValidatorFound)
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2020-02-06 11:21:02 -08:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
validators[i] = validator
|
|
|
|
i++
|
|
|
|
}
|
2020-02-06 11:21:02 -08:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return validators[:i] // trim
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a validator that a delegator is bonded to
|
2020-02-06 11:21:02 -08:00
|
|
|
func (k Keeper) GetDelegatorValidator(
|
|
|
|
ctx sdk.Context, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress,
|
|
|
|
) (validator types.Validator, err error) {
|
2018-09-13 14:23:44 -07:00
|
|
|
delegation, found := k.GetDelegation(ctx, delegatorAddr, validatorAddr)
|
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
return validator, types.ErrNoDelegation
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
validator, found = k.GetValidator(ctx, delegation.GetValidatorAddr())
|
2018-09-13 14:23:44 -07:00
|
|
|
if !found {
|
2019-12-27 09:57:54 -08:00
|
|
|
panic(types.ErrNoValidatorFound)
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
2020-02-06 11:21:02 -08:00
|
|
|
|
|
|
|
return validator, nil
|
2018-09-13 14:23:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// return all delegations for a delegator
|
2019-05-15 07:44:47 -07:00
|
|
|
func (k Keeper) GetAllDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddress) []types.Delegation {
|
|
|
|
delegations := make([]types.Delegation, 0)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2019-05-28 01:44:04 -07:00
|
|
|
delegatorPrefixKey := types.GetDelegationsKey(delegator)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2021-02-23 00:46:01 -08:00
|
|
|
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
|
2018-09-13 14:23:44 -07:00
|
|
|
defer iterator.Close()
|
|
|
|
|
|
|
|
i := 0
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
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++
|
|
|
|
}
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return delegations
|
|
|
|
}
|
|
|
|
|
|
|
|
// return all unbonding-delegations for a delegator
|
2019-05-15 07:44:47 -07:00
|
|
|
func (k Keeper) GetAllUnbondingDelegations(ctx sdk.Context, delegator sdk.AccAddress) []types.UnbondingDelegation {
|
|
|
|
unbondingDelegations := make([]types.UnbondingDelegation, 0)
|
2018-09-13 14:23:44 -07:00
|
|
|
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2019-05-28 01:44:04 -07:00
|
|
|
delegatorPrefixKey := types.GetUBDsKey(delegator)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
|
2018-09-13 14:23:44 -07:00
|
|
|
defer iterator.Close()
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
for i := 0; 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++
|
|
|
|
}
|
2019-05-15 07:44:47 -07:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return unbondingDelegations
|
|
|
|
}
|
|
|
|
|
|
|
|
// return all redelegations for a delegator
|
2020-02-06 11:21:02 -08:00
|
|
|
func (k Keeper) GetAllRedelegations(
|
|
|
|
ctx sdk.Context, delegator sdk.AccAddress, srcValAddress, dstValAddress sdk.ValAddress,
|
|
|
|
) []types.Redelegation {
|
2018-09-13 14:23:44 -07:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2019-05-28 01:44:04 -07:00
|
|
|
delegatorPrefixKey := types.GetREDsKey(delegator)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-02-07 17:41:23 -08:00
|
|
|
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
|
2018-09-13 14:23:44 -07:00
|
|
|
defer iterator.Close()
|
|
|
|
|
2019-02-08 15:54:40 -08:00
|
|
|
srcValFilter := !(srcValAddress.Empty())
|
|
|
|
dstValFilter := !(dstValAddress.Empty())
|
2018-12-18 13:43:41 -08:00
|
|
|
|
2020-02-06 11:21:02 -08:00
|
|
|
redelegations := []types.Redelegation{}
|
|
|
|
|
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())
|
2020-09-25 03:25:37 -07:00
|
|
|
valSrcAddr, err := sdk.ValAddressFromBech32(redelegation.ValidatorSrcAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
valDstAddr, err := sdk.ValAddressFromBech32(redelegation.ValidatorDstAddress)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if srcValFilter && !(srcValAddress.Equals(valSrcAddr)) {
|
2018-12-18 13:43:41 -08:00
|
|
|
continue
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
if dstValFilter && !(dstValAddress.Equals(valDstAddr)) {
|
2018-12-18 13:43:41 -08:00
|
|
|
continue
|
|
|
|
}
|
2020-02-06 11:21:02 -08:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
redelegations = append(redelegations, redelegation)
|
|
|
|
}
|
2020-02-06 11:21:02 -08:00
|
|
|
|
2018-09-13 14:23:44 -07:00
|
|
|
return redelegations
|
|
|
|
}
|