diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index 8e6a22c9ba..d44d366212 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -303,7 +303,7 @@ public final class XmrConnectionService { // switch to best connection if (bestConnection == null) { - log.warn("Could not get connection to switch to"); + log.warn("No connection to switch to"); return false; } setConnection(bestConnection); diff --git a/core/src/main/java/haveno/core/trade/Trade.java b/core/src/main/java/haveno/core/trade/Trade.java index 811281649c..cfd4930937 100644 --- a/core/src/main/java/haveno/core/trade/Trade.java +++ b/core/src/main/java/haveno/core/trade/Trade.java @@ -627,7 +627,7 @@ public abstract class Trade implements Tradable, Model { // handle connection change on dedicated thread xmrConnectionService.addConnectionListener(connection -> { - ThreadUtils.execute(() -> onConnectionChanged(connection), getConnectionChangedThreadId()); + ThreadUtils.execute(() -> onConnectionChanged(connection), getId()); }); // reset buyer's payment sent state if no ack receive @@ -1501,7 +1501,6 @@ public abstract class Trade implements Tradable, Model { isShutDown = true; List shutDownThreads = new ArrayList<>(); shutDownThreads.add(() -> ThreadUtils.shutDown(getId())); - shutDownThreads.add(() -> ThreadUtils.shutDown(getConnectionChangedThreadId())); ThreadUtils.awaitTasks(shutDownThreads); } @@ -2326,10 +2325,6 @@ public abstract class Trade implements Tradable, Model { return tradeVolumeProperty; } - private String getConnectionChangedThreadId() { - return getId() + ".onConnectionChanged"; - } - private void onConnectionChanged(MoneroRpcConnection connection) { synchronized (walletLock) { @@ -2399,24 +2394,29 @@ public abstract class Trade implements Tradable, Model { } private void syncWallet(boolean pollWallet) { - if (getWallet() == null) throw new RuntimeException("Cannot sync trade wallet because it doesn't exist for " + getClass().getSimpleName() + ", " + getId()); - if (getWallet().getDaemonConnection() == null) throw new RuntimeException("Cannot sync trade wallet because it's not connected to a Monero daemon for " + getClass().getSimpleName() + ", " + getId()); - if (isWalletBehind()) { - log.info("Syncing wallet for {} {}", getClass().getSimpleName(), getShortId()); - long startTime = System.currentTimeMillis(); - syncWalletIfBehind(); - log.info("Done syncing wallet for {} {} in {} ms", getClass().getSimpleName(), getShortId(), System.currentTimeMillis() - startTime); - } - - // apply tor after wallet synced depending on configuration - if (!wasWalletSynced) { - wasWalletSynced = true; - if (xmrWalletService.isProxyApplied(wasWalletSynced)) { - onConnectionChanged(xmrConnectionService.getConnection()); + try { + if (getWallet() == null) throw new RuntimeException("Cannot sync trade wallet because it doesn't exist for " + getClass().getSimpleName() + ", " + getId()); + if (getWallet().getDaemonConnection() == null) throw new RuntimeException("Cannot sync trade wallet because it's not connected to a Monero daemon for " + getClass().getSimpleName() + ", " + getId()); + if (isWalletBehind()) { + log.info("Syncing wallet for {} {}", getClass().getSimpleName(), getShortId()); + long startTime = System.currentTimeMillis(); + syncWalletIfBehind(); + log.info("Done syncing wallet for {} {} in {} ms", getClass().getSimpleName(), getShortId(), System.currentTimeMillis() - startTime); } + + // apply tor after wallet synced depending on configuration + if (!wasWalletSynced) { + wasWalletSynced = true; + if (xmrWalletService.isProxyApplied(wasWalletSynced)) { + onConnectionChanged(xmrConnectionService.getConnection()); + } + } + + if (pollWallet) pollWallet(); + } catch (Exception e) { + ThreadUtils.execute(() -> requestSwitchToNextBestConnection(), getId()); + throw e; } - - if (pollWallet) pollWallet(); } public void updatePollPeriod() { @@ -2593,7 +2593,7 @@ public abstract class Trade implements Tradable, Model { boolean isWalletConnected = isWalletConnectedToDaemon(); if (wallet != null && !isShutDownStarted && isWalletConnected) { log.warn("Error polling trade wallet for {} {}, errorMessage={}. Monerod={}", getClass().getSimpleName(), getShortId(), e.getMessage(), getXmrWalletService().getConnectionService().getConnection()); - requestSwitchToNextBestConnection(); + ThreadUtils.execute(() -> requestSwitchToNextBestConnection(), getId()); // do not block polling thread //e.printStackTrace(); } } diff --git a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java index f838c92beb..2e609d77d4 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -1285,7 +1285,9 @@ public class XmrWalletService { else log.info(appliedMsg); // listen for connection changes - xmrConnectionService.addConnectionListener(connection -> ThreadUtils.execute(() -> onConnectionChanged(connection), THREAD_ID)); + xmrConnectionService.addConnectionListener(connection -> ThreadUtils.execute(() -> { + onConnectionChanged(connection); + }, THREAD_ID)); // initialize main wallet when daemon synced walletInitListener = (obs, oldVal, newVal) -> initMainWalletIfConnected(); @@ -1669,7 +1671,7 @@ public class XmrWalletService { if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return; String oldProxyUri = wallet == null || wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getProxyUri(); String newProxyUri = connection == null ? null : connection.getProxyUri(); - log.info("Setting daemon connection for main wallet: uri={}, proxyUri={}", connection == null ? null : connection.getUri(), newProxyUri); + log.info("Setting daemon connection for main wallet, monerod={}, proxyUri={}", connection == null ? null : connection.getUri(), newProxyUri); // force restart main wallet if connection changed before synced if (!wasWalletSynced) { @@ -1706,7 +1708,7 @@ public class XmrWalletService { updatePollPeriod(); } - log.info("Done setting main wallet monerod=" + (wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getUri())); + log.info("Done setting daemon connection for main wallet, monerod=" + (wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getUri())); } }