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

Adjust score normalization to consider more scores samples #702

Merged
merged 4 commits into from
Dec 15, 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
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Security

## [Unreleased]
# [Unreleased]

## v0.X.X
## v0.8.0

### Added

Expand All @@ -69,12 +69,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [#702](https://github.com/allora-network/allora-chain/pull/702) Adjust score normalization to consider more scores samples
* [#699](https://github.com/allora-network/allora-chain/pull/699) Make install script manage new release assets naming
* [#704](https://github.com/allora-network/allora-chain/pull/704) Correct Discrepancies with Whitepaper modified reward fractions vs plain EMA

### Security

## [Released]
# [Released]

## v0.7.0

Expand Down
22 changes: 12 additions & 10 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3106,7 +3106,8 @@ func (k *Keeper) InsertWorkerInferenceScore(ctx context.Context, topicId TopicId
if err != nil {
return errorsmod.Wrapf(err, "Error getting params")
}
maxNumScores := moduleParams.MaxSamplesToScaleScores

maxNumScores := moduleParams.MaxSamplesToScaleScores * moduleParams.MaxTopInferersToReward

lenScores := uint64(len(scores.Scores))
if lenScores > maxNumScores {
Expand Down Expand Up @@ -3137,24 +3138,24 @@ func (k *Keeper) GetInferenceScoresUntilBlock(ctx context.Context, topicId Topic
if err != nil {
return nil, errorsmod.Wrap(err, "error getting params")
}
maxNumTimeSteps := moduleParams.MaxSamplesToScaleScores

scores := make([]*types.Score, 0, maxNumTimeSteps)
maxNumScores := moduleParams.MaxSamplesToScaleScores * moduleParams.MaxTopInferersToReward

scores := make([]*types.Score, 0, maxNumScores)
for iter.Valid() {
existingScores, err := iter.KeyValue()
if err != nil {
return nil, errorsmod.Wrap(err, "error getting key value")
}

for _, score := range existingScores.Value.Scores {
if uint64(len(scores)) < maxNumTimeSteps {
if uint64(len(scores)) < maxNumScores {
scores = append(scores, score)
} else {
break
}
}
if uint64(len(scores)) >= maxNumTimeSteps {
if uint64(len(scores)) >= maxNumScores {
break
}
iter.Next()
Expand Down Expand Up @@ -3194,7 +3195,8 @@ func (k *Keeper) InsertWorkerForecastScore(ctx context.Context, topicId TopicId,
if err != nil {
return errorsmod.Wrap(err, "error getting params")
}
maxNumScores := moduleParams.MaxSamplesToScaleScores

maxNumScores := moduleParams.MaxSamplesToScaleScores * moduleParams.MaxTopForecastersToReward

lenScores := uint64(len(scores.Scores))
if lenScores > maxNumScores {
Expand Down Expand Up @@ -3225,24 +3227,24 @@ func (k *Keeper) GetForecastScoresUntilBlock(ctx context.Context, topicId TopicI
if err != nil {
return nil, errorsmod.Wrap(err, "error getting params")
}
maxNumTimeSteps := moduleParams.MaxSamplesToScaleScores

scores := make([]*types.Score, 0, maxNumTimeSteps)
maxNumScores := moduleParams.MaxSamplesToScaleScores * moduleParams.MaxTopForecastersToReward

scores := make([]*types.Score, 0, maxNumScores)
for iter.Valid() {
existingScores, err := iter.KeyValue()
if err != nil {
return nil, errorsmod.Wrap(err, "error getting key value")
}

for _, score := range existingScores.Value.Scores {
if uint64(len(scores)) < maxNumTimeSteps {
if uint64(len(scores)) < maxNumScores {
scores = append(scores, score)
} else {
break
}
}
if uint64(len(scores)) >= maxNumTimeSteps {
if uint64(len(scores)) >= maxNumScores {
break
}
iter.Next()
Expand Down
36 changes: 36 additions & 0 deletions x/emissions/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2642,6 +2642,7 @@ func (s *KeeperTestSuite) TestInsertWorkerInferenceScore() {
maxNumScores := 5
params := types.DefaultParams()
params.MaxSamplesToScaleScores = uint64(maxNumScores)
params.MaxTopInferersToReward = 1
err := keeper.SetParams(ctx, params)
s.Require().NoError(err, "Setting parameters should not fail")

Expand All @@ -2667,6 +2668,7 @@ func (s *KeeperTestSuite) TestInsertWorkerInferenceScore2() {
maxNumScores := 5
params := types.DefaultParams()
params.MaxSamplesToScaleScores = uint64(maxNumScores)
params.MaxTopInferersToReward = 1
err := keeper.SetParams(ctx, params)
s.Require().NoError(err, "Setting parameters should not fail")

Expand Down Expand Up @@ -2740,6 +2742,7 @@ func (s *KeeperTestSuite) TestInsertWorkerForecastScore() {
maxNumScores := 5
params := types.DefaultParams()
params.MaxSamplesToScaleScores = uint64(maxNumScores)
params.MaxTopForecastersToReward = 1
err := keeper.SetParams(ctx, params)
s.Require().NoError(err, "Setting parameters should not fail")

Expand Down Expand Up @@ -4946,3 +4949,36 @@ func (s *KeeperTestSuite) TestGetCountForecasterInclusionsInTopic() {
require.NoError(err)
require.Equal(uint64(1), count)
}

func (s *KeeperTestSuite) TestScoreLimiting() {
k := s.emissionsKeeper
ctx := s.ctx
topicId := s.CreateOneTopic(10800)
blockHeight := int64(10)

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

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

scores, err := k.GetWorkerInferenceScoresAtBlock(ctx, topicId, blockHeight)
s.Require().NoError(err)
s.Require().Len(scores.Scores, 6, "Should keep MaxSamplesToScaleScores * MaxTopInferersToReward scores")

for i, score := range scores.Scores {
expectedWorker := s.addrsStr[i+2]
s.Require().Equal(expectedWorker, score.Address)
}
}
1 change: 1 addition & 0 deletions x/emissions/keeper/queryserver/query_server_score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (s *QueryServerTestSuite) TestGetWorkerInferenceScoresAtBlock() {
maxNumScores := 5
params := types.DefaultParams()
params.MaxSamplesToScaleScores = uint64(maxNumScores)
params.MaxTopInferersToReward = 1
err := keeper.SetParams(ctx, params)
s.Require().NoError(err, "Setting parameters should not fail")

Expand Down
9 changes: 9 additions & 0 deletions x/emissions/module/rewards/worker_rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ func (s *RewardsTestSuite) TestGetWorkersRewardFractionsFromCsv() {
forecaster2 := s.addrsStr[7]
forecasterAddresses := []string{forecaster0, forecaster1, forecaster2}

// CSV values were generated considering just max scores = 10
// The actual implementation considers max scores = 10 * max actors to reward
params, err := s.emissionsKeeper.GetParams(s.ctx)
s.Require().NoError(err)
params.MaxTopInferersToReward = 1
params.MaxTopForecastersToReward = 1
err = s.emissionsKeeper.SetParams(s.ctx, params)
s.Require().NoError(err)

// Add scores from previous epochs
infererLastScores := make([]types.Score, 0)
forecasterLastScores := make([]types.Score, 0)
Expand Down
Loading