GovernorBase.spec 12 KB

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