From 8097b0f499242772ef5b08c0a3840d4d05b13c68 Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 19 Apr 2024 13:17:42 -0400 Subject: [PATCH] use cached txs in xmr wallet service instead of querying wallet --- .../haveno/core/api/CoreWalletsService.java | 2 +- .../haveno/core/offer/OpenOfferManager.java | 8 ++--- .../main/java/haveno/core/trade/Trade.java | 4 +-- .../core/xmr/wallet/XmrWalletService.java | 34 +++++++++++++------ .../desktop/components/TxIdTextField.java | 2 +- .../transactions/DisplayedTransactions.java | 2 +- .../pendingtrades/steps/TradeStepView.java | 2 +- .../DisplayedTransactionsTest.java | 4 +-- 8 files changed, 36 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/haveno/core/api/CoreWalletsService.java b/core/src/main/java/haveno/core/api/CoreWalletsService.java index 3b5dcefb..351ec7d1 100644 --- a/core/src/main/java/haveno/core/api/CoreWalletsService.java +++ b/core/src/main/java/haveno/core/api/CoreWalletsService.java @@ -158,7 +158,7 @@ class CoreWalletsService { List getXmrTxs() { accountService.checkAccountOpen(); - return xmrWalletService.getWallet().getTxs(); + return xmrWalletService.getTxs(); } MoneroTxWallet createXmrTx(List destinations) { diff --git a/core/src/main/java/haveno/core/offer/OpenOfferManager.java b/core/src/main/java/haveno/core/offer/OpenOfferManager.java index 8ebf1d1b..83b7f3be 100644 --- a/core/src/main/java/haveno/core/offer/OpenOfferManager.java +++ b/core/src/main/java/haveno/core/offer/OpenOfferManager.java @@ -947,14 +947,14 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe // return split output tx if already assigned if (openOffer != null && openOffer.getSplitOutputTxHash() != null) { - return xmrWalletService.getWallet().getTx(openOffer.getSplitOutputTxHash()); + return xmrWalletService.getTx(openOffer.getSplitOutputTxHash()); } // return earliest tx with exact amount to offer's subaddress if available if (preferredSubaddressIndex != null) { // get txs with exact output amount - fundingTxs = xmrWalletService.getWallet().getTxs(new MoneroTxQuery() + fundingTxs = xmrWalletService.getTxs(new MoneroTxQuery() .setIsConfirmed(true) .setOutputQuery(new MoneroOutputQuery() .setAccountIndex(0) @@ -972,7 +972,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe if (openOffer.getScheduledTxHashes() != null) return null; // get all transactions including from pool - List allTxs = xmrWalletService.getTransactions(false); + List allTxs = xmrWalletService.getTxs(false); if (preferredSubaddressIndex != null) { @@ -1067,7 +1067,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe } // get locked txs - List lockedTxs = xmrWalletService.getWallet().getTxs(new MoneroTxQuery().setIsLocked(true)); + List lockedTxs = xmrWalletService.getTxs(new MoneroTxQuery().setIsLocked(true)); // get earliest unscheduled txs with sufficient incoming amount List scheduledTxHashes = new ArrayList(); diff --git a/core/src/main/java/haveno/core/trade/Trade.java b/core/src/main/java/haveno/core/trade/Trade.java index f90e3049..53f68ad0 100644 --- a/core/src/main/java/haveno/core/trade/Trade.java +++ b/core/src/main/java/haveno/core/trade/Trade.java @@ -1215,7 +1215,7 @@ public abstract class Trade implements Tradable, Model { } // then check daemon - if (tx == null) tx = getXmrWalletService().getTxWithCache(txId); + if (tx == null) tx = xmrWalletService.getDaemonTxWithCache(txId); return tx; } @@ -1541,7 +1541,7 @@ public abstract class Trade implements Tradable, Model { @Nullable public MoneroTx getPayoutTx() { if (payoutTx == null) { - payoutTx = payoutTxId == null ? null : (this instanceof ArbitratorTrade) ? xmrWalletService.getTxWithCache(payoutTxId) : xmrWalletService.getWallet().getTx(payoutTxId); + payoutTx = payoutTxId == null ? null : (this instanceof ArbitratorTrade) ? xmrWalletService.getDaemonTxWithCache(payoutTxId) : xmrWalletService.getTx(payoutTxId); } return payoutTx; } diff --git a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java index 335b5720..70098e63 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -779,12 +779,12 @@ public class XmrWalletService { return feeEstimate; } - public MoneroTx getTx(String txHash) { - List txs = getTxs(Arrays.asList(txHash)); + public MoneroTx getDaemonTx(String txHash) { + List txs = getDaemonTxs(Arrays.asList(txHash)); return txs.isEmpty() ? null : txs.get(0); } - public List getTxs(List txHashes) { + public List getDaemonTxs(List txHashes) { synchronized (txCache) { // fetch txs @@ -804,12 +804,12 @@ public class XmrWalletService { } } - public MoneroTx getTxWithCache(String txHash) { - List cachedTxs = getTxsWithCache(Arrays.asList(txHash)); + public MoneroTx getDaemonTxWithCache(String txHash) { + List cachedTxs = getDaemonTxsWithCache(Arrays.asList(txHash)); return cachedTxs.isEmpty() ? null : cachedTxs.get(0); } - public List getTxsWithCache(List txHashes) { + public List getDaemonTxsWithCache(List txHashes) { synchronized (txCache) { try { // get cached txs @@ -821,7 +821,7 @@ public class XmrWalletService { } // return txs from cache if available, otherwise fetch - return uncachedTxHashes.isEmpty() ? cachedTxs : getTxs(txHashes); + return uncachedTxHashes.isEmpty() ? cachedTxs : getDaemonTxs(txHashes); } catch (Exception e) { if (!isShutDownStarted) throw e; return null; @@ -1136,13 +1136,27 @@ public class XmrWalletService { xmrAddressEntryList.requestPersistence(); } - public List getTransactions(boolean includeFailed) { + public List getTxs(boolean includeFailed) { + List txs = getTxs(); + if (includeFailed) return txs; + return txs.stream().filter(tx -> !tx.isFailed()).collect(Collectors.toList()); + } + + public List getTxs() { + return getTxs(new MoneroTxQuery()); + } + + public List getTxs(MoneroTxQuery query) { if (cachedTxs == null) { log.warn("Transactions not cached, fetching from wallet"); cachedTxs = wallet.getTxs(new MoneroTxQuery().setIncludeOutputs(true)); // fetches from pool } - if (includeFailed) return cachedTxs; - return cachedTxs.stream().filter(tx -> !tx.isFailed()).collect(Collectors.toList()); + return cachedTxs.stream().filter(tx -> query.meetsCriteria(tx)).collect(Collectors.toList()); + } + + public MoneroTxWallet getTx(String txId) { + List txs = getTxs(new MoneroTxQuery().setHash(txId)); + return txs.isEmpty() ? null : txs.get(0); } public BigInteger getBalance() { diff --git a/desktop/src/main/java/haveno/desktop/components/TxIdTextField.java b/desktop/src/main/java/haveno/desktop/components/TxIdTextField.java index 21786959..aed93dd1 100644 --- a/desktop/src/main/java/haveno/desktop/components/TxIdTextField.java +++ b/desktop/src/main/java/haveno/desktop/components/TxIdTextField.java @@ -171,7 +171,7 @@ public class TxIdTextField extends AnchorPane { private synchronized void updateConfidence(String txId, boolean useCache, Long height) { MoneroTx tx = null; try { - tx = useCache ? xmrWalletService.getTxWithCache(txId) : xmrWalletService.getTx(txId); + tx = useCache ? xmrWalletService.getDaemonTxWithCache(txId) : xmrWalletService.getDaemonTx(txId); tx.setNumConfirmations(tx.isConfirmed() ? (height == null ? xmrWalletService.getConnectionService().getLastInfo().getHeight() : height) - tx.getHeight(): 0l); // TODO: don't set if tx.getNumConfirmations() works reliably on non-local testnet } catch (Exception e) { // do nothing diff --git a/desktop/src/main/java/haveno/desktop/main/funds/transactions/DisplayedTransactions.java b/desktop/src/main/java/haveno/desktop/main/funds/transactions/DisplayedTransactions.java index 50c02e17..4400bfba 100644 --- a/desktop/src/main/java/haveno/desktop/main/funds/transactions/DisplayedTransactions.java +++ b/desktop/src/main/java/haveno/desktop/main/funds/transactions/DisplayedTransactions.java @@ -50,7 +50,7 @@ class DisplayedTransactions extends ObservableListDecorator getTransactionListItems() { - List transactions = xmrWalletService.getTransactions(false); + List transactions = xmrWalletService.getTxs(false); return transactions.stream() .map(this::convertTransactionToListItem) .collect(Collectors.toList()); diff --git a/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/steps/TradeStepView.java b/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/steps/TradeStepView.java index b3ba0aed..1831978d 100644 --- a/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/steps/TradeStepView.java +++ b/desktop/src/main/java/haveno/desktop/main/portfolio/pendingtrades/steps/TradeStepView.java @@ -171,7 +171,7 @@ public abstract class TradeStepView extends AnchorPane { List txIds = new ArrayList(); if (!model.dataModel.makerTxId.isEmpty().get()) txIds.add(model.dataModel.makerTxId.get()); if (!model.dataModel.takerTxId.isEmpty().get()) txIds.add(model.dataModel.takerTxId.get()); - new Thread(() -> trade.getXmrWalletService().getTxsWithCache(txIds)).start(); + new Thread(() -> trade.getXmrWalletService().getDaemonTxsWithCache(txIds)).start(); } public void activate() { diff --git a/desktop/src/test/java/haveno/desktop/main/funds/transactions/DisplayedTransactionsTest.java b/desktop/src/test/java/haveno/desktop/main/funds/transactions/DisplayedTransactionsTest.java index d7237ba8..4f43d6e0 100644 --- a/desktop/src/test/java/haveno/desktop/main/funds/transactions/DisplayedTransactionsTest.java +++ b/desktop/src/test/java/haveno/desktop/main/funds/transactions/DisplayedTransactionsTest.java @@ -40,7 +40,7 @@ public class DisplayedTransactionsTest { List transactions = Lists.newArrayList(mock(MoneroTxWallet.class), mock(MoneroTxWallet.class)); XmrWalletService walletService = mock(XmrWalletService.class); - when(walletService.getTransactions(false)).thenReturn(transactions); + when(walletService.getTxs(false)).thenReturn(transactions); TransactionListItemFactory transactionListItemFactory = mock(TransactionListItemFactory.class, RETURNS_DEEP_STUBS); @@ -60,7 +60,7 @@ public class DisplayedTransactionsTest { @Test public void testUpdateWhenRepositoryIsEmpty() { XmrWalletService walletService = mock(XmrWalletService.class); - when(walletService.getTransactions(false)) + when(walletService.getTxs(false)) .thenReturn(Collections.singletonList(mock(MoneroTxWallet.class))); TradableRepository tradableRepository = mock(TradableRepository.class);