github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/staking/keeper/alias_functions.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	"github.com/fibonacci-chain/fbc/x/staking/exported"
     8  	"github.com/fibonacci-chain/fbc/x/staking/types"
     9  )
    10  
    11  //_______________________________________________________________________
    12  // Validator Set
    13  
    14  // IterateValidators iterates through the validator set and performs the provided function
    15  func (k Keeper) IterateValidators(ctx sdk.Context, fn func(index int64, validator exported.ValidatorI) (stop bool)) {
    16  	store := ctx.KVStore(k.storeKey)
    17  	iterator := sdk.KVStorePrefixIterator(store, types.ValidatorsKey)
    18  	defer iterator.Close()
    19  	i := int64(0)
    20  	for ; iterator.Valid(); iterator.Next() {
    21  		validator := types.MustUnmarshalValidator(k.cdcMarshl.GetCdc(), iterator.Value())
    22  		stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
    23  		if stop {
    24  			break
    25  		}
    26  		i++
    27  	}
    28  }
    29  
    30  // IterateBondedValidatorsByPower iterates through the bonded validator set and performs the provided function
    31  func (k Keeper) IterateBondedValidatorsByPower(ctx sdk.Context,
    32  	fn func(index int64, validator exported.ValidatorI) (stop bool)) {
    33  	store := ctx.KVStore(k.storeKey)
    34  	maxValidators := k.MaxValidators(ctx)
    35  
    36  	iterator := sdk.KVStoreReversePrefixIterator(store, types.ValidatorsByPowerIndexKey)
    37  	defer iterator.Close()
    38  
    39  	i := int64(0)
    40  	for ; iterator.Valid() && i < int64(maxValidators); iterator.Next() {
    41  		address := iterator.Value()
    42  		validator := k.mustGetValidator(ctx, address)
    43  
    44  		if validator.IsBonded() {
    45  			stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
    46  			if stop {
    47  				break
    48  			}
    49  			i++
    50  		}
    51  	}
    52  }
    53  
    54  // IterateLastValidators iterates through the active validator set and performs the provided function
    55  func (k Keeper) IterateLastValidators(ctx sdk.Context,
    56  	fn func(index int64, validator exported.ValidatorI) (stop bool)) {
    57  	iterator := k.LastValidatorsIterator(ctx)
    58  	defer iterator.Close()
    59  	i := int64(0)
    60  	for ; iterator.Valid(); iterator.Next() {
    61  		address := types.AddressFromLastValidatorPowerKey(iterator.Key())
    62  		validator, found := k.GetValidator(ctx, address)
    63  		if !found {
    64  			panic(fmt.Sprintf("validator record not found for address: %v\n", address))
    65  		}
    66  
    67  		stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
    68  		if stop {
    69  			break
    70  		}
    71  		i++
    72  	}
    73  }
    74  
    75  // Validator gets the Validator interface for a particular address
    76  func (k Keeper) Validator(ctx sdk.Context, address sdk.ValAddress) exported.ValidatorI {
    77  	val, found := k.GetValidator(ctx, address)
    78  	if !found {
    79  		return nil
    80  	}
    81  	return val
    82  }
    83  
    84  // ValidatorByConsAddr gets the validator interface for a particular pubkey
    85  func (k Keeper) ValidatorByConsAddr(ctx sdk.Context, addr sdk.ConsAddress) exported.ValidatorI {
    86  	val, found := k.GetValidatorByConsAddr(ctx, addr)
    87  	if !found {
    88  		return nil
    89  	}
    90  	return val
    91  }
    92  
    93  // Delegator gets the DelegatorI interface for other module
    94  func (k Keeper) Delegator(ctx sdk.Context, delAddr sdk.AccAddress) exported.DelegatorI {
    95  	delegator, found := k.GetDelegator(ctx, delAddr)
    96  	if !found {
    97  		return nil
    98  	}
    99  
   100  	return delegator
   101  }