clear and shut down inactive trades, move more to trade threads

This commit is contained in:
woodser 2024-01-05 13:23:52 -05:00
parent c14f37b595
commit c06a85b929
9 changed files with 82 additions and 75 deletions

View file

@ -331,7 +331,8 @@ public final class ArbitrationManager extends DisputeManager<ArbitrationDisputeL
}
public void maybeReprocessDisputeClosedMessage(Trade trade, boolean reprocessOnError) {
new Thread(() -> {
if (trade.isShutDownStarted()) return;
ThreadUtils.execute(() -> {
synchronized (trade) {
// skip if no need to reprocess
@ -342,7 +343,7 @@ public final class ArbitrationManager extends DisputeManager<ArbitrationDisputeL
log.warn("Reprocessing dispute closed message for {} {}", trade.getClass().getSimpleName(), trade.getId());
handleDisputeClosedMessage(trade.getArbitrator().getDisputeClosedMessage(), reprocessOnError);
}
}).start();
}, trade.getId());
}
private MoneroTxSet signAndPublishDisputePayoutTx(Trade trade) {

View file

@ -591,6 +591,12 @@ public abstract class Trade implements Tradable, Model {
ThreadUtils.await(() -> {
if (isInitialized) throw new IllegalStateException(getClass().getSimpleName() + " " + getId() + " is already initialized");
// check if done
if (isPayoutUnlocked()) {
clearAndShutDown();
return;
}
// set arbitrator pub key ring once known
serviceProvider.getArbitratorManager().getDisputeAgentByNodeAddress(getArbitratorNodeAddress()).ifPresent(arbitrator -> {
getArbitrator().setPubKeyRing(arbitrator.getPubKeyRing());
@ -603,12 +609,6 @@ public abstract class Trade implements Tradable, Model {
});
});
// check if done
if (isPayoutUnlocked()) {
maybeClearProcessData();
return;
}
// reset buyer's payment sent state if no ack receive
if (this instanceof BuyerTrade && getState().ordinal() >= Trade.State.BUYER_CONFIRMED_IN_UI_PAYMENT_SENT.ordinal() && getState().ordinal() < Trade.State.BUYER_STORED_IN_MAILBOX_PAYMENT_SENT_MSG.ordinal()) {
log.warn("Resetting state of {} {} from {} to {} because no ack was received", getClass().getSimpleName(), getId(), getState(), Trade.State.DEPOSIT_TXS_UNLOCKED_IN_BLOCKCHAIN);
@ -623,6 +623,7 @@ public abstract class Trade implements Tradable, Model {
// handle trade state events
tradeStateSubscription = EasyBind.subscribe(stateProperty, newValue -> {
if (isShutDownStarted) return;
ThreadUtils.execute(() -> {
if (newValue == Trade.State.MULTISIG_COMPLETED) {
updateWalletRefreshPeriod();
@ -633,6 +634,7 @@ public abstract class Trade implements Tradable, Model {
// handle trade phase events
tradePhaseSubscription = EasyBind.subscribe(phaseProperty, newValue -> {
if (isShutDownStarted) return;
ThreadUtils.execute(() -> {
if (isDepositsPublished() && !isPayoutUnlocked()) updateWalletRefreshPeriod();
if (isPaymentReceived()) {
@ -648,6 +650,7 @@ public abstract class Trade implements Tradable, Model {
// handle payout events
payoutStateSubscription = EasyBind.subscribe(payoutStateProperty, newValue -> {
if (isShutDownStarted) return;
ThreadUtils.execute(() -> {
if (isPayoutPublished()) updateWalletRefreshPeriod();
@ -679,21 +682,7 @@ public abstract class Trade implements Tradable, Model {
if (newValue == Trade.PayoutState.PAYOUT_UNLOCKED) {
if (!isInitialized) return;
log.info("Payout unlocked for {} {}, deleting multisig wallet", getClass().getSimpleName(), getId());
ThreadUtils.execute(() -> {
deleteWallet();
maybeClearProcessData();
if (idlePayoutSyncer != null) {
xmrWalletService.removeWalletListener(idlePayoutSyncer);
idlePayoutSyncer = null;
}
UserThread.execute(() -> {
if (payoutStateSubscription != null) {
payoutStateSubscription.unsubscribe();
payoutStateSubscription = null;
}
});
}, getId());
clearAndShutDown();
}
}, getId());
});
@ -912,7 +901,7 @@ public abstract class Trade implements Tradable, Model {
throw new RuntimeException("Refusing to delete wallet for " + getClass().getSimpleName() + " " + getId() + " because it has a balance");
}
// force stop the wallet
// force stop wallet
if (wallet != null) stopWallet();
// delete wallet
@ -1195,21 +1184,24 @@ public abstract class Trade implements Tradable, Model {
return payoutAmountFromMediation < normalPayoutAmount;
}
public void maybeClearProcessData() {
public void clearAndShutDown() {
ThreadUtils.execute(() -> clearProcessData(), getId());
ThreadUtils.submitToPool(() -> shutDown()); // run off trade thread
}
private void clearProcessData() {
// delete trade wallet
synchronized (walletLock) {
// skip if already cleared
if (!walletExists()) return;
// delete trade wallet
if (!walletExists()) return; // done if already cleared
deleteWallet();
}
// TODO: clear other process data
setPayoutTxHex(null);
for (TradePeer peer : getPeers()) {
peer.setUnsignedPayoutTxHex(null);
peer.setUpdatedMultisigHex(null);
}
// TODO: clear other process data
setPayoutTxHex(null);
for (TradePeer peer : getPeers()) {
peer.setUnsignedPayoutTxHex(null);
peer.setUpdatedMultisigHex(null);
}
}
@ -1230,6 +1222,8 @@ public abstract class Trade implements Tradable, Model {
}
public void shutDown() {
if (isShutDown) return; // ignore if already shut down
isShutDownStarted = true;
if (!isPayoutUnlocked()) log.info("Shutting down {} {}", getClass().getSimpleName(), getId());
// shut down thread pools with timeout

View file

@ -1262,10 +1262,10 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
// remove trade from list
removeTrade(trade);
// delete trade wallet
if (trade.walletExists()) trade.deleteWallet();
}
// clear and shut down trade
trade.clearAndShutDown();
}
private void listenForCleanup(Trade trade) {

View file

@ -17,6 +17,7 @@
package haveno.core.trade.protocol;
import haveno.common.ThreadUtils;
import haveno.common.handlers.ErrorMessageHandler;
import haveno.common.handlers.ResultHandler;
import haveno.core.trade.BuyerTrade;
@ -97,7 +98,7 @@ public class BuyerProtocol extends DisputeProtocol {
public void onPaymentSent(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
System.out.println("BuyerProtocol.onPaymentSent()");
new Thread(() -> {
ThreadUtils.execute(() -> {
synchronized (trade) {
latchTrade();
this.errorMessageHandler = errorMessageHandler;
@ -127,7 +128,7 @@ public class BuyerProtocol extends DisputeProtocol {
}
awaitTradeLatch();
}
}).start();
}, trade.getId());
}
@SuppressWarnings("unchecked")

View file

@ -17,6 +17,7 @@
package haveno.core.trade.protocol;
import haveno.common.ThreadUtils;
import haveno.common.handlers.ErrorMessageHandler;
import haveno.common.handlers.ResultHandler;
import haveno.core.trade.SellerTrade;
@ -94,7 +95,7 @@ public class SellerProtocol extends DisputeProtocol {
public void onPaymentReceived(ResultHandler resultHandler, ErrorMessageHandler errorMessageHandler) {
log.info("SellerProtocol.onPaymentReceived()");
new Thread(() -> {
ThreadUtils.execute(() -> {
synchronized (trade) {
latchTrade();
this.errorMessageHandler = errorMessageHandler;
@ -123,7 +124,7 @@ public class SellerProtocol extends DisputeProtocol {
}
awaitTradeLatch();
}
}).start();
}, trade.getId());
}
@SuppressWarnings("unchecked")

View file

@ -265,6 +265,7 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
}
public void maybeSendDepositsConfirmedMessages() {
if (!trade.isInitialized() || trade.isShutDownStarted()) return;
ThreadUtils.execute(() -> {
if (!trade.isDepositsConfirmed() || trade.isDepositsConfirmedAcked() || trade.isPayoutPublished()) return;
synchronized (trade) {
@ -286,6 +287,7 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
}
public void maybeReprocessPaymentReceivedMessage(boolean reprocessOnError) {
if (trade.isShutDownStarted()) return;
ThreadUtils.execute(() -> {
synchronized (trade) {