ERC20Votes.spec 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. // blocked by tool error
  35. invariant totalVotes_gte_accounts()
  36. forall address a. forall address b. a != b => totalVotes() >= getVotes(a) + getVotes(b)
  37. hook Sstore _checkpoints[KEY address account][INDEX uint32 index].votes uint224 newVotes (uint224 oldVotes) STORAGE {
  38. havoc userVotes assuming
  39. userVotes@new(account) == newVotes;
  40. havoc totalVotes assuming
  41. totalVotes@new() == totalVotes@old() + to_mathint(newVotes) - to_mathint(userVotes(account));
  42. havoc lastIndex assuming
  43. lastIndex@new(account) == index;
  44. }
  45. ghost lastFromBlock(address) returns uint32;
  46. ghost doubleFromBlock(address) returns bool {
  47. init_state axiom forall address a. doubleFromBlock(a) == false;
  48. }
  49. hook Sstore _checkpoints[KEY address account][INDEX uint32 index].fromBlock uint32 newBlock (uint32 oldBlock) STORAGE {
  50. havoc lastFromBlock assuming
  51. lastFromBlock@new(account) == newBlock;
  52. havoc doubleFromBlock assuming
  53. doubleFromBlock@new(account) == (newBlock == lastFromBlock(account));
  54. }
  55. rule sanity(method f) {
  56. env e;
  57. calldataarg arg;
  58. f(e, arg);
  59. assert false;
  60. }
  61. // something stupid just to see
  62. invariant sanity_invariant()
  63. totalSupply() >= 0
  64. // sum of user balances is >= total amount of delegated votes
  65. // blocked by tool error
  66. invariant votes_solvency()
  67. to_mathint(totalSupply()) >= totalVotes()
  68. { preserved {
  69. require forall address account. unsafeNumCheckpoints(account) < 4294967295;
  70. requireInvariant totalVotes_gte_accounts();
  71. }}
  72. // for some checkpoint, the fromBlock is less than the current block number
  73. // passes but fails rule sanity from hash on delegate by sig
  74. invariant timestamp_constrains_fromBlock(address account, uint32 index, env e)
  75. ckptFromBlock(account, index) < e.block.number
  76. {
  77. preserved {
  78. require index < numCheckpoints(account);
  79. }
  80. }
  81. // TODO add a note about this in the report
  82. // // numCheckpoints are less than maxInt
  83. // // passes because numCheckpoints does a safeCast
  84. // invariant maxInt_constrains_numBlocks(address account)
  85. // numCheckpoints(account) < 4294967295 // 2^32
  86. // // fails because there are no checks to stop it
  87. // invariant maxInt_constrains_ckptsLength(address account)
  88. // unsafeNumCheckpoints(account) < 4294967295 // 2^32
  89. // can't have more checkpoints for a given account than the last from block
  90. // passes
  91. invariant fromBlock_constrains_numBlocks(address account)
  92. numCheckpoints(account) <= ckptFromBlock(account, numCheckpoints(account) - 1)
  93. { preserved with(env e) {
  94. uint32 pos;
  95. uint32 pos2;
  96. requireInvariant fromBlock_greaterThanEq_pos(account, pos);
  97. requireInvariant fromBlock_increasing(account, pos, pos2);
  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. invariant no_delegate_no_checkpoints(address account)
  113. delegates(account) == 0x0 => numCheckpoints(account) == 0
  114. { preserved delegate(address delegatee) with(env e) {
  115. require delegatee != 0;
  116. } preserved _delegate(address delegator, address delegatee) with(env e) {
  117. require delegatee != 0;
  118. }}
  119. // converted from an invariant to a rule to slightly change the logic
  120. // if the fromBlock is the same as before, then the number of checkpoints stays the same
  121. // however if the fromBlock is new than the number of checkpoints increases
  122. // passes, fails rule sanity because tautology check seems to be bugged
  123. rule unique_checkpoints_rule(method f) {
  124. env e; calldataarg args;
  125. // require e.block.number > 0; // we don't care about this exception
  126. address account;
  127. // address delegates_pre = delegates(account);
  128. // require unsafeNumCheckpoints(account) < 1000000; // 2^32 // we don't want to deal with the checkpoint overflow error here
  129. uint32 num_ckpts_ = numCheckpoints(account);
  130. uint32 fromBlock_ = num_ckpts_ == 0 ? 0 : ckptFromBlock(account, num_ckpts_ - 1);
  131. f(e, args);
  132. uint32 _num_ckpts = numCheckpoints(account);
  133. uint32 _fromBlock = _num_ckpts == 0 ? 0 : ckptFromBlock(account, _num_ckpts - 1);
  134. assert fromBlock_ == _fromBlock => num_ckpts_ == _num_ckpts || _num_ckpts == 1, "same fromBlock, new checkpoint";
  135. // assert doubleFromBlock(account) => num_ckpts_ == _num_ckpts, "same fromBlock, new checkpoint";
  136. // this assert fails consistently
  137. // assert !doubleFromBlock(account) => ckpts_ != _ckpts, "new fromBlock but total checkpoints not being increased";
  138. }
  139. // assumes neither account has delegated
  140. // currently fails due to this scenario. A has maxint number of checkpoints
  141. // an additional checkpoint is added which overflows and sets A's votes to 0
  142. // passes + rule sanity (- a bad tautology check)
  143. rule transfer_safe() {
  144. env e;
  145. uint256 amount;
  146. address a; address b;
  147. // require a != b;
  148. require delegates(a) != delegates(b); // confirmed if they both delegate to the same person then transfer keeps the votes the same
  149. // requireInvariant fromBlock_constrains_numBlocks(a);
  150. // requireInvariant fromBlock_constrains_numBlocks(b);
  151. // requireInvariant totalVotes_gte_accounts(a, b);
  152. // require lastIndex(delegates(a)) < 1000000;
  153. // require lastIndex(delegates(b)) < 1000000;
  154. require numCheckpoints(delegates(a)) < 1000000;
  155. require numCheckpoints(delegates(b)) < 1000000;
  156. uint256 votesA_pre = getVotes(delegates(a));
  157. uint256 votesB_pre = getVotes(delegates(b));
  158. // for debugging
  159. uint256 balA_ = balanceOf(e, a);
  160. uint256 balB_ = balanceOf(e, b);
  161. mathint totalVotes_pre = totalVotes();
  162. erc20votes.transferFrom(e, a, b, amount);
  163. // require lastIndex(delegates(a)) < 1000000;
  164. // require lastIndex(delegates(b)) < 1000000;
  165. mathint totalVotes_post = totalVotes();
  166. uint256 votesA_post = getVotes(delegates(a));
  167. uint256 votesB_post = getVotes(delegates(b));
  168. // for debugging
  169. uint256 _balA = balanceOf(e, a);
  170. uint256 _balB = balanceOf(e, b);
  171. // if an account that has not delegated transfers balance to an account that has, it will increase the total supply of votes
  172. assert totalVotes_pre == totalVotes_post, "transfer changed total supply";
  173. assert delegates(a) != 0 => votesA_pre - votesA_post == amount, "A lost the wrong amount of votes";
  174. assert delegates(b) != 0 => votesB_post - votesB_pre == amount, "B lost the wrong amount of votes";
  175. }
  176. // for any given function f, if the delegate is changed the function must be delegate or delegateBySig
  177. // passes
  178. rule delegates_safe(method f) filtered {f -> (f.selector != delegate(address).selector &&
  179. f.selector != _delegate(address, address).selector &&
  180. f.selector != delegateBySig(address, uint256, uint256, uint8, bytes32, bytes32).selector) }
  181. {
  182. env e; calldataarg args;
  183. address account;
  184. address pre = delegates(account);
  185. f(e, args);
  186. address post = delegates(account);
  187. assert pre == post, "invalid delegate change";
  188. }
  189. // delegates increases the delegatee's votes by the proper amount
  190. // passes + rule sanity
  191. rule delegatee_receives_votes() {
  192. env e;
  193. address delegator; address delegatee;
  194. require delegates(delegator) != delegatee;
  195. require delegatee != 0x0;
  196. uint256 delegator_bal = balanceOf(e, delegator);
  197. uint256 votes_= getVotes(delegatee);
  198. _delegate(e, delegator, delegatee);
  199. require lastIndex(delegatee) < 1000000;
  200. uint256 _votes = getVotes(delegatee);
  201. assert _votes == votes_ + delegator_bal, "delegatee did not receive votes";
  202. }
  203. // passes + rule sanity
  204. rule previous_delegatee_votes_removed() {
  205. env e;
  206. address delegator; address delegatee; address third;
  207. require third != delegatee;
  208. require delegates(delegator) == third;
  209. require numCheckpoints(third) < 1000000;
  210. uint256 delegator_bal = balanceOf(e, delegator);
  211. uint256 votes_ = getVotes(third);
  212. _delegate(e, delegator, delegatee);
  213. uint256 _votes = getVotes(third);
  214. assert third != 0x0 => _votes == votes_ - delegator_bal, "votes not removed from the previous delegatee";
  215. }
  216. // passes with rule sanity
  217. rule delegate_contained() {
  218. env e;
  219. address delegator; address delegatee; address other;
  220. require other != delegatee;
  221. require other != delegates(delegator);
  222. uint256 votes_ = getVotes(other);
  223. _delegate(e, delegator, delegatee);
  224. uint256 _votes = getVotes(other);
  225. assert votes_ == _votes, "votes not contained";
  226. }
  227. // checks all of the above delegate rules with front running
  228. rule delegate_no_frontrunning(method f) {
  229. env e; calldataarg args;
  230. address delegator; address delegatee; address third; address other;
  231. require delegates(delegator) == third;
  232. require third != delegatee;
  233. require other != third;
  234. require other != delegatee;
  235. require delegatee != 0x0;
  236. require numCheckpoints(delegatee) < 1000000;
  237. require numCheckpoints(third) < 1000000;
  238. uint256 delegatee_votes_ = getVotes(delegatee);
  239. uint256 third_votes_ = getVotes(third);
  240. uint256 other_votes_ = getVotes(other);
  241. // require third is address for previous delegator
  242. f(e, args);
  243. uint256 delegator_bal = erc20votes.balanceOf(e, delegator);
  244. _delegate(e, delegator, delegatee);
  245. uint256 _delegatee_votes = getVotes(delegatee);
  246. uint256 _third_votes = getVotes(third);
  247. uint256 _other_votes = getVotes(other);
  248. // previous delegatee loses all of their votes
  249. // delegatee gains that many votes
  250. // third loses any votes delegated to them
  251. assert _delegatee_votes == delegatee_votes_ + delegator_bal, "delegatee did not receive votes";
  252. assert third != 0 => _third_votes == third_votes_ - delegator_bal, "votes not removed from third";
  253. assert other_votes_ == _other_votes, "delegate not contained";
  254. }
  255. // passes
  256. rule mint_increases_totalSupply() {
  257. env e;
  258. uint256 amount; address account;
  259. uint256 fromBlock = e.block.number;
  260. uint256 totalSupply_ = totalSupply();
  261. mint(e, account, amount);
  262. uint256 _totalSupply = totalSupply();
  263. require _totalSupply < _maxSupply();
  264. assert _totalSupply == totalSupply_ + amount, "totalSupply not increased properly";
  265. assert getPastTotalSupply(e, fromBlock) == totalSupply_ , "previous total supply not saved properly";
  266. }
  267. // passes
  268. rule burn_decreases_totalSupply() {
  269. env e;
  270. uint256 amount; address account;
  271. uint256 fromBlock = e.block.number;
  272. uint256 totalSupply_ = totalSupply();
  273. require totalSupply_ > balanceOf(e, account);
  274. burn(e, account, amount);
  275. uint256 _totalSupply = totalSupply();
  276. require _totalSupply < _maxSupply();
  277. assert _totalSupply == totalSupply_ - amount, "totalSupply not decreased properly";
  278. assert getPastTotalSupply(e, fromBlock) == totalSupply_ , "previous total supply not saved properly";
  279. }
  280. // passes
  281. rule mint_doesnt_increase_totalVotes() {
  282. env e;
  283. uint256 amount; address account;
  284. mathint totalVotes_ = totalVotes();
  285. mint(e, account, amount);
  286. assert totalVotes() == totalVotes_, "totalVotes increased";
  287. }
  288. // passes
  289. rule burn_doesnt_decrease_totalVotes() {
  290. env e;
  291. uint256 amount; address account;
  292. mathint totalVotes_ = totalVotes();
  293. burn(e, account, amount);
  294. assert totalVotes() == totalVotes_, "totalVotes decreased";
  295. }
  296. // // fails
  297. // rule mint_increases_totalVotes() {
  298. // env e;
  299. // uint256 amount; address account;
  300. // mathint totalVotes_ = totalVotes();
  301. // mint(e, account, amount);
  302. // assert totalVotes() == totalVotes_ + to_mathint(amount), "totalVotes not increased";
  303. // }
  304. // // fails
  305. // rule burn_decreases_totalVotes() {
  306. // env e;
  307. // uint256 amount; address account;
  308. // mathint totalVotes_ = totalVotes();
  309. // burn(e, account, amount);
  310. // assert totalVotes() == totalVotes_ - to_mathint(amount), "totalVotes not decreased";
  311. // }