GovernorBase.spec 14 KB

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