From 60341002fd2027070af5bbec0645e08301f70de8 Mon Sep 17 00:00:00 2001 From: woodser Date: Thu, 9 Mar 2023 12:08:30 -0500 Subject: [PATCH] prevent non-terminating BigDecimal division --- .../java/haveno/core/trade/ClosedTradableFormatter.java | 4 ++-- core/src/main/java/haveno/core/trade/HavenoUtils.java | 8 +++++--- core/src/main/java/haveno/core/util/coin/CoinUtil.java | 4 +--- .../desktop/main/offer/offerbook/OfferBookView.java | 3 +-- .../desktop/main/offer/offerbook/OfferBookViewModel.java | 3 +-- desktop/src/main/java/haveno/desktop/util/GUIUtil.java | 3 ++- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/haveno/core/trade/ClosedTradableFormatter.java b/core/src/main/java/haveno/core/trade/ClosedTradableFormatter.java index 64112ab910..c4989d4153 100644 --- a/core/src/main/java/haveno/core/trade/ClosedTradableFormatter.java +++ b/core/src/main/java/haveno/core/trade/ClosedTradableFormatter.java @@ -78,7 +78,7 @@ public class ClosedTradableFormatter { } public String getTotalTxFeeAsString(BigInteger totalTradeAmount, BigInteger totalTxFee) { - double percentage = new BigDecimal(totalTxFee).divide(new BigDecimal(totalTradeAmount)).doubleValue(); + double percentage = HavenoUtils.divide(totalTxFee, totalTradeAmount); return Res.get(I18N_KEY_TOTAL_TX_FEE, HavenoUtils.formatXmr(totalTxFee, true), formatToPercentWithSymbol(percentage)); @@ -98,7 +98,7 @@ public class ClosedTradableFormatter { } public String getTotalTradeFeeAsString(BigInteger totalTradeAmount, BigInteger totalTradeFee) { - double percentage = new BigDecimal(totalTradeFee).divide(new BigDecimal(totalTradeAmount)).doubleValue(); + double percentage = HavenoUtils.divide(totalTradeFee, totalTradeAmount); return Res.get(I18N_KEY_TOTAL_TRADE_FEE_BTC, HavenoUtils.formatXmr(totalTradeFee, true), formatToPercentWithSymbol(percentage)); diff --git a/core/src/main/java/haveno/core/trade/HavenoUtils.java b/core/src/main/java/haveno/core/trade/HavenoUtils.java index 3aeca238e5..dd66e5155f 100644 --- a/core/src/main/java/haveno/core/trade/HavenoUtils.java +++ b/core/src/main/java/haveno/core/trade/HavenoUtils.java @@ -129,6 +129,9 @@ public class HavenoUtils { return atomicUnitsToXmr(coinToAtomicUnits(coin)); } + public static double divide(BigInteger auDividend, BigInteger auDivisor) { + return (double) atomicUnitsToCentineros(auDividend) / (double) atomicUnitsToCentineros(auDivisor); + } // ------------------------- FORMAT UTILS --------------------------------- @@ -230,9 +233,8 @@ public class HavenoUtils { public static BigInteger getFeePerXmr(BigInteger feePerXmr, BigInteger amount) { BigDecimal feePerXmrAsDecimal = feePerXmr == null ? BigDecimal.valueOf(0) : new BigDecimal(feePerXmr); - BigDecimal amountAsDecimal = amount == null ? BigDecimal.valueOf(0) : new BigDecimal(amount); - BigDecimal xmrAsDecimal = new BigDecimal(HavenoUtils.xmrToAtomicUnits(1.0)); - return feePerXmrAsDecimal.multiply(amountAsDecimal.divide(xmrAsDecimal)).toBigInteger(); + BigDecimal amountMultiplier = BigDecimal.valueOf(divide(amount == null ? BigInteger.valueOf(0) : amount, HavenoUtils.xmrToAtomicUnits(1.0))); + return feePerXmrAsDecimal.multiply(amountMultiplier).toBigInteger(); } diff --git a/core/src/main/java/haveno/core/util/coin/CoinUtil.java b/core/src/main/java/haveno/core/util/coin/CoinUtil.java index 98f59dee30..fae061bc3c 100644 --- a/core/src/main/java/haveno/core/util/coin/CoinUtil.java +++ b/core/src/main/java/haveno/core/util/coin/CoinUtil.java @@ -64,9 +64,7 @@ public class CoinUtil { * @return The percentage value as double (e.g. 1% is 0.01) */ public static double getAsPercentPerBtc(BigInteger part, BigInteger total) { - BigDecimal partDecimal = part == null ? BigDecimal.valueOf(0) : new BigDecimal(part); - BigDecimal totalDecimal = total == null ? BigDecimal.valueOf(1) : new BigDecimal(total); - return MathUtils.roundDouble(partDecimal.divide(totalDecimal).doubleValue(), 4); + return MathUtils.roundDouble(HavenoUtils.divide(part == null ? BigInteger.valueOf(0) : part, total == null ? BigInteger.valueOf(1) : total), 4); } /** diff --git a/desktop/src/main/java/haveno/desktop/main/offer/offerbook/OfferBookView.java b/desktop/src/main/java/haveno/desktop/main/offer/offerbook/OfferBookView.java index 134edc0025..9f891f00e3 100644 --- a/desktop/src/main/java/haveno/desktop/main/offer/offerbook/OfferBookView.java +++ b/desktop/src/main/java/haveno/desktop/main/offer/offerbook/OfferBookView.java @@ -109,7 +109,6 @@ import javafx.collections.ListChangeListener; import javafx.util.Callback; import javafx.util.StringConverter; -import java.math.BigDecimal; import java.math.BigInteger; import java.util.Comparator; import java.util.Map; @@ -306,7 +305,7 @@ abstract public class OfferBookView T getParentOfType(Node node, Class t) {