GovernorBase.spec 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //////////////////////////////////////////////////////////////////////////////
  2. ///////////////////// Governor.sol base definitions //////////////////////////
  3. //////////////////////////////////////////////////////////////////////////////
  4. methods {
  5. proposalSnapshot(uint256) returns uint256 envfree // matches proposalVoteStart
  6. proposalDeadline(uint256) returns uint256 envfree
  7. hashProposal(address[],uint256[],bytes[],bytes32) returns uint256 envfree
  8. isExecuted(uint256) returns bool envfree
  9. isCanceled(uint256) returns bool envfree
  10. initialized(uint256) returns bool envfree
  11. hasVoted(uint256, address) returns bool
  12. castVote(uint256, uint8) returns uint256
  13. // internal functions made public in harness:
  14. _quorumReached(uint256) returns bool envfree
  15. _voteSucceeded(uint256) returns bool envfree
  16. // getter for checking the sums
  17. counter_vote_power_by_id(uint256) returns uint256 envfree
  18. ghost_vote_power_by_id(uint256) returns uint256 envfree
  19. counted_weight(uint256) returns uint256 envfree
  20. }
  21. //////////////////////////////////////////////////////////////////////////////
  22. ///////////////////////////////// GHOSTS /////////////////////////////////////
  23. //////////////////////////////////////////////////////////////////////////////
  24. ghost vote_power_ghost() returns uint256;
  25. hook Sstore ghost_vote_power_by_id[KEY uint256 pId] uint256 current_power STORAGE{
  26. havoc vote_power_ghost assuming vote_power_ghost@new() == vote_power_ghost@old() + current_power;
  27. }
  28. //////////////////////////////////////////////////////////////////////////////
  29. ////////////////////////////// INVARIANTS ////////////////////////////////////
  30. //////////////////////////////////////////////////////////////////////////////
  31. /**
  32. * A proposal cannot end unless it started.
  33. */
  34. //invariant voteStartBeforeVoteEnd1(uint256 pId) proposalSnapshot(pId) < proposalDeadline(pId)
  35. invariant voteStartBeforeVoteEnd(uint256 pId)
  36. (proposalSnapshot(pId) > 0 => proposalSnapshot(pId) < proposalDeadline(pId))
  37. && (proposalSnapshot(pId) == 0 => proposalDeadline(pId) == 0)
  38. /*
  39. proposalSnapshot(pId) < proposalDeadline(pId) || (proposalSnapshot(pId) == 0 && proposalDeadline(pId) == 0)
  40. { preserved {
  41. require initialized(pId) == true;
  42. }}
  43. */
  44. /**
  45. * A proposal cannot be both executed and canceled.
  46. */
  47. invariant noBothExecutedAndCanceled(uint256 pId)
  48. !isExecuted(pId) || !isCanceled(pId)
  49. /**
  50. * A proposal cannot be neither executed nor canceled before it starts
  51. */
  52. invariant noExecuteOrCancelBeforeStarting(env e, uint256 pId)
  53. e.block.number < proposalSnapshot(pId)
  54. => !isExecuted(pId) && !isCanceled(pId)
  55. /**
  56. * A proposal could be executed only if quorum was reached and vote succeeded
  57. */
  58. invariant executionOnlyIfQuoromReachedAndVoteSucceeded(uint256 pId)
  59. isExecuted(pId) => _quorumReached(pId) && _voteSucceeded(pId)
  60. /*
  61. * No functions should be allowed to run after a job is deemed as canceled
  62. */
  63. invariant cannotSetIfCanceled(uint256 pId)
  64. isCanceled(pId) => lastReverted == true
  65. /*
  66. * No functions should be allowed to run after a job is deemed as executed
  67. */
  68. invariant cannotSetIfExecuted(uint256 pId)
  69. isExecuted(pId) => lastReverted == true
  70. {
  71. preserved execute(address[] targets, uint256[] values, bytes[] calldatas, bytes32 descriptionHash) with (env e)
  72. {
  73. require(isExecuted(pId));
  74. }
  75. }
  76. /*
  77. * sum of all votes casted is equal to the sum of voting power of those who voted
  78. */
  79. invariant SumOfVotesCastEqualSumOfPowerOfVoted(uint256 pId)
  80. counted_weight(pId) == counter_vote_power_by_id(pId) &&
  81. counted_weight(pId) == vote_power_ghost &&
  82. counter_vote_power_by_id(pId) == vote_power_ghost
  83. //////////////////////////////////////////////////////////////////////////////
  84. /////////////////////////////////// RULES ////////////////////////////////////
  85. //////////////////////////////////////////////////////////////////////////////
  86. /**
  87. * The voting must start not before the proposal’s creation time
  88. */
  89. rule noStartBeforeCreation(uint256 pId) {
  90. uint previousStart = proposalSnapshot(pId);
  91. require previousStart == 0;
  92. env e;
  93. calldataarg arg;
  94. propose(e, arg);
  95. uint newStart = proposalSnapshot(pId);
  96. // if created, start is after creation
  97. assert newStart != 0 => newStart >= e.block.number;
  98. }
  99. /**
  100. * Check hashProposal hashing is reliable (different inputs lead to different buffers hashed)
  101. */
  102. /*
  103. rule checkHashProposal {
  104. address[] t1;
  105. address[] t2;
  106. uint256[] v1;
  107. uint256[] v2;
  108. bytes[] c1;
  109. bytes[] c2;
  110. bytes32 d1;
  111. bytes32 d2;
  112. uint256 h1 = hashProposal(t1,v1,c1,d1);
  113. uint256 h2 = hashProposal(t2,v2,c2,d2);
  114. bool equalHashes = h1 == h2;
  115. assert equalHashes => t1.length == t2.length;
  116. assert equalHashes => v1.length == v2.length;
  117. assert equalHashes => c1.length == c2.length;
  118. assert equalHashes => d1 == d2;
  119. }
  120. */
  121. /**
  122. * Once a proposal is created, voteStart and voteEnd are immutable
  123. */
  124. rule immutableFieldsAfterProposalCreation(uint256 pId, method f) {
  125. uint _voteStart = proposalSnapshot(pId);
  126. uint _voteEnd = proposalDeadline(pId);
  127. require _voteStart > 0; // proposal was created
  128. env e;
  129. calldataarg arg;
  130. f(e, arg);
  131. uint voteStart_ = proposalSnapshot(pId);
  132. uint voteEnd_ = proposalDeadline(pId);
  133. assert _voteStart == voteStart_;
  134. assert _voteEnd == voteEnd_;
  135. }
  136. /**
  137. * Check if it's possible to vote two time. Relevant to GovernorCountingSimpleHarness.sol contract
  138. */
  139. rule doubleVoting(uint256 pId, uint8 sup) {
  140. env e;
  141. address user = e.msg.sender;
  142. bool votedCheck = hasVoted(e, pId, user);
  143. require votedCheck == true;
  144. castVote@withrevert(e, pId, sup);
  145. bool reverted = lastReverted;
  146. assert reverted, "double voting accured";
  147. }