delete trade wallet on error if deposit not requested

This commit is contained in:
woodser 2022-11-24 17:10:10 +00:00
parent 79db2bc0af
commit 456ef912ed

View file

@ -482,7 +482,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
((ArbitratorProtocol) getTradeProtocol(trade)).handleInitTradeRequest(request, sender, errorMessage -> { ((ArbitratorProtocol) getTradeProtocol(trade)).handleInitTradeRequest(request, sender, errorMessage -> {
log.warn("Arbitrator error during trade initialization for trade {}: {}", trade.getId(), errorMessage); log.warn("Arbitrator error during trade initialization for trade {}: {}", trade.getId(), errorMessage);
removeTrade(trade); removeTradeOnError(trade);
if (takeOfferRequestErrorMessageHandler != null) takeOfferRequestErrorMessageHandler.handleErrorMessage(errorMessage); if (takeOfferRequestErrorMessageHandler != null) takeOfferRequestErrorMessageHandler.handleErrorMessage(errorMessage);
}); });
@ -567,7 +567,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
((MakerProtocol) getTradeProtocol(trade)).handleInitTradeRequest(request, sender, errorMessage -> { ((MakerProtocol) getTradeProtocol(trade)).handleInitTradeRequest(request, sender, errorMessage -> {
log.warn("Maker error during trade initialization: " + errorMessage); log.warn("Maker error during trade initialization: " + errorMessage);
removeTrade(trade); removeTradeOnError(trade);
openOfferManager.unreserveOpenOffer(openOffer); // offer remains available // TODO: only unreserve if funds not deposited to multisig openOfferManager.unreserveOpenOffer(openOffer); // offer remains available // TODO: only unreserve if funds not deposited to multisig
if (takeOfferRequestErrorMessageHandler != null) takeOfferRequestErrorMessageHandler.handleErrorMessage(errorMessage); if (takeOfferRequestErrorMessageHandler != null) takeOfferRequestErrorMessageHandler.handleErrorMessage(errorMessage);
}); });
@ -750,7 +750,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
requestPersistence(); requestPersistence();
}, errorMessage -> { }, errorMessage -> {
log.warn("Taker error during trade initialization: " + errorMessage); log.warn("Taker error during trade initialization: " + errorMessage);
removeTrade(trade); removeTradeOnError(trade);
errorMessageHandler.handleErrorMessage(errorMessage); errorMessageHandler.handleErrorMessage(errorMessage);
if (takeOfferRequestErrorMessageHandler != null) takeOfferRequestErrorMessageHandler.handleErrorMessage(errorMessage); if (takeOfferRequestErrorMessageHandler != null) takeOfferRequestErrorMessageHandler.handleErrorMessage(errorMessage);
}); });
@ -875,7 +875,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
// If trade is in already in critical state (if taker role: taker fee; both roles: after deposit published) // If trade is in already in critical state (if taker role: taker fee; both roles: after deposit published)
// we move the trade to failedTradesManager // we move the trade to failedTradesManager
public void onMoveInvalidTradeToFailedTrades(Trade trade) { public void onMoveInvalidTradeToFailedTrades(Trade trade) {
removeTrade(trade); removeTradeOnError(trade);
failedTradesManager.add(trade); failedTradesManager.add(trade);
} }
@ -1028,6 +1028,14 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
return closedTradableManager.getClosedTrades().stream().filter(e -> e.getId().equals(tradeId)).findFirst(); return closedTradableManager.getClosedTrades().stream().filter(e -> e.getId().equals(tradeId)).findFirst();
} }
private void addTrade(Trade trade) {
synchronized(tradableList) {
if (tradableList.add(trade)) {
requestPersistence();
}
}
}
private synchronized void removeTrade(Trade trade) { private synchronized void removeTrade(Trade trade) {
log.info("TradeManager.removeTrade() " + trade.getId()); log.info("TradeManager.removeTrade() " + trade.getId());
synchronized(tradableList) { synchronized(tradableList) {
@ -1036,28 +1044,34 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
// remove trade // remove trade
tradableList.remove(trade); tradableList.remove(trade);
// unreserve trade key images
if (trade instanceof TakerTrade && trade.getSelf().getReserveTxKeyImages() != null) {
for (String keyImage : trade.getSelf().getReserveTxKeyImages()) xmrWalletService.getWallet().thawOutput(keyImage);
xmrWalletService.getWallet().save();
}
// delete trade wallet if before funded
if (xmrWalletService.multisigWalletExists(trade.getId()) && !trade.isDepositRequested()) {
trade.deleteWallet();
}
// unregister and persist // unregister and persist
p2PService.removeDecryptedDirectMessageListener(getTradeProtocol(trade)); p2PService.removeDecryptedDirectMessageListener(getTradeProtocol(trade));
requestPersistence(); requestPersistence();
} }
} }
private void addTrade(Trade trade) { private synchronized void removeTradeOnError(Trade trade) {
log.info("TradeManager.removeTradeOnError() " + trade.getId());
synchronized(tradableList) { synchronized(tradableList) {
if (tradableList.add(trade)) { if (!tradableList.contains(trade)) return;
requestPersistence();
// skip if trade wallet possibly funded
if (xmrWalletService.multisigWalletExists(trade.getId()) && trade.isDepositRequested()) {
log.warn("Not removing trade {} because trade wallet could be funded", trade.getId());
return;
} }
// delete trade wallet
trade.deleteWallet();
// unreserve key images
if (trade instanceof TakerTrade && trade.getSelf().getReserveTxKeyImages() != null) {
for (String keyImage : trade.getSelf().getReserveTxKeyImages()) xmrWalletService.getWallet().thawOutput(keyImage);
xmrWalletService.getWallet().save();
}
// remove trade
removeTrade(trade);
} }
} }