Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix number of inferers considered in the reward cycle #691

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#651](https://github.com/allora-network/allora-chain/pull/651) Refactor: Fuzzer rename invariants test to fuzz test
* [#653](https://github.com/allora-network/allora-chain/pull/653) Fuzzer Bugfixes, Allow User to Set Fuzzer Transition Probability Distribution
* [#686](https://github.com/allora-network/allora-chain/pull/686) CLI query commands alignment
* [#691](https://github.com/allora-network/allora-chain/pull/691/files) Fix number of inferers considered in the reward cycle
* [#693](https://github.com/allora-network/allora-chain/pull/693) Migration test updates

### Deprecated
Expand Down
153 changes: 92 additions & 61 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,19 +1112,7 @@ func (k *Keeper) AppendInference(
return errors.New("inference already submitted")
}

workerAddresses, err := k.GetActiveInferersForTopic(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting active inferers for topic")
}
// If there are less than maxTopInferersToReward, add the current inferer
if uint64(len(workerAddresses)) < maxTopInferersToReward {
err := k.AddActiveInferer(ctx, topic.Id, inference.Inferer)
if err != nil {
return errorsmod.Wrap(err, "error adding active inferer")
}
return k.InsertInference(ctx, topic.Id, *inference)
}

// Get previous EMA score for the current inferer
previousEmaScore, err := k.GetInfererScoreEma(ctx, topic.Id, inference.Inferer)
if err != nil {
return errorsmod.Wrapf(err, "Error getting inferer score ema")
Expand All @@ -1134,21 +1122,37 @@ func (k *Keeper) AppendInference(
return types.ErrCantUpdateEmaMoreThanOncePerWindow
}

lowestEmaScore, found, err := k.GetLowestInfererScoreEma(ctx, topic.Id)
// Get lowest inferer score ema for the topic
lowestEmaScore, _, err := k.GetLowestInfererScoreEma(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting lowest inferer score ema")
// If there are no lowest inferer score ema, calculate it
} else if !found {
lowestEmaScore, err = GetLowestScoreFromAllInferers(ctx, k, topic.Id, workerAddresses)
if err != nil {
return errorsmod.Wrap(err, "error getting lowest score from all inferers")
}

// Get active inferers for topic
workerAddresses, err := k.GetActiveInferersForTopic(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting active inferers for topic")
}

// If there are less than maxTopInferersToReward, add the current inferer, update the lowest inferer score ema if needed, and return
if uint64(len(workerAddresses)) < maxTopInferersToReward {
// Update lowest inferer score ema if needed
if uint64(len(workerAddresses)) == 0 || lowestEmaScore.Score.Gt(previousEmaScore.Score) {
err = k.SetLowestInfererScoreEma(ctx, topic.Id, previousEmaScore)
if err != nil {
return errorsmod.Wrap(err, "error setting lowest inferer score ema")
}
}
err = k.SetLowestInfererScoreEma(ctx, topic.Id, lowestEmaScore)

err = k.AddActiveInferer(ctx, topic.Id, inference.Inferer)
if err != nil {
return errorsmod.Wrap(err, "error setting lowest inferer score ema")
return errorsmod.Wrap(err, "error adding active inferer")
}
return k.InsertInference(ctx, topic.Id, *inference)
}

// Else ...
// Checks if the inferer's previous EMA score is greater than the lowest EMA score
if previousEmaScore.Score.Gt(lowestEmaScore.Score) {
// Update EMA score for the lowest score inferer, who is not the current inferer
err = k.CalcAndSaveInfererScoreEmaWithLastSavedTopicQuantile(
Expand All @@ -1160,6 +1164,16 @@ func (k *Keeper) AppendInference(
if err != nil {
return errorsmod.Wrap(err, "error calculating and saving inferer score ema with last saved topic quantile")
}

// Check if the inferer with lowest score is active before removing it, because remove will not fail if the inferer is not active
isActive, err := k.IsActiveInferer(ctx, topic.Id, lowestEmaScore.Address)
if err != nil {
return errorsmod.Wrap(err, "error checking if inferer is active")
}
if !isActive {
return errors.New("inferer with lowest score is not active")
}

kpeluso marked this conversation as resolved.
Show resolved Hide resolved
// Remove inferer with lowest score
err = k.RemoveActiveInferer(ctx, topic.Id, lowestEmaScore.Address)
if err != nil {
Expand Down Expand Up @@ -1245,19 +1259,6 @@ func (k *Keeper) AppendForecast(
return errors.New("forecast already submitted")
}

forecasterAddresses, err := k.GetActiveForecastersForTopic(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting active forecasters for topic")
}
// If there are less than maxTopForecastersToReward, add the current forecaster
if uint64(len(forecasterAddresses)) < maxTopForecastersToReward {
err := k.AddActiveForecaster(ctx, topic.Id, forecast.Forecaster)
if err != nil {
return errorsmod.Wrap(err, "error adding active forecaster")
}
return k.InsertForecast(ctx, topic.Id, *forecast)
}

previousEmaScore, err := k.GetForecasterScoreEma(ctx, topic.Id, forecast.Forecaster)
if err != nil {
return errorsmod.Wrapf(err, "Error getting forecaster score ema")
Expand All @@ -1267,18 +1268,30 @@ func (k *Keeper) AppendForecast(
return types.ErrCantUpdateEmaMoreThanOncePerWindow
}

lowestEmaScore, found, err := k.GetLowestForecasterScoreEma(ctx, topic.Id)
lowestEmaScore, _, err := k.GetLowestForecasterScoreEma(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting lowest forecaster score ema")
} else if !found {
lowestEmaScore, err = GetLowestScoreFromAllForecasters(ctx, k, topic.Id, forecasterAddresses)
if err != nil {
return errorsmod.Wrap(err, "error getting lowest score from all forecasters")
}

forecasterAddresses, err := k.GetActiveForecastersForTopic(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting active forecasters for topic")
}

// If there are less than maxTopForecastersToReward, add the current forecaster
if uint64(len(forecasterAddresses)) < maxTopForecastersToReward {
if uint64(len(forecasterAddresses)) == 0 || lowestEmaScore.Score.Gt(previousEmaScore.Score) {
err = k.SetLowestForecasterScoreEma(ctx, topic.Id, previousEmaScore)
if err != nil {
return errorsmod.Wrap(err, "error setting lowest forecaster score ema")
}
}
err = k.SetLowestForecasterScoreEma(ctx, topic.Id, lowestEmaScore)

err = k.AddActiveForecaster(ctx, topic.Id, forecast.Forecaster)
if err != nil {
return errorsmod.Wrap(err, "error setting lowest forecaster score ema")
return errorsmod.Wrap(err, "error adding active forecaster")
}
return k.InsertForecast(ctx, topic.Id, *forecast)
}

if previousEmaScore.Score.Gt(lowestEmaScore.Score) {
Expand All @@ -1292,6 +1305,16 @@ func (k *Keeper) AppendForecast(
if err != nil {
return errorsmod.Wrap(err, "error calculating and saving forecaster score ema with last saved topic quantile")
}

// Check if the forecaster with lowest score is active before removing it, because remove will not fail if the forecaster is not active
isActive, err := k.IsActiveForecaster(ctx, topic.Id, lowestEmaScore.Address)
if err != nil {
return errorsmod.Wrap(err, "error checking if forecaster is active")
}
if !isActive {
return errors.New("forecaster with lowest score is not active")
}

// Remove forecaster with lowest score
err = k.RemoveActiveForecaster(ctx, topic.Id, lowestEmaScore.Address)
if err != nil {
Expand Down Expand Up @@ -1431,19 +1454,6 @@ func (k *Keeper) AppendReputerLoss(
return errors.New("reputer loss already submitted")
}

reputerAddresses, err := k.GetActiveReputersForTopic(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting active reputers for topic")
}
// If there are less than maxTopReputersToReward, add the current reputer
if uint64(len(reputerAddresses)) < moduleParams.MaxTopReputersToReward {
err := k.AddActiveReputer(ctx, topic.Id, reputerLoss.ValueBundle.Reputer)
if err != nil {
return errorsmod.Wrap(err, "error adding active reputer")
}
return k.InsertReputerLoss(ctx, topic.Id, *reputerLoss)
}

previousEmaScore, err := k.GetReputerScoreEma(ctx, topic.Id, reputerLoss.ValueBundle.Reputer)
if err != nil {
return errorsmod.Wrapf(err, "Error getting reputer score ema")
Expand All @@ -1453,18 +1463,29 @@ func (k *Keeper) AppendReputerLoss(
return types.ErrCantUpdateEmaMoreThanOncePerWindow
}

lowestEmaScore, found, err := k.GetLowestReputerScoreEma(ctx, topic.Id)
lowestEmaScore, _, err := k.GetLowestReputerScoreEma(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting lowest reputer score ema")
} else if !found {
lowestEmaScore, err = GetLowestScoreFromAllReputers(ctx, k, topic.Id, reputerAddresses)
if err != nil {
return errorsmod.Wrap(err, "error getting lowest score from all reputers")
}

reputerAddresses, err := k.GetActiveReputersForTopic(ctx, topic.Id)
if err != nil {
return errorsmod.Wrap(err, "error getting active reputers for topic")
}
// If there are less than maxTopReputersToReward, add the current reputer
if uint64(len(reputerAddresses)) < moduleParams.MaxTopReputersToReward {
if uint64(len(reputerAddresses)) == 0 || lowestEmaScore.Score.Gt(previousEmaScore.Score) {
err = k.SetLowestReputerScoreEma(ctx, topic.Id, previousEmaScore)
if err != nil {
return errorsmod.Wrap(err, "error setting lowest reputer score ema")
}
}
err = k.SetLowestReputerScoreEma(ctx, topic.Id, lowestEmaScore)

err = k.AddActiveReputer(ctx, topic.Id, reputerLoss.ValueBundle.Reputer)
if err != nil {
return errorsmod.Wrap(err, "error setting lowest reputer score ema")
return errorsmod.Wrap(err, "error adding active reputer")
}
return k.InsertReputerLoss(ctx, topic.Id, *reputerLoss)
}

if previousEmaScore.Score.Gt(lowestEmaScore.Score) {
Expand All @@ -1478,6 +1499,16 @@ func (k *Keeper) AppendReputerLoss(
if err != nil {
return errorsmod.Wrap(err, "error calculating and saving reputer score ema with last saved topic quantile")
}

// Check if the reputer with lowest score is active before removing it, because remove will not fail if the reputer is not active
isActive, err := k.IsActiveReputer(ctx, topic.Id, lowestEmaScore.Address)
if err != nil {
return errorsmod.Wrap(err, "error checking if reputer is active")
}
if !isActive {
return errors.New("reputer with lowest score is not active")
}

// Remove reputer with lowest score
err = k.RemoveActiveReputer(ctx, topic.Id, lowestEmaScore.Address)
if err != nil {
Expand Down
Loading
Loading