GovernorBaseRules.spec 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import "methods/IGovernor.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 arg) {
  25. require proposalCreated(pId);
  26. uint256 voteStart = proposalSnapshot(pId);
  27. uint256 voteEnd = proposalDeadline(pId);
  28. address proposer = proposalProposer(pId);
  29. f(e, arg);
  30. assert voteStart == proposalSnapshot(pId), "Start date was changed";
  31. assert voteEnd == proposalDeadline(pId), "End date was changed";
  32. assert proposer == proposalProposer(pId), "Proposer was changed";
  33. }
  34. /*
  35. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  36. │ Rule: A user cannot vote twice │
  37. │ │
  38. │ Checked for castVote only. all 3 castVote functions call _castVote, so the completeness of the verification is │
  39. │ counted on the fact that the 3 functions themselves makes no changes, but rather call an internal function to │
  40. │ execute. That means that we do not check those 3 functions directly, however for castVote & castVoteWithReason it │
  41. │ is quite trivial to understand why this is ok. For castVoteBySig we basically assume that the signature referendum │
  42. │ is correct without checking it. We could check each function separately and pass the rule, but that would have │
  43. │ uglyfied the code with no concrete benefit, as it is evident that nothing is happening in the first 2 functions │
  44. │ (calling a view function), and we do not desire to check the signature verification. │
  45. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  46. */
  47. rule noDoubleVoting(uint256 pId, env e, uint8 sup) {
  48. bool votedCheck = hasVoted(pId, e.msg.sender);
  49. castVote@withrevert(e, pId, sup);
  50. assert votedCheck => lastReverted, "double voting occurred";
  51. }
  52. /*
  53. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  54. │ Rule: A proposal could be executed only if quorum was reached and vote succeeded │
  55. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  56. */
  57. rule executionOnlyIfQuoromReachedAndVoteSucceeded(uint256 pId, env e, method f, calldataarg args) {
  58. require !isExecuted(pId);
  59. bool quorumReachedBefore = quorumReached(pId);
  60. bool voteSucceededBefore = voteSucceeded(pId);
  61. f(e, args);
  62. assert isExecuted(pId) => (quorumReachedBefore && voteSucceededBefore), "quorum not met or vote not successful";
  63. }
  64. /*
  65. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  66. │ Rule: Voting cannot start at a block number prior to proposal’s creation block number │
  67. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  68. */
  69. rule noStartBeforeCreation(uint256 pId, env e, method f, calldataarg args){
  70. require !proposalCreated(pId);
  71. f(e, args);
  72. assert proposalCreated(pId) => proposalSnapshot(pId) >= clock(e), "starts before proposal";
  73. }
  74. /*
  75. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  76. │ Rule: A proposal cannot be executed before it ends │
  77. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  78. */
  79. rule noExecuteBeforeDeadline(uint256 pId, env e, method f, calldataarg args) {
  80. require !isExecuted(pId);
  81. f(e, args);
  82. assert isExecuted(pId) => proposalDeadline(pId) <= clock(e), "executed before deadline";
  83. }
  84. /*
  85. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  86. │ Rule: All proposal specific (non-view) functions should revert if proposal is executed │
  87. │ │
  88. │ In this rule we show that if a function is executed, i.e. execute() was called on the proposal ID, non of the │
  89. │ proposal specific functions can make changes again. In executedOnlyAfterExecuteFunc we connected the executed │
  90. │ attribute to the execute() function, showing that only execute() can change it, and that it will always change it. │
  91. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  92. */
  93. rule allFunctionsRevertIfExecuted(uint256 pId, env e, method f, calldataarg args)
  94. filtered { f -> !skip(f) }
  95. {
  96. require isExecuted(pId);
  97. requireInvariant noBothExecutedAndCanceled(pId);
  98. requireInvariant executedImplyCreated(pId);
  99. helperFunctionsWithRevert(e, f, pId);
  100. assert lastReverted, "Function was not reverted";
  101. }
  102. /*
  103. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  104. │ Rule: All proposal specific (non-view) functions should revert if proposal is canceled │
  105. │ │
  106. │ In this rule we show that if a function is executed, i.e. execute() was called on the proposal ID, non of the │
  107. │ proposal specific functions can make changes again. In executedOnlyAfterExecuteFunc we connected the executed │
  108. │ attribute to the execute() function, showing that only execute() can change it, and that it will always change it. │
  109. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  110. */
  111. rule allFunctionsRevertIfCanceled(uint256 pId, env e, method f, calldataarg args)
  112. filtered { f -> !skip(f) }
  113. {
  114. require isCanceled(pId);
  115. requireInvariant noBothExecutedAndCanceled(pId);
  116. requireInvariant canceledImplyCreated(pId);
  117. helperFunctionsWithRevert(e, f, pId);
  118. assert lastReverted, "Function was not reverted";
  119. }
  120. /*
  121. ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  122. │ Rule: Proposal can be switched state only by specific functions │
  123. └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
  124. */
  125. rule stateOnlyAfterFunc(uint256 pId, env e, method f) {
  126. bool createdBefore = proposalCreated(pId);
  127. bool executedBefore = isExecuted(pId);
  128. bool canceledBefore = isCanceled(pId);
  129. helperFunctionsWithRevert(e, f, pId);
  130. assert (proposalCreated(pId) != createdBefore) => (
  131. createdBefore == false &&
  132. f.selector == propose(address[], uint256[], bytes[], string).selector
  133. ), "proposalCreated only changes in the propose method";
  134. assert (isExecuted(pId) != executedBefore) => (
  135. executedBefore == false &&
  136. f.selector == execute(address[], uint256[], bytes[], bytes32).selector
  137. ), "isExecuted only changes in the execute method";
  138. assert (isCanceled(pId) != canceledBefore) => (
  139. canceledBefore == false &&
  140. f.selector == cancel(address[], uint256[], bytes[], bytes32).selector
  141. ), "isCanceled only changes in the cancel method";
  142. }