From 0529dbcd209bc548732b353d39b4e9aa7b55033f Mon Sep 17 00:00:00 2001 From: testinginprod <98415576+testinginprod@users.noreply.github.com> Date: Fri, 16 Jun 2023 15:54:20 +0200 Subject: [PATCH] refactor(distribution)!: use collections for ValidatorOutstandingRewards state management (#16590) Co-authored-by: unknown unknown --- CHANGELOG.md | 3 +- .../distribution/keeper/grpc_query_test.go | 6 +-- .../distribution/keeper/msg_server_test.go | 10 +++-- x/distribution/keeper/alias_functions.go | 6 ++- x/distribution/keeper/allocation.go | 6 +-- x/distribution/keeper/allocation_test.go | 30 ++++++------- x/distribution/keeper/delegation.go | 2 +- x/distribution/keeper/genesis.go | 20 +++++---- x/distribution/keeper/grpc_query.go | 4 +- x/distribution/keeper/hooks.go | 2 +- x/distribution/keeper/invariants.go | 16 +++++-- x/distribution/keeper/keeper.go | 24 +++++++---- x/distribution/keeper/keeper_test.go | 6 +-- x/distribution/keeper/store.go | 42 ------------------- x/distribution/keeper/validator.go | 8 ++-- x/distribution/migrations/v2/store_test.go | 2 +- x/distribution/simulation/decoder_test.go | 3 -- x/distribution/simulation/operations_test.go | 13 +++--- x/distribution/types/keys.go | 41 ++---------------- 19 files changed, 95 insertions(+), 149 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6128a5b23c..054c718b1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/tests/integration/distribution/keeper/grpc_query_test.go b/tests/integration/distribution/keeper/grpc_query_test.go index 9e6a19fbe1..7f33f8f361 100644 --- a/tests/integration/distribution/keeper/grpc_query_test.go +++ b/tests/integration/distribution/keeper/grpc_query_test.go @@ -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)}}, diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 02e0d470af..c26f7c267b 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -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)) diff --git a/x/distribution/keeper/alias_functions.go b/x/distribution/keeper/alias_functions.go index 28ea51c0c2..e0b44aeb54 100644 --- a/x/distribution/keeper/alias_functions.go +++ b/x/distribution/keeper/alias_functions.go @@ -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 } diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index 93bde40152..9f460f8e99 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -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) } diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index 94052aa846..fc1d4f97c1 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -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()) } diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 29058326d7..ea61b35adb 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -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 } diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index dc3a5c4f2b..825a0321e1 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -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) { diff --git a/x/distribution/keeper/grpc_query.go b/x/distribution/keeper/grpc_query.go index 309cf2a37d..ddcb0bbe5c 100644 --- a/x/distribution/keeper/grpc_query.go +++ b/x/distribution/keeper/grpc_query.go @@ -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 } diff --git a/x/distribution/keeper/hooks.go b/x/distribution/keeper/hooks.go index b18b299459..ab6aca15d0 100644 --- a/x/distribution/keeper/hooks.go +++ b/x/distribution/keeper/hooks.go @@ -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 } diff --git a/x/distribution/keeper/invariants.go b/x/distribution/keeper/invariants.go index 2750f9dd8b..f66242134c 100644 --- a/x/distribution/keeper/invariants.go +++ b/x/distribution/keeper/invariants.go @@ -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 { diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index dfa84ea8fc..163a83025a 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -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 } diff --git a/x/distribution/keeper/keeper_test.go b/x/distribution/keeper/keeper_test.go index 81dfa2a5f5..325af52689 100644 --- a/x/distribution/keeper/keeper_test.go +++ b/x/distribution/keeper/keeper_test.go @@ -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) diff --git a/x/distribution/keeper/store.go b/x/distribution/keeper/store.go index 757830c1b7..e299249f32 100644 --- a/x/distribution/keeper/store.go +++ b/x/distribution/keeper/store.go @@ -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) diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index eab8aeebab..2a6a69173a 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -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 } diff --git a/x/distribution/migrations/v2/store_test.go b/x/distribution/migrations/v2/store_test.go index 9b8a081f5a..4362c4da08 100644 --- a/x/distribution/migrations/v2/store_test.go +++ b/x/distribution/migrations/v2/store_test.go @@ -48,7 +48,7 @@ func TestStoreMigration(t *testing.T) { { "ValidatorOutstandingRewards", v1.GetValidatorOutstandingRewardsKey(valAddr), - types.GetValidatorOutstandingRewardsKey(valAddr), + append(types.ValidatorOutstandingRewardsPrefix, address.MustLengthPrefix(valAddr.Bytes())...), }, { "DelegatorWithdrawAddr", diff --git a/x/distribution/simulation/decoder_test.go b/x/distribution/simulation/decoder_test.go index 6fb4d797ae..294b405666 100644 --- a/x/distribution/simulation/decoder_test.go +++ b/x/distribution/simulation/decoder_test.go @@ -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", ""}, diff --git a/x/distribution/simulation/operations_test.go b/x/distribution/simulation/operations_test.go index 843c0e9078..b5f7a8eabd 100644 --- a/x/distribution/simulation/operations_test.go +++ b/x/distribution/simulation/operations_test.go @@ -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) diff --git a/x/distribution/types/keys.go b/x/distribution/types/keys.go index ea6c76f0db..7e555b6c2b 100644 --- a/x/distribution/types/keys.go +++ b/x/distribution/types/keys.go @@ -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 - - // 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: 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())...)