GovernorCountingSimple.spec 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import "GovernorBase.spec"
  2. using ERC20VotesHarness as erc20votes
  3. methods {
  4. ghost_sum_vote_power_by_id(uint256) returns uint256 envfree
  5. quorum(uint256) returns uint256
  6. proposalVotes(uint256) returns (uint256, uint256, uint256) envfree
  7. quorumNumerator() returns uint256
  8. _executor() returns address
  9. erc20votes._getPastVotes(address, uint256) returns uint256
  10. getExecutor() returns address
  11. timelock() returns address
  12. }
  13. //////////////////////////////////////////////////////////////////////////////
  14. ///////////////////////////////// GHOSTS /////////////////////////////////////
  15. //////////////////////////////////////////////////////////////////////////////
  16. //////////// ghosts to keep track of votes counting ////////////
  17. /*
  18. * the sum of voting power of those who voted
  19. */
  20. ghost sum_all_votes_power() returns uint256 {
  21. init_state axiom sum_all_votes_power() == 0;
  22. }
  23. hook Sstore ghost_sum_vote_power_by_id [KEY uint256 pId] uint256 current_power(uint256 old_power) STORAGE {
  24. havoc sum_all_votes_power assuming sum_all_votes_power@new() == sum_all_votes_power@old() - old_power + current_power;
  25. }
  26. /*
  27. * sum of all votes casted per proposal
  28. */
  29. ghost tracked_weight(uint256) returns uint256 {
  30. init_state axiom forall uint256 p. tracked_weight(p) == 0;
  31. }
  32. /*
  33. * sum of all votes casted
  34. */
  35. ghost sum_tracked_weight() returns uint256 {
  36. init_state axiom sum_tracked_weight() == 0;
  37. }
  38. /*
  39. * getter for _proposalVotes.againstVotes
  40. */
  41. ghost votesAgainst() returns uint256 {
  42. init_state axiom votesAgainst() == 0;
  43. }
  44. /*
  45. * getter for _proposalVotes.forVotes
  46. */
  47. ghost votesFor() returns uint256 {
  48. init_state axiom votesFor() == 0;
  49. }
  50. /*
  51. * getter for _proposalVotes.abstainVotes
  52. */
  53. ghost votesAbstain() returns uint256 {
  54. init_state axiom votesAbstain() == 0;
  55. }
  56. hook Sstore _proposalVotes [KEY uint256 pId].againstVotes uint256 votes(uint256 old_votes) STORAGE {
  57. havoc tracked_weight assuming forall uint256 p.(p == pId => tracked_weight@new(p) == tracked_weight@old(p) - old_votes + votes) &&
  58. (p != pId => tracked_weight@new(p) == tracked_weight@old(p));
  59. havoc sum_tracked_weight assuming sum_tracked_weight@new() == sum_tracked_weight@old() - old_votes + votes;
  60. havoc votesAgainst assuming votesAgainst@new() == votesAgainst@old() - old_votes + votes;
  61. }
  62. hook Sstore _proposalVotes [KEY uint256 pId].forVotes uint256 votes(uint256 old_votes) STORAGE {
  63. havoc tracked_weight assuming forall uint256 p.(p == pId => tracked_weight@new(p) == tracked_weight@old(p) - old_votes + votes) &&
  64. (p != pId => tracked_weight@new(p) == tracked_weight@old(p));
  65. havoc sum_tracked_weight assuming sum_tracked_weight@new() == sum_tracked_weight@old() - old_votes + votes;
  66. havoc votesFor assuming votesFor@new() == votesFor@old() - old_votes + votes;
  67. }
  68. hook Sstore _proposalVotes [KEY uint256 pId].abstainVotes uint256 votes(uint256 old_votes) STORAGE {
  69. havoc tracked_weight assuming forall uint256 p.(p == pId => tracked_weight@new(p) == tracked_weight@old(p) - old_votes + votes) &&
  70. (p != pId => tracked_weight@new(p) == tracked_weight@old(p));
  71. havoc sum_tracked_weight assuming sum_tracked_weight@new() == sum_tracked_weight@old() - old_votes + votes;
  72. havoc votesAbstain assuming votesAbstain@new() == votesAbstain@old() - old_votes + votes;
  73. }
  74. //////////////////////////////////////////////////////////////////////////////
  75. ////////////////////////////// INVARIANTS ////////////////////////////////////
  76. //////////////////////////////////////////////////////////////////////////////
  77. /*
  78. * sum of all votes casted is equal to the sum of voting power of those who voted, per each proposal
  79. */
  80. invariant SumOfVotesCastEqualSumOfPowerOfVotedPerProposal(uint256 pId)
  81. tracked_weight(pId) == ghost_sum_vote_power_by_id(pId)
  82. /*
  83. * sum of all votes casted is equal to the sum of voting power of those who voted
  84. */
  85. invariant SumOfVotesCastEqualSumOfPowerOfVoted()
  86. sum_tracked_weight() == sum_all_votes_power()
  87. /*
  88. * sum of all votes casted is greater or equal to the sum of voting power of those who voted at a specific proposal
  89. */
  90. invariant OneIsNotMoreThanAll(uint256 pId)
  91. sum_all_votes_power() >= tracked_weight(pId)
  92. //////////////////////////////////////////////////////////////////////////////
  93. ///////////////////////////////// RULES //////////////////////////////////////
  94. //////////////////////////////////////////////////////////////////////////////
  95. /*
  96. * Only sender's voting status can be changed by execution of any cast vote function
  97. */
  98. // Checked for castVote only. all 3 castVote functions call _castVote, so the completness of the verification is counted on
  99. // the fact that the 3 functions themselves makes no chages, but rather call an internal function to execute.
  100. // That means that we do not check those 3 functions directly, however for castVote & castVoteWithReason it is quite trivial
  101. // to understand why this is ok. For castVoteBySig we basically assume that the signature referendum is correct without checking it.
  102. // We could check each function seperately and pass the rule, but that would have uglyfied the code with no concrete
  103. // 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.
  104. rule noVoteForSomeoneElse(uint256 pId, uint8 sup, method f) {
  105. env e; calldataarg args;
  106. address voter = e.msg.sender;
  107. address user;
  108. bool hasVotedBefore_User = hasVoted(e, pId, user);
  109. castVote@withrevert(e, pId, sup);
  110. require(!lastReverted);
  111. bool hasVotedAfter_User = hasVoted(e, pId, user);
  112. assert user != voter => hasVotedBefore_User == hasVotedAfter_User;
  113. }
  114. /*
  115. * Total voting tally is monotonically non-decreasing in every operation
  116. */
  117. rule votingWeightMonotonicity(method f){
  118. uint256 votingWeightBefore = sum_tracked_weight();
  119. env e;
  120. calldataarg args;
  121. f(e, args);
  122. uint256 votingWeightAfter = sum_tracked_weight();
  123. assert votingWeightBefore <= votingWeightAfter, "Voting weight was decreased somehow";
  124. }
  125. /*
  126. * A change in hasVoted must be correlated with an non-decreasing of the vote supports (nondecrease because user can vote with weight 0)
  127. */
  128. rule hasVotedCorrelation(uint256 pId, method f, env e, uint256 bn) {
  129. address acc = e.msg.sender;
  130. uint256 againstBefore = votesAgainst();
  131. uint256 forBefore = votesFor();
  132. uint256 abstainBefore = votesAbstain();
  133. bool hasVotedBefore = hasVoted(e, pId, acc);
  134. helperFunctionsWithRevert(pId, f, e);
  135. require(!lastReverted);
  136. uint256 againstAfter = votesAgainst();
  137. uint256 forAfter = votesFor();
  138. uint256 abstainAfter = votesAbstain();
  139. bool hasVotedAfter = hasVoted(e, pId, acc);
  140. assert (!hasVotedBefore && hasVotedAfter) => againstBefore <= againstAfter || forBefore <= forAfter || abstainBefore <= abstainAfter, "no correlation";
  141. }
  142. /*
  143. * Only privileged users can execute privileged operations, e.g. change _quorumNumerator or _timelock
  144. */
  145. rule privilegedOnlyNumerator(method f, uint256 newQuorumNumerator){
  146. env e;
  147. calldataarg arg;
  148. uint256 quorumNumBefore = quorumNumerator(e);
  149. f(e, arg);
  150. uint256 quorumNumAfter = quorumNumerator(e);
  151. address executorCheck = getExecutor(e);
  152. assert quorumNumBefore != quorumNumAfter => e.msg.sender == executorCheck, "non priveleged user changed quorum numerator";
  153. }
  154. rule privilegedOnlyTimelock(method f, uint256 newQuorumNumerator){
  155. env e;
  156. calldataarg arg;
  157. uint256 timelockBefore = timelock(e);
  158. f(e, arg);
  159. uint256 timelockAfter = timelock(e);
  160. assert timelockBefore != timelockAfter => e.msg.sender == timelockBefore, "non priveleged user changed timelock";
  161. }