ERC1155Burnable.spec 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /// Using only burnBatch, possible approach:
  36. /// Token with smaller and larger burn amounts
  37. /// Round one smaller burn
  38. /// Round larger burn
  39. rule burnBatchAmountProportionalToBalanceReduction { // TODO implement rule or remove
  40. storage beforeBurn = lastStorage;
  41. env e;
  42. address holder; uint256 token;
  43. mathint startingBalance = balanceOf(holder, token);
  44. uint256 smallBurn; uint256 largeBurn;
  45. require smallBurn < largeBurn;
  46. uint256[] tokens; uint256[] smallBurnAmounts; uint256[] largeBurnAmounts;
  47. require tokens.length == 1; require smallBurnAmounts.length == 1; require largeBurnAmounts.length == 1;
  48. require tokens[0] == token; require smallBurnAmounts[0] == smallBurn; require largeBurnAmounts[0] == largeBurn;
  49. // smaller burn amount
  50. burnBatch(e, holder, tokens, smallBurnAmounts) at beforeBurn;
  51. mathint smallBurnBalanceChange =
  52. assert true,
  53. "just a placeholder that should never show up";
  54. }
  55. /// Two sequential burns must be equivalent to a single burn of the sum of their
  56. /// amounts.
  57. rule sequentialBurnsEquivalentToSingleBurnOfSum {
  58. storage beforeBurns = lastStorage;
  59. env e;
  60. address holder; uint256 token;
  61. mathint startingBalance = balanceOf(holder, token);
  62. uint256 firstBurn; uint256 secondBurn; uint256 sumBurn;
  63. require sumBurn == firstBurn + secondBurn;
  64. // sequential burns
  65. burn(e, holder, token, firstBurn) at beforeBurns;
  66. burn(e, holder, token, secondBurn);
  67. mathint sequentialBurnsBalanceChange = startingBalance - balanceOf(holder, token);
  68. // burn of sum of sequential burns
  69. burn(e, holder, token, sumBurn) at beforeBurns;
  70. mathint sumBurnBalanceChange = startingBalance - balanceOf(holder, token);
  71. assert sequentialBurnsBalanceChange == sumBurnBalanceChange,
  72. "Sequential burns must be equivalent to a burn of their sum";
  73. }
  74. /// Unimplemented rule to verify additivty of burnBatch.
  75. /// Using only burnBatch, possible approach:
  76. /// Token with first and second burn amounts
  77. /// Round one two sequential burns in separate transactions
  78. /// Round two two sequential burns in the same transaction
  79. /// Round three one burn of sum
  80. rule sequentialBatchBurnsEquivalentToSingleBurnBatchOfSum { // TODO implement rule or remove
  81. assert true,
  82. "just a placeholder that should never show up";
  83. }
  84. /// The result of burning a single token must be equivalent whether done via
  85. /// burn or burnBatch.
  86. rule singleTokenBurnBurnBatchEquivalence {
  87. storage beforeBurn = lastStorage;
  88. env e;
  89. address holder;
  90. uint256 token; uint256 burnAmount;
  91. uint256[] tokens; uint256[] burnAmounts;
  92. mathint startingBalance = balanceOf(holder, token);
  93. require tokens.length == 1; require burnAmounts.length == 1;
  94. require tokens[0] == token; require burnAmounts[0] == burnAmount;
  95. // burning via burn
  96. burn(e, holder, token, burnAmount) at beforeBurn;
  97. mathint burnBalanceChange = startingBalance - balanceOf(holder, token);
  98. // burning via burnBatch
  99. burnBatch(e, holder, tokens, burnAmounts) at beforeBurn;
  100. mathint burnBatchBalanceChange = startingBalance - balanceOf(holder, token);
  101. assert burnBalanceChange == burnBatchBalanceChange,
  102. "Burning a single token via burn or burnBatch must be equivalent";
  103. }
  104. /// The results of burning multiple tokens must be equivalent whether done
  105. /// separately via burn or together via burnBatch.
  106. rule multipleTokenBurnBurnBatchEquivalence {
  107. storage beforeBurns = lastStorage;
  108. env e;
  109. address holder;
  110. uint256 tokenA; uint256 tokenB; uint256 tokenC;
  111. uint256 burnAmountA; uint256 burnAmountB; uint256 burnAmountC;
  112. uint256[] tokens; uint256[] burnAmounts;
  113. require tokenA != tokenB; require tokenB != tokenC; require tokenC != tokenA;
  114. mathint startingBalanceA = balanceOf(holder, tokenA);
  115. mathint startingBalanceB = balanceOf(holder, tokenB);
  116. mathint startingBalanceC = balanceOf(holder, tokenC);
  117. require tokens.length == 3; require burnAmounts.length == 3;
  118. require tokens[0] == tokenA; require burnAmounts[0] == burnAmountA;
  119. require tokens[1] == tokenB; require burnAmounts[1] == burnAmountB;
  120. require tokens[2] == tokenC; require burnAmounts[2] == burnAmountC;
  121. // burning via burn
  122. burn(e, holder, tokenA, burnAmountA) at beforeBurns;
  123. mathint burnBalanceChangeA = startingBalanceA - balanceOf(holder, tokenA);
  124. burn(e, holder, tokenB, burnAmountB) at beforeBurns;
  125. mathint burnBalanceChangeB = startingBalanceB - balanceOf(holder, tokenB);
  126. burn(e, holder, tokenC, burnAmountC) at beforeBurns;
  127. mathint burnBalanceChangeC = startingBalanceC - balanceOf(holder, tokenC);
  128. // burning via burnBatch
  129. burnBatch(e, holder, tokens, burnAmounts) at beforeBurns;
  130. mathint burnBatchBalanceChangeA = startingBalanceA - balanceOf(holder, tokenA);
  131. mathint burnBatchBalanceChangeB = startingBalanceB - balanceOf(holder, tokenB);
  132. mathint burnBatchBalanceChangeC = startingBalanceC - balanceOf(holder, tokenC);
  133. assert burnBalanceChangeA == burnBatchBalanceChangeA
  134. && burnBalanceChangeB == burnBatchBalanceChangeB
  135. && burnBalanceChangeC == burnBatchBalanceChangeC,
  136. "Burning multiple tokens via burn or burnBatch must be equivalent";
  137. }
  138. /// This rule should always fail.
  139. rule sanity {
  140. method f; env e; calldataarg args;
  141. f(e, args);
  142. assert false,
  143. "This rule should always fail";
  144. }