ERC721Votes.spec 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // sum of user balances is >= total amount of delegated votes
  52. // blocked by tool error
  53. invariant votes_solvency()
  54. to_mathint(totalSupply()) >= totalVotes()
  55. { preserved with(env e) {
  56. require forall address account. numCheckpoints(account) < 1000000;
  57. } }
  58. // for some checkpoint, the fromBlock is less than the current block number
  59. // passes but fails rule sanity from hash on delegate by sig
  60. invariant timestamp_constrains_fromBlock(address account, uint32 index, env e)
  61. ckptFromBlock(account, index) < e.block.number
  62. {
  63. preserved {
  64. require index < numCheckpoints(account);
  65. }
  66. }
  67. // numCheckpoints are less than maxInt
  68. // passes because numCheckpoints does a safeCast
  69. // invariant maxInt_constrains_numBlocks(address account)
  70. // numCheckpoints(account) < 4294967295 // 2^32
  71. // can't have more checkpoints for a given account than the last from block
  72. // passes
  73. invariant fromBlock_constrains_numBlocks(address account)
  74. numCheckpoints(account) <= ckptFromBlock(account, numCheckpoints(account) - 1)
  75. { preserved with(env e) {
  76. require e.block.number >= ckptFromBlock(account, numCheckpoints(account) - 1); // this should be true from the invariant above!!
  77. }}
  78. // for any given checkpoint, the fromBlock must be greater than the checkpoint
  79. // this proves the above invariant in combination with the below invariant
  80. // if checkpoint has a greater fromBlock than the last, and the FromBlock is always greater than the pos.
  81. // Then the number of positions must be less than the currentFromBlock
  82. // ^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
  83. // passes + rule sanity
  84. invariant fromBlock_greaterThanEq_pos(address account, uint32 pos)
  85. ckptFromBlock(account, pos) >= pos
  86. // a larger index must have a larger fromBlock
  87. // passes + rule sanity
  88. invariant fromBlock_increasing(address account, uint32 pos, uint32 pos2)
  89. pos > pos2 => ckptFromBlock(account, pos) > ckptFromBlock(account, pos2)
  90. // converted from an invariant to a rule to slightly change the logic
  91. // if the fromBlock is the same as before, then the number of checkpoints stays the same
  92. // however if the fromBlock is new than the number of checkpoints increases
  93. // passes, fails rule sanity because tautology check seems to be bugged
  94. rule unique_checkpoints_rule(method f) {
  95. env e; calldataarg args;
  96. address account;
  97. uint32 num_ckpts_ = numCheckpoints(account);
  98. uint32 fromBlock_ = num_ckpts_ == 0 ? 0 : ckptFromBlock(account, num_ckpts_ - 1);
  99. f(e, args);
  100. uint32 _num_ckpts = numCheckpoints(account);
  101. uint32 _fromBlock = _num_ckpts == 0 ? 0 : ckptFromBlock(account, _num_ckpts - 1);
  102. assert fromBlock_ == _fromBlock => num_ckpts_ == _num_ckpts || _num_ckpts == 1, "same fromBlock, new checkpoint";
  103. // this assert fails consistently
  104. // assert !doubleFromBlock(account) => ckpts_ != _ckpts, "new fromBlock but total checkpoints not being increased";
  105. }
  106. // assumes neither account has delegated
  107. // currently fails due to this scenario. A has maxint number of checkpoints
  108. // an additional checkpoint is added which overflows and sets A's votes to 0
  109. // passes + rule sanity (- a bad tautology check)
  110. rule transfer_safe() {
  111. env e;
  112. uint256 ID;
  113. address a; address b;
  114. require delegates(a) != delegates(b); // confirmed if they both delegate to the same person then transfer keeps the votes the same
  115. require numCheckpoints(delegates(a)) < 1000000;
  116. require numCheckpoints(delegates(b)) < 1000000;
  117. uint256 votesA_pre = getVotes(delegates(a));
  118. uint256 votesB_pre = getVotes(delegates(b));
  119. mathint totalVotes_pre = totalVotes();
  120. transferFrom(e, a, b, ID);
  121. mathint totalVotes_post = totalVotes();
  122. uint256 votesA_post = getVotes(delegates(a));
  123. uint256 votesB_post = getVotes(delegates(b));
  124. // if an account that has not delegated transfers balance to an account that has, it will increase the total supply of votes
  125. assert totalVotes_pre == totalVotes_post, "transfer changed total supply";
  126. assert delegates(a) != 0 => votesA_pre - 1 == votesA_post, "A lost the wrong amount of votes";
  127. assert delegates(b) != 0 => votesB_pre + 1 == votesB_post, "B gained the wrong amount of votes";
  128. }
  129. // for any given function f, if the delegate is changed the function must be delegate or delegateBySig
  130. // passes
  131. rule delegates_safe(method f) filtered {f -> (f.selector != delegate(address).selector &&
  132. f.selector != _delegate(address, address).selector &&
  133. f.selector != delegateBySig(address, uint256, uint256, uint8, bytes32, bytes32).selector) }
  134. {
  135. env e; calldataarg args;
  136. address account;
  137. address pre = delegates(account);
  138. f(e, args);
  139. address post = delegates(account);
  140. assert pre == post, "invalid delegate change";
  141. }
  142. // delegates increases the delegatee's votes by the proper amount
  143. // passes + rule sanity
  144. rule delegatee_receives_votes() {
  145. env e;
  146. address delegator; address delegatee;
  147. require numCheckpoints(delegatee) < 1000000;
  148. require delegates(delegator) != delegatee;
  149. require delegatee != 0x0;
  150. uint256 delegator_bal = balanceOf(e, delegator);
  151. uint256 votes_= getVotes(delegatee);
  152. _delegate(e, delegator, delegatee);
  153. uint256 _votes = getVotes(delegatee);
  154. assert _votes == votes_ + delegator_bal, "delegatee did not receive votes";
  155. }
  156. // passes + rule sanity
  157. rule previous_delegatee_votes_removed() {
  158. env e;
  159. address delegator; address delegatee; address third;
  160. require third != delegatee;
  161. require delegates(delegator) == third;
  162. require numCheckpoints(third) < 1000000;
  163. uint256 delegator_bal = balanceOf(e, delegator);
  164. uint256 votes_ = getVotes(third);
  165. _delegate(e, delegator, delegatee);
  166. uint256 _votes = getVotes(third);
  167. assert third != 0x0 => _votes == votes_ - delegator_bal, "votes not removed from the previous delegatee";
  168. }
  169. // passes with rule sanity
  170. rule delegate_contained() {
  171. env e;
  172. address delegator; address delegatee; address other;
  173. require other != delegatee;
  174. require other != delegates(delegator);
  175. uint256 votes_ = getVotes(other);
  176. _delegate(e, delegator, delegatee);
  177. uint256 _votes = getVotes(other);
  178. assert votes_ == _votes, "votes not contained";
  179. }
  180. rule delegate_no_frontrunning(method f) {
  181. env e; calldataarg args;
  182. address delegator; address delegatee; address third; address other;
  183. require numCheckpoints(delegatee) < 1000000;
  184. require numCheckpoints(third) < 1000000;
  185. f(e, args);
  186. uint256 delegator_bal = balanceOf(e, delegator);
  187. uint256 delegatee_votes_ = getVotes(delegatee);
  188. uint256 third_votes_ = getVotes(third);
  189. uint256 other_votes_ = getVotes(other);
  190. require delegates(delegator) == third;
  191. require third != delegatee;
  192. require other != third;
  193. require other != delegatee;
  194. require delegatee != 0x0;
  195. _delegate(e, delegator, delegatee);
  196. uint256 _delegatee_votes = getVotes(delegatee);
  197. uint256 _third_votes = getVotes(third);
  198. uint256 _other_votes = getVotes(other);
  199. // previous delegatee loses all of their votes
  200. // delegatee gains that many votes
  201. // third loses any votes delegated to them
  202. assert _delegatee_votes == delegatee_votes_ + delegator_bal, "delegatee did not receive votes";
  203. assert third != 0 => _third_votes == third_votes_ - delegator_bal, "votes not removed from third";
  204. assert other_votes_ == _other_votes, "delegate not contained";
  205. }