GovernorCountingSimple.spec 7.6 KB

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