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.

1071 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
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. require.NoError(t, err)
  581. // no changes in voting power (ProposerPrio += VotingPower == Voting in 1st round; than shiftByAvg == 0,
  582. // than -Total == -Voting)
  583. // -> no change in ProposerPrio (stays zero):
  584. assert.EqualValues(t, oldState.NextValidators, updatedState.NextValidators)
  585. assert.EqualValues(t, 0, updatedState.NextValidators.Proposer.ProposerPriority)
  586. oldState = updatedState
  587. }
  588. // add another validator, do a few iterations (create blocks),
  589. // add more validators with same voting power as the 2nd
  590. // let the genesis validator "unbond",
  591. // see how long it takes until the effect wears off and both begin to alternate
  592. // see: https://github.com/tendermint/tendermint/issues/2960
  593. firstAddedValPubKey := ed25519.GenPrivKey().PubKey()
  594. firstAddedValVotingPower := int64(10)
  595. firstAddedVal := abci.ValidatorUpdate{PubKey: types.TM2PB.PubKey(firstAddedValPubKey), Power: firstAddedValVotingPower}
  596. validatorUpdates, err := types.PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{firstAddedVal})
  597. assert.NoError(t, err)
  598. abciResponses := &ABCIResponses{
  599. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{firstAddedVal}},
  600. }
  601. block := makeBlock(oldState, oldState.LastBlockHeight+1)
  602. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  603. updatedState, err := updateState(oldState, blockID, &block.Header, abciResponses, validatorUpdates)
  604. require.NoError(t, err)
  605. lastState := updatedState
  606. for i := 0; i < 200; i++ {
  607. // no updates:
  608. abciResponses := &ABCIResponses{
  609. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  610. }
  611. validatorUpdates, err := types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  612. require.NoError(t, err)
  613. block := makeBlock(lastState, lastState.LastBlockHeight+1)
  614. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  615. updatedStateInner, err := updateState(lastState, blockID, &block.Header, abciResponses, validatorUpdates)
  616. require.NoError(t, err)
  617. lastState = updatedStateInner
  618. }
  619. // set state to last state of above iteration
  620. state = lastState
  621. // set oldState to state before above iteration
  622. oldState = updatedState
  623. _, oldGenesisVal := oldState.NextValidators.GetByAddress(genesisVal.Address)
  624. _, newGenesisVal := state.NextValidators.GetByAddress(genesisVal.Address)
  625. _, addedOldVal := oldState.NextValidators.GetByAddress(firstAddedValPubKey.Address())
  626. _, addedNewVal := state.NextValidators.GetByAddress(firstAddedValPubKey.Address())
  627. // expect large negative proposer priority for both (genesis validator decreased, 2nd validator increased):
  628. assert.True(t, oldGenesisVal.ProposerPriority > newGenesisVal.ProposerPriority)
  629. assert.True(t, addedOldVal.ProposerPriority < addedNewVal.ProposerPriority)
  630. // add 10 validators with the same voting power as the one added directly after genesis:
  631. for i := 0; i < 10; i++ {
  632. addedPubKey := ed25519.GenPrivKey().PubKey()
  633. addedVal := abci.ValidatorUpdate{PubKey: types.TM2PB.PubKey(addedPubKey), Power: firstAddedValVotingPower}
  634. validatorUpdates, err := types.PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{addedVal})
  635. assert.NoError(t, err)
  636. abciResponses := &ABCIResponses{
  637. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{addedVal}},
  638. }
  639. block := makeBlock(oldState, oldState.LastBlockHeight+1)
  640. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  641. state, err = updateState(state, blockID, &block.Header, abciResponses, validatorUpdates)
  642. require.NoError(t, err)
  643. }
  644. require.Equal(t, 10+2, len(state.NextValidators.Validators))
  645. // remove genesis validator:
  646. removeGenesisVal := abci.ValidatorUpdate{PubKey: types.TM2PB.PubKey(genesisPubKey), Power: 0}
  647. abciResponses = &ABCIResponses{
  648. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{removeGenesisVal}},
  649. }
  650. block = makeBlock(oldState, oldState.LastBlockHeight+1)
  651. blockID = types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  652. validatorUpdates, err = types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  653. require.NoError(t, err)
  654. updatedState, err = updateState(state, blockID, &block.Header, abciResponses, validatorUpdates)
  655. require.NoError(t, err)
  656. // only the first added val (not the genesis val) should be left
  657. assert.Equal(t, 11, len(updatedState.NextValidators.Validators))
  658. // call update state until the effect for the 3rd added validator
  659. // being proposer for a long time after the genesis validator left wears off:
  660. curState := updatedState
  661. count := 0
  662. isProposerUnchanged := true
  663. for isProposerUnchanged {
  664. abciResponses := &ABCIResponses{
  665. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  666. }
  667. validatorUpdates, err = types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  668. require.NoError(t, err)
  669. block = makeBlock(curState, curState.LastBlockHeight+1)
  670. blockID = types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  671. curState, err = updateState(curState, blockID, &block.Header, abciResponses, validatorUpdates)
  672. require.NoError(t, err)
  673. if !bytes.Equal(curState.Validators.Proposer.Address, curState.NextValidators.Proposer.Address) {
  674. isProposerUnchanged = false
  675. }
  676. count++
  677. }
  678. updatedState = curState
  679. // the proposer changes after this number of blocks
  680. firstProposerChangeExpectedAfter := 1
  681. assert.Equal(t, firstProposerChangeExpectedAfter, count)
  682. // store proposers here to see if we see them again in the same order:
  683. numVals := len(updatedState.Validators.Validators)
  684. proposers := make([]*types.Validator, numVals)
  685. for i := 0; i < 100; i++ {
  686. // no updates:
  687. abciResponses := &ABCIResponses{
  688. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  689. }
  690. validatorUpdates, err := types.PB2TM.ValidatorUpdates(abciResponses.EndBlock.ValidatorUpdates)
  691. require.NoError(t, err)
  692. block := makeBlock(updatedState, updatedState.LastBlockHeight+1)
  693. blockID := types.BlockID{block.Hash(), block.MakePartSet(testPartSize).Header()}
  694. updatedState, err = updateState(updatedState, blockID, &block.Header, abciResponses, validatorUpdates)
  695. require.NoError(t, err)
  696. if i > numVals { // expect proposers to cycle through after the first iteration (of numVals blocks):
  697. if proposers[i%numVals] == nil {
  698. proposers[i%numVals] = updatedState.NextValidators.Proposer
  699. } else {
  700. assert.Equal(t, proposers[i%numVals], updatedState.NextValidators.Proposer)
  701. }
  702. }
  703. }
  704. }
  705. func TestStoreLoadValidatorsIncrementsProposerPriority(t *testing.T) {
  706. const valSetSize = 2
  707. tearDown, stateDB, state := setupTestCase(t)
  708. defer tearDown(t)
  709. state.Validators = genValSet(valSetSize)
  710. state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
  711. SaveState(stateDB, state)
  712. nextHeight := state.LastBlockHeight + 1
  713. v0, err := LoadValidators(stateDB, nextHeight)
  714. assert.Nil(t, err)
  715. acc0 := v0.Validators[0].ProposerPriority
  716. v1, err := LoadValidators(stateDB, nextHeight+1)
  717. assert.Nil(t, err)
  718. acc1 := v1.Validators[0].ProposerPriority
  719. assert.NotEqual(t, acc1, acc0, "expected ProposerPriority value to change between heights")
  720. }
  721. // TestValidatorChangesSaveLoad tests saving and loading a validator set with
  722. // changes.
  723. func TestManyValidatorChangesSaveLoad(t *testing.T) {
  724. const valSetSize = 7
  725. tearDown, stateDB, state := setupTestCase(t)
  726. defer tearDown(t)
  727. require.Equal(t, int64(0), state.LastBlockHeight)
  728. state.Validators = genValSet(valSetSize)
  729. state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
  730. SaveState(stateDB, state)
  731. _, valOld := state.Validators.GetByIndex(0)
  732. var pubkeyOld = valOld.PubKey
  733. pubkey := ed25519.GenPrivKey().PubKey()
  734. const height = 1
  735. // Swap the first validator with a new one (validator set size stays the same).
  736. header, blockID, responses := makeHeaderPartsResponsesValPubKeyChange(state, height, pubkey)
  737. // Save state etc.
  738. var err error
  739. var validatorUpdates []*types.Validator
  740. validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.EndBlock.ValidatorUpdates)
  741. require.NoError(t, err)
  742. state, err = updateState(state, blockID, &header, responses, validatorUpdates)
  743. require.Nil(t, err)
  744. nextHeight := state.LastBlockHeight + 1
  745. saveValidatorsInfo(stateDB, nextHeight+1, state.LastHeightValidatorsChanged, state.NextValidators)
  746. // Load nextheight, it should be the oldpubkey.
  747. v0, err := LoadValidators(stateDB, nextHeight)
  748. assert.Nil(t, err)
  749. assert.Equal(t, valSetSize, v0.Size())
  750. index, val := v0.GetByAddress(pubkeyOld.Address())
  751. assert.NotNil(t, val)
  752. if index < 0 {
  753. t.Fatal("expected to find old validator")
  754. }
  755. // Load nextheight+1, it should be the new pubkey.
  756. v1, err := LoadValidators(stateDB, nextHeight+1)
  757. assert.Nil(t, err)
  758. assert.Equal(t, valSetSize, v1.Size())
  759. index, val = v1.GetByAddress(pubkey.Address())
  760. assert.NotNil(t, val)
  761. if index < 0 {
  762. t.Fatal("expected to find newly added validator")
  763. }
  764. }
  765. func genValSet(size int) *types.ValidatorSet {
  766. vals := make([]*types.Validator, size)
  767. for i := 0; i < size; i++ {
  768. vals[i] = types.NewValidator(ed25519.GenPrivKey().PubKey(), 10)
  769. }
  770. return types.NewValidatorSet(vals)
  771. }
  772. func TestStateMakeBlock(t *testing.T) {
  773. tearDown, _, state := setupTestCase(t)
  774. defer tearDown(t)
  775. proposerAddress := state.Validators.GetProposer().Address
  776. stateVersion := state.Version.Consensus
  777. block := makeBlock(state, 2)
  778. // test we set some fields
  779. assert.Equal(t, stateVersion, block.Version)
  780. assert.Equal(t, proposerAddress, block.ProposerAddress)
  781. }
  782. // TestConsensusParamsChangesSaveLoad tests saving and loading consensus params
  783. // with changes.
  784. func TestConsensusParamsChangesSaveLoad(t *testing.T) {
  785. tearDown, stateDB, state := setupTestCase(t)
  786. defer tearDown(t)
  787. // Change vals at these heights.
  788. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20}
  789. N := len(changeHeights)
  790. // Each valset is just one validator.
  791. // create list of them.
  792. params := make([]types.ConsensusParams, N+1)
  793. params[0] = state.ConsensusParams
  794. for i := 1; i < N+1; i++ {
  795. params[i] = *types.DefaultConsensusParams()
  796. params[i].Block.MaxBytes += int64(i)
  797. }
  798. // Build the params history by running updateState
  799. // with the right params set for each height.
  800. highestHeight := changeHeights[N-1] + 5
  801. changeIndex := 0
  802. cp := params[changeIndex]
  803. var err error
  804. var validatorUpdates []*types.Validator
  805. for i := int64(1); i < highestHeight; i++ {
  806. // When we get to a change height, use the next params.
  807. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] {
  808. changeIndex++
  809. cp = params[changeIndex]
  810. }
  811. header, blockID, responses := makeHeaderPartsResponsesParams(state, i, cp)
  812. validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.EndBlock.ValidatorUpdates)
  813. require.NoError(t, err)
  814. state, err = updateState(state, blockID, &header, responses, validatorUpdates)
  815. require.Nil(t, err)
  816. nextHeight := state.LastBlockHeight + 1
  817. saveConsensusParamsInfo(stateDB, nextHeight, state.LastHeightConsensusParamsChanged, state.ConsensusParams)
  818. }
  819. // Make all the test cases by using the same params until after the change.
  820. testCases := make([]paramsChangeTestCase, highestHeight)
  821. changeIndex = 0
  822. cp = params[changeIndex]
  823. for i := int64(1); i < highestHeight+1; i++ {
  824. // We get to the height after a change height use the next pubkey (note
  825. // our counter starts at 0 this time).
  826. if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 {
  827. changeIndex++
  828. cp = params[changeIndex]
  829. }
  830. testCases[i-1] = paramsChangeTestCase{i, cp}
  831. }
  832. for _, testCase := range testCases {
  833. p, err := LoadConsensusParams(stateDB, testCase.height)
  834. assert.Nil(t, err, fmt.Sprintf("expected no err at height %d", testCase.height))
  835. assert.Equal(t, testCase.params, p, fmt.Sprintf(`unexpected consensus params at
  836. height %d`, testCase.height))
  837. }
  838. }
  839. func makeParams(
  840. blockBytes, blockGas int64,
  841. blockTimeIotaMs int64,
  842. evidenceAge int64,
  843. ) types.ConsensusParams {
  844. return types.ConsensusParams{
  845. Block: types.BlockParams{
  846. MaxBytes: blockBytes,
  847. MaxGas: blockGas,
  848. TimeIotaMs: blockTimeIotaMs,
  849. },
  850. Evidence: types.EvidenceParams{
  851. MaxAge: evidenceAge,
  852. },
  853. }
  854. }
  855. func TestApplyUpdates(t *testing.T) {
  856. initParams := makeParams(1, 2, 3, 4)
  857. cases := [...]struct {
  858. init types.ConsensusParams
  859. updates abci.ConsensusParams
  860. expected types.ConsensusParams
  861. }{
  862. 0: {initParams, abci.ConsensusParams{}, initParams},
  863. 1: {initParams, abci.ConsensusParams{}, initParams},
  864. 2: {initParams,
  865. abci.ConsensusParams{
  866. Block: &abci.BlockParams{
  867. MaxBytes: 44,
  868. MaxGas: 55,
  869. },
  870. },
  871. makeParams(44, 55, 3, 4)},
  872. 3: {initParams,
  873. abci.ConsensusParams{
  874. Evidence: &abci.EvidenceParams{
  875. MaxAge: 66,
  876. },
  877. },
  878. makeParams(1, 2, 3, 66)},
  879. }
  880. for i, tc := range cases {
  881. res := tc.init.Update(&(tc.updates))
  882. assert.Equal(t, tc.expected, res, "case %d", i)
  883. }
  884. }
  885. func makeHeaderPartsResponsesValPubKeyChange(state State, height int64,
  886. pubkey crypto.PubKey) (types.Header, types.BlockID, *ABCIResponses) {
  887. block := makeBlock(state, state.LastBlockHeight+1)
  888. abciResponses := &ABCIResponses{
  889. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  890. }
  891. // If the pubkey is new, remove the old and add the new.
  892. _, val := state.NextValidators.GetByIndex(0)
  893. if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
  894. abciResponses.EndBlock = &abci.ResponseEndBlock{
  895. ValidatorUpdates: []abci.ValidatorUpdate{
  896. types.TM2PB.NewValidatorUpdate(val.PubKey, 0),
  897. types.TM2PB.NewValidatorUpdate(pubkey, 10),
  898. },
  899. }
  900. }
  901. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  902. }
  903. func makeHeaderPartsResponsesValPowerChange(state State, height int64,
  904. power int64) (types.Header, types.BlockID, *ABCIResponses) {
  905. block := makeBlock(state, state.LastBlockHeight+1)
  906. abciResponses := &ABCIResponses{
  907. EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
  908. }
  909. // If the pubkey is new, remove the old and add the new.
  910. _, val := state.NextValidators.GetByIndex(0)
  911. if val.VotingPower != power {
  912. abciResponses.EndBlock = &abci.ResponseEndBlock{
  913. ValidatorUpdates: []abci.ValidatorUpdate{
  914. types.TM2PB.NewValidatorUpdate(val.PubKey, power),
  915. },
  916. }
  917. }
  918. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  919. }
  920. func makeHeaderPartsResponsesParams(state State, height int64,
  921. params types.ConsensusParams) (types.Header, types.BlockID, *ABCIResponses) {
  922. block := makeBlock(state, state.LastBlockHeight+1)
  923. abciResponses := &ABCIResponses{
  924. EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(&params)},
  925. }
  926. return block.Header, types.BlockID{block.Hash(), types.PartSetHeader{}}, abciResponses
  927. }
  928. type paramsChangeTestCase struct {
  929. height int64
  930. params types.ConsensusParams
  931. }