Skip to content

Commit

Permalink
Quo vs Mul to apply dripping (#564)
Browse files Browse the repository at this point in the history
## Purpose of Changes and their Description

Fixes hyper aggressive topic dripping
Adds a test, fixes another test that was not setting the
`BlocksPerEpoch` on the params, getting a div by zero.

## Link(s) to Ticket(s) or Issue(s) resolved by this PR
PROTO-2150, RES-302


## Are these changes tested and documented?

- [X] If tested, please describe how. If not, why tests are not needed.
-- tested locally for now, in line with design
- [X] If documented, please describe where. If not, describe why docs
are not needed. -- in line with design now
- [X] Added to `Unreleased` section of `CHANGELOG.md`? -- no need since
it is a fix on new feature that already is in the Changelog.
  • Loading branch information
xmariachi authored Aug 30, 2024
1 parent 5d8c26a commit 9630dad
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
20 changes: 15 additions & 5 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,10 @@ func (k *Keeper) DripTopicFeeRevenue(ctx sdk.Context, topicId TopicId, block Blo
return err
}
blocksPerEpoch := alloraMath.NewDecFromInt64(topic.EpochLength)
if blocksPerEpoch.IsZero() {
ctx.Logger().Warn(fmt.Sprintf("Blocks per epoch is zero for topic %d. Skipping fee revenue drip.", topicId))
return nil
}
blocksPerWeek, err := calculateBlocksPerWeek(ctx, *k)
if err != nil {
return err
Expand All @@ -2056,8 +2060,13 @@ func (k *Keeper) DripTopicFeeRevenue(ctx sdk.Context, topicId TopicId, block Blo
if err != nil {
return err
}
if epochsPerWeek.IsZero() {
// Log a warning
ctx.Logger().Warn(fmt.Sprintf("Epochs per week is zero for topic %d. Skipping fee revenue drip.", topicId))
return nil
}
// this delta is the drip per epoch
dripPerEpoch, err := topicFeeRevenueDec.Mul(epochsPerWeek)
dripPerEpoch, err := topicFeeRevenueDec.Quo(epochsPerWeek)
if err != nil {
return err
}
Expand All @@ -2084,10 +2093,11 @@ func (k *Keeper) DripTopicFeeRevenue(ctx sdk.Context, topicId TopicId, block Blo
if err = k.SetLastDripBlock(ctx, topicId, topic.EpochLastEnded); err != nil {
return err
}
logMsg := fmt.Sprintf(
"Dripping topic fee revenue: block %d, topicId %d, oldRevenue %v, newRevenue %v",
ctx.BlockHeight(), topicId, topicFeeRevenue, newTopicFeeRevenue)
ctx.Logger().Debug(logMsg)
ctx.Logger().Debug("Dripping topic fee revenue",
"block", ctx.BlockHeight(),
"topicId", topicId,
"oldRevenue", topicFeeRevenue,
"newRevenue", newTopicFeeRevenue)
return k.topicFeeRevenue.Set(ctx, topicId, newTopicFeeRevenue)
}
return nil
Expand Down
44 changes: 44 additions & 0 deletions x/emissions/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3764,3 +3764,47 @@ func (s *KeeperTestSuite) TestAppendReputerLoss() {
s.Require().Equal(uint64(len(newAllReputerLosses.ReputerValueBundles)), params.MaxTopReputersToReward)
s.Require().Equal(newAllReputerLosses.ReputerValueBundles[1].ValueBundle.Reputer, reputer3)
}

func (s *KeeperTestSuite) TestDripTopicFeeRevenue() {
// Initialize the test environment
ctx := s.ctx
k := s.emissionsKeeper
require := s.Require()

// Define test data
topicId := uint64(1)
epochLength := int64(5)
block := int64(100)
// Calculated expected drip with these values: 26
expectedDrip := cosmosMath.NewInt(26)
initialRevenue := cosmosMath.NewInt(1000000) // 0.001 in Int representation (assuming 6 decimal places)

// Create and activate a topic
topic := types.Topic{Id: topicId, EpochLength: epochLength}
err := k.SetTopic(ctx, topicId, topic)
require.NoError(err, "Setting a new topic should not fail")

err = k.ActivateTopic(ctx, topicId)
require.NoError(err, "Activating the topic should not fail")

// Set up initial topic fee revenue
err = k.AddTopicFeeRevenue(ctx, topicId, initialRevenue)
require.NoError(err, "Setting initial topic fee revenue should not fail")

// Call the function under test
err = k.DripTopicFeeRevenue(ctx, topicId, block)
require.NoError(err, "DripTopicFeeRevenue should not return an error")

// Retrieve the updated topic fee revenue
updatedTopicFeeRevenue, err := k.GetTopicFeeRevenue(ctx, topicId)
require.NoError(err, "Getting topic fee revenue should not fail")

// Assert the expected results
require.True(updatedTopicFeeRevenue.LT(initialRevenue),
"The topic fee revenue should have decreased after dripping")

// Calculate expected revenue (this may need adjustment based on your actual implementation)
expectedRevenue := initialRevenue.Sub(expectedDrip)
require.Equal(expectedRevenue.String(), updatedTopicFeeRevenue.String(),
"The topic fee revenue should match the expected value after dripping")
}
1 change: 1 addition & 0 deletions x/emissions/module/rewards/topic_rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func (s *RewardsTestSuite) TestGetAndUpdateActiveTopicWeights() {
ctx := s.ctx
maxActiveTopicsNum := uint64(2)
params := types.Params{
BlocksPerMonth: 864000,
MaxActiveTopicsPerBlock: maxActiveTopicsNum,
MaxPageLimit: uint64(100),
TopicRewardAlpha: alloraMath.MustNewDecFromString("0.5"),
Expand Down

0 comments on commit 9630dad

Please sign in to comment.