GovernorCountingSimple.spec 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /*
  17. * ghost to keep track of changes in hasVoted status of users
  18. */
  19. ghost hasVoteGhost(uint256) returns uint256 {
  20. init_state axiom forall uint256 pId. hasVoteGhost(pId) == 0;
  21. }
  22. hook Sstore _proposalVotes[KEY uint256 pId].hasVoted[KEY address user] bool current_voting_State (bool old_voting_state) STORAGE{
  23. havoc hasVoteGhost assuming forall uint256 p. ((p == pId && current_voting_State && !old_voting_state) ? (hasVoteGhost@new(p) == hasVoteGhost@old(p) + 1) :
  24. (hasVoteGhost@new(p) == hasVoteGhost@old(p)));
  25. }
  26. //////////// ghosts to keep track of votes counting ////////////
  27. /*
  28. * the sum of voting power of those who voted
  29. */
  30. ghost sum_all_votes_power() returns uint256 {
  31. init_state axiom sum_all_votes_power() == 0;
  32. }
  33. hook Sstore ghost_sum_vote_power_by_id [KEY uint256 pId] uint256 current_power(uint256 old_power) STORAGE {
  34. havoc sum_all_votes_power assuming sum_all_votes_power@new() == sum_all_votes_power@old() - old_power + current_power;
  35. }
  36. /*
  37. * sum of all votes casted per proposal
  38. */
  39. ghost tracked_weight(uint256) returns uint256 {
  40. init_state axiom forall uint256 p. tracked_weight(p) == 0;
  41. }
  42. /*
  43. * sum of all votes casted
  44. */
  45. ghost sum_tracked_weight() returns uint256 {
  46. init_state axiom sum_tracked_weight() == 0;
  47. }
  48. /*
  49. * getter for _proposalVotes.againstVotes
  50. */
  51. ghost votesAgainst() returns uint256 {
  52. init_state axiom votesAgainst() == 0;
  53. }
  54. /*
  55. * getter for _proposalVotes.forVotes
  56. */
  57. ghost votesFor() returns uint256 {
  58. init_state axiom votesFor() == 0;
  59. }
  60. /*
  61. * getter for _proposalVotes.abstainVotes
  62. */
  63. ghost votesAbstain() returns uint256 {
  64. init_state axiom votesAbstain() == 0;
  65. }
  66. hook Sstore _proposalVotes [KEY uint256 pId].againstVotes 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 votesAgainst assuming votesAgainst@new() == votesAgainst@old() - old_votes + votes;
  71. }
  72. hook Sstore _proposalVotes [KEY uint256 pId].forVotes uint256 votes(uint256 old_votes) STORAGE {
  73. havoc tracked_weight assuming forall uint256 p.(p == pId => tracked_weight@new(p) == tracked_weight@old(p) - old_votes + votes) &&
  74. (p != pId => tracked_weight@new(p) == tracked_weight@old(p));
  75. havoc sum_tracked_weight assuming sum_tracked_weight@new() == sum_tracked_weight@old() - old_votes + votes;
  76. havoc votesFor assuming votesFor@new() == votesFor@old() - old_votes + votes;
  77. }
  78. hook Sstore _proposalVotes [KEY uint256 pId].abstainVotes uint256 votes(uint256 old_votes) STORAGE {
  79. havoc tracked_weight assuming forall uint256 p.(p == pId => tracked_weight@new(p) == tracked_weight@old(p) - old_votes + votes) &&
  80. (p != pId => tracked_weight@new(p) == tracked_weight@old(p));
  81. havoc sum_tracked_weight assuming sum_tracked_weight@new() == sum_tracked_weight@old() - old_votes + votes;
  82. havoc votesAbstain assuming votesAbstain@new() == votesAbstain@old() - old_votes + votes;
  83. }
  84. //////////////////////////////////////////////////////////////////////////////
  85. ////////////////////////////// INVARIANTS ////////////////////////////////////
  86. //////////////////////////////////////////////////////////////////////////////
  87. /*
  88. * sum of all votes casted is equal to the sum of voting power of those who voted, per each proposal
  89. */
  90. invariant SumOfVotesCastEqualSumOfPowerOfVotedPerProposal(uint256 pId)
  91. tracked_weight(pId) == ghost_sum_vote_power_by_id(pId)
  92. /*
  93. * sum of all votes casted is equal to the sum of voting power of those who voted
  94. */
  95. invariant SumOfVotesCastEqualSumOfPowerOfVoted()
  96. sum_tracked_weight() == sum_all_votes_power()
  97. /*
  98. * sum of all votes casted is greater or equal to the sum of voting power of those who voted at a specific proposal
  99. */
  100. invariant OneIsNotMoreThanAll(uint256 pId)
  101. sum_all_votes_power() >= tracked_weight(pId)
  102. //////////////////////////////////////////////////////////////////////////////
  103. ///////////////////////////////// RULES //////////////////////////////////////
  104. //////////////////////////////////////////////////////////////////////////////
  105. //NOT FINISHED
  106. /*
  107. * the sum of voting power of those who voted is less or equal to the maximum possible votes, per each proposal
  108. */
  109. rule possibleTotalVotes(uint256 pId, uint8 sup, env e, method f) {
  110. // add requireinvariant for all i, j. i = i - 1 && i < j => checkpointlookup[i] < checkpointlookup[j];
  111. require tracked_weight(pId) <= erc20votes.getPastTotalSupply(e, proposalSnapshot(pId));
  112. uint256 againstB;
  113. uint256 forB;
  114. uint256 absatinB;
  115. againstB, forB, absatinB = proposalVotes(pId);
  116. calldataarg args;
  117. //f(e, args);
  118. castVote(e, pId, sup);
  119. uint256 against;
  120. uint256 for;
  121. uint256 absatin;
  122. against, for, absatin = proposalVotes(pId);
  123. uint256 ps = proposalSnapshot(pId);
  124. assert tracked_weight(pId) <= erc20votes.getPastTotalSupply(e, proposalSnapshot(pId)), "bla bla bla";
  125. }
  126. /*
  127. * Checks that only one user is updated in the system when calling cast vote functions (assuming hasVoted is changing correctly, false->true, with every vote cast)
  128. */
  129. rule oneUserVotesInCast(uint256 pId, uint8 sup, method f) filtered {f -> f.selector == castVote(uint256, uint8).selector
  130. || f.selector == castVoteWithReason(uint256, uint8, string).selector
  131. || f.selector == castVoteBySig(uint256, uint8, uint8, bytes32, bytes32).selector} {
  132. env e; calldataarg args;
  133. uint256 ghost_Before = hasVoteGhost(pId);
  134. helperFunctionsWithRevert(pId, f, e);
  135. require(!lastReverted);
  136. uint256 ghost_After = hasVoteGhost(pId);
  137. assert(ghost_After == ghost_Before + 1, "Raised by more than 1");
  138. }
  139. /*
  140. * Only sender's voting status can be changed by execution of any cast vote function
  141. */
  142. rule noVoteForSomeoneElse(uint256 pId, uint8 sup, method f) filtered {f -> f.selector == castVote(uint256, uint8).selector
  143. || f.selector == castVoteWithReason(uint256, uint8, string).selector
  144. || f.selector == castVoteBySig(uint256, uint8, uint8, bytes32, bytes32).selector } {
  145. env e; calldataarg args;
  146. address voter = e.msg.sender;
  147. address user;
  148. bool hasVotedBefore_Voter = hasVoted(e, pId, voter);
  149. bool hasVotedBefore_User = hasVoted(e, pId, user);
  150. helperFunctionsWithRevert(pId, f, e);
  151. require(!lastReverted);
  152. bool hasVotedAfter_Voter = hasVoted(e, pId, voter);
  153. bool hasVotedAfter_User = hasVoted(e, pId, user);
  154. assert !hasVotedBefore_Voter && hasVotedAfter_Voter && (user != voter => hasVotedBefore_User == hasVotedAfter_User);
  155. }
  156. /*
  157. * Total voting tally is monotonically non-decreasing in every operation
  158. */
  159. rule votingWeightMonotonicity(method f){
  160. uint256 votingWeightBefore = sum_tracked_weight();
  161. env e;
  162. calldataarg args;
  163. f(e, args);
  164. uint256 votingWeightAfter = sum_tracked_weight();
  165. assert votingWeightBefore <= votingWeightAfter, "Voting weight was decreased somehow";
  166. }
  167. /*
  168. * A change in hasVoted must be correlated with an non-decreasing of the vote supports (nondecrease because user can vote with weight 0)
  169. */
  170. rule hasVotedCorrelation(uint256 pId, method f, env e, uint256 bn) filtered {f -> f.selector != 0x7d5e81e2}{
  171. address acc = e.msg.sender;
  172. uint256 againstBefore = votesAgainst();
  173. uint256 forBefore = votesFor();
  174. uint256 abstainBefore = votesAbstain();
  175. bool hasVotedBefore = hasVoted(e, pId, acc);
  176. helperFunctionsWithRevert(pId, f, e);
  177. uint256 againstAfter = votesAgainst();
  178. uint256 forAfter = votesFor();
  179. uint256 abstainAfter = votesAbstain();
  180. bool hasVotedAfter = hasVoted(e, pId, acc);
  181. assert (!hasVotedBefore && hasVotedAfter) => againstBefore <= againstAfter || forBefore <= forAfter || abstainBefore <= abstainAfter, "no correlation";
  182. }
  183. /*
  184. * Only privileged users can execute privileged operations, e.g. change _quorumNumerator or _timelock
  185. */
  186. rule privilegedOnlyNumerator(method f, uint256 newQuorumNumerator){
  187. env e;
  188. calldataarg arg;
  189. uint256 quorumNumBefore = quorumNumerator(e);
  190. f(e, arg);
  191. uint256 quorumNumAfter = quorumNumerator(e);
  192. address executorCheck = getExecutor(e);
  193. assert quorumNumBefore != quorumNumAfter => e.msg.sender == executorCheck, "non priveleged user changed quorum numerator";
  194. }
  195. rule privilegedOnlyTimelock(method f, uint256 newQuorumNumerator){
  196. env e;
  197. calldataarg arg;
  198. uint256 timelockBefore = timelock(e);
  199. f(e, arg);
  200. uint256 timelockAfter = timelock(e);
  201. assert timelockBefore != timelockAfter => e.msg.sender == timelockBefore, "non priveleged user changed timelock";
  202. }