GovernorBase.spec 12 KB

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