ERC20Votes.spec 11 KB

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