Skip to content

Commit

Permalink
ORA1530 Investigate strange results for TestRewardForTopicGoesUpWhenR…
Browse files Browse the repository at this point in the history
…elativeStakeGoesUp (#325)

Currently there are no rewards distributed in the first round, and a low
amount of rewards distributed in the second round. Investigate why this
is happening.

Result: Normalizing all of the reputer values gives more predictable
rewards and isolates the effect of stake on rewards.

---------

Co-authored-by: Kenny P <17100641+kpeluso@users.noreply.github.com>
  • Loading branch information
br4e and kpeluso authored Jun 5, 2024
1 parent 1af6ec0 commit 60c422d
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions x/emissions/module/rewards/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2713,7 +2713,7 @@ func (s *RewardsTestSuite) TestTotalInferersRewardFractionGrowsWithMoreInferers(
}

func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {
/// SETUP
// setup
require := s.Require()

alphaRegret := alloraMath.MustNewDecFromString("0.1")
Expand All @@ -2729,6 +2729,7 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {
s.addrs[2],
}

// separate addresses for the second topic
reputer1Addrs := []sdk.AccAddress{
s.addrs[3],
s.addrs[4],
Expand All @@ -2741,29 +2742,32 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {
s.addrs[8],
}

// setup topics
stake := cosmosMath.NewInt(1000).Mul(inference_synthesis.CosmosIntOneE18())

topicId0 := s.setUpTopicWithEpochLength(block, workerAddrs, reputer0Addrs, stake, alphaRegret, 1)
topicId1 := s.setUpTopicWithEpochLength(block, workerAddrs, reputer1Addrs, stake, alphaRegret, 1)

// setup values to be identical for both topics
reputer0Values := []TestWorkerValue{
{Address: s.addrs[0], Value: "0.1"},
{Address: s.addrs[0], Value: "0.2"},
{Address: s.addrs[1], Value: "0.2"},
{Address: s.addrs[2], Value: "0.3"},
{Address: s.addrs[2], Value: "0.2"},
}

reputer1Values := []TestWorkerValue{
{Address: s.addrs[3], Value: "0.1"},
{Address: s.addrs[3], Value: "0.2"},
{Address: s.addrs[4], Value: "0.2"},
{Address: s.addrs[5], Value: "0.3"},
{Address: s.addrs[5], Value: "0.2"},
}

workerValues := []TestWorkerValue{
{Address: s.addrs[6], Value: "0.1"},
{Address: s.addrs[6], Value: "0.2"},
{Address: s.addrs[7], Value: "0.2"},
{Address: s.addrs[8], Value: "0.3"},
{Address: s.addrs[8], Value: "0.2"},
}

// record the stakes on each topic so we can see the reward differences
reputer0_Stake0, err := s.emissionsKeeper.GetStakeOnReputerInTopic(s.ctx, topicId0, s.addrs[0].String())
require.NoError(err)
reputer1_Stake0, err := s.emissionsKeeper.GetStakeOnReputerInTopic(s.ctx, topicId0, s.addrs[1].String())
Expand All @@ -2778,6 +2782,7 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {
reputer5_Stake0, err := s.emissionsKeeper.GetStakeOnReputerInTopic(s.ctx, topicId1, s.addrs[5].String())
require.NoError(err)

// do work on the topics to earn rewards
s.getRewardsDistribution(
topicId0,
block,
Expand All @@ -2798,6 +2803,7 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {
"0.1",
)

// force rewards to be distributed
s.MintTokensToModule(types.AlloraRewardsAccountName, cosmosMath.NewInt(1000))

err = s.emissionsAppModule.EndBlock(s.ctx)
Expand Down Expand Up @@ -2836,6 +2842,7 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {
reputer0_Reward0 := reputer0_Stake1.Sub(reputer0_Stake0)
reputer1_Reward0 := reputer1_Stake1.Sub(reputer1_Stake0)
reputer2_Reward0 := reputer2_Stake1.Sub(reputer2_Stake0)

reputer3_Reward0 := reputer3_Stake1.Sub(reputer3_Stake0)
reputer4_Reward0 := reputer4_Stake1.Sub(reputer4_Stake0)
reputer5_Reward0 := reputer5_Stake1.Sub(reputer5_Stake0)
Expand All @@ -2845,20 +2852,20 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {

require.Equal(topic0RewardTotal0, topic1RewardTotal0)

// Now, in second trial, increase stake for all reputers in topic 1
for i := 3; i < 6; i++ {
s.MintTokensToAddress(s.addrs[i], stake)
_, err = s.msgServer.AddStake(s.ctx, &types.MsgAddStake{
Sender: s.addrs[i].String(),
Amount: stake,
TopicId: topicId1,
})
require.NoError(err)
}
// Now, in second trial, increase stake for all first reputer in topic1
s.MintTokensToAddress(s.addrs[3], stake)
_, err = s.msgServer.AddStake(s.ctx, &types.MsgAddStake{
Sender: s.addrs[3].String(),
Amount: stake,
TopicId: topicId1,
})
require.NoError(err)

// record the updated stakes
reputer3_Stake1, err = s.emissionsKeeper.GetStakeOnReputerInTopic(s.ctx, topicId1, s.addrs[3].String())
require.NoError(err)

// do work on the topics to earn rewards
s.getRewardsDistribution(
topicId0,
block,
Expand All @@ -2879,6 +2886,7 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {
"0.1",
)

// force rewards to be distributed
block++
s.ctx = s.ctx.WithBlockHeight(block)

Expand All @@ -2887,6 +2895,7 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {
err = s.emissionsAppModule.EndBlock(s.ctx)
require.NoError(err)

// record the stakes after
reputer0_Stake2, err := s.emissionsKeeper.GetStakeOnReputerInTopic(s.ctx, topicId0, s.addrs[0].String())
require.NoError(err)
reputer1_Stake2, err := s.emissionsKeeper.GetStakeOnReputerInTopic(s.ctx, topicId0, s.addrs[1].String())
Expand All @@ -2901,18 +2910,27 @@ func (s *RewardsTestSuite) TestRewardForTopicGoesUpWhenRelativeStakeGoesUp() {
reputer5_Stake2, err := s.emissionsKeeper.GetStakeOnReputerInTopic(s.ctx, topicId1, s.addrs[5].String())
require.NoError(err)

// calculate rewards
reputer0_Reward1 := reputer0_Stake2.Sub(reputer0_Stake1)
reputer1_Reward1 := reputer1_Stake2.Sub(reputer1_Stake1)
reputer2_Reward1 := reputer2_Stake2.Sub(reputer2_Stake1)

reputer3_Reward1 := reputer3_Stake2.Sub(reputer3_Stake1)
reputer4_Reward1 := reputer4_Stake2.Sub(reputer4_Stake1)
reputer5_Reward1 := reputer5_Stake2.Sub(reputer5_Stake1)

// calculate total rewards for each topic
topic0RewardTotal1 := reputer0_Reward1.Add(reputer1_Reward1).Add(reputer2_Reward1)
topic1RewardTotal1 := reputer3_Reward1.Add(reputer4_Reward1).Add(reputer5_Reward1)

// in the first round, the rewards should be equal for each topic
require.True(topic0RewardTotal0.Equal(topic1RewardTotal0))
// for topic 0, the rewards should be less in the second round
require.True(topic0RewardTotal0.GT(topic0RewardTotal1))
// in the second round, the rewards should be greater for topic 1
require.True(topic0RewardTotal1.LT(topic1RewardTotal1))
require.True(topic1RewardTotal1.GT(topic1RewardTotal0))
// the rewards for topic 1 should be greater in the second round
require.True(topic1RewardTotal0.LT(topic1RewardTotal1))
}

func (s *RewardsTestSuite) TestReputerAboveConsensusGetsLessRewards() {
Expand Down

0 comments on commit 60c422d

Please sign in to comment.