fixes to persisted trades initialization

This commit is contained in:
woodser 2023-07-14 13:06:03 -04:00
parent 9f3b0c96c4
commit 5c814700fb

View File

@ -407,6 +407,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
Set<Runnable> tasks = new HashSet<Runnable>(); Set<Runnable> tasks = new HashSet<Runnable>();
Set<String> uids = new HashSet<String>(); Set<String> uids = new HashSet<String>();
Set<Trade> tradesToSkip = new HashSet<Trade>(); Set<Trade> tradesToSkip = new HashSet<Trade>();
Set<Trade> tradesToMaybeRemoveOnError = new HashSet<Trade>();
for (Trade trade : trades) { for (Trade trade : trades) {
tasks.add(() -> { tasks.add(() -> {
try { try {
@ -423,9 +424,10 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
// remove trade if protocol didn't initialize // remove trade if protocol didn't initialize
if (getOpenTradeByUid(trade.getUid()).isPresent() && !trade.isDepositsPublished()) { if (getOpenTradeByUid(trade.getUid()).isPresent() && !trade.isDepositsPublished()) {
maybeRemoveTradeOnError(trade); tradesToMaybeRemoveOnError.add(trade);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
log.warn("Error initializing {} {}: {}", trade.getClass().getSimpleName(), trade.getId(), e.getMessage()); log.warn("Error initializing {} {}: {}", trade.getClass().getSimpleName(), trade.getId(), e.getMessage());
trade.setInitError(e); trade.setInitError(e);
} }
@ -458,6 +460,11 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
EasyBind.subscribe(HavenoUtils.havenoSetup.getAppStartupState().applicationFullyInitializedProperty(), appInitialized -> { EasyBind.subscribe(HavenoUtils.havenoSetup.getAppStartupState().applicationFullyInitializedProperty(), appInitialized -> {
if (!appInitialized) return; if (!appInitialized) return;
// maybe remove trades on error
for (Trade trade : tradesToMaybeRemoveOnError) {
maybeRemoveTradeOnError(trade);
}
// thaw unreserved outputs // thaw unreserved outputs
thawUnreservedOutputs(); thawUnreservedOutputs();
@ -468,25 +475,32 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
log.warn("Swapping pending {} entries at startup. offerId={}", addressEntry.getContext(), addressEntry.getOfferId()); log.warn("Swapping pending {} entries at startup. offerId={}", addressEntry.getContext(), addressEntry.getOfferId());
xmrWalletService.swapTradeEntryToAvailableEntry(addressEntry.getOfferId(), addressEntry.getContext()); xmrWalletService.swapTradeEntryToAvailableEntry(addressEntry.getOfferId(), addressEntry.getContext());
}); });
});
}
// notify that persisted trades initialized onTradesInitiailizedAndAppFullyInitialized();
persistedTradesInitialized.set(true); });
} else {
// We do not include failed trades as they should not be counted anyway in the trade statistics onTradesInitiailizedAndAppFullyInitialized();
// TODO: remove stats? }
Set<Trade> nonFailedTrades = new HashSet<>(closedTradableManager.getClosedTrades());
nonFailedTrades.addAll(tradableList.getList());
String referralId = referralIdService.getOptionalReferralId().orElse(null);
boolean isTorNetworkNode = p2PService.getNetworkNode() instanceof TorNetworkNode;
tradeStatisticsManager.maybeRepublishTradeStatistics(nonFailedTrades, referralId, isTorNetworkNode);
}).start(); }).start();
// allow execution to start // allow execution to start
GenUtils.waitFor(100); GenUtils.waitFor(100);
} }
private void onTradesInitiailizedAndAppFullyInitialized() {
// notify that persisted trades initialized
persistedTradesInitialized.set(true);
// We do not include failed trades as they should not be counted anyway in the trade statistics
// TODO: remove stats?
Set<Trade> nonFailedTrades = new HashSet<>(closedTradableManager.getClosedTrades());
nonFailedTrades.addAll(tradableList.getList());
String referralId = referralIdService.getOptionalReferralId().orElse(null);
boolean isTorNetworkNode = p2PService.getNetworkNode() instanceof TorNetworkNode;
tradeStatisticsManager.maybeRepublishTradeStatistics(nonFailedTrades, referralId, isTorNetworkNode);
}
private void initPersistedTrade(Trade trade) { private void initPersistedTrade(Trade trade) {
if (isShutDown) return; if (isShutDown) return;
if (getTradeProtocol(trade) != null) return; if (getTradeProtocol(trade) != null) return;