Skip to content

Fix #262: reject non-positive pricePerToken when parsing a babel fee box - #263

Open
Kshot3000 wants to merge 1 commit into
ergoplatform:developfrom
Kshot3000:fix/babel-fee-nonpositive-price
Open

Kshot3000 wants to merge 1 commit into
ergoplatform:developfrom
Kshot3000:fix/babel-fee-nonpositive-price

Conversation

@Kshot3000

Copy link
Copy Markdown
Contributor

Fixes #262

BabelFeeBoxStateBuilder already enforces pricePerToken > 0, but the BabelFeeBoxState(TransactionBox) constructor — used for boxes loaded from the chain, whose registers anyone could have set — read the price from R5 unchecked. A box with price 0 then crashed getMaxTokenAmountToBuy() / calcTokensToSellForErgAmount() with ArithmeticException: / by zero (reachable via BabelFeeOperations.addBabelFeeBoxes), and a negative price made the calculations return nonsense amounts.

This PR enforces the builder's invariant on the parse path:

  • BabelFeeBoxState(TransactionBox) throws IllegalStateException for pricePerToken <= 0 (same exception style as the token-id mismatch check in the same constructor).
  • The internal constructor rejects it too, with IllegalArgumentException, so no instance can carry a non-positive price. This is inert for existing callers: the builder validates first, and buildSucceedingState copies an already-valid price.

Boxes with a regular price parse and calculate exactly as before.

Test: new BabelFeeSpec property "babel fee box with non-positive price is rejected" — it builds babel fee boxes with price 0 and −1 directly (bypassing the validating builder, the way an externally created box appears on chain) and asserts both are rejected, and that a regular price still parses and calculates. On develop the new property fails with "Expected exception IllegalStateException to be thrown, but no exception was thrown"; with the fix, BabelFeeSpec is 4/4 and the full sbt test run is green (appkit module: 96 tests, 0 failures, per the test reports; common: 18/18).


Submitted by @Kshot9000 (https://x.com/kshot9000) · Tips: ERG 9fcM5RWnAjmP4vx5bnW6yohB6H9bLq8sJbaPLHtwZLtQPB32Pvy

… fee box

BabelFeeBoxStateBuilder.build() validates pricePerToken > 0, but the
BabelFeeBoxState(TransactionBox) constructor - the path used for boxes
loaded from the chain - read the price from register R5 with no check.

A babel fee box can be created outside appkit with any register values,
so a box with price 0 or a negative price parses into a state whose
getMaxTokenAmountToBuy() and calcTokensToSellForErgAmount() then divide
by that price: ArithmeticException (/ by zero) for 0, and nonsense
token amounts for a negative price, surfacing far from the cause in
BabelFeeOperations.addBabelFeeBoxes.

Reject pricePerToken <= 0 at parse time with IllegalStateException
(matching the token-id mismatch check in the same constructor), and
enforce the same invariant in the internal constructor so no instance
can carry a non-positive price. Boxes with a regular price parse and
calculate exactly as before.

New BabelFeeSpec property builds babel boxes with price 0 and -1
directly (bypassing the builder) and asserts both are rejected, and
that a regular price still parses and calculates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BabelFeeBoxState parsed from a box accepts pricePerToken <= 0, then divides by it

1 participant