Skip to content

Calculate topic rewards in a more precise way - #967

Draft
fstecker wants to merge 2 commits into
devfrom
florian/precise_topic_rewards
Draft

Calculate topic rewards in a more precise way#967
fstecker wants to merge 2 commits into
devfrom
florian/precise_topic_rewards

Conversation

@fstecker

@fstecker fstecker commented Jul 20, 2026

Copy link
Copy Markdown

This is a proposed fix for an inaccuracy in how the rewards allocated to a topic are calculated at the end of a topic.

 topicRewardPerEpoch = topicWeight / totalSumPreviousTopicWeights * rewardCurrentBlockEmission * epochLength

This would be correct if all quantities involved were constant throughout the epoch. But in reality, because the epochs of other topics end in the meantime, the normalized topic weight of our topic constantly updates throughout its epoch. A correct implementation would keep track of the normalized topic weight in each block of the epoch, and sum these up, instead of just multiplying the last one of them with epochLength. But naively, this would come with a signification computational cost.

This PR is an optimized version of this, which sidesteps the computational. We keep a history of the topic-independent quantity

blockRewardsByTotalWeight = rewardCurrentBlockEmission / totalSumPreviousTopicWeights

over every block. We hold this in a "Fenwick tree", a type of list which allows to summing over ranges in logarithmic time. Then, at the end of every epoch, we can just compute

topicRewardPerEpoch = topicWeight * blockRewardsByTotalWeight.rangeSum(height - epoch_length, height)

The details are described in RES-919.

This PR is not intended to be merged as-is, I haven't even tested it. I just wanted to put this suggestion into code, hoping that it can serve as a starting point for a proper fix.


Summary by cubic

Compute topic rewards block-by-block using a Fenwick tree to fix inaccuracies when topic weights change mid-epoch. This implements the approach described in RES-919 and keeps reward calculations precise without heavy compute.

  • New Features

    • Added a per-block record of reward per unit topic weight: rewardCurrentBlockEmission / totalSumPreviousTopicWeights, stored in a Fenwick tree for O(log N) range sums.
    • New keeper APIs to append and sum per-unit-weight rewards over block ranges, then multiply by a topic’s weight to get epoch rewards.
  • Bug Fixes

    • Corrected epoch reward calculation to sum per-block credits over the epoch instead of extrapolating a single value.
    • Deferred committing computed topic weights until after rewards are distributed, ensuring rewards use the weights that were in effect during the paid epochs.

Written for commit 88eaab1. Summary will update on new commits.

Review in cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant