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

Diego/ora-1452 ORA-1455 stress tests fix nonces error #289

Merged
merged 14 commits into from
May 22, 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
14 changes: 13 additions & 1 deletion x/emissions/keeper/msgserver/msg_server_util_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package msgserver
import (
"container/heap"
"math/rand"
"sort"

"github.com/allora-network/allora-chain/x/emissions/types"
)
Expand Down Expand Up @@ -63,7 +64,18 @@ func FindTopNByScoreDesc(n uint64, scoresByActor map[Actor]Score, randSeed Block
r := rand.New(rand.NewSource(randSeed))
queue := &PriorityQueue{}
i := 0
for actor, score := range scoresByActor {
// Extract and sort the keys
keys := make([]Actor, 0, len(scoresByActor))
for actor := range scoresByActor {
keys = append(keys, actor)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})

// Iterate over the sorted keys
for _, actor := range keys {
score := scoresByActor[actor]
queue.Push(&SortableItem{actor, score, r.Uint32(), i})
i++
}
Expand Down
6 changes: 0 additions & 6 deletions x/emissions/keeper/topic_weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ func (k *Keeper) GetCurrentTopicWeight(
topicStakeDec, err := alloraMath.NewDecFromSdkInt(topicStake)
if err != nil {
return alloraMath.Dec{}, cosmosMath.Int{}, errors.Wrapf(err, "failed to convert topic stake to dec")
} else {
fmt.Println("Topic stake: ", topicStake)
}

// Get and total topic fee revenue
Expand Down Expand Up @@ -93,14 +91,10 @@ func (k *Keeper) GetCurrentTopicWeight(
previousTopicWeight, noPrior, err := k.GetPreviousTopicWeight(ctx, topicId)
if err != nil {
return alloraMath.Dec{}, cosmosMath.Int{}, errors.Wrapf(err, "failed to get previous topic weight")
} else {
fmt.Println("Previous topic weight: ", previousTopicWeight)
}
weight, err = alloraMath.CalcEma(topicRewardAlpha, targetWeight, previousTopicWeight, noPrior)
if err != nil {
return alloraMath.Dec{}, cosmosMath.Int{}, errors.Wrapf(err, "failed to calculate EMA")
} else {
fmt.Println("EMA Weight: ", weight)
}

return weight, topicFeeRevenue.Revenue, nil
Expand Down
2 changes: 0 additions & 2 deletions x/emissions/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ func EndBlocker(ctx context.Context, am AppModule) error {
return errors.Wrapf(err, "Weights error")
} else {
fmt.Println("Weights: Weights: ", weights)
fmt.Println("Weights: Sum of weights: ", sumWeight)
fmt.Println("Weights: Total revenue: ", totalRevenue)
}

// REWARDS (will internally filter any non-RewardReady topics)
Expand Down
32 changes: 20 additions & 12 deletions x/emissions/module/rewards/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func EmitRewards(ctx sdk.Context, k keeper.Keeper, blockHeight BlockHeight, weig
}
if totalReward.IsZero() {
ctx.Logger().Warn("The total scheduled rewards to distribute this epoch are zero!")
return nil
}

moduleParams, err := k.GetParams(ctx)
if err != nil {
return errors.Wrapf(err, "failed to get module params")
Expand Down Expand Up @@ -51,10 +53,12 @@ func EmitRewards(ctx sdk.Context, k keeper.Keeper, blockHeight BlockHeight, weig
totalRewardsDistribution, rewardInTopicToReputers, err := GenerateRewardsDistributionByTopicParticipant(ctx, k, topicId, topicReward, topicRewardNonce, moduleParams)
if err != nil {
ctx.Logger().Warn(
"Failed to Generate Rewards for Topic, Skipping:\nTopic Id %d\nTopic Reward Amount %s\nError:\n%s\n\n",
topicId,
topicReward.String(),
err,
fmt.Sprintf(
"Failed to Generate Rewards for Topic, Skipping:\nTopic Id %d\nTopic Reward Amount %s\nError:\n%s\n\n",
topicId,
topicReward.String(),
err.Error(),
),
)
continue
}
Expand All @@ -73,10 +77,12 @@ func EmitRewards(ctx sdk.Context, k keeper.Keeper, blockHeight BlockHeight, weig
if len(payoutErrors) > 0 {
for _, err := range payoutErrors {
ctx.Logger().Warn(
"Failed to pay out rewards to participant in Topic:\nTopic Id %d\nTopic Reward Amount %s\nError:\n%s\n\n",
topicId,
topicReward.String(),
err,
fmt.Sprintf(
"Failed to pay out rewards to participant in Topic:\nTopic Id %d\nTopic Reward Amount %s\nError:\n%s\n\n",
topicId,
topicReward.String(),
err.Error(),
),
)
}
continue
Expand All @@ -86,10 +92,12 @@ func EmitRewards(ctx sdk.Context, k keeper.Keeper, blockHeight BlockHeight, weig
err = pruneRecordsAfterRewards(ctx, k, moduleParams.MinEpochLengthRecordLimit, topicId, topicRewardNonce)
if err != nil {
ctx.Logger().Warn(
"Failed to prune records after rewards for Topic, Skipping:\nTopic Id %d\nTopic Reward Amount %s\nError:\n%s\n\n",
topicId,
topicReward.String(),
err,
fmt.Sprintf(
"Failed to prune records after rewards for Topic, Skipping:\nTopic Id %d\nTopic Reward Amount %s\nError:\n%s\n\n",
topicId,
topicReward.String(),
err.Error(),
),
)
continue
}
Expand Down
14 changes: 11 additions & 3 deletions x/emissions/module/rewards/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@ func (s *RewardsTestSuite) TestStandardRewardEmissionShouldRewardTopicsWithFulfi
beforeRewardsTopic2FeeRevenue, err := s.emissionsKeeper.GetTopicFeeRevenue(s.ctx, topicId2)
s.Require().NoError(err)

// mint some rewards to give out
s.MintTokensToModule(types.AlloraRewardsAccountName, cosmosMath.NewInt(1000))

block += 1
s.ctx = s.ctx.WithBlockHeight(block)

Expand All @@ -592,7 +595,12 @@ func (s *RewardsTestSuite) TestStandardRewardEmissionShouldRewardTopicsWithFulfi
s.Require().NoError(err)

// Topic 1 should have less revenue after rewards distribution -> rewards distributed
s.Require().True(beforeRewardsTopic1FeeRevenue.Revenue.GT(afterRewardsTopic1FeeRevenue.Revenue))
s.Require().True(
beforeRewardsTopic1FeeRevenue.Revenue.GT(afterRewardsTopic1FeeRevenue.Revenue),
"Topic 1 should have more fee revenue: %s > %s",
beforeRewardsTopic1FeeRevenue.Revenue.String(),
afterRewardsTopic1FeeRevenue.Revenue.String(),
)
// Topic 2 should have the same revenue after rewards distribution -> no rewards distributed
s.Require().Equal(beforeRewardsTopic2FeeRevenue.Revenue, afterRewardsTopic2FeeRevenue.Revenue)
}
Expand Down Expand Up @@ -1676,8 +1684,8 @@ func (s *RewardsTestSuite) TestRewardsIncreasesBalance() {
block += epochLength * 3
s.ctx = s.ctx.WithBlockHeight(block)

workerInitialBalanceCoins := sdk.NewCoins(sdk.NewCoin(params.DefaultBondDenom, cosmosMath.NewInt(1000)))
s.bankKeeper.MintCoins(s.ctx, types.AlloraRewardsAccountName, workerInitialBalanceCoins)
// mint some rewards to give out
s.MintTokensToModule(types.AlloraRewardsAccountName, cosmosMath.NewInt(1000))

// Trigger end block - rewards distribution
err = s.emissionsAppModule.EndBlock(s.ctx)
Expand Down
2 changes: 0 additions & 2 deletions x/emissions/module/rewards/topic_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ func GetAndOptionallyUpdateActiveTopicWeights(
)
if err != nil {
return errors.Wrapf(err, "failed to get current topic weight")
} else {
fmt.Println("Topic ID: ", topic.Id, " Weight: ", weight)
}

totalRevenue = totalRevenue.Add(topicFeeRevenue)
Expand Down
5 changes: 4 additions & 1 deletion x/emissions/module/rewards/topic_skimming.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func SkimTopTopicsByWeightDesc(weights map[TopicId]*alloraMath.Dec, N uint64, bl
for topicId := range weights {
topicIds = append(topicIds, topicId)
}

// sort topicIds to ensure deterministic order
sort.Slice(topicIds, func(i, j int) bool {
return (*weights[topicIds[i]]).Gt(*weights[topicIds[j]])
})
sortedTopicIds := SortTopicsByWeightDescWithRandomTiebreaker(topicIds, weights, block)

numberToAdd := N
Expand Down
Loading