ERC1155Supply.spec 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //// ## Verification of `ERC1155Supply`
  2. ////
  3. //// `ERC1155Supply` extends the `ERC1155` functionality. The contract creates a publicly callable `totalSupply` wrapper for the private `_totalSupply` method, a public `exists` method to check for a positive balance of a given token, and updates `_beforeTokenTransfer` to appropriately change the mapping `_totalSupply` in the context of minting and burning tokens.
  4. ////
  5. //// ### Assumptions and Simplifications
  6. //// - The `exists` method was wrapped in the `exists_wrapper` method because `exists` is a keyword in CVL.
  7. //// - The public functions `burn`, `burnBatch`, `mint`, and `mintBatch` were implemented in the harnessing contract make their respective internal functions callable by the CVL. This was used to test the increase and decrease of `totalSupply` when tokens are minted and burned.
  8. //// - We created the `onlyOwner` modifier to be used in the above functions so that they are not called in unrelated rules.
  9. ////
  10. //// ### Properties
  11. methods {
  12. totalSupply(uint256) returns uint256 envfree
  13. balanceOf(address, uint256) returns uint256 envfree
  14. exists_wrapper(uint256) returns bool envfree
  15. owner() returns address envfree
  16. }
  17. /// Given two different token ids, if total supply for one changes, then
  18. /// total supply for other must not.
  19. rule token_totalSupply_independence(method f)
  20. filtered {
  21. f -> f.selector != safeBatchTransferFrom(address,address,uint256[],uint256[],bytes).selector
  22. }
  23. {
  24. uint256 token1; uint256 token2;
  25. require token1 != token2;
  26. uint256 token1_before = totalSupply(token1);
  27. uint256 token2_before = totalSupply(token2);
  28. env e; calldataarg args;
  29. require e.msg.sender != owner(); // owner can call mintBatch and burnBatch in our harness
  30. f(e, args);
  31. uint256 token1_after = totalSupply(token1);
  32. uint256 token2_after = totalSupply(token2);
  33. assert token1_after != token1_before => token2_after == token2_before,
  34. "methods must not change the total supply of more than one token";
  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. /// The sum of the balances over all users must equal the total supply for a
  44. /// given token.
  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. /// The balance of a token for the zero address must be zero.
  54. invariant balanceOfZeroAddressIsZero(uint256 token)
  55. balanceOf(0, token) == 0
  56. /// If a user has a token, then the token should exist.
  57. rule held_tokens_should_exist {
  58. address user; uint256 token;
  59. requireInvariant balanceOfZeroAddressIsZero(token);
  60. // This assumption is safe because of total_supply_is_sum_of_balances
  61. require balanceOf(user, token) <= totalSupply(token);
  62. // note: `exists_wrapper` just calls `exists`
  63. assert balanceOf(user, token) > 0 => exists_wrapper(token),
  64. "if a user's balance for a token is positive, the token must exist";
  65. }
  66. /******************************************************************************/
  67. /*
  68. rule sanity {
  69. method f; env e; calldataarg args;
  70. f(e, args);
  71. assert false;
  72. }
  73. */