Skip to content

Commit

Permalink
Add fix to avoid double score update in first payload submission
Browse files Browse the repository at this point in the history
  • Loading branch information
guilherme-brandao committed Dec 19, 2024
1 parent e27bdbe commit 8ea9305
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
12 changes: 9 additions & 3 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,9 @@ func (k *Keeper) AppendInference(
}

// Check if the inferer is new and set initial EMA score
firstSubmission := false
if previousEmaScore.BlockHeight == 0 {
firstSubmission = true
initialEmaScore, err := k.GetTopicInitialInfererEmaScore(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting topic initial ema score")
Expand Down Expand Up @@ -1394,7 +1396,7 @@ func (k *Keeper) AppendInference(
return k.InsertInference(ctx, topic.Id, *inference)
} else {
// Update EMA score for the current inferer, who is the lowest score inferer
if previousEmaScore.BlockHeight != 0 { // Only update if not a new inferer
if !firstSubmission { // Only update if not a new inferer
err = k.CalcAndSaveInfererScoreEmaWithLastSavedTopicQuantile(ctx, topic, nonceBlockHeight, previousEmaScore)
if err != nil {
return errorsmod.Wrap(err, "error calculating and saving inferer score ema with last saved topic quantile")
Expand Down Expand Up @@ -1474,7 +1476,9 @@ func (k *Keeper) AppendForecast(
}

// Check if the forecaster is new and set initial EMA score
firstSubmission := false
if previousEmaScore.BlockHeight == 0 {
firstSubmission = true
initialEmaScore, err := k.GetTopicInitialForecasterEmaScore(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting topic initial ema score")
Expand Down Expand Up @@ -1560,7 +1564,7 @@ func (k *Keeper) AppendForecast(
return k.InsertForecast(ctx, topic.Id, *forecast)
} else {
// Update EMA score for the current forecaster, who is the lowest score forecaster
if previousEmaScore.BlockHeight != 0 {
if !firstSubmission {
err = k.CalcAndSaveForecasterScoreEmaWithLastSavedTopicQuantile(ctx, topic, nonceBlockHeight, previousEmaScore)
if err != nil {
return errorsmod.Wrap(err, "error calculating and saving forecaster score ema with last saved topic quantile")
Expand Down Expand Up @@ -1693,7 +1697,9 @@ func (k *Keeper) AppendReputerLoss(
}

// Check if the reputer is new and set initial EMA score
firstSubmission := false
if previousEmaScore.BlockHeight == 0 {
firstSubmission = true
initialEmaScore, err := k.GetTopicInitialReputerEmaScore(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting topic initial ema score")
Expand Down Expand Up @@ -1778,7 +1784,7 @@ func (k *Keeper) AppendReputerLoss(
return k.InsertReputerLoss(ctx, topic.Id, *reputerLoss)
} else {
// Update EMA score for the current reputer, who is the lowest score reputer
if previousEmaScore.BlockHeight != 0 {
if !firstSubmission {
err = k.CalcAndSaveReputerScoreEmaWithLastSavedTopicQuantile(ctx, topic, nonceBlockHeight, previousEmaScore)
if err != nil {
return errorsmod.Wrap(err, "error calculating and saving reputer score ema with last saved topic quantile")
Expand Down
58 changes: 58 additions & 0 deletions x/emissions/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5361,3 +5361,61 @@ func (s *KeeperTestSuite) TestInitialEmaScoreSettingInAppendReputer() {
s.Require().Equal(reputer, score.Address)
s.Require().Equal(topicId, score.TopicId)
}

func (s *KeeperTestSuite) TestFirstSubmissionDoesNotUpdateEMAUsingQuantile() {
ctx := s.ctx
k := s.emissionsKeeper
topicId := s.CreateOneTopic(10800)

params := types.DefaultParams()
params.MaxTopInferersToReward = 4
err := k.SetParams(ctx, params)
s.Require().NoError(err)

for i := 0; i < int(params.MaxTopInferersToReward); i++ {
score := types.Score{
TopicId: topicId,
Address: s.addrsStr[i],
BlockHeight: 1,
Score: alloraMath.NewDecFromInt64(90 + int64(i)),
}
err := k.SetInfererScoreEma(ctx, topicId, s.addrsStr[i], score)
s.Require().NoError(err)

err = k.AddActiveInferer(ctx, topicId, s.addrsStr[i])
s.Require().NoError(err)

Check failure on line 5386 in x/emissions/keeper/keeper_test.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int (gosec)

if i == 0 {
err = k.SetLowestInfererScoreEma(ctx, topicId, score)
s.Require().NoError(err)
}
}

// Set a low initial score for new actors
initialScore := alloraMath.NewDecFromInt64(50)
err = k.SetTopicInitialInfererEmaScore(ctx, topicId, initialScore)
s.Require().NoError(err)

// Create a new inference from a new actor
inference := &types.Inference{
TopicId: topicId,
BlockHeight: 2,
Inferer: s.addrsStr[9], // Using a different address
Value: alloraMath.NewDecFromInt64(100),
ExtraData: nil,
Proof: "",
}

topic, err := k.GetTopic(ctx, topicId)
s.Require().NoError(err)

// Submit inference - should not trigger EMA update using quantile since it's first submission
// and score is lower than active set
err = k.AppendInference(ctx, topic, 2, inference, params.MaxTopInferersToReward)
s.Require().NoError(err)

// Verify score remains at initial value
score, err := k.GetInfererScoreEma(ctx, topicId, s.addrsStr[9])
s.Require().NoError(err)
s.Require().Equal(initialScore, score.Score)
}

0 comments on commit 8ea9305

Please sign in to comment.