ERC20Votes.spec 11 KB

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