You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1060 lines
39 KiB

Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
Normalize priorities to not exceed total voting power (#3049) * more proposer priority tests - test that we don't reset to zero when updating / adding - test that same power validators alternate * add another test to track / simulate similar behaviour as in #2960 * address some of Chris' review comments * address some more of Chris' review comments * temporarily pushing branch with the following changes: The total power might change if: - a validator is added - a validator is removed - a validator is updated Decrement the accums (of all validators) directly after any of these events (by the inverse of the change) * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * Fix 2960 by re-normalizing / scaling priorities to be in bounds of total power, additionally: - remove heap where it doesn't make sense - avg. only at the end of IncrementProposerPriority instead of each iteration - update (and slightly improve) TestAveragingInIncrementProposerPriorityWithVotingPower to reflect above changes * fix tests * add comment * update changelog pending & some minor changes * comment about division will floor the result & fix typo * Update TestLargeGenesisValidator: - remove TODO and increase large genesis validator's voting power accordingly * move changelog entry to P2P Protocol * Ceil instead of flooring when dividing & update test * quickly fix failing TestProposerPriorityDoesNotGetResetToZero: - divide by Ceil((maxPriority - minPriority) / 2*totalVotingPower) * fix typo: rename getValWitMostPriority -> getValWithMostPriority * test proposer frequencies * return absolute value for diff. keep testing * use for loop for div * cleanup, more tests * spellcheck * get rid of using floats: manually ceil where necessary * Remove float, simplify, fix tests to match chris's proof (#3157)
6 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math"
  6. "math/big"
  7. "os"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. abci "github.com/tendermint/tendermint/abci/types"
  12. "github.com/tendermint/tendermint/crypto"
  13. "github.com/tendermint/tendermint/crypto/ed25519"
  14. cmn "github.com/tendermint/tendermint/libs/common"
  15. dbm "github.com/tendermint/tendermint/libs/db"
  16. cfg "github.com/tendermint/tendermint/config"
  17. "github.com/tendermint/tendermint/types"
  18. )
  19. // setupTestCase does setup common to all test cases.
  20. func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, State) {
  21. config := cfg.ResetTestRoot("state_")
  22. dbType := dbm.DBBackendType(config.DBBackend)
  23. stateDB := dbm.NewDB("state", dbType, config.DBDir())
  24. state, err := LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile())
  25. assert.NoError(t, err, "expected no error on LoadStateFromDBOrGenesisFile")
  26. tearDown := func(t *testing.T) { os.RemoveAll(config.RootDir) }
  27. return tearDown, stateDB, state
  28. }
  29. // TestStateCopy tests the correct copying behaviour of State.
  30. func TestStateCopy(t *testing.T) {
  31. tearDown, _, state := setupTestCase(t)
  32. defer tearDown(t)
  33. // nolint: vetshadow
  34. assert := assert.New(t)
  35. stateCopy := state.Copy()
  36. assert.True(state.Equals(stateCopy),
  37. fmt.Sprintf("expected state and its copy to be identical.\ngot: %v\nexpected: %v\n",
  38. stateCopy, state))
  39. stateCopy.LastBlockHeight++
  40. assert.False(state.Equals(stateCopy), fmt.Sprintf(`expected states to be different. got same
  41. %v`, state))
  42. }
  43. //TestMakeGenesisStateNilValidators tests state's consistency when genesis file's validators field is nil.
  44. func TestMakeGenesisStateNilValidators(t *testing.T) {
  45. doc := types.GenesisDoc{
  46. ChainID: "dummy",
  47. Validators: nil,
  48. }
  49. require.Nil(t, doc.ValidateAndComplete())
  50. state, err := MakeGenesisState(&doc)
  51. require.Nil(t, err)
  52. require.Equal(t, 0, len(state.Validators.Validators))
  53. require.Equal(t, 0, len(state.NextValidators.Validators))
  54. }
  55. // TestStateSaveLoad tests saving and loading State from a db.
  56. func TestStateSaveLoad(t *testing.T) {
  57. tearDown, stateDB, state := setupTestCase(t)
  58. defer tearDown(t)
  59. // nolint: vetshadow
  60. assert := assert.New(t)
  61. state.LastBlockHeight++
  62. SaveState(stateDB, state)
  63. loadedState := LoadState(stateDB)
  64. assert.True(state.Equals(loadedState),
  65. fmt.Sprintf("expected state and its copy to be identical.\ngot: %v\nexpected: %v\n",
  66. loadedState, state))
  67. }
  68. // TestABCIResponsesSaveLoad tests saving and loading ABCIResponses.
  69. func TestABCIResponsesSaveLoad1(t *testing.T) {
  70. tearDown, stateDB, state := setupTestCase(t)
  71. defer tearDown(t)
  72. // nolint: vetshadow
  73. assert := assert.New(t)
  74. state.LastBlockHeight++
  75. // Build mock responses.
  76. block := makeBlock(state, 2)
  77. abciResponses := NewABCIResponses(block)
  78. abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo"), Tags: nil}
  79. abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok", Tags: nil}
  80. abciResponses.EndBlock = &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{
  81. types.TM2PB.NewValidatorUpdate(ed25519.GenPrivKey().PubKey(), 10),
  82. }}
  83. saveABCIResponses(stateDB, block.Height, abciResponses)
  84. loadedABCIResponses, err := LoadABCIResponses(stateDB, block.Height)
  85. assert.Nil(err)
  86. assert.Equal(abciResponses, loadedABCIResponses,
  87. fmt.Sprintf("ABCIResponses don't match:\ngot: %v\nexpected: %v\n",
  88. loadedABCIResponses, abciResponses))
  89. }
  90. // TestResultsSaveLoad tests saving and loading ABCI results.
  91. func TestABCIResponsesSaveLoad2(t *testing.T) {
  92. tearDown, stateDB, _ := setupTestCase(t)
  93. defer tearDown(t)
  94. // nolint: vetshadow
  95. assert := assert.New(t)
  96. cases := [...]struct {
  97. // Height is implied to equal index+2,
  98. // as block 1 is created from genesis.
  99. added []*abci.ResponseDeliverTx
  100. expected types.ABCIResults
  101. }{
  102. 0: {
  103. nil,
  104. nil,
  105. },
  106. 1: {
  107. []*abci.ResponseDeliverTx{
  108. {Code: 32, Data: []byte("Hello"), Log: "Huh?"},
  109. },
  110. types.ABCIResults{
  111. {32, []byte("Hello")},
  112. }},
  113. 2: {
  114. []*abci.ResponseDeliverTx{
  115. {Code: 383},
  116. {Data: []byte("Gotcha!"),
  117. Tags: []cmn.KVPair{
  118. {Key: []byte("a"), Value: []byte("1")},
  119. {Key: []byte("build"), Value: []byte("stuff")},
  120. }},
  121. },
  122. types.ABCIResults{
  123. {383, nil},
  124. {0, []byte("Gotcha!")},
  125. }},
  126. 3: {
  127. nil,
  128. nil,
  129. },
  130. }
  131. // Query all before, this should return error.
  132. for i := range cases {
  133. h := int64(i + 1)
  134. res, err := LoadABCIResponses(stateDB, h)
  135. assert.Error(err, "%d: %#v", i, res)
  136. }
  137. // Add all cases.
  138. for i, tc := range cases {
  139. h := int64(i + 1) // last block height, one below what we save
  140. responses := &ABCIResponses{
  141. DeliverTx: tc.added,
  142. EndBlock: &abci.ResponseEndBlock{},
  143. }
  144. saveABCIResponses(stateDB, h, responses)
  145. }
  146. // Query all before, should return expected value.
  147. for i, tc := range cases {
  148. h := int64(i + 1)
  149. res, err := LoadABCIResponses(stateDB, h)
  150. assert.NoError(err, "%d", i)
  151. assert.Equal(tc.expected.Hash(), res.ResultsHash(), "%d", i)
  152. }
  153. }
  154. // TestValidatorSimpleSaveLoad tests saving and loading validators.
  155. func TestValidatorSimpleSaveLoad(t *testing.T) {
  156. tearDown, stateDB, state := setupTestCase(t)
  157. defer tearDown(t)
  158. // nolint: vetshadow
  159. assert := assert.New(t)
  160. // Can't load anything for height 0.
  161. v, err := LoadValidators(stateDB, 0)
  162. assert.IsType(ErrNoValSetForHeight{}, err, "expected err at height 0")
  163. // Should be able to load for height 1.
  164. v, err = LoadValidators(stateDB, 1)
  165. assert.Nil(err, "expected no err at height 1")
  166. assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  167. // Should be able to load for height 2.
  168. v, err = LoadValidators(stateDB, 2)
  169. assert.Nil(err, "expected no err at height 2")
  170. assert.Equal(v.Hash(), state.NextValidators.Hash(), "expected validator hashes to match")
  171. // Increment height, save; should be able to load for next & next next height.
  172. state.LastBlockHeight++
  173. nextHeight := state.LastBlockHeight + 1
  174. saveValidatorsInfo(stateDB, nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators)
  175. vp0, err := LoadValidators(stateDB, nextHeight+0)
  176. assert.Nil(err, "expected no err")
  177. vp1, err := LoadValidators(stateDB, nextHeight+1)
  178. assert.Nil(err, "expected no err")
  179. assert.Equal(vp0.Hash(), state.Validators.Hash(), "expected validator hashes to match")
  180. assert.Equal(vp1.Hash(), state.NextValidators.Hash(), "expected next validator hashes to match")
  181. }
  182. // TestValidatorChangesSaveLoad tests saving and loading a validator set with changes.
  183. func TestOneValidatorChangesSaveLoad(t *testing.T) {
  184. tearDown, stateDB, state := setupTestCase(t)
  185. defer tearDown(t)
  186. // Change vals at these heights.
  187. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20}
  188. N := len(changeHeights)
  189. // Build the validator history by running updateState
  190. // with the right validator set for each height.
  191. highestHeight := changeHeights[N-1] + 5
  192. changeIndex := 0
  193. _, val := state.Validators.GetByIndex(0)
  194. power := val.VotingPower
  195. var err error
  196. var validatorUpdates []*types.Validator
  197. for i := int64(1); i < highestHeight; i++ {
  198. // When we get to a change height, use the next pubkey.
  199. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  200. changeIndex++
  201. power++
  202. }
  203. header, blockID, responses := makeHeaderPartsResponsesValPowerChange(state, i, power)
  204. validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.EndBlock.ValidatorUpdates)
  205. require.NoError(t, err)
  206. state, err = updateState(state, blockID, &header, responses, validatorUpdates)
  207. require.NoError(t, err)
  208. nextHeight := state.LastBlockHeight + 1
  209. saveValidatorsInfo(stateDB, nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators)
  210. }
  211. // On each height change, increment the power by one.
  212. testCases := make([]int64, highestHeight)
  213. changeIndex = 0
  214. power = val.VotingPower
  215. for i := int64(1); i < highestHeight+1; i++ {
  216. // We get to the height after a change height use the next pubkey (note
  217. // our counter starts at 0 this time).
  218. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  219. changeIndex++
  220. power++
  221. }
  222. testCases[i-1] = power
  223. }
  224. for i, power := range testCases {
  225. v, err := LoadValidators(stateDB, int64(i+1+1)) // +1 because vset changes delayed by 1 block.
  226. assert.Nil(t, err, fmt.Sprintf("expected no err at height %d", i))
  227. assert.Equal(t, v.Size(), 1, "validator set size is greater than 1: %d", v.Size())
  228. _, val := v.GetByIndex(0)
  229. assert.Equal(t, val.VotingPower, power, fmt.Sprintf(`unexpected powerat
  230. height %d`, i))
  231. }
  232. }
  233. func TestProposerFrequency(t *testing.T) {
  234. // some explicit test cases
  235. testCases := []struct {
  236. powers []int64
  237. }{
  238. // 2 vals
  239. {[]int64{1, 1}},
  240. {[]int64{1, 2}},
  241. {[]int64{1, 100}},
  242. {[]int64{5, 5}},
  243. {[]int64{5, 100}},
  244. {[]int64{50, 50}},
  245. {[]int64{50, 100}},
  246. {[]int64{1, 1000}},
  247. // 3 vals
  248. {[]int64{1, 1, 1}},
  249. {[]int64{1, 2, 3}},
  250. {[]int64{1, 2, 3}},
  251. {[]int64{1, 1, 10}},
  252. {[]int64{1, 1, 100}},
  253. {[]int64{1, 10, 100}},
  254. {[]int64{1, 1, 1000}},
  255. {[]int64{1, 10, 1000}},
  256. {[]int64{1, 100, 1000}},
  257. // 4 vals
  258. {[]int64{1, 1, 1, 1}},
  259. {[]int64{1, 2, 3, 4}},
  260. {[]int64{1, 1, 1, 10}},
  261. {[]int64{1, 1, 1, 100}},
  262. {[]int64{1, 1, 1, 1000}},
  263. {[]int64{1, 1, 10, 100}},
  264. {[]int64{1, 1, 10, 1000}},
  265. {[]int64{1, 1, 100, 1000}},
  266. {[]int64{1, 10, 100, 1000}},
  267. }
  268. for caseNum, testCase := range testCases {
  269. // run each case 5 times to sample different
  270. // initial priorities
  271. for i := 0; i < 5; i++ {
  272. valSet := genValSetWithPowers(testCase.powers)
  273. testProposerFreq(t, caseNum, valSet)
  274. }
  275. }
  276. // some random test cases with up to 100 validators
  277. maxVals := 100
  278. maxPower := 1000
  279. nTestCases := 5
  280. for i := 0; i < nTestCases; i++ {
  281. N := cmn.RandInt()%maxVals + 1
  282. vals := make([]*types.Validator, N)
  283. totalVotePower := int64(0)
  284. for j := 0; j < N; j++ {
  285. // make sure votePower > 0
  286. votePower := int64(cmn.RandInt()%maxPower) + 1
  287. totalVotePower += votePower
  288. privVal := types.NewMockPV()
  289. pubKey := privVal.GetPubKey()
  290. val := types.NewValidator(pubKey, votePower)
  291. val.ProposerPriority = cmn.RandInt64()
  292. vals[j] = val
  293. }
  294. valSet := types.NewValidatorSet(vals)
  295. valSet.RescalePriorities(totalVotePower)
  296. testProposerFreq(t, i, valSet)
  297. }
  298. }
  299. // new val set with given powers and random initial priorities
  300. func genValSetWithPowers(powers []int64) *types.ValidatorSet {
  301. size := len(powers)
  302. vals := make([]*types.Validator, size)
  303. totalVotePower := int64(0)
  304. for i := 0; i < size; i++ {
  305. totalVotePower += powers[i]
  306. val := types.NewValidator(ed25519.GenPrivKey().PubKey(), powers[i])
  307. val.ProposerPriority = cmn.RandInt64()
  308. vals[i] = val
  309. }
  310. valSet := types.NewValidatorSet(vals)
  311. valSet.RescalePriorities(totalVotePower)
  312. return valSet
  313. }
  314. // test a proposer appears as frequently as expected
  315. func testProposerFreq(t *testing.T, caseNum int, valSet *types.ValidatorSet) {
  316. N := valSet.Size()
  317. totalPower := valSet.TotalVotingPower()
  318. // run the proposer selection and track frequencies
  319. runMult := 1
  320. runs := int(totalPower) * runMult
  321. freqs := make([]int, N)
  322. for i := 0; i < runs; i++ {
  323. prop := valSet.GetProposer()
  324. idx, _ := valSet.GetByAddress(prop.Address)
  325. freqs[idx] += 1
  326. valSet.IncrementProposerPriority(1)
  327. }
  328. // assert frequencies match expected (max off by 1)
  329. for i, freq := range freqs {
  330. _, val := valSet.GetByIndex(i)
  331. expectFreq := int(val.VotingPower) * runMult
  332. gotFreq := freq
  333. abs := int(math.Abs(float64(expectFreq - gotFreq)))
  334. // max bound on expected vs seen freq was proven
  335. // to be 1 for the 2 validator case in
  336. // https://github.com/cwgoes/tm-proposer-idris
  337. // and inferred to generalize to N-1
  338. bound := N - 1
  339. require.True(t, abs <= bound, fmt.Sprintf("Case %d val %d (%d): got %d, expected %d", caseNum, i, N, gotFreq, expectFreq))
  340. }
  341. }
  342. // TestProposerPriorityDoesNotGetResetToZero assert that we preserve accum when calling updateState
  343. // see https://github.com/tendermint/tendermint/issues/2718
  344. func TestProposerPriorityDoesNotGetResetToZero(t *testing.T) {
  345. tearDown, _, state := setupTestCase(t)
  346. defer tearDown(t)
  347. val1VotingPower := int64(10)
  348. val1PubKey := ed25519.GenPrivKey().PubKey()
  349. val1 := &types.Validator{Address: val1PubKey.Address(), PubKey: val1PubKey, VotingPower: val1VotingPower}
  350. state.Validators = types.NewValidatorSet([]*types.Validator{val1})
  351. state.NextValidators = state.Validators
  352. // NewValidatorSet calls IncrementProposerPriority but uses on a copy of val1
  353. assert.EqualValues(t, 0, val1.ProposerPriority)
  354. block := makeBlock(state, state.LastBlockHeight+1)
  355. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  356. abciResponses := &ABCIResponses{
  357. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  358. }
  359. validatorUpdates, err := types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  360. require.NoError(t, err)
  361. updatedState, err := updateState(state, blockID, &block.Header, abciResponses, validatorUpdates)
  362. assert.NoError(t, err)
  363. curTotal := val1VotingPower
  364. // one increment step and one validator: 0 + power - total_power == 0
  365. assert.Equal(t, 0+val1VotingPower-curTotal, updatedState.NextValidators.Validators[0].ProposerPriority)
  366. // add a validator
  367. val2PubKey := ed25519.GenPrivKey().PubKey()
  368. val2VotingPower := int64(100)
  369. updateAddVal := abci.ValidatorUpdate{PubKey: types.TM2PB.PubKey(val2PubKey), Power: val2VotingPower}
  370. validatorUpdates, err = types.PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{updateAddVal})
  371. assert.NoError(t, err)
  372. updatedState2, err := updateState(updatedState, blockID, &block.Header, abciResponses, validatorUpdates)
  373. assert.NoError(t, err)
  374. require.Equal(t, len(updatedState2.NextValidators.Validators), 2)
  375. _, updatedVal1 := updatedState2.NextValidators.GetByAddress(val1PubKey.Address())
  376. _, addedVal2 := updatedState2.NextValidators.GetByAddress(val2PubKey.Address())
  377. // adding a validator should not lead to a ProposerPriority equal to zero (unless the combination of averaging and
  378. // incrementing would cause so; which is not the case here)
  379. // Steps from adding new validator:
  380. // 0 - val1 prio is 0, TVP after add:
  381. wantVal1Prio := int64(0)
  382. totalPowerAfter := val1VotingPower + val2VotingPower
  383. // 1. Add - Val2 should be initially added with (-123) =>
  384. wantVal2Prio := -(totalPowerAfter + (totalPowerAfter >> 3))
  385. // 2. Scale - noop
  386. // 3. Center - with avg, resulting val2:-61, val1:62
  387. avg := big.NewInt(0).Add(big.NewInt(wantVal1Prio), big.NewInt(wantVal2Prio))
  388. avg.Div(avg, big.NewInt(2))
  389. wantVal2Prio = wantVal2Prio - avg.Int64() // -61
  390. wantVal1Prio = wantVal1Prio - avg.Int64() // 62
  391. // 4. Steps from IncrementProposerPriority
  392. wantVal1Prio = wantVal1Prio + val1VotingPower // 72
  393. wantVal2Prio = wantVal2Prio + val2VotingPower // 39
  394. wantVal1Prio = wantVal1Prio - totalPowerAfter // -38 as val1 is proposer
  395. assert.Equal(t, wantVal1Prio, updatedVal1.ProposerPriority)
  396. assert.Equal(t, wantVal2Prio, addedVal2.ProposerPriority)
  397. // Updating a validator does not reset the ProposerPriority to zero:
  398. // 1. Add - Val2 VotingPower change to 1 =>
  399. updatedVotingPowVal2 := int64(1)
  400. updateVal := abci.ValidatorUpdate{PubKey: types.TM2PB.PubKey(val2PubKey), Power: updatedVotingPowVal2}
  401. validatorUpdates, err = types.PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{updateVal})
  402. assert.NoError(t, err)
  403. // this will cause the diff of priorities (77)
  404. // to be larger than threshold == 2*totalVotingPower (22):
  405. updatedState3, err := updateState(updatedState2, blockID, &block.Header, abciResponses, validatorUpdates)
  406. assert.NoError(t, err)
  407. require.Equal(t, len(updatedState3.NextValidators.Validators), 2)
  408. _, prevVal1 := updatedState3.Validators.GetByAddress(val1PubKey.Address())
  409. _, prevVal2 := updatedState3.Validators.GetByAddress(val2PubKey.Address())
  410. _, updatedVal1 = updatedState3.NextValidators.GetByAddress(val1PubKey.Address())
  411. _, updatedVal2 := updatedState3.NextValidators.GetByAddress(val2PubKey.Address())
  412. // 2. Scale
  413. // old prios: v1(10):-38, v2(1):39
  414. wantVal1Prio = prevVal1.ProposerPriority
  415. wantVal2Prio = prevVal2.ProposerPriority
  416. // scale to diffMax = 22 = 2 * tvp, diff=39-(-38)=77
  417. // new totalPower
  418. totalPower := updatedVal1.VotingPower + updatedVal2.VotingPower
  419. dist := wantVal2Prio - wantVal1Prio
  420. // ratio := (dist + 2*totalPower - 1) / 2*totalPower = 98/22 = 4
  421. ratio := (dist + 2*totalPower - 1) / (2 * totalPower)
  422. // v1(10):-38/4, v2(1):39/4
  423. wantVal1Prio /= ratio // -9
  424. wantVal2Prio /= ratio // 9
  425. // 3. Center - noop
  426. // 4. IncrementProposerPriority() ->
  427. // v1(10):-9+10, v2(1):9+1 -> v2 proposer so subsract tvp(11)
  428. // v1(10):1, v2(1):-1
  429. wantVal2Prio += updatedVal2.VotingPower // 10 -> prop
  430. wantVal1Prio += updatedVal1.VotingPower // 1
  431. wantVal2Prio -= totalPower // -1
  432. assert.Equal(t, wantVal2Prio, updatedVal2.ProposerPriority)
  433. assert.Equal(t, wantVal1Prio, updatedVal1.ProposerPriority)
  434. }
  435. func TestProposerPriorityProposerAlternates(t *testing.T) {
  436. // Regression test that would fail if the inner workings of
  437. // IncrementProposerPriority change.
  438. // Additionally, make sure that same power validators alternate if both
  439. // have the same voting power (and the 2nd was added later).
  440. tearDown, _, state := setupTestCase(t)
  441. defer tearDown(t)
  442. val1VotingPower := int64(10)
  443. val1PubKey := ed25519.GenPrivKey().PubKey()
  444. val1 := &types.Validator{Address: val1PubKey.Address(), PubKey: val1PubKey, VotingPower: val1VotingPower}
  445. // reset state validators to above validator
  446. state.Validators = types.NewValidatorSet([]*types.Validator{val1})
  447. state.NextValidators = state.Validators
  448. // we only have one validator:
  449. assert.Equal(t, val1PubKey.Address(), state.Validators.Proposer.Address)
  450. block := makeBlock(state, state.LastBlockHeight+1)
  451. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  452. // no updates:
  453. abciResponses := &ABCIResponses{
  454. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  455. }
  456. validatorUpdates, err := types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  457. require.NoError(t, err)
  458. updatedState, err := updateState(state, blockID, &block.Header, abciResponses, validatorUpdates)
  459. assert.NoError(t, err)
  460. // 0 + 10 (initial prio) - 10 (avg) - 10 (mostest - total) = -10
  461. totalPower := val1VotingPower
  462. wantVal1Prio := 0 + val1VotingPower - totalPower
  463. assert.Equal(t, wantVal1Prio, updatedState.NextValidators.Validators[0].ProposerPriority)
  464. assert.Equal(t, val1PubKey.Address(), updatedState.NextValidators.Proposer.Address)
  465. // add a validator with the same voting power as the first
  466. val2PubKey := ed25519.GenPrivKey().PubKey()
  467. updateAddVal := abci.ValidatorUpdate{PubKey: types.TM2PB.PubKey(val2PubKey), Power: val1VotingPower}
  468. validatorUpdates, err = types.PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{updateAddVal})
  469. assert.NoError(t, err)
  470. updatedState2, err := updateState(updatedState, blockID, &block.Header, abciResponses, validatorUpdates)
  471. assert.NoError(t, err)
  472. require.Equal(t, len(updatedState2.NextValidators.Validators), 2)
  473. assert.Equal(t, updatedState2.Validators, updatedState.NextValidators)
  474. // val1 will still be proposer as val2 just got added:
  475. assert.Equal(t, val1PubKey.Address(), updatedState.NextValidators.Proposer.Address)
  476. assert.Equal(t, updatedState2.Validators.Proposer.Address, updatedState2.NextValidators.Proposer.Address)
  477. assert.Equal(t, updatedState2.Validators.Proposer.Address, val1PubKey.Address())
  478. assert.Equal(t, updatedState2.NextValidators.Proposer.Address, val1PubKey.Address())
  479. _, updatedVal1 := updatedState2.NextValidators.GetByAddress(val1PubKey.Address())
  480. _, oldVal1 := updatedState2.Validators.GetByAddress(val1PubKey.Address())
  481. _, updatedVal2 := updatedState2.NextValidators.GetByAddress(val2PubKey.Address())
  482. // 1. Add
  483. val2VotingPower := val1VotingPower
  484. totalPower = val1VotingPower + val2VotingPower // 20
  485. v2PrioWhenAddedVal2 := -(totalPower + (totalPower >> 3)) // -22
  486. // 2. Scale - noop
  487. // 3. Center
  488. avgSum := big.NewInt(0).Add(big.NewInt(v2PrioWhenAddedVal2), big.NewInt(oldVal1.ProposerPriority))
  489. avg := avgSum.Div(avgSum, big.NewInt(2)) // -11
  490. expectedVal2Prio := v2PrioWhenAddedVal2 - avg.Int64() // -11
  491. expectedVal1Prio := oldVal1.ProposerPriority - avg.Int64() // 11
  492. // 4. Increment
  493. expectedVal2Prio = expectedVal2Prio + val2VotingPower // -11 + 10 = -1
  494. expectedVal1Prio = expectedVal1Prio + val1VotingPower // 11 + 10 == 21
  495. expectedVal1Prio = expectedVal1Prio - totalPower // 1, val1 proposer
  496. assert.EqualValues(t, expectedVal1Prio, updatedVal1.ProposerPriority)
  497. assert.EqualValues(t, expectedVal2Prio, updatedVal2.ProposerPriority, "unexpected proposer priority for validator: %v", updatedVal2)
  498. validatorUpdates, err = types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  499. require.NoError(t, err)
  500. updatedState3, err := updateState(updatedState2, blockID, &block.Header, abciResponses, validatorUpdates)
  501. assert.NoError(t, err)
  502. assert.Equal(t, updatedState3.Validators.Proposer.Address, updatedState3.NextValidators.Proposer.Address)
  503. assert.Equal(t, updatedState3.Validators, updatedState2.NextValidators)
  504. _, updatedVal1 = updatedState3.NextValidators.GetByAddress(val1PubKey.Address())
  505. _, updatedVal2 = updatedState3.NextValidators.GetByAddress(val2PubKey.Address())
  506. // val1 will still be proposer:
  507. assert.Equal(t, val1PubKey.Address(), updatedState3.NextValidators.Proposer.Address)
  508. // check if expected proposer prio is matched:
  509. // Increment
  510. expectedVal2Prio2 := expectedVal2Prio + val2VotingPower // -1 + 10 = 9
  511. expectedVal1Prio2 := expectedVal1Prio + val1VotingPower // 1 + 10 == 11
  512. expectedVal1Prio2 = expectedVal1Prio2 - totalPower // -9, val1 proposer
  513. assert.EqualValues(t, expectedVal1Prio2, updatedVal1.ProposerPriority, "unexpected proposer priority for validator: %v", updatedVal2)
  514. assert.EqualValues(t, expectedVal2Prio2, updatedVal2.ProposerPriority, "unexpected proposer priority for validator: %v", updatedVal2)
  515. // no changes in voting power and both validators have same voting power
  516. // -> proposers should alternate:
  517. oldState := updatedState3
  518. abciResponses = &ABCIResponses{
  519. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  520. }
  521. validatorUpdates, err = types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  522. require.NoError(t, err)
  523. oldState, err = updateState(oldState, blockID, &block.Header, abciResponses, validatorUpdates)
  524. assert.NoError(t, err)
  525. expectedVal1Prio2 = 1
  526. expectedVal2Prio2 = -1
  527. expectedVal1Prio = -9
  528. expectedVal2Prio = 9
  529. for i := 0; i < 1000; i++ {
  530. // no validator updates:
  531. abciResponses := &ABCIResponses{
  532. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  533. }
  534. validatorUpdates, err = types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  535. require.NoError(t, err)
  536. updatedState, err := updateState(oldState, blockID, &block.Header, abciResponses, validatorUpdates)
  537. assert.NoError(t, err)
  538. // alternate (and cyclic priorities):
  539. assert.NotEqual(t, updatedState.Validators.Proposer.Address, updatedState.NextValidators.Proposer.Address, "iter: %v", i)
  540. assert.Equal(t, oldState.Validators.Proposer.Address, updatedState.NextValidators.Proposer.Address, "iter: %v", i)
  541. _, updatedVal1 = updatedState.NextValidators.GetByAddress(val1PubKey.Address())
  542. _, updatedVal2 = updatedState.NextValidators.GetByAddress(val2PubKey.Address())
  543. if i%2 == 0 {
  544. assert.Equal(t, updatedState.Validators.Proposer.Address, val2PubKey.Address())
  545. assert.Equal(t, expectedVal1Prio, updatedVal1.ProposerPriority) // -19
  546. assert.Equal(t, expectedVal2Prio, updatedVal2.ProposerPriority) // 0
  547. } else {
  548. assert.Equal(t, updatedState.Validators.Proposer.Address, val1PubKey.Address())
  549. assert.Equal(t, expectedVal1Prio2, updatedVal1.ProposerPriority) // -9
  550. assert.Equal(t, expectedVal2Prio2, updatedVal2.ProposerPriority) // -10
  551. }
  552. // update for next iteration:
  553. oldState = updatedState
  554. }
  555. }
  556. func TestLargeGenesisValidator(t *testing.T) {
  557. tearDown, _, state := setupTestCase(t)
  558. defer tearDown(t)
  559. genesisVotingPower := int64(types.MaxTotalVotingPower / 1000)
  560. genesisPubKey := ed25519.GenPrivKey().PubKey()
  561. // fmt.Println("genesis addr: ", genesisPubKey.Address())
  562. genesisVal := &types.Validator{Address: genesisPubKey.Address(), PubKey: genesisPubKey, VotingPower: genesisVotingPower}
  563. // reset state validators to above validator
  564. state.Validators = types.NewValidatorSet([]*types.Validator{genesisVal})
  565. state.NextValidators = state.Validators
  566. require.True(t, len(state.Validators.Validators) == 1)
  567. // update state a few times with no validator updates
  568. // asserts that the single validator's ProposerPrio stays the same
  569. oldState := state
  570. for i := 0; i < 10; i++ {
  571. // no updates:
  572. abciResponses := &ABCIResponses{
  573. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  574. }
  575. validatorUpdates, err := types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  576. require.NoError(t, err)
  577. block := makeBlock(oldState, oldState.LastBlockHeight+1)
  578. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  579. updatedState, err := updateState(oldState, blockID, &block.Header, abciResponses, validatorUpdates)
  580. // no changes in voting power (ProposerPrio += VotingPower == Voting in 1st round; than shiftByAvg == 0,
  581. // than -Total == -Voting)
  582. // -> no change in ProposerPrio (stays zero):
  583. assert.EqualValues(t, oldState.NextValidators, updatedState.NextValidators)
  584. assert.EqualValues(t, 0, updatedState.NextValidators.Proposer.ProposerPriority)
  585. oldState = updatedState
  586. }
  587. // add another validator, do a few iterations (create blocks),
  588. // add more validators with same voting power as the 2nd
  589. // let the genesis validator "unbond",
  590. // see how long it takes until the effect wears off and both begin to alternate
  591. // see: https://github.com/tendermint/tendermint/issues/2960
  592. firstAddedValPubKey := ed25519.GenPrivKey().PubKey()
  593. firstAddedValVotingPower := int64(10)
  594. firstAddedVal := abci.ValidatorUpdate{PubKey: types.TM2PB.PubKey(firstAddedValPubKey), Power: firstAddedValVotingPower}
  595. validatorUpdates, err := types.PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{firstAddedVal})
  596. assert.NoError(t, err)
  597. abciResponses := &ABCIResponses{
  598. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{firstAddedVal}},
  599. }
  600. block := makeBlock(oldState, oldState.LastBlockHeight+1)
  601. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  602. updatedState, err := updateState(oldState, blockID, &block.Header, abciResponses, validatorUpdates)
  603. lastState := updatedState
  604. for i := 0; i < 200; i++ {
  605. // no updates:
  606. abciResponses := &ABCIResponses{
  607. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  608. }
  609. validatorUpdates, err := types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  610. require.NoError(t, err)
  611. block := makeBlock(lastState, lastState.LastBlockHeight+1)
  612. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  613. updatedStateInner, err := updateState(lastState, blockID, &block.Header, abciResponses, validatorUpdates)
  614. lastState = updatedStateInner
  615. }
  616. // set state to last state of above iteration
  617. state = lastState
  618. // set oldState to state before above iteration
  619. oldState = updatedState
  620. _, oldGenesisVal := oldState.NextValidators.GetByAddress(genesisVal.Address)
  621. _, newGenesisVal := state.NextValidators.GetByAddress(genesisVal.Address)
  622. _, addedOldVal := oldState.NextValidators.GetByAddress(firstAddedValPubKey.Address())
  623. _, addedNewVal := state.NextValidators.GetByAddress(firstAddedValPubKey.Address())
  624. // expect large negative proposer priority for both (genesis validator decreased, 2nd validator increased):
  625. assert.True(t, oldGenesisVal.ProposerPriority > newGenesisVal.ProposerPriority)
  626. assert.True(t, addedOldVal.ProposerPriority < addedNewVal.ProposerPriority)
  627. // add 10 validators with the same voting power as the one added directly after genesis:
  628. for i := 0; i < 10; i++ {
  629. addedPubKey := ed25519.GenPrivKey().PubKey()
  630. addedVal := abci.ValidatorUpdate{PubKey: types.TM2PB.PubKey(addedPubKey), Power: firstAddedValVotingPower}
  631. validatorUpdates, err := types.PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{addedVal})
  632. assert.NoError(t, err)
  633. abciResponses := &ABCIResponses{
  634. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{addedVal}},
  635. }
  636. block := makeBlock(oldState, oldState.LastBlockHeight+1)
  637. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  638. state, err = updateState(state, blockID, &block.Header, abciResponses, validatorUpdates)
  639. }
  640. require.Equal(t, 10+2, len(state.NextValidators.Validators))
  641. // remove genesis validator:
  642. removeGenesisVal := abci.ValidatorUpdate{PubKey: types.TM2PB.PubKey(genesisPubKey), Power: 0}
  643. abciResponses = &ABCIResponses{
  644. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{removeGenesisVal}},
  645. }
  646. block = makeBlock(oldState, oldState.LastBlockHeight+1)
  647. blockID = types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  648. validatorUpdates, err = types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  649. require.NoError(t, err)
  650. updatedState, err = updateState(state, blockID, &block.Header, abciResponses, validatorUpdates)
  651. require.NoError(t, err)
  652. // only the first added val (not the genesis val) should be left
  653. assert.Equal(t, 11, len(updatedState.NextValidators.Validators))
  654. // call update state until the effect for the 3rd added validator
  655. // being proposer for a long time after the genesis validator left wears off:
  656. curState := updatedState
  657. count := 0
  658. isProposerUnchanged := true
  659. for isProposerUnchanged {
  660. abciResponses := &ABCIResponses{
  661. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  662. }
  663. validatorUpdates, err = types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  664. require.NoError(t, err)
  665. block = makeBlock(curState, curState.LastBlockHeight+1)
  666. blockID = types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  667. curState, err = updateState(curState, blockID, &block.Header, abciResponses, validatorUpdates)
  668. if !bytes.Equal(curState.Validators.Proposer.Address, curState.NextValidators.Proposer.Address) {
  669. isProposerUnchanged = false
  670. }
  671. count++
  672. }
  673. updatedState = curState
  674. // the proposer changes after this number of blocks
  675. firstProposerChangeExpectedAfter := 1
  676. assert.Equal(t, firstProposerChangeExpectedAfter, count)
  677. // store proposers here to see if we see them again in the same order:
  678. numVals := len(updatedState.Validators.Validators)
  679. proposers := make([]*types.Validator, numVals)
  680. for i := 0; i < 100; i++ {
  681. // no updates:
  682. abciResponses := &ABCIResponses{
  683. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  684. }
  685. validatorUpdates, err := types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  686. require.NoError(t, err)
  687. block := makeBlock(updatedState, updatedState.LastBlockHeight+1)
  688. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  689. updatedState, err = updateState(updatedState, blockID, &block.Header, abciResponses, validatorUpdates)
  690. if i > numVals { // expect proposers to cycle through after the first iteration (of numVals blocks):
  691. if proposers[i%numVals] == nil {
  692. proposers[i%numVals] = updatedState.NextValidators.Proposer
  693. } else {
  694. assert.Equal(t, proposers[i%numVals], updatedState.NextValidators.Proposer)
  695. }
  696. }
  697. }
  698. }
  699. func TestStoreLoadValidatorsIncrementsProposerPriority(t *testing.T) {
  700. const valSetSize = 2
  701. tearDown, stateDB, state := setupTestCase(t)
  702. defer tearDown(t)
  703. state.Validators = genValSet(valSetSize)
  704. state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
  705. SaveState(stateDB, state)
  706. nextHeight := state.LastBlockHeight + 1
  707. v0, err := LoadValidators(stateDB, nextHeight)
  708. assert.Nil(t, err)
  709. acc0 := v0.Validators[0].ProposerPriority
  710. v1, err := LoadValidators(stateDB, nextHeight+1)
  711. assert.Nil(t, err)
  712. acc1 := v1.Validators[0].ProposerPriority
  713. assert.NotEqual(t, acc1, acc0, "expected ProposerPriority value to change between heights")
  714. }
  715. // TestValidatorChangesSaveLoad tests saving and loading a validator set with
  716. // changes.
  717. func TestManyValidatorChangesSaveLoad(t *testing.T) {
  718. const valSetSize = 7
  719. tearDown, stateDB, state := setupTestCase(t)
  720. defer tearDown(t)
  721. require.Equal(t, int64(0), state.LastBlockHeight)
  722. state.Validators = genValSet(valSetSize)
  723. state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
  724. SaveState(stateDB, state)
  725. _, valOld := state.Validators.GetByIndex(0)
  726. var pubkeyOld = valOld.PubKey
  727. pubkey := ed25519.GenPrivKey().PubKey()
  728. const height = 1
  729. // Swap the first validator with a new one (validator set size stays the same).
  730. header, blockID, responses := makeHeaderPartsResponsesValPubKeyChange(state, height, pubkey)
  731. // Save state etc.
  732. var err error
  733. var validatorUpdates []*types.Validator
  734. validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.EndBlock.ValidatorUpdates)
  735. require.NoError(t, err)
  736. state, err = updateState(state, blockID, &header, responses, validatorUpdates)
  737. require.Nil(t, err)
  738. nextHeight := state.LastBlockHeight + 1
  739. saveValidatorsInfo(stateDB, nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators)
  740. // Load nextheight, it should be the oldpubkey.
  741. v0, err := LoadValidators(stateDB, nextHeight)
  742. assert.Nil(t, err)
  743. assert.Equal(t, valSetSize, v0.Size())
  744. index, val := v0.GetByAddress(pubkeyOld.Address())
  745. assert.NotNil(t, val)
  746. if index < 0 {
  747. t.Fatal("expected to find old validator")
  748. }
  749. // Load nextheight+1, it should be the new pubkey.
  750. v1, err := LoadValidators(stateDB, nextHeight+1)
  751. assert.Nil(t, err)
  752. assert.Equal(t, valSetSize, v1.Size())
  753. index, val = v1.GetByAddress(pubkey.Address())
  754. assert.NotNil(t, val)
  755. if index < 0 {
  756. t.Fatal("expected to find newly added validator")
  757. }
  758. }
  759. func genValSet(size int) *types.ValidatorSet {
  760. vals := make([]*types.Validator, size)
  761. for i := 0; i < size; i++ {
  762. vals[i] = types.NewValidator(ed25519.GenPrivKey().PubKey(), 10)
  763. }
  764. return types.NewValidatorSet(vals)
  765. }
  766. func TestStateMakeBlock(t *testing.T) {
  767. tearDown, _, state := setupTestCase(t)
  768. defer tearDown(t)
  769. proposerAddress := state.Validators.GetProposer().Address
  770. stateVersion := state.Version.Consensus
  771. block := makeBlock(state, 2)
  772. // test we set some fields
  773. assert.Equal(t, stateVersion, block.Version)
  774. assert.Equal(t, proposerAddress, block.ProposerAddress)
  775. }
  776. // TestConsensusParamsChangesSaveLoad tests saving and loading consensus params
  777. // with changes.
  778. func TestConsensusParamsChangesSaveLoad(t *testing.T) {
  779. tearDown, stateDB, state := setupTestCase(t)
  780. defer tearDown(t)
  781. // Change vals at these heights.
  782. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20}
  783. N := len(changeHeights)
  784. // Each valset is just one validator.
  785. // create list of them.
  786. params := make([]types.ConsensusParams, N+1)
  787. params[0] = state.ConsensusParams
  788. for i := 1; i < N+1; i++ {
  789. params[i] = *types.DefaultConsensusParams()
  790. params[i].BlockSize.MaxBytes += int64(i)
  791. }
  792. // Build the params history by running updateState
  793. // with the right params set for each height.
  794. highestHeight := changeHeights[N-1] + 5
  795. changeIndex := 0
  796. cp := params[changeIndex]
  797. var err error
  798. var validatorUpdates []*types.Validator
  799. for i := int64(1); i < highestHeight; i++ {
  800. // When we get to a change height, use the next params.
  801. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  802. changeIndex++
  803. cp = params[changeIndex]
  804. }
  805. header, blockID, responses := makeHeaderPartsResponsesParams(state, i, cp)
  806. validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.EndBlock.ValidatorUpdates)
  807. require.NoError(t, err)
  808. state, err = updateState(state, blockID, &header, responses, validatorUpdates)
  809. require.Nil(t, err)
  810. nextHeight := state.LastBlockHeight + 1
  811. saveConsensusParamsInfo(stateDB, nextHeight, state.LastHeightConsensusParamsChanged, state.ConsensusParams)
  812. }
  813. // Make all the test cases by using the same params until after the change.
  814. testCases := make([]paramsChangeTestCase, highestHeight)
  815. changeIndex = 0
  816. cp = params[changeIndex]
  817. for i := int64(1); i < highestHeight+1; i++ {
  818. // We get to the height after a change height use the next pubkey (note
  819. // our counter starts at 0 this time).
  820. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  821. changeIndex++
  822. cp = params[changeIndex]
  823. }
  824. testCases[i-1] = paramsChangeTestCase{i, cp}
  825. }
  826. for _, testCase := range testCases {
  827. p, err := LoadConsensusParams(stateDB, testCase.height)
  828. assert.Nil(t, err, fmt.Sprintf("expected no err at height %d", testCase.height))
  829. assert.Equal(t, testCase.params, p, fmt.Sprintf(`unexpected consensus params at
  830. height %d`, testCase.height))
  831. }
  832. }
  833. func makeParams(blockBytes, blockGas, evidenceAge int64) types.ConsensusParams {
  834. return types.ConsensusParams{
  835. BlockSize: types.BlockSizeParams{
  836. MaxBytes: blockBytes,
  837. MaxGas: blockGas,
  838. },
  839. Evidence: types.EvidenceParams{
  840. MaxAge: evidenceAge,
  841. },
  842. }
  843. }
  844. func TestApplyUpdates(t *testing.T) {
  845. initParams := makeParams(1, 2, 3)
  846. cases := [...]struct {
  847. init types.ConsensusParams
  848. updates abci.ConsensusParams
  849. expected types.ConsensusParams
  850. }{
  851. 0: {initParams, abci.ConsensusParams{}, initParams},
  852. 1: {initParams, abci.ConsensusParams{}, initParams},
  853. 2: {initParams,
  854. abci.ConsensusParams{
  855. BlockSize: &abci.BlockSizeParams{
  856. MaxBytes: 44,
  857. MaxGas: 55,
  858. },
  859. },
  860. makeParams(44, 55, 3)},
  861. 3: {initParams,
  862. abci.ConsensusParams{
  863. Evidence: &abci.EvidenceParams{
  864. MaxAge: 66,
  865. },
  866. },
  867. makeParams(1, 2, 66)},
  868. }
  869. for i, tc := range cases {
  870. res := tc.init.Update(&(tc.updates))
  871. assert.Equal(t, tc.expected, res, "case %d", i)
  872. }
  873. }
  874. func makeHeaderPartsResponsesValPubKeyChange(state State, height int64,
  875. pubkey crypto.PubKey) (types.Header, types.BlockID, *ABCIResponses) {
  876. block := makeBlock(state, state.LastBlockHeight+1)
  877. abciResponses := &ABCIResponses{
  878. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  879. }
  880. // If the pubkey is new, remove the old and add the new.
  881. _, val := state.NextValidators.GetByIndex(0)
  882. if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
  883. abciResponses.EndBlock = &abci.ResponseEndBlock{
  884. ValidatorUpdates: []abci.ValidatorUpdate{
  885. types.TM2PB.NewValidatorUpdate(val.PubKey, 0),
  886. types.TM2PB.NewValidatorUpdate(pubkey, 10),
  887. },
  888. }
  889. }
  890. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  891. }
  892. func makeHeaderPartsResponsesValPowerChange(state State, height int64,
  893. power int64) (types.Header, types.BlockID, *ABCIResponses) {
  894. block := makeBlock(state, state.LastBlockHeight+1)
  895. abciResponses := &ABCIResponses{
  896. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  897. }
  898. // If the pubkey is new, remove the old and add the new.
  899. _, val := state.NextValidators.GetByIndex(0)
  900. if val.VotingPower != power {
  901. abciResponses.EndBlock = &abci.ResponseEndBlock{
  902. ValidatorUpdates: []abci.ValidatorUpdate{
  903. types.TM2PB.NewValidatorUpdate(val.PubKey, power),
  904. },
  905. }
  906. }
  907. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  908. }
  909. func makeHeaderPartsResponsesParams(state State, height int64,
  910. params types.ConsensusParams) (types.Header, types.BlockID, *ABCIResponses) {
  911. block := makeBlock(state, state.LastBlockHeight+1)
  912. abciResponses := &ABCIResponses{
  913. EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(&params)},
  914. }
  915. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  916. }
  917. type paramsChangeTestCase struct {
  918. height int64
  919. params types.ConsensusParams
  920. }