ERC20Votes.spec 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. rule sanity(method f) {
  54. env e;
  55. calldataarg arg;
  56. f(e, arg);
  57. assert false;
  58. }
  59. // something stupid just to see
  60. invariant sanity_invariant()
  61. totalSupply() >= 0
  62. // sum of user balances is >= total amount of delegated votes
  63. // fails on burn. This is because burn does not remove votes from the users
  64. invariant votes_solvency()
  65. to_mathint(totalSupply()) >= totalVotes()
  66. { preserved with(env e) {
  67. require forall address account. numCheckpoints(account) < 1000000;
  68. // requireInvariant totalVotes_sums_accounts();
  69. } }
  70. // invariant totalVotes_sums_accounts()
  71. // forall address a. forall address b. (a != b && a != 0x0 && b != 0x0) => totalVotes() >= getVotes(delegates(a)) + getVotes(delegates(b))
  72. // invariant totalVotes_sums_accounts()
  73. // forall address a. forall address b. (a != b) => totalVotes() >= userVotes(a) + userVotes(b)
  74. // { preserved {
  75. // require forall address account. numCheckpoints(account) < 1000000;
  76. // }}
  77. // for some checkpoint, the fromBlock is less than the current block number
  78. invariant blockNum_constrains_fromBlock(address account, uint32 index, env e)
  79. ckptFromBlock(account, index) < e.block.number
  80. {
  81. preserved {
  82. require index < numCheckpoints(account);
  83. }
  84. }
  85. // TODO add a note about this in the report
  86. // // numCheckpoints are less than maxInt
  87. // // passes because numCheckpoints does a safeCast
  88. // invariant maxInt_constrains_numBlocks(address account)
  89. // numCheckpoints(account) < 4294967295 // 2^32
  90. // // fails because there are no checks to stop it
  91. // invariant maxInt_constrains_ckptsLength(address account)
  92. // unsafeNumCheckpoints(account) < 4294967295 // 2^32
  93. // can't have more checkpoints for a given account than the last from block
  94. // passes
  95. invariant fromBlock_constrains_numBlocks(address account)
  96. numCheckpoints(account) <= ckptFromBlock(account, numCheckpoints(account) - 1)
  97. { preserved with(env e) {
  98. require e.block.number >= ckptFromBlock(account, numCheckpoints(account) - 1); // this should be true from the invariant above!!
  99. }}
  100. // for any given checkpoint, the fromBlock must be greater than the checkpoint
  101. // this proves the above invariant in combination with the below invariant
  102. // if checkpoint has a greater fromBlock than the last, and the FromBlock is always greater than the pos.
  103. // Then the number of positions must be less than the currentFromBlock
  104. // ^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
  105. // passes + rule sanity
  106. invariant fromBlock_greaterThanEq_pos(address account, uint32 pos)
  107. ckptFromBlock(account, pos) >= pos
  108. // a larger index must have a larger fromBlock
  109. // passes + rule sanity
  110. invariant fromBlock_increasing(address account, uint32 pos, uint32 pos2)
  111. pos > pos2 => ckptFromBlock(account, pos) > ckptFromBlock(account, pos2)
  112. // converted from an invariant to a rule to slightly change the logic
  113. // if the fromBlock is the same as before, then the number of checkpoints stays the same
  114. // however if the fromBlock is new than the number of checkpoints increases
  115. // passes, fails rule sanity because tautology check seems to be bugged
  116. rule unique_checkpoints_rule(method f) {
  117. env e; calldataarg args;
  118. address account;
  119. uint32 num_ckpts_ = numCheckpoints(account);
  120. uint32 fromBlock_ = num_ckpts_ == 0 ? 0 : ckptFromBlock(account, num_ckpts_ - 1);
  121. f(e, args);
  122. uint32 _num_ckpts = numCheckpoints(account);
  123. uint32 _fromBlock = _num_ckpts == 0 ? 0 : ckptFromBlock(account, _num_ckpts - 1);
  124. assert fromBlock_ == _fromBlock => num_ckpts_ == _num_ckpts || _num_ckpts == 1, "same fromBlock, new checkpoint";
  125. }
  126. // assumes neither account has delegated
  127. // currently fails due to this scenario. A has maxint number of checkpoints
  128. // an additional checkpoint is added which overflows and sets A's votes to 0
  129. // passes + rule sanity (- a bad tautology check)
  130. rule transfer_safe() {
  131. env e;
  132. uint256 amount;
  133. address a; address b;
  134. require delegates(a) != delegates(b); // confirmed if they both delegate to the same person then transfer keeps the votes the same
  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. 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. // }