refactor(distribution)!: use collections for DelegatorStartingInfo state (#16483)
Co-authored-by: unknown unknown <unknown@unknown>
This commit is contained in:
co-authored by
unknown unknown
parent
bbb452f609
commit
b3da8bb4e8
@@ -2,8 +2,10 @@ package keeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
"cosmossdk.io/math"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@@ -31,7 +33,7 @@ func (k Keeper) initializeDelegation(ctx context.Context, val sdk.ValAddress, de
|
||||
// we don't store directly, so multiply delegation shares * (tokens per share)
|
||||
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
|
||||
stake := validator.TokensFromSharesTruncated(delegation.GetShares())
|
||||
return k.SetDelegatorStartingInfo(ctx, val, del, types.NewDelegatorStartingInfo(previousPeriod, stake, uint64(sdkCtx.BlockHeight())))
|
||||
return k.DelegatorStartingInfo.Set(ctx, collections.Join(val, del), types.NewDelegatorStartingInfo(previousPeriod, stake, uint64(sdkCtx.BlockHeight())))
|
||||
}
|
||||
|
||||
// calculate the rewards accrued by a delegation between two periods
|
||||
@@ -71,8 +73,8 @@ func (k Keeper) calculateDelegationRewardsBetween(ctx context.Context, val staki
|
||||
// calculate the total rewards accrued by a delegation
|
||||
func (k Keeper) CalculateDelegationRewards(ctx context.Context, val stakingtypes.ValidatorI, del stakingtypes.DelegationI, endingPeriod uint64) (rewards sdk.DecCoins, err error) {
|
||||
// fetch starting info for delegation
|
||||
startingInfo, err := k.GetDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr())
|
||||
if err != nil {
|
||||
startingInfo, err := k.DelegatorStartingInfo.Get(ctx, collections.Join(del.GetValidatorAddr(), del.GetDelegatorAddr()))
|
||||
if err != nil && !errors.Is(err, collections.ErrNotFound) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -167,7 +169,7 @@ func (k Keeper) CalculateDelegationRewards(ctx context.Context, val stakingtypes
|
||||
|
||||
func (k Keeper) withdrawDelegationRewards(ctx context.Context, val stakingtypes.ValidatorI, del stakingtypes.DelegationI) (sdk.Coins, error) {
|
||||
// check existence of delegator starting info
|
||||
hasInfo, err := k.HasDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr())
|
||||
hasInfo, err := k.DelegatorStartingInfo.Has(ctx, collections.Join(del.GetValidatorAddr(), del.GetDelegatorAddr()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -241,8 +243,8 @@ func (k Keeper) withdrawDelegationRewards(ctx context.Context, val stakingtypes.
|
||||
}
|
||||
|
||||
// decrement reference count of starting period
|
||||
startingInfo, err := k.GetDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr())
|
||||
if err != nil {
|
||||
startingInfo, err := k.DelegatorStartingInfo.Get(ctx, collections.Join(del.GetValidatorAddr(), del.GetDelegatorAddr()))
|
||||
if err != nil && !errors.Is(err, collections.ErrNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -253,7 +255,7 @@ func (k Keeper) withdrawDelegationRewards(ctx context.Context, val stakingtypes.
|
||||
}
|
||||
|
||||
// remove delegator starting info
|
||||
err = k.DeleteDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr())
|
||||
err = k.DelegatorStartingInfo.Remove(ctx, collections.Join(del.GetValidatorAddr(), del.GetDelegatorAddr()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = k.SetDelegatorStartingInfo(ctx, valAddr, delegatorAddress, del.StartingInfo)
|
||||
err = k.DelegatorStartingInfo.Set(ctx, collections.Join(valAddr, sdk.AccAddress(delegatorAddress)), del.StartingInfo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -212,16 +212,17 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
|
||||
}
|
||||
|
||||
dels := make([]types.DelegatorStartingInfoRecord, 0)
|
||||
k.IterateDelegatorStartingInfos(ctx,
|
||||
func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool) {
|
||||
dels = append(dels, types.DelegatorStartingInfoRecord{
|
||||
ValidatorAddress: val.String(),
|
||||
DelegatorAddress: del.String(),
|
||||
StartingInfo: info,
|
||||
})
|
||||
return false
|
||||
},
|
||||
)
|
||||
err = k.DelegatorStartingInfo.Walk(ctx, nil, func(key collections.Pair[sdk.ValAddress, sdk.AccAddress], value types.DelegatorStartingInfo) (stop bool, err error) {
|
||||
dels = append(dels, types.DelegatorStartingInfoRecord{
|
||||
DelegatorAddress: key.K2().String(),
|
||||
ValidatorAddress: key.K1().String(),
|
||||
StartingInfo: value,
|
||||
})
|
||||
return false, nil
|
||||
})
|
||||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
slashes := make([]types.ValidatorSlashEventRecord, 0)
|
||||
k.IterateValidatorSlashEvents(ctx,
|
||||
|
||||
@@ -32,6 +32,7 @@ type Keeper struct {
|
||||
FeePool collections.Item[types.FeePool]
|
||||
DelegatorsWithdrawAddress collections.Map[sdk.AccAddress, sdk.AccAddress]
|
||||
ValidatorCurrentRewards collections.Map[sdk.ValAddress, types.ValidatorCurrentRewards]
|
||||
DelegatorStartingInfo collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], types.DelegatorStartingInfo]
|
||||
|
||||
feeCollectorName string // name of the FeeCollector ModuleAccount
|
||||
}
|
||||
@@ -72,6 +73,13 @@ func NewKeeper(
|
||||
sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
|
||||
codec.CollValue[types.ValidatorCurrentRewards](cdc),
|
||||
),
|
||||
DelegatorStartingInfo: collections.NewMap(
|
||||
sb,
|
||||
types.DelegatorStartingInfoPrefix,
|
||||
"delegators_starting_info",
|
||||
collections.PairKeyCodec(sdk.ValAddressKey, sdk.LengthPrefixedAddressKey(sdk.AccAddressKey)), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
|
||||
codec.CollValue[types.DelegatorStartingInfo](cdc),
|
||||
),
|
||||
}
|
||||
|
||||
schema, err := sb.Build()
|
||||
|
||||
@@ -52,56 +52,6 @@ func (k Keeper) SetPreviousProposerConsAddr(ctx context.Context, consAddr sdk.Co
|
||||
store.Set(types.ProposerKey, bz)
|
||||
}
|
||||
|
||||
// get the starting info associated with a delegator
|
||||
func (k Keeper) GetDelegatorStartingInfo(ctx context.Context, val sdk.ValAddress, del sdk.AccAddress) (period types.DelegatorStartingInfo, err error) {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
b, err := store.Get(types.GetDelegatorStartingInfoKey(val, del))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = k.cdc.Unmarshal(b, &period)
|
||||
return period, err
|
||||
}
|
||||
|
||||
// set the starting info associated with a delegator
|
||||
func (k Keeper) SetDelegatorStartingInfo(ctx context.Context, val sdk.ValAddress, del sdk.AccAddress, period types.DelegatorStartingInfo) error {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
b, err := k.cdc.Marshal(&period)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return store.Set(types.GetDelegatorStartingInfoKey(val, del), b)
|
||||
}
|
||||
|
||||
// check existence of the starting info associated with a delegator
|
||||
func (k Keeper) HasDelegatorStartingInfo(ctx context.Context, val sdk.ValAddress, del sdk.AccAddress) (bool, error) {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
return store.Has(types.GetDelegatorStartingInfoKey(val, del))
|
||||
}
|
||||
|
||||
// delete the starting info associated with a delegator
|
||||
func (k Keeper) DeleteDelegatorStartingInfo(ctx context.Context, val sdk.ValAddress, del sdk.AccAddress) error {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
return store.Delete(types.GetDelegatorStartingInfoKey(val, del))
|
||||
}
|
||||
|
||||
// iterate over delegator starting infos
|
||||
func (k Keeper) IterateDelegatorStartingInfos(ctx context.Context, handler func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool)) {
|
||||
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
|
||||
iter := storetypes.KVStorePrefixIterator(store, types.DelegatorStartingInfoPrefix)
|
||||
defer iter.Close()
|
||||
for ; iter.Valid(); iter.Next() {
|
||||
var info types.DelegatorStartingInfo
|
||||
k.cdc.MustUnmarshal(iter.Value(), &info)
|
||||
val, del := types.GetDelegatorStartingInfoAddresses(iter.Key())
|
||||
if handler(val, del, info) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get historical rewards for a particular period
|
||||
func (k Keeper) GetValidatorHistoricalRewards(ctx context.Context, val sdk.ValAddress, period uint64) (rewards types.ValidatorHistoricalRewards, err error) {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
|
||||
Reference in New Issue
Block a user