GovernorBase.spec 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // not complete
  36. /*
  37. * If any of the properties are non zero, the rest has to be non zero
  38. */
  39. // start !0 !0 !0
  40. // end = !0 !0 !0
  41. // exe = 0 0 1 1
  42. // can = 0 1 0 1
  43. invariant proposalInitiated(uint256 pId)
  44. (proposalSnapshot(pId) != 0 <=> proposalDeadline(pId) != 0) &&
  45. (isCanceled(pId) => proposalSnapshot(pId) != 0) &&
  46. (isExecuted(pId) => proposalSnapshot(pId) != 0)
  47. // pass
  48. /*
  49. * A proposal cannot end unless it started.
  50. */
  51. invariant voteStartBeforeVoteEnd(uint256 pId)
  52. (proposalSnapshot(pId) > 0 => proposalSnapshot(pId) < proposalDeadline(pId))
  53. && (proposalSnapshot(pId) == 0 => proposalDeadline(pId) == 0)
  54. // pass
  55. /*
  56. * A proposal cannot be both executed and canceled.
  57. */
  58. invariant noBothExecutedAndCanceled(uint256 pId)
  59. !isExecuted(pId) || !isCanceled(pId)
  60. ///////////////////////////////////////////////////////////////////////////////////////
  61. ////////////////////////////////// In-State Rules /////////////////////////////////////
  62. ///////////////////////////////////////////////////////////////////////////////////////
  63. //==========================================
  64. //------------- Voting Period --------------
  65. //==========================================
  66. // pass
  67. /*
  68. * A user cannot vote twice
  69. */
  70. rule doubleVoting(uint256 pId, uint8 sup) {
  71. env e;
  72. address user = e.msg.sender;
  73. bool votedCheck = hasVoted(e, pId, user);
  74. require votedCheck == true;
  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. // pass
  86. /*
  87. * The voting must start not before the proposal’s creation time
  88. */
  89. rule noStartBeforeCreation(uint256 pId) {
  90. uint256 previousStart = proposalSnapshot(pId);
  91. require previousStart == 0;
  92. env e;
  93. calldataarg arg;
  94. propose(e, arg);
  95. uint newStart = proposalSnapshot(pId);
  96. // if created, start is after current block number (creation block)
  97. assert(newStart != previousStart => newStart >= e.block.number);
  98. }
  99. // pass
  100. /*
  101. * Once a proposal is created, voteStart and voteEnd are immutable
  102. */
  103. rule immutableFieldsAfterProposalCreation(uint256 pId, method f) {
  104. uint _voteStart = proposalSnapshot(pId);
  105. uint _voteEnd = proposalDeadline(pId);
  106. require _voteStart > 0; // proposal was created - relation proved in noStartBeforeCreation
  107. env e;
  108. calldataarg arg;
  109. f(e, arg);
  110. uint voteStart_ = proposalSnapshot(pId);
  111. uint voteEnd_ = proposalDeadline(pId);
  112. assert _voteStart == voteStart_;
  113. assert _voteEnd == voteEnd_;
  114. }
  115. // pass
  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. // pass
  130. /*
  131. * A proposal cannot be neither executed nor canceled before proposal's deadline
  132. */
  133. rule noExecuteOrCancelBeforeDeadline(uint256 pId, method f){
  134. env e;
  135. requireInvariant voteStartBeforeVoteEnd(pId);
  136. require !isExecuted(pId) && !isCanceled(pId);
  137. calldataarg arg;
  138. f(e, arg);
  139. assert e.block.number < proposalDeadline(pId) => (!isExecuted(pId) && !isCanceled(pId)), "executed/cancelled before deadline";
  140. }
  141. ////////////////////////////////////////////////////////////////////////////////
  142. ////////////////////// Integrity Of Functions (Unit Tests) /////////////////////
  143. ////////////////////////////////////////////////////////////////////////////////
  144. /**
  145. * Check hashProposal hashing is reliable (different inputs lead to different buffers hashed)
  146. */
  147. /*
  148. rule checkHashProposal {
  149. address[] t1;
  150. address[] t2;
  151. uint256[] v1;
  152. uint256[] v2;
  153. bytes[] c1;
  154. bytes[] c2;
  155. bytes32 d1;
  156. bytes32 d2;
  157. uint256 h1 = hashProposal(t1,v1,c1,d1);
  158. uint256 h2 = hashProposal(t2,v2,c2,d2);
  159. bool equalHashes = h1 == h2;
  160. assert equalHashes => t1.length == t2.length;
  161. assert equalHashes => v1.length == v2.length;
  162. assert equalHashes => c1.length == c2.length;
  163. assert equalHashes => d1 == d2;
  164. }
  165. */
  166. ////////////////////////////////////////////////////////////////////////////////
  167. //////////////////////////////// High Level Rules //////////////////////////////
  168. ////////////////////////////////////////////////////////////////////////////////
  169. // not passing
  170. /*
  171. * all non-view functions should revert if proposal is executed
  172. */
  173. rule allFunctionsRevertIfExecuted(uint256 pId, method f) filtered { f -> !f.isView } {
  174. env e; calldataarg args;
  175. require(isExecuted(pId));
  176. f@withrevert(e,args);
  177. assert(lastReverted == true, "Function was not reverted");
  178. }
  179. // not passing
  180. /*
  181. * all non-view functions should revert if proposal is canceled
  182. */
  183. rule allFunctionsRevertIfCanceled(method f) filtered { f -> !f.isView } {
  184. env e; calldataarg args;
  185. uint256 pId = e.msg.value;
  186. require(isCanceled(pId));
  187. requireInvariant noBothExecutedAndCanceled(pId);
  188. f@withrevert(e,args);
  189. assert(lastReverted == true, "Function was not reverted");
  190. }