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

     1  package keeper
     2  
     3  import (
     4  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     5  
     6  	"github.com/fibonacci-chain/fbc/x/distribution/types"
     7  )
     8  
     9  // GetDelegatorWithdrawAddr returns the delegator withdraw address, defaulting to the delegator address
    10  func (k Keeper) GetDelegatorWithdrawAddr(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress {
    11  	store := ctx.KVStore(k.storeKey)
    12  	b := store.Get(types.GetDelegatorWithdrawAddrKey(delAddr))
    13  	if b == nil {
    14  		return delAddr
    15  	}
    16  	return sdk.AccAddress(b)
    17  }
    18  
    19  // SetDelegatorWithdrawAddr sets the delegator withdraw address
    20  func (k Keeper) SetDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
    21  	store := ctx.KVStore(k.storeKey)
    22  	store.Set(types.GetDelegatorWithdrawAddrKey(delAddr), withdrawAddr.Bytes())
    23  }
    24  
    25  // IterateDelegatorWithdrawAddrs iterates over delegator withdraw addrs
    26  func (k Keeper) IterateDelegatorWithdrawAddrs(ctx sdk.Context,
    27  	handler func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool)) {
    28  	store := ctx.KVStore(k.storeKey)
    29  	iter := sdk.KVStorePrefixIterator(store, types.DelegatorWithdrawAddrPrefix)
    30  	defer iter.Close()
    31  	for ; iter.Valid(); iter.Next() {
    32  		addr := sdk.AccAddress(iter.Value())
    33  		del := types.GetDelegatorWithdrawInfoAddress(iter.Key())
    34  		if handler(del, addr) {
    35  			break
    36  		}
    37  	}
    38  }
    39  
    40  // GetFeePool returns the global fee pool distribution info
    41  func (k Keeper) GetFeePool(ctx sdk.Context) (feePool types.FeePool) {
    42  	store := ctx.KVStore(k.storeKey)
    43  	b := store.Get(types.FeePoolKey)
    44  	if b == nil {
    45  		panic("Stored fee pool should not have been nil")
    46  	}
    47  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &feePool)
    48  	return
    49  }
    50  
    51  // SetFeePool sets the global fee pool distribution info
    52  func (k Keeper) SetFeePool(ctx sdk.Context, feePool types.FeePool) {
    53  	store := ctx.KVStore(k.storeKey)
    54  	b := k.cdc.MustMarshalBinaryLengthPrefixed(feePool)
    55  	store.Set(types.FeePoolKey, b)
    56  }
    57  
    58  // GetFeePoolCommunityCoins returns the community coins
    59  func (k Keeper) GetFeePoolCommunityCoins(ctx sdk.Context) sdk.SysCoins {
    60  	return k.GetFeePool(ctx).CommunityPool
    61  }
    62  
    63  // GetPreviousProposerConsAddr returns the proposer public key for this block
    64  func (k Keeper) GetPreviousProposerConsAddr(ctx sdk.Context) (consAddr sdk.ConsAddress) {
    65  	store := ctx.KVStore(k.storeKey)
    66  	b := store.Get(types.ProposerKey)
    67  	if b == nil {
    68  		panic("Previous proposer not set")
    69  	}
    70  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &consAddr)
    71  	return consAddr
    72  }
    73  
    74  // SetPreviousProposerConsAddr sets the proposer public key for this block
    75  func (k Keeper) SetPreviousProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) {
    76  	store := ctx.KVStore(k.storeKey)
    77  	b := k.cdc.MustMarshalBinaryLengthPrefixed(consAddr)
    78  	store.Set(types.ProposerKey, b)
    79  }
    80  
    81  // GetValidatorAccumulatedCommission returns accumulated commission for a validator
    82  func (k Keeper) GetValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress) (
    83  	commission types.ValidatorAccumulatedCommission) {
    84  
    85  	store := ctx.KVStore(k.storeKey)
    86  	b := store.Get(types.GetValidatorAccumulatedCommissionKey(val))
    87  	if b == nil {
    88  		return types.ValidatorAccumulatedCommission{}
    89  	}
    90  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &commission)
    91  	return commission
    92  }
    93  
    94  // SetValidatorAccumulatedCommission sets accumulated commission for a validator
    95  func (k Keeper) SetValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress,
    96  	commission types.ValidatorAccumulatedCommission) {
    97  
    98  	var bz []byte
    99  	store := ctx.KVStore(k.storeKey)
   100  	if commission.IsZero() {
   101  		bz = k.cdc.MustMarshalBinaryLengthPrefixed(types.InitialValidatorAccumulatedCommission())
   102  	} else {
   103  		bz = k.cdc.MustMarshalBinaryLengthPrefixed(commission)
   104  	}
   105  	store.Set(types.GetValidatorAccumulatedCommissionKey(val), bz)
   106  }
   107  
   108  // deleteValidatorAccumulatedCommission deletes accumulated commission for a validator
   109  func (k Keeper) deleteValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress) {
   110  	store := ctx.KVStore(k.storeKey)
   111  	store.Delete(types.GetValidatorAccumulatedCommissionKey(val))
   112  }
   113  
   114  // IterateValidatorAccumulatedCommissions iterates over accumulated commissions
   115  func (k Keeper) IterateValidatorAccumulatedCommissions(ctx sdk.Context,
   116  	handler func(val sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool)) {
   117  
   118  	store := ctx.KVStore(k.storeKey)
   119  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorAccumulatedCommissionPrefix)
   120  	defer iter.Close()
   121  	for ; iter.Valid(); iter.Next() {
   122  		var commission types.ValidatorAccumulatedCommission
   123  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &commission)
   124  		addr := types.GetValidatorAccumulatedCommissionAddress(iter.Key())
   125  		if handler(addr, commission) {
   126  			break
   127  		}
   128  	}
   129  }