From dc900d381a8feaa5f34a52cb17a87d5ec5d2ebeb Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Thu, 25 May 2023 18:47:32 +0200 Subject: [PATCH 1/5] start moving distr FeePool to collections --- x/distribution/keeper/keeper.go | 6 ++++-- x/distribution/keeper/store.go | 8 +------- x/distribution/types/keys.go | 6 +++--- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index e25fa99f4638..4fd0d21876f9 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -26,8 +26,9 @@ type Keeper struct { // should be the x/gov module account. authority string - Schema collections.Schema - Params collections.Item[types.Params] + Schema collections.Schema + Params collections.Item[types.Params] + FeePool collections.Item[types.FeePool] feeCollectorName string // name of the FeeCollector ModuleAccount } @@ -53,6 +54,7 @@ func NewKeeper( feeCollectorName: feeCollectorName, authority: authority, Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), + FeePool: collections.NewItem(sb, types.FeePoolKey, "fee_pool", codec.CollValue[types.FeePool](cdc)), } schema, err := sb.Build() diff --git a/x/distribution/keeper/store.go b/x/distribution/keeper/store.go index 50afcf6e7520..20f1492a82f5 100644 --- a/x/distribution/keeper/store.go +++ b/x/distribution/keeper/store.go @@ -66,13 +66,7 @@ func (k Keeper) GetFeePool(ctx context.Context) (feePool types.FeePool, err erro // set the global fee pool distribution info func (k Keeper) SetFeePool(ctx context.Context, feePool types.FeePool) error { - store := k.storeService.OpenKVStore(ctx) - b, err := k.cdc.Marshal(&feePool) - if err != nil { - return err - } - - return store.Set(types.FeePoolKey, b) + return k.FeePool.Set(ctx, feePool) } // GetPreviousProposerConsAddr returns the proposer consensus address for the diff --git a/x/distribution/types/keys.go b/x/distribution/types/keys.go index 38858e78ca8c..30aa8923c740 100644 --- a/x/distribution/types/keys.go +++ b/x/distribution/types/keys.go @@ -44,9 +44,9 @@ const ( // // - 0x09: Params var ( - FeePoolKey = []byte{0x00} // 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 = []byte{0x02} // key for outstanding rewards DelegatorWithdrawAddrPrefix = []byte{0x03} // key for delegator withdraw address DelegatorStartingInfoPrefix = []byte{0x04} // key for delegator starting info From f9cab40dd998beffb81b3bb7869b45e807344cc4 Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Fri, 26 May 2023 08:38:52 +0200 Subject: [PATCH 2/5] more code refactors --- simapp/export.go | 4 +- .../distribution/keeper/msg_server_test.go | 101 ++++++++++-------- x/distribution/keeper/alias_functions.go | 10 -- x/distribution/keeper/allocation.go | 6 +- x/distribution/keeper/allocation_test.go | 20 ++-- x/distribution/keeper/delegation.go | 4 +- x/distribution/keeper/delegation_test.go | 74 ++++++------- x/distribution/keeper/fee_pool.go | 4 +- x/distribution/keeper/genesis.go | 42 ++++++-- x/distribution/keeper/grpc_query.go | 4 +- x/distribution/keeper/hooks.go | 8 +- x/distribution/keeper/invariants.go | 4 +- x/distribution/keeper/keeper.go | 4 +- x/distribution/keeper/keeper_test.go | 14 +-- x/distribution/keeper/store.go | 20 ---- x/distribution/keeper/validator.go | 4 +- 16 files changed, 165 insertions(+), 158 deletions(-) diff --git a/simapp/export.go b/simapp/export.go index c489d0110911..50830adb4562 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -112,12 +112,12 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [] if err != nil { panic(err) } - feePool, err := app.DistrKeeper.GetFeePool(ctx) + feePool, err := app.DistrKeeper.FeePool.Get(ctx) if err != nil { panic(err) } feePool.CommunityPool = feePool.CommunityPool.Add(scraps...) - if err := app.DistrKeeper.SetFeePool(ctx, feePool); err != nil { + if err := app.DistrKeeper.FeePool.Set(ctx, feePool); err != nil { panic(err) } diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 088de5cd2641..b13c708263c6 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -7,6 +7,7 @@ import ( "cosmossdk.io/log" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" + "github.com/stretchr/testify/require" cmtabcitypes "github.com/cometbft/cometbft/abci/types" "github.com/cometbft/cometbft/proto/tendermint/types" @@ -146,11 +147,12 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { t.Parallel() f := initFixture(t) - f.distrKeeper.SetFeePool(f.sdkCtx, distrtypes.FeePool{ + err := f.distrKeeper.FeePool.Set(f.sdkCtx, distrtypes.FeePool{ CommunityPool: sdk.NewDecCoins(sdk.DecCoin{Denom: "stake", Amount: math.LegacyNewDec(10000)}), }) - f.distrKeeper.Params.Set(f.sdkCtx, distrtypes.DefaultParams()) - initFeePool, err := f.distrKeeper.GetFeePool(f.sdkCtx) + require.NoError(t, err) + require.NoError(t, f.distrKeeper.Params.Set(f.sdkCtx, distrtypes.DefaultParams())) + initFeePool, err := f.distrKeeper.FeePool.Get(f.sdkCtx) assert.NilError(t, err) delAddr := sdk.AccAddress(PKS[1].Address()) @@ -173,10 +175,11 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { // set module account coins initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) - f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) - + err = f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + require.NoError(t, err) // send funds to val addr - f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + err = f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + require.NoError(t, err) initBalance := f.bankKeeper.GetAllBalances(f.sdkCtx, delAddr) @@ -185,16 +188,19 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { validator, issuedShares := validator.AddTokensFromDel(delTokens) delegation := stakingtypes.NewDelegation(delAddr, validator.GetOperator(), issuedShares) f.stakingKeeper.SetDelegation(f.sdkCtx, delegation) - f.distrKeeper.SetDelegatorStartingInfo(f.sdkCtx, validator.GetOperator(), delAddr, distrtypes.NewDelegatorStartingInfo(2, math.LegacyOneDec(), 20)) - + err = f.distrKeeper.SetDelegatorStartingInfo(f.sdkCtx, validator.GetOperator(), delAddr, distrtypes.NewDelegatorStartingInfo(2, math.LegacyOneDec(), 20)) + require.NoError(t, err) // setup validator rewards decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyOneDec())} historicalRewards := distrtypes.NewValidatorHistoricalRewards(decCoins, 2) - f.distrKeeper.SetValidatorHistoricalRewards(f.sdkCtx, validator.GetOperator(), 2, historicalRewards) + err = f.distrKeeper.SetValidatorHistoricalRewards(f.sdkCtx, validator.GetOperator(), 2, historicalRewards) + require.NoError(t, err) // setup current rewards and outstanding rewards currentRewards := distrtypes.NewValidatorCurrentRewards(decCoins, 3) - f.distrKeeper.SetValidatorCurrentRewards(f.sdkCtx, f.valAddr, currentRewards) - f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, distrtypes.ValidatorOutstandingRewards{Rewards: valCommission}) + err = f.distrKeeper.SetValidatorCurrentRewards(f.sdkCtx, f.valAddr, currentRewards) + require.NoError(t, err) + err = f.distrKeeper.SetValidatorOutstandingRewards(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) @@ -293,7 +299,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { assert.Assert(t, initBalance.IsAllLTE(curBalance)) // check rewards - curFeePool, _ := f.distrKeeper.GetFeePool(f.sdkCtx) + curFeePool, _ := f.distrKeeper.FeePool.Get(f.sdkCtx) rewards := curFeePool.GetCommunityPool().Sub(initFeePool.CommunityPool) curOutstandingRewards, _ := f.distrKeeper.GetValidatorOutstandingRewards(f.sdkCtx, f.valAddr) assert.DeepEqual(t, rewards, initOutstandingRewards.Sub(curOutstandingRewards.Rewards)) @@ -316,7 +322,7 @@ func TestMsgSetWithdrawAddress(t *testing.T) { t.Parallel() f := initFixture(t) - f.distrKeeper.Params.Set(f.sdkCtx, distrtypes.DefaultParams()) + require.NoError(t, f.distrKeeper.Params.Set(f.sdkCtx, distrtypes.DefaultParams())) delAddr := sdk.AccAddress(PKS[0].Address()) withdrawAddr := sdk.AccAddress(PKS[1].Address()) @@ -454,13 +460,14 @@ func TestMsgWithdrawValidatorCommission(t *testing.T) { // set module account coins initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) - f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) - + err := f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + require.NoError(t, err) // send funds to val addr - f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) - + err = f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + require.NoError(t, err) coins := sdk.NewCoins(sdk.NewCoin("mytoken", sdk.NewInt(2)), sdk.NewCoin("stake", sdk.NewInt(2))) - f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, coins) + err = f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, coins) + require.NoError(t, err) // check initial balance balance := f.bankKeeper.GetAllBalances(f.sdkCtx, sdk.AccAddress(f.valAddr)) @@ -469,10 +476,12 @@ func TestMsgWithdrawValidatorCommission(t *testing.T) { assert.DeepEqual(t, expCoins, balance) // set outstanding rewards - f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, distrtypes.ValidatorOutstandingRewards{Rewards: valCommission}) + err = f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, distrtypes.ValidatorOutstandingRewards{Rewards: valCommission}) + require.NoError(t, err) // set commission - f.distrKeeper.SetValidatorAccumulatedCommission(f.sdkCtx, f.valAddr, distrtypes.ValidatorAccumulatedCommission{Commission: valCommission}) + err = f.distrKeeper.SetValidatorAccumulatedCommission(f.sdkCtx, f.valAddr, distrtypes.ValidatorAccumulatedCommission{Commission: valCommission}) + require.NoError(t, err) testCases := []struct { name string @@ -548,19 +557,19 @@ func TestMsgFundCommunityPool(t *testing.T) { f := initFixture(t) // reset fee pool - f.distrKeeper.SetFeePool(f.sdkCtx, distrtypes.InitialFeePool()) - initPool, _ := f.distrKeeper.GetFeePool(f.sdkCtx) - assert.Assert(t, initPool.CommunityPool.Empty()) + initPool := distrtypes.InitialFeePool() + require.NoError(t, f.distrKeeper.FeePool.Set(f.sdkCtx, initPool)) initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(100)) - f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + err := f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + require.NoError(t, err) addr := sdk.AccAddress(PKS[0].Address()) addr2 := sdk.AccAddress(PKS[1].Address()) amount := sdk.NewCoins(sdk.NewInt64Coin("stake", 100)) // fund the account by minting and sending amount from distribution module to addr - err := f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, amount) + err = f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, amount) assert.NilError(t, err) err = f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, addr, amount) assert.NilError(t, err) @@ -627,7 +636,8 @@ func TestMsgFundCommunityPool(t *testing.T) { assert.NilError(t, err) // query the community pool funds - feePool, _ := f.distrKeeper.GetFeePool(f.sdkCtx) + feePool, err := f.distrKeeper.FeePool.Get(f.sdkCtx) + require.NoError(t, err) assert.DeepEqual(t, initPool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...), feePool.CommunityPool) assert.Assert(t, f.bankKeeper.GetAllBalances(f.sdkCtx, addr).Empty()) } @@ -765,14 +775,15 @@ func TestMsgCommunityPoolSpend(t *testing.T) { t.Parallel() f := initFixture(t) - f.distrKeeper.Params.Set(f.sdkCtx, distrtypes.DefaultParams()) - f.distrKeeper.SetFeePool(f.sdkCtx, distrtypes.FeePool{ - CommunityPool: sdk.NewDecCoins(sdk.DecCoin{Denom: "stake", Amount: math.LegacyNewDec(10000)}), - }) - initialFeePool, _ := f.distrKeeper.GetFeePool(f.sdkCtx) + require.NoError(t, f.distrKeeper.Params.Set(f.sdkCtx, distrtypes.DefaultParams())) + initialFeePool := sdk.NewDecCoins(sdk.DecCoin{Denom: "stake", Amount: math.LegacyNewDec(10000)}) + require.NoError(t, f.distrKeeper.FeePool.Set(f.sdkCtx, distrtypes.FeePool{ + CommunityPool: initialFeePool, + })) initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(100)) - f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + err := f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + require.NoError(t, err) recipient := sdk.AccAddress([]byte("addr1")) @@ -832,10 +843,11 @@ func TestMsgCommunityPoolSpend(t *testing.T) { assert.NilError(t, err) // query the community pool to verify it has been updated - communityPool, _ := f.distrKeeper.GetFeePoolCommunityCoins(f.sdkCtx) - newPool, negative := initialFeePool.CommunityPool.SafeSub(sdk.NewDecCoinsFromCoins(tc.msg.Amount...)) + communityPool, err := f.distrKeeper.FeePool.Get(f.sdkCtx) + require.NoError(t, err) + newPool, negative := initialFeePool.SafeSub(sdk.NewDecCoinsFromCoins(tc.msg.Amount...)) assert.Assert(t, negative == false) - assert.DeepEqual(t, communityPool, newPool) + assert.DeepEqual(t, communityPool.CommunityPool, newPool) } }) } @@ -845,27 +857,28 @@ func TestMsgDepositValidatorRewardsPool(t *testing.T) { t.Parallel() f := initFixture(t) - f.distrKeeper.Params.Set(f.sdkCtx, distrtypes.DefaultParams()) - f.distrKeeper.SetFeePool(f.sdkCtx, distrtypes.FeePool{ + require.NoError(t, f.distrKeeper.Params.Set(f.sdkCtx, distrtypes.DefaultParams())) + err := f.distrKeeper.FeePool.Set(f.sdkCtx, distrtypes.FeePool{ CommunityPool: sdk.NewDecCoins(sdk.DecCoin{Denom: "stake", Amount: math.LegacyNewDec(100)}), }) + require.NoError(t, err) initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(10000)) - f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + require.NoError(t, f.bankKeeper.MintCoins(f.sdkCtx, distrtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens)))) // Set default staking params - f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams()) + require.NoError(t, f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams())) - addr := sdk.AccAddress([]byte("addr")) + addr := sdk.AccAddress("addr") addr1 := sdk.AccAddress(PKS[0].Address()) valAddr1 := sdk.ValAddress(addr1) // send funds to val addr tokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) - f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, sdk.AccAddress(valAddr1), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tokens))) - + err = f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, sdk.AccAddress(valAddr1), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tokens))) + require.NoError(t, err) // send funds from module to addr to perform DepositValidatorRewardsPool - f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, addr, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tokens))) - + err = f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, distrtypes.ModuleName, addr, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, tokens))) + require.NoError(t, err) tstaking := stakingtestutil.NewHelper(t, f.sdkCtx, f.stakingKeeper) tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0)) tstaking.CreateValidator(valAddr1, valConsPk0, sdk.NewInt(100), true) diff --git a/x/distribution/keeper/alias_functions.go b/x/distribution/keeper/alias_functions.go index 373bae58c896..28ea51c0c26c 100644 --- a/x/distribution/keeper/alias_functions.go +++ b/x/distribution/keeper/alias_functions.go @@ -17,16 +17,6 @@ func (k Keeper) GetValidatorOutstandingRewardsCoins(ctx context.Context, val sdk return rewards.Rewards, nil } -// get the community coins -func (k Keeper) GetFeePoolCommunityCoins(ctx context.Context) (sdk.DecCoins, error) { - feePool, err := k.GetFeePool(ctx) - if err != nil { - return nil, err - } - - return feePool.CommunityPool, nil -} - // GetDistributionAccount returns the distribution ModuleAccount func (k Keeper) GetDistributionAccount(ctx context.Context) sdk.ModuleAccountI { return k.authKeeper.GetModuleAccount(ctx, types.ModuleName) diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index da330c43e7a4..fce0cfefc24a 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -30,14 +30,14 @@ func (k Keeper) AllocateTokens(ctx context.Context, totalPreviousPower int64, bo // temporary workaround to keep CanWithdrawInvariant happy // general discussions here: https://github.com/cosmos/cosmos-sdk/issues/2906#issuecomment-441867634 - feePool, err := k.GetFeePool(ctx) + feePool, err := k.FeePool.Get(ctx) if err != nil { return err } if totalPreviousPower == 0 { feePool.CommunityPool = feePool.CommunityPool.Add(feesCollected...) - return k.SetFeePool(ctx, feePool) + return k.FeePool.Set(ctx, feePool) } // calculate fraction allocated to validators @@ -74,7 +74,7 @@ func (k Keeper) AllocateTokens(ctx context.Context, totalPreviousPower int64, bo // allocate community funding feePool.CommunityPool = feePool.CommunityPool.Add(remaining...) - return k.SetFeePool(ctx, feePool) + return k.FeePool.Set(ctx, feePool) } // AllocateTokensToValidator allocate tokens to a particular validator, diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index 08b08b8096a4..5ca02a56eef2 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -58,7 +58,7 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) { tokens := sdk.DecCoins{ {Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(10)}, } - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // check commission expected := sdk.DecCoins{ @@ -102,8 +102,8 @@ func TestAllocateTokensToManyValidators(t *testing.T) { ) // reset fee pool & set params - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) // create validator with 50% commission valAddr0 := sdk.ValAddress(valConsAddr0) @@ -137,7 +137,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) { require.NoError(t, err) require.True(t, val1OutstandingRewards.Rewards.IsZero()) - feePool, err := distrKeeper.GetFeePool(ctx) + feePool, err := distrKeeper.FeePool.Get(ctx) require.NoError(t, err) require.True(t, feePool.CommunityPool.IsZero()) @@ -170,7 +170,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) { Validator: abciValB, }, } - distrKeeper.AllocateTokens(ctx, 200, votes) + require.NoError(t, distrKeeper.AllocateTokens(ctx, 200, votes)) // 98 outstanding rewards (100 less 2 to community pool) val0OutstandingRewards, err = distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr0) @@ -182,7 +182,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) { require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDecWithPrec(490, 1)}}, val1OutstandingRewards.Rewards) // 2 community pool coins - feePool, err = distrKeeper.GetFeePool(ctx) + feePool, err = distrKeeper.FeePool.Get(ctx) require.NoError(t, err) require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(2)}}, feePool.CommunityPool) @@ -234,8 +234,8 @@ func TestAllocateTokensTruncation(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) // create validator with 10% commission valAddr0 := sdk.ValAddress(valConsAddr0) @@ -280,7 +280,7 @@ func TestAllocateTokensTruncation(t *testing.T) { require.NoError(t, err) require.True(t, val1OutstandingRewards.Rewards.IsZero()) - feePool, err := distrKeeper.GetFeePool(ctx) + feePool, err := distrKeeper.FeePool.Get(ctx) require.NoError(t, err) require.True(t, feePool.CommunityPool.IsZero()) @@ -316,7 +316,7 @@ func TestAllocateTokensTruncation(t *testing.T) { Validator: abciValC, }, } - distrKeeper.AllocateTokens(ctx, 31, votes) + require.NoError(t, distrKeeper.AllocateTokens(ctx, 31, votes)) val0OutstandingRewards, err = distrKeeper.GetValidatorOutstandingRewards(ctx, valAddr0) require.NoError(t, err) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 7068bd6d89a8..fdd19fd8ce32 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -229,13 +229,13 @@ func (k Keeper) withdrawDelegationRewards(ctx context.Context, val stakingtypes. return nil, err } - feePool, err := k.GetFeePool(ctx) + feePool, err := k.FeePool.Get(ctx) if err != nil { return nil, err } feePool.CommunityPool = feePool.CommunityPool.Add(remainder...) - err = k.SetFeePool(ctx, feePool) + err = k.FeePool.Set(ctx, feePool) if err != nil { return nil, err } diff --git a/x/distribution/keeper/delegation_test.go b/x/distribution/keeper/delegation_test.go index f4ef44b5463f..e1e1680244ae 100644 --- a/x/distribution/keeper/delegation_test.go +++ b/x/distribution/keeper/delegation_test.go @@ -47,8 +47,8 @@ func TestCalculateRewardsBasic(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) @@ -87,7 +87,7 @@ func TestCalculateRewardsBasic(t *testing.T) { // allocate some rewards initial := int64(10) tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial)}} - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // end period endingPeriod, _ = distrKeeper.IncrementValidatorPeriod(ctx, val) @@ -130,8 +130,8 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) @@ -186,7 +186,7 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { // allocate some rewards initial := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDecFromInt(initial)}} - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // end period endingPeriod, _ = distrKeeper.IncrementValidatorPeriod(ctx, val) @@ -230,8 +230,8 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) @@ -288,7 +288,7 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { // allocate some rewards initial := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDecFromInt(initial)}} - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // slash the validator by 50% again slashedTokens = distrtestutil.SlashValidator( @@ -306,7 +306,7 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) // allocate some more rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // end period endingPeriod, _ = distrKeeper.IncrementValidatorPeriod(ctx, val) @@ -350,8 +350,8 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) @@ -377,7 +377,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { // allocate some rewards initial := int64(20) tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial)}} - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // second delegation addr1 := sdk.AccAddress(valConsAddr1) @@ -395,7 +395,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // allocate some more rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // end period endingPeriod, _ := distrKeeper.IncrementValidatorPeriod(ctx, val) @@ -445,8 +445,8 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) @@ -472,7 +472,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { initial := sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction) tokens := sdk.DecCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, initial)} - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // historical count should be 2 (initial + latest for delegation) require.Equal(t, uint64(2), distrKeeper.GetValidatorHistoricalReferenceCount(ctx)) @@ -518,8 +518,8 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) @@ -557,7 +557,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { // allocate some rewards initial := math.LegacyNewDecFromInt(sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction)) tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: initial}} - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) valPower := int64(100) // slash the validator by 50% (simulated with manual calls; we assume the validator is bonded) @@ -587,7 +587,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) // allocate some more rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // end period endingPeriod, _ = distrKeeper.IncrementValidatorPeriod(ctx, val) @@ -630,8 +630,8 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) valPower := int64(100) @@ -658,7 +658,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { // allocate some rewards initial := math.LegacyNewDecFromInt(sdk.TokensFromConsensusPower(30, sdk.DefaultPowerReduction)) tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: initial}} - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // slash the validator ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) @@ -699,7 +699,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // allocate some more rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // slash the validator again ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) @@ -762,8 +762,8 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) @@ -788,7 +788,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { // allocate some rewards initial := int64(20) tokens := sdk.DecCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, math.NewInt(initial))} - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // historical count should be 2 (validator init, delegation init) require.Equal(t, uint64(2), distrKeeper.GetValidatorHistoricalReferenceCount(ctx)) @@ -819,7 +819,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // allocate some more rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // first delegator withdraws expRewards := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(initial*3/4))} @@ -868,7 +868,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // allocate some more rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // first delegator withdraws again expCommission = sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(initial*1/4))} @@ -902,7 +902,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // allocate some more rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // withdraw commission expCommission = sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(initial))} @@ -958,8 +958,8 @@ func Test100PercentCommissionReward(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool()) - distrKeeper.Params.Set(ctx, disttypes.DefaultParams()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, disttypes.InitialFeePool())) + require.NoError(t, distrKeeper.Params.Set(ctx, disttypes.DefaultParams())) // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) @@ -984,25 +984,25 @@ func Test100PercentCommissionReward(t *testing.T) { // allocate some rewards initial := int64(20) tokens := sdk.DecCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, math.NewInt(initial))} - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // next block ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // allocate some rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // next block ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // allocate some more rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) // next block ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) // allocate some more rewards - distrKeeper.AllocateTokensToValidator(ctx, val, tokens) + require.NoError(t, distrKeeper.AllocateTokensToValidator(ctx, val, tokens)) rewards, err := distrKeeper.WithdrawDelegationRewards(ctx, addr, valAddr) require.NoError(t, err) diff --git a/x/distribution/keeper/fee_pool.go b/x/distribution/keeper/fee_pool.go index b8bb8bf7ee64..63c66782dedc 100644 --- a/x/distribution/keeper/fee_pool.go +++ b/x/distribution/keeper/fee_pool.go @@ -10,7 +10,7 @@ import ( // DistributeFromFeePool distributes funds from the distribution module account to // a receiver address while updating the community pool func (k Keeper) DistributeFromFeePool(ctx context.Context, amount sdk.Coins, receiveAddr sdk.AccAddress) error { - feePool, err := k.GetFeePool(ctx) + feePool, err := k.FeePool.Get(ctx) if err != nil { return err } @@ -30,5 +30,5 @@ func (k Keeper) DistributeFromFeePool(ctx context.Context, amount sdk.Coins, rec return err } - return k.SetFeePool(ctx, feePool) + return k.FeePool.Set(ctx, feePool) } diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index ed67b6229e55..4ef2cb23fa2a 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -11,7 +11,10 @@ import ( func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { var moduleHoldings sdk.DecCoins - k.SetFeePool(ctx, data.FeePool) + err := k.FeePool.Set(ctx, data.FeePool) + if err != nil { + panic(err) + } if err := k.Params.Set(ctx, data.Params); err != nil { panic(err) @@ -26,7 +29,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { if err != nil { panic(err) } - k.SetDelegatorWithdrawAddr(ctx, delegatorAddress, withdrawAddress) + err = k.SetDelegatorWithdrawAddr(ctx, delegatorAddress, withdrawAddress) + if err != nil { + panic(err) + } } var previousProposer sdk.ConsAddress @@ -45,7 +51,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { if err != nil { panic(err) } - k.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards}) + err = k.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards}) + if err != nil { + panic(err) + } moduleHoldings = moduleHoldings.Add(rew.OutstandingRewards...) } for _, acc := range data.ValidatorAccumulatedCommissions { @@ -53,21 +62,30 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { if err != nil { panic(err) } - k.SetValidatorAccumulatedCommission(ctx, valAddr, acc.Accumulated) + err = k.SetValidatorAccumulatedCommission(ctx, valAddr, acc.Accumulated) + if err != nil { + panic(err) + } } for _, his := range data.ValidatorHistoricalRewards { valAddr, err := sdk.ValAddressFromBech32(his.ValidatorAddress) if err != nil { panic(err) } - k.SetValidatorHistoricalRewards(ctx, valAddr, his.Period, his.Rewards) + err = k.SetValidatorHistoricalRewards(ctx, valAddr, his.Period, his.Rewards) + if err != nil { + panic(err) + } } for _, cur := range data.ValidatorCurrentRewards { valAddr, err := sdk.ValAddressFromBech32(cur.ValidatorAddress) if err != nil { panic(err) } - k.SetValidatorCurrentRewards(ctx, valAddr, cur.Rewards) + err = k.SetValidatorCurrentRewards(ctx, valAddr, cur.Rewards) + if err != nil { + panic(err) + } } for _, del := range data.DelegatorStartingInfos { valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress) @@ -79,14 +97,20 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { panic(err) } - k.SetDelegatorStartingInfo(ctx, valAddr, delegatorAddress, del.StartingInfo) + err = k.SetDelegatorStartingInfo(ctx, valAddr, delegatorAddress, del.StartingInfo) + if err != nil { + panic(err) + } } for _, evt := range data.ValidatorSlashEvents { valAddr, err := sdk.ValAddressFromBech32(evt.ValidatorAddress) if err != nil { panic(err) } - k.SetValidatorSlashEvent(ctx, valAddr, evt.Height, evt.Period, evt.ValidatorSlashEvent) + err = k.SetValidatorSlashEvent(ctx, valAddr, evt.Height, evt.Period, evt.ValidatorSlashEvent) + if err != nil { + panic(err) + } } moduleHoldings = moduleHoldings.Add(data.FeePool.CommunityPool...) @@ -109,7 +133,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) { // ExportGenesis returns a GenesisState for a given context and keeper. func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { - feePool, err := k.GetFeePool(ctx) + feePool, err := k.FeePool.Get(ctx) if err != nil { panic(err) } diff --git a/x/distribution/keeper/grpc_query.go b/x/distribution/keeper/grpc_query.go index 63a4c2e26a5b..c4f69aded672 100644 --- a/x/distribution/keeper/grpc_query.go +++ b/x/distribution/keeper/grpc_query.go @@ -335,10 +335,10 @@ func (k Querier) DelegatorWithdrawAddress(c context.Context, req *types.QueryDel // CommunityPool queries the community pool coins func (k Querier) CommunityPool(c context.Context, req *types.QueryCommunityPoolRequest) (*types.QueryCommunityPoolResponse, error) { - pool, err := k.GetFeePoolCommunityCoins(c) + pool, err := k.FeePool.Get(c) if err != nil { return nil, err } - return &types.QueryCommunityPoolResponse{Pool: pool}, nil + return &types.QueryCommunityPoolResponse{Pool: pool.CommunityPool}, nil } diff --git a/x/distribution/keeper/hooks.go b/x/distribution/keeper/hooks.go index e0574a250c4c..3c7fdd9092a6 100644 --- a/x/distribution/keeper/hooks.go +++ b/x/distribution/keeper/hooks.go @@ -50,13 +50,13 @@ func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, _ sdk.ConsAddress, valAddr coins, remainder := commission.TruncateDecimal() // remainder to community pool - feePool, err := h.k.GetFeePool(ctx) + feePool, err := h.k.FeePool.Get(ctx) if err != nil { return err } feePool.CommunityPool = feePool.CommunityPool.Add(remainder...) - err = h.k.SetFeePool(ctx, feePool) + err = h.k.FeePool.Set(ctx, feePool) if err != nil { return err } @@ -78,13 +78,13 @@ func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, _ sdk.ConsAddress, valAddr // Add outstanding to community pool // The validator is removed only after it has no more delegations. // This operation sends only the remaining dust to the community pool. - feePool, err := h.k.GetFeePool(ctx) + feePool, err := h.k.FeePool.Get(ctx) if err != nil { return err } feePool.CommunityPool = feePool.CommunityPool.Add(outstanding...) - err = h.k.SetFeePool(ctx, feePool) + err = h.k.FeePool.Set(ctx, feePool) if err != nil { return err } diff --git a/x/distribution/keeper/invariants.go b/x/distribution/keeper/invariants.go index 68a79d77550d..2750f9dd8b34 100644 --- a/x/distribution/keeper/invariants.go +++ b/x/distribution/keeper/invariants.go @@ -146,12 +146,12 @@ func ModuleAccountInvariant(k Keeper) sdk.Invariant { return false }) - communityPool, err := k.GetFeePoolCommunityCoins(ctx) + communityPool, err := k.FeePool.Get(ctx) if err != nil { panic(err) } - expectedInt, _ := expectedCoins.Add(communityPool...).TruncateDecimal() + expectedInt, _ := expectedCoins.Add(communityPool.CommunityPool...).TruncateDecimal() macc := k.GetDistributionAccount(ctx) balances := k.bankKeeper.GetAllBalances(ctx, macc.GetAddress()) diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index 4fd0d21876f9..3f6c2a73d425 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -201,11 +201,11 @@ func (k Keeper) FundCommunityPool(ctx context.Context, amount sdk.Coins, sender return err } - feePool, err := k.GetFeePool(ctx) + feePool, err := k.FeePool.Get(ctx) if err != nil { return err } feePool.CommunityPool = feePool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...) - return k.SetFeePool(ctx, feePool) + return k.FeePool.Set(ctx, feePool) } diff --git a/x/distribution/keeper/keeper_test.go b/x/distribution/keeper/keeper_test.go index 84f37d92e921..76cbf3eb2cbb 100644 --- a/x/distribution/keeper/keeper_test.go +++ b/x/distribution/keeper/keeper_test.go @@ -103,10 +103,10 @@ func TestWithdrawValidatorCommission(t *testing.T) { ) // set outstanding rewards - distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: valCommission}) + require.NoError(t, distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: valCommission})) // set commission - distrKeeper.SetValidatorAccumulatedCommission(ctx, valAddr, types.ValidatorAccumulatedCommission{Commission: valCommission}) + require.NoError(t, distrKeeper.SetValidatorAccumulatedCommission(ctx, valAddr, types.ValidatorAccumulatedCommission{Commission: valCommission})) // withdraw commission coins := sdk.NewCoins(sdk.NewCoin("mytoken", math.NewInt(1)), sdk.NewCoin("stake", math.NewInt(1))) @@ -159,8 +159,8 @@ func TestGetTotalRewards(t *testing.T) { sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(3).Quo(math.LegacyNewDec(2))), } - distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr0, types.ValidatorOutstandingRewards{Rewards: valCommission}) - distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr1, types.ValidatorOutstandingRewards{Rewards: valCommission}) + require.NoError(t, distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr0, types.ValidatorOutstandingRewards{Rewards: valCommission})) + require.NoError(t, distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr1, types.ValidatorOutstandingRewards{Rewards: valCommission})) expectedRewards := valCommission.MulDec(math.LegacyNewDec(2)) totalRewards := distrKeeper.GetTotalRewards(ctx) @@ -194,9 +194,9 @@ func TestFundCommunityPool(t *testing.T) { ) // reset fee pool - distrKeeper.SetFeePool(ctx, types.InitialFeePool()) + require.NoError(t, distrKeeper.FeePool.Set(ctx, types.InitialFeePool())) - initPool, err := distrKeeper.GetFeePool(ctx) + initPool, err := distrKeeper.FeePool.Get(ctx) require.NoError(t, err) require.Empty(t, initPool.CommunityPool) @@ -205,7 +205,7 @@ func TestFundCommunityPool(t *testing.T) { err = distrKeeper.FundCommunityPool(ctx, amount, addrs[0]) require.NoError(t, err) - feePool, err := distrKeeper.GetFeePool(ctx) + feePool, err := distrKeeper.FeePool.Get(ctx) require.NoError(t, err) require.Equal(t, initPool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...), feePool.CommunityPool) } diff --git a/x/distribution/keeper/store.go b/x/distribution/keeper/store.go index 20f1492a82f5..c5b9132dbaa0 100644 --- a/x/distribution/keeper/store.go +++ b/x/distribution/keeper/store.go @@ -49,26 +49,6 @@ func (k Keeper) IterateDelegatorWithdrawAddrs(ctx context.Context, handler func( } } -// get the global fee pool distribution info -func (k Keeper) GetFeePool(ctx context.Context) (feePool types.FeePool, err error) { - store := k.storeService.OpenKVStore(ctx) - b, err := store.Get(types.FeePoolKey) - if err != nil { - return - } - - if b == nil { - panic("Stored fee pool should not have been nil") - } - err = k.cdc.Unmarshal(b, &feePool) - return feePool, err -} - -// set the global fee pool distribution info -func (k Keeper) SetFeePool(ctx context.Context, feePool types.FeePool) error { - return k.FeePool.Set(ctx, feePool) -} - // GetPreviousProposerConsAddr returns the proposer consensus address for the // current block. func (k Keeper) GetPreviousProposerConsAddr(ctx context.Context) (sdk.ConsAddress, error) { diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index 04205a236e8b..7d17cebec0e9 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -51,7 +51,7 @@ func (k Keeper) IncrementValidatorPeriod(ctx context.Context, val stakingtypes.V // can't calculate ratio for zero-token validators // ergo we instead add to the community pool - feePool, err := k.GetFeePool(ctx) + feePool, err := k.FeePool.Get(ctx) if err != nil { return 0, err } @@ -63,7 +63,7 @@ func (k Keeper) IncrementValidatorPeriod(ctx context.Context, val stakingtypes.V feePool.CommunityPool = feePool.CommunityPool.Add(rewards.Rewards...) outstanding.Rewards = outstanding.GetRewards().Sub(rewards.Rewards) - err = k.SetFeePool(ctx, feePool) + err = k.FeePool.Set(ctx, feePool) if err != nil { return 0, err } From 6ffd0c19399e2d5bb97d7bb34bebb4042d32f42b Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Fri, 26 May 2023 09:01:01 +0200 Subject: [PATCH 3/5] fix more tests --- .../distribution/keeper/grpc_query_test.go | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tests/integration/distribution/keeper/grpc_query_test.go b/tests/integration/distribution/keeper/grpc_query_test.go index 1e929f031c3c..61799034ac6e 100644 --- a/tests/integration/distribution/keeper/grpc_query_test.go +++ b/tests/integration/distribution/keeper/grpc_query_test.go @@ -18,7 +18,7 @@ func TestGRPCParams(t *testing.T) { t.Parallel() f := initFixture(t) - f.distrKeeper.Params.Set(f.sdkCtx, types.DefaultParams()) + assert.NilError(t, f.distrKeeper.Params.Set(f.sdkCtx, types.DefaultParams())) qr := f.app.QueryHelper() queryClient := types.NewQueryClient(qr) @@ -77,10 +77,10 @@ func TestGRPCValidatorOutstandingRewards(t *testing.T) { // set module account coins initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) - f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens)))) // Set default staking params - f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams()) + assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams())) qr := f.app.QueryHelper() queryClient := types.NewQueryClient(qr) @@ -92,7 +92,7 @@ func TestGRPCValidatorOutstandingRewards(t *testing.T) { // send funds to val addr funds := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) - f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds))) + assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds)))) initialStake := int64(10) tstaking := stakingtestutil.NewHelper(t, f.sdkCtx, f.stakingKeeper) @@ -120,7 +120,7 @@ func TestGRPCValidatorOutstandingRewards(t *testing.T) { }, { name: "invalid address", - msg: &types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: sdk.ValAddress([]byte("addr1_______________")).String()}, + msg: &types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: sdk.ValAddress("addr1_______________").String()}, expPass: false, expErrMsg: "validator does not exist", }, @@ -154,17 +154,17 @@ func TestGRPCValidatorCommission(t *testing.T) { // set module account coins initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) - f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens)))) // Set default staking params - f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams()) + assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams())) qr := f.app.QueryHelper() queryClient := types.NewQueryClient(qr) // send funds to val addr funds := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) - f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds))) + assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds)))) initialStake := int64(10) tstaking := stakingtestutil.NewHelper(t, f.sdkCtx, f.stakingKeeper) @@ -172,7 +172,7 @@ func TestGRPCValidatorCommission(t *testing.T) { tstaking.CreateValidator(f.valAddr, valConsPk0, sdk.NewInt(initialStake), true) commission := sdk.DecCoins{sdk.DecCoin{Denom: "token1", Amount: math.LegacyNewDec(4)}, {Denom: "token2", Amount: math.LegacyNewDec(2)}} - f.distrKeeper.SetValidatorAccumulatedCommission(f.sdkCtx, f.valAddr, types.ValidatorAccumulatedCommission{Commission: commission}) + assert.NilError(t, f.distrKeeper.SetValidatorAccumulatedCommission(f.sdkCtx, f.valAddr, types.ValidatorAccumulatedCommission{Commission: commission})) testCases := []struct { name string @@ -188,7 +188,7 @@ func TestGRPCValidatorCommission(t *testing.T) { }, { name: "invalid validator", - msg: &types.QueryValidatorCommissionRequest{ValidatorAddress: sdk.ValAddress([]byte("addr1_______________")).String()}, + msg: &types.QueryValidatorCommissionRequest{ValidatorAddress: sdk.ValAddress("addr1_______________").String()}, expPass: false, expErrMsg: "validator does not exist", }, @@ -234,7 +234,7 @@ func TestGRPCValidatorSlashes(t *testing.T) { } for i, slash := range slashes { - f.distrKeeper.SetValidatorSlashEvent(f.sdkCtx, f.valAddr, uint64(i+2), 0, slash) + assert.NilError(t, f.distrKeeper.SetValidatorSlashEvent(f.sdkCtx, f.valAddr, uint64(i+2), 0, slash)) } var ( @@ -369,7 +369,7 @@ func TestGRPCDelegatorWithdrawAddress(t *testing.T) { t.Parallel() f := initFixture(t) - f.distrKeeper.Params.Set(f.sdkCtx, types.DefaultParams()) + assert.NilError(t, f.distrKeeper.Params.Set(f.sdkCtx, types.DefaultParams())) qr := f.app.QueryHelper() queryClient := types.NewQueryClient(qr) @@ -418,9 +418,9 @@ func TestGRPCCommunityPool(t *testing.T) { t.Parallel() f := initFixture(t) - f.distrKeeper.SetFeePool(f.sdkCtx, types.FeePool{ + assert.NilError(t, f.distrKeeper.FeePool.Set(f.sdkCtx, types.FeePool{ CommunityPool: sdk.NewDecCoins(sdk.DecCoin{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(0)}), - }) + })) qr := f.app.QueryHelper() queryClient := types.NewQueryClient(qr) @@ -474,16 +474,16 @@ func TestGRPCDelegationRewards(t *testing.T) { t.Parallel() f := initFixture(t) - f.distrKeeper.SetFeePool(f.sdkCtx, types.FeePool{ + assert.NilError(t, f.distrKeeper.FeePool.Set(f.sdkCtx, types.FeePool{ CommunityPool: sdk.NewDecCoins(sdk.DecCoin{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(1000)}), - }) + })) // set module account coins initTokens := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) - f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))) + assert.NilError(t, f.bankKeeper.MintCoins(f.sdkCtx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens)))) // Set default staking params - f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams()) + assert.NilError(t, f.stakingKeeper.SetParams(f.sdkCtx, stakingtypes.DefaultParams())) qr := f.app.QueryHelper() queryClient := types.NewQueryClient(qr) @@ -494,7 +494,7 @@ func TestGRPCDelegationRewards(t *testing.T) { // send funds to val addr funds := f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, int64(1000)) - f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds))) + assert.NilError(t, f.bankKeeper.SendCoinsFromModuleToAccount(f.sdkCtx, types.ModuleName, sdk.AccAddress(f.valAddr), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, funds)))) initialStake := int64(10) tstaking := stakingtestutil.NewHelper(t, f.sdkCtx, f.stakingKeeper) @@ -509,16 +509,16 @@ func TestGRPCDelegationRewards(t *testing.T) { validator, issuedShares := val.AddTokensFromDel(delTokens) delegation := stakingtypes.NewDelegation(delAddr, f.valAddr, issuedShares) f.stakingKeeper.SetDelegation(f.sdkCtx, delegation) - f.distrKeeper.SetDelegatorStartingInfo(f.sdkCtx, validator.GetOperator(), delAddr, types.NewDelegatorStartingInfo(2, math.LegacyNewDec(initialStake), 20)) + assert.NilError(t, f.distrKeeper.SetDelegatorStartingInfo(f.sdkCtx, validator.GetOperator(), delAddr, types.NewDelegatorStartingInfo(2, math.LegacyNewDec(initialStake), 20))) // setup validator rewards decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyOneDec())} historicalRewards := types.NewValidatorHistoricalRewards(decCoins, 2) - f.distrKeeper.SetValidatorHistoricalRewards(f.sdkCtx, validator.GetOperator(), 2, historicalRewards) + assert.NilError(t, f.distrKeeper.SetValidatorHistoricalRewards(f.sdkCtx, validator.GetOperator(), 2, historicalRewards)) // setup current rewards and outstanding rewards currentRewards := types.NewValidatorCurrentRewards(decCoins, 3) - f.distrKeeper.SetValidatorCurrentRewards(f.sdkCtx, f.valAddr, currentRewards) - f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, types.ValidatorOutstandingRewards{Rewards: decCoins}) + assert.NilError(t, f.distrKeeper.SetValidatorCurrentRewards(f.sdkCtx, f.valAddr, currentRewards)) + assert.NilError(t, f.distrKeeper.SetValidatorOutstandingRewards(f.sdkCtx, f.valAddr, types.ValidatorOutstandingRewards{Rewards: decCoins})) expRes := &types.QueryDelegationRewardsResponse{ Rewards: sdk.DecCoins{sdk.DecCoin{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initialStake / 10)}}, From fd5c18be195de64cc88ca58410a95dcc4de64883 Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Fri, 26 May 2023 11:33:11 +0200 Subject: [PATCH 4/5] chore: CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d6ef4acbb13..2758d88e615f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -233,6 +233,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * `SigVerifiableTx.GetSigners()` now returns `([][]byte, error)` instead of `[]sdk.AccAddress`. * (x/authx) [#15284](https://github.com/cosmos/cosmos-sdk/pull/15284) `NewKeeper` now requires `codec.Codec`. * (x/gov) [#15284](https://github.com/cosmos/cosmos-sdk/pull/15284) `NewKeeper` now requires `codec.Codec`. +* (x/distribution) [](https://github.com/cosmos/cosmos-sdk/pull/16302) Use collections for FeePool state management. + * removed: keeper `GetFeePool`, `SetFeePool`, `GetFeePoolCommunityCoins ### Client Breaking Changes From 19a58e74e9868e64d63ba649b14d07bdad89e3b4 Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Fri, 26 May 2023 11:36:37 +0200 Subject: [PATCH 5/5] fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2758d88e615f..f9f01b824f79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -234,7 +234,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/authx) [#15284](https://github.com/cosmos/cosmos-sdk/pull/15284) `NewKeeper` now requires `codec.Codec`. * (x/gov) [#15284](https://github.com/cosmos/cosmos-sdk/pull/15284) `NewKeeper` now requires `codec.Codec`. * (x/distribution) [](https://github.com/cosmos/cosmos-sdk/pull/16302) Use collections for FeePool state management. - * removed: keeper `GetFeePool`, `SetFeePool`, `GetFeePoolCommunityCoins + * removed: keeper `GetFeePool`, `SetFeePool`, `GetFeePoolCommunityCoins` ### Client Breaking Changes