From 71a80e5dd3d82953e6ccc2b7c92a6604d95efd50 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Fri, 16 Feb 2024 00:06:35 -0600 Subject: [PATCH] feat: distribute every 50 blocks (#527) * distribute every 10 blocks * total power logic in loop * distribute on block height 2 as well * set distribution to each block for tests * remove special case for test * add test displaying delayed logic * add clarifying comments, change to 20 * remove block height 2 hack * comment * Update allocation_test.go * test fixes, up to 50 blocks --- tests/e2e/distribution/suite.go | 5 +++++ x/distribution/abci.go | 19 ++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/e2e/distribution/suite.go b/tests/e2e/distribution/suite.go index 54d02961f747..dd2426417d17 100644 --- a/tests/e2e/distribution/suite.go +++ b/tests/e2e/distribution/suite.go @@ -19,6 +19,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" distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" @@ -42,6 +43,10 @@ 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) cfg.NumValidators = 1 s.cfg = cfg diff --git a/x/distribution/abci.go b/x/distribution/abci.go index 70fdb7eeb270..7bf43c5b82d2 100644 --- a/x/distribution/abci.go +++ b/x/distribution/abci.go @@ -7,20 +7,25 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/types" ) +var BlockMultipleToDistributeRewards = int64(50) + // BeginBlocker sets the proposer for determining distribution during endblock // and distribute rewards for the previous block. func BeginBlocker(ctx sdk.Context, k keeper.Keeper) error { defer telemetry.ModuleMeasureSince(types.ModuleName, telemetry.Now(), telemetry.MetricKeyBeginBlocker) - // determine the total power signing the block - var previousTotalPower int64 - for _, voteInfo := range ctx.VoteInfos() { - previousTotalPower += voteInfo.Validator.Power - } - // 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 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 + for _, voteInfo := range ctx.VoteInfos() { + previousTotalPower += voteInfo.Validator.Power + } + if err := k.AllocateTokens(ctx, previousTotalPower, ctx.VoteInfos()); err != nil { return err }