ERC1155Supply.spec 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. methods {
  2. totalSupply(uint256) returns uint256 envfree
  3. balanceOf(address, uint256) returns uint256 envfree
  4. exists_wrapper(uint256) returns bool envfree
  5. }
  6. /// given two different token ids, if totalSupply for one changes, then
  7. /// totalSupply for other should not
  8. rule token_totalSupply_independence(method f)
  9. filtered {
  10. f -> f.selector != _burnBatch(address,uint256[],uint256[]).selector
  11. && f.selector != _mintBatch(address,uint256[],uint256[],bytes).selector
  12. && f.selector != safeBatchTransferFrom(address,address,uint256[],uint256[],bytes).selector
  13. }
  14. {
  15. uint256 token1; uint256 token2;
  16. require token1 != token2;
  17. uint256 token1_before = totalSupply(token1);
  18. uint256 token2_before = totalSupply(token2);
  19. env e; calldataarg args;
  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. /// TODO possibly show equivalence between batch and non-batch methods
  27. /// in order to leverage non-batch rules wrt batch rules
  28. rule singleTokenSafeTransferFromSafeBatchTransferFromEquivalence {
  29. assert false,
  30. "TODO implement this rule using burn version as structural model";
  31. }
  32. rule multipleTokenSafeTransferFromSafeBatchTransferFromEquivalence {
  33. assert false,
  34. "TODO implement this rule using burn version as structural model";
  35. }
  36. /******************************************************************************/
  37. ghost mapping(uint256 => mathint) sumOfBalances {
  38. init_state axiom forall uint256 token . sumOfBalances[token] == 0;
  39. }
  40. hook Sstore _balances[KEY uint256 token][KEY address user] uint256 newValue (uint256 oldValue) STORAGE {
  41. sumOfBalances[token] = sumOfBalances[token] + newValue - oldValue;
  42. }
  43. // status: not passing, because mint and burn are the same as transferring to/from
  44. // the 0 address.
  45. invariant total_supply_is_sum_of_balances(uint256 token)
  46. sumOfBalances[token] == totalSupply(token)
  47. {
  48. preserved {
  49. requireInvariant balanceOfZeroAddressIsZero(token);
  50. }
  51. }
  52. /*
  53. rule total_supply_is_sum_of_balances_as_rule {
  54. uint256 token;
  55. require sumOfBalances[token] == totalSupply(token) + balanceOf(0, token);
  56. mathint sum_before = sumOfBalances[token];
  57. method f; calldataarg arg; env e;
  58. f(e, arg);
  59. mathint sum_after = sumOfBalances[token];
  60. assert sumOfBalances[token] == totalSupply(token) + balanceOf(0, token);
  61. }
  62. */
  63. /******************************************************************************/
  64. /// The balance of a token for the zero address must be zero.
  65. invariant balanceOfZeroAddressIsZero(uint256 token)
  66. balanceOf(0, token) == 0
  67. // if a user has a token, then the token should exist
  68. /*
  69. hook Sload _balances[...] {
  70. require balance <= totalSupply
  71. }
  72. */
  73. rule held_tokens_should_exist {
  74. address user; uint256 token;
  75. requireInvariant balanceOfZeroAddressIsZero(token);
  76. // This assumption is safe because of total_supply_is_sum_of_balances
  77. require balanceOf(user, token) <= totalSupply(token);
  78. assert balanceOf(user, token) > 0 => exists_wrapper(token),
  79. "if a user's balance for a token is positive, the token must exist";
  80. }
  81. /******************************************************************************/
  82. rule sanity {
  83. method f; env e; calldataarg args;
  84. f(e, args);
  85. assert false;
  86. }