GovernorBase.spec 15 KB

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