From 145d46e123ea68be5dde6419278fcc5269075aeb Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Wed, 14 Feb 2024 21:39:10 -0700 Subject: [PATCH 01/11] distribute every 10 blocks --- x/distribution/abci.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 320f3961feaa..9eb70a9b3201 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -24,7 +24,10 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) // TODO this is Tendermint-dependent // ref https://github.com/cosmos/cosmos-sdk/issues/3095 - if ctx.BlockHeight() > 1 { + blockHeight := ctx.BlockHeight() + // only allocate rewards if the block height is greater than 1 + // and only allocate rewards for every multiple of 10 blocks for performance reasons. + if blockHeight > 1 && blockHeight%10 == 0 { k.AllocateTokens(ctx, previousTotalPower, req.LastCommitInfo.GetVotes()) } From bf8248c0b2146fd5f3b74d803d09797526172272 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Wed, 14 Feb 2024 21:41:29 -0700 Subject: [PATCH 02/11] total power logic in loop --- x/distribution/abci.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 9eb70a9b3201..0ef8e94d590c 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -16,18 +16,18 @@ import ( func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) - // determine the total power signing the block - var previousTotalPower int64 - for _, voteInfo := range req.LastCommitInfo.GetVotes() { - previousTotalPower += voteInfo.Validator.Power - } - // TODO this is Tendermint-dependent // ref https://github.com/cosmos/cosmos-sdk/issues/3095 blockHeight := ctx.BlockHeight() // only allocate rewards if the block height is greater than 1 // and only allocate rewards for every multiple of 10 blocks for performance reasons. if blockHeight > 1 && blockHeight%10 == 0 { + // determine the total power signing the block + var previousTotalPower int64 + for _, voteInfo := range req.LastCommitInfo.GetVotes() { + previousTotalPower += voteInfo.Validator.Power + } + k.AllocateTokens(ctx, previousTotalPower, req.LastCommitInfo.GetVotes()) } From 3d554297189a249418264752cc607ae8d8807087 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Wed, 14 Feb 2024 22:57:44 -0700 Subject: [PATCH 03/11] distribute on block height 2 as well --- x/distribution/abci.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 0ef8e94d590c..c380b24e2da4 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -21,7 +21,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) blockHeight := ctx.BlockHeight() // only allocate rewards if the block height is greater than 1 // and only allocate rewards for every multiple of 10 blocks for performance reasons. - if blockHeight > 1 && blockHeight%10 == 0 { + if (blockHeight > 1 && blockHeight%10 == 0) || blockHeight == 2 { // determine the total power signing the block var previousTotalPower int64 for _, voteInfo := range req.LastCommitInfo.GetVotes() { From 21ebd30bd2e88e0136b373de41c86643fa8a7f81 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 15 Feb 2024 10:27:32 -0700 Subject: [PATCH 04/11] set distribution to each block for tests --- tests/e2e/distribution/suite.go | 3 +++ x/distribution/abci.go | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/e2e/distribution/suite.go b/tests/e2e/distribution/suite.go index 00b7884ab879..c6ebb47e8af4 100644 --- a/tests/e2e/distribution/suite.go +++ b/tests/e2e/distribution/suite.go @@ -15,6 +15,7 @@ import ( clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" + distr "github.com/cosmos/cosmos-sdk/x/distribution" "github.com/cosmos/cosmos-sdk/x/distribution/client/cli" distrclitestutil "github.com/cosmos/cosmos-sdk/x/distribution/client/testutil" distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" @@ -39,6 +40,8 @@ func NewE2ETestSuite(cfg network.Config) *E2ETestSuite { func (s *E2ETestSuite) SetupSuite() { s.T().Log("setting up e2e test suite") + distr.BlockMultipleToDistributeRewards = 1 + cfg := network.DefaultConfig(simapp.NewTestNetworkFixture) cfg.NumValidators = 1 s.cfg = cfg diff --git a/x/distribution/abci.go b/x/distribution/abci.go index c380b24e2da4..01f367212656 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -11,6 +11,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/types" ) +var BlockMultipleToDistributeRewards = int64(10) + // BeginBlocker sets the proposer for determining distribution during endblock // and distribute rewards for the previous block. func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) { @@ -20,8 +22,9 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) // ref https://github.com/cosmos/cosmos-sdk/issues/3095 blockHeight := ctx.BlockHeight() // only allocate rewards if the block height is greater than 1 - // and only allocate rewards for every multiple of 10 blocks for performance reasons. - if (blockHeight > 1 && blockHeight%10 == 0) || blockHeight == 2 { + // and for every multiple of 10 blocks for performance reasons. + // We special case blockHeight 2 to allow test cases to still be valid. + if (blockHeight > 1 && blockHeight%BlockMultipleToDistributeRewards == 0) || blockHeight == 2 { // determine the total power signing the block var previousTotalPower int64 for _, voteInfo := range req.LastCommitInfo.GetVotes() { From e35a0383ad21f89748ff65ffe378884bc4152bbd Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 15 Feb 2024 10:50:23 -0700 Subject: [PATCH 05/11] remove special case for test --- x/distribution/abci.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 01f367212656..be7751ff3206 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -23,8 +23,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) blockHeight := ctx.BlockHeight() // only allocate rewards if the block height is greater than 1 // and for every multiple of 10 blocks for performance reasons. - // We special case blockHeight 2 to allow test cases to still be valid. - if (blockHeight > 1 && blockHeight%BlockMultipleToDistributeRewards == 0) || blockHeight == 2 { + if blockHeight > 1 && blockHeight%BlockMultipleToDistributeRewards == 0 { // determine the total power signing the block var previousTotalPower int64 for _, voteInfo := range req.LastCommitInfo.GetVotes() { From 747d59a4559ff5328d32fc18c2ff16892097db95 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 15 Feb 2024 15:00:19 -0700 Subject: [PATCH 06/11] add test displaying delayed logic --- .../distribution/keeper/allocation_test.go | 69 +++++++++++++++++-- x/distribution/abci.go | 2 +- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/tests/integration/distribution/keeper/allocation_test.go b/tests/integration/distribution/keeper/allocation_test.go index 2090c17174e6..0b6316f81d42 100644 --- a/tests/integration/distribution/keeper/allocation_test.go +++ b/tests/integration/distribution/keeper/allocation_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "fmt" "testing" "cosmossdk.io/math" @@ -135,25 +136,81 @@ func TestAllocateTokensToManyValidators(t *testing.T) { }, } distrKeeper.AllocateTokens(ctx, 200, votes) + fmt.Println(distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards) // 98 outstanding rewards (100 less 2 to community pool) - require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards) - require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards) + firstValidator0OutstandingRewards := distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards + firstValidator1OutstandingRewards := distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards + require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, firstValidator0OutstandingRewards) + require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, firstValidator1OutstandingRewards) // 2 community pool coins require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(2)}}, distrKeeper.GetFeePool(ctx).CommunityPool) // 50% commission for first proposer, (0.5 * 98%) * 100 / 2 = 23.25 - require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2450, 2)}}, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission) + firstValidator0Commission := distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission + require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2450, 2)}}, firstValidator0Commission) // zero commission for second proposer - require.True(t, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission.IsZero()) + firstValidator1Commission := distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission + require.True(t, firstValidator1Commission.IsZero()) // just staking.proportional for first proposer less commission = (0.5 * 98%) * 100 / 2 = 24.50 - require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2450, 2)}}, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards) + firstValidator0CurrentRewards := distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards + require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2450, 2)}}, firstValidator0CurrentRewards) // proposer reward + staking.proportional for second proposer = (0.5 * (98%)) * 100 = 49 - require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards) + firstValidator1CurrentRewards := distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards + require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, firstValidator1CurrentRewards) + + // test that the block height triggers the distribution + + // 9 is not a multiple of 10, should not trigger allocation (no change in rewards) + ctx = ctx.WithBlockHeight(9) + app.BeginBlocker(ctx, abci.RequestBeginBlock{Header: tmproto.Header{ProposerAddress: valAddrs[0].Bytes()}, + LastCommitInfo: abci.CommitInfo{ + Votes: votes, + }, + }) + require.Equal(t, firstValidator0OutstandingRewards, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards) + require.Equal(t, firstValidator1OutstandingRewards, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards) + require.Equal(t, firstValidator0Commission, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission) + require.Equal(t, firstValidator1Commission, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission) + require.Equal(t, firstValidator0CurrentRewards, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards) + require.Equal(t, firstValidator1CurrentRewards, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards) + + // 10 is a multiple of 10, should trigger allocation + feesCollectedInt := bankKeeper.GetAllBalances(ctx, feeCollector.GetAddress()) + + // feesCollected was increased from last BeginBlocker call, then will occur again from the new BeginBlocker call, + // so we need to double the feesCollected to simulate the new BeginBlocker call + feesCollectedInt[0].Amount = feesCollectedInt[0].Amount.MulRaw(2) + feesCollected := sdk.NewDecCoinsFromCoins(feesCollectedInt...) + + communityTax := distrKeeper.GetCommunityTax(ctx) + voteMultiplier := math.LegacyOneDec().Sub(communityTax) + feeMultiplier := feesCollected.MulDecTruncate(voteMultiplier) + powerFraction := math.LegacyNewDec(100).QuoTruncate(math.LegacyNewDec(200)) + + newRewards := feeMultiplier.MulDecTruncate(powerFraction) + pendingRewards := firstValidator0OutstandingRewards.Add(newRewards...) + + pendingCommission := firstValidator0OutstandingRewards.Add(newRewards...) + pendingCommission[0].Amount = pendingCommission[0].Amount.Quo(sdk.NewDec(2)) + + ctx = ctx.WithBlockHeight(10) + app.BeginBlocker(ctx, abci.RequestBeginBlock{Header: tmproto.Header{ProposerAddress: valAddrs[0].Bytes()}, + LastCommitInfo: abci.CommitInfo{ + Votes: votes, + }, + }) + + require.Equal(t, pendingRewards, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards) + require.Equal(t, pendingRewards, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards) + require.Equal(t, pendingCommission, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission) + require.True(t, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission.IsZero()) + require.Equal(t, pendingCommission, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards) + require.Equal(t, pendingRewards, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards) } func TestAllocateTokensTruncation(t *testing.T) { diff --git a/x/distribution/abci.go b/x/distribution/abci.go index be7751ff3206..0b3c9c5e2de3 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -23,7 +23,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) blockHeight := ctx.BlockHeight() // only allocate rewards if the block height is greater than 1 // and for every multiple of 10 blocks for performance reasons. - if blockHeight > 1 && blockHeight%BlockMultipleToDistributeRewards == 0 { + if (blockHeight > 1 && blockHeight%BlockMultipleToDistributeRewards == 0) || blockHeight == 2 { // determine the total power signing the block var previousTotalPower int64 for _, voteInfo := range req.LastCommitInfo.GetVotes() { From f51763440d01aaca741a1b9753b38e1bca6380bd Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 15 Feb 2024 15:05:07 -0700 Subject: [PATCH 07/11] add clarifying comments, change to 20 --- tests/e2e/distribution/suite.go | 2 ++ .../distribution/keeper/allocation_test.go | 12 ++++++------ x/distribution/abci.go | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/e2e/distribution/suite.go b/tests/e2e/distribution/suite.go index c6ebb47e8af4..a7b8b50f195c 100644 --- a/tests/e2e/distribution/suite.go +++ b/tests/e2e/distribution/suite.go @@ -40,6 +40,8 @@ func NewE2ETestSuite(cfg network.Config) *E2ETestSuite { func (s *E2ETestSuite) SetupSuite() { s.T().Log("setting up e2e test suite") + // We distribute rewards every block here since we test the delayed distribution + // in another test. distr.BlockMultipleToDistributeRewards = 1 cfg := network.DefaultConfig(simapp.NewTestNetworkFixture) diff --git a/tests/integration/distribution/keeper/allocation_test.go b/tests/integration/distribution/keeper/allocation_test.go index 0b6316f81d42..2909fb5730ad 100644 --- a/tests/integration/distribution/keeper/allocation_test.go +++ b/tests/integration/distribution/keeper/allocation_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - "fmt" "testing" "cosmossdk.io/math" @@ -15,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" + dist "github.com/cosmos/cosmos-sdk/x/distribution" "github.com/cosmos/cosmos-sdk/x/distribution/keeper" "github.com/cosmos/cosmos-sdk/x/distribution/testutil" disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types" @@ -136,7 +136,6 @@ func TestAllocateTokensToManyValidators(t *testing.T) { }, } distrKeeper.AllocateTokens(ctx, 200, votes) - fmt.Println(distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards) // 98 outstanding rewards (100 less 2 to community pool) firstValidator0OutstandingRewards := distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards @@ -165,8 +164,8 @@ func TestAllocateTokensToManyValidators(t *testing.T) { // test that the block height triggers the distribution - // 9 is not a multiple of 10, should not trigger allocation (no change in rewards) - ctx = ctx.WithBlockHeight(9) + // block height is not a multiple, should not trigger allocation (no change in rewards) + ctx = ctx.WithBlockHeight(dist.BlockMultipleToDistributeRewards - 1) app.BeginBlocker(ctx, abci.RequestBeginBlock{Header: tmproto.Header{ProposerAddress: valAddrs[0].Bytes()}, LastCommitInfo: abci.CommitInfo{ Votes: votes, @@ -179,7 +178,9 @@ func TestAllocateTokensToManyValidators(t *testing.T) { require.Equal(t, firstValidator0CurrentRewards, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards) require.Equal(t, firstValidator1CurrentRewards, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards) - // 10 is a multiple of 10, should trigger allocation + // block height is not a multiple, should trigger allocation + ctx = ctx.WithBlockHeight(dist.BlockMultipleToDistributeRewards) + feesCollectedInt := bankKeeper.GetAllBalances(ctx, feeCollector.GetAddress()) // feesCollected was increased from last BeginBlocker call, then will occur again from the new BeginBlocker call, @@ -198,7 +199,6 @@ func TestAllocateTokensToManyValidators(t *testing.T) { pendingCommission := firstValidator0OutstandingRewards.Add(newRewards...) pendingCommission[0].Amount = pendingCommission[0].Amount.Quo(sdk.NewDec(2)) - ctx = ctx.WithBlockHeight(10) app.BeginBlocker(ctx, abci.RequestBeginBlock{Header: tmproto.Header{ProposerAddress: valAddrs[0].Bytes()}, LastCommitInfo: abci.CommitInfo{ Votes: votes, diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 0b3c9c5e2de3..10dc19477bb6 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -11,7 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/types" ) -var BlockMultipleToDistributeRewards = int64(10) +var BlockMultipleToDistributeRewards = int64(20) // BeginBlocker sets the proposer for determining distribution during endblock // and distribute rewards for the previous block. From a9dae04724322b2ab15c2d346af1c13a2aa88503 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 15 Feb 2024 15:21:07 -0700 Subject: [PATCH 08/11] remove block height 2 hack --- x/distribution/abci.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 10dc19477bb6..1b06a059ce4b 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -23,7 +23,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) blockHeight := ctx.BlockHeight() // only allocate rewards if the block height is greater than 1 // and for every multiple of 10 blocks for performance reasons. - if (blockHeight > 1 && blockHeight%BlockMultipleToDistributeRewards == 0) || blockHeight == 2 { + if blockHeight > 1 && blockHeight%BlockMultipleToDistributeRewards == 0 { // determine the total power signing the block var previousTotalPower int64 for _, voteInfo := range req.LastCommitInfo.GetVotes() { From e263fe0d8a5566ab834fd094848038b7e5e9194a Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 15 Feb 2024 15:21:31 -0700 Subject: [PATCH 09/11] comment --- x/distribution/abci.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 1b06a059ce4b..f0f7d6d8e413 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -22,7 +22,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) // ref https://github.com/cosmos/cosmos-sdk/issues/3095 blockHeight := ctx.BlockHeight() // only allocate rewards if the block height is greater than 1 - // and for every multiple of 10 blocks for performance reasons. + // and for every multiple of 20 blocks for performance reasons. if blockHeight > 1 && blockHeight%BlockMultipleToDistributeRewards == 0 { // determine the total power signing the block var previousTotalPower int64 From 7aa7b478e4316e864689f388e0c9189f1fa2981c Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 15 Feb 2024 16:22:27 -0600 Subject: [PATCH 10/11] Update allocation_test.go --- tests/integration/distribution/keeper/allocation_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/distribution/keeper/allocation_test.go b/tests/integration/distribution/keeper/allocation_test.go index 2909fb5730ad..27827253f02d 100644 --- a/tests/integration/distribution/keeper/allocation_test.go +++ b/tests/integration/distribution/keeper/allocation_test.go @@ -178,7 +178,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) { require.Equal(t, firstValidator0CurrentRewards, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards) require.Equal(t, firstValidator1CurrentRewards, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards) - // block height is not a multiple, should trigger allocation + // block height is a multiple, should trigger allocation ctx = ctx.WithBlockHeight(dist.BlockMultipleToDistributeRewards) feesCollectedInt := bankKeeper.GetAllBalances(ctx, feeCollector.GetAddress()) From 15584928a95e50d77ce7e814f5a8260b42943f70 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 15 Feb 2024 21:09:54 -0700 Subject: [PATCH 11/11] test fixes, up to 50 blocks --- tests/integration/distribution/keeper/allocation_test.go | 5 +++++ tests/integration/distribution/keeper/msg_server_test.go | 9 ++++++++- x/distribution/abci.go | 4 ++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/integration/distribution/keeper/allocation_test.go b/tests/integration/distribution/keeper/allocation_test.go index 27827253f02d..37c14d645044 100644 --- a/tests/integration/distribution/keeper/allocation_test.go +++ b/tests/integration/distribution/keeper/allocation_test.go @@ -72,6 +72,10 @@ func TestAllocateTokensToManyValidators(t *testing.T) { stakingKeeper *stakingkeeper.Keeper ) + // set distribute to every block for first test. + // we test the delayed distribution in the next test. + dist.BlockMultipleToDistributeRewards = 1 + app, err := simtestutil.Setup(testutil.AppConfig, &accountKeeper, &bankKeeper, @@ -163,6 +167,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) { require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, firstValidator1CurrentRewards) // test that the block height triggers the distribution + dist.BlockMultipleToDistributeRewards = 50 // block height is not a multiple, should not trigger allocation (no change in rewards) ctx = ctx.WithBlockHeight(dist.BlockMultipleToDistributeRewards - 1) diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 59862f6d5c50..00b544d6ad15 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -2,6 +2,7 @@ package keeper_test import ( sdk "github.com/cosmos/cosmos-sdk/types" + banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" "github.com/cosmos/cosmos-sdk/x/distribution/types" ) @@ -159,7 +160,13 @@ func (s *KeeperTestSuite) TestCommunityPoolSpend() { for _, tc := range testCases { tc := tc s.Run(tc.name, func() { - _, err := s.msgServer.CommunityPoolSpend(s.ctx, tc.input) + amount := sdk.NewCoins(sdk.NewInt64Coin("stake", 100)) + s.Require().NoError(banktestutil.FundAccount(s.bankKeeper, s.ctx, s.addrs[0], amount)) + + err := s.distrKeeper.FundCommunityPool(s.ctx, amount, s.addrs[0]) + s.Require().Nil(err) + + _, err = s.msgServer.CommunityPoolSpend(s.ctx, tc.input) if tc.expErr { s.Require().Error(err) diff --git a/x/distribution/abci.go b/x/distribution/abci.go index f0f7d6d8e413..2b21fe67b225 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -11,7 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/types" ) -var BlockMultipleToDistributeRewards = int64(20) +var BlockMultipleToDistributeRewards = int64(50) // BeginBlocker sets the proposer for determining distribution during endblock // and distribute rewards for the previous block. @@ -22,7 +22,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) // ref https://github.com/cosmos/cosmos-sdk/issues/3095 blockHeight := ctx.BlockHeight() // only allocate rewards if the block height is greater than 1 - // and for every multiple of 20 blocks for performance reasons. + // and for every multiple of 50 blocks for performance reasons. if blockHeight > 1 && blockHeight%BlockMultipleToDistributeRewards == 0 { // determine the total power signing the block var previousTotalPower int64