update trades on UserThread in TradeManager to fix npe

This commit is contained in:
woodser 2024-01-04 14:48:11 -05:00
parent 0d60df2aa7
commit 52a44d020e

View file

@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableList;
import common.utils.GenUtils; import common.utils.GenUtils;
import haveno.common.ClockWatcher; import haveno.common.ClockWatcher;
import haveno.common.ThreadUtils; import haveno.common.ThreadUtils;
import haveno.common.UserThread;
import haveno.common.crypto.KeyRing; import haveno.common.crypto.KeyRing;
import haveno.common.crypto.PubKeyRing; import haveno.common.crypto.PubKeyRing;
import haveno.common.handlers.ErrorMessageHandler; import haveno.common.handlers.ErrorMessageHandler;
@ -573,9 +574,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
// initialize trade protocol // initialize trade protocol
initTradeAndProtocol(trade, createTradeProtocol(trade)); initTradeAndProtocol(trade, createTradeProtocol(trade));
synchronized (tradableList) { addTrade(trade);
tradableList.add(trade);
}
} }
// process with protocol // process with protocol
@ -654,9 +653,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
trade.getSelf().setReserveTxHex(openOffer.getReserveTxHex()); trade.getSelf().setReserveTxHex(openOffer.getReserveTxHex());
trade.getSelf().setReserveTxKey(openOffer.getReserveTxKey()); trade.getSelf().setReserveTxKey(openOffer.getReserveTxKey());
trade.getSelf().setReserveTxKeyImages(offer.getOfferPayload().getReserveTxKeyImages()); trade.getSelf().setReserveTxKeyImages(offer.getOfferPayload().getReserveTxKeyImages());
synchronized (tradableList) { addTrade(trade);
tradableList.add(trade);
}
// notify on phase changes // notify on phase changes
// TODO (woodser): save subscription, bind on startup // TODO (woodser): save subscription, bind on startup
@ -671,8 +668,6 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
log.warn("Maker error during trade initialization: " + errorMessage); log.warn("Maker error during trade initialization: " + errorMessage);
maybeRemoveTradeOnError(trade); maybeRemoveTradeOnError(trade);
}); });
requestPersistence();
} }
} }
@ -845,9 +840,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
// initialize trade protocol // initialize trade protocol
TradeProtocol tradeProtocol = createTradeProtocol(trade); TradeProtocol tradeProtocol = createTradeProtocol(trade);
synchronized (tradableList) { addTrade(trade);
tradableList.add(trade);
}
initTradeAndProtocol(trade, tradeProtocol); initTradeAndProtocol(trade, tradeProtocol);
@ -1065,11 +1058,13 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
initPersistedTrade(trade); initPersistedTrade(trade);
UserThread.execute(() -> {
synchronized (tradableList) { synchronized (tradableList) {
if (!tradableList.contains(trade)) { if (!tradableList.contains(trade)) {
tradableList.add(trade); tradableList.add(trade);
} }
} }
});
return true; return true;
} }
@ -1211,26 +1206,32 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
} }
private void addTrade(Trade trade) { private void addTrade(Trade trade) {
UserThread.execute(() -> {
synchronized (tradableList) { synchronized (tradableList) {
if (tradableList.add(trade)) { if (tradableList.add(trade)) {
requestPersistence(); requestPersistence();
} }
} }
});
} }
private void removeTrade(Trade trade) { private void removeTrade(Trade trade) {
log.info("TradeManager.removeTrade() " + trade.getId()); log.info("TradeManager.removeTrade() " + trade.getId());
synchronized (tradableList) { synchronized (tradableList) {
if (!tradableList.contains(trade)) return; if (!tradableList.contains(trade)) return;
}
// remove trade // remove trade
UserThread.execute(() -> {
synchronized (tradableList) {
tradableList.remove(trade); tradableList.remove(trade);
}
});
// unregister and persist // unregister and persist
p2PService.removeDecryptedDirectMessageListener(getTradeProtocol(trade)); p2PService.removeDecryptedDirectMessageListener(getTradeProtocol(trade));
requestPersistence(); requestPersistence();
} }
}
private void maybeRemoveTradeOnError(Trade trade) { private void maybeRemoveTradeOnError(Trade trade) {
synchronized (tradableList) { synchronized (tradableList) {