github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/distribution/keeper/store_distr_proposal.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/x/distribution/types" 6 ) 7 8 // CheckInitExistedValidatorFlag check init existed validator for distribution proposal flag 9 func (k Keeper) CheckInitExistedValidatorFlag(ctx sdk.Context) bool { 10 store := ctx.KVStore(k.storeKey) 11 b := store.Get(types.InitExistedValidatorForDistrProposalKey) 12 if b == nil { 13 return false 14 } 15 result := true 16 k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &result) 17 return result 18 } 19 20 // SetInitExistedValidatorFlag set init existed validator for distribution proposal flag 21 func (k Keeper) SetInitExistedValidatorFlag(ctx sdk.Context, init bool) { 22 store := ctx.KVStore(k.storeKey) 23 b := k.cdc.MustMarshalBinaryLengthPrefixed(init) 24 store.Set(types.InitExistedValidatorForDistrProposalKey, b) 25 } 26 27 // get the starting info associated with a delegator 28 func (k Keeper) GetDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) (period types.DelegatorStartingInfo) { 29 store := ctx.KVStore(k.storeKey) 30 b := store.Get(types.GetDelegatorStartingInfoKey(val, del)) 31 k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &period) 32 return 33 } 34 35 // set the starting info associated with a delegator 36 func (k Keeper) SetDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress, period types.DelegatorStartingInfo) { 37 store := ctx.KVStore(k.storeKey) 38 b := k.cdc.MustMarshalBinaryLengthPrefixed(period) 39 store.Set(types.GetDelegatorStartingInfoKey(val, del), b) 40 } 41 42 // check existence of the starting info associated with a delegator 43 func (k Keeper) HasDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) bool { 44 store := ctx.KVStore(k.storeKey) 45 return store.Has(types.GetDelegatorStartingInfoKey(val, del)) 46 } 47 48 // delete the starting info associated with a delegator 49 func (k Keeper) DeleteDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) { 50 store := ctx.KVStore(k.storeKey) 51 store.Delete(types.GetDelegatorStartingInfoKey(val, del)) 52 } 53 54 // iterate over delegator starting infos 55 func (k Keeper) IterateDelegatorStartingInfos(ctx sdk.Context, handler func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool)) { 56 store := ctx.KVStore(k.storeKey) 57 iter := sdk.KVStorePrefixIterator(store, types.DelegatorStartingInfoPrefix) 58 defer iter.Close() 59 for ; iter.Valid(); iter.Next() { 60 var info types.DelegatorStartingInfo 61 k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &info) 62 val, del := types.GetDelegatorStartingInfoAddresses(iter.Key()) 63 if handler(val, del, info) { 64 break 65 } 66 } 67 } 68 69 // get historical rewards for a particular period 70 func (k Keeper) GetValidatorHistoricalRewards(ctx sdk.Context, val sdk.ValAddress, period uint64) (rewards types.ValidatorHistoricalRewards) { 71 store := ctx.KVStore(k.storeKey) 72 b := store.Get(types.GetValidatorHistoricalRewardsKey(val, period)) 73 k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &rewards) 74 return 75 } 76 77 // set historical rewards for a particular period 78 func (k Keeper) SetValidatorHistoricalRewards(ctx sdk.Context, val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) { 79 store := ctx.KVStore(k.storeKey) 80 b := k.cdc.MustMarshalBinaryLengthPrefixed(rewards) 81 store.Set(types.GetValidatorHistoricalRewardsKey(val, period), b) 82 } 83 84 // iterate over historical rewards 85 func (k Keeper) IterateValidatorHistoricalRewards(ctx sdk.Context, handler func(val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool)) { 86 store := ctx.KVStore(k.storeKey) 87 iter := sdk.KVStorePrefixIterator(store, types.ValidatorHistoricalRewardsPrefix) 88 defer iter.Close() 89 for ; iter.Valid(); iter.Next() { 90 var rewards types.ValidatorHistoricalRewards 91 k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &rewards) 92 addr, period := types.GetValidatorHistoricalRewardsAddressPeriod(iter.Key()) 93 if handler(addr, period, rewards) { 94 break 95 } 96 } 97 } 98 99 // delete a historical reward 100 func (k Keeper) DeleteValidatorHistoricalReward(ctx sdk.Context, val sdk.ValAddress, period uint64) { 101 store := ctx.KVStore(k.storeKey) 102 store.Delete(types.GetValidatorHistoricalRewardsKey(val, period)) 103 104 } 105 106 // delete historical rewards for a validator 107 func (k Keeper) DeleteValidatorHistoricalRewards(ctx sdk.Context, val sdk.ValAddress) { 108 store := ctx.KVStore(k.storeKey) 109 iter := sdk.KVStorePrefixIterator(store, types.GetValidatorHistoricalRewardsPrefix(val)) 110 defer iter.Close() 111 for ; iter.Valid(); iter.Next() { 112 store.Delete(iter.Key()) 113 } 114 } 115 116 // delete all historical rewards 117 func (k Keeper) DeleteAllValidatorHistoricalRewards(ctx sdk.Context) { 118 store := ctx.KVStore(k.storeKey) 119 iter := sdk.KVStorePrefixIterator(store, types.ValidatorHistoricalRewardsPrefix) 120 defer iter.Close() 121 for ; iter.Valid(); iter.Next() { 122 store.Delete(iter.Key()) 123 } 124 } 125 126 // historical reference count (used for testcases) 127 func (k Keeper) GetValidatorHistoricalReferenceCount(ctx sdk.Context) (count uint64) { 128 store := ctx.KVStore(k.storeKey) 129 iter := sdk.KVStorePrefixIterator(store, types.ValidatorHistoricalRewardsPrefix) 130 defer iter.Close() 131 for ; iter.Valid(); iter.Next() { 132 var rewards types.ValidatorHistoricalRewards 133 k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &rewards) 134 count += uint64(rewards.ReferenceCount) 135 } 136 return 137 } 138 139 // get current rewards for a validator 140 func (k Keeper) GetValidatorCurrentRewards(ctx sdk.Context, val sdk.ValAddress) (rewards types.ValidatorCurrentRewards) { 141 store := ctx.KVStore(k.storeKey) 142 b := store.Get(types.GetValidatorCurrentRewardsKey(val)) 143 k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &rewards) 144 return 145 } 146 147 // set current rewards for a validator 148 func (k Keeper) SetValidatorCurrentRewards(ctx sdk.Context, val sdk.ValAddress, rewards types.ValidatorCurrentRewards) { 149 store := ctx.KVStore(k.storeKey) 150 b := k.cdc.MustMarshalBinaryLengthPrefixed(rewards) 151 store.Set(types.GetValidatorCurrentRewardsKey(val), b) 152 } 153 154 // delete current rewards for a validator 155 func (k Keeper) DeleteValidatorCurrentRewards(ctx sdk.Context, val sdk.ValAddress) { 156 store := ctx.KVStore(k.storeKey) 157 store.Delete(types.GetValidatorCurrentRewardsKey(val)) 158 } 159 160 // iterate over current rewards 161 func (k Keeper) IterateValidatorCurrentRewards(ctx sdk.Context, handler func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool)) { 162 store := ctx.KVStore(k.storeKey) 163 iter := sdk.KVStorePrefixIterator(store, types.ValidatorCurrentRewardsPrefix) 164 defer iter.Close() 165 for ; iter.Valid(); iter.Next() { 166 var rewards types.ValidatorCurrentRewards 167 k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &rewards) 168 addr := types.GetValidatorCurrentRewardsAddress(iter.Key()) 169 if handler(addr, rewards) { 170 break 171 } 172 } 173 } 174 175 // get validator outstanding rewards 176 func (k Keeper) GetValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress) (rewards types.ValidatorOutstandingRewards) { 177 store := ctx.KVStore(k.storeKey) 178 b := store.Get(types.GetValidatorOutstandingRewardsKey(val)) 179 k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &rewards) 180 return 181 } 182 183 // set validator outstanding rewards 184 func (k Keeper) SetValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress, rewards types.ValidatorOutstandingRewards) { 185 store := ctx.KVStore(k.storeKey) 186 b := k.cdc.MustMarshalBinaryLengthPrefixed(rewards) 187 store.Set(types.GetValidatorOutstandingRewardsKey(val), b) 188 } 189 190 // delete validator outstanding rewards 191 func (k Keeper) DeleteValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress) { 192 store := ctx.KVStore(k.storeKey) 193 store.Delete(types.GetValidatorOutstandingRewardsKey(val)) 194 } 195 196 // set validator outstanding rewards 197 func (k Keeper) HasValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress) bool { 198 store := ctx.KVStore(k.storeKey) 199 return store.Has(types.GetValidatorOutstandingRewardsKey(val)) 200 } 201 202 // iterate validator outstanding rewards 203 func (k Keeper) IterateValidatorOutstandingRewards(ctx sdk.Context, handler func(val sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool)) { 204 store := ctx.KVStore(k.storeKey) 205 iter := sdk.KVStorePrefixIterator(store, types.ValidatorOutstandingRewardsPrefix) 206 defer iter.Close() 207 for ; iter.Valid(); iter.Next() { 208 var rewards types.ValidatorOutstandingRewards 209 k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &rewards) 210 addr := types.GetValidatorOutstandingRewardsAddress(iter.Key()) 211 if handler(addr, rewards) { 212 break 213 } 214 } 215 }