diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index 7260715d7d4..910598f00de 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -105,6 +105,7 @@ public final class XmrConnectionService { @Getter private boolean isShutDownStarted; private List listeners = new ArrayList<>(); + private Set excludedConnections = new HashSet<>(); @Inject public XmrConnectionService(P2PService p2PService, @@ -213,13 +214,20 @@ private void switchToBestConnection() { if (bestConnection != null) setConnection(bestConnection); } - public void switchToNextBestConnection() { + public boolean switchToNextBestConnection() { if (isFixedConnection() || !connectionManager.getAutoSwitch()) { log.info("Skipping switch to next best Monero connection because connection is fixed or auto switch is disabled"); - return; + return false; } - MoneroRpcConnection bestConnection = getBestAvailableConnection(List.of(getConnection())); - if (bestConnection != null) setConnection(bestConnection); + MoneroRpcConnection currentConnection = getConnection(); + excludedConnections.add(currentConnection); + MoneroRpcConnection bestConnection = getBestAvailableConnection(excludedConnections); + UserThread.runAfter(() -> { + excludedConnections.remove(currentConnection); + }, 600); // remove from excluded connections after 10 minutes + if (bestConnection == null) return false; + setConnection(bestConnection); + return true; } public void setConnection(String connectionUri) { @@ -531,7 +539,6 @@ public void onConnectionChanged(MoneroRpcConnection connection) { // register connection listener connectionManager.addListener(this::onConnectionChanged); - isInitialized = true; } 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 d8fce780804..4df0aa7e8f0 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -1438,6 +1438,12 @@ private void syncWithProgress() { // start sync progress timeout resetSyncProgressTimeout(); + // switch connection if disconnected + if (!wallet.isConnectedToDaemon()) { + log.warn("Switching connection before syncing with progress because disconnected"); + if (xmrConnectionService.switchToNextBestConnection()) return; + } + // show sync progress updateSyncProgress(wallet.getHeight()); @@ -1689,7 +1695,7 @@ private void onConnectionChanged(MoneroRpcConnection connection) { if (StringUtils.equals(oldProxyUri, newProxyUri)) { wallet.setDaemonConnection(connection); } else { - log.info("Restarting main wallet because proxy URI has changed, old={}, new={}", oldProxyUri, newProxyUri); + log.info("Restarting main wallet because proxy URI has changed, old={}, new={}", oldProxyUri, newProxyUri); // TODO: set proxy without restarting wallet closeMainWallet(true); maybeInitMainWallet(false); } @@ -1698,7 +1704,13 @@ private void onConnectionChanged(MoneroRpcConnection connection) { wallet.setProxyUri(connection.getProxyUri()); } - // sync wallet on new thread + // switch if wallet disconnected + if (Boolean.TRUE.equals(connection.isConnected() && !wallet.isConnectedToDaemon())) { + log.warn("Switching to next best connection because main wallet is disconnected"); + if (xmrConnectionService.switchToNextBestConnection()) return; // calls back into this method + } + + // update poll period if (connection != null && !isShutDownStarted) { wallet.getDaemonConnection().setPrintStackTrace(PRINT_RPC_STACK_TRACE); updatePollPeriod();