GovernorBase.spec 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //////////////////////////////////////////////////////////////////////////////
  2. ///////////////////// Governor.sol base definitions //////////////////////////
  3. //////////////////////////////////////////////////////////////////////////////
  4. methods {
  5. proposalSnapshot(uint256) returns uint256 envfree // matches proposalVoteStart
  6. proposalDeadline(uint256) returns uint256 envfree
  7. hashProposal(address[],uint256[],bytes[],bytes32) returns uint256 envfree
  8. isExecuted(uint256) returns bool envfree
  9. isCanceled(uint256) returns bool envfree
  10. execute(address[], uint256[], bytes[], bytes32) returns uint256
  11. // initialized(uint256) returns bool envfree
  12. hasVoted(uint256, address) returns bool
  13. castVote(uint256, uint8) returns uint256
  14. // castVoteBySig(uint256,uint8,uint8,bytes32,bytes32) returns uint256
  15. // internal functions made public in harness:
  16. _quorumReached(uint256) returns bool envfree
  17. _voteSucceeded(uint256) returns bool envfree
  18. // hashProposal(address[] targets, uint256[] values, bytes[] calldatas, bytes32 descriptionHash) => uniqueHashGhost(descriptionHash)
  19. }
  20. //////////////////////////////////////////////////////////////////////////////
  21. ////////////////////////////////// Ghosts ////////////////////////////////////
  22. //////////////////////////////////////////////////////////////////////////////
  23. // ghost uniqueHashGhost(bytes32) returns uint256;
  24. //////////////////////////////////////////////////////////////////////////////
  25. ///////////////////////////// Helper Functions ///////////////////////////////
  26. //////////////////////////////////////////////////////////////////////////////
  27. /*
  28. function callFunctionWithParams(address[] targets, uint256[] values, bytes[] calldatas, bytes32 descriptionHash, uint256 proposalId, uint8 support, string reason, uint8 v, bytes32 r, bytes32 s, method f) {
  29. env e;
  30. if (f.selector == callPropose(address[], uint256[], bytes[]).selector) {
  31. callPropose(e, targets, values, calldatas);
  32. } else if (f.selector == execute(address[], uint256[], bytes[], bytes32).selector) {
  33. execute(e, targets, values, calldatas, descriptionHash);
  34. } else if (f.selector == castVote(uint256, uint8).selector) {
  35. castVote(e, proposalId, support);
  36. } else if (f.selector == castVoteWithReason(uint256, uint8, string).selector) {
  37. castVoteWithReason(e, proposalId, support, reason);
  38. } else if (f.selector == castVoteBySig(uint256, uint8,uint8, bytes32, bytes32).selector) {
  39. castVoteBySig(e, proposalId, support, v, r, s);
  40. } else {
  41. calldataarg args;
  42. f(e,args);
  43. }
  44. }
  45. */
  46. /*
  47. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. ///////////////////////////////////////////////////// State Diagram //////////////////////////////////////////////////////////
  49. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. // //
  51. // castVote(s)() //
  52. // ------------- propose() ---------------------- time pass --------------- time passes ----------- //
  53. // | No Proposal | --------> | Before Start (Delay) | --------> | Voting Period | ----------------------> | execute() | //
  54. // ------------- ---------------------- --------------- -> Executed/Canceled ----------- //
  55. // ------------------------------------------------------------|---------------|-------------------------|--------------> //
  56. // t start end timelock //
  57. // //
  58. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. */
  60. ///////////////////////////////////////////////////////////////////////////////////////
  61. ///////////////////////////////// Global Valid States /////////////////////////////////
  62. ///////////////////////////////////////////////////////////////////////////////////////
  63. /*
  64. * If any of the properties are non zero, the rest has to be non zero
  65. */
  66. invariant proposalInitiated(uint256 pId)
  67. (proposalSnapshot(pId) != 0 <=> proposalDeadline(pId) != 0) &&
  68. (isCanceled(pId) => proposalSnapshot(pId) != 0) &&
  69. (isExecuted(pId) => proposalSnapshot(pId) != 0)
  70. /*{ preserved with (env e){
  71. require e.block.number > 0;
  72. }}*/
  73. /*
  74. * A proposal cannot end unless it started.
  75. */
  76. invariant voteStartBeforeVoteEnd(uint256 pId)
  77. (proposalSnapshot(pId) > 0 => proposalSnapshot(pId) < proposalDeadline(pId))
  78. && (proposalSnapshot(pId) == 0 => proposalDeadline(pId) == 0)
  79. /*
  80. * A proposal cannot be both executed and canceled.
  81. */
  82. invariant noBothExecutedAndCanceled(uint256 pId)
  83. !isExecuted(pId) || !isCanceled(pId)
  84. /**
  85. * A proposal could be executed only if quorum was reached and vote succeeded
  86. */
  87. invariant executionOnlyIfQuoromReachedAndVoteSucceeded(uint256 pId)
  88. isExecuted(pId) => _quorumReached(pId) && _voteSucceeded(pId)
  89. ///////////////////////////////////////////////////////////////////////////////////////
  90. ////////////////////////////////// In-State Rules /////////////////////////////////////
  91. ///////////////////////////////////////////////////////////////////////////////////////
  92. //==========================================
  93. //------------- Voting Period --------------
  94. //==========================================
  95. /*
  96. * A user cannot vote twice
  97. */
  98. rule doubleVoting(uint256 pId, uint8 sup) {
  99. env e;
  100. address user = e.msg.sender;
  101. bool votedCheck = hasVoted(e, pId, user);
  102. castVote@withrevert(e, pId, sup);
  103. bool reverted = lastReverted;
  104. assert votedCheck => reverted, "double voting accured";
  105. }
  106. ///////////////////////////////////////////////////////////////////////////////////////
  107. //////////////////////////// State Transitions Rules //////////////////////////////////
  108. ///////////////////////////////////////////////////////////////////////////////////////
  109. //===========================================
  110. //-------- Propose() --> End of Time --------
  111. //===========================================
  112. /*
  113. * The voting must start not before the proposal’s creation time
  114. */
  115. rule noStartBeforeCreation(uint256 pId) {
  116. uint256 previousStart = proposalSnapshot(pId);
  117. require previousStart == 0;
  118. env e;
  119. calldataarg arg;
  120. propose(e, arg);
  121. uint newStart = proposalSnapshot(pId);
  122. // if created, start is after current block number (creation block)
  123. assert(newStart != previousStart => newStart >= e.block.number);
  124. }
  125. /*
  126. * Once a proposal is created, voteStart and voteEnd are immutable
  127. */
  128. rule immutableFieldsAfterProposalCreation(uint256 pId, method f) {
  129. uint _voteStart = proposalSnapshot(pId);
  130. uint _voteEnd = proposalDeadline(pId);
  131. require _voteStart > 0; // proposal was created - relation proved in noStartBeforeCreation
  132. env e;
  133. calldataarg arg;
  134. f(e, arg);
  135. uint voteStart_ = proposalSnapshot(pId);
  136. uint voteEnd_ = proposalDeadline(pId);
  137. assert _voteStart == voteStart_;
  138. assert _voteEnd == voteEnd_;
  139. }
  140. /*
  141. * A proposal cannot be neither executed nor canceled before it starts
  142. */
  143. rule noExecuteOrCancelBeforeStarting(uint256 pId, method f){
  144. env e;
  145. require !isExecuted(pId) && !isCanceled(pId);
  146. calldataarg arg;
  147. f(e, arg);
  148. assert e.block.number < proposalSnapshot(pId) => (!isExecuted(pId) && !isCanceled(pId)), "executed/cancelled before start";
  149. }
  150. //============================================
  151. //--- End of Voting Period --> End of Time ---
  152. //============================================
  153. /*
  154. * A proposal cannot be neither executed nor canceled before proposal's deadline
  155. */
  156. rule noExecuteOrCancelBeforeDeadline(uint256 pId, method f){
  157. env e;
  158. requireInvariant voteStartBeforeVoteEnd(pId);
  159. require !isExecuted(pId) && !isCanceled(pId);
  160. calldataarg arg;
  161. f(e, arg);
  162. assert e.block.number < proposalDeadline(pId) => (!isExecuted(pId) && !isCanceled(pId)), "executed/cancelled before deadline";
  163. }
  164. ////////////////////////////////////////////////////////////////////////////////
  165. ////////////////////// Integrity Of Functions (Unit Tests) /////////////////////
  166. ////////////////////////////////////////////////////////////////////////////////
  167. /**
  168. * Check hashProposal hashing is reliable (different inputs lead to different buffers hashed)
  169. */
  170. /*
  171. rule checkHashProposal {
  172. address[] t1;
  173. address[] t2;
  174. uint256[] v1;
  175. uint256[] v2;
  176. bytes[] c1;
  177. bytes[] c2;
  178. bytes32 d1;
  179. bytes32 d2;
  180. uint256 h1 = hashProposal(t1,v1,c1,d1);
  181. uint256 h2 = hashProposal(t2,v2,c2,d2);
  182. bool equalHashes = h1 == h2;
  183. assert equalHashes => t1.length == t2.length;
  184. assert equalHashes => v1.length == v2.length;
  185. assert equalHashes => c1.length == c2.length;
  186. assert equalHashes => d1 == d2;
  187. }
  188. */
  189. ////////////////////////////////////////////////////////////////////////////////
  190. ////////////////////////////// High Level Rules ////////////////////////////////
  191. ////////////////////////////////////////////////////////////////////////////////
  192. /*
  193. * all non-view functions should revert if proposal is executed
  194. */
  195. rule allFunctionsRevertIfExecuted(method f) filtered { f -> !f.isView } {
  196. env e; calldataarg args;
  197. uint256 pId; uint8 sup; uint8 v; bytes32 r; bytes32 s ;
  198. require(isExecuted(pId));
  199. requireInvariant noBothExecutedAndCanceled(pId);
  200. // f@withrevert(e,args);
  201. // castVote@withrevert(e, pId, sup);
  202. castVoteBySig@withrevert(e, pId, sup, v, r, s);
  203. assert(lastReverted == true, "Function was not reverted");
  204. }
  205. /*
  206. * all non-view functions should revert if proposal is canceled
  207. */
  208. rule allFunctionsRevertIfCanceled(method f) filtered { f -> !f.isView } {
  209. env e; calldataarg args;
  210. address[] targets; uint256[] values; bytes[] calldatas; bytes32 descriptionHash;
  211. uint256 pId = uniqueHashGhost(descriptionHash);
  212. uint8 sup; uint8 v; bytes32 r; bytes32 s;
  213. require(isCanceled(pId));
  214. requireInvariant noBothExecutedAndCanceled(pId);
  215. // castVote@withrevert(e, pId, sup);
  216. // castVoteWithReason(e, pId, sup, "");
  217. // castVoteBySig@withrevert(e, pId, sup, v, r, s);
  218. require(isCanceled(pId) => proposalSnapshot(pId) != 0);
  219. execute@withrevert(e, targets, values, calldatas, descriptionHash);
  220. assert(lastReverted == true, "Function was not reverted");
  221. }
  222. /*
  223. * Shows that executed can only change due to execute()
  224. */
  225. rule executedOnlyAfterExecuteFunc(address[] targets, uint256[] values, bytes[] calldatas, bytes32 descriptionHash, method f) filtered {f -> f.selector != (execute(address[], uint256[], bytes[], bytes32).selector)} {
  226. env e; calldataarg args;
  227. uint256 pId = hashProposal(targets, values, calldatas, descriptionHash);
  228. bool executedBefore = isExecuted(pId);
  229. require(!executedBefore);
  230. f(e, args);
  231. // execute(e, targets, values, calldatas, descriptionHash);
  232. bool executedAfter = isExecuted(pId);
  233. assert(executedAfter != executedBefore, "executed property did not change");
  234. }