github.com/Finschia/finschia-sdk@v0.48.1/x/staking/keeper/alias_functions.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/Finschia/finschia-sdk/types"
     7  	"github.com/Finschia/finschia-sdk/x/staking/types"
     8  )
     9  
    10  // Validator Set
    11  
    12  // iterate through the validator set and perform the provided function
    13  func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validator types.ValidatorI) (stop bool)) {
    14  	store := ctx.KVStore(k.storeKey)
    15  
    16  	iterator := sdk.KVStorePrefixIterator(store, types.ValidatorsKey)
    17  	defer iterator.Close()
    18  
    19  	i := int64(0)
    20  
    21  	for ; iterator.Valid(); iterator.Next() {
    22  		validator := types.MustUnmarshalValidator(k.cdc, iterator.Value())
    23  		stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
    24  
    25  		if stop {
    26  			break
    27  		}
    28  		i++
    29  	}
    30  }
    31  
    32  // iterate through the bonded validator set and perform the provided function
    33  func (k Keeper) IterateBondedValidatorsByPower(ctx sdk.Context, fn func(index int64, validator types.ValidatorI) (stop bool)) {
    34  	store := ctx.KVStore(k.storeKey)
    35  	maxValidators := k.MaxValidators(ctx)
    36  
    37  	iterator := sdk.KVStoreReversePrefixIterator(store, types.ValidatorsByPowerIndexKey)
    38  	defer iterator.Close()
    39  
    40  	i := int64(0)
    41  	for ; iterator.Valid() && i < int64(maxValidators); iterator.Next() {
    42  		address := iterator.Value()
    43  		validator := k.mustGetValidator(ctx, address)
    44  
    45  		if validator.IsBonded() {
    46  			stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
    47  			if stop {
    48  				break
    49  			}
    50  			i++
    51  		}
    52  	}
    53  }
    54  
    55  // iterate through the active validator set and perform the provided function
    56  func (k Keeper) IterateLastValidators(ctx sdk.Context, fn func(index int64, validator types.ValidatorI) (stop bool)) {
    57  	iterator := k.LastValidatorsIterator(ctx)
    58  	defer iterator.Close()
    59  
    60  	i := int64(0)
    61  
    62  	for ; iterator.Valid(); iterator.Next() {
    63  		address := types.AddressFromLastValidatorPowerKey(iterator.Key())
    64  
    65  		validator, found := k.GetValidator(ctx, address)
    66  		if !found {
    67  			panic(fmt.Sprintf("validator record not found for address: %v\n", address))
    68  		}
    69  
    70  		stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
    71  		if stop {
    72  			break
    73  		}
    74  		i++
    75  	}
    76  }
    77  
    78  // Validator gets the Validator interface for a particular address
    79  func (k Keeper) Validator(ctx sdk.Context, address sdk.ValAddress) types.ValidatorI {
    80  	val, found := k.GetValidator(ctx, address)
    81  	if !found {
    82  		return nil
    83  	}
    84  
    85  	return val
    86  }
    87  
    88  // ValidatorByConsAddr gets the validator interface for a particular pubkey
    89  func (k Keeper) ValidatorByConsAddr(ctx sdk.Context, addr sdk.ConsAddress) types.ValidatorI {
    90  	val, found := k.GetValidatorByConsAddr(ctx, addr)
    91  	if !found {
    92  		return nil
    93  	}
    94  
    95  	return val
    96  }
    97  
    98  // Delegation Set
    99  
   100  // Returns self as it is both a validatorset and delegationset
   101  func (k Keeper) GetValidatorSet() types.ValidatorSet {
   102  	return k
   103  }
   104  
   105  // Delegation get the delegation interface for a particular set of delegator and validator addresses
   106  func (k Keeper) Delegation(ctx sdk.Context, addrDel sdk.AccAddress, addrVal sdk.ValAddress) types.DelegationI {
   107  	bond, ok := k.GetDelegation(ctx, addrDel, addrVal)
   108  	if !ok {
   109  		return nil
   110  	}
   111  
   112  	return bond
   113  }
   114  
   115  // iterate through all of the delegations from a delegator
   116  func (k Keeper) IterateDelegations(ctx sdk.Context, delAddr sdk.AccAddress,
   117  	fn func(index int64, del types.DelegationI) (stop bool),
   118  ) {
   119  	store := ctx.KVStore(k.storeKey)
   120  	delegatorPrefixKey := types.GetDelegationsKey(delAddr)
   121  
   122  	iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
   123  	defer iterator.Close()
   124  
   125  	for i := int64(0); iterator.Valid(); iterator.Next() {
   126  		del := types.MustUnmarshalDelegation(k.cdc, iterator.Value())
   127  
   128  		stop := fn(i, del)
   129  		if stop {
   130  			break
   131  		}
   132  		i++
   133  	}
   134  }
   135  
   136  // return all delegations used during genesis dump
   137  // TODO: remove this func, change all usage for iterate functionality
   138  func (k Keeper) GetAllSDKDelegations(ctx sdk.Context) (delegations []types.Delegation) {
   139  	store := ctx.KVStore(k.storeKey)
   140  
   141  	iterator := sdk.KVStorePrefixIterator(store, types.DelegationKey)
   142  	defer iterator.Close()
   143  
   144  	for ; iterator.Valid(); iterator.Next() {
   145  		delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value())
   146  		delegations = append(delegations, delegation)
   147  	}
   148  
   149  	return
   150  }