GovernorBase.spec 8.4 KB

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