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