ERC20Votes.spec 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. unsafeNumCheckpoints(address) returns (uint256) envfree
  21. // solidity generated getters
  22. _delegates(address) returns (address) envfree
  23. // external functions
  24. }
  25. // gets the most recent votes for a user
  26. ghost userVotes(address) returns uint224;
  27. // sums the total votes for all users
  28. ghost totalVotes() returns mathint {
  29. init_state axiom totalVotes() == 0;
  30. 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() + to_mathint(newVotes) - to_mathint(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. rule sanity(method f) {
  53. env e;
  54. calldataarg arg;
  55. f(e, arg);
  56. assert false;
  57. }
  58. // something stupid just to see
  59. invariant sanity_invariant()
  60. totalSupply() >= 0
  61. // sum of user balances is >= total amount of delegated votes
  62. // blocked by tool error
  63. invariant votes_solvency()
  64. to_mathint(totalSupply()) >= totalVotes()
  65. { preserved with(env e) {
  66. require forall address account. numCheckpoints(account) < 1000000;
  67. requireInvariant totalVotes_sums_accounts();
  68. } }
  69. invariant totalVotes_sums_accounts()
  70. forall address a. forall address b. (a != b && a != 0x0 && b != 0x0) => totalVotes() >= getVotes(delegates(a)) + getVotes(delegates(b))
  71. // for some checkpoint, the fromBlock is less than the current block number
  72. invariant blockNum_constrains_fromBlock(address account, uint32 index, env e)
  73. ckptFromBlock(account, index) < e.block.number
  74. {
  75. preserved {
  76. require index < numCheckpoints(account);
  77. }
  78. }
  79. // TODO add a note about this in the report
  80. // // numCheckpoints are less than maxInt
  81. // // passes because numCheckpoints does a safeCast
  82. // invariant maxInt_constrains_numBlocks(address account)
  83. // numCheckpoints(account) < 4294967295 // 2^32
  84. // // fails because there are no checks to stop it
  85. // invariant maxInt_constrains_ckptsLength(address account)
  86. // unsafeNumCheckpoints(account) < 4294967295 // 2^32
  87. // can't have more checkpoints for a given account than the last from block
  88. // passes
  89. invariant fromBlock_constrains_numBlocks(address account)
  90. numCheckpoints(account) <= ckptFromBlock(account, numCheckpoints(account) - 1)
  91. { preserved with(env e) {
  92. require e.block.number >= ckptFromBlock(account, numCheckpoints(account) - 1); // this should be true from the invariant above!!
  93. }}
  94. // for any given checkpoint, the fromBlock must be greater than the checkpoint
  95. // this proves the above invariant in combination with the below invariant
  96. // if checkpoint has a greater fromBlock than the last, and the FromBlock is always greater than the pos.
  97. // Then the number of positions must be less than the currentFromBlock
  98. // ^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
  99. // passes + rule sanity
  100. invariant fromBlock_greaterThanEq_pos(address account, uint32 pos)
  101. ckptFromBlock(account, pos) >= pos
  102. // a larger index must have a larger fromBlock
  103. // passes + rule sanity
  104. invariant fromBlock_increasing(address account, uint32 pos, uint32 pos2)
  105. pos > pos2 => ckptFromBlock(account, pos) > ckptFromBlock(account, pos2)
  106. // converted from an invariant to a rule to slightly change the logic
  107. // if the fromBlock is the same as before, then the number of checkpoints stays the same
  108. // however if the fromBlock is new than the number of checkpoints increases
  109. // passes, fails rule sanity because tautology check seems to be bugged
  110. rule unique_checkpoints_rule(method f) {
  111. env e; calldataarg args;
  112. address account;
  113. uint32 num_ckpts_ = numCheckpoints(account);
  114. uint32 fromBlock_ = num_ckpts_ == 0 ? 0 : ckptFromBlock(account, num_ckpts_ - 1);
  115. f(e, args);
  116. uint32 _num_ckpts = numCheckpoints(account);
  117. uint32 _fromBlock = _num_ckpts == 0 ? 0 : ckptFromBlock(account, _num_ckpts - 1);
  118. assert fromBlock_ == _fromBlock => num_ckpts_ == _num_ckpts || _num_ckpts == 1, "same fromBlock, new checkpoint";
  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 amount;
  127. address a; address b;
  128. // require a != b;
  129. require delegates(a) != delegates(b); // confirmed if they both delegate to the same person then transfer keeps the votes the same
  130. // requireInvariant fromBlock_constrains_numBlocks(a);
  131. // requireInvariant fromBlock_constrains_numBlocks(b);
  132. // requireInvariant totalVotes_gte_accounts(a, b);
  133. // require lastIndex(delegates(a)) < 1000000;
  134. // require lastIndex(delegates(b)) < 1000000;
  135. require numCheckpoints(delegates(a)) < 1000000;
  136. require numCheckpoints(delegates(b)) < 1000000;
  137. uint256 votesA_pre = getVotes(delegates(a));
  138. uint256 votesB_pre = getVotes(delegates(b));
  139. mathint totalVotes_pre = totalVotes();
  140. erc20votes.transferFrom(e, a, b, amount);
  141. mathint totalVotes_post = totalVotes();
  142. uint256 votesA_post = getVotes(delegates(a));
  143. uint256 votesB_post = getVotes(delegates(b));
  144. // if an account that has not delegated transfers balance to an account that has, it will increase the total supply of votes
  145. assert totalVotes_pre == totalVotes_post, "transfer changed total supply";
  146. assert delegates(a) != 0 => votesA_pre - votesA_post == amount, "A lost the wrong amount of votes";
  147. assert delegates(b) != 0 => votesB_post - votesB_pre == amount, "B lost the wrong amount of votes";
  148. }
  149. // for any given function f, if the delegate is changed the function must be delegate or delegateBySig
  150. // passes
  151. rule delegates_safe(method f) filtered {f -> (f.selector != delegate(address).selector &&
  152. f.selector != _delegate(address, address).selector &&
  153. f.selector != delegateBySig(address, uint256, uint256, uint8, bytes32, bytes32).selector) }
  154. {
  155. env e; calldataarg args;
  156. address account;
  157. address pre = delegates(account);
  158. f(e, args);
  159. address post = delegates(account);
  160. assert pre == post, "invalid delegate change";
  161. }
  162. // delegates increases the delegatee's votes by the proper amount
  163. // passes + rule sanity
  164. rule delegatee_receives_votes() {
  165. env e;
  166. address delegator; address delegatee;
  167. require delegates(delegator) != delegatee;
  168. require delegatee != 0x0;
  169. uint256 delegator_bal = balanceOf(e, delegator);
  170. uint256 votes_= getVotes(delegatee);
  171. _delegate(e, delegator, delegatee);
  172. require lastIndex(delegatee) < 1000000;
  173. uint256 _votes = getVotes(delegatee);
  174. assert _votes == votes_ + delegator_bal, "delegatee did not receive votes";
  175. }
  176. // passes + rule sanity
  177. rule previous_delegatee_votes_removed() {
  178. env e;
  179. address delegator; address delegatee; address third;
  180. require third != delegatee;
  181. require delegates(delegator) == third;
  182. require numCheckpoints(third) < 1000000;
  183. uint256 delegator_bal = balanceOf(e, delegator);
  184. uint256 votes_ = getVotes(third);
  185. _delegate(e, delegator, delegatee);
  186. uint256 _votes = getVotes(third);
  187. assert third != 0x0 => _votes == votes_ - delegator_bal, "votes not removed from the previous delegatee";
  188. }
  189. // passes with rule sanity
  190. rule delegate_contained() {
  191. env e;
  192. address delegator; address delegatee; address other;
  193. require other != delegatee;
  194. require other != delegates(delegator);
  195. uint256 votes_ = getVotes(other);
  196. _delegate(e, delegator, delegatee);
  197. uint256 _votes = getVotes(other);
  198. assert votes_ == _votes, "votes not contained";
  199. }
  200. rule delegate_no_frontrunning(method f) {
  201. env e; calldataarg args;
  202. address delegator; address delegatee; address third; address other;
  203. require numCheckpoints(delegatee) < 1000000;
  204. require numCheckpoints(third) < 1000000;
  205. f(e, args);
  206. uint256 delegator_bal = balanceOf(e, delegator);
  207. uint256 delegatee_votes_ = getVotes(delegatee);
  208. uint256 third_votes_ = getVotes(third);
  209. uint256 other_votes_ = getVotes(other);
  210. require delegates(delegator) == third;
  211. require third != delegatee;
  212. require other != third;
  213. require other != delegatee;
  214. require delegatee != 0x0;
  215. _delegate(e, delegator, delegatee);
  216. uint256 _delegatee_votes = getVotes(delegatee);
  217. uint256 _third_votes = getVotes(third);
  218. uint256 _other_votes = getVotes(other);
  219. // previous delegatee loses all of their votes
  220. // delegatee gains that many votes
  221. // third loses any votes delegated to them
  222. assert _delegatee_votes == delegatee_votes_ + delegator_bal, "delegatee did not receive votes";
  223. assert third != 0 => _third_votes == third_votes_ - delegator_bal, "votes not removed from third";
  224. assert other_votes_ == _other_votes, "delegate not contained";
  225. }
  226. // passes
  227. rule mint_increases_totalSupply() {
  228. env e;
  229. uint256 amount; address account;
  230. uint256 fromBlock = e.block.number;
  231. uint256 totalSupply_ = totalSupply();
  232. mint(e, account, amount);
  233. uint256 _totalSupply = totalSupply();
  234. require _totalSupply < _maxSupply();
  235. assert _totalSupply == totalSupply_ + amount, "totalSupply not increased properly";
  236. assert getPastTotalSupply(e, fromBlock) == totalSupply_ , "previous total supply not saved properly";
  237. }
  238. // passes
  239. rule burn_decreases_totalSupply() {
  240. env e;
  241. uint256 amount; address account;
  242. uint256 fromBlock = e.block.number;
  243. uint256 totalSupply_ = totalSupply();
  244. burn(e, account, amount);
  245. uint256 _totalSupply = totalSupply();
  246. assert _totalSupply == totalSupply_ - amount, "totalSupply not decreased properly";
  247. assert getPastTotalSupply(e, fromBlock) == totalSupply_ , "previous total supply not saved properly";
  248. }
  249. // passes
  250. rule mint_doesnt_increase_totalVotes() {
  251. env e;
  252. uint256 amount; address account;
  253. mathint totalVotes_ = totalVotes();
  254. mint(e, account, amount);
  255. assert totalVotes() == totalVotes_, "totalVotes increased";
  256. }
  257. // passes
  258. rule burn_doesnt_decrease_totalVotes() {
  259. env e;
  260. uint256 amount; address account;
  261. mathint totalVotes_ = totalVotes();
  262. burn(e, account, amount);
  263. assert totalVotes() == totalVotes_, "totalVotes decreased";
  264. }
  265. // // fails
  266. // rule mint_increases_totalVotes() {
  267. // env e;
  268. // uint256 amount; address account;
  269. // mathint totalVotes_ = totalVotes();
  270. // mint(e, account, amount);
  271. // assert totalVotes() == totalVotes_ + to_mathint(amount), "totalVotes not increased";
  272. // }
  273. // // fails
  274. // rule burn_decreases_totalVotes() {
  275. // env e;
  276. // uint256 amount; address account;
  277. // mathint totalVotes_ = totalVotes();
  278. // burn(e, account, amount);
  279. // assert totalVotes() == totalVotes_ - to_mathint(amount), "totalVotes not decreased";
  280. // }