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

Static pool reward #2924

Merged
merged 6 commits into from
Jul 15, 2024
Merged

Static pool reward #2924

merged 6 commits into from
Jul 15, 2024

Conversation

Bushstar
Copy link
Member

Summary

The CalculateOwnerRewards function is currently the most performance intensive part of DeFiChain, this function calculates the coinbase, loan, commission and custom pool rewards for a liquidity owner from when their address was last used to the current usage which triggers this call. When calculating rewards, every single block since between the last usage and now is checked with the coinbase, loan, commission and custom pool rewards calculated for the address' liquidity against the total liquidity. This is heavy, especially if the address has not been used for a long time and has liquidity in multiple pools.

This PR greatly simplifies this process by avoiding working through each block a time. On each block we workout the coinbase, loan, commission and custom pool reward for each liquidity share in the pool and save this against a running total stored with the current height. When an address is used we get this total from the last time the address was used and the current total, deducting the last usage total from the current we can see the reward per liquidity share for this period and use that to reward the address for however many liquidity shares it has.

Rather than work through each block and run calculations for each reward type, we get the two totals from the last usage block to the current block for each reward type and calculate the rewards. There is additional work storing these totals on each block, but this can be considered negligible and a reasonable trade off.

Implications

  • Storage

    • Database reindex required
    • Database reindex optional
    • Database reindex not required
    • None
  • Consensus

    • Network upgrade required
    • Includes backward compatible changes
    • Includes consensus workarounds
    • Includes consensus refactors
    • None

Copy link
Contributor

@kuegi kuegi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really elegant solution. I like it!

src/dfi/poolpairs.cpp Outdated Show resolved Hide resolved
src/dfi/poolpairs.cpp Outdated Show resolved Hide resolved
@prasannavl prasannavl changed the title WIP: Static pool reward Static pool reward Jun 4, 2024
@prasannavl prasannavl added protocol Has network protocol changes p2 Medium priority v/next-network-upgrade Items ready or targeted for upcoming network upgrade release(s) e3 Medium effort // takes significant effort or expertise labels Jul 3, 2024
@prasannavl prasannavl merged commit 5370c8c into master Jul 15, 2024
22 of 29 checks passed
@prasannavl prasannavl deleted the bush/static-reward branch July 15, 2024 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e3 Medium effort // takes significant effort or expertise p2 Medium priority protocol Has network protocol changes v/next-network-upgrade Items ready or targeted for upcoming network upgrade release(s)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants