Skip to content

Commit

Permalink
feat: reuse old penalty in no active set
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Dec 18, 2024
1 parent 4c37a44 commit 31f6976
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
83 changes: 45 additions & 38 deletions x/emissions/keeper/actor_penalties.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// UpdateTopicPenalties updates the penalties for workers based on the current EMA scores of the current active set.
func (k *Keeper) UpdateTopicPenalties(ctx sdk.Context, topicId TopicId) error {
// UpdateTopicWorkerPenalties updates the penalties for workers based on the current EMA scores of the current active set.
func (k *Keeper) UpdateTopicWorkerPenalties(ctx sdk.Context, topicId TopicId) error {
params, err := k.GetParams(ctx)
if err != nil {
return errorsmod.Wrap(err, "error updating worker penalties")
Expand All @@ -32,20 +32,6 @@ func (k *Keeper) UpdateTopicPenalties(ctx sdk.Context, topicId TopicId) error {

// updateTopicInfererPenalties updates the penalty for the given topic based on the current EMA scores of the current active set.
func (k *Keeper) updateTopicInfererPenalties(ctx sdk.Context, topicId TopicId, penaltyLambda alloraMath.Dec) error {
lowestEMAScore, exists, err := k.GetLowestInfererScoreEma(ctx, topicId)
if err != nil {
return errorsmod.Wrap(err, "error getting lowest inferer score EMA")
}

// Empty active set so no penalty
if !exists {
err = k.infererPenaltyInTopic.Remove(ctx, topicId)
if err != nil {
return errorsmod.Wrap(err, "error removing penalty for topic")
}
return nil
}

// Get the EMA score from the active set
scores, err := getTopicActiveActorsEMAScores(
ctx,
Expand All @@ -59,31 +45,21 @@ func (k *Keeper) updateTopicInfererPenalties(ctx sdk.Context, topicId TopicId, p
return err
}

penalty, err := computePenalty(lowestEMAScore.Score, scores, penaltyLambda)
if err != nil {
return err
}

return k.infererPenaltyInTopic.Set(ctx, topicId, penalty)
return updateTopicWorkerPenalties(
func(topicId TopicId) (types.Score, bool, error) {
return k.GetLowestInfererScoreEma(ctx, topicId)
},
func(topicId TopicId, penalty alloraMath.Dec) error {
return k.infererPenaltyInTopic.Set(ctx, topicId, penalty)
},
topicId,
scores,
penaltyLambda,
)
}

// updateTopicForecasterPenalties updates the penalty for the given topic based on the current EMA scores of the current active set.
func (k *Keeper) updateTopicForecasterPenalties(ctx sdk.Context, topicId TopicId, penaltyLambda alloraMath.Dec) error {
lowestEMAScore, exists, err := k.GetLowestForecasterScoreEma(ctx, topicId)
if err != nil {
return errorsmod.Wrap(err, "error getting lowest forecaster score EMA")
}

// Empty active set so no penalty
if !exists {
err = k.forecasterPenaltyInTopic.Remove(ctx, topicId)
if err != nil {
return errorsmod.Wrap(err, "error removing penalty for topic")
}
return nil
}

// Get the EMA score from the active set
scores, err := getTopicActiveActorsEMAScores(
ctx,
func(topicId TopicId, actorId ActorId) (types.Score, error) {
Expand All @@ -96,12 +72,43 @@ func (k *Keeper) updateTopicForecasterPenalties(ctx sdk.Context, topicId TopicId
return err
}

return updateTopicWorkerPenalties(
func(topicId TopicId) (types.Score, bool, error) {
return k.GetLowestForecasterScoreEma(ctx, topicId)
},
func(topicId TopicId, penalty alloraMath.Dec) error {
return k.forecasterPenaltyInTopic.Set(ctx, topicId, penalty)
},
topicId,
scores,
penaltyLambda,
)
}

// updateTopicWorkerPenalties updates the penalty for the given topic based on the current EMA scores of the current active set.
func updateTopicWorkerPenalties(
getLowestEMAFn func(topicId TopicId) (types.Score, bool, error),
setPenaltyFn func(topicId TopicId, penalty alloraMath.Dec) error,
topicId TopicId,
scores []alloraMath.Dec,
penaltyLambda alloraMath.Dec,
) error {
lowestEMAScore, exists, err := getLowestEMAFn(topicId)
if err != nil {
return errorsmod.Wrap(err, "error getting lowest EMA score")
}

// Empty active set so no penalty update
if !exists {
return nil
}

penalty, err := computePenalty(lowestEMAScore.Score, scores, penaltyLambda)
if err != nil {
return err
}

return k.forecasterPenaltyInTopic.Set(ctx, topicId, penalty)
return setPenaltyFn(topicId, penalty)
}

func getTopicActiveActorsEMAScores(
Expand Down
2 changes: 1 addition & 1 deletion x/emissions/keeper/actor_utils/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func CloseWorkerNonce(k *keeper.Keeper, ctx sdk.Context, topic types.Topic, nonc
return err
}

err = k.UpdateTopicPenalties(ctx, topic.Id)
err = k.UpdateTopicWorkerPenalties(ctx, topic.Id)
if err != nil {
return err
}
Expand Down

0 comments on commit 31f6976

Please sign in to comment.