GovernorPreventLateQuorum.spec 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import "GovernorCountingSimple.spec"
  2. using ERC20VotesHarness as token
  3. /***
  4. ## Verification of `GovernorPreventLateQuorum`
  5. `GovernorPreventLateQuorum` extends the Governor group of contracts to add the
  6. feature of giving voters more time to vote in the case that a proposal reaches
  7. quorum with less than `voteExtension` amount of time left to vote.
  8. ### Assumptions and Simplifications
  9. None
  10. #### Harnessing
  11. - The contract that the specification was verified against is
  12. `GovernorPreventLateQuorumHarness`, which inherits from all of the Governor
  13. contracts — excluding Compound variations — and implements a number of view
  14. functions to gain access to values that are impossible/difficult to access in
  15. CVL. It also implements all of the required functions not implemented in the
  16. abstract contracts it inherits from.
  17. - `_castVote` was overridden to add an additional flag before calling the parent
  18. version. This flag stores the `block.number` in a variable
  19. `latestCastVoteCall` and is used as a way to check when any of variations of
  20. `castVote` are called.
  21. #### Munging
  22. - Various variables' visibility was changed from private to internal or from
  23. internal to public throughout the Governor contracts in order to make them
  24. accessible in the spec.
  25. - Arbitrary low level calls are assumed to change nothing and thus the function
  26. `_execute` is changed to do nothing. The tool normally havocs in this
  27. situation, assuming all storage can change due to possible reentrancy. We
  28. assume, however, there is no risk of reentrancy because `_execute` is a
  29. protected call locked behind the timelocked governance vote. All other
  30. governance functions are verified separately.
  31. */
  32. methods {
  33. // summarized
  34. hashProposal(address[],uint256[],bytes[],bytes32) returns (uint256) => NONDET
  35. _hashTypedDataV4(bytes32) returns (bytes32)
  36. // envfree
  37. quorumNumerator(uint256) returns uint256
  38. quorumDenominator() returns uint256 envfree
  39. votingPeriod() returns uint256 envfree
  40. lateQuorumVoteExtension() returns uint64 envfree
  41. propose(address[], uint256[], bytes[], string)
  42. // harness
  43. getDeprecatedQuorumNumerator() returns uint256 envfree
  44. getQuorumNumeratorLength() returns uint256 envfree
  45. getQuorumNumeratorLatest() returns uint256 envfree
  46. getExtendedDeadlineIsUnset(uint256) returns bool envfree
  47. getExtendedDeadlineIsStarted(uint256) returns bool envfree
  48. getExtendedDeadline(uint256) returns uint64 envfree
  49. getAgainstVotes(uint256) returns uint256 envfree
  50. getAbstainVotes(uint256) returns uint256 envfree
  51. getForVotes(uint256) returns uint256 envfree
  52. getPastTotalSupply(uint256) returns (uint256) envfree
  53. // more robust check than f.selector == _castVote(...).selector
  54. latestCastVoteCall() returns uint256 envfree
  55. // timelock dispatch
  56. getMinDelay() returns uint256 => DISPATCHER(true)
  57. hashOperationBatch(address[], uint256[], bytes[], bytes32, bytes32) => DISPATCHER(true)
  58. executeBatch(address[], uint256[], bytes[], bytes32, bytes32) => CONSTANT
  59. scheduleBatch(address[], uint256[], bytes[], bytes32, bytes32, uint256) => CONSTANT
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////
  62. // Helper Functions //
  63. ////////////////////////////////////////////////////////////////////////////////
  64. function helperFunctionsWithRevertOnlyCastVote(uint256 proposalId, method f, env e) {
  65. string reason; uint8 support; uint8 v; bytes32 r; bytes32 s; bytes params;
  66. if (f.selector == castVoteBySig(uint256, uint8,uint8, bytes32, bytes32).selector) {
  67. castVoteBySig@withrevert(e, proposalId, support, v, r, s);
  68. } else {
  69. calldataarg args;
  70. f@withrevert(e, args);
  71. }
  72. }
  73. /// Restricting out common reasons why rules break. We assume quorum length won't overflow (uint256) and that functions
  74. /// called in env `e2` have a `block.number` greater than or equal `e1`'s `block.number`.
  75. function setup(env e1, env e2) {
  76. require getQuorumNumeratorLength() + 1 < max_uint;
  77. require e2.block.number >= e1.block.number;
  78. }
  79. ////////////////////////////////////////////////////////////////////////////////
  80. //// #### Definitions //
  81. ////////////////////////////////////////////////////////////////////////////////
  82. /// The proposal with proposal id `pId` has a deadline which is extendable.
  83. definition deadlineExtendable(env e, uint256 pId) returns bool =
  84. getExtendedDeadlineIsUnset(pId) // deadline == 0
  85. && !quorumReached(e, pId);
  86. /// The proposal with proposal id `pId` has a deadline which has been extended.
  87. definition deadlineExtended(env e, uint256 pId) returns bool =
  88. getExtendedDeadlineIsStarted(pId) // deadline > 0
  89. && quorumReached(e, pId);
  90. /// The proposal with proposal id `pId` has not been created.
  91. definition proposalNotCreated(env e, uint256 pId) returns bool =
  92. proposalSnapshot(pId) == 0
  93. && proposalDeadline(pId) == 0
  94. && getAgainstVotes(pId) == 0
  95. && getAbstainVotes(pId) == 0
  96. && getForVotes(pId) == 0;
  97. /// Method f is a version of `castVote` whose state changing effects are covered by `castVoteBySig`.
  98. /// @dev castVoteBySig allows anyone to cast a vote for anyone else if they can supply the signature. Specifically,
  99. /// it covers the case where the msg.sender supplies a signature for themselves which is normally done using the normal
  100. /// `castVote`.
  101. definition castVoteSubset(method f) returns bool =
  102. f.selector == castVote(uint256, uint8).selector ||
  103. f.selector == castVoteWithReason(uint256, uint8, string).selector ||
  104. f.selector == castVoteWithReasonAndParamsBySig(uint256,uint8,string,bytes,uint8,bytes32,bytes32).selector ||
  105. f.selector == castVoteWithReasonAndParams(uint256,uint8,string,bytes).selector;
  106. ////////////////////////////////////////////////////////////////////////////////
  107. //// ### Properties //
  108. ////////////////////////////////////////////////////////////////////////////////
  109. ////////////////////////////////////////////////////////////////////////////////
  110. // Invariants //
  111. ////////////////////////////////////////////////////////////////////////////////
  112. /**
  113. * If a proposal has reached quorum then the proposal snapshot (start `block.number`) must be non-zero
  114. */
  115. invariant quorumReachedEffect(env e1, uint256 pId)
  116. quorumReached(e1, pId) && getPastTotalSupply(0) > 0 => proposalCreated(pId) // bug: 0 supply 0 votes => quorumReached
  117. // relay havocs external contracts, changing pastTotalSupply and thus quorumReached
  118. filtered { f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector }
  119. {
  120. preserved with (env e2) {
  121. setup(e1, e2);
  122. }
  123. }
  124. /**
  125. * A created proposal must be in state `deadlineExtendable` or `deadlineExtended`.
  126. */
  127. invariant proposalInOneState(env e1, uint256 pId)
  128. getPastTotalSupply(0) > 0 => (proposalNotCreated(e1, pId) || deadlineExtendable(e1, pId) || deadlineExtended(e1, pId))
  129. filtered { f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector }
  130. {
  131. preserved with (env e2) {
  132. require proposalCreated(pId);
  133. setup(e1, e2);
  134. }
  135. }
  136. /**
  137. * The quorum numerator is always less than or equal to the quorum denominator.
  138. */
  139. invariant quorumNumerLTEDenom(env e1, uint256 blockNumber)
  140. quorumNumerator(e1, blockNumber) <= quorumDenominator()
  141. {
  142. preserved with (env e2) {
  143. setup(e1, e2);
  144. }
  145. }
  146. /**
  147. * The deprecated quorum numerator variable `_quorumNumerator` is always 0 in a contract that is not upgraded.
  148. */
  149. invariant deprecatedQuorumStateIsUninitialized()
  150. getDeprecatedQuorumNumerator() == 0
  151. //////////////////////////////////////////////////////////////////////////////
  152. // Rules //
  153. //////////////////////////////////////////////////////////////////////////////
  154. /**
  155. * `updateQuorumNumerator` can only change quorum requirements for future proposals.
  156. * @dev In the case that the array containing past quorum numerators overflows, this rule will fail.
  157. */
  158. rule quorumReachedCantChange(method f) filtered { f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector } {
  159. env e1; uint256 pId;
  160. bool _quorumReached = quorumReached(e1, pId);
  161. env e2; uint256 newQuorumNumerator;
  162. setup(e1, e2);
  163. updateQuorumNumerator(e2, newQuorumNumerator);
  164. env e3;
  165. bool quorumReached_ = quorumReached(e3, pId);
  166. assert _quorumReached == quorumReached_, "function changed quorumReached";
  167. }
  168. ///////////////////////////// #### first set of rules ////////////////////////
  169. //// The rules [`deadlineChangeEffects`](#deadlineChangeEffects) and [`deadlineCantBeUnextended`](#deadlineCantBeUnextended)
  170. //// are assumed in rule [`canExtendDeadlineOnce`](#canExtendDeadlineOnce), so we prove them first.
  171. /**
  172. * If deadline increases then we are in `deadlineExtended` state and `castVote`
  173. * was called.
  174. */
  175. rule deadlineChangeEffects(method f) filtered { f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector } {
  176. env e; calldataarg args; uint256 pId;
  177. requireInvariant quorumReachedEffect(e, pId);
  178. uint256 deadlineBefore = proposalDeadline(pId);
  179. f(e, args);
  180. uint256 deadlineAfter = proposalDeadline(pId);
  181. assert(deadlineAfter > deadlineBefore => latestCastVoteCall() == e.block.number && deadlineExtended(e, pId));
  182. }
  183. /**
  184. * @title Deadline can't be unextended
  185. * @notice A proposal can't leave `deadlineExtended` state.
  186. */
  187. rule deadlineCantBeUnextended(method f) filtered { f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector } {
  188. env e1; env e2; env e3; env e4; calldataarg args; uint256 pId;
  189. setup(e1, e2);
  190. require(deadlineExtended(e1, pId));
  191. requireInvariant quorumReachedEffect(e1, pId);
  192. f(e2, args);
  193. assert(deadlineExtended(e1, pId));
  194. }
  195. /**
  196. * A proposal's deadline can't change in `deadlineExtended` state.
  197. */
  198. rule canExtendDeadlineOnce(method f) filtered {f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector} {
  199. env e1; env e2; calldataarg args; uint256 pId;
  200. require(deadlineExtended(e1, pId));
  201. require(proposalSnapshot(pId) > 0);
  202. requireInvariant quorumReachedEffect(e1, pId);
  203. setup(e1, e2);
  204. uint256 deadlineBefore = proposalDeadline(pId);
  205. f(e2, args);
  206. uint256 deadlineAfter = proposalDeadline(pId);
  207. assert(deadlineBefore == deadlineAfter, "deadline can not be extended twice");
  208. }
  209. /////////////////////// #### second set of rules ////////////////////////////
  210. //// The main rule in this section is [the deadline can only be extended if quorum reached with <= `timeOfExtension` left to vote](#deadlineExtnededIfQuorumReached)
  211. //// The other rules of this section are assumed in the proof, so we prove them
  212. //// first.
  213. /**
  214. * A change in `hasVoted` must be correlated with an increasing of the vote
  215. * supports, i.e. casting a vote increases the total number of votes.
  216. */
  217. rule hasVotedCorrelationNonzero(uint256 pId, method f, env e) filtered {f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector} {
  218. address acc = e.msg.sender;
  219. require(getVotes(e, acc, proposalSnapshot(pId)) > 0); // assuming voter has non-zero voting power
  220. uint256 againstBefore = votesAgainst();
  221. uint256 forBefore = votesFor();
  222. uint256 abstainBefore = votesAbstain();
  223. bool hasVotedBefore = hasVoted(e, pId, acc);
  224. helperFunctionsWithRevertOnlyCastVote(pId, f, e); // should be f(e, args)
  225. uint256 againstAfter = votesAgainst();
  226. uint256 forAfter = votesFor();
  227. uint256 abstainAfter = votesAbstain();
  228. bool hasVotedAfter = hasVoted(e, pId, acc);
  229. // want all vote categories to not decrease and at least one category to increase
  230. assert
  231. (!hasVotedBefore && hasVotedAfter) =>
  232. (againstBefore <= againstAfter && forBefore <= forAfter && abstainBefore <= abstainAfter),
  233. "after a vote is cast, the number of votes for each category must not decrease"; // currently vacous but keeping for CI tests
  234. assert
  235. (!hasVotedBefore && hasVotedAfter) =>
  236. (againstBefore < againstAfter || forBefore < forAfter || abstainBefore < abstainAfter),
  237. "after a vote is cast, the number of votes of at least one category must increase";
  238. }
  239. /**
  240. * @title Against votes don't count
  241. * @notice An against vote does not make a proposal reach quorum.
  242. */
  243. rule againstVotesDontCount(method f) filtered { f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector } {
  244. env e; calldataarg args; uint256 pId;
  245. address acc = e.msg.sender;
  246. bool quorumBefore = quorumReached(e, pId);
  247. uint256 againstBefore = votesAgainst();
  248. f(e, args);
  249. bool quorumAfter = quorumReached(e, pId);
  250. uint256 againstAfter = votesAgainst();
  251. assert (againstBefore < againstAfter) => quorumBefore == quorumAfter, "quorum must not be reached with an against vote";
  252. }
  253. /**
  254. * Deadline can only be extended from a `deadlineExtendible` state with quorum being reached with <= `lateQuorumVoteExtension` time left to vote
  255. */
  256. // not reasonable rule since tool can arbitrarily pick a pre-state where quorum is reached
  257. // rule deadlineExtendedIfQuorumReached(method f) filtered { f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector } {
  258. // env e; calldataarg args; uint256 pId;
  259. // requireInvariant proposalInOneState(e, pId);
  260. // requireInvariant quorumReachedEffect(e, pId);
  261. // require proposalCreated(pId);
  262. // require getPastTotalSupply(proposalSnapshot(pId)) >= 100;
  263. // require quorumNumerator(e, proposalSnapshot(pId)) > 0;
  264. // bool wasDeadlineExtendable = deadlineExtendable(e, pId);
  265. // uint64 extension = lateQuorumVoteExtension();
  266. // uint256 deadlineBefore = proposalDeadline(pId);
  267. // f(e, args);
  268. // uint256 deadlineAfter = proposalDeadline(pId);
  269. // assert deadlineAfter > deadlineBefore => wasDeadlineExtendable, "deadline must have been extendable for the deadline to be extended";
  270. // assert deadlineAfter > deadlineBefore => deadlineBefore - e.block.number <= extension, "deadline extension should not be used";
  271. // }
  272. /**
  273. * `extendedDeadlineField` is set if and only if `_castVote` is called and quorum is reached.
  274. */
  275. // tool picks a state where quorum is unreached but extendedDeadline is set and then casts a vote which causes quorum
  276. // to be reached, so the rule breaks. Need to write a rule that says that if quorum is unreached, then extendedDeadline
  277. // must be unset.
  278. // rule extendedDeadlineValueSetIfQuorumReached(method f) filtered { f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector } {
  279. // env e; calldataarg args; uint256 pId;
  280. // setup(e, e);
  281. // requireInvariant proposalInOneState(e, pId);
  282. // require lateQuorumVoteExtension() + e.block.number < max_uint64;
  283. // bool extendedBefore = deadlineExtended(e, pId);
  284. // f(e, args);
  285. // bool extendedAfter = deadlineExtended(e, pId);
  286. // uint256 extDeadline = getExtendedDeadline(pId);
  287. // assert(
  288. // !extendedBefore && extendedAfter
  289. // => extDeadline == e.block.number + lateQuorumVoteExtension(),
  290. // "extended deadline was not set"
  291. // );
  292. // }
  293. /**
  294. * Deadline can never be reduced.
  295. */
  296. rule deadlineNeverReduced(method f) filtered { f -> !f.isFallback && !f.isView && !castVoteSubset(f) && f.selector != relay(address,uint256,bytes).selector } {
  297. env e1; env e2; calldataarg args; uint256 pId;
  298. requireInvariant quorumReachedEffect(e1, pId);
  299. require proposalCreated(pId);
  300. setup(e1, e2);
  301. uint256 deadlineBefore = proposalDeadline(pId);
  302. f(e2, args);
  303. uint256 deadlineAfter = proposalDeadline(pId);
  304. assert(deadlineAfter >= deadlineBefore);
  305. }