-
Notifications
You must be signed in to change notification settings - Fork 725
[Bug] Fix misaligned height in calls to GetBlockValue #1848
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
[Bug] Fix misaligned height in calls to GetBlockValue #1848
Conversation
6234f54 to
0f1e995
Compare
0f1e995 to
5372a93
Compare
|
Rebased on master. Ready for review. |
furszy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK 5372a934.
furszy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Synced from scratch on master, ACK 777a912
777a912 to
4ada5b9
Compare
|
Rebased (fixing conflict with #1872 just merged) |
4ada5b9 to
288953d
Compare
furszy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 288953d after rebase.
Fuzzbawls
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 288953d
This is a known old bug, which was never truly fixed, due to it being harmless (and discovered after the last block-value reduction).
The function
GetBlockValue(int nHeight)returns the block-reward value for a block at a given height (based on a "halving" schedule hardcoded within the function).The issue is that it was being called passing the height of the previous block.
At some point, one particular call (in
CreateCoinStake) was changed to use the (correct) current block height, while leaving the wrong one in all other places (including masternode payments/budgets and validation code).This made no real difference with PIVX, as the change was introduced before the last milestone (which increased the block value from 4.5 to 5, rather than reducing it), and, after that, the block value never changed anymore (so it has always been
GetBlockValue(N) == GetBlockValue(N-1)).But it was an issue for all PIVX forks that had different schedules (or that introduced drastic changes to the masternode/staker-rewards proportions).
In #967 @CaveSpectre11 gives a complete explanation of the problem (already discussed on a wrong fix attempt in #814), and proposes to change only the call in
CreateCoinStake, aligning it to the (wrong) height (mostly for the sake of consistency and for the benefit of future PIVX forks, as it has no effect on PIVX chain, at his current height).This PR, instead, proposes the opposite solution. Align all other calls, so that the correct height is passed (
pindex->nHeightduring validation, andpindexPrev->nHeight + 1when creating a new block).This makes the code consistent with the actual block values on chain.
For example, the first-6-masternodes-premine was not in the genesis block, but at block 1.
Therefore we should have had
60001 * COINreturned byGetBlockValue(1), not byGetBlockValue(0).Thus, to align it with the values on chain (and prevent issues during initial sync) the schedule defined inside
GetBlockValueis "shifted ahead" by one block.This also refactors
GetBlockValueto make it more readable.Tested Resyncing from scratch on both testnet and mainnet.
Based on top of
just to avoid merge conflicts with the refactoring of
CBudgetManager::FillBlockPayee.Only the last two commits are new here.