GovernorCountingSimple.spec 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. //NOT FINISHED
  96. /*
  97. * the sum of voting power of those who voted is less or equal to the maximum possible votes, per each proposal
  98. */
  99. rule possibleTotalVotes(uint256 pId, uint8 sup, env e, method f) {
  100. // add requireinvariant for all i, j. i = i - 1 && i < j => checkpointlookup[i] < checkpointlookup[j];
  101. require tracked_weight(pId) <= erc20votes.getPastTotalSupply(e, proposalSnapshot(pId));
  102. uint256 againstB;
  103. uint256 forB;
  104. uint256 absatinB;
  105. againstB, forB, absatinB = proposalVotes(pId);
  106. calldataarg args;
  107. //f(e, args);
  108. castVote(e, pId, sup);
  109. uint256 against;
  110. uint256 for;
  111. uint256 absatin;
  112. against, for, absatin = proposalVotes(pId);
  113. uint256 ps = proposalSnapshot(pId);
  114. assert tracked_weight(pId) <= erc20votes.getPastTotalSupply(e, proposalSnapshot(pId)), "bla bla bla";
  115. }
  116. /*
  117. * Only sender's voting status can be changed by execution of any cast vote function
  118. */
  119. // Checked for castVote only. all 3 castVote functions call _castVote, so the completness of the verification is counted on
  120. // the fact that the 3 functions themselves makes no chages, but rather call an internal function to execute.
  121. // That means that we do not check those 3 functions directly, however for castVote & castVoteWithReason it is quite trivial
  122. // to understand why this is ok. For castVoteBySig we basically assume that the signature referendum is correct without checking it.
  123. // We could check each function seperately and pass the rule, but that would have uglyfied the code with no concrete
  124. // 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.
  125. rule noVoteForSomeoneElse(uint256 pId, uint8 sup, method f) {
  126. env e; calldataarg args;
  127. address voter = e.msg.sender;
  128. address user;
  129. bool hasVotedBefore_User = hasVoted(e, pId, user);
  130. castVote@withrevert(e, pId, sup);
  131. require(!lastReverted);
  132. bool hasVotedAfter_User = hasVoted(e, pId, user);
  133. assert user != voter => hasVotedBefore_User == hasVotedAfter_User;
  134. }
  135. /*
  136. * Total voting tally is monotonically non-decreasing in every operation
  137. */
  138. rule votingWeightMonotonicity(method f){
  139. uint256 votingWeightBefore = sum_tracked_weight();
  140. env e;
  141. calldataarg args;
  142. f(e, args);
  143. uint256 votingWeightAfter = sum_tracked_weight();
  144. assert votingWeightBefore <= votingWeightAfter, "Voting weight was decreased somehow";
  145. }
  146. /*
  147. * A change in hasVoted must be correlated with an non-decreasing of the vote supports (nondecrease because user can vote with weight 0)
  148. */
  149. rule hasVotedCorrelation(uint256 pId, method f, env e, uint256 bn) {
  150. address acc = e.msg.sender;
  151. uint256 againstBefore = votesAgainst();
  152. uint256 forBefore = votesFor();
  153. uint256 abstainBefore = votesAbstain();
  154. bool hasVotedBefore = hasVoted(e, pId, acc);
  155. helperFunctionsWithRevert(pId, f, e);
  156. require(!lastReverted);
  157. uint256 againstAfter = votesAgainst();
  158. uint256 forAfter = votesFor();
  159. uint256 abstainAfter = votesAbstain();
  160. bool hasVotedAfter = hasVoted(e, pId, acc);
  161. assert (!hasVotedBefore && hasVotedAfter) => againstBefore <= againstAfter || forBefore <= forAfter || abstainBefore <= abstainAfter, "no correlation";
  162. }
  163. /*
  164. * Only privileged users can execute privileged operations, e.g. change _quorumNumerator or _timelock
  165. */
  166. rule privilegedOnlyNumerator(method f, uint256 newQuorumNumerator){
  167. env e;
  168. calldataarg arg;
  169. uint256 quorumNumBefore = quorumNumerator(e);
  170. f(e, arg);
  171. uint256 quorumNumAfter = quorumNumerator(e);
  172. address executorCheck = getExecutor(e);
  173. assert quorumNumBefore != quorumNumAfter => e.msg.sender == executorCheck, "non priveleged user changed quorum numerator";
  174. }
  175. rule privilegedOnlyTimelock(method f, uint256 newQuorumNumerator){
  176. env e;
  177. calldataarg arg;
  178. uint256 timelockBefore = timelock(e);
  179. f(e, arg);
  180. uint256 timelockAfter = timelock(e);
  181. assert timelockBefore != timelockAfter => e.msg.sender == timelockBefore, "non priveleged user changed timelock";
  182. }