Skip to content

Commit

Permalink
feat: distribute every 50 blocks (#527)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
czarcas7ic committed May 9, 2024
1 parent 3ef7ba4 commit 71a80e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions tests/e2e/distribution/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
19 changes: 12 additions & 7 deletions x/distribution/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 71a80e5

Please sign in to comment.