Skip to content

Commit

Permalink
iterate through reporters deterministically (#403)
Browse files Browse the repository at this point in the history
* iterate through reporters deterministically

* execute after tally for quorum votes

* removed one comment that appeared way more than intended and added one comment

---------

Co-authored-by: Cjpotter10 <[email protected]>
  • Loading branch information
tkernell and CJPotter10 authored Oct 29, 2024
1 parent b404fed commit 6a5beb1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
2 changes: 1 addition & 1 deletion daemons/reporter/client/reporter_monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func (c *Client) MonitorForTippedQueries(ctx context.Context, wg *sync.WaitGroup
continue
}
if len(res.Queries) == 0 {
c.logger.Info("No tipped queries returned")
time.Sleep(200 * time.Millisecond)
continue
}
Expand All @@ -95,6 +94,7 @@ func (c *Client) MonitorForTippedQueries(ctx context.Context, wg *sync.WaitGroup
if err != nil {
c.logger.Error("Error generating report for tipped query: ", err)
}
c.logger.Info("Broadcasted report for tipped query")
}(res.Queries[i])
}

Expand Down
9 changes: 6 additions & 3 deletions tests/integration/dispute_keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,12 @@ func (s *IntegrationTestSuite) TestExecuteVoteAgainst() {
s.Equal(newBal, votersBalanceAfter[v.Voter.String()].Amount)
}

// execute vote
s.NoError(s.Setup.Disputekeeper.ExecuteVote(s.Setup.Ctx, 1))

dispute, err := s.Setup.Disputekeeper.Disputes.Get(s.Setup.Ctx, 1)
s.NoError(err)
s.Equal(types.Resolved, dispute.DisputeStatus)
voteInfo, err := s.Setup.Disputekeeper.Votes.Get(s.Setup.Ctx, 1)
s.NoError(err)
s.True(voteInfo.Executed)
// Check voter rewards
disputerVoterReward, err := s.Setup.Disputekeeper.CalculateReward(s.Setup.Ctx, disputer, 1)
s.NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion x/dispute/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func CheckClosedDisputesForExecution(ctx context.Context, k keeper.Keeper) error
if err != nil {
return err
}
if sdk.UnwrapSDKContext(ctx).BlockTime().After(dispute.DisputeEndTime) {
if sdk.UnwrapSDKContext(ctx).BlockTime().After(dispute.DisputeEndTime) || dispute.DisputeStatus == types.Resolved {
if err := k.ExecuteVote(ctx, key); err != nil {
return err
}
Expand Down
69 changes: 52 additions & 17 deletions x/oracle/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"sort"

layer "github.com/tellor-io/layer/types"
minttypes "github.com/tellor-io/layer/x/mint/types"
Expand All @@ -28,43 +29,77 @@ func (k Keeper) AllocateRewards(ctx context.Context, reporters []*types.Aggregat
}
// Initialize totalPower to keep track of the total power of all reporters.
totalPower := uint64(0)
// reportCounts maps reporter's address to their ValidatorReportCount.
_reporters := make(map[string]ReportersReportCount)

// Loop through each reporter to calculate total power and individual report counts.
// Use a struct to hold reporter info
type ReporterInfo struct {
address string
data ReportersReportCount
}

// First pass: collect data in map
reportersMap := make(map[string]ReportersReportCount)
for _, r := range reporters {
reporter, found := _reporters[r.Reporter]
reporter, found := reportersMap[r.Reporter]
if found {
// If the reporter is already in the map, increment their report count.
reporter.Reports++
} else {
// If not found, add the reporter with their initial power and report count set to 1.
reporter = ReportersReportCount{Power: r.Power, Reports: 1, Height: r.BlockNumber}
reporter = ReportersReportCount{
Power: r.Power,
Reports: 1,
Height: r.BlockNumber,
}
}
_reporters[r.Reporter] = reporter
// Add the reporter's power to the total power.
reportersMap[r.Reporter] = reporter
totalPower += r.Power
}
i := len(_reporters)

// Convert to sorted slice for deterministic iteration
sortedReporters := make([]ReporterInfo, 0, len(reportersMap))
for addr, data := range reportersMap {
sortedReporters = append(sortedReporters, ReporterInfo{
address: addr,
data: data,
})
}

// Sort by address for deterministic ordering
sort.Slice(sortedReporters, func(i, j int) bool {
return sortedReporters[i].address < sortedReporters[j].address
})

// Process rewards in deterministic order
totaldist := math.ZeroUint()
for r, c := range _reporters {
amount := CalculateRewardAmount(c.Power, c.Reports, totalPower, reward)
for i, reporter := range sortedReporters {
amount := CalculateRewardAmount(
reporter.data.Power,
reporter.data.Reports,
totalPower,
reward,
)
totaldist = totaldist.Add(amount.Value)
reporterAddr, err := sdk.AccAddressFromBech32(r)

reporterAddr, err := sdk.AccAddressFromBech32(reporter.address)
if err != nil {
return err
}
i--
if i == 0 {

// Handle final reporter
if i == len(sortedReporters)-1 {
amount.Value = amount.Value.Add(math.NewUint(reward.Uint64()).MulUint64(1e6)).Sub(totaldist)
}
err = k.AllocateTip(ctx, reporterAddr.Bytes(), amount, c.Height)

err = k.AllocateTip(ctx, reporterAddr.Bytes(), amount, reporter.data.Height)
if err != nil {
return err
}
}

return k.bankKeeper.SendCoinsFromModuleToModule(ctx, fromPool, reportertypes.TipsEscrowPool, sdk.NewCoins(sdk.NewCoin(layer.BondDenom, reward)))
return k.bankKeeper.SendCoinsFromModuleToModule(
ctx,
fromPool,
reportertypes.TipsEscrowPool,
sdk.NewCoins(sdk.NewCoin(layer.BondDenom, reward)),
)
}

func (k Keeper) GetTimeBasedRewards(ctx context.Context) math.Int {
Expand Down

0 comments on commit 6a5beb1

Please sign in to comment.