ERC721Votes.spec 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using Checkpoints as Checkpoints
  2. methods {
  3. // functions
  4. checkpoints(address, uint32) envfree
  5. numCheckpoints(address) returns (uint32) envfree
  6. getVotes(address) returns (uint256) envfree
  7. getPastVotes(address, uint256) returns (uint256)
  8. getPastTotalSupply(uint256) returns (uint256)
  9. delegates(address) returns (address) envfree
  10. delegate(address)
  11. _delegate(address, address)
  12. delegateBySig(address, uint256, uint256, uint8, bytes32, bytes32)
  13. nonces(address) returns (uint256)
  14. totalSupply() returns (uint256) envfree
  15. _maxSupply() returns (uint224) envfree
  16. // harnesss functions
  17. ckptFromBlock(address, uint32) returns (uint32) envfree
  18. ckptVotes(address, uint32) returns (uint224) envfree
  19. mint(address, uint256)
  20. burn(uint256)
  21. unsafeNumCheckpoints(address) returns (uint256) envfree
  22. // solidity generated getters
  23. _delegation(address) returns (address) envfree
  24. // external functions
  25. }
  26. // gets the most recent votes for a user
  27. ghost userVotes(address) returns uint224{
  28. init_state axiom forall address a. userVotes(a) == 0;
  29. }
  30. // sums the total votes for all users
  31. ghost totalVotes() returns mathint {
  32. init_state axiom totalVotes() == 0;
  33. axiom totalVotes() >= 0;
  34. }
  35. hook Sstore _checkpoints[KEY address account].votes uint224 newVotes (uint224 oldVotes) STORAGE {
  36. havoc userVotes assuming
  37. userVotes@new(account) == newVotes;
  38. havoc totalVotes assuming
  39. totalVotes@new() == totalVotes@old() + to_mathint(newVotes) - to_mathint(userVotes(account));
  40. }
  41. ghost lastFromBlock(address) returns uint32;
  42. ghost doubleFromBlock(address) returns bool {
  43. init_state axiom forall address a. doubleFromBlock(a) == false;
  44. }
  45. hook Sstore _checkpoints[KEY address account].fromBlock uint32 newBlock (uint32 oldBlock) STORAGE {
  46. havoc lastFromBlock assuming
  47. lastFromBlock@new(account) == newBlock;
  48. havoc doubleFromBlock assuming
  49. doubleFromBlock@new(account) == (newBlock == lastFromBlock(account));
  50. }
  51. // for some checkpoint, the fromBlock is less than the current block number
  52. // passes but fails rule sanity from hash on delegate by sig
  53. invariant timestamp_constrains_fromBlock(address account, uint32 index, env e)
  54. ckptFromBlock(account, index) < e.block.number
  55. filtered { f -> !f.isView }
  56. {
  57. preserved {
  58. require index < numCheckpoints(account);
  59. }
  60. }
  61. // numCheckpoints are less than maxInt
  62. // passes because numCheckpoints does a safeCast
  63. // invariant maxInt_constrains_numBlocks(address account)
  64. // numCheckpoints(account) < 4294967295 // 2^32
  65. // can't have more checkpoints for a given account than the last from block
  66. // passes
  67. invariant fromBlock_constrains_numBlocks(address account)
  68. numCheckpoints(account) <= ckptFromBlock(account, numCheckpoints(account) - 1)
  69. filtered { f -> !f.isView }
  70. { preserved with(env e) {
  71. require e.block.number >= ckptFromBlock(account, numCheckpoints(account) - 1); // this should be true from the invariant above!!
  72. }}
  73. // for any given checkpoint, the fromBlock must be greater than the checkpoint
  74. // this proves the above invariant in combination with the below invariant
  75. // if checkpoint has a greater fromBlock than the last, and the FromBlock is always greater than the pos.
  76. // Then the number of positions must be less than the currentFromBlock
  77. // ^note that the tool is assuming it's possible for the starting fromBlock to be 0 or anything, and does not know the current starting block
  78. // passes + rule sanity
  79. invariant fromBlock_greaterThanEq_pos(address account, uint32 pos)
  80. ckptFromBlock(account, pos) >= pos
  81. filtered { f -> !f.isView }
  82. // a larger index must have a larger fromBlock
  83. // passes + rule sanity
  84. invariant fromBlock_increasing(address account, uint32 pos, uint32 pos2)
  85. pos > pos2 => ckptFromBlock(account, pos) > ckptFromBlock(account, pos2)
  86. filtered { f -> !f.isView }
  87. // converted from an invariant to a rule to slightly change the logic
  88. // if the fromBlock is the same as before, then the number of checkpoints stays the same
  89. // however if the fromBlock is new than the number of checkpoints increases
  90. // passes, fails rule sanity because tautology check seems to be bugged
  91. rule unique_checkpoints_rule(method f) {
  92. env e; calldataarg args;
  93. address account;
  94. uint32 num_ckpts_ = numCheckpoints(account);
  95. uint32 fromBlock_ = num_ckpts_ == 0 ? 0 : ckptFromBlock(account, num_ckpts_ - 1);
  96. f(e, args);
  97. uint32 _num_ckpts = numCheckpoints(account);
  98. uint32 _fromBlock = _num_ckpts == 0 ? 0 : ckptFromBlock(account, _num_ckpts - 1);
  99. assert fromBlock_ == _fromBlock => num_ckpts_ == _num_ckpts || _num_ckpts == 1, "same fromBlock, new checkpoint";
  100. // this assert fails consistently
  101. // assert !doubleFromBlock(account) => ckpts_ != _ckpts, "new fromBlock but total checkpoints not being increased";
  102. }
  103. // assumes neither account has delegated
  104. // currently fails due to this scenario. A has maxint number of checkpoints
  105. // an additional checkpoint is added which overflows and sets A's votes to 0
  106. // passes + rule sanity (- a bad tautology check)
  107. rule transfer_safe() {
  108. env e;
  109. uint256 ID;
  110. address a; address b;
  111. require delegates(a) != delegates(b); // confirmed if they both delegate to the same person then transfer keeps the votes the same
  112. require numCheckpoints(delegates(a)) < 1000000;
  113. require numCheckpoints(delegates(b)) < 1000000;
  114. uint256 votesA_pre = getVotes(delegates(a));
  115. uint256 votesB_pre = getVotes(delegates(b));
  116. mathint totalVotes_pre = totalVotes();
  117. transferFrom(e, a, b, ID);
  118. mathint totalVotes_post = totalVotes();
  119. uint256 votesA_post = getVotes(delegates(a));
  120. uint256 votesB_post = getVotes(delegates(b));
  121. // if an account that has not delegated transfers balance to an account that has, it will increase the total supply of votes
  122. assert totalVotes_pre == totalVotes_post, "transfer changed total supply";
  123. assert delegates(a) != 0 => votesA_pre - 1 == votesA_post, "A lost the wrong amount of votes";
  124. assert delegates(b) != 0 => votesB_pre + 1 == votesB_post, "B gained the wrong amount of votes";
  125. }
  126. // for any given function f, if the delegate is changed the function must be delegate or delegateBySig
  127. // passes
  128. rule delegates_safe(method f) filtered {f -> (f.selector != delegate(address).selector &&
  129. f.selector != _delegate(address, address).selector &&
  130. f.selector != delegateBySig(address, uint256, uint256, uint8, bytes32, bytes32).selector) }
  131. {
  132. env e; calldataarg args;
  133. address account;
  134. address pre = delegates(account);
  135. f(e, args);
  136. address post = delegates(account);
  137. assert pre == post, "invalid delegate change";
  138. }
  139. // delegates increases the delegatee's votes by the proper amount
  140. // passes + rule sanity
  141. rule delegatee_receives_votes() {
  142. env e;
  143. address delegator; address delegatee;
  144. require numCheckpoints(delegatee) < 1000000;
  145. require delegates(delegator) != delegatee;
  146. require delegatee != 0x0;
  147. uint256 delegator_bal = balanceOf(e, delegator);
  148. uint256 votes_= getVotes(delegatee);
  149. _delegate(e, delegator, delegatee);
  150. uint256 _votes = getVotes(delegatee);
  151. assert _votes == votes_ + delegator_bal, "delegatee did not receive votes";
  152. }
  153. // passes + rule sanity
  154. rule previous_delegatee_votes_removed() {
  155. env e;
  156. address delegator; address delegatee; address third;
  157. require third != delegatee;
  158. require delegates(delegator) == third;
  159. require numCheckpoints(third) < 1000000;
  160. uint256 delegator_bal = balanceOf(e, delegator);
  161. uint256 votes_ = getVotes(third);
  162. _delegate(e, delegator, delegatee);
  163. uint256 _votes = getVotes(third);
  164. assert third != 0x0 => _votes == votes_ - delegator_bal, "votes not removed from the previous delegatee";
  165. }
  166. // passes with rule sanity
  167. rule delegate_contained() {
  168. env e;
  169. address delegator; address delegatee; address other;
  170. require other != delegatee;
  171. require other != delegates(delegator);
  172. uint256 votes_ = getVotes(other);
  173. _delegate(e, delegator, delegatee);
  174. uint256 _votes = getVotes(other);
  175. assert votes_ == _votes, "votes not contained";
  176. }
  177. rule delegate_no_frontrunning(method f) {
  178. env e; calldataarg args;
  179. address delegator; address delegatee; address third; address other;
  180. require numCheckpoints(delegatee) < 1000000;
  181. require numCheckpoints(third) < 1000000;
  182. f(e, args);
  183. uint256 delegator_bal = balanceOf(e, delegator);
  184. uint256 delegatee_votes_ = getVotes(delegatee);
  185. uint256 third_votes_ = getVotes(third);
  186. uint256 other_votes_ = getVotes(other);
  187. require delegates(delegator) == third;
  188. require third != delegatee;
  189. require other != third;
  190. require other != delegatee;
  191. require delegatee != 0x0;
  192. _delegate(e, delegator, delegatee);
  193. uint256 _delegatee_votes = getVotes(delegatee);
  194. uint256 _third_votes = getVotes(third);
  195. uint256 _other_votes = getVotes(other);
  196. // previous delegatee loses all of their votes
  197. // delegatee gains that many votes
  198. // third loses any votes delegated to them
  199. assert _delegatee_votes == delegatee_votes_ + delegator_bal, "delegatee did not receive votes";
  200. assert third != 0 => _third_votes == third_votes_ - delegator_bal, "votes not removed from third";
  201. assert other_votes_ == _other_votes, "delegate not contained";
  202. }