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