GovernorBase.spec 12 KB

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