refactor(distribution)!: use collections for ValidatorOutstandingRewards state management (#16590)
Co-authored-by: unknown unknown <unknown@unknown>
This commit is contained in:
parent
13e61bd0c5
commit
0529dbcd20
@ -53,7 +53,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
* remove `Keeper`: `IterateDelegatorStartingInfo`, `GetDelegatorStartingInfo`, `SetDelegatorStartingInfo`, `DeleteDelegatorStartingInfo`, `HasDelegatorStartingInfo`
|
||||
* (x/distribution) [#16571](https://github.com/cosmos/cosmos-sdk/pull/16571) use collections for `ValidatorAccumulatedCommission` state management:
|
||||
* remove `Keeper`: `IterateValidatorAccumulatedCommission`, `GetValidatorAccumulatedCommission`, `SetValidatorAccumulatedCommission`, `DeleteValidatorAccumulatedCommission`
|
||||
|
||||
* (x/distribution) [#16590](https://github.com/cosmos/cosmos-sdk/pull/16590) use collections for `ValidatorOutstandingRewards` state management:
|
||||
* remove `Keeper`: `IterateValidatorOutstandingRewards`, `GetValidatorOutstandingRewards`, `SetValidatorOutstandingRewards`, `DeleteValidatorOutstandingRewards`
|
||||
### Bug Fixes
|
||||
|
||||
* (x/auth/types) [#16554](https://github.com/cosmos/cosmos-sdk/pull/16554) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking.
|
||||
|
||||
@ -101,10 +101,10 @@ func TestGRPCValidatorOutstandingRewards(t *testing.T) {
|
||||
tstaking.CreateValidator(f.valAddr, valConsPk0, sdk.NewInt(initialStake), true)
|
||||
|
||||
// set outstanding rewards
|
||||
err := f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, types.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
err := f.distrKeeper.ValidatorOutstandingRewards.Set(f.sdkCtx, f.valAddr, types.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
assert.NilError(t, err)
|
||||
|
||||
rewards, err := f.distrKeeper.GetValidatorOutstandingRewards(f.sdkCtx, f.valAddr)
|
||||
rewards, err := f.distrKeeper.ValidatorOutstandingRewards.Get(f.sdkCtx, f.valAddr)
|
||||
assert.NilError(t, err)
|
||||
|
||||
testCases := []struct {
|
||||
@ -519,7 +519,7 @@ func TestGRPCDelegationRewards(t *testing.T) {
|
||||
// setup current rewards and outstanding rewards
|
||||
currentRewards := types.NewValidatorCurrentRewards(decCoins, 3)
|
||||
assert.NilError(t, f.distrKeeper.ValidatorCurrentRewards.Set(f.sdkCtx, f.valAddr, currentRewards))
|
||||
assert.NilError(t, f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, types.ValidatorOutstandingRewards{Rewards: decCoins}))
|
||||
assert.NilError(t, f.distrKeeper.ValidatorOutstandingRewards.Set(f.sdkCtx, f.valAddr, types.ValidatorOutstandingRewards{Rewards: decCoins}))
|
||||
|
||||
expRes := &types.QueryDelegationRewardsResponse{
|
||||
Rewards: sdk.DecCoins{sdk.DecCoin{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initialStake / 10)}},
|
||||
|
||||
@ -200,7 +200,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) {
|
||||
currentRewards := distrtypes.NewValidatorCurrentRewards(decCoins, 3)
|
||||
err = f.distrKeeper.ValidatorCurrentRewards.Set(f.sdkCtx, f.valAddr, currentRewards)
|
||||
require.NoError(t, err)
|
||||
err = f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, distrtypes.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
err = f.distrKeeper.ValidatorOutstandingRewards.Set(f.sdkCtx, f.valAddr, distrtypes.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
require.NoError(t, err)
|
||||
initOutstandingRewards, err := f.distrKeeper.GetValidatorOutstandingRewardsCoins(f.sdkCtx, f.valAddr)
|
||||
assert.NilError(t, err)
|
||||
@ -302,7 +302,8 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) {
|
||||
// check rewards
|
||||
curFeePool, _ := f.distrKeeper.FeePool.Get(f.sdkCtx)
|
||||
rewards := curFeePool.GetCommunityPool().Sub(initFeePool.CommunityPool)
|
||||
curOutstandingRewards, _ := f.distrKeeper.GetValidatorOutstandingRewards(f.sdkCtx, f.valAddr)
|
||||
curOutstandingRewards, err := f.distrKeeper.ValidatorOutstandingRewards.Get(f.sdkCtx, f.valAddr)
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, rewards, initOutstandingRewards.Sub(curOutstandingRewards.Rewards))
|
||||
}
|
||||
|
||||
@ -477,7 +478,7 @@ func TestMsgWithdrawValidatorCommission(t *testing.T) {
|
||||
assert.DeepEqual(t, expCoins, balance)
|
||||
|
||||
// set outstanding rewards
|
||||
err = f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, distrtypes.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
err = f.distrKeeper.ValidatorOutstandingRewards.Set(f.sdkCtx, f.valAddr, distrtypes.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
require.NoError(t, err)
|
||||
|
||||
// set commission
|
||||
@ -947,7 +948,8 @@ func TestMsgDepositValidatorRewardsPool(t *testing.T) {
|
||||
assert.NilError(t, err)
|
||||
|
||||
// check validator outstanding rewards
|
||||
outstandingRewards, _ := f.distrKeeper.GetValidatorOutstandingRewards(f.sdkCtx, val)
|
||||
outstandingRewards, err := f.distrKeeper.ValidatorOutstandingRewards.Get(f.sdkCtx, val)
|
||||
assert.NilError(t, err)
|
||||
for _, c := range tc.msg.Amount {
|
||||
x := outstandingRewards.Rewards.AmountOf(c.Denom)
|
||||
assert.DeepEqual(t, x, sdk.NewDecFromInt(c.Amount))
|
||||
|
||||
@ -2,15 +2,17 @@ package keeper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
)
|
||||
|
||||
// get outstanding rewards
|
||||
func (k Keeper) GetValidatorOutstandingRewardsCoins(ctx context.Context, val sdk.ValAddress) (sdk.DecCoins, error) {
|
||||
rewards, err := k.GetValidatorOutstandingRewards(ctx, val)
|
||||
if err != nil {
|
||||
rewards, err := k.ValidatorOutstandingRewards.Get(ctx, val)
|
||||
if err != nil && !errors.Is(err, collections.ErrNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@ -128,11 +128,11 @@ func (k Keeper) AllocateTokensToValidator(ctx context.Context, val stakingtypes.
|
||||
),
|
||||
)
|
||||
|
||||
outstanding, err := k.GetValidatorOutstandingRewards(ctx, val.GetOperator())
|
||||
if err != nil {
|
||||
outstanding, err := k.ValidatorOutstandingRewards.Get(ctx, val.GetOperator())
|
||||
if err != nil && !errors.Is(err, collections.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
outstanding.Rewards = outstanding.Rewards.Add(tokens...)
|
||||
return k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), outstanding)
|
||||
return k.ValidatorOutstandingRewards.Set(ctx, val.GetOperator(), outstanding)
|
||||
}
|
||||
|
||||
@ -130,13 +130,11 @@ func TestAllocateTokensToManyValidators(t *testing.T) {
|
||||
}
|
||||
|
||||
// assert initial state: zero outstanding rewards, zero community pool, zero commission, zero current rewards
|
||||
val0OutstandingRewards, err := distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr0)
|
||||
require.NoError(t, err)
|
||||
require.True(t, val0OutstandingRewards.Rewards.IsZero())
|
||||
_, err = distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr0)
|
||||
require.ErrorIs(t, err, collections.ErrNotFound)
|
||||
|
||||
val1OutstandingRewards, err := distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr1)
|
||||
require.NoError(t, err)
|
||||
require.True(t, val1OutstandingRewards.Rewards.IsZero())
|
||||
_, err = distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr1)
|
||||
require.ErrorIs(t, err, collections.ErrNotFound)
|
||||
|
||||
feePool, err := distrKeeper.FeePool.Get(ctx)
|
||||
require.NoError(t, err)
|
||||
@ -170,11 +168,11 @@ func TestAllocateTokensToManyValidators(t *testing.T) {
|
||||
require.NoError(t, distrKeeper.AllocateTokens(ctx, 200, votes))
|
||||
|
||||
// 98 outstanding rewards (100 less 2 to community pool)
|
||||
val0OutstandingRewards, err = distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr0)
|
||||
val0OutstandingRewards, err := distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr0)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDecWithPrec(490, 1)}}, val0OutstandingRewards.Rewards)
|
||||
|
||||
val1OutstandingRewards, err = distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr1)
|
||||
val1OutstandingRewards, err := distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr1)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDecWithPrec(490, 1)}}, val1OutstandingRewards.Rewards)
|
||||
|
||||
@ -269,13 +267,11 @@ func TestAllocateTokensTruncation(t *testing.T) {
|
||||
}
|
||||
|
||||
// assert initial state: zero outstanding rewards, zero community pool, zero commission, zero current rewards
|
||||
val0OutstandingRewards, err := distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr0)
|
||||
require.NoError(t, err)
|
||||
require.True(t, val0OutstandingRewards.Rewards.IsZero())
|
||||
_, err = distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr0)
|
||||
require.ErrorIs(t, err, collections.ErrNotFound)
|
||||
|
||||
val1OutstandingRewards, err := distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr1)
|
||||
require.NoError(t, err)
|
||||
require.True(t, val1OutstandingRewards.Rewards.IsZero())
|
||||
_, err = distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr1)
|
||||
require.ErrorIs(t, err, collections.ErrNotFound)
|
||||
|
||||
feePool, err := distrKeeper.FeePool.Get(ctx)
|
||||
require.NoError(t, err)
|
||||
@ -311,15 +307,15 @@ func TestAllocateTokensTruncation(t *testing.T) {
|
||||
}
|
||||
require.NoError(t, distrKeeper.AllocateTokens(ctx, 31, votes))
|
||||
|
||||
val0OutstandingRewards, err = distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr0)
|
||||
val0OutstandingRewards, err := distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr0)
|
||||
require.NoError(t, err)
|
||||
require.True(t, val0OutstandingRewards.Rewards.IsValid())
|
||||
|
||||
val1OutstandingRewards, err = distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr1)
|
||||
val1OutstandingRewards, err := distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr1)
|
||||
require.NoError(t, err)
|
||||
require.True(t, val1OutstandingRewards.Rewards.IsValid())
|
||||
|
||||
val2OutstandingRewards, err := distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr2)
|
||||
val2OutstandingRewards, err := distrKeeper.ValidatorOutstandingRewards.Get(ctx, valAddr2)
|
||||
require.NoError(t, err)
|
||||
require.True(t, val2OutstandingRewards.Rewards.IsValid())
|
||||
}
|
||||
|
||||
@ -226,7 +226,7 @@ func (k Keeper) withdrawDelegationRewards(ctx context.Context, val stakingtypes.
|
||||
|
||||
// update the outstanding rewards and the community pool only if the
|
||||
// transaction was successful
|
||||
err = k.SetValidatorOutstandingRewards(ctx, del.GetValidatorAddr(), types.ValidatorOutstandingRewards{Rewards: outstanding.Sub(rewards)})
|
||||
err = k.ValidatorOutstandingRewards.Set(ctx, del.GetValidatorAddr(), types.ValidatorOutstandingRewards{Rewards: outstanding.Sub(rewards)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = k.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards})
|
||||
err = k.ValidatorOutstandingRewards.Set(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -164,15 +164,17 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
|
||||
|
||||
outstanding := make([]types.ValidatorOutstandingRewardsRecord, 0)
|
||||
|
||||
k.IterateValidatorOutstandingRewards(ctx,
|
||||
func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
|
||||
outstanding = append(outstanding, types.ValidatorOutstandingRewardsRecord{
|
||||
ValidatorAddress: addr.String(),
|
||||
OutstandingRewards: rewards.Rewards,
|
||||
})
|
||||
return false
|
||||
},
|
||||
err = k.ValidatorOutstandingRewards.Walk(ctx, nil, func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool, err error) {
|
||||
outstanding = append(outstanding, types.ValidatorOutstandingRewardsRecord{
|
||||
ValidatorAddress: addr.String(),
|
||||
OutstandingRewards: rewards.Rewards,
|
||||
})
|
||||
return false, nil
|
||||
},
|
||||
)
|
||||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
acc := make([]types.ValidatorAccumulatedCommissionRecord, 0)
|
||||
err = k.ValidatorsAccumulatedCommission.Walk(ctx, nil, func(addr sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool, err error) {
|
||||
|
||||
@ -112,8 +112,8 @@ func (k Querier) ValidatorOutstandingRewards(c context.Context, req *types.Query
|
||||
return nil, errors.Wrapf(types.ErrNoValidatorExists, valAdr.String())
|
||||
}
|
||||
|
||||
rewards, err := k.GetValidatorOutstandingRewards(ctx, valAdr)
|
||||
if err != nil {
|
||||
rewards, err := k.Keeper.ValidatorOutstandingRewards.Get(ctx, valAdr)
|
||||
if err != nil && !errors.IsOf(err, collections.ErrNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@ -93,7 +93,7 @@ func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, _ sdk.ConsAddress, valAddr
|
||||
}
|
||||
|
||||
// delete outstanding
|
||||
err = h.k.DeleteValidatorOutstandingRewards(ctx, valAddr)
|
||||
err = h.k.ValidatorOutstandingRewards.Remove(ctx, valAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
@ -46,14 +48,17 @@ func NonNegativeOutstandingInvariant(k Keeper) sdk.Invariant {
|
||||
var count int
|
||||
var outstanding sdk.DecCoins
|
||||
|
||||
k.IterateValidatorOutstandingRewards(ctx, func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
|
||||
err := k.ValidatorOutstandingRewards.Walk(ctx, nil, func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool, err error) {
|
||||
outstanding = rewards.GetRewards()
|
||||
if outstanding.IsAnyNegative() {
|
||||
count++
|
||||
msg += fmt.Sprintf("\t%v has negative outstanding coins: %v\n", addr, outstanding)
|
||||
}
|
||||
return false
|
||||
return false, nil
|
||||
})
|
||||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) {
|
||||
return sdk.FormatInvariant(types.ModuleName, "nonnegative outstanding", err.Error()), true
|
||||
}
|
||||
broken := count != 0
|
||||
|
||||
return sdk.FormatInvariant(types.ModuleName, "nonnegative outstanding",
|
||||
@ -141,10 +146,13 @@ func ReferenceCountInvariant(k Keeper) sdk.Invariant {
|
||||
func ModuleAccountInvariant(k Keeper) sdk.Invariant {
|
||||
return func(ctx sdk.Context) (string, bool) {
|
||||
var expectedCoins sdk.DecCoins
|
||||
k.IterateValidatorOutstandingRewards(ctx, func(_ sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
|
||||
err := k.ValidatorOutstandingRewards.Walk(ctx, nil, func(_ sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool, err error) {
|
||||
expectedCoins = expectedCoins.Add(rewards.Rewards...)
|
||||
return false
|
||||
return false, nil
|
||||
})
|
||||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) {
|
||||
return sdk.FormatInvariant(types.ModuleName, "module account coins", err.Error()), true
|
||||
}
|
||||
|
||||
communityPool, err := k.FeePool.Get(ctx)
|
||||
if err != nil {
|
||||
|
||||
@ -35,6 +35,7 @@ type Keeper struct {
|
||||
ValidatorCurrentRewards collections.Map[sdk.ValAddress, types.ValidatorCurrentRewards]
|
||||
DelegatorStartingInfo collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], types.DelegatorStartingInfo]
|
||||
ValidatorsAccumulatedCommission collections.Map[sdk.ValAddress, types.ValidatorAccumulatedCommission]
|
||||
ValidatorOutstandingRewards collections.Map[sdk.ValAddress, types.ValidatorOutstandingRewards]
|
||||
|
||||
feeCollectorName string // name of the FeeCollector ModuleAccount
|
||||
}
|
||||
@ -89,6 +90,13 @@ func NewKeeper(
|
||||
sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
|
||||
codec.CollValue[types.ValidatorAccumulatedCommission](cdc),
|
||||
),
|
||||
ValidatorOutstandingRewards: collections.NewMap(
|
||||
sb,
|
||||
types.ValidatorOutstandingRewardsPrefix,
|
||||
"validator_outstanding_rewards",
|
||||
sdk.LengthPrefixedAddressKey(sdk.ValAddressKey), // nolint: staticcheck // sdk.LengthPrefixedAddressKey is needed to retain state compatibility
|
||||
codec.CollValue[types.ValidatorOutstandingRewards](cdc),
|
||||
),
|
||||
}
|
||||
|
||||
schema, err := sb.Build()
|
||||
@ -181,12 +189,12 @@ func (k Keeper) WithdrawValidatorCommission(ctx context.Context, valAddr sdk.Val
|
||||
return nil, err
|
||||
}
|
||||
// update outstanding
|
||||
outstanding, err := k.GetValidatorOutstandingRewards(ctx, valAddr)
|
||||
outstanding, err := k.ValidatorOutstandingRewards.Get(ctx, valAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = k.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: outstanding.Rewards.Sub(sdk.NewDecCoinsFromCoins(commission...))})
|
||||
err = k.ValidatorOutstandingRewards.Set(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: outstanding.Rewards.Sub(sdk.NewDecCoinsFromCoins(commission...))})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -217,12 +225,14 @@ func (k Keeper) WithdrawValidatorCommission(ctx context.Context, valAddr sdk.Val
|
||||
|
||||
// GetTotalRewards returns the total amount of fee distribution rewards held in the store
|
||||
func (k Keeper) GetTotalRewards(ctx context.Context) (totalRewards sdk.DecCoins) {
|
||||
k.IterateValidatorOutstandingRewards(ctx,
|
||||
func(_ sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
|
||||
totalRewards = totalRewards.Add(rewards.Rewards...)
|
||||
return false
|
||||
},
|
||||
err := k.ValidatorOutstandingRewards.Walk(ctx, nil, func(_ sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool, err error) {
|
||||
totalRewards = totalRewards.Add(rewards.Rewards...)
|
||||
return false, nil
|
||||
},
|
||||
)
|
||||
if err != nil && !errors.Is(err, collections.ErrInvalidIterator) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return totalRewards
|
||||
}
|
||||
|
||||
@ -106,7 +106,7 @@ func TestWithdrawValidatorCommission(t *testing.T) {
|
||||
)
|
||||
|
||||
// set outstanding rewards
|
||||
require.NoError(t, distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: valCommission}))
|
||||
require.NoError(t, distrKeeper.ValidatorOutstandingRewards.Set(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: valCommission}))
|
||||
|
||||
// set commission
|
||||
require.NoError(t, distrKeeper.ValidatorsAccumulatedCommission.Set(ctx, valAddr, types.ValidatorAccumulatedCommission{Commission: valCommission}))
|
||||
@ -162,8 +162,8 @@ func TestGetTotalRewards(t *testing.T) {
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(3).Quo(math.LegacyNewDec(2))),
|
||||
}
|
||||
|
||||
require.NoError(t, distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr0, types.ValidatorOutstandingRewards{Rewards: valCommission}))
|
||||
require.NoError(t, distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr1, types.ValidatorOutstandingRewards{Rewards: valCommission}))
|
||||
require.NoError(t, distrKeeper.ValidatorOutstandingRewards.Set(ctx, valAddr0, types.ValidatorOutstandingRewards{Rewards: valCommission}))
|
||||
require.NoError(t, distrKeeper.ValidatorOutstandingRewards.Set(ctx, valAddr1, types.ValidatorOutstandingRewards{Rewards: valCommission}))
|
||||
|
||||
expectedRewards := valCommission.MulDec(math.LegacyNewDec(2))
|
||||
totalRewards := distrKeeper.GetTotalRewards(ctx)
|
||||
|
||||
@ -129,48 +129,6 @@ func (k Keeper) GetValidatorHistoricalReferenceCount(ctx context.Context) (count
|
||||
return
|
||||
}
|
||||
|
||||
// get validator outstanding rewards
|
||||
func (k Keeper) GetValidatorOutstandingRewards(ctx context.Context, val sdk.ValAddress) (rewards types.ValidatorOutstandingRewards, err error) {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
bz, err := store.Get(types.GetValidatorOutstandingRewardsKey(val))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = k.cdc.Unmarshal(bz, &rewards)
|
||||
return
|
||||
}
|
||||
|
||||
// set validator outstanding rewards
|
||||
func (k Keeper) SetValidatorOutstandingRewards(ctx context.Context, val sdk.ValAddress, rewards types.ValidatorOutstandingRewards) error {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
b, err := k.cdc.Marshal(&rewards)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return store.Set(types.GetValidatorOutstandingRewardsKey(val), b)
|
||||
}
|
||||
|
||||
// delete validator outstanding rewards
|
||||
func (k Keeper) DeleteValidatorOutstandingRewards(ctx context.Context, val sdk.ValAddress) error {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
return store.Delete(types.GetValidatorOutstandingRewardsKey(val))
|
||||
}
|
||||
|
||||
// iterate validator outstanding rewards
|
||||
func (k Keeper) IterateValidatorOutstandingRewards(ctx context.Context, handler func(val sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool)) {
|
||||
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
|
||||
iter := storetypes.KVStorePrefixIterator(store, types.ValidatorOutstandingRewardsPrefix)
|
||||
defer iter.Close()
|
||||
for ; iter.Valid(); iter.Next() {
|
||||
rewards := types.ValidatorOutstandingRewards{}
|
||||
k.cdc.MustUnmarshal(iter.Value(), &rewards)
|
||||
addr := types.GetValidatorOutstandingRewardsAddress(iter.Key())
|
||||
if handler(addr, rewards) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get slash event for height
|
||||
func (k Keeper) GetValidatorSlashEvent(ctx context.Context, val sdk.ValAddress, height, period uint64) (event types.ValidatorSlashEvent, found bool, err error) {
|
||||
store := k.storeService.OpenKVStore(ctx)
|
||||
|
||||
@ -35,7 +35,7 @@ func (k Keeper) initializeValidator(ctx context.Context, val stakingtypes.Valida
|
||||
}
|
||||
|
||||
// set outstanding rewards
|
||||
err = k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), types.ValidatorOutstandingRewards{Rewards: sdk.DecCoins{}})
|
||||
err = k.ValidatorOutstandingRewards.Set(ctx, val.GetOperator(), types.ValidatorOutstandingRewards{Rewards: sdk.DecCoins{}})
|
||||
return err
|
||||
}
|
||||
|
||||
@ -58,8 +58,8 @@ func (k Keeper) IncrementValidatorPeriod(ctx context.Context, val stakingtypes.V
|
||||
return 0, err
|
||||
}
|
||||
|
||||
outstanding, err := k.GetValidatorOutstandingRewards(ctx, val.GetOperator())
|
||||
if err != nil {
|
||||
outstanding, err := k.ValidatorOutstandingRewards.Get(ctx, val.GetOperator())
|
||||
if err != nil && !errors.Is(err, collections.ErrNotFound) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ func (k Keeper) IncrementValidatorPeriod(ctx context.Context, val stakingtypes.V
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = k.SetValidatorOutstandingRewards(ctx, val.GetOperator(), outstanding)
|
||||
err = k.ValidatorOutstandingRewards.Set(ctx, val.GetOperator(), outstanding)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@ -48,7 +48,7 @@ func TestStoreMigration(t *testing.T) {
|
||||
{
|
||||
"ValidatorOutstandingRewards",
|
||||
v1.GetValidatorOutstandingRewardsKey(valAddr),
|
||||
types.GetValidatorOutstandingRewardsKey(valAddr),
|
||||
append(types.ValidatorOutstandingRewardsPrefix, address.MustLengthPrefix(valAddr.Bytes())...),
|
||||
},
|
||||
{
|
||||
"DelegatorWithdrawAddr",
|
||||
|
||||
@ -31,7 +31,6 @@ func TestDecodeDistributionStore(t *testing.T) {
|
||||
decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyOneDec())}
|
||||
feePool := types.InitialFeePool()
|
||||
feePool.CommunityPool = decCoins
|
||||
outstanding := types.ValidatorOutstandingRewards{Rewards: decCoins}
|
||||
historicalRewards := types.NewValidatorHistoricalRewards(decCoins, 100)
|
||||
slashEvent := types.NewValidatorSlashEvent(10, math.LegacyOneDec())
|
||||
|
||||
@ -39,7 +38,6 @@ func TestDecodeDistributionStore(t *testing.T) {
|
||||
Pairs: []kv.Pair{
|
||||
{Key: types.FeePoolKey, Value: cdc.MustMarshal(&feePool)},
|
||||
{Key: types.ProposerKey, Value: consAddr1.Bytes()},
|
||||
{Key: types.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshal(&outstanding)},
|
||||
{Key: types.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshal(&historicalRewards)},
|
||||
{Key: types.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshal(&slashEvent)},
|
||||
{Key: []byte{0x99}, Value: []byte{0x99}},
|
||||
@ -52,7 +50,6 @@ func TestDecodeDistributionStore(t *testing.T) {
|
||||
}{
|
||||
{"FeePool", fmt.Sprintf("%v\n%v", feePool, feePool)},
|
||||
{"Proposer", fmt.Sprintf("%v\n%v", consAddr1, consAddr1)},
|
||||
{"ValidatorOutstandingRewards", fmt.Sprintf("%v\n%v", outstanding, outstanding)},
|
||||
{"ValidatorHistoricalRewards", fmt.Sprintf("%v\n%v", historicalRewards, historicalRewards)},
|
||||
{"ValidatorSlashEvent", fmt.Sprintf("%v\n%v", slashEvent, slashEvent)},
|
||||
{"other", ""},
|
||||
|
||||
@ -73,10 +73,11 @@ func (suite *SimTestSuite) TestSimulateMsgSetWithdrawAddress() {
|
||||
r := rand.New(s)
|
||||
accounts := suite.getTestingAccounts(r, 3)
|
||||
|
||||
suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
|
||||
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
|
||||
Height: suite.app.LastBlockHeight() + 1,
|
||||
Hash: suite.app.LastCommitID().Hash,
|
||||
})
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// execute operation
|
||||
op := simulation.SimulateMsgSetWithdrawAddress(suite.txConfig, suite.accountKeeper, suite.bankKeeper, suite.distrKeeper)
|
||||
@ -168,17 +169,18 @@ func (suite *SimTestSuite) testSimulateMsgWithdrawValidatorCommission(tokenName
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(1).Quo(math.LegacyNewDec(1))),
|
||||
)
|
||||
|
||||
suite.distrKeeper.SetValidatorOutstandingRewards(suite.ctx, validator0.GetOperator(), types.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
suite.distrKeeper.SetValidatorOutstandingRewards(suite.ctx, suite.genesisVals[0].GetOperator(), types.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
suite.Require().NoError(suite.distrKeeper.ValidatorOutstandingRewards.Set(suite.ctx, validator0.GetOperator(), types.ValidatorOutstandingRewards{Rewards: valCommission}))
|
||||
suite.Require().NoError(suite.distrKeeper.ValidatorOutstandingRewards.Set(suite.ctx, suite.genesisVals[0].GetOperator(), types.ValidatorOutstandingRewards{Rewards: valCommission}))
|
||||
|
||||
// setup validator accumulated commission
|
||||
suite.Require().NoError(suite.distrKeeper.ValidatorsAccumulatedCommission.Set(suite.ctx, validator0.GetOperator(), types.ValidatorAccumulatedCommission{Commission: valCommission}))
|
||||
suite.Require().NoError(suite.distrKeeper.ValidatorsAccumulatedCommission.Set(suite.ctx, suite.genesisVals[0].GetOperator(), types.ValidatorAccumulatedCommission{Commission: valCommission}))
|
||||
|
||||
suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
|
||||
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
|
||||
Height: suite.app.LastBlockHeight() + 1,
|
||||
Hash: suite.app.LastCommitID().Hash,
|
||||
})
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// execute operation
|
||||
op := simulation.SimulateMsgWithdrawValidatorCommission(suite.txConfig, suite.accountKeeper, suite.bankKeeper, suite.distrKeeper, suite.stakingKeeper)
|
||||
@ -206,10 +208,11 @@ func (suite *SimTestSuite) TestSimulateMsgFundCommunityPool() {
|
||||
r := rand.New(s)
|
||||
accounts := suite.getTestingAccounts(r, 3)
|
||||
|
||||
suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
|
||||
_, err := suite.app.FinalizeBlock(&abci.RequestFinalizeBlock{
|
||||
Height: suite.app.LastBlockHeight() + 1,
|
||||
Hash: suite.app.LastCommitID().Hash,
|
||||
})
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// execute operation
|
||||
op := simulation.SimulateMsgFundCommunityPool(suite.txConfig, suite.accountKeeper, suite.bankKeeper, suite.distrKeeper, suite.stakingKeeper)
|
||||
|
||||
@ -44,33 +44,18 @@ const (
|
||||
//
|
||||
// - 0x09: Params
|
||||
var (
|
||||
FeePoolKey = collections.NewPrefix(0) // key for global distribution state
|
||||
ProposerKey = []byte{0x01} // key for the proposer operator address
|
||||
ValidatorOutstandingRewardsPrefix = []byte{0x02} // key for outstanding rewards
|
||||
|
||||
FeePoolKey = collections.NewPrefix(0) // key for global distribution state
|
||||
ProposerKey = []byte{0x01} // key for the proposer operator address
|
||||
ValidatorOutstandingRewardsPrefix = collections.NewPrefix(2) // key for outstanding rewards
|
||||
DelegatorWithdrawAddrPrefix = collections.NewPrefix(3) // key for delegator withdraw address
|
||||
DelegatorStartingInfoPrefix = collections.NewPrefix(4) // key for delegator starting info
|
||||
ValidatorHistoricalRewardsPrefix = []byte{0x05} // key for historical validators rewards / stake
|
||||
ValidatorCurrentRewardsPrefix = collections.NewPrefix(6) // key for current validator rewards
|
||||
ValidatorAccumulatedCommissionPrefix = collections.NewPrefix(7) // key for accumulated validator commission
|
||||
ValidatorSlashEventPrefix = []byte{0x08} // key for validator slash fraction
|
||||
|
||||
ParamsKey = collections.NewPrefix(9) // key for distribution module params
|
||||
ParamsKey = collections.NewPrefix(9) // key for distribution module params
|
||||
)
|
||||
|
||||
// GetValidatorOutstandingRewardsAddress creates an address from a validator's outstanding rewards key.
|
||||
func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) {
|
||||
// key is in the format:
|
||||
// 0x02<valAddrLen (1 Byte)><valAddr_Bytes>
|
||||
|
||||
// Remove prefix and address length.
|
||||
kv.AssertKeyAtLeastLength(key, 3)
|
||||
addr := key[2:]
|
||||
kv.AssertKeyLength(addr, int(key[1]))
|
||||
|
||||
return sdk.ValAddress(addr)
|
||||
}
|
||||
|
||||
// GetValidatorHistoricalRewardsAddressPeriod creates the address & period from a validator's historical rewards key.
|
||||
func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) {
|
||||
// key is in the format:
|
||||
@ -85,19 +70,6 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr
|
||||
return
|
||||
}
|
||||
|
||||
// GetValidatorAccumulatedCommissionAddress creates the address from a validator's accumulated commission key.
|
||||
func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) {
|
||||
// key is in the format:
|
||||
// 0x07<valAddrLen (1 Byte)><valAddr_Bytes>: ValidatorCurrentRewards
|
||||
|
||||
// Remove prefix and address length.
|
||||
kv.AssertKeyAtLeastLength(key, 3)
|
||||
addr := key[2:]
|
||||
kv.AssertKeyLength(addr, int(key[1]))
|
||||
|
||||
return sdk.ValAddress(addr)
|
||||
}
|
||||
|
||||
// GetValidatorSlashEventAddressHeight creates the height from a validator's slash event key.
|
||||
func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) {
|
||||
// key is in the format:
|
||||
@ -113,11 +85,6 @@ func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, he
|
||||
return
|
||||
}
|
||||
|
||||
// GetValidatorOutstandingRewardsKey creates the outstanding rewards key for a validator.
|
||||
func GetValidatorOutstandingRewardsKey(valAddr sdk.ValAddress) []byte {
|
||||
return append(ValidatorOutstandingRewardsPrefix, address.MustLengthPrefix(valAddr.Bytes())...)
|
||||
}
|
||||
|
||||
// GetValidatorHistoricalRewardsPrefix creates the prefix key for a validator's historical rewards.
|
||||
func GetValidatorHistoricalRewardsPrefix(v sdk.ValAddress) []byte {
|
||||
return append(ValidatorHistoricalRewardsPrefix, address.MustLengthPrefix(v.Bytes())...)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user