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.
This commit is contained in:
Steven Barclay 2023-02-09 14:34:18 +08:00
parent b1e2cb03ad
commit 88dd924577
No known key found for this signature in database
GPG key ID: 9FED6BF1176D500B
2 changed files with 11 additions and 22 deletions

View file

@ -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<Dispute> disputes = refundManager.getDisputesAsObservableList();
boolean isAnyDisputeRelatedToThis = refundManager.getDisputedTradeIds().contains(tradeId);

View file

@ -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));
}