Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update atomic unit conversion utils to use monero-java #1551

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ configure(subprojects) {
gsonVersion = '2.8.5'
guavaVersion = '32.1.1-jre'
guiceVersion = '7.0.0'
moneroJavaVersion = '0.8.34'
moneroJavaVersion = '0.8.35'
httpclient5Version = '5.0'
hamcrestVersion = '2.2'
httpclientVersion = '4.5.12'
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/haveno/core/trade/HavenoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

import lombok.extern.slf4j.Slf4j;
import monero.common.MoneroRpcConnection;
import monero.common.MoneroUtils;
import monero.daemon.model.MoneroOutput;
import monero.wallet.model.MoneroDestination;
import monero.wallet.model.MoneroTxWallet;
Expand Down Expand Up @@ -204,11 +205,11 @@ public static double atomicUnitsToXmr(long atomicUnits) {
}

public static double atomicUnitsToXmr(BigInteger atomicUnits) {
return new BigDecimal(atomicUnits).divide(new BigDecimal(XMR_AU_MULTIPLIER)).doubleValue();
return MoneroUtils.atomicUnitsToXmr(atomicUnits);
}

public static BigInteger xmrToAtomicUnits(double xmr) {
return new BigDecimal(xmr).multiply(new BigDecimal(XMR_AU_MULTIPLIER)).toBigInteger();
return MoneroUtils.xmrToAtomicUnits(xmr);
}

public static long xmrToCentineros(double xmr) {
Expand All @@ -220,11 +221,11 @@ public static double coinToXmr(Coin coin) {
}

public static double divide(BigInteger auDividend, BigInteger auDivisor) {
return atomicUnitsToXmr(auDividend) / atomicUnitsToXmr(auDivisor);
return MoneroUtils.divide(auDividend, auDivisor);
}

public static BigInteger multiply(BigInteger amount1, double amount2) {
return amount1 == null ? null : new BigDecimal(amount1).multiply(BigDecimal.valueOf(amount2)).toBigInteger();
return MoneroUtils.multiply(amount1, amount2);
}

// ------------------------- FORMAT UTILS ---------------------------------
Expand Down
21 changes: 19 additions & 2 deletions core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -781,11 +781,23 @@ public MoneroTx verifyTradeTx(String offerId, BigInteger tradeFeeAmount, String
BigInteger actualSendAmount = transferCheck.getReceivedAmount();

// verify trade fee amount
if (!actualTradeFee.equals(tradeFeeAmount)) throw new RuntimeException("Invalid trade fee amount, expected " + tradeFeeAmount + " but was " + actualTradeFee);
if (!actualTradeFee.equals(tradeFeeAmount)) {
if (equalsWithinFractionError(actualTradeFee, tradeFeeAmount)) {
log.warn("Trade tx fee amount is within fraction error, expected " + tradeFeeAmount + " but was " + actualTradeFee);
} else {
throw new RuntimeException("Invalid trade fee amount, expected " + tradeFeeAmount + " but was " + actualTradeFee);
}
}

// verify send amount
BigInteger expectedSendAmount = sendAmount.subtract(tx.getFee());
if (!actualSendAmount.equals(expectedSendAmount)) throw new RuntimeException("Invalid send amount, expected " + expectedSendAmount + " but was " + actualSendAmount + " with tx fee " + tx.getFee());
if (!actualSendAmount.equals(expectedSendAmount)) {
if (equalsWithinFractionError(actualSendAmount, expectedSendAmount)) {
log.warn("Trade tx send amount is within fraction error, expected " + expectedSendAmount + " but was " + actualSendAmount + " with tx fee " + tx.getFee());
} else {
throw new RuntimeException("Invalid send amount, expected " + expectedSendAmount + " but was " + actualSendAmount + " with tx fee " + tx.getFee());
}
}
return tx;
} catch (Exception e) {
log.warn("Error verifying trade tx with offer id=" + offerId + (tx == null ? "" : ", tx=\n" + tx) + ": " + e.getMessage());
Expand All @@ -801,6 +813,11 @@ public MoneroTx verifyTradeTx(String offerId, BigInteger tradeFeeAmount, String
}
}

// TODO: old bug in atomic unit conversion could cause fractional difference error, remove this in future release, maybe re-sign all offers then
private static boolean equalsWithinFractionError(BigInteger a, BigInteger b) {
return a.subtract(b).abs().compareTo(new BigInteger("1")) <= 0;
}

/**
* Get the tx fee estimate based on its weight.
*
Expand Down
6 changes: 3 additions & 3 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -815,9 +815,9 @@
<sha256 value="c92e2ca40a3f2474d61e56831aeb379cf8ae3dddeea61b4a828cee2d99f71f38" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="io.github.woodser" name="monero-java" version="0.8.34">
<artifact name="monero-java-0.8.34.jar">
<sha256 value="12662589eec0a188175274c6ae3ca309172358a936b27fc95a9013d6015fc81a" origin="Generated by Gradle"/>
<component group="io.github.woodser" name="monero-java" version="0.8.35">
<artifact name="monero-java-0.8.35.jar">
<sha256 value="bd6e7ae8cba64928f16279cb9d21f84484ca7e7377ee7da68bd6686762bb1016" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="io.grpc" name="grpc-api" version="1.42.1">
Expand Down
Loading