GovernorStates.spec 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import "helpers/helpers.spec"
  2. import "helpers/Governor.helpers.spec"
  3. import "GovernorInvariants.spec"
  4. use invariant proposalStateConsistency
  5. use invariant votesImplySnapshotPassed
  6. /*
  7. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  8. โ”‚ Rule: state returns one of the value in the enumeration โ”‚
  9. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  10. */
  11. rule stateConsistency(env e, uint256 pId) {
  12. uint8 result = state(e, pId);
  13. assert (
  14. result == PENDING() ||
  15. result == ACTIVE() ||
  16. result == CANCELED() ||
  17. result == DEFEATED() ||
  18. result == SUCCEEDED() ||
  19. result == QUEUED() ||
  20. result == EXECUTED()
  21. );
  22. }
  23. /*
  24. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  25. โ”‚ Rule: State transitions caused by function calls โ”‚
  26. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  27. */
  28. /// Previous version that results in the prover timing out.
  29. // rule stateTransitionFn(uint256 pId, env e, method f, calldataarg args)
  30. // filtered { f -> !assumedSafe(f) }
  31. // {
  32. // require clockSanity(e);
  33. // require quorumNumeratorLength() < max_uint256; // sanity
  34. //
  35. // uint8 stateBefore = state(e, pId);
  36. // f(e, args);
  37. // uint8 stateAfter = state(e, pId);
  38. //
  39. // assert (stateBefore != stateAfter) => (
  40. // (stateBefore == UNSET() && stateAfter == PENDING() && f.selector == propose(address[],uint256[],bytes[],string).selector ) ||
  41. // (stateBefore == PENDING() && stateAfter == CANCELED() && f.selector == cancel(address[],uint256[],bytes[],bytes32).selector ) ||
  42. // (stateBefore == SUCCEEDED() && stateAfter == QUEUED() && f.selector == queue(address[],uint256[],bytes[],bytes32).selector ) ||
  43. // (stateBefore == SUCCEEDED() && stateAfter == EXECUTED() && f.selector == execute(address[],uint256[],bytes[],bytes32).selector) ||
  44. // (stateBefore == QUEUED() && stateAfter == EXECUTED() && f.selector == execute(address[],uint256[],bytes[],bytes32).selector)
  45. // );
  46. // }
  47. function stateTransitionFnHelper(method f, uint8 s) returns uint8 {
  48. uint256 pId; env e; calldataarg args;
  49. require clockSanity(e);
  50. require quorumNumeratorLength() < max_uint256; // sanity
  51. require state(e, pId) == s; // constrain state before
  52. f(e, args);
  53. require state(e, pId) != s; // constrain state after
  54. return state(e, pId);
  55. }
  56. rule stateTransitionFn_PENDING(method f) filtered { f -> !assumedSafe(f) } {
  57. uint8 stateAfter = stateTransitionFnHelper(f, PENDING());
  58. assert stateAfter == CANCELED() && f.selector == cancel(address[],uint256[],bytes[],bytes32).selector;
  59. }
  60. rule stateTransitionFn_ACTIVE(method f) filtered { f -> !assumedSafe(f) } {
  61. uint8 stateAfter = stateTransitionFnHelper(f, ACTIVE());
  62. assert false;
  63. }
  64. rule stateTransitionFn_CANCELED(method f) filtered { f -> !assumedSafe(f) } {
  65. uint8 stateAfter = stateTransitionFnHelper(f, CANCELED());
  66. assert false;
  67. }
  68. rule stateTransitionFn_DEFEATED(method f) filtered { f -> !assumedSafe(f) } {
  69. uint8 stateAfter = stateTransitionFnHelper(f, DEFEATED());
  70. assert false;
  71. }
  72. rule stateTransitionFn_SUCCEEDED(method f) filtered { f -> !assumedSafe(f) } {
  73. uint8 stateAfter = stateTransitionFnHelper(f, SUCCEEDED());
  74. assert (
  75. (stateAfter == QUEUED() && f.selector == queue(address[],uint256[],bytes[],bytes32).selector) ||
  76. (stateAfter == EXECUTED() && f.selector == execute(address[],uint256[],bytes[],bytes32).selector)
  77. );
  78. }
  79. rule stateTransitionFn_QUEUED(method f) filtered { f -> !assumedSafe(f) } {
  80. uint8 stateAfter = stateTransitionFnHelper(f, QUEUED());
  81. assert state(e, pId) == EXECUTED() && f.selector == execute(address[],uint256[],bytes[],bytes32).selector;
  82. }
  83. rule stateTransitionFn_EXECUTED(method f) filtered { f -> !assumedSafe(f) } {
  84. uint8 stateAfter = stateTransitionFnHelper(f, EXECUTED());
  85. assert false;
  86. }
  87. /*
  88. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  89. โ”‚ Rule: State transitions caused by time passing โ”‚
  90. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  91. */
  92. // The timelockId can be set in states QUEUED, EXECUTED and CANCELED. However, checking the full scope of this results
  93. // in a timeout. This is a weaker version that is still useful
  94. invariant noTimelockBeforeEndOfVote(env e, uint256 pId)
  95. state(e, pId) == ACTIVE() => timelockId(pId) == 0
  96. rule stateTransitionWait(uint256 pId, env e1, env e2) {
  97. require clockSanity(e1);
  98. require clockSanity(e2);
  99. require clock(e2) > clock(e1);
  100. // Force the state to be consistent with e1 (before). We want the storage related to `pId` to match what is
  101. // possible before the time passes. We don't want the state transition include elements that cannot have happened
  102. // before e1. This ensure that the e1 โ†’ e2 state transition is purelly a consequence of time passing.
  103. requireInvariant votesImplySnapshotPassed(e1, pId);
  104. requireInvariant noTimelockBeforeEndOfVote(e1, pId);
  105. uint8 stateBefore = state(e1, pId);
  106. uint8 stateAfter = state(e2, pId);
  107. assert (stateBefore != stateAfter) => (
  108. (stateBefore == PENDING() && stateAfter == ACTIVE() ) ||
  109. (stateBefore == PENDING() && stateAfter == DEFEATED() ) ||
  110. (stateBefore == ACTIVE() && stateAfter == SUCCEEDED()) ||
  111. (stateBefore == ACTIVE() && stateAfter == DEFEATED() )
  112. );
  113. }
  114. /*
  115. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  116. โ”‚ Rule: State corresponds to the vote timing and results โ”‚
  117. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  118. */
  119. rule stateIsConsistentWithVotes(uint256 pId, env e) {
  120. require clockSanity(e);
  121. requireInvariant proposalStateConsistency(pId);
  122. uint48 currentClock = clock(e);
  123. uint8 currentState = state(e, pId);
  124. uint256 snapshot = proposalSnapshot(pId);
  125. uint256 deadline = proposalDeadline(pId);
  126. bool quorumSuccess = quorumReached(pId);
  127. bool voteSuccess = voteSucceeded(pId);
  128. // Pending: before vote starts
  129. assert currentState == PENDING() => (
  130. snapshot >= currentClock
  131. );
  132. // Active: after vote starts & before vote ends
  133. assert currentState == ACTIVE() => (
  134. snapshot < currentClock &&
  135. deadline >= currentClock
  136. );
  137. // Succeeded: after vote end, with vote successful and quorum reached
  138. assert currentState == SUCCEEDED() => (
  139. deadline < currentClock &&
  140. (
  141. quorumSuccess &&
  142. voteSuccess
  143. )
  144. );
  145. // Defeated: after vote end, with vote not successful or quorum not reached
  146. assert currentState == DEFEATED() => (
  147. deadline < currentClock &&
  148. (
  149. !quorumSuccess ||
  150. !voteSuccess
  151. )
  152. );
  153. }
  154. /*
  155. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  156. โ”‚ [NEED WORK] Rule: `updateQuorumNumerator` cannot cause quorumReached to change. โ”‚
  157. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  158. */
  159. //// This would be nice, but its way to slow to run because "quorumReached" is a FV nightmare
  160. //// Also, for it to work we need to prove that the checkpoints have (strictly) increasing keys.
  161. // rule onlyVoteCanChangeQuorumReached(uint256 pId, env e, method f, calldataarg args)
  162. // filtered { f -> !assumedSafe(f) }
  163. // {
  164. // require clockSanity(e);
  165. // require clock(e) > proposalSnapshot(pId); // vote has started
  166. // require quorumNumeratorLength() < max_uint256; // sanity
  167. //
  168. // bool quorumReachedBefore = quorumReached(pId);
  169. //
  170. // uint256 snapshot = proposalSnapshot(pId);
  171. // uint256 totalSupply = token_getPastTotalSupply(snapshot);
  172. //
  173. // f(e, args);
  174. //
  175. // // Needed because the prover doesn't understand the checkpoint properties of the voting token.
  176. // require clock(e) > snapshot => token_getPastTotalSupply(snapshot) == totalSupply;
  177. //
  178. // assert quorumReached(pId) != quorumReachedBefore => (
  179. // !quorumReachedBefore &&
  180. // votingAll(f)
  181. // );
  182. // }
  183. //// To prove that, we need to prove that the checkpoints have (strictly) increasing keys.
  184. //// otherwise it gives us counter example where the checkpoint history has keys:
  185. //// [ 12,12,13,13,12] and the lookup obviously fail to get the correct value
  186. // rule quorumUpdateDoesntAffectPastProposals(uint256 pId, env e) {
  187. // require clockSanity(e);
  188. // require clock(e) > proposalSnapshot(pId); // vote has started
  189. // require quorumNumeratorLength() < max_uint256; // sanity
  190. //
  191. // bool quorumReachedBefore = quorumReached(pId);
  192. //
  193. // uint256 newQuorumNumerator;
  194. // updateQuorumNumerator(e, newQuorumNumerator);
  195. //
  196. // assert quorumReached(pId) == quorumReachedBefore;
  197. // }