Skip to content

Commit

Permalink
calculate score emas and replace latestScore with them
Browse files Browse the repository at this point in the history
  • Loading branch information
relyt29 committed Aug 29, 2024
1 parent 2da1f0e commit 7b26fbe
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 98 deletions.
60 changes: 22 additions & 38 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -2245,50 +2234,45 @@ 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
}
return score, nil
}

// 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
}
Expand Down
50 changes: 25 additions & 25 deletions x/emissions/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
}
Expand All @@ -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")
}

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion x/emissions/keeper/msgserver/msg_server_worker_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions x/emissions/keeper/msgserver/msg_server_worker_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions x/emissions/keeper/queryserver/query_server_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions x/emissions/keeper/queryserver/query_server_score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions x/emissions/keeper/score_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 7b26fbe

Please sign in to comment.