GovernorBase.spec 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // internal functions made public in harness:
  15. _quorumReached(uint256) returns bool envfree
  16. _voteSucceeded(uint256) returns bool envfree
  17. }
  18. /*
  19. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. ///////////////////////////////////////////////////// State Diagram //////////////////////////////////////////////////////////
  21. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. // //
  23. // castVote(s)() //
  24. // ------------- propose() ---------------------- time pass --------------- time passes ----------- //
  25. // | No Proposal | --------> | Before Start (Delay) | --------> | Voting Period | ----------------------> | execute() | //
  26. // ------------- ---------------------- --------------- -> Executed/Canceled ----------- //
  27. // ------------------------------------------------------------|---------------|-------------------------|--------------> //
  28. // t start end timelock //
  29. // //
  30. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. */
  32. ///////////////////////////////////////////////////////////////////////////////////////
  33. ///////////////////////////////// Global Valid States /////////////////////////////////
  34. ///////////////////////////////////////////////////////////////////////////////////////
  35. /*
  36. * If any of the properties are non zero, the rest has to be non zero
  37. */
  38. // start !0 !0 !0
  39. // end = !0 !0 !0
  40. // exe = 0 0 1 1
  41. // can = 0 1 0 1
  42. invariant proposalInitiated(uint256 pId)
  43. (proposalSnapshot(pId) != 0 <=> proposalDeadline(pId) != 0) &&
  44. (isCanceled(pId) => proposalSnapshot(pId) != 0) &&
  45. (isExecuted(pId) => proposalSnapshot(pId) != 0)
  46. { preserved with (env e){
  47. require e.block.number > 0;
  48. }}
  49. /*
  50. * A proposal cannot end unless it started.
  51. */
  52. invariant voteStartBeforeVoteEnd(uint256 pId)
  53. (proposalSnapshot(pId) > 0 => proposalSnapshot(pId) < proposalDeadline(pId))
  54. && (proposalSnapshot(pId) == 0 => proposalDeadline(pId) == 0)
  55. /*
  56. * A proposal cannot be both executed and canceled.
  57. */
  58. invariant noBothExecutedAndCanceled(uint256 pId)
  59. !isExecuted(pId) || !isCanceled(pId)
  60. /**
  61. * A proposal could be executed only if quorum was reached and vote succeeded
  62. */
  63. invariant executionOnlyIfQuoromReachedAndVoteSucceeded(uint256 pId)
  64. isExecuted(pId) => _quorumReached(pId) && _voteSucceeded(pId)
  65. ///////////////////////////////////////////////////////////////////////////////////////
  66. ////////////////////////////////// In-State Rules /////////////////////////////////////
  67. ///////////////////////////////////////////////////////////////////////////////////////
  68. //==========================================
  69. //------------- Voting Period --------------
  70. //==========================================
  71. /*
  72. * A user cannot vote twice
  73. */
  74. rule doubleVoting(uint256 pId, uint8 sup) {
  75. env e;
  76. address user = e.msg.sender;
  77. bool votedCheck = hasVoted(e, pId, user);
  78. castVote@withrevert(e, pId, sup);
  79. bool reverted = lastReverted;
  80. assert votedCheck => reverted, "double voting accured";
  81. }
  82. ///////////////////////////////////////////////////////////////////////////////////////
  83. //////////////////////////// State Transitions Rules //////////////////////////////////
  84. ///////////////////////////////////////////////////////////////////////////////////////
  85. //===========================================
  86. //-------- Propose() --> End of Time --------
  87. //===========================================
  88. /*
  89. * The voting must start not before the proposal’s creation time
  90. */
  91. rule noStartBeforeCreation(uint256 pId) {
  92. uint256 previousStart = proposalSnapshot(pId);
  93. require previousStart == 0;
  94. env e;
  95. calldataarg arg;
  96. propose(e, arg);
  97. uint newStart = proposalSnapshot(pId);
  98. // if created, start is after current block number (creation block)
  99. assert(newStart != previousStart => newStart >= e.block.number);
  100. }
  101. /*
  102. * Once a proposal is created, voteStart and voteEnd are immutable
  103. */
  104. rule immutableFieldsAfterProposalCreation(uint256 pId, method f) {
  105. uint _voteStart = proposalSnapshot(pId);
  106. uint _voteEnd = proposalDeadline(pId);
  107. require _voteStart > 0; // proposal was created - relation proved in noStartBeforeCreation
  108. env e;
  109. calldataarg arg;
  110. f(e, arg);
  111. uint voteStart_ = proposalSnapshot(pId);
  112. uint voteEnd_ = proposalDeadline(pId);
  113. assert _voteStart == voteStart_;
  114. assert _voteEnd == voteEnd_;
  115. }
  116. /*
  117. * A proposal cannot be neither executed nor canceled before it starts
  118. */
  119. rule noExecuteOrCancelBeforeStarting(uint256 pId, method f){
  120. env e;
  121. require !isExecuted(pId) && !isCanceled(pId);
  122. calldataarg arg;
  123. f(e, arg);
  124. assert e.block.number < proposalSnapshot(pId) => (!isExecuted(pId) && !isCanceled(pId)), "executed/cancelled before start";
  125. }
  126. //============================================
  127. //--- End of Voting Period --> End of Time ---
  128. //============================================
  129. /*
  130. * A proposal cannot be neither executed nor canceled before proposal's deadline
  131. */
  132. rule noExecuteOrCancelBeforeDeadline(uint256 pId, method f){
  133. env e;
  134. requireInvariant voteStartBeforeVoteEnd(pId);
  135. require !isExecuted(pId) && !isCanceled(pId);
  136. calldataarg arg;
  137. f(e, arg);
  138. assert e.block.number < proposalDeadline(pId) => (!isExecuted(pId) && !isCanceled(pId)), "executed/cancelled before deadline";
  139. }
  140. ////////////////////////////////////////////////////////////////////////////////
  141. ////////////////////// Integrity Of Functions (Unit Tests) /////////////////////
  142. ////////////////////////////////////////////////////////////////////////////////
  143. /**
  144. * Check hashProposal hashing is reliable (different inputs lead to different buffers hashed)
  145. */
  146. /*
  147. rule checkHashProposal {
  148. address[] t1;
  149. address[] t2;
  150. uint256[] v1;
  151. uint256[] v2;
  152. bytes[] c1;
  153. bytes[] c2;
  154. bytes32 d1;
  155. bytes32 d2;
  156. uint256 h1 = hashProposal(t1,v1,c1,d1);
  157. uint256 h2 = hashProposal(t2,v2,c2,d2);
  158. bool equalHashes = h1 == h2;
  159. assert equalHashes => t1.length == t2.length;
  160. assert equalHashes => v1.length == v2.length;
  161. assert equalHashes => c1.length == c2.length;
  162. assert equalHashes => d1 == d2;
  163. }
  164. */
  165. ////////////////////////////////////////////////////////////////////////////////
  166. //////////////////////////////// High Level Rules //////////////////////////////
  167. ////////////////////////////////////////////////////////////////////////////////
  168. /*
  169. * all non-view functions should revert if proposal is executed
  170. */
  171. rule allFunctionsRevertIfExecuted(uint256 pId, method f) filtered { f -> !f.isView } {
  172. env e; calldataarg args;
  173. require(isExecuted(pId));
  174. f@withrevert(e,args);
  175. assert(lastReverted == true, "Function was not reverted");
  176. }
  177. /*
  178. * all non-view functions should revert if proposal is canceled
  179. */
  180. rule allFunctionsRevertIfCanceled(method f) filtered { f -> !f.isView } {
  181. env e; calldataarg args;
  182. uint256 pId = e.msg.value;
  183. require(isCanceled(pId));
  184. requireInvariant noBothExecutedAndCanceled(pId);
  185. f@withrevert(e,args);
  186. assert(lastReverted == true, "Function was not reverted");
  187. }