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