GovernorBase.spec 15 KB

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