diff --git a/x/emissions/keeper/keeper.go b/x/emissions/keeper/keeper.go index ef91e748a..c416f175a 100644 --- a/x/emissions/keeper/keeper.go +++ b/x/emissions/keeper/keeper.go @@ -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 @@ -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 } @@ -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 diff --git a/x/emissions/keeper/keeper_test.go b/x/emissions/keeper/keeper_test.go index 0165c020b..c5b4200f5 100644 --- a/x/emissions/keeper/keeper_test.go +++ b/x/emissions/keeper/keeper_test.go @@ -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") +} diff --git a/x/emissions/module/rewards/topic_rewards_test.go b/x/emissions/module/rewards/topic_rewards_test.go index 1f204e89e..46f4230d6 100644 --- a/x/emissions/module/rewards/topic_rewards_test.go +++ b/x/emissions/module/rewards/topic_rewards_test.go @@ -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"),