ERC20Votes.spec 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using ERC20VotesHarness as erc20votes
  2. methods {
  3. // functions
  4. checkpoints(address, uint32) envfree
  5. numCheckpoints(address) returns (uint32) envfree
  6. delegates(address) returns (address) envfree
  7. getVotes(address) returns (uint256) envfree
  8. getPastVotes(address, uint256) returns (uint256)
  9. getPastTotalSupply(uint256) returns (uint256)
  10. delegate(address)
  11. _delegate(address, address)
  12. delegateBySig(address, uint256, uint256, uint8, bytes32, bytes32)
  13. totalSupply() returns (uint256) envfree
  14. _maxSupply() returns (uint224) envfree
  15. // harnesss functions
  16. ckptFromBlock(address, uint32) returns (uint32) envfree
  17. ckptVotes(address, uint32) returns (uint224) envfree
  18. mint(address, uint256)
  19. burn(address, uint256)
  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. // sums the total votes for all users
  27. ghost totalVotes() returns mathint {
  28. axiom totalVotes() > 0;
  29. }
  30. hook Sstore _checkpoints[KEY address account][INDEX uint32 index].votes uint224 newVotes (uint224 oldVotes) STORAGE {
  31. havoc userVotes assuming
  32. userVotes@new(account) == newVotes;
  33. havoc totalVotes assuming
  34. totalVotes@new() == totalVotes@old() + to_mathint(newVotes) - to_mathint(userVotes(account));
  35. }
  36. ghost lastFromBlock(address) returns uint32;
  37. ghost doubleFromBlock(address) returns bool {
  38. init_state axiom forall address a. doubleFromBlock(a) == false;
  39. }
  40. hook Sstore _checkpoints[KEY address account][INDEX uint32 index].fromBlock uint32 newBlock (uint32 oldBlock) STORAGE {
  41. havoc lastFromBlock assuming
  42. lastFromBlock@new(account) == newBlock;
  43. havoc doubleFromBlock assuming
  44. doubleFromBlock@new(account) == (newBlock == oldBlock);
  45. }
  46. rule sanity(method f) {
  47. env e;
  48. calldataarg arg;
  49. f(e, arg);
  50. assert false;
  51. }
  52. // something stupid just to see
  53. invariant sanity_invariant()
  54. totalSupply() >= 0
  55. // sum of user balances is >= total amount of delegated votes
  56. invariant votes_solvency()
  57. to_mathint(totalSupply()) >= totalVotes()
  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
  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. invariant fromBlock_constrains_numBlocks(address account)
  73. numCheckpoints(account) <= lastFromBlock(account)
  74. invariant unique_checkpoints(address account)
  75. !doubleFromBlock(account)
  76. rule transfer_safe() {
  77. env e;
  78. uint256 amount;
  79. address a; address b;
  80. require a != b;
  81. uint256 votesA_pre = getVotes(a);
  82. uint256 votesB_pre = getVotes(b);
  83. require votesA_pre == erc20votes.balanceOf(e, a);
  84. require votesB_pre == erc20votes.balanceOf(e, b);
  85. mathint totalVotes_pre = totalVotes();
  86. erc20votes.transferFrom(e, a, b, amount);
  87. mathint totalVotes_post = totalVotes();
  88. uint256 votesA_post = getVotes(a);
  89. uint256 votesB_post = getVotes(b);
  90. assert totalVotes_pre == totalVotes_post, "transfer changed total supply";
  91. assert votesA_pre - votesA_post == amount, "a lost the proper amount of votes";
  92. assert votesB_post - votesB_pre == amount, "b lost the proper amount of votes";
  93. }
  94. rule delegator_votes_removed() {
  95. env e;
  96. address delegator; address delegatee;
  97. require delegator != delegatee;
  98. require delegates(delegator) == 0; // has not delegated
  99. uint256 pre = getVotes(delegator);
  100. _delegate(e, delegator, delegatee);
  101. uint256 balance = balanceOf(e, delegator);
  102. uint256 post = getVotes(delegator);
  103. assert post == pre - balance, "delegator retained votes";
  104. }
  105. rule delegatee_receives_votes() {
  106. env e;
  107. address delegator; address delegatee;
  108. require delegator != delegatee;
  109. uint256 delegator_bal = balanceOf(e, delegator);
  110. uint256 votes_= getVotes(delegatee);
  111. _delegate(e, delegator, delegatee);
  112. uint256 _votes = getVotes(delegatee);
  113. assert _votes == votes_ + delegator_bal, "delegatee did not receive votes";
  114. }
  115. rule previous_delegatee_zerod() {
  116. env e;
  117. address delegator; address delegatee; address third;
  118. require delegator != delegatee;
  119. require third != delegatee;
  120. require third != delegator;
  121. uint256 delegator_bal = balanceOf(e, delegator);
  122. uint256 votes_ = getVotes(third);
  123. _delegate(e, delegator, delegatee);
  124. uint256 _votes = getVotes(third);
  125. assert votes_ == votes_ - delegator_bal, "votes not removed from the previous delegatee";
  126. }
  127. // passes
  128. rule delegate_contained() {
  129. env e;
  130. address delegator; address delegatee; address other;
  131. require delegator != delegatee;
  132. require other != delegator;
  133. require other != delegatee;
  134. require other != delegates(delegator);
  135. uint256 votes_ = getVotes(other);
  136. _delegate(e, delegator, delegatee);
  137. uint256 _votes = getVotes(other);
  138. assert votes_ == _votes, "votes not contained";
  139. }
  140. // checks all of the above rules with front running
  141. rule delegate_no_frontrunning(method f) {
  142. env e; calldataarg args;
  143. address delegator; address delegatee; address third; address other;
  144. require delegator != delegatee;
  145. require delegates(delegator) == third;
  146. require other != third;
  147. uint256 delegator_bal = erc20votes.balanceOf(e, delegator);
  148. uint256 dr_ = getVotes(delegator);
  149. uint256 de_ = getVotes(delegatee);
  150. uint256 third_ = getVotes(third);
  151. uint256 other_ = getVotes(other);
  152. // require third is address for previous delegator
  153. f(e, args);
  154. _delegate(e, delegator, delegatee);
  155. uint256 _dr = getVotes(delegator);
  156. uint256 _de = getVotes(delegatee);
  157. uint256 _third = getVotes(third);
  158. uint256 _other = getVotes(other);
  159. // delegator loses all of their votes
  160. // delegatee gains that many votes
  161. // third loses any votes delegated to them
  162. assert _dr == 0, "delegator retained votes";
  163. assert _de == de_ + delegator_bal, "delegatee not received votes";
  164. assert _third != 0 => _third == third_ - delegator_bal, "votes not removed from third";
  165. assert other_ == _other, "delegate not contained";
  166. }
  167. // passes
  168. rule mint_increases_totalSupply() {
  169. env e;
  170. uint256 amount; address account;
  171. uint256 fromBlock = e.block.number;
  172. uint256 totalSupply_ = totalSupply();
  173. mint(e, account, amount);
  174. uint256 _totalSupply = totalSupply();
  175. require _totalSupply < _maxSupply();
  176. assert _totalSupply == totalSupply_ + amount, "totalSupply not increased properly";
  177. assert getPastTotalSupply(e, fromBlock) == totalSupply_ , "previous total supply not saved properly";
  178. }
  179. // passes
  180. rule burn_decreases_totalSupply() {
  181. env e;
  182. uint256 amount; address account;
  183. uint256 fromBlock = e.block.number;
  184. uint256 totalSupply_ = totalSupply();
  185. require totalSupply_ > balanceOf(e, account);
  186. burn(e, account, amount);
  187. uint256 _totalSupply = totalSupply();
  188. require _totalSupply < _maxSupply();
  189. assert _totalSupply == totalSupply_ - amount, "totalSupply not decreased properly";
  190. assert getPastTotalSupply(e, fromBlock) == totalSupply_ , "previous total supply not saved properly";
  191. }
  192. // passes
  193. rule mint_doesnt_increase_totalVotes() {
  194. env e;
  195. uint256 amount; address account;
  196. mathint totalVotes_ = totalVotes();
  197. mint(e, account, amount);
  198. assert totalVotes() == totalVotes_, "totalVotes increased";
  199. }
  200. // passes
  201. rule burn_doesnt_decrease_totalVotes() {
  202. env e;
  203. uint256 amount; address account;
  204. mathint totalVotes_ = totalVotes();
  205. burn(e, account, amount);
  206. assert totalVotes() == totalVotes_, "totalVotes decreased";
  207. }
  208. // // fails
  209. // rule mint_increases_totalVotes() {
  210. // env e;
  211. // uint256 amount; address account;
  212. // mathint totalVotes_ = totalVotes();
  213. // mint(e, account, amount);
  214. // assert totalVotes() == totalVotes_ + to_mathint(amount), "totalVotes not increased";
  215. // }
  216. // // fails
  217. // rule burn_decreases_totalVotes() {
  218. // env e;
  219. // uint256 amount; address account;
  220. // mathint totalVotes_ = totalVotes();
  221. // burn(e, account, amount);
  222. // assert totalVotes() == totalVotes_ - to_mathint(amount), "totalVotes not decreased";
  223. // }