ERC721Votes.spec 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. // sums the total votes for all users
  29. ghost totalVotes() returns mathint {
  30. init_state axiom totalVotes() == 0;
  31. axiom totalVotes() >= 0;
  32. }
  33. hook Sstore _checkpoints[KEY address account].votes uint224 newVotes (uint224 oldVotes) STORAGE {
  34. havoc userVotes assuming
  35. userVotes@new(account) == newVotes;
  36. havoc totalVotes assuming
  37. totalVotes@new() == totalVotes@old() + to_mathint(newVotes) - to_mathint(userVotes(account));
  38. }
  39. ghost lastFromBlock(address) returns uint32;
  40. ghost doubleFromBlock(address) returns bool {
  41. init_state axiom forall address a. doubleFromBlock(a) == false;
  42. }
  43. hook Sstore _checkpoints[KEY address account].fromBlock uint32 newBlock (uint32 oldBlock) STORAGE {
  44. havoc lastFromBlock assuming
  45. lastFromBlock@new(account) == newBlock;
  46. havoc doubleFromBlock assuming
  47. doubleFromBlock@new(account) == (newBlock == lastFromBlock(account));
  48. }
  49. rule sanity(method f) {
  50. env e;
  51. calldataarg arg;
  52. f(e, arg);
  53. assert false;
  54. }
  55. // something stupid just to see
  56. invariant sanity_invariant()
  57. totalSupply() >= 0
  58. // sum of user balances is >= total amount of delegated votes
  59. // blocked by tool error
  60. invariant votes_solvency()
  61. to_mathint(totalSupply()) >= totalVotes()
  62. { preserved with(env e) {
  63. require forall address account. numCheckpoints(account) < 1000000;
  64. requireInvariant totalVotes_sums_accounts();
  65. } }
  66. invariant totalVotes_sums_accounts()
  67. forall address a. forall address b. (a != b && a != 0x0 && b != 0x0) => totalVotes() >= getVotes(delegates(a)) + getVotes(delegates(b))
  68. // for some checkpoint, the fromBlock is less than the current block number
  69. // passes but fails rule sanity from hash on delegate by sig
  70. invariant timestamp_constrains_fromBlock(address account, uint32 index, env e)
  71. ckptFromBlock(account, index) < e.block.number
  72. {
  73. preserved {
  74. require index < numCheckpoints(account);
  75. }
  76. }
  77. // TODO add a note about this in the report
  78. // // numCheckpoints are less than maxInt
  79. // // passes because numCheckpoints does a safeCast
  80. // invariant maxInt_constrains_numBlocks(address account)
  81. // numCheckpoints(account) < 4294967295 // 2^32
  82. // // fails because there are no checks to stop it
  83. // invariant maxInt_constrains_ckptsLength(address account)
  84. // unsafeNumCheckpoints(account) < 4294967295 // 2^32
  85. // can't have more checkpoints for a given account than the last from block
  86. // passes
  87. invariant fromBlock_constrains_numBlocks(address account)
  88. numCheckpoints(account) <= ckptFromBlock(account, numCheckpoints(account) - 1)
  89. { preserved with(env e) {
  90. require e.block.number >= ckptFromBlock(account, numCheckpoints(account) - 1); // this should be true from the invariant above!!
  91. }}
  92. // for any given checkpoint, the fromBlock must be greater than the checkpoint
  93. // this proves the above invariant in combination with the below invariant
  94. // if checkpoint has a greater fromBlock than the last, and the FromBlock is always greater than the pos.
  95. // Then the number of positions must be less than the currentFromBlock
  96. // ^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
  97. // passes + rule sanity
  98. invariant fromBlock_greaterThanEq_pos(address account, uint32 pos)
  99. ckptFromBlock(account, pos) >= pos
  100. // a larger index must have a larger fromBlock
  101. // passes + rule sanity
  102. invariant fromBlock_increasing(address account, uint32 pos, uint32 pos2)
  103. pos > pos2 => ckptFromBlock(account, pos) > ckptFromBlock(account, pos2)
  104. // converted from an invariant to a rule to slightly change the logic
  105. // if the fromBlock is the same as before, then the number of checkpoints stays the same
  106. // however if the fromBlock is new than the number of checkpoints increases
  107. // passes, fails rule sanity because tautology check seems to be bugged
  108. rule unique_checkpoints_rule(method f) {
  109. env e; calldataarg args;
  110. address account;
  111. uint32 num_ckpts_ = numCheckpoints(account);
  112. uint32 fromBlock_ = num_ckpts_ == 0 ? 0 : ckptFromBlock(account, num_ckpts_ - 1);
  113. f(e, args);
  114. uint32 _num_ckpts = numCheckpoints(account);
  115. uint32 _fromBlock = _num_ckpts == 0 ? 0 : ckptFromBlock(account, _num_ckpts - 1);
  116. assert fromBlock_ == _fromBlock => num_ckpts_ == _num_ckpts || _num_ckpts == 1, "same fromBlock, new checkpoint";
  117. // this assert fails consistently
  118. // assert !doubleFromBlock(account) => ckpts_ != _ckpts, "new fromBlock but total checkpoints not being increased";
  119. }
  120. // assumes neither account has delegated
  121. // currently fails due to this scenario. A has maxint number of checkpoints
  122. // an additional checkpoint is added which overflows and sets A's votes to 0
  123. // passes + rule sanity (- a bad tautology check)
  124. rule transfer_safe() {
  125. env e;
  126. uint256 ID;
  127. address a; address b;
  128. require delegates(a) != delegates(b); // confirmed if they both delegate to the same person then transfer keeps the votes the same
  129. require numCheckpoints(delegates(a)) < 1000000;
  130. require numCheckpoints(delegates(b)) < 1000000;
  131. uint256 votesA_pre = getVotes(delegates(a));
  132. uint256 votesB_pre = getVotes(delegates(b));
  133. mathint totalVotes_pre = totalVotes();
  134. transferFrom(e, a, b, ID);
  135. mathint totalVotes_post = totalVotes();
  136. uint256 votesA_post = getVotes(delegates(a));
  137. uint256 votesB_post = getVotes(delegates(b));
  138. // if an account that has not delegated transfers balance to an account that has, it will increase the total supply of votes
  139. assert totalVotes_pre == totalVotes_post, "transfer changed total supply";
  140. assert delegates(a) != 0 => votesA_pre - 1 == votesA_post, "A lost the wrong amount of votes";
  141. assert delegates(b) != 0 => votesB_pre + 1 == votesB_post, "B gained the wrong amount of votes";
  142. }
  143. // for any given function f, if the delegate is changed the function must be delegate or delegateBySig
  144. // passes
  145. rule delegates_safe(method f) filtered {f -> (f.selector != delegate(address).selector &&
  146. f.selector != _delegate(address, address).selector &&
  147. f.selector != delegateBySig(address, uint256, uint256, uint8, bytes32, bytes32).selector) }
  148. {
  149. env e; calldataarg args;
  150. address account;
  151. address pre = delegates(account);
  152. f(e, args);
  153. address post = delegates(account);
  154. assert pre == post, "invalid delegate change";
  155. }
  156. // delegates increases the delegatee's votes by the proper amount
  157. // passes + rule sanity
  158. rule delegatee_receives_votes() {
  159. env e;
  160. address delegator; address delegatee;
  161. require numCheckpoints(delegatee) < 1000000;
  162. require delegates(delegator) != delegatee;
  163. require delegatee != 0x0;
  164. uint256 delegator_bal = balanceOf(e, delegator);
  165. uint256 votes_= getVotes(delegatee);
  166. _delegate(e, delegator, delegatee);
  167. uint256 _votes = getVotes(delegatee);
  168. assert _votes == votes_ + delegator_bal, "delegatee did not receive votes";
  169. }
  170. // passes + rule sanity
  171. rule previous_delegatee_votes_removed() {
  172. env e;
  173. address delegator; address delegatee; address third;
  174. require third != delegatee;
  175. require delegates(delegator) == third;
  176. require numCheckpoints(third) < 1000000;
  177. uint256 delegator_bal = balanceOf(e, delegator);
  178. uint256 votes_ = getVotes(third);
  179. _delegate(e, delegator, delegatee);
  180. uint256 _votes = getVotes(third);
  181. assert third != 0x0 => _votes == votes_ - delegator_bal, "votes not removed from the previous delegatee";
  182. }
  183. // passes with rule sanity
  184. rule delegate_contained() {
  185. env e;
  186. address delegator; address delegatee; address other;
  187. require other != delegatee;
  188. require other != delegates(delegator);
  189. uint256 votes_ = getVotes(other);
  190. _delegate(e, delegator, delegatee);
  191. uint256 _votes = getVotes(other);
  192. assert votes_ == _votes, "votes not contained";
  193. }
  194. rule delegate_no_frontrunning(method f) {
  195. env e; calldataarg args;
  196. address delegator; address delegatee; address third; address other;
  197. require numCheckpoints(delegatee) < 1000000;
  198. require numCheckpoints(third) < 1000000;
  199. f(e, args);
  200. uint256 delegator_bal = balanceOf(e, delegator);
  201. uint256 delegatee_votes_ = getVotes(delegatee);
  202. uint256 third_votes_ = getVotes(third);
  203. uint256 other_votes_ = getVotes(other);
  204. require delegates(delegator) == third;
  205. require third != delegatee;
  206. require other != third;
  207. require other != delegatee;
  208. require delegatee != 0x0;
  209. _delegate(e, delegator, delegatee);
  210. uint256 _delegatee_votes = getVotes(delegatee);
  211. uint256 _third_votes = getVotes(third);
  212. uint256 _other_votes = getVotes(other);
  213. // previous delegatee loses all of their votes
  214. // delegatee gains that many votes
  215. // third loses any votes delegated to them
  216. assert _delegatee_votes == delegatee_votes_ + delegator_bal, "delegatee did not receive votes";
  217. assert third != 0 => _third_votes == third_votes_ - delegator_bal, "votes not removed from third";
  218. assert other_votes_ == _other_votes, "delegate not contained";
  219. }
  220. // mint and burn need to be handled differently for ERC721
  221. // rule mint_increases_totalSupply() {
  222. // env e;
  223. // uint256 amount; address account;
  224. // uint256 fromBlock = e.block.number;
  225. // uint256 totalSupply_ = totalSupply();
  226. // mint(e, account, amount);
  227. // uint256 _totalSupply = totalSupply();
  228. // require _totalSupply < _maxSupply();
  229. // assert _totalSupply == totalSupply_ + amount, "totalSupply not increased properly";
  230. // assert getPastTotalSupply(e, fromBlock) == totalSupply_ , "previous total supply not saved properly";
  231. // }
  232. // rule burn_decreases_totalSupply() {
  233. // env e;
  234. // uint256 amount; address account;
  235. // uint256 fromBlock = e.block.number;
  236. // uint256 totalSupply_ = totalSupply();
  237. // burn(e, account, amount);
  238. // uint256 _totalSupply = totalSupply();
  239. // assert _totalSupply == totalSupply_ - amount, "totalSupply not decreased properly";
  240. // assert getPastTotalSupply(e, fromBlock) == totalSupply_ , "previous total supply not saved properly";
  241. // }
  242. // rule mint_doesnt_increase_totalVotes() {
  243. // env e;
  244. // uint256 amount; address account;
  245. // mathint totalVotes_ = totalVotes();
  246. // mint(e, account, amount);
  247. // assert totalVotes() == totalVotes_, "totalVotes increased";
  248. // }
  249. // rule burn_doesnt_decrease_totalVotes() {
  250. // env e;
  251. // uint256 amount; address account;
  252. // mathint totalVotes_ = totalVotes();
  253. // burn(e, account, amount);
  254. // assert totalVotes() == totalVotes_, "totalVotes decreased";
  255. // }