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

     1  package keeper
     2  
     3  import (
     4  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     5  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/distribution/types"
     6  )
     7  
     8  // get the delegator withdraw address, defaulting to the delegator address
     9  func (k Keeper) GetDelegatorWithdrawAddr(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress {
    10  	store := ctx.KVStore(k.storeKey)
    11  	b := store.Get(types.GetDelegatorWithdrawAddrKey(delAddr))
    12  	if b == nil {
    13  		return delAddr
    14  	}
    15  	return sdk.AccAddress(b)
    16  }
    17  
    18  // set the delegator withdraw address
    19  func (k Keeper) SetDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
    20  	store := ctx.KVStore(k.storeKey)
    21  	store.Set(types.GetDelegatorWithdrawAddrKey(delAddr), withdrawAddr.Bytes())
    22  }
    23  
    24  // delete a delegator withdraw addr
    25  func (k Keeper) DeleteDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
    26  	store := ctx.KVStore(k.storeKey)
    27  	store.Delete(types.GetDelegatorWithdrawAddrKey(delAddr))
    28  }
    29  
    30  // iterate over delegator withdraw addrs
    31  func (k Keeper) IterateDelegatorWithdrawAddrs(ctx sdk.Context, handler func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool)) {
    32  	store := ctx.KVStore(k.storeKey)
    33  	iter := sdk.KVStorePrefixIterator(store, types.DelegatorWithdrawAddrPrefix)
    34  	defer iter.Close()
    35  	for ; iter.Valid(); iter.Next() {
    36  		addr := sdk.AccAddress(iter.Value())
    37  		del := types.GetDelegatorWithdrawInfoAddress(iter.Key())
    38  		if handler(del, addr) {
    39  			break
    40  		}
    41  	}
    42  }
    43  
    44  // get the global fee pool distribution info
    45  func (k Keeper) GetFeePool(ctx sdk.Context) (feePool types.FeePool) {
    46  	store := ctx.KVStore(k.storeKey)
    47  	b := store.Get(types.FeePoolKey)
    48  	if b == nil {
    49  		panic("Stored fee pool should not have been nil")
    50  	}
    51  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &feePool)
    52  	return
    53  }
    54  
    55  // set the global fee pool distribution info
    56  func (k Keeper) SetFeePool(ctx sdk.Context, feePool types.FeePool) {
    57  	store := ctx.KVStore(k.storeKey)
    58  	b := k.cdc.MustMarshalBinaryLengthPrefixed(feePool)
    59  	store.Set(types.FeePoolKey, b)
    60  }
    61  
    62  // get the proposer public key for this block
    63  func (k Keeper) GetPreviousProposerConsAddr(ctx sdk.Context) (consAddr sdk.ConsAddress) {
    64  	store := ctx.KVStore(k.storeKey)
    65  	b := store.Get(types.ProposerKey)
    66  	if b == nil {
    67  		panic("Previous proposer not set")
    68  	}
    69  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &consAddr)
    70  	return
    71  }
    72  
    73  // set the proposer public key for this block
    74  func (k Keeper) SetPreviousProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) {
    75  	store := ctx.KVStore(k.storeKey)
    76  	b := k.cdc.MustMarshalBinaryLengthPrefixed(consAddr)
    77  	store.Set(types.ProposerKey, b)
    78  }
    79  
    80  // get the starting info associated with a delegator
    81  func (k Keeper) GetDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) (period types.DelegatorStartingInfo) {
    82  	store := ctx.KVStore(k.storeKey)
    83  	b := store.Get(types.GetDelegatorStartingInfoKey(val, del))
    84  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &period)
    85  	return
    86  }
    87  
    88  // set the starting info associated with a delegator
    89  func (k Keeper) SetDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress, period types.DelegatorStartingInfo) {
    90  	store := ctx.KVStore(k.storeKey)
    91  	b := k.cdc.MustMarshalBinaryLengthPrefixed(period)
    92  	store.Set(types.GetDelegatorStartingInfoKey(val, del), b)
    93  }
    94  
    95  // check existence of the starting info associated with a delegator
    96  func (k Keeper) HasDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) bool {
    97  	store := ctx.KVStore(k.storeKey)
    98  	return store.Has(types.GetDelegatorStartingInfoKey(val, del))
    99  }
   100  
   101  // delete the starting info associated with a delegator
   102  func (k Keeper) DeleteDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) {
   103  	store := ctx.KVStore(k.storeKey)
   104  	store.Delete(types.GetDelegatorStartingInfoKey(val, del))
   105  }
   106  
   107  // iterate over delegator starting infos
   108  func (k Keeper) IterateDelegatorStartingInfos(ctx sdk.Context, handler func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool)) {
   109  	store := ctx.KVStore(k.storeKey)
   110  	iter := sdk.KVStorePrefixIterator(store, types.DelegatorStartingInfoPrefix)
   111  	defer iter.Close()
   112  	for ; iter.Valid(); iter.Next() {
   113  		var info types.DelegatorStartingInfo
   114  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &info)
   115  		val, del := types.GetDelegatorStartingInfoAddresses(iter.Key())
   116  		if handler(val, del, info) {
   117  			break
   118  		}
   119  	}
   120  }
   121  
   122  // get historical rewards for a particular period
   123  func (k Keeper) GetValidatorHistoricalRewards(ctx sdk.Context, val sdk.ValAddress, period uint64) (rewards types.ValidatorHistoricalRewards) {
   124  	store := ctx.KVStore(k.storeKey)
   125  	b := store.Get(types.GetValidatorHistoricalRewardsKey(val, period))
   126  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &rewards)
   127  	return
   128  }
   129  
   130  // set historical rewards for a particular period
   131  func (k Keeper) SetValidatorHistoricalRewards(ctx sdk.Context, val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) {
   132  	store := ctx.KVStore(k.storeKey)
   133  	b := k.cdc.MustMarshalBinaryLengthPrefixed(rewards)
   134  	store.Set(types.GetValidatorHistoricalRewardsKey(val, period), b)
   135  }
   136  
   137  // iterate over historical rewards
   138  func (k Keeper) IterateValidatorHistoricalRewards(ctx sdk.Context, handler func(val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool)) {
   139  	store := ctx.KVStore(k.storeKey)
   140  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorHistoricalRewardsPrefix)
   141  	defer iter.Close()
   142  	for ; iter.Valid(); iter.Next() {
   143  		var rewards types.ValidatorHistoricalRewards
   144  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &rewards)
   145  		addr, period := types.GetValidatorHistoricalRewardsAddressPeriod(iter.Key())
   146  		if handler(addr, period, rewards) {
   147  			break
   148  		}
   149  	}
   150  }
   151  
   152  // delete a historical reward
   153  func (k Keeper) DeleteValidatorHistoricalReward(ctx sdk.Context, val sdk.ValAddress, period uint64) {
   154  	store := ctx.KVStore(k.storeKey)
   155  	store.Delete(types.GetValidatorHistoricalRewardsKey(val, period))
   156  }
   157  
   158  // delete historical rewards for a validator
   159  func (k Keeper) DeleteValidatorHistoricalRewards(ctx sdk.Context, val sdk.ValAddress) {
   160  	store := ctx.KVStore(k.storeKey)
   161  	iter := sdk.KVStorePrefixIterator(store, types.GetValidatorHistoricalRewardsPrefix(val))
   162  	defer iter.Close()
   163  	for ; iter.Valid(); iter.Next() {
   164  		store.Delete(iter.Key())
   165  	}
   166  }
   167  
   168  // delete all historical rewards
   169  func (k Keeper) DeleteAllValidatorHistoricalRewards(ctx sdk.Context) {
   170  	store := ctx.KVStore(k.storeKey)
   171  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorHistoricalRewardsPrefix)
   172  	defer iter.Close()
   173  	for ; iter.Valid(); iter.Next() {
   174  		store.Delete(iter.Key())
   175  	}
   176  }
   177  
   178  // historical reference count (used for testcases)
   179  func (k Keeper) GetValidatorHistoricalReferenceCount(ctx sdk.Context) (count uint64) {
   180  	store := ctx.KVStore(k.storeKey)
   181  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorHistoricalRewardsPrefix)
   182  	defer iter.Close()
   183  	for ; iter.Valid(); iter.Next() {
   184  		var rewards types.ValidatorHistoricalRewards
   185  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &rewards)
   186  		count += uint64(rewards.ReferenceCount)
   187  	}
   188  	return
   189  }
   190  
   191  // get current rewards for a validator
   192  func (k Keeper) GetValidatorCurrentRewards(ctx sdk.Context, val sdk.ValAddress) (rewards types.ValidatorCurrentRewards) {
   193  	store := ctx.KVStore(k.storeKey)
   194  	b := store.Get(types.GetValidatorCurrentRewardsKey(val))
   195  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &rewards)
   196  	return
   197  }
   198  
   199  // set current rewards for a validator
   200  func (k Keeper) SetValidatorCurrentRewards(ctx sdk.Context, val sdk.ValAddress, rewards types.ValidatorCurrentRewards) {
   201  	store := ctx.KVStore(k.storeKey)
   202  	b := k.cdc.MustMarshalBinaryLengthPrefixed(rewards)
   203  	store.Set(types.GetValidatorCurrentRewardsKey(val), b)
   204  }
   205  
   206  // delete current rewards for a validator
   207  func (k Keeper) DeleteValidatorCurrentRewards(ctx sdk.Context, val sdk.ValAddress) {
   208  	store := ctx.KVStore(k.storeKey)
   209  	store.Delete(types.GetValidatorCurrentRewardsKey(val))
   210  }
   211  
   212  // iterate over current rewards
   213  func (k Keeper) IterateValidatorCurrentRewards(ctx sdk.Context, handler func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool)) {
   214  	store := ctx.KVStore(k.storeKey)
   215  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorCurrentRewardsPrefix)
   216  	defer iter.Close()
   217  	for ; iter.Valid(); iter.Next() {
   218  		var rewards types.ValidatorCurrentRewards
   219  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &rewards)
   220  		addr := types.GetValidatorCurrentRewardsAddress(iter.Key())
   221  		if handler(addr, rewards) {
   222  			break
   223  		}
   224  	}
   225  }
   226  
   227  // get accumulated commission for a validator
   228  func (k Keeper) GetValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress) (commission types.ValidatorAccumulatedCommission) {
   229  	store := ctx.KVStore(k.storeKey)
   230  	b := store.Get(types.GetValidatorAccumulatedCommissionKey(val))
   231  	if b == nil {
   232  		return types.ValidatorAccumulatedCommission{}
   233  	}
   234  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &commission)
   235  	return
   236  }
   237  
   238  // set accumulated commission for a validator
   239  func (k Keeper) SetValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress, commission types.ValidatorAccumulatedCommission) {
   240  	var bz []byte
   241  
   242  	store := ctx.KVStore(k.storeKey)
   243  	if commission.IsZero() {
   244  		bz = k.cdc.MustMarshalBinaryLengthPrefixed(types.InitialValidatorAccumulatedCommission())
   245  	} else {
   246  		bz = k.cdc.MustMarshalBinaryLengthPrefixed(commission)
   247  	}
   248  
   249  	store.Set(types.GetValidatorAccumulatedCommissionKey(val), bz)
   250  }
   251  
   252  // delete accumulated commission for a validator
   253  func (k Keeper) DeleteValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress) {
   254  	store := ctx.KVStore(k.storeKey)
   255  	store.Delete(types.GetValidatorAccumulatedCommissionKey(val))
   256  }
   257  
   258  // iterate over accumulated commissions
   259  func (k Keeper) IterateValidatorAccumulatedCommissions(ctx sdk.Context, handler func(val sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool)) {
   260  	store := ctx.KVStore(k.storeKey)
   261  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorAccumulatedCommissionPrefix)
   262  	defer iter.Close()
   263  	for ; iter.Valid(); iter.Next() {
   264  		var commission types.ValidatorAccumulatedCommission
   265  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &commission)
   266  		addr := types.GetValidatorAccumulatedCommissionAddress(iter.Key())
   267  		if handler(addr, commission) {
   268  			break
   269  		}
   270  	}
   271  }
   272  
   273  // get validator outstanding rewards
   274  func (k Keeper) GetValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress) (rewards types.ValidatorOutstandingRewards) {
   275  	store := ctx.KVStore(k.storeKey)
   276  	b := store.Get(types.GetValidatorOutstandingRewardsKey(val))
   277  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &rewards)
   278  	return
   279  }
   280  
   281  // set validator outstanding rewards
   282  func (k Keeper) SetValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress, rewards types.ValidatorOutstandingRewards) {
   283  	store := ctx.KVStore(k.storeKey)
   284  	b := k.cdc.MustMarshalBinaryLengthPrefixed(rewards)
   285  	store.Set(types.GetValidatorOutstandingRewardsKey(val), b)
   286  }
   287  
   288  // delete validator outstanding rewards
   289  func (k Keeper) DeleteValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress) {
   290  	store := ctx.KVStore(k.storeKey)
   291  	store.Delete(types.GetValidatorOutstandingRewardsKey(val))
   292  }
   293  
   294  // iterate validator outstanding rewards
   295  func (k Keeper) IterateValidatorOutstandingRewards(ctx sdk.Context, handler func(val sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool)) {
   296  	store := ctx.KVStore(k.storeKey)
   297  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorOutstandingRewardsPrefix)
   298  	defer iter.Close()
   299  	for ; iter.Valid(); iter.Next() {
   300  		var rewards types.ValidatorOutstandingRewards
   301  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &rewards)
   302  		addr := types.GetValidatorOutstandingRewardsAddress(iter.Key())
   303  		if handler(addr, rewards) {
   304  			break
   305  		}
   306  	}
   307  }
   308  
   309  // get slash event for height
   310  func (k Keeper) GetValidatorSlashEvent(ctx sdk.Context, val sdk.ValAddress, height, period uint64) (event types.ValidatorSlashEvent, found bool) {
   311  	store := ctx.KVStore(k.storeKey)
   312  	b := store.Get(types.GetValidatorSlashEventKey(val, height, period))
   313  	if b == nil {
   314  		return types.ValidatorSlashEvent{}, false
   315  	}
   316  	k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &event)
   317  	return event, true
   318  }
   319  
   320  // set slash event for height
   321  func (k Keeper) SetValidatorSlashEvent(ctx sdk.Context, val sdk.ValAddress, height, period uint64, event types.ValidatorSlashEvent) {
   322  	store := ctx.KVStore(k.storeKey)
   323  	b := k.cdc.MustMarshalBinaryLengthPrefixed(event)
   324  	store.Set(types.GetValidatorSlashEventKey(val, height, period), b)
   325  }
   326  
   327  // iterate over slash events between heights, inclusive
   328  func (k Keeper) IterateValidatorSlashEventsBetween(ctx sdk.Context, val sdk.ValAddress, startingHeight uint64, endingHeight uint64,
   329  	handler func(height uint64, event types.ValidatorSlashEvent) (stop bool)) {
   330  	store := ctx.KVStore(k.storeKey)
   331  	iter := store.Iterator(
   332  		types.GetValidatorSlashEventKeyPrefix(val, startingHeight),
   333  		types.GetValidatorSlashEventKeyPrefix(val, endingHeight+1),
   334  	)
   335  	defer iter.Close()
   336  	for ; iter.Valid(); iter.Next() {
   337  		var event types.ValidatorSlashEvent
   338  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &event)
   339  		_, height := types.GetValidatorSlashEventAddressHeight(iter.Key())
   340  		if handler(height, event) {
   341  			break
   342  		}
   343  	}
   344  }
   345  
   346  // iterate over all slash events
   347  func (k Keeper) IterateValidatorSlashEvents(ctx sdk.Context, handler func(val sdk.ValAddress, height uint64, event types.ValidatorSlashEvent) (stop bool)) {
   348  	store := ctx.KVStore(k.storeKey)
   349  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorSlashEventPrefix)
   350  	defer iter.Close()
   351  	for ; iter.Valid(); iter.Next() {
   352  		var event types.ValidatorSlashEvent
   353  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &event)
   354  		val, height := types.GetValidatorSlashEventAddressHeight(iter.Key())
   355  		if handler(val, height, event) {
   356  			break
   357  		}
   358  	}
   359  }
   360  
   361  // delete slash events for a particular validator
   362  func (k Keeper) DeleteValidatorSlashEvents(ctx sdk.Context, val sdk.ValAddress) {
   363  	store := ctx.KVStore(k.storeKey)
   364  	iter := sdk.KVStorePrefixIterator(store, types.GetValidatorSlashEventPrefix(val))
   365  	defer iter.Close()
   366  	for ; iter.Valid(); iter.Next() {
   367  		store.Delete(iter.Key())
   368  	}
   369  }
   370  
   371  // delete all slash events
   372  func (k Keeper) DeleteAllValidatorSlashEvents(ctx sdk.Context) {
   373  	store := ctx.KVStore(k.storeKey)
   374  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorSlashEventPrefix)
   375  	defer iter.Close()
   376  	for ; iter.Valid(); iter.Next() {
   377  		store.Delete(iter.Key())
   378  	}
   379  }