GovernorPreventLateQuorum.sol 4.3 KB

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