GovernorPreventLateQuorumUpgradeable.sol 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0-rc.0) (governance/extensions/GovernorPreventLateQuorum.sol)
  3. pragma solidity ^0.8.0;
  4. import "../GovernorUpgradeable.sol";
  5. import "../../utils/math/MathUpgradeable.sol";
  6. import "../../proxy/utils/Initializable.sol";
  7. /**
  8. * @dev A module that ensures there is a minimum voting period after quorum is reached. This prevents a large voter from
  9. * swaying a vote and triggering quorum at the last minute, by ensuring there is always time for other voters to react
  10. * and try to oppose the decision.
  11. *
  12. * If a vote causes quorum to be reached, the proposal's voting period may be extended so that it does not end before at
  13. * least a given number of blocks have passed (the "vote extension" parameter). This parameter can be set by the
  14. * governance executor (e.g. through a governance proposal).
  15. *
  16. * _Available since v4.5._
  17. */
  18. abstract contract GovernorPreventLateQuorumUpgradeable is Initializable, GovernorUpgradeable {
  19. using SafeCastUpgradeable for uint256;
  20. using TimersUpgradeable for TimersUpgradeable.BlockNumber;
  21. uint64 private _voteExtension;
  22. mapping(uint256 => TimersUpgradeable.BlockNumber) private _extendedDeadlines;
  23. /// @dev Emitted when a proposal deadline is pushed back due to reaching quorum late in its voting period.
  24. event ProposalExtended(uint256 indexed proposalId, uint64 extendedDeadline);
  25. /// @dev Emitted when the {lateQuorumVoteExtension} parameter is changed.
  26. event LateQuorumVoteExtensionSet(uint64 oldVoteExtension, uint64 newVoteExtension);
  27. /**
  28. * @dev Initializes the vote extension parameter: the number of blocks that are required to pass since a proposal
  29. * reaches quorum until its voting period ends. If necessary the voting period will be extended beyond the one set
  30. * at proposal creation.
  31. */
  32. function __GovernorPreventLateQuorum_init(uint64 initialVoteExtension) internal onlyInitializing {
  33. __Context_init_unchained();
  34. __ERC165_init_unchained();
  35. __IGovernor_init_unchained();
  36. __GovernorPreventLateQuorum_init_unchained(initialVoteExtension);
  37. }
  38. function __GovernorPreventLateQuorum_init_unchained(uint64 initialVoteExtension) internal onlyInitializing {
  39. _setLateQuorumVoteExtension(initialVoteExtension);
  40. }
  41. /**
  42. * @dev Returns the proposal deadline, which may have been extended beyond that set at proposal creation, if the
  43. * proposal reached quorum late in the voting period. See {Governor-proposalDeadline}.
  44. */
  45. function proposalDeadline(uint256 proposalId) public view virtual override returns (uint256) {
  46. return MathUpgradeable.max(super.proposalDeadline(proposalId), _extendedDeadlines[proposalId].getDeadline());
  47. }
  48. /**
  49. * @dev Casts a vote and detects if it caused quorum to be reached, potentially extending the voting period. See
  50. * {Governor-_castVote}.
  51. *
  52. * May emit a {ProposalExtended} event.
  53. */
  54. function _castVote(
  55. uint256 proposalId,
  56. address account,
  57. uint8 support,
  58. string memory reason
  59. ) internal virtual override returns (uint256) {
  60. uint256 result = super._castVote(proposalId, account, support, reason);
  61. TimersUpgradeable.BlockNumber storage extendedDeadline = _extendedDeadlines[proposalId];
  62. if (extendedDeadline.isUnset() && _quorumReached(proposalId)) {
  63. uint64 extendedDeadlineValue = block.number.toUint64() + lateQuorumVoteExtension();
  64. if (extendedDeadlineValue > proposalDeadline(proposalId)) {
  65. emit ProposalExtended(proposalId, extendedDeadlineValue);
  66. }
  67. extendedDeadline.setDeadline(extendedDeadlineValue);
  68. }
  69. return result;
  70. }
  71. /**
  72. * @dev Returns the current value of the vote extension parameter: the number of blocks that are required to pass
  73. * from the time a proposal reaches quorum until its voting period ends.
  74. */
  75. function lateQuorumVoteExtension() public view virtual returns (uint64) {
  76. return _voteExtension;
  77. }
  78. /**
  79. * @dev Changes the {lateQuorumVoteExtension}. This operation can only be performed by the governance executor,
  80. * generally through a governance proposal.
  81. *
  82. * Emits a {LateQuorumVoteExtensionSet} event.
  83. */
  84. function setLateQuorumVoteExtension(uint64 newVoteExtension) public virtual onlyGovernance {
  85. _setLateQuorumVoteExtension(newVoteExtension);
  86. }
  87. /**
  88. * @dev Changes the {lateQuorumVoteExtension}. This is an internal function that can be exposed in a public function
  89. * like {setLateQuorumVoteExtension} if another access control mechanism is needed.
  90. *
  91. * Emits a {LateQuorumVoteExtensionSet} event.
  92. */
  93. function _setLateQuorumVoteExtension(uint64 newVoteExtension) internal virtual {
  94. emit LateQuorumVoteExtensionSet(_voteExtension, newVoteExtension);
  95. _voteExtension = newVoteExtension;
  96. }
  97. uint256[48] private __gap;
  98. }