ERC1155Supply.spec 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. methods {
  2. totalSupply(uint256) returns uint256 envfree
  3. balanceOf(address, uint256) returns uint256 envfree
  4. exists_wrapper(uint256) returns bool envfree
  5. owner() returns address envfree
  6. }
  7. /// given two different token ids, if totalSupply for one changes, then
  8. /// totalSupply for other must not
  9. rule token_totalSupply_independence(method f)
  10. filtered {
  11. f -> f.selector != safeBatchTransferFrom(address,address,uint256[],uint256[],bytes).selector
  12. }
  13. {
  14. uint256 token1; uint256 token2;
  15. require token1 != token2;
  16. uint256 token1_before = totalSupply(token1);
  17. uint256 token2_before = totalSupply(token2);
  18. env e; calldataarg args;
  19. require e.msg.sender != owner(); // owner can call mintBatch and burnBatch in our harness
  20. f(e, args);
  21. uint256 token1_after = totalSupply(token1);
  22. uint256 token2_after = totalSupply(token2);
  23. assert token1_after != token1_before => token2_after == token2_before,
  24. "methods must not change the total supply of more than one token";
  25. }
  26. /******************************************************************************/
  27. ghost mapping(uint256 => mathint) sumOfBalances {
  28. init_state axiom forall uint256 token . sumOfBalances[token] == 0;
  29. }
  30. hook Sstore _balances[KEY uint256 token][KEY address user] uint256 newValue (uint256 oldValue) STORAGE {
  31. sumOfBalances[token] = sumOfBalances[token] + newValue - oldValue;
  32. }
  33. /// The sum of the balances over all users must equal the total supply for a
  34. /// given token.
  35. invariant total_supply_is_sum_of_balances(uint256 token)
  36. sumOfBalances[token] == totalSupply(token)
  37. {
  38. preserved {
  39. requireInvariant balanceOfZeroAddressIsZero(token);
  40. }
  41. }
  42. /******************************************************************************/
  43. /// The balance of a token for the zero address must be zero.
  44. invariant balanceOfZeroAddressIsZero(uint256 token)
  45. balanceOf(0, token) == 0
  46. /// If a user has a token, then the token should exist.
  47. rule held_tokens_should_exist {
  48. address user; uint256 token;
  49. requireInvariant balanceOfZeroAddressIsZero(token);
  50. // This assumption is safe because of total_supply_is_sum_of_balances
  51. require balanceOf(user, token) <= totalSupply(token);
  52. // note: `exists_wrapper` just calls `exists`
  53. assert balanceOf(user, token) > 0 => exists_wrapper(token),
  54. "if a user's balance for a token is positive, the token must exist";
  55. }
  56. /******************************************************************************/
  57. /*
  58. rule sanity {
  59. method f; env e; calldataarg args;
  60. f(e, args);
  61. assert false;
  62. }
  63. */