GovernorBase.spec 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //////////////////////////////////////////////////////////////////////////////
  2. ///////////////////// Governor.sol base definitions //////////////////////////
  3. //////////////////////////////////////////////////////////////////////////////
  4. methods {
  5. proposalSnapshot(uint256) returns uint256 envfree // matches proposalVoteStart
  6. proposalDeadline(uint256) returns uint256 envfree // matches proposalVoteEnd
  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. hasVoted(uint256, address) returns bool
  12. castVote(uint256, uint8) returns uint256
  13. updateQuorumNumerator(uint256)
  14. queue(address[], uint256[], bytes[], bytes32) returns uint256
  15. // internal functions made public in harness:
  16. quorumReached(uint256) returns bool
  17. voteSucceeded(uint256) returns bool envfree
  18. // function summarization
  19. proposalThreshold() returns uint256 envfree
  20. getVotes(address, uint256) returns uint256 => DISPATCHER(true)
  21. getPastTotalSupply(uint256) returns uint256 => DISPATCHER(true)
  22. getPastVotes(address, uint256) returns uint256 => DISPATCHER(true)
  23. //scheduleBatch(address[],uint256[],bytes[],bytes32,bytes32,uint256) => DISPATCHER(true)
  24. //executeBatch(address[], uint256[], bytes[], bytes32, bytes32) => DISPATCHER(true)
  25. }
  26. //////////////////////////////////////////////////////////////////////////////
  27. //////////////////////////////// Definitions /////////////////////////////////
  28. //////////////////////////////////////////////////////////////////////////////
  29. // proposal was created - relation proved in noStartBeforeCreation
  30. definition proposalCreated(uint256 pId) returns bool =
  31. proposalSnapshot(pId) > 0
  32. && proposalDeadline(pId) > 0;
  33. //////////////////////////////////////////////////////////////////////////////
  34. ///////////////////////////// Helper Functions ///////////////////////////////
  35. //////////////////////////////////////////////////////////////////////////////
  36. function helperFunctionsWithRevert(uint256 proposalId, method f, env e) {
  37. address[] targets; uint256[] values; bytes[] calldatas; string reason; bytes32 descriptionHash;
  38. uint8 support; uint8 v; bytes32 r; bytes32 s; bytes params;
  39. if (f.selector == propose(address[], uint256[], bytes[], string).selector) {
  40. uint256 result = propose@withrevert(e, targets, values, calldatas, reason);
  41. require(result == proposalId);
  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 == castVoteWithReason(uint256, uint8, string).selector) {
  48. castVoteWithReason@withrevert(e, proposalId, support, reason);
  49. } else if (f.selector == castVoteBySig(uint256, uint8,uint8, bytes32, bytes32).selector) {
  50. castVoteBySig@withrevert(e, proposalId, support, v, r, s);
  51. } else if (f.selector == queue(address[], uint256[], bytes[], bytes32).selector) {
  52. queue@withrevert(e, targets, values, calldatas, descriptionHash);
  53. } else if (f.selector == castVoteWithReasonAndParamsBySig(uint256,uint8,string,bytes,uint8,bytes32,bytes32).selector) {
  54. castVoteWithReasonAndParamsBySig@withrevert(e, proposalId, support, reason, params, v, r, s);
  55. } else if (f.selector == castVoteWithReasonAndParams(uint256,uint8,string,bytes).selector) {
  56. castVoteWithReasonAndParams@withrevert(e, proposalId, support, reason, params);
  57. } else {
  58. calldataarg args;
  59. f@withrevert(e, args);
  60. }
  61. }
  62. /*
  63. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64. ///////////////////////////////////////////////////// State Diagram //////////////////////////////////////////////////////////
  65. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. // //
  67. // castVote(s)() //
  68. // ------------- propose() ---------------------- time pass --------------- time passes ----------- //
  69. // | No Proposal | --------> | Before Start (Delay) | --------> | Voting Period | ----------------------> | execute() | //
  70. // ------------- ---------------------- --------------- -> Executed/Canceled ----------- //
  71. // ------------------------------------------------------------|---------------|-------------------------|--------------> //
  72. // t start end timelock //
  73. // //
  74. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. */
  76. ///////////////////////////////////////////////////////////////////////////////////////
  77. ///////////////////////////////// Global Valid States /////////////////////////////////
  78. ///////////////////////////////////////////////////////////////////////////////////////
  79. /*
  80. * Start and end date are either initialized (non zero) or uninitialized (zero) simultaneously
  81. * This invariant assumes that the block number cannot be 0 at any stage of the contract cycle
  82. * This is very safe assumption as usually the 0 block is genesis block which is uploaded with data
  83. * by the developers and will not be valid to raise proposals (at the current way that block chain is functioning)
  84. */
  85. // To use env with general preserved block disable type checking [--disableLocalTypeChecking]
  86. invariant startAndEndDatesNonZero(uint256 pId)
  87. proposalSnapshot(pId) != 0 <=> proposalDeadline(pId) != 0
  88. { preserved with (env e){
  89. require e.block.number > 0;
  90. }}
  91. /*
  92. * If a proposal is canceled it must have a start and an end date
  93. */
  94. // To use env with general preserved block disable type checking [--disableLocalTypeChecking]
  95. invariant canceledImplyStartAndEndDateNonZero(uint pId)
  96. isCanceled(pId) => proposalSnapshot(pId) != 0
  97. {preserved with (env e){
  98. require e.block.number > 0;
  99. }}
  100. /*
  101. * If a proposal is executed it must have a start and an end date
  102. */
  103. // To use env with general preserved block disable type checking [--disableLocalTypeChecking]
  104. invariant executedImplyStartAndEndDateNonZero(uint pId)
  105. isExecuted(pId) => proposalSnapshot(pId) != 0
  106. { preserved with (env e){
  107. requireInvariant startAndEndDatesNonZero(pId);
  108. require e.block.number > 0;
  109. }}
  110. /*
  111. * A proposal starting block number must be less or equal than 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. // (proposalSnapshot(pId) > 0 => proposalSnapshot(pId) <= proposalDeadline(pId))
  119. { preserved {
  120. requireInvariant startAndEndDatesNonZero(pId);
  121. }}
  122. /*
  123. * A proposal cannot be both executed and canceled simultaneously.
  124. */
  125. invariant noBothExecutedAndCanceled(uint256 pId)
  126. !isExecuted(pId) || !isCanceled(pId)
  127. /*
  128. * A proposal could be executed only if quorum was reached and vote succeeded
  129. */
  130. rule executionOnlyIfQuoromReachedAndVoteSucceeded(uint256 pId, env e, method f){
  131. bool isExecutedBefore = isExecuted(pId);
  132. bool quorumReachedBefore = quorumReached(e, pId);
  133. bool voteSucceededBefore = voteSucceeded(pId);
  134. calldataarg args;
  135. f(e, args);
  136. bool isExecutedAfter = isExecuted(pId);
  137. assert (!isExecutedBefore && isExecutedAfter) => (quorumReachedBefore && voteSucceededBefore), "quorum was changed";
  138. }
  139. ///////////////////////////////////////////////////////////////////////////////////////
  140. ////////////////////////////////// In-State Rules /////////////////////////////////////
  141. ///////////////////////////////////////////////////////////////////////////////////////
  142. //==========================================
  143. //------------- Voting Period --------------
  144. //==========================================
  145. /*
  146. * A user cannot vote twice
  147. */
  148. // Checked for castVote only. all 3 castVote functions call _castVote, so the completeness of the verification is counted on
  149. // the fact that the 3 functions themselves makes no changes, but rather call an internal function to execute.
  150. // That means that we do not check those 3 functions directly, however for castVote & castVoteWithReason it is quite trivial
  151. // to understand why this is ok. For castVoteBySig we basically assume that the signature referendum is correct without checking it.
  152. // We could check each function separately and pass the rule, but that would have uglyfied the code with no concrete
  153. // 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.
  154. rule doubleVoting(uint256 pId, uint8 sup, method f) {
  155. env e;
  156. address user = e.msg.sender;
  157. bool votedCheck = hasVoted(e, pId, user);
  158. castVote@withrevert(e, pId, sup);
  159. assert votedCheck => lastReverted, "double voting occurred";
  160. }
  161. ///////////////////////////////////////////////////////////////////////////////////////
  162. //////////////////////////// State Transitions Rules //////////////////////////////////
  163. ///////////////////////////////////////////////////////////////////////////////////////
  164. //===========================================
  165. //-------- Propose() --> End of Time --------
  166. //===========================================
  167. /*
  168. * Once a proposal is created, voteStart and voteEnd are immutable
  169. */
  170. rule immutableFieldsAfterProposalCreation(uint256 pId, method f) {
  171. uint256 _voteStart = proposalSnapshot(pId);
  172. uint256 _voteEnd = proposalDeadline(pId);
  173. require proposalCreated(pId); // startDate > 0
  174. env e; calldataarg arg;
  175. f(e, arg);
  176. uint256 voteStart_ = proposalSnapshot(pId);
  177. uint256 voteEnd_ = proposalDeadline(pId);
  178. assert _voteStart == voteStart_, "Start date was changed";
  179. assert _voteEnd == voteEnd_, "End date was changed";
  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 !proposalCreated(pId); // previousStart == 0;
  189. env e; calldataarg args;
  190. propose(e, args);
  191. uint256 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. //--- End of Voting Period --> End of Time ---
  197. //============================================
  198. /*
  199. * A proposal can neither be executed nor canceled before it ends
  200. */
  201. // By induction it cannot be executed nor canceled before it starts, due to voteStartBeforeVoteEnd
  202. rule noExecuteOrCancelBeforeDeadline(uint256 pId, method f){
  203. require !isExecuted(pId) && !isCanceled(pId);
  204. env e; calldataarg args;
  205. f(e, args);
  206. assert e.block.number < proposalDeadline(pId) => (!isExecuted(pId) && !isCanceled(pId)), "executed/cancelled before deadline";
  207. }
  208. ////////////////////////////////////////////////////////////////////////////////
  209. ////////////////////// Integrity Of Functions (Unit Tests) /////////////////////
  210. ////////////////////////////////////////////////////////////////////////////////
  211. ////////////////////////////////////////////////////////////////////////////////
  212. ////////////////////////////// High Level Rules ////////////////////////////////
  213. ////////////////////////////////////////////////////////////////////////////////
  214. ////////////////////////////////////////////////////////////////////////////////
  215. ///////////////////////////// Not Categorized Yet //////////////////////////////
  216. ////////////////////////////////////////////////////////////////////////////////
  217. /*
  218. * All proposal specific (non-view) functions should revert if proposal is executed
  219. */
  220. // In this rule we show that if a function is executed, i.e. execute() was called on the proposal ID,
  221. // non of the proposal specific functions can make changes again. In executedOnlyAfterExecuteFunc
  222. // we connected the executed attribute to the execute() function, showing that only execute() can
  223. // change it, and that it will always change it.
  224. rule allFunctionsRevertIfExecuted(method f) filtered { f ->
  225. !f.isView && !f.isFallback
  226. && f.selector != updateTimelock(address).selector
  227. && f.selector != updateQuorumNumerator(uint256).selector
  228. && f.selector != queue(address[],uint256[],bytes[],bytes32).selector
  229. && f.selector != relay(address,uint256,bytes).selector
  230. && f.selector != 0xb9a61961 // __acceptAdmin()
  231. && f.selector != onERC721Received(address,address,uint256,bytes).selector
  232. && f.selector != onERC1155Received(address,address,uint256,uint256,bytes).selector
  233. && f.selector != onERC1155BatchReceived(address,address,uint256[],uint256[],bytes).selector
  234. } {
  235. env e; calldataarg args;
  236. uint256 pId;
  237. require(isExecuted(pId));
  238. requireInvariant noBothExecutedAndCanceled(pId);
  239. requireInvariant executedImplyStartAndEndDateNonZero(pId);
  240. helperFunctionsWithRevert(pId, f, e);
  241. assert(lastReverted, "Function was not reverted");
  242. }
  243. /*
  244. * All proposal specific (non-view) functions should revert if proposal is canceled
  245. */
  246. rule allFunctionsRevertIfCanceled(method f) filtered {
  247. f -> !f.isView && !f.isFallback
  248. && f.selector != updateTimelock(address).selector
  249. && f.selector != updateQuorumNumerator(uint256).selector
  250. && f.selector != queue(address[],uint256[],bytes[],bytes32).selector
  251. && f.selector != relay(address,uint256,bytes).selector
  252. && f.selector != 0xb9a61961 // __acceptAdmin()
  253. && f.selector != onERC721Received(address,address,uint256,bytes).selector
  254. && f.selector != onERC1155Received(address,address,uint256,uint256,bytes).selector
  255. && f.selector != onERC1155BatchReceived(address,address,uint256[],uint256[],bytes).selector
  256. } {
  257. env e; calldataarg args;
  258. uint256 pId;
  259. require(isCanceled(pId));
  260. requireInvariant noBothExecutedAndCanceled(pId);
  261. requireInvariant canceledImplyStartAndEndDateNonZero(pId);
  262. helperFunctionsWithRevert(pId, f, e);
  263. assert(lastReverted, "Function was not reverted");
  264. }
  265. /*
  266. * Proposal can be switched to executed only via execute() function
  267. */
  268. rule executedOnlyAfterExecuteFunc(address[] targets, uint256[] values, bytes[] calldatas, bytes32 descriptionHash, method f) {
  269. env e; calldataarg args;
  270. uint256 pId;
  271. bool executedBefore = isExecuted(pId);
  272. require(!executedBefore);
  273. helperFunctionsWithRevert(pId, f, e);
  274. bool executedAfter = isExecuted(pId);
  275. assert(executedAfter != executedBefore => f.selector == execute(address[], uint256[], bytes[], bytes32).selector, "isExecuted only changes in the execute method");
  276. }