From 7b26fbe802528cd3bf400f0ea1a1045c0dd08ec3 Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 29 Aug 2024 19:43:36 -0400 Subject: [PATCH] calculate score emas and replace latestScore with them --- x/emissions/keeper/keeper.go | 60 +++++++------------ x/emissions/keeper/keeper_test.go | 50 ++++++++-------- .../msgserver/msg_server_worker_payload.go | 2 +- .../msg_server_worker_payload_test.go | 16 ++--- .../keeper/queryserver/query_server_score.go | 6 +- .../queryserver/query_server_score_test.go | 8 +-- x/emissions/keeper/score_utils.go | 12 ++-- x/emissions/keeper/score_utils_test.go | 18 +++--- x/emissions/module/rewards/scores.go | 51 +++++++++++++++- x/emissions/proto/emissions/v3/genesis.proto | 2 +- 10 files changed, 127 insertions(+), 98 deletions(-) diff --git a/x/emissions/keeper/keeper.go b/x/emissions/keeper/keeper.go index 56915b2d8..c9bee6122 100644 --- a/x/emissions/keeper/keeper.go +++ b/x/emissions/keeper/keeper.go @@ -814,7 +814,7 @@ func (k *Keeper) AppendInference(ctx context.Context, topicId TopicId, nonce typ return k.allInferences.Set(ctx, key, newInferences) } // get score of current inference and check with - score, err := k.GetLatestInfererScore(ctx, topicId, inference.Inferer) + score, err := k.GetInfererScoreEma(ctx, topicId, inference.Inferer) if err != nil { return err } @@ -876,7 +876,7 @@ func (k *Keeper) AppendForecast(ctx context.Context, topicId TopicId, nonce type return k.allForecasts.Set(ctx, key, newForecasts) } // get score of current inference and check with - score, err := k.GetLatestForecasterScore(ctx, topicId, forecast.Forecaster) + score, err := k.GetForecasterScoreEma(ctx, topicId, forecast.Forecaster) if err != nil { return err } @@ -976,7 +976,7 @@ func (k *Keeper) AppendReputerLoss(ctx context.Context, topicId TopicId, block B } // get score of current inference and check with - score, err := k.GetLatestReputerScore(ctx, topicId, reputerLoss.ValueBundle.Reputer) + score, err := k.GetReputerScoreEma(ctx, topicId, reputerLoss.ValueBundle.Reputer) if err != nil { return err } @@ -2212,23 +2212,12 @@ func (k *Keeper) RemoveRewardableTopic(ctx context.Context, topicId TopicId) err /// SCORES // If the new score is older than the current score, don't update -func (k *Keeper) SetLatestInfererScore(ctx context.Context, topicId TopicId, worker ActorId, score types.Score) error { - oldScore, err := k.GetLatestInfererScore(ctx, topicId, worker) - if err != nil { - return errorsmod.Wrap(err, "error getting latest inferer score") - } - if oldScore.BlockHeight >= score.BlockHeight { - return nil - } +func (k *Keeper) SetInfererScoreEma(ctx context.Context, topicId TopicId, worker ActorId, score types.Score) error { key := collections.Join(topicId, worker) - err = k.infererScoreEmas.Set(ctx, key, score) - if err != nil { - return errorsmod.Wrap(err, "error setting latest inferer score") - } - return nil + return k.infererScoreEmas.Set(ctx, key, score) } -func (k *Keeper) GetLatestInfererScore(ctx context.Context, topicId TopicId, worker ActorId) (types.Score, error) { +func (k *Keeper) GetInfererScoreEma(ctx context.Context, topicId TopicId, worker ActorId) (types.Score, error) { key := collections.Join(topicId, worker) score, err := k.infererScoreEmas.Get(ctx, key) if err != nil { @@ -2245,25 +2234,22 @@ func (k *Keeper) GetLatestInfererScore(ctx context.Context, topicId TopicId, wor return score, nil } -// If the new score is older than the current score, don't update -func (k *Keeper) SetLatestForecasterScore(ctx context.Context, topicId TopicId, worker ActorId, score types.Score) error { - oldScore, err := k.GetLatestForecasterScore(ctx, topicId, worker) - if err != nil { - return err - } - if oldScore.BlockHeight >= score.BlockHeight { - return nil - } +func (k *Keeper) SetForecasterScoreEma(ctx context.Context, topicId TopicId, worker ActorId, score types.Score) error { key := collections.Join(topicId, worker) return k.forecasterScoreEmas.Set(ctx, key, score) } -func (k *Keeper) GetLatestForecasterScore(ctx context.Context, topicId TopicId, worker ActorId) (types.Score, error) { +func (k *Keeper) GetForecasterScoreEma(ctx context.Context, topicId TopicId, worker ActorId) (types.Score, error) { key := collections.Join(topicId, worker) score, err := k.forecasterScoreEmas.Get(ctx, key) if err != nil { if errors.Is(err, collections.ErrNotFound) { - return types.Score{}, nil + return types.Score{ + BlockHeight: 0, + Address: worker, + TopicId: topicId, + Score: alloraMath.ZeroDec(), + }, nil } return types.Score{}, err } @@ -2271,24 +2257,22 @@ func (k *Keeper) GetLatestForecasterScore(ctx context.Context, topicId TopicId, } // If the new score is older than the current score, don't update -func (k *Keeper) SetLatestReputerScore(ctx context.Context, topicId TopicId, reputer ActorId, score types.Score) error { - oldScore, err := k.GetLatestReputerScore(ctx, topicId, reputer) - if err != nil { - return err - } - if oldScore.BlockHeight >= score.BlockHeight { - return nil - } +func (k *Keeper) SetReputerScoreEma(ctx context.Context, topicId TopicId, reputer ActorId, score types.Score) error { key := collections.Join(topicId, reputer) return k.reputerScoreEmas.Set(ctx, key, score) } -func (k *Keeper) GetLatestReputerScore(ctx context.Context, topicId TopicId, reputer ActorId) (types.Score, error) { +func (k *Keeper) GetReputerScoreEma(ctx context.Context, topicId TopicId, reputer ActorId) (types.Score, error) { key := collections.Join(topicId, reputer) score, err := k.reputerScoreEmas.Get(ctx, key) if err != nil { if errors.Is(err, collections.ErrNotFound) { - return types.Score{}, nil + return types.Score{ + BlockHeight: 0, + Address: reputer, + TopicId: topicId, + Score: alloraMath.ZeroDec(), + }, nil } return types.Score{}, err } diff --git a/x/emissions/keeper/keeper_test.go b/x/emissions/keeper/keeper_test.go index 619dbf1a1..c505ff658 100644 --- a/x/emissions/keeper/keeper_test.go +++ b/x/emissions/keeper/keeper_test.go @@ -2564,7 +2564,7 @@ func (s *KeeperTestSuite) TestGetLatestScores() { reputer := "reputer1" // Test getting latest scores when none are set - infererScore, err := keeper.GetLatestInfererScore(ctx, topicId, worker) + infererScore, err := keeper.GetInfererScoreEma(ctx, topicId, worker) s.Require().NoError(err, "Fetching latest inferer score should not fail") s.Require().Equal(types.Score{ TopicId: topicId, @@ -2573,11 +2573,11 @@ func (s *KeeperTestSuite) TestGetLatestScores() { Score: alloraMath.ZeroDec(), }, infererScore, "Inferer score should be zero if not set") - forecasterScore, err := keeper.GetLatestForecasterScore(ctx, topicId, forecaster) + forecasterScore, err := keeper.GetForecasterScoreEma(ctx, topicId, forecaster) s.Require().NoError(err, "Fetching latest forecaster score should not fail") s.Require().Equal(types.Score{}, forecasterScore, "Forecaster score should be empty if not set") - reputerScore, err := keeper.GetLatestReputerScore(ctx, topicId, reputer) + reputerScore, err := keeper.GetReputerScoreEma(ctx, topicId, reputer) s.Require().NoError(err, "Fetching latest reputer score should not fail") s.Require().Equal(types.Score{}, reputerScore, "Reputer score should be empty if not set") } @@ -2593,20 +2593,20 @@ func (s *KeeperTestSuite) TestSetLatestScores() { newScore := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker, Score: alloraMath.NewDecFromInt64(95)} // Set an initial score for inferer and attempt to update with an older score - _ = keeper.SetLatestInfererScore(ctx, topicId, worker, newScore) - err := keeper.SetLatestInfererScore(ctx, topicId, worker, oldScore) + _ = keeper.SetInfererScoreEma(ctx, topicId, worker, newScore) + err := keeper.SetInfererScoreEma(ctx, topicId, worker, oldScore) s.Require().NoError(err, "Setting an older inferer score should not fail but should not update") - updatedScore, _ := keeper.GetLatestInfererScore(ctx, topicId, worker) + updatedScore, _ := keeper.GetInfererScoreEma(ctx, topicId, worker) s.Require().NotEqual(oldScore.Score, updatedScore.Score, "Older score should not replace newer score") // Set a new score for forecaster - _ = keeper.SetLatestForecasterScore(ctx, topicId, forecaster, newScore) - forecasterScore, _ := keeper.GetLatestForecasterScore(ctx, topicId, forecaster) + _ = keeper.SetForecasterScoreEma(ctx, topicId, forecaster, newScore) + forecasterScore, _ := keeper.GetForecasterScoreEma(ctx, topicId, forecaster) s.Require().Equal(newScore.Score, forecasterScore.Score, "Newer forecaster score should be set") // Set a new score for reputer - _ = keeper.SetLatestReputerScore(ctx, topicId, reputer, newScore) - reputerScore, _ := keeper.GetLatestReputerScore(ctx, topicId, reputer) + _ = keeper.SetReputerScoreEma(ctx, topicId, reputer, newScore) + reputerScore, _ := keeper.GetReputerScoreEma(ctx, topicId, reputer) s.Require().Equal(newScore.Score, reputerScore.Score, "Newer reputer score should be set") } @@ -3577,15 +3577,15 @@ func (s *KeeperTestSuite) TestAppendForecast() { score3 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker3, Score: alloraMath.NewDecFromInt64(99)} score4 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker4, Score: alloraMath.NewDecFromInt64(91)} score5 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker5, Score: alloraMath.NewDecFromInt64(96)} - err := k.SetLatestInfererScore(ctx, topicId, worker1, score1) + err := k.SetInfererScoreEma(ctx, topicId, worker1, score1) s.Require().NoError(err) - err = k.SetLatestInfererScore(ctx, topicId, worker2, score2) + err = k.SetInfererScoreEma(ctx, topicId, worker2, score2) s.Require().NoError(err) - err = k.SetLatestInfererScore(ctx, topicId, worker3, score3) + err = k.SetInfererScoreEma(ctx, topicId, worker3, score3) s.Require().NoError(err) - err = k.SetLatestInfererScore(ctx, topicId, worker4, score4) + err = k.SetInfererScoreEma(ctx, topicId, worker4, score4) s.Require().NoError(err) - err = k.SetLatestInfererScore(ctx, topicId, worker5, score5) + err = k.SetInfererScoreEma(ctx, topicId, worker5, score5) s.Require().NoError(err) allInferences := types.Inferences{ @@ -3640,15 +3640,15 @@ func (s *KeeperTestSuite) TestAppendInference() { score3 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker3, Score: alloraMath.NewDecFromInt64(99)} score4 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker4, Score: alloraMath.NewDecFromInt64(91)} score5 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker5, Score: alloraMath.NewDecFromInt64(96)} - err := k.SetLatestForecasterScore(ctx, topicId, worker1, score1) + err := k.SetForecasterScoreEma(ctx, topicId, worker1, score1) s.Require().NoError(err) - err = k.SetLatestForecasterScore(ctx, topicId, worker2, score2) + err = k.SetForecasterScoreEma(ctx, topicId, worker2, score2) s.Require().NoError(err) - err = k.SetLatestForecasterScore(ctx, topicId, worker3, score3) + err = k.SetForecasterScoreEma(ctx, topicId, worker3, score3) s.Require().NoError(err) - err = k.SetLatestForecasterScore(ctx, topicId, worker4, score4) + err = k.SetForecasterScoreEma(ctx, topicId, worker4, score4) s.Require().NoError(err) - err = k.SetLatestForecasterScore(ctx, topicId, worker5, score5) + err = k.SetForecasterScoreEma(ctx, topicId, worker5, score5) s.Require().NoError(err) allForecasts := types.Forecasts{ @@ -3772,15 +3772,15 @@ func (s *KeeperTestSuite) TestAppendReputerLoss() { score3 := types.Score{TopicId: topicId, BlockHeight: 2, Address: reputer3, Score: alloraMath.NewDecFromInt64(99)} score4 := types.Score{TopicId: topicId, BlockHeight: 2, Address: reputer4, Score: alloraMath.NewDecFromInt64(91)} score5 := types.Score{TopicId: topicId, BlockHeight: 2, Address: reputer5, Score: alloraMath.NewDecFromInt64(96)} - err := k.SetLatestReputerScore(ctx, topicId, reputer1, score1) + err := k.SetReputerScoreEma(ctx, topicId, reputer1, score1) s.Require().NoError(err) - err = k.SetLatestReputerScore(ctx, topicId, reputer2, score2) + err = k.SetReputerScoreEma(ctx, topicId, reputer2, score2) s.Require().NoError(err) - err = k.SetLatestReputerScore(ctx, topicId, reputer3, score3) + err = k.SetReputerScoreEma(ctx, topicId, reputer3, score3) s.Require().NoError(err) - err = k.SetLatestReputerScore(ctx, topicId, reputer4, score4) + err = k.SetReputerScoreEma(ctx, topicId, reputer4, score4) s.Require().NoError(err) - err = k.SetLatestReputerScore(ctx, topicId, reputer5, score5) + err = k.SetReputerScoreEma(ctx, topicId, reputer5, score5) s.Require().NoError(err) allReputerLosses := types.ReputerValueBundles{ diff --git a/x/emissions/keeper/msgserver/msg_server_worker_payload.go b/x/emissions/keeper/msgserver/msg_server_worker_payload.go index 4247d408f..fa6f97fde 100644 --- a/x/emissions/keeper/msgserver/msg_server_worker_payload.go +++ b/x/emissions/keeper/msgserver/msg_server_worker_payload.go @@ -118,7 +118,7 @@ func (ms msgServer) InsertWorkerPayload(ctx context.Context, msg *types.MsgInser // LImit forecast elements for top inferers latestScoresForForecastedInferers := make([]types.Score, 0) for _, el := range forecast.ForecastElements { - score, err := ms.k.GetLatestInfererScore(ctx, forecast.TopicId, el.Inferer) + score, err := ms.k.GetInfererScoreEma(ctx, forecast.TopicId, el.Inferer) if err != nil { continue } diff --git a/x/emissions/keeper/msgserver/msg_server_worker_payload_test.go b/x/emissions/keeper/msgserver/msg_server_worker_payload_test.go index 11f1450a3..9d148a1fc 100644 --- a/x/emissions/keeper/msgserver/msg_server_worker_payload_test.go +++ b/x/emissions/keeper/msgserver/msg_server_worker_payload_test.go @@ -310,10 +310,10 @@ func (s *MsgServerTestSuite) TestMsgInsertWorkerPayloadWithFewTopElementsPerFore score3 := types.Score{TopicId: topicId, BlockHeight: blockHeight, Address: inferer3, Score: alloraMath.NewDecFromInt64(80)} score4 := types.Score{TopicId: topicId, BlockHeight: blockHeight, Address: inferer4, Score: alloraMath.NewDecFromInt64(99)} - _ = s.emissionsKeeper.SetLatestInfererScore(ctx, topicId, inferer1, score1) - _ = s.emissionsKeeper.SetLatestInfererScore(ctx, topicId, inferer2, score2) - _ = s.emissionsKeeper.SetLatestInfererScore(ctx, topicId, inferer3, score3) - _ = s.emissionsKeeper.SetLatestInfererScore(ctx, topicId, inferer4, score4) + _ = s.emissionsKeeper.SetInfererScoreEma(ctx, topicId, inferer1, score1) + _ = s.emissionsKeeper.SetInfererScoreEma(ctx, topicId, inferer2, score2) + _ = s.emissionsKeeper.SetInfererScoreEma(ctx, topicId, inferer3, score3) + _ = s.emissionsKeeper.SetInfererScoreEma(ctx, topicId, inferer4, score4) _, err = msgServer.InsertWorkerPayload(ctx, &workerMsg) require.NoError(err, "InsertWorkerPayload should not return an error") @@ -652,10 +652,10 @@ func (s *MsgServerTestSuite) TestMsgInsertWorkerPayloadWithLowScoreForecastsAreR score3 := types.Score{TopicId: topicId, BlockHeight: blockHeight, Address: inferer3, Score: alloraMath.NewDecFromInt64(80)} score4 := types.Score{TopicId: topicId, BlockHeight: blockHeight, Address: inferer4, Score: alloraMath.NewDecFromInt64(50)} - _ = keeper.SetLatestInfererScore(ctx, topicId, inferer1, score1) - _ = keeper.SetLatestInfererScore(ctx, topicId, inferer2, score2) - _ = keeper.SetLatestInfererScore(ctx, topicId, inferer3, score3) - _ = keeper.SetLatestInfererScore(ctx, topicId, inferer4, score4) + _ = keeper.SetInfererScoreEma(ctx, topicId, inferer1, score1) + _ = keeper.SetInfererScoreEma(ctx, topicId, inferer2, score2) + _ = keeper.SetInfererScoreEma(ctx, topicId, inferer3, score3) + _ = keeper.SetInfererScoreEma(ctx, topicId, inferer4, score4) _, err = msgServer.InsertWorkerPayload(ctx, &workerMsg) require.NoError(err, "InsertWorkerPayload should not return an error even if the forecast elements are below the threshold") diff --git a/x/emissions/keeper/queryserver/query_server_score.go b/x/emissions/keeper/queryserver/query_server_score.go index a22c206da..52ef9cc9f 100644 --- a/x/emissions/keeper/queryserver/query_server_score.go +++ b/x/emissions/keeper/queryserver/query_server_score.go @@ -13,7 +13,7 @@ func (qs queryServer) GetLatestInfererScore( *types.QueryLatestInfererScoreResponse, error, ) { - latestInfererScore, err := qs.k.GetLatestInfererScore(ctx, req.TopicId, req.Inferer) + latestInfererScore, err := qs.k.GetInfererScoreEma(ctx, req.TopicId, req.Inferer) if err != nil { return nil, err } @@ -28,7 +28,7 @@ func (qs queryServer) GetLatestForecasterScore( *types.QueryLatestForecasterScoreResponse, error, ) { - latestForecasterScore, err := qs.k.GetLatestForecasterScore(ctx, req.TopicId, req.Forecaster) + latestForecasterScore, err := qs.k.GetForecasterScoreEma(ctx, req.TopicId, req.Forecaster) if err != nil { return nil, err } @@ -43,7 +43,7 @@ func (qs queryServer) GetLatestReputerScore( *types.QueryLatestReputerScoreResponse, error, ) { - latestReputerScore, err := qs.k.GetLatestReputerScore(ctx, req.TopicId, req.Reputer) + latestReputerScore, err := qs.k.GetReputerScoreEma(ctx, req.TopicId, req.Reputer) if err != nil { return nil, err } diff --git a/x/emissions/keeper/queryserver/query_server_score_test.go b/x/emissions/keeper/queryserver/query_server_score_test.go index ab68fb197..89c45ba47 100644 --- a/x/emissions/keeper/queryserver/query_server_score_test.go +++ b/x/emissions/keeper/queryserver/query_server_score_test.go @@ -17,8 +17,8 @@ func (s *QueryServerTestSuite) TestGetLatestInfererScore() { newScore := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker, Score: alloraMath.NewDecFromInt64(95)} // Set an initial score for inferer and attempt to update with an older score - _ = keeper.SetLatestInfererScore(ctx, topicId, worker, newScore) - err := keeper.SetLatestInfererScore(ctx, topicId, worker, oldScore) + _ = keeper.SetInfererScoreEma(ctx, topicId, worker, newScore) + err := keeper.SetInfererScoreEma(ctx, topicId, worker, oldScore) s.Require().NoError(err, "Setting an older inferer score should not fail but should not update") req := &types.QueryLatestInfererScoreRequest{ @@ -41,7 +41,7 @@ func (s *QueryServerTestSuite) TestGetLatestForecasterScore() { newScore := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker, Score: alloraMath.NewDecFromInt64(95)} // Set a new score for forecaster - _ = keeper.SetLatestForecasterScore(ctx, topicId, forecaster, newScore) + _ = keeper.SetForecasterScoreEma(ctx, topicId, forecaster, newScore) req := &types.QueryLatestForecasterScoreRequest{ TopicId: topicId, @@ -63,7 +63,7 @@ func (s *QueryServerTestSuite) TestGetLatestReputerScore() { newScore := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker, Score: alloraMath.NewDecFromInt64(95)} // Set a new score for reputer - _ = keeper.SetLatestReputerScore(ctx, topicId, reputer, newScore) + _ = keeper.SetReputerScoreEma(ctx, topicId, reputer, newScore) req := &types.QueryLatestReputerScoreRequest{ TopicId: topicId, diff --git a/x/emissions/keeper/score_utils.go b/x/emissions/keeper/score_utils.go index 90699d9f8..bfbde707f 100644 --- a/x/emissions/keeper/score_utils.go +++ b/x/emissions/keeper/score_utils.go @@ -14,12 +14,12 @@ func GetLowScoreFromAllLossBundles( lossBundles types.ReputerValueBundles, ) (types.Score, int, error) { lowScoreIndex := 0 - lowScore, err := k.GetLatestReputerScore(ctx, topicId, lossBundles.ReputerValueBundles[0].ValueBundle.Reputer) + lowScore, err := k.GetReputerScoreEma(ctx, topicId, lossBundles.ReputerValueBundles[0].ValueBundle.Reputer) if err != nil { return types.Score{}, lowScoreIndex, err } for index, extLossBundle := range lossBundles.ReputerValueBundles { - extScore, err := k.GetLatestReputerScore(ctx, topicId, extLossBundle.ValueBundle.Reputer) + extScore, err := k.GetReputerScoreEma(ctx, topicId, extLossBundle.ValueBundle.Reputer) if err != nil { continue } @@ -39,12 +39,12 @@ func GetLowScoreFromAllInferences( inferences types.Inferences, ) (types.Score, int, error) { lowScoreIndex := 0 - lowScore, err := k.GetLatestInfererScore(ctx, topicId, inferences.Inferences[0].Inferer) + lowScore, err := k.GetInfererScoreEma(ctx, topicId, inferences.Inferences[0].Inferer) if err != nil { return types.Score{}, lowScoreIndex, err } for index, extInference := range inferences.Inferences { - extScore, err := k.GetLatestInfererScore(ctx, topicId, extInference.Inferer) + extScore, err := k.GetInfererScoreEma(ctx, topicId, extInference.Inferer) if err != nil { continue } @@ -64,12 +64,12 @@ func GetLowScoreFromAllForecasts( forecasts types.Forecasts, ) (types.Score, int, error) { lowScoreIndex := 0 - lowScore, err := k.GetLatestForecasterScore(ctx, topicId, forecasts.Forecasts[0].Forecaster) + lowScore, err := k.GetForecasterScoreEma(ctx, topicId, forecasts.Forecasts[0].Forecaster) if err != nil { return types.Score{}, lowScoreIndex, err } for index, extForecast := range forecasts.Forecasts { - extScore, err := k.GetLatestForecasterScore(ctx, topicId, extForecast.Forecaster) + extScore, err := k.GetForecasterScoreEma(ctx, topicId, extForecast.Forecaster) if err != nil { continue } diff --git a/x/emissions/keeper/score_utils_test.go b/x/emissions/keeper/score_utils_test.go index 2167f87be..6901efc04 100644 --- a/x/emissions/keeper/score_utils_test.go +++ b/x/emissions/keeper/score_utils_test.go @@ -19,9 +19,9 @@ func (s *KeeperTestSuite) TestGetLowScoreFromAllInferences() { score1 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker1, Score: alloraMath.NewDecFromInt64(95)} score2 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker2, Score: alloraMath.NewDecFromInt64(90)} score3 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker3, Score: alloraMath.NewDecFromInt64(99)} - _ = k.SetLatestInfererScore(ctx, topicId, worker1, score1) - _ = k.SetLatestInfererScore(ctx, topicId, worker2, score2) - _ = k.SetLatestInfererScore(ctx, topicId, worker3, score3) + _ = k.SetInfererScoreEma(ctx, topicId, worker1, score1) + _ = k.SetInfererScoreEma(ctx, topicId, worker2, score2) + _ = k.SetInfererScoreEma(ctx, topicId, worker3, score3) allInferences := types.Inferences{ Inferences: []*types.Inference{ @@ -49,9 +49,9 @@ func (s *KeeperTestSuite) TestGetLowScoreFromAllForecasts() { score1 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker1, Score: alloraMath.NewDecFromInt64(95)} score2 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker2, Score: alloraMath.NewDecFromInt64(90)} score3 := types.Score{TopicId: topicId, BlockHeight: 2, Address: worker3, Score: alloraMath.NewDecFromInt64(99)} - _ = k.SetLatestForecasterScore(ctx, topicId, worker1, score1) - _ = k.SetLatestForecasterScore(ctx, topicId, worker2, score2) - _ = k.SetLatestForecasterScore(ctx, topicId, worker3, score3) + _ = k.SetForecasterScoreEma(ctx, topicId, worker1, score1) + _ = k.SetForecasterScoreEma(ctx, topicId, worker2, score2) + _ = k.SetForecasterScoreEma(ctx, topicId, worker3, score3) allForecasts := types.Forecasts{ Forecasts: []*types.Forecast{ @@ -124,9 +124,9 @@ func (s *KeeperTestSuite) TestGetLowScoreFromAllLossBundles() { score1 := types.Score{TopicId: topicId, BlockHeight: 2, Address: reputer1, Score: alloraMath.NewDecFromInt64(95)} score2 := types.Score{TopicId: topicId, BlockHeight: 2, Address: reputer2, Score: alloraMath.NewDecFromInt64(90)} score3 := types.Score{TopicId: topicId, BlockHeight: 2, Address: reputer3, Score: alloraMath.NewDecFromInt64(99)} - _ = k.SetLatestReputerScore(ctx, topicId, reputer1, score1) - _ = k.SetLatestReputerScore(ctx, topicId, reputer2, score2) - _ = k.SetLatestReputerScore(ctx, topicId, reputer3, score3) + _ = k.SetReputerScoreEma(ctx, topicId, reputer1, score1) + _ = k.SetReputerScoreEma(ctx, topicId, reputer2, score2) + _ = k.SetReputerScoreEma(ctx, topicId, reputer3, score3) allReputerLosses := types.ReputerValueBundles{ ReputerValueBundles: []*types.ReputerValueBundle{ diff --git a/x/emissions/module/rewards/scores.go b/x/emissions/module/rewards/scores.go index 7a1b9d4b5..9f43b5af8 100644 --- a/x/emissions/module/rewards/scores.go +++ b/x/emissions/module/rewards/scores.go @@ -27,6 +27,10 @@ func GenerateReputerScores( // This is necessary to ensure that all workers are accounted for in the final scores // If a worker is missing from the reported losses, it will be added with a NaN value reportedLosses = ensureWorkerPresence(reportedLosses) + topic, err := keeper.GetTopic(ctx, topicId) + if err != nil { + return []types.Score{}, errors.Wrapf(err, "Error getting topic") + } // Fetch reputers data var reputers []string @@ -100,11 +104,22 @@ func GenerateReputerScores( Address: reputer, Score: scores[i], } + previousScore, err := keeper.GetReputerScoreEma(ctx, topicId, reputer) + firstTime := previousScore.BlockHeight == 0 && previousScore.Score.IsZero() + newScore.Score, err = alloraMath.CalcEma( + topic.MeritSortitionAlpha, + newScore.Score, + previousScore.Score, + firstTime, + ) + if err != nil { + return []types.Score{}, errors.Wrapf(err, "Error calculating ema") + } err = keeper.InsertReputerScore(ctx, topicId, block, newScore) if err != nil { return []types.Score{}, errors.Wrapf(err, "Error inserting reputer score") } - err = keeper.SetLatestReputerScore(ctx, topicId, reputer, newScore) + err = keeper.SetReputerScoreEma(ctx, topicId, reputer, newScore) if err != nil { return []types.Score{}, errors.Wrapf(err, "Error setting latest reputer score") } @@ -141,6 +156,10 @@ func GenerateInferenceScores( newScores = append(newScores, newScore) return newScores, nil } + topic, err := keeper.GetTopic(ctx, topicId) + if err != nil { + return []types.Score{}, errors.Wrapf(err, "Error getting topic") + } for _, oneOutLoss := range networkLosses.OneOutInfererValues { workerNewScore, err := oneOutLoss.Value.Sub(networkLosses.CombinedValue) @@ -154,11 +173,22 @@ func GenerateInferenceScores( Address: oneOutLoss.Worker, Score: workerNewScore, } + previousScore, err := keeper.GetInfererScoreEma(ctx, topicId, oneOutLoss.Worker) + firstTime := previousScore.BlockHeight == 0 && previousScore.Score.IsZero() + newScore.Score, err = alloraMath.CalcEma( + topic.MeritSortitionAlpha, + newScore.Score, + previousScore.Score, + firstTime, + ) + if err != nil { + return []types.Score{}, errors.Wrapf(err, "Error calculating ema") + } err = keeper.InsertWorkerInferenceScore(ctx, topicId, block, newScore) if err != nil { return []types.Score{}, errors.Wrapf(err, "Error inserting worker inference score") } - err = keeper.SetLatestInfererScore(ctx, topicId, oneOutLoss.Worker, newScore) + err = keeper.SetInfererScoreEma(ctx, topicId, oneOutLoss.Worker, newScore) if err != nil { return []types.Score{}, errors.Wrapf(err, "error setting latest inferer score") } @@ -178,6 +208,10 @@ func GenerateForecastScores( networkLosses types.ValueBundle, ) ([]types.Score, error) { var newScores []types.Score + topic, err := keeper.GetTopic(ctx, topicId) + if err != nil { + return []types.Score{}, errors.Wrapf(err, "Error getting topic") + } // If there is only one forecaster, set score to 0 // More than one forecaster is required to have one-out losses @@ -232,11 +266,22 @@ func GenerateForecastScores( Address: oneInNaiveLoss.Worker, Score: workerFinalScore, } + previousScore, err := keeper.GetForecasterScoreEma(ctx, topicId, oneInNaiveLoss.Worker) + firstTime := previousScore.BlockHeight == 0 && previousScore.Score.IsZero() + newScore.Score, err = alloraMath.CalcEma( + topic.MeritSortitionAlpha, + newScore.Score, + previousScore.Score, + firstTime, + ) + if err != nil { + return []types.Score{}, errors.Wrapf(err, "Error calculating ema") + } err = keeper.InsertWorkerForecastScore(ctx, topicId, block, newScore) if err != nil { return []types.Score{}, errors.Wrapf(err, "Error inserting worker forecast score") } - err = keeper.SetLatestForecasterScore(ctx, topicId, oneInNaiveLoss.Worker, newScore) + err = keeper.SetForecasterScoreEma(ctx, topicId, oneInNaiveLoss.Worker, newScore) if err != nil { return []types.Score{}, errors.Wrapf(err, "Error setting latest forecaster score") } diff --git a/x/emissions/proto/emissions/v3/genesis.proto b/x/emissions/proto/emissions/v3/genesis.proto index 1f1f38db1..bccc5012a 100644 --- a/x/emissions/proto/emissions/v3/genesis.proto +++ b/x/emissions/proto/emissions/v3/genesis.proto @@ -18,7 +18,7 @@ option go_package = "github.com/allora-network/allora-chain/x/emissions/types"; // GenesisState is the state that must be provided at genesis. message GenesisState { - reserved 13,14,15; + reserved 13, 14, 15; reserved "latestInfererScoresByWorker", "latestForecasterScoresByWorker", "latestReputerScoresByReputer"; // params defines all the parameters of the module.