ERC1155Burnable.spec 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. methods {
  2. balanceOf(address, uint256) returns uint256 envfree
  3. isApprovedForAll(address,address) returns bool envfree
  4. }
  5. /// If a method call reduces account balances, the caller must be either the
  6. /// holder of the account or approved to act on the holder's behalf.
  7. rule onlyHolderOrApprovedCanReduceBalance {
  8. address holder; uint256 token; uint256 amount;
  9. uint256 balanceBefore = balanceOf(holder, token);
  10. method f; env e; calldataarg args;
  11. f(e, args);
  12. uint256 balanceAfter = balanceOf(holder, token);
  13. assert balanceAfter < balanceBefore => e.msg.sender == holder || isApprovedForAll(holder, e.msg.sender),
  14. "An account balance may only be reduced by the holder or a holder-approved agent";
  15. }
  16. /// Burning a larger amount of a token must reduce that token's balance more
  17. /// than burning a smaller amount.
  18. rule burnAmountProportionalToBalanceReduction {
  19. storage beforeBurn = lastStorage;
  20. env e;
  21. address holder; uint256 token;
  22. mathint startingBalance = balanceOf(holder, token);
  23. uint256 smallBurn; uint256 largeBurn;
  24. require smallBurn < largeBurn;
  25. // smaller burn amount
  26. burn(e, holder, token, smallBurn) at beforeBurn;
  27. mathint smallBurnBalanceChange = startingBalance - balanceOf(holder, token);
  28. // larger burn amount
  29. burn(e, holder, token, largeBurn) at beforeBurn;
  30. mathint largeBurnBalanceChange = startingBalance - balanceOf(holder, token);
  31. assert smallBurnBalanceChange < largeBurnBalanceChange,
  32. "A larger burn must lead to a larger decrease in balance";
  33. }
  34. /// Unimplemented rule to verify monotonicity of burnBatch.
  35. rule burnBatchAmountProportionalToBalanceReduction { // TODO implement rule or remove
  36. assert true,
  37. "just a placeholder that should never show up";
  38. }
  39. /// Two sequential burns must be equivalent to a single burn of the sum of their
  40. /// amounts.
  41. rule sequentialBurnsEquivalentToSingleBurnOfSum {
  42. storage beforeBurns = lastStorage;
  43. env e;
  44. address holder; uint256 token;
  45. mathint startingBalance = balanceOf(holder, token);
  46. uint256 firstBurn; uint256 secondBurn; uint256 sumBurn;
  47. require sumBurn == firstBurn + secondBurn;
  48. // sequential burns
  49. burn(e, holder, token, firstBurn) at beforeBurns;
  50. burn(e, holder, token, secondBurn);
  51. mathint sequentialBurnsBalanceChange = startingBalance - balanceOf(holder, token);
  52. // burn of sum of sequential burns
  53. burn(e, holder, token, sumBurn) at beforeBurns;
  54. mathint sumBurnBalanceChange = startingBalance - balanceOf(holder, token);
  55. assert sequentialBurnsBalanceChange == sumBurnBalanceChange,
  56. "Sequential burns must be equivalent to a burn of their sum";
  57. }
  58. /// Unimplemented rule to verify additivty of burnBatch.
  59. rule sequentialBatchBurnsEquivalentToSingleBurnBatchOfSum { // TODO implement rule or remove
  60. assert true,
  61. "just a placeholder that should never show up";
  62. }
  63. /// The result of burning a single token must be equivalent whether done via
  64. /// burn or burnBatch.
  65. rule singleTokenBurnBurnBatchEquivalence {
  66. storage beforeBurn = lastStorage;
  67. env e;
  68. address holder;
  69. uint256 token; uint256 burnAmount;
  70. uint256[] tokens; uint256[] burnAmounts;
  71. mathint startingBalance = balanceOf(holder, token);
  72. require tokens.length == 1; require burnAmounts.length == 1;
  73. require tokens[0] == token; require burnAmounts[0] == burnAmount;
  74. // burning via burn
  75. burn(e, holder, token, burnAmount) at beforeBurn;
  76. mathint burnBalanceChange = startingBalance - balanceOf(holder, token);
  77. // burning via burnBatch
  78. burnBatch(e, holder, tokens, burnAmounts) at beforeBurn;
  79. mathint burnBatchBalanceChange = startingBalance - balanceOf(holder, token);
  80. assert burnBalanceChange == burnBatchBalanceChange,
  81. "Burning a single token via burn or burnBatch must be equivalent";
  82. }
  83. /// The results of burning multiple tokens must be equivalent whether done
  84. /// separately via burn or together via burnBatch.
  85. rule multipleTokenBurnBurnBatchEquivalence {
  86. storage beforeBurns = lastStorage;
  87. env e;
  88. address holder;
  89. uint256 tokenA; uint256 tokenB; uint256 tokenC;
  90. uint256 burnAmountA; uint256 burnAmountB; uint256 burnAmountC;
  91. uint256[] tokens; uint256[] burnAmounts;
  92. require tokenA != tokenB; require tokenB != tokenC; require tokenC != tokenA;
  93. mathint startingBalanceA = balanceOf(holder, tokenA);
  94. mathint startingBalanceB = balanceOf(holder, tokenB);
  95. mathint startingBalanceC = balanceOf(holder, tokenC);
  96. require tokens.length == 3; require burnAmounts.length == 3;
  97. require tokens[0] == tokenA; require burnAmounts[0] == burnAmountA;
  98. require tokens[1] == tokenB; require burnAmounts[1] == burnAmountB;
  99. require tokens[2] == tokenC; require burnAmounts[2] == burnAmountC;
  100. // burning via burn
  101. burn(e, holder, tokenA, burnAmountA) at beforeBurns;
  102. mathint burnBalanceChangeA = startingBalanceA - balanceOf(holder, tokenA);
  103. burn(e, holder, tokenB, burnAmountB) at beforeBurns;
  104. mathint burnBalanceChangeB = startingBalanceB - balanceOf(holder, tokenB);
  105. burn(e, holder, tokenC, burnAmountC) at beforeBurns;
  106. mathint burnBalanceChangeC = startingBalanceC - balanceOf(holder, tokenC);
  107. // burning via burnBatch
  108. burnBatch(e, holder, tokens, burnAmounts) at beforeBurns;
  109. mathint burnBatchBalanceChangeA = startingBalanceA - balanceOf(holder, tokenA);
  110. mathint burnBatchBalanceChangeB = startingBalanceB - balanceOf(holder, tokenB);
  111. mathint burnBatchBalanceChangeC = startingBalanceC - balanceOf(holder, tokenC);
  112. assert burnBalanceChangeA == burnBatchBalanceChangeA
  113. && burnBalanceChangeB == burnBatchBalanceChangeB
  114. && burnBalanceChangeC == burnBatchBalanceChangeC,
  115. "Burning multiple tokens via burn or burnBatch must be equivalent";
  116. }
  117. /// This rule should always fail.
  118. rule sanity {
  119. method f; env e; calldataarg args;
  120. f(e, args);
  121. assert false,
  122. "This rule should always fail";
  123. }