Skip to content

Conversation

@random-zebra
Copy link

@random-zebra random-zebra commented Sep 10, 2020

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->nHeight during validation, and pindexPrev->nHeight + 1 when 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 * COIN returned by GetBlockValue(1), not by GetBlockValue(0).

Thus, to align it with the values on chain (and prevent issues during initial sync) the schedule defined inside GetBlockValue is "shifted ahead" by one block.

This also refactors GetBlockValue to 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.

@random-zebra random-zebra added Bug Block Generation Mining/Staking related issues labels Sep 10, 2020
@random-zebra random-zebra added this to the 5.0.0 milestone Sep 10, 2020
@random-zebra random-zebra self-assigned this Sep 10, 2020
@random-zebra random-zebra force-pushed the 202009_getblockvalue_inconsistency branch from 6234f54 to 0f1e995 Compare September 21, 2020 22:43
@random-zebra random-zebra force-pushed the 202009_getblockvalue_inconsistency branch from 0f1e995 to 5372a93 Compare September 29, 2020 09:46
@random-zebra
Copy link
Author

Rebased on master. Ready for review.

Copy link

@furszy furszy left a 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
furszy previously approved these changes Sep 30, 2020
Copy link

@furszy furszy left a 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

@random-zebra
Copy link
Author

Rebased (fixing conflict with #1872 just merged)

@random-zebra random-zebra force-pushed the 202009_getblockvalue_inconsistency branch from 4ada5b9 to 288953d Compare October 3, 2020 22:13
Copy link

@furszy furszy left a 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.

Copy link
Collaborator

@Fuzzbawls Fuzzbawls left a comment

Choose a reason for hiding this comment

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

ACK 288953d

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

Labels

Block Generation Mining/Staking related issues Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants