GovernorBaseRules.spec 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import "helpers.spec"
  2. import "Governor.helpers.spec"
  3. import "GovernorInvariants.spec"
  4. use invariant proposalStateConsistency
  5. use invariant canceledImplyCreated
  6. use invariant executedImplyCreated
  7. use invariant noBothExecutedAndCanceled
  8. /*
  9. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  10. │ Rule: No double proposition │
  11. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  12. */
  13. rule noDoublePropose(uint256 pId, env e) {
  14. require proposalCreated(pId);
  15. address[] targets; uint256[] values; bytes[] calldatas; string reason;
  16. uint256 result = propose(e, targets, values, calldatas, reason);
  17. assert result != pId, "double proposal";
  18. }
  19. /*
  20. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  21. │ Rule: Once a proposal is created, voteStart, voteEnd and proposer are immutable │
  22. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  23. */
  24. rule immutableFieldsAfterProposalCreation(uint256 pId, env e, method f, calldataarg args)
  25. filtered { f -> !skip(f) }
  26. {
  27. require proposalCreated(pId);
  28. uint256 voteStart = proposalSnapshot(pId);
  29. uint256 voteEnd = proposalDeadline(pId);
  30. address proposer = proposalProposer(pId);
  31. f(e, args);
  32. assert voteStart == proposalSnapshot(pId), "Start date was changed";
  33. assert voteEnd == proposalDeadline(pId), "End date was changed";
  34. assert proposer == proposalProposer(pId), "Proposer was changed";
  35. }
  36. /*
  37. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  38. │ Rule: A user cannot vote twice │
  39. │ │
  40. │ Checked for castVote only. all 3 castVote functions call _castVote, so the completeness of the verification is │
  41. │ counted on the fact that the 3 functions themselves makes no changes, but rather call an internal function to │
  42. │ execute. That means that we do not check those 3 functions directly, however for castVote & castVoteWithReason it │
  43. │ is quite trivial to understand why this is ok. For castVoteBySig we basically assume that the signature referendum │
  44. │ is correct without checking it. We could check each function separately and pass the rule, but that would have │
  45. │ uglyfied the code with no concrete benefit, as it is evident that nothing is happening in the first 2 functions │
  46. │ (calling a view function), and we do not desire to check the signature verification. │
  47. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  48. */
  49. rule noDoubleVoting(uint256 pId, env e, method f)
  50. filtered { f -> voting(f) }
  51. {
  52. address voter;
  53. uint8 support;
  54. bool votedCheck = hasVoted(pId, voter);
  55. helperVoteWithRevert(e, f, pId, voter, support);
  56. assert votedCheck => lastReverted, "double voting occurred";
  57. }
  58. /*
  59. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  60. │ Rule: Voting against a proposal does not count towards quorum. │
  61. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  62. */
  63. rule againstVotesDontCountTowardsQuorum(uint256 pId, env e, method f)
  64. filtered { f -> voting(f) }
  65. {
  66. address voter;
  67. bool quorumReachedBefore = quorumReached(pId);
  68. helperVoteWithRevert(e, f, pId, voter, 0); // support 0 = against
  69. assert quorumReached(pId) == quorumReachedBefore, "quorum must not be reached with an against vote";
  70. }
  71. /*
  72. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  73. │ Rule: A proposal could be executed only if quorum was reached and vote succeeded │
  74. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  75. */
  76. rule executionOnlyIfQuoromReachedAndVoteSucceeded(uint256 pId, env e, method f, calldataarg args)
  77. filtered { f -> !skip(f) }
  78. {
  79. require !isExecuted(pId);
  80. bool quorumReachedBefore = quorumReached(pId);
  81. bool voteSucceededBefore = voteSucceeded(pId);
  82. f(e, args);
  83. assert isExecuted(pId) => (quorumReachedBefore && voteSucceededBefore), "quorum not met or vote not successful";
  84. }
  85. /*
  86. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  87. │ Rule: Voting cannot start at a block number prior to proposal’s creation block number │
  88. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  89. */
  90. rule noStartBeforeCreation(uint256 pId, env e, method f, calldataarg args)
  91. filtered { f -> !skip(f) }
  92. {
  93. require !proposalCreated(pId);
  94. f(e, args);
  95. assert proposalCreated(pId) => proposalSnapshot(pId) >= clock(e), "starts before proposal";
  96. }
  97. /*
  98. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  99. │ Rule: A proposal cannot be executed before it ends │
  100. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  101. */
  102. rule noExecuteBeforeDeadline(uint256 pId, env e, method f, calldataarg args)
  103. filtered { f -> !skip(f) }
  104. {
  105. require !isExecuted(pId);
  106. f(e, args);
  107. assert isExecuted(pId) => proposalDeadline(pId) <= clock(e), "executed before deadline";
  108. }
  109. /*
  110. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  111. │ Invariant: The quorum numerator is always less than or equal to the quorum denominator │
  112. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  113. */
  114. invariant quorumRatioLessThanOne()
  115. quorumNumerator() <= quorumDenominator()
  116. filtered { f -> !skip(f) }
  117. {
  118. preserved {
  119. require quorumNumeratorLength() < max_uint256;
  120. }
  121. }
  122. /*
  123. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  124. │ Rule: All proposal specific (non-view) functions should revert if proposal is executed │
  125. │ │
  126. │ In this rule we show that if a function is executed, i.e. execute() was called on the proposal ID, non of the │
  127. │ proposal specific functions can make changes again. In executedOnlyAfterExecuteFunc we connected the executed │
  128. │ attribute to the execute() function, showing that only execute() can change it, and that it will always change it. │
  129. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  130. */
  131. rule allFunctionsRevertIfExecuted(uint256 pId, env e, method f, calldataarg args) filtered { f ->
  132. !skip(f) &&
  133. f.selector != updateQuorumNumerator(uint256).selector &&
  134. f.selector != updateTimelock(address).selector
  135. } {
  136. require isExecuted(pId);
  137. requireInvariant noBothExecutedAndCanceled(pId);
  138. requireInvariant executedImplyCreated(pId);
  139. helperFunctionsWithRevert(e, f, pId);
  140. assert lastReverted, "Function was not reverted";
  141. }
  142. /*
  143. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  144. │ Rule: All proposal specific (non-view) functions should revert if proposal is canceled │
  145. │ │
  146. │ In this rule we show that if a function is executed, i.e. execute() was called on the proposal ID, non of the │
  147. │ proposal specific functions can make changes again. In executedOnlyAfterExecuteFunc we connected the executed │
  148. │ attribute to the execute() function, showing that only execute() can change it, and that it will always change it. │
  149. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  150. */
  151. rule allFunctionsRevertIfCanceled(uint256 pId, env e, method f, calldataarg args) filtered { f ->
  152. !skip(f) &&
  153. f.selector != updateQuorumNumerator(uint256).selector &&
  154. f.selector != updateTimelock(address).selector
  155. } {
  156. require isCanceled(pId);
  157. requireInvariant noBothExecutedAndCanceled(pId);
  158. requireInvariant canceledImplyCreated(pId);
  159. helperFunctionsWithRevert(e, f, pId);
  160. assert lastReverted, "Function was not reverted";
  161. }
  162. /*
  163. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  164. │ Rule: Update operation are restricted to executor │
  165. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  166. */
  167. rule privilegedUpdate(env e, method f, calldataarg args)
  168. filtered { f -> !skip(f) }
  169. {
  170. address executorBefore = getExecutor();
  171. uint256 quorumNumeratorBefore = quorumNumerator();
  172. address timelockBefore = timelock();
  173. f(e, args);
  174. assert quorumNumerator() != quorumNumeratorBefore => e.msg.sender == executorBefore;
  175. assert timelock() != timelockBefore => e.msg.sender == executorBefore;
  176. }