initialize trades when all services initialized and p2p bootstrapped

This commit is contained in:
woodser 2023-07-26 10:29:59 -04:00
parent c548fdaf29
commit aa36518f69
2 changed files with 38 additions and 53 deletions

View file

@ -202,13 +202,6 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
p2PService.addDecryptedDirectMessageListener(this); p2PService.addDecryptedDirectMessageListener(this);
failedTradesManager.setUnFailTradeCallback(this::unFailTrade); failedTradesManager.setUnFailTradeCallback(this::unFailTrade);
// initialize trades when connected to p2p network
p2PService.addP2PServiceListener(new BootstrapListener() {
public void onUpdatedDataReceived() {
initPersistedTrades();
}
});
} }
@ -259,6 +252,16 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
public void onAllServicesInitialized() { public void onAllServicesInitialized() {
if (p2PService.isBootstrapped()) {
initPersistedTrades();
} else {
p2PService.addP2PServiceListener(new BootstrapListener() {
@Override
public void onUpdatedDataReceived() {
initPersistedTrades();
}
});
}
// listen for account updates // listen for account updates
accountService.addListener(new AccountServiceListener() { accountService.addListener(new AccountServiceListener() {
@ -443,21 +446,11 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
if (trade.isIdling()) HavenoUtils.submitTask(() -> trade.syncWallet()); if (trade.isIdling()) HavenoUtils.submitTask(() -> trade.syncWallet());
} }
getObservableList().addListener((ListChangeListener<Trade>) change -> onTradesChanged());
onTradesChanged();
xmrWalletService.setTradeManager(this); xmrWalletService.setTradeManager(this);
// process after all wallets initialized // process after all wallets initialized
if (HavenoUtils.havenoSetup != null) { // null for seednode if (HavenoUtils.havenoSetup != null) { // null for seednode
// TODO: this subscription fails to fire about 50% on startup
// MonadicBinding<Boolean> walletsInitialized = EasyBind.combine(HavenoUtils.havenoSetup.getWalletInitialized(), persistedTradesInitialized, (a, b) -> a && b);
// walletsInitialized.subscribe((observable, oldValue, newValue) -> {}
EasyBind.subscribe(HavenoUtils.havenoSetup.getAppStartupState().applicationFullyInitializedProperty(), appInitialized -> {
if (!appInitialized) return;
// maybe remove trades on error // maybe remove trades on error
for (Trade trade : tradesToMaybeRemoveOnError) { for (Trade trade : tradesToMaybeRemoveOnError) {
maybeRemoveTradeOnError(trade); maybeRemoveTradeOnError(trade);
@ -473,22 +466,12 @@ 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.swapAddressEntryToAvailable(addressEntry.getOfferId(), addressEntry.getContext()); xmrWalletService.swapAddressEntryToAvailable(addressEntry.getOfferId(), addressEntry.getContext());
}); });
onTradesInitiailizedAndAppFullyInitialized();
});
} else {
onTradesInitiailizedAndAppFullyInitialized();
} }
}).start();
// allow execution to start
GenUtils.waitFor(100);
}
private void onTradesInitiailizedAndAppFullyInitialized() {
// notify that persisted trades initialized // notify that persisted trades initialized
persistedTradesInitialized.set(true); persistedTradesInitialized.set(true);
getObservableList().addListener((ListChangeListener<Trade>) change -> onTradesChanged());
onTradesChanged();
// We do not include failed trades as they should not be counted anyway in the trade statistics // We do not include failed trades as they should not be counted anyway in the trade statistics
// TODO: remove stats? // TODO: remove stats?
@ -497,6 +480,10 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi
String referralId = referralIdService.getOptionalReferralId().orElse(null); String referralId = referralIdService.getOptionalReferralId().orElse(null);
boolean isTorNetworkNode = p2PService.getNetworkNode() instanceof TorNetworkNode; boolean isTorNetworkNode = p2PService.getNetworkNode() instanceof TorNetworkNode;
tradeStatisticsManager.maybeRepublishTradeStatistics(nonFailedTrades, referralId, isTorNetworkNode); tradeStatisticsManager.maybeRepublishTradeStatistics(nonFailedTrades, referralId, isTorNetworkNode);
}).start();
// allow execution to start
GenUtils.waitFor(100);
} }
private void initPersistedTrade(Trade trade) { private void initPersistedTrade(Trade trade) {

View file

@ -246,10 +246,8 @@ public abstract class TradeProtocol implements DecryptedDirectMessageListener, D
// listen for direct messages unless completed // listen for direct messages unless completed
if (!trade.isCompleted()) processModel.getP2PService().addDecryptedDirectMessageListener(this); if (!trade.isCompleted()) processModel.getP2PService().addDecryptedDirectMessageListener(this);
// initialize trade with lock // initialize trade
synchronized (trade) {
trade.initialize(processModel.getProvider()); trade.initialize(processModel.getProvider());
}
// process mailbox messages // process mailbox messages
MailboxMessageService mailboxMessageService = processModel.getP2PService().getMailboxMessageService(); MailboxMessageService mailboxMessageService = processModel.getP2PService().getMailboxMessageService();