GovernorBase.spec 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. queue(address[], uint256[], bytes[], bytes32) returns uint256
  16. // internal functions made public in harness:
  17. _quorumReached(uint256) returns bool
  18. _voteSucceeded(uint256) returns bool envfree
  19. _pId_Harness() returns uint256 envfree;
  20. // function summarization
  21. proposalThreshold() returns uint256 envfree
  22. getVotes(address, uint256) returns uint256 => DISPATCHER(true)
  23. erc20votes.getPastTotalSupply(uint256) returns uint256
  24. erc20votes.getPastVotes(address, uint256) returns uint256
  25. //scheduleBatch(address[],uint256[],bytes[],bytes32,bytes32,uint256) => DISPATCHER(true)
  26. //executeBatch(address[], uint256[], bytes[], bytes32, bytes32) => DISPATCHER(true)
  27. }
  28. definition proposalCreated(uint256 pId) returns bool = proposalSnapshot(pId) > 0;
  29. //////////////////////////////////////////////////////////////////////////////
  30. ///////////////////////////// Helper Functions ///////////////////////////////
  31. //////////////////////////////////////////////////////////////////////////////
  32. function callFunctionWithProposal(uint256 proposalId, method f) {
  33. address[] targets; uint256[] values; bytes[] calldatas; bytes32 descriptionHash;
  34. uint8 support; uint8 v; bytes32 r; bytes32 s;
  35. env e;
  36. if (f.selector == callPropose(address[], uint256[], bytes[]).selector) {
  37. uint256 result = callPropose@withrevert(e, targets, values, calldatas);
  38. require(proposalId == result);
  39. } else if (f.selector == execute(address[], uint256[], bytes[], bytes32).selector) {
  40. uint256 result = execute@withrevert(e, targets, values, calldatas, descriptionHash);
  41. require(result == proposalId);
  42. } else if (f.selector == castVote(uint256, uint8).selector) {
  43. castVote@withrevert(e, proposalId, support);
  44. } else if (f.selector == 0x7b3c71d3 /* castVoteWithReason */) {
  45. calldataarg args;
  46. require(_pId_Harness() == proposalId);
  47. f@withrevert(e, args);
  48. } else if (f.selector == castVoteBySig(uint256, uint8,uint8, bytes32, bytes32).selector) {
  49. castVoteBySig@withrevert(e, proposalId, support, v, r, s);
  50. } else if (f.selector == queue(address[], uint256[], bytes[], bytes32).selector) {
  51. require targets.length <= 1 && values.length <= 1 && calldatas.length <= 1;
  52. queue@withrevert(e, targets, values, calldatas, descriptionHash);
  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. * Start and end date are either initialized (non zero) or uninitialized (zero) simultaneously
  77. * This invariant assumes that the block number cannot be 0 at any stage of the contract cycle
  78. * This is very safe assumption as usually the 0 block is genesis block which is uploaded with data
  79. * by the developers and will not be valid to raise proposals (at the current way that block chain is functioning)
  80. */
  81. // To use env with general preserved block first disable type checking then
  82. // use Uri's branch - --staging uri/add_with_env_to_preserved_all
  83. invariant startAndEndDatesNonZero(uint256 pId)
  84. proposalSnapshot(pId) != 0 <=> proposalDeadline(pId) != 0
  85. /*{ preserved with (env e){
  86. require e.block.number > 0;
  87. }}*/
  88. /*
  89. * If a proposal is canceled it must have a start and an end date
  90. */
  91. // To use env with general preserved block first disable type checking then
  92. // use Uri's branch - --staging uri/add_with_env_to_preserved_all
  93. invariant canceledImplyStartAndEndDateNonZero(uint pId)
  94. isCanceled(pId) => proposalSnapshot(pId) != 0
  95. /*{ preserved with (env e){
  96. requireInvariant startAndEndDatesNonZero(pId); //@note maybe unndeeded
  97. require e.block.number > 0;
  98. }}*/
  99. /*
  100. * If a proposal is executed it must have a start and an end date
  101. */
  102. // To use env with general preserved block first disable type checking then
  103. // use Uri's branch - --staging uri/add_with_env_to_preserved_all
  104. invariant executedImplyStartAndEndDateNonZero(uint pId)
  105. isExecuted(pId) => proposalSnapshot(pId) != 0
  106. /*{ preserved with (env e){
  107. requireInvariant startAndEndDatesNonZero(pId); //@note maybe unndeeded
  108. require e.block.number > 0;
  109. }}*/
  110. /*
  111. * A proposal starting block number must be <= to the proposal end date
  112. */
  113. invariant voteStartBeforeVoteEnd(uint256 pId)
  114. // from < to <= because snapshot and deadline can be the same block number if delays are set to 0
  115. // This is possible before the integration of GovernorSettings.sol to the system.
  116. // After integration of GovernorSettings.sol the invariant expression should be changed from <= to <
  117. (proposalSnapshot(pId) > 0 => proposalSnapshot(pId) <= proposalDeadline(pId))
  118. { preserved {
  119. requireInvariant startAndEndDatesNonZero(pId);
  120. }}
  121. /*
  122. * A proposal cannot be both executed and canceled simultaneously.
  123. */
  124. invariant noBothExecutedAndCanceled(uint256 pId)
  125. !isExecuted(pId) || !isCanceled(pId)
  126. /*
  127. * A proposal could be executed only if quorum was reached and vote succeeded
  128. */
  129. rule executionOnlyIfQuoromReachedAndVoteSucceeded(uint256 pId, env e, method f){
  130. bool isExecutedBefore = isExecuted(pId);
  131. bool quorumReachedBefore = _quorumReached(e, pId);
  132. bool voteSucceededBefore = _voteSucceeded(pId);
  133. calldataarg args;
  134. f(e, args);
  135. bool isExecutedAfter = isExecuted(pId);
  136. assert (!isExecutedBefore && isExecutedAfter) => (quorumReachedBefore && voteSucceededBefore), "quorum was changed";
  137. }
  138. ///////////////////////////////////////////////////////////////////////////////////////
  139. ////////////////////////////////// In-State Rules /////////////////////////////////////
  140. ///////////////////////////////////////////////////////////////////////////////////////
  141. //==========================================
  142. //------------- Voting Period --------------
  143. //==========================================
  144. /*
  145. * A user cannot vote twice
  146. */
  147. // Checked for castVote only. all 3 castVote functions call _castVote, so the completness of the verification is counted on
  148. // the fact that the 3 functions themselves makes no chages, but rather call an internal function to execute.
  149. // That means that we do not check those 3 functions directly, however for castVote & castVoteWithReason it is quite trivial
  150. // to understand why this is ok. For castVoteBySig we basically assume that the signature referendum is correct without checking it.
  151. // We could check each function seperately and pass the rule, but that would have uglyfied the code with no concrete
  152. // benefit, as it is evident that nothing is happening in the first 2 functions (calling a view function), and we do not desire to check the signature verification.
  153. rule doubleVoting(uint256 pId, uint8 sup, method f) {
  154. env e;
  155. address user = e.msg.sender;
  156. bool votedCheck = hasVoted(e, pId, user);
  157. castVote@withrevert(e, pId, sup);
  158. assert votedCheck => lastReverted, "double voting accured";
  159. }
  160. ///////////////////////////////////////////////////////////////////////////////////////
  161. //////////////////////////// State Transitions Rules //////////////////////////////////
  162. ///////////////////////////////////////////////////////////////////////////////////////
  163. //===========================================
  164. //-------- Propose() --> End of Time --------
  165. //===========================================
  166. /*
  167. * Once a proposal is created, voteStart and voteEnd are immutable
  168. */
  169. rule immutableFieldsAfterProposalCreation(uint256 pId, method f) {
  170. uint _voteStart = proposalSnapshot(pId);
  171. uint _voteEnd = proposalDeadline(pId);
  172. require _voteStart > 0; // proposal was created - relation proved in noStartBeforeCreation
  173. env e;
  174. calldataarg arg;
  175. f(e, arg);
  176. uint voteStart_ = proposalSnapshot(pId);
  177. uint voteEnd_ = proposalDeadline(pId);
  178. assert _voteStart == voteStart_;
  179. assert _voteEnd == voteEnd_;
  180. }
  181. /*
  182. * Voting cannot start at a block number prior to proposal’s creation block number
  183. */
  184. rule noStartBeforeCreation(uint256 pId) {
  185. uint256 previousStart = proposalSnapshot(pId);
  186. // This line makes sure that we see only cases where start date is changed from 0, i.e. creation of proposal
  187. // We proved in immutableFieldsAfterProposalCreation that once dates set for proposal, it cannot be changed
  188. require previousStart == 0;
  189. env e; calldataarg arg;
  190. propose(e, arg);
  191. uint newStart = proposalSnapshot(pId);
  192. // if created, start is after current block number (creation block)
  193. assert(newStart != previousStart => newStart >= e.block.number);
  194. }
  195. /*
  196. * A proposal cannot be neither executed nor canceled before it starts
  197. */
  198. rule noExecuteOrCancelBeforeStarting(uint256 pId, method f){
  199. env e;
  200. require !isExecuted(pId) && !isCanceled(pId);
  201. calldataarg arg;
  202. f(e, arg);
  203. assert e.block.number < proposalSnapshot(pId) => (!isExecuted(pId) && !isCanceled(pId)), "executed/cancelled before start";
  204. }
  205. //============================================
  206. //--- End of Voting Period --> End of Time ---
  207. //============================================
  208. /*
  209. * A proposal cannot be neither executed nor canceled before proposal's deadline
  210. */
  211. rule noExecuteOrCancelBeforeDeadline(uint256 pId, method f){
  212. env e;
  213. requireInvariant voteStartBeforeVoteEnd(pId);
  214. require !isExecuted(pId) && !isCanceled(pId);
  215. calldataarg arg;
  216. f(e, arg);
  217. assert e.block.number < proposalDeadline(pId) => (!isExecuted(pId) && !isCanceled(pId)), "executed/cancelled before deadline";
  218. }
  219. ////////////////////////////////////////////////////////////////////////////////
  220. ////////////////////// Integrity Of Functions (Unit Tests) /////////////////////
  221. ////////////////////////////////////////////////////////////////////////////////
  222. ////////////////////////////////////////////////////////////////////////////////
  223. ////////////////////////////// High Level Rules ////////////////////////////////
  224. ////////////////////////////////////////////////////////////////////////////////
  225. ////////////////////////////////////////////////////////////////////////////////
  226. ///////////////////////////// Not Categorized Yet //////////////////////////////
  227. ////////////////////////////////////////////////////////////////////////////////
  228. /*
  229. * all non-view functions should revert if proposal is executed
  230. */
  231. // summarization - hashProposal => Const - for any set of arguments passed to the function the same value will be returned.
  232. // 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)
  233. // the summarization is not an under estimation in this case, because we want to check that for a specific proposal ID (pId), any
  234. // (non view) function call is reverting. We dont care what happen with other pIds, and dont care how the hash function generates the ID.
  235. rule allFunctionsRevertIfExecuted(method f) filtered { f -> !f.isView && f.selector != 0x7d5e81e2 && !f.isFallback && f.selector != updateQuorumNumerator(uint256).selector && f.selector != 0xa890c910} {
  236. env e; calldataarg args; // ^ ^
  237. uint256 pId; // propose updateTimelock
  238. require(isExecuted(pId));
  239. // requireInvariant proposalInitiated(pId);
  240. requireInvariant noBothExecutedAndCanceled(pId);
  241. callFunctionWithProposal(pId, f);
  242. assert(lastReverted, "Function was not reverted");
  243. }
  244. /*
  245. * all non-view functions should revert if proposal is canceled
  246. */
  247. rule allFunctionsRevertIfCanceled(method f) filtered { f -> !f.isView && f.selector != 0x7d5e81e2 && !f.isFallback && f.selector != updateQuorumNumerator(uint256).selector && f.selector != 0xa890c910} {
  248. env e; calldataarg args; // ^ ^
  249. uint256 pId; // propose updateTimelock
  250. require(isCanceled(pId));
  251. requireInvariant noBothExecutedAndCanceled(pId);
  252. // requireInvariant proposalInitiated(pId);
  253. callFunctionWithProposal(pId, f);
  254. assert(lastReverted, "Function was not reverted");
  255. }
  256. /*
  257. * Shows that executed can only change due to execute()
  258. */
  259. rule executedOnlyAfterExecuteFunc(address[] targets, uint256[] values, bytes[] calldatas, bytes32 descriptionHash, method f) {
  260. env e; calldataarg args;
  261. uint256 pId;
  262. bool executedBefore = isExecuted(pId);
  263. require(!executedBefore);
  264. callFunctionWithProposal(pId, f);
  265. require(!lastReverted);
  266. // execute(e, targets, values, calldatas, descriptionHash);
  267. bool executedAfter = isExecuted(pId);
  268. assert(executedAfter != executedBefore, "executed property did not change");
  269. }
  270. /*
  271. * User should not be able to affect proposal threshold
  272. */
  273. rule unaffectedThreshhold(method f){
  274. uint256 thresholdBefore = proposalThreshold();
  275. env e;
  276. calldataarg args;
  277. f(e, args);
  278. uint256 thresholdAfter = proposalThreshold();
  279. assert thresholdBefore == thresholdAfter, "threshold was changed";
  280. }