From 10a5b55dfef27b40c544a94a52c1cb517f73f3c1 Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 22 Mar 2024 09:57:06 -0400 Subject: [PATCH] check for trade timeout after arbitrator request and reserve tx --- .../tasks/MakerSendSignOfferRequest.java | 2 +- .../main/java/haveno/core/trade/TradeManager.java | 6 ++++++ .../protocol/tasks/TakerReserveTradeFunds.java | 7 +++---- .../TakerSendInitTradeRequestToArbitrator.java | 15 ++++++++++++++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/haveno/core/offer/placeoffer/tasks/MakerSendSignOfferRequest.java b/core/src/main/java/haveno/core/offer/placeoffer/tasks/MakerSendSignOfferRequest.java index 0b746a1b0d..537bffb566 100644 --- a/core/src/main/java/haveno/core/offer/placeoffer/tasks/MakerSendSignOfferRequest.java +++ b/core/src/main/java/haveno/core/offer/placeoffer/tasks/MakerSendSignOfferRequest.java @@ -133,7 +133,7 @@ public class MakerSendSignOfferRequest extends Task { // if unavailable, try alternative arbitrator @Override public void onFault(String errorMessage) { - log.warn("Arbitrator unavailable: address={}: {}", arbitratorNodeAddress, errorMessage); + log.warn("Arbitrator unavailable: address={}, error={}", arbitratorNodeAddress, errorMessage); excludedArbitrators.add(arbitratorNodeAddress); // get alternative arbitrator diff --git a/core/src/main/java/haveno/core/trade/TradeManager.java b/core/src/main/java/haveno/core/trade/TradeManager.java index 9157f3699e..a8b6af0cd8 100644 --- a/core/src/main/java/haveno/core/trade/TradeManager.java +++ b/core/src/main/java/haveno/core/trade/TradeManager.java @@ -1217,6 +1217,12 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi } } + public boolean hasOpenTrade(Trade trade) { + synchronized (tradableList) { + return tradableList.contains(trade); + } + } + public Optional getOpenTradeByUid(String tradeUid) { synchronized (tradableList) { return tradableList.stream().filter(e -> e.getUid().equals(tradeUid)).findFirst(); diff --git a/core/src/main/java/haveno/core/trade/protocol/tasks/TakerReserveTradeFunds.java b/core/src/main/java/haveno/core/trade/protocol/tasks/TakerReserveTradeFunds.java index 51d7960bd7..b31b30efee 100644 --- a/core/src/main/java/haveno/core/trade/protocol/tasks/TakerReserveTradeFunds.java +++ b/core/src/main/java/haveno/core/trade/protocol/tasks/TakerReserveTradeFunds.java @@ -51,10 +51,9 @@ public class TakerReserveTradeFunds extends TradeTask { List reservedKeyImages = new ArrayList(); for (MoneroOutput input : reserveTx.getInputs()) reservedKeyImages.add(input.getKeyImage().getHex()); - // check for error in case creating reserve tx exceeded timeout - // TODO: better way? - if (!model.getXmrWalletService().getAddressEntry(trade.getId(), XmrAddressEntry.Context.TRADE_PAYOUT).isPresent()) { - throw new RuntimeException("An error has occurred taking trade " + trade.getId() + " causing its subaddress entry to be deleted"); + // check if trade still exists + if (!processModel.getTradeManager().hasOpenTrade(trade)) { + throw new RuntimeException("Trade protocol no longer exists after creating reserve tx, tradeId=" + trade.getId()); } // reset protocol timeout diff --git a/core/src/main/java/haveno/core/trade/protocol/tasks/TakerSendInitTradeRequestToArbitrator.java b/core/src/main/java/haveno/core/trade/protocol/tasks/TakerSendInitTradeRequestToArbitrator.java index 2013531521..4cebead917 100644 --- a/core/src/main/java/haveno/core/trade/protocol/tasks/TakerSendInitTradeRequestToArbitrator.java +++ b/core/src/main/java/haveno/core/trade/protocol/tasks/TakerSendInitTradeRequestToArbitrator.java @@ -70,14 +70,27 @@ public class TakerSendInitTradeRequestToArbitrator extends TradeTask { @Override public void onArrived() { log.info("{} arrived at arbitrator: offerId={}", InitTradeRequest.class.getSimpleName(), trade.getId()); + + // check if trade still exists + if (!processModel.getTradeManager().hasOpenTrade(trade)) { + errorMessageHandler.handleErrorMessage("Trade protocol no longer exists, tradeId=" + trade.getId()); + return; + } resultHandler.handleResult(); } // if unavailable, try alternative arbitrator @Override public void onFault(String errorMessage) { - log.warn("Arbitrator {} unavailable: {}", arbitratorNodeAddress, errorMessage); + log.warn("Arbitrator unavailable: address={}, error={}", arbitratorNodeAddress, errorMessage); excludedArbitrators.add(arbitratorNodeAddress); + + // check if trade still exists + if (!processModel.getTradeManager().hasOpenTrade(trade)) { + errorMessageHandler.handleErrorMessage("Trade protocol no longer exists, tradeId=" + trade.getId()); + return; + } + Arbitrator altArbitrator = DisputeAgentSelection.getLeastUsedArbitrator(processModel.getTradeStatisticsManager(), processModel.getArbitratorManager(), excludedArbitrators); if (altArbitrator == null) { errorMessageHandler.handleErrorMessage("Cannot take offer because no arbitrators are available");