From 88dd92457740effeac848b3602f288adc728af50 Mon Sep 17 00:00:00 2001 From: Steven Barclay Date: Thu, 9 Feb 2023 14:34:18 +0800 Subject: [PATCH] Clean up TransactionAwareTrade & speed it up slightly Replace the "Optional.ofNullable(...)..." constructs with more direct code using short-circuit operators, as this is shorter and a little faster. Also use "trade.get[Deposit|Payout]TxId()" instead of the code "trade.get[Deposit|Payout]TxId().getTxId()", as (upon inspection of the code) there should never be a case where the deposit/payout transaction field of a Trade object is set but the respective txID field is null (or set to an inconsistent value). Also remove a redundant 'RefundManager.getDisputesAsObservableList' method call, which was also slowing things down slightly. The minor speedups afforded by the above are important because the method 'TransactionAwareTrade.isRelatedToTransaction' is called a quadratic number of times and consequently a major bottleneck when loading the Transactions view. --- .../transactions/TransactionAwareTrade.java | 29 ++++++------------- .../TransactionAwareTradeTest.java | 4 +-- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionAwareTrade.java b/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionAwareTrade.java index d940ad5df7..cc44923100 100644 --- a/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionAwareTrade.java +++ b/desktop/src/main/java/bisq/desktop/main/funds/transactions/TransactionAwareTrade.java @@ -37,8 +37,6 @@ import org.bitcoinj.core.TransactionOutput; import javafx.collections.ObservableList; -import java.util.Optional; - import lombok.extern.slf4j.Slf4j; import static com.google.common.base.Preconditions.checkNotNull; @@ -73,8 +71,8 @@ class TransactionAwareTrade implements TransactionAwareTradable { Trade trade = (Trade) tradeModel; boolean isTakerOfferFeeTx = txId.equals(trade.getTakerFeeTxId()); boolean isOfferFeeTx = isOfferFeeTx(txId); - boolean isDepositTx = isDepositTx(hash); - boolean isPayoutTx = isPayoutTx(hash); + boolean isDepositTx = isDepositTx(txId); + boolean isPayoutTx = isPayoutTx(txId); boolean isDisputedPayoutTx = isDisputedPayoutTx(txId); boolean isDelayedPayoutTx = transaction.getLockTime() != 0 && isDelayedPayoutTx(txId); boolean isRefundPayoutTx = isRefundPayoutTx(trade, txId); @@ -91,36 +89,28 @@ class TransactionAwareTrade implements TransactionAwareTradable { return tradeRelated || isBsqSwapTrade; } - private boolean isPayoutTx(Sha256Hash txId) { + private boolean isPayoutTx(String txId) { if (isBsqSwapTrade()) return false; Trade trade = (Trade) tradeModel; - return Optional.ofNullable(trade.getPayoutTx()) - .map(Transaction::getTxId) - .map(hash -> hash.equals(txId)) - .orElse(false); + return txId.equals(trade.getPayoutTxId()); } - private boolean isDepositTx(Sha256Hash txId) { + private boolean isDepositTx(String txId) { if (isBsqSwapTrade()) return false; Trade trade = (Trade) tradeModel; - return Optional.ofNullable(trade.getDepositTx()) - .map(Transaction::getTxId) - .map(hash -> hash.equals(txId)) - .orElse(false); + return txId.equals(trade.getDepositTxId()); } private boolean isOfferFeeTx(String txId) { if (isBsqSwapTrade()) return false; - return Optional.ofNullable(tradeModel.getOffer()) - .map(Offer::getOfferFeePaymentTxId) - .map(paymentTxId -> paymentTxId.equals(txId)) - .orElse(false); + Offer offer = tradeModel.getOffer(); + return offer != null && txId.equals(offer.getOfferFeePaymentTxId()); } private boolean isDisputedPayoutTx(String txId) { @@ -168,7 +158,7 @@ class TransactionAwareTrade implements TransactionAwareTradable { if (parentTransaction == null) { return false; } - return isDepositTx(parentTransaction.getTxId()); + return isDepositTx(parentTransaction.getTxId().toString()); }); } @@ -177,7 +167,6 @@ class TransactionAwareTrade implements TransactionAwareTradable { return false; String tradeId = tradeModel.getId(); - ObservableList disputes = refundManager.getDisputesAsObservableList(); boolean isAnyDisputeRelatedToThis = refundManager.getDisputedTradeIds().contains(tradeId); diff --git a/desktop/src/test/java/bisq/desktop/main/funds/transactions/TransactionAwareTradeTest.java b/desktop/src/test/java/bisq/desktop/main/funds/transactions/TransactionAwareTradeTest.java index 711dac141c..9659e202fe 100644 --- a/desktop/src/test/java/bisq/desktop/main/funds/transactions/TransactionAwareTradeTest.java +++ b/desktop/src/test/java/bisq/desktop/main/funds/transactions/TransactionAwareTradeTest.java @@ -66,13 +66,13 @@ public class TransactionAwareTradeTest { @Test public void testIsRelatedToTransactionWhenPayoutTx() { - when(delegate.getPayoutTx().getTxId()).thenReturn(XID); + when(delegate.getPayoutTxId()).thenReturn(XID.toString()); assertTrue(trade.isRelatedToTransaction(transaction)); } @Test public void testIsRelatedToTransactionWhenDepositTx() { - when(delegate.getDepositTx().getTxId()).thenReturn(XID); + when(delegate.getDepositTxId()).thenReturn(XID.toString()); assertTrue(trade.isRelatedToTransaction(transaction)); }