2019-01-16 13:38:05 -08:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2020-02-11 06:58:37 -08:00
|
|
|
gogotypes "github.com/gogo/protobuf/types"
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// get the delegator withdraw address, defaulting to the delegator address
|
|
|
|
func (k Keeper) GetDelegatorWithdrawAddr(ctx sdk.Context, delAddr sdk.AccAddress) sdk.AccAddress {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
b := store.Get(types.GetDelegatorWithdrawAddrKey(delAddr))
|
2019-01-16 13:38:05 -08:00
|
|
|
if b == nil {
|
|
|
|
return delAddr
|
|
|
|
}
|
|
|
|
return sdk.AccAddress(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the delegator withdraw address
|
|
|
|
func (k Keeper) SetDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Set(types.GetDelegatorWithdrawAddrKey(delAddr), withdrawAddr.Bytes())
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// delete a delegator withdraw addr
|
|
|
|
func (k Keeper) DeleteDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAddr sdk.AccAddress) {
|
2019-01-16 13:38:05 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Delete(types.GetDelegatorWithdrawAddrKey(delAddr))
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over delegator withdraw addrs
|
|
|
|
func (k Keeper) IterateDelegatorWithdrawAddrs(ctx sdk.Context, handler func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.DelegatorWithdrawAddrPrefix)
|
2019-01-16 13:38:05 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
addr := sdk.AccAddress(iter.Value())
|
2020-01-10 12:08:14 -08:00
|
|
|
del := types.GetDelegatorWithdrawInfoAddress(iter.Key())
|
2019-01-16 13:38:05 -08:00
|
|
|
if handler(del, addr) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the global fee pool distribution info
|
|
|
|
func (k Keeper) GetFeePool(ctx sdk.Context) (feePool types.FeePool) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
b := store.Get(types.FeePoolKey)
|
2019-01-16 13:38:05 -08:00
|
|
|
if b == nil {
|
|
|
|
panic("Stored fee pool should not have been nil")
|
|
|
|
}
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(b, &feePool)
|
2019-01-16 13:38:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the global fee pool distribution info
|
|
|
|
func (k Keeper) SetFeePool(ctx sdk.Context, feePool types.FeePool) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-03-13 12:58:43 -07:00
|
|
|
b := k.cdc.MustMarshalBinaryBare(&feePool)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Set(types.FeePoolKey, b)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
2020-02-11 06:58:37 -08:00
|
|
|
// GetPreviousProposerConsAddr returns the proposer consensus address for the
|
|
|
|
// current block.
|
|
|
|
func (k Keeper) GetPreviousProposerConsAddr(ctx sdk.Context) sdk.ConsAddress {
|
2019-01-16 13:38:05 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-02-11 06:58:37 -08:00
|
|
|
bz := store.Get(types.ProposerKey)
|
|
|
|
if bz == nil {
|
|
|
|
panic("previous proposer not set")
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
2020-02-11 06:58:37 -08:00
|
|
|
|
|
|
|
addrValue := gogotypes.BytesValue{}
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &addrValue)
|
|
|
|
return addrValue.GetValue()
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the proposer public key for this block
|
|
|
|
func (k Keeper) SetPreviousProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-03-13 12:58:43 -07:00
|
|
|
bz := k.cdc.MustMarshalBinaryBare(&gogotypes.BytesValue{Value: consAddr})
|
2020-02-11 06:58:37 -08:00
|
|
|
store.Set(types.ProposerKey, bz)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// get the starting info associated with a delegator
|
2019-01-16 13:38:05 -08:00
|
|
|
func (k Keeper) GetDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) (period types.DelegatorStartingInfo) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
b := store.Get(types.GetDelegatorStartingInfoKey(val, del))
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(b, &period)
|
2019-01-16 13:38:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// set the starting info associated with a delegator
|
2019-01-16 13:38:05 -08:00
|
|
|
func (k Keeper) SetDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress, period types.DelegatorStartingInfo) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-03-13 12:58:43 -07:00
|
|
|
b := k.cdc.MustMarshalBinaryBare(&period)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Set(types.GetDelegatorStartingInfoKey(val, del), b)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// check existence of the starting info associated with a delegator
|
|
|
|
func (k Keeper) HasDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) bool {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
return store.Has(types.GetDelegatorStartingInfoKey(val, del))
|
2019-01-23 03:37:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete the starting info associated with a delegator
|
|
|
|
func (k Keeper) DeleteDelegatorStartingInfo(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Delete(types.GetDelegatorStartingInfoKey(val, del))
|
2019-01-23 03:37:03 -08:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// iterate over delegator starting infos
|
|
|
|
func (k Keeper) IterateDelegatorStartingInfos(ctx sdk.Context, handler func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.DelegatorStartingInfoPrefix)
|
2019-01-16 13:38:05 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
var info types.DelegatorStartingInfo
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &info)
|
2020-01-10 12:08:14 -08:00
|
|
|
val, del := types.GetDelegatorStartingInfoAddresses(iter.Key())
|
2019-01-16 13:38:05 -08:00
|
|
|
if handler(val, del, info) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get historical rewards for a particular period
|
|
|
|
func (k Keeper) GetValidatorHistoricalRewards(ctx sdk.Context, val sdk.ValAddress, period uint64) (rewards types.ValidatorHistoricalRewards) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
b := store.Get(types.GetValidatorHistoricalRewardsKey(val, period))
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(b, &rewards)
|
2019-01-16 13:38:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// set historical rewards for a particular period
|
|
|
|
func (k Keeper) SetValidatorHistoricalRewards(ctx sdk.Context, val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-03-13 12:58:43 -07:00
|
|
|
b := k.cdc.MustMarshalBinaryBare(&rewards)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Set(types.GetValidatorHistoricalRewardsKey(val, period), b)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over historical rewards
|
|
|
|
func (k Keeper) IterateValidatorHistoricalRewards(ctx sdk.Context, handler func(val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.ValidatorHistoricalRewardsPrefix)
|
2019-01-16 13:38:05 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
var rewards types.ValidatorHistoricalRewards
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &rewards)
|
2020-01-10 12:08:14 -08:00
|
|
|
addr, period := types.GetValidatorHistoricalRewardsAddressPeriod(iter.Key())
|
2019-01-16 13:38:05 -08:00
|
|
|
if handler(addr, period, rewards) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// delete a historical reward
|
|
|
|
func (k Keeper) DeleteValidatorHistoricalReward(ctx sdk.Context, val sdk.ValAddress, period uint64) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Delete(types.GetValidatorHistoricalRewardsKey(val, period))
|
2019-01-23 03:37:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete historical rewards for a validator
|
|
|
|
func (k Keeper) DeleteValidatorHistoricalRewards(ctx sdk.Context, val sdk.ValAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.GetValidatorHistoricalRewardsPrefix(val))
|
2019-01-23 03:37:03 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
store.Delete(iter.Key())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete all historical rewards
|
|
|
|
func (k Keeper) DeleteAllValidatorHistoricalRewards(ctx sdk.Context) {
|
2019-01-16 13:38:05 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.ValidatorHistoricalRewardsPrefix)
|
2019-01-16 13:38:05 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
store.Delete(iter.Key())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 13:01:32 -08:00
|
|
|
// historical reference count (used for testcases)
|
|
|
|
func (k Keeper) GetValidatorHistoricalReferenceCount(ctx sdk.Context) (count uint64) {
|
2019-01-23 03:37:03 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.ValidatorHistoricalRewardsPrefix)
|
2019-01-23 03:37:03 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
2019-01-24 13:01:32 -08:00
|
|
|
var rewards types.ValidatorHistoricalRewards
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &rewards)
|
2019-01-24 13:01:32 -08:00
|
|
|
count += uint64(rewards.ReferenceCount)
|
2019-01-23 03:37:03 -08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// get current rewards for a validator
|
|
|
|
func (k Keeper) GetValidatorCurrentRewards(ctx sdk.Context, val sdk.ValAddress) (rewards types.ValidatorCurrentRewards) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
b := store.Get(types.GetValidatorCurrentRewardsKey(val))
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(b, &rewards)
|
2019-01-16 13:38:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// set current rewards for a validator
|
|
|
|
func (k Keeper) SetValidatorCurrentRewards(ctx sdk.Context, val sdk.ValAddress, rewards types.ValidatorCurrentRewards) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-03-13 12:58:43 -07:00
|
|
|
b := k.cdc.MustMarshalBinaryBare(&rewards)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Set(types.GetValidatorCurrentRewardsKey(val), b)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// delete current rewards for a validator
|
|
|
|
func (k Keeper) DeleteValidatorCurrentRewards(ctx sdk.Context, val sdk.ValAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Delete(types.GetValidatorCurrentRewardsKey(val))
|
2019-01-23 03:37:03 -08:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// iterate over current rewards
|
|
|
|
func (k Keeper) IterateValidatorCurrentRewards(ctx sdk.Context, handler func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.ValidatorCurrentRewardsPrefix)
|
2019-01-16 13:38:05 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
var rewards types.ValidatorCurrentRewards
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &rewards)
|
2020-01-10 12:08:14 -08:00
|
|
|
addr := types.GetValidatorCurrentRewardsAddress(iter.Key())
|
2019-01-16 13:38:05 -08:00
|
|
|
if handler(addr, rewards) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get accumulated commission for a validator
|
|
|
|
func (k Keeper) GetValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress) (commission types.ValidatorAccumulatedCommission) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
b := store.Get(types.GetValidatorAccumulatedCommissionKey(val))
|
2019-01-16 13:38:05 -08:00
|
|
|
if b == nil {
|
|
|
|
return types.ValidatorAccumulatedCommission{}
|
|
|
|
}
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(b, &commission)
|
2019-01-16 13:38:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// set accumulated commission for a validator
|
|
|
|
func (k Keeper) SetValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress, commission types.ValidatorAccumulatedCommission) {
|
2019-03-12 08:10:20 -07:00
|
|
|
var bz []byte
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-02-11 06:58:37 -08:00
|
|
|
if commission.Commission.IsZero() {
|
2020-03-13 12:58:43 -07:00
|
|
|
bz = k.cdc.MustMarshalBinaryBare(&types.ValidatorAccumulatedCommission{})
|
2019-03-12 08:10:20 -07:00
|
|
|
} else {
|
2020-03-13 12:58:43 -07:00
|
|
|
bz = k.cdc.MustMarshalBinaryBare(&commission)
|
2019-03-12 08:10:20 -07:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Set(types.GetValidatorAccumulatedCommissionKey(val), bz)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// delete accumulated commission for a validator
|
|
|
|
func (k Keeper) DeleteValidatorAccumulatedCommission(ctx sdk.Context, val sdk.ValAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Delete(types.GetValidatorAccumulatedCommissionKey(val))
|
2019-01-23 03:37:03 -08:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// iterate over accumulated commissions
|
|
|
|
func (k Keeper) IterateValidatorAccumulatedCommissions(ctx sdk.Context, handler func(val sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.ValidatorAccumulatedCommissionPrefix)
|
2019-01-16 13:38:05 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
var commission types.ValidatorAccumulatedCommission
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &commission)
|
2020-01-10 12:08:14 -08:00
|
|
|
addr := types.GetValidatorAccumulatedCommissionAddress(iter.Key())
|
2019-01-16 13:38:05 -08:00
|
|
|
if handler(addr, commission) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:54:12 -08:00
|
|
|
// get validator outstanding rewards
|
|
|
|
func (k Keeper) GetValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress) (rewards types.ValidatorOutstandingRewards) {
|
2019-01-16 13:38:05 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-02-11 06:58:37 -08:00
|
|
|
bz := store.Get(types.GetValidatorOutstandingRewardsKey(val))
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &rewards)
|
2019-01-16 13:38:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:54:12 -08:00
|
|
|
// set validator outstanding rewards
|
|
|
|
func (k Keeper) SetValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress, rewards types.ValidatorOutstandingRewards) {
|
2019-01-16 13:38:05 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-03-13 12:58:43 -07:00
|
|
|
b := k.cdc.MustMarshalBinaryBare(&rewards)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Set(types.GetValidatorOutstandingRewardsKey(val), b)
|
2019-03-06 10:54:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete validator outstanding rewards
|
|
|
|
func (k Keeper) DeleteValidatorOutstandingRewards(ctx sdk.Context, val sdk.ValAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Delete(types.GetValidatorOutstandingRewardsKey(val))
|
2019-03-06 10:54:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterate validator outstanding rewards
|
|
|
|
func (k Keeper) IterateValidatorOutstandingRewards(ctx sdk.Context, handler func(val sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.ValidatorOutstandingRewardsPrefix)
|
2019-03-06 10:54:12 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
2020-02-11 06:58:37 -08:00
|
|
|
rewards := types.ValidatorOutstandingRewards{}
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &rewards)
|
2020-01-10 12:08:14 -08:00
|
|
|
addr := types.GetValidatorOutstandingRewardsAddress(iter.Key())
|
2019-03-06 10:54:12 -08:00
|
|
|
if handler(addr, rewards) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// get slash event for height
|
2019-07-01 09:03:34 -07:00
|
|
|
func (k Keeper) GetValidatorSlashEvent(ctx sdk.Context, val sdk.ValAddress, height, period uint64) (event types.ValidatorSlashEvent, found bool) {
|
2019-01-16 13:38:05 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
b := store.Get(types.GetValidatorSlashEventKey(val, height, period))
|
2019-01-16 13:38:05 -08:00
|
|
|
if b == nil {
|
|
|
|
return types.ValidatorSlashEvent{}, false
|
|
|
|
}
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(b, &event)
|
2019-01-16 13:38:05 -08:00
|
|
|
return event, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// set slash event for height
|
2019-07-01 09:03:34 -07:00
|
|
|
func (k Keeper) SetValidatorSlashEvent(ctx sdk.Context, val sdk.ValAddress, height, period uint64, event types.ValidatorSlashEvent) {
|
2019-01-16 13:38:05 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-03-13 12:58:43 -07:00
|
|
|
b := k.cdc.MustMarshalBinaryBare(&event)
|
2020-01-10 12:08:14 -08:00
|
|
|
store.Set(types.GetValidatorSlashEventKey(val, height, period), b)
|
2019-01-16 13:38:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over slash events between heights, inclusive
|
|
|
|
func (k Keeper) IterateValidatorSlashEventsBetween(ctx sdk.Context, val sdk.ValAddress, startingHeight uint64, endingHeight uint64,
|
|
|
|
handler func(height uint64, event types.ValidatorSlashEvent) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
iter := store.Iterator(
|
2020-01-10 12:08:14 -08:00
|
|
|
types.GetValidatorSlashEventKeyPrefix(val, startingHeight),
|
|
|
|
types.GetValidatorSlashEventKeyPrefix(val, endingHeight+1),
|
2019-01-16 13:38:05 -08:00
|
|
|
)
|
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
var event types.ValidatorSlashEvent
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &event)
|
2020-01-10 12:08:14 -08:00
|
|
|
_, height := types.GetValidatorSlashEventAddressHeight(iter.Key())
|
2019-01-16 13:38:05 -08:00
|
|
|
if handler(height, event) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over all slash events
|
|
|
|
func (k Keeper) IterateValidatorSlashEvents(ctx sdk.Context, handler func(val sdk.ValAddress, height uint64, event types.ValidatorSlashEvent) (stop bool)) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.ValidatorSlashEventPrefix)
|
2019-01-16 13:38:05 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
var event types.ValidatorSlashEvent
|
2020-03-13 12:58:43 -07:00
|
|
|
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &event)
|
2020-01-10 12:08:14 -08:00
|
|
|
val, height := types.GetValidatorSlashEventAddressHeight(iter.Key())
|
2019-01-16 13:38:05 -08:00
|
|
|
if handler(val, height, event) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// delete slash events for a particular validator
|
|
|
|
func (k Keeper) DeleteValidatorSlashEvents(ctx sdk.Context, val sdk.ValAddress) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.GetValidatorSlashEventPrefix(val))
|
2019-01-23 03:37:03 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
store.Delete(iter.Key())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// delete all slash events
|
2019-01-23 03:37:03 -08:00
|
|
|
func (k Keeper) DeleteAllValidatorSlashEvents(ctx sdk.Context) {
|
2019-01-16 13:38:05 -08:00
|
|
|
store := ctx.KVStore(k.storeKey)
|
2020-01-10 12:08:14 -08:00
|
|
|
iter := sdk.KVStorePrefixIterator(store, types.ValidatorSlashEventPrefix)
|
2019-01-16 13:38:05 -08:00
|
|
|
defer iter.Close()
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
store.Delete(iter.Key())
|
|
|
|
}
|
|
|
|
}
|