Skip to content

Commit

Permalink
refactor(distribution)!: make FeePool state use collections (#16302)
Browse files Browse the repository at this point in the history
Co-authored-by: unknown unknown <unknown@unknown>
  • Loading branch information
testinginprod and unknown unknown authored May 26, 2023
1 parent b62a28a commit f851e18
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 192 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,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`

* (x/gov) [#16268](https://github.com/cosmos/cosmos-sdk/pull/16268) Use collections for proposal state management (part 2):
* this finalizes the gov collections migration
Expand Down
4 changes: 2 additions & 2 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
46 changes: 23 additions & 23 deletions tests/integration/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -154,25 +154,25 @@ 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)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
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
Expand All @@ -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",
},
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)}},
Expand Down
Loading

0 comments on commit f851e18

Please sign in to comment.