From db18762976dba1c73e49e7f6c7fdbb72d0c36c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilherme=20Brand=C3=A3o?= Date: Wed, 18 Dec 2024 15:29:52 -0300 Subject: [PATCH] Test initial ema score --- x/emissions/module/rewards/scores_test.go | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/x/emissions/module/rewards/scores_test.go b/x/emissions/module/rewards/scores_test.go index 61c499c5e..f4953af15 100644 --- a/x/emissions/module/rewards/scores_test.go +++ b/x/emissions/module/rewards/scores_test.go @@ -1804,3 +1804,41 @@ func (s *RewardsTestSuite) TestGenerateReputerScoresWithZeroListeningCoefficient s.Require().NoError(err) s.Require().True(coefficient.Coefficient.Equal(params.EpsilonReputer)) } + +func (s *RewardsTestSuite) TestCalculateTopicInitialEmaScore() { + // Setup test scores + scores := []types.Score{ + {Score: alloraMath.MustNewDecFromString("0.5")}, + {Score: alloraMath.MustNewDecFromString("0.3")}, + {Score: alloraMath.MustNewDecFromString("0.1")}, + {Score: alloraMath.MustNewDecFromString("0.4")}, + {Score: alloraMath.MustNewDecFromString("0.2")}, + } + + // Calculate initial EMA score + initialScore, err := rewards.CalculateTopicInitialEmaScore(s.ctx, s.emissionsKeeper, scores) + s.Require().NoError(err) + + // Get lambda from params + params, err := s.emissionsKeeper.GetParams(s.ctx) + s.Require().NoError(err) + lambda := params.SortitionLambdaPenalty + + // Calculate expected score manually + // Standard deviation ≈ 0.1581139 + stdDev := alloraMath.MustNewDecFromString("0.1581139") + lambdaStdDev, err := lambda.Mul(stdDev) + s.Require().NoError(err) + + // Lowest score is 0.1 + lowestScore := alloraMath.MustNewDecFromString("0.1") + expectedScore, err := lowestScore.Sub(lambdaStdDev) + s.Require().NoError(err) + + // Verify result matches expected + diff, err := initialScore.Sub(expectedScore) + s.Require().NoError(err) + absDiff, err := diff.Abs() + s.Require().NoError(err) + s.Require().True(absDiff.Lt(alloraMath.MustNewDecFromString("0.000001"))) +}