From 26a5ffcb31cc2c507da12fdbd9c992f63309401a Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 21 Jun 2024 10:30:27 -0400 Subject: [PATCH] fallback from custom node to provided nodes on startup error #923 Co-authored-by: nsec1 <167650977+nsec1@users.noreply.github.com> --- .../haveno/core/api/XmrConnectionService.java | 22 ++++++++++++++----- .../java/haveno/core/user/Preferences.java | 22 +++++++++---------- .../core/xmr/wallet/XmrWalletService.java | 3 ++- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index 40a33f76..4830bb9d 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -557,6 +557,7 @@ public final class XmrConnectionService { // update polling doPollDaemon(); + if (currentConnection != getConnection()) return; // polling can change connection UserThread.runAfter(() -> updatePolling(), getRefreshPeriodMs() / 1000); // notify listeners in parallel @@ -607,13 +608,22 @@ public final class XmrConnectionService { try { lastInfo = daemon.getInfo(); } catch (Exception e) { - try { - log.warn("Failed to fetch daemon info, trying to switch to best connection: " + e.getMessage()); - switchToBestConnection(); - lastInfo = daemon.getInfo(); - } catch (Exception e2) { - throw e2; // caught internally + + // skip handling if shutting down + if (isShutDownStarted) return; + + // fallback to provided nodes if custom connection fails on startup + if (lastInfo == null && "".equals(config.xmrNode) && preferences.getMoneroNodesOption() == XmrNodes.MoneroNodesOption.CUSTOM) { + log.warn("Failed to fetch daemon info from custom node on startup, falling back to provided nodes: " + e.getMessage()); + preferences.setMoneroNodesOptionOrdinal(XmrNodes.MoneroNodesOption.PROVIDED.ordinal()); + initializeConnections(); + return; } + + // switch to best connection + log.warn("Failed to fetch daemon info, trying to switch to best connection: " + e.getMessage()); + switchToBestConnection(); + lastInfo = daemon.getInfo(); // caught internally if still fails } // connected to daemon diff --git a/core/src/main/java/haveno/core/user/Preferences.java b/core/src/main/java/haveno/core/user/Preferences.java index 97e7fc67..311bd310 100644 --- a/core/src/main/java/haveno/core/user/Preferences.java +++ b/core/src/main/java/haveno/core/user/Preferences.java @@ -262,11 +262,11 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid cssThemeProperty.set(prefPayload.getCssTheme()); - // if no valid Bitcoin block explorer is set, select the 1st valid Bitcoin block explorer - ArrayList btcExplorers = getBlockChainExplorers(); + // if no valid Monero block explorer is set, select the 1st valid Monero block explorer + ArrayList xmrExplorers = getBlockChainExplorers(); if (getBlockChainExplorer() == null || getBlockChainExplorer().name.length() == 0) { - setBlockChainExplorer(btcExplorers.get(0)); + setBlockChainExplorer(xmrExplorers.get(0)); } tradeCurrenciesAsObservable.addAll(prefPayload.getTraditionalCurrencies()); tradeCurrenciesAsObservable.addAll(prefPayload.getCryptoCurrencies()); @@ -278,8 +278,8 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid if (xmrNodesFromOptions != null && !xmrNodesFromOptions.isEmpty()) { if (getMoneroNodes() != null && !getMoneroNodes().equals(xmrNodesFromOptions)) { - log.warn("The Bitcoin node(s) from the program argument and the one(s) persisted in the UI are different. " + - "The Bitcoin node(s) {} from the program argument will be used.", xmrNodesFromOptions); + log.warn("The Monero node(s) from the program argument and the one(s) persisted in the UI are different. " + + "The Monero node(s) {} from the program argument will be used.", xmrNodesFromOptions); } setMoneroNodes(xmrNodesFromOptions); setMoneroNodesOptionOrdinal(XmrNodes.MoneroNodesOption.CUSTOM.ordinal()); @@ -567,8 +567,8 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid requestPersistence(); } - public void setMoneroNodes(String bitcoinNodes) { - prefPayload.setMoneroNodes(bitcoinNodes); + public void setMoneroNodes(String moneroNodes) { + prefPayload.setMoneroNodes(moneroNodes); requestPersistence(); } @@ -658,8 +658,8 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid requestPersistence(); } - public void setMoneroNodesOptionOrdinal(int bitcoinNodesOptionOrdinal) { - prefPayload.setMoneroNodesOptionOrdinal(bitcoinNodesOptionOrdinal); + public void setMoneroNodesOptionOrdinal(int moneroNodesOptionOrdinal) { + prefPayload.setMoneroNodesOptionOrdinal(moneroNodesOptionOrdinal); requestPersistence(); } @@ -892,7 +892,7 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid void setSortMarketCurrenciesNumerically(boolean sortMarketCurrenciesNumerically); - void setMoneroNodes(String bitcoinNodes); + void setMoneroNodes(String moneroNodes); void setUseCustomWithdrawalTxFee(boolean useCustomWithdrawalTxFee); @@ -926,7 +926,7 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid void setUseTorForXmrOrdinal(int useTorForXmrOrdinal); - void setMoneroNodesOptionOrdinal(int bitcoinNodesOption); + void setMoneroNodesOptionOrdinal(int moneroNodesOption); void setReferralId(String referralId); 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 d4704781..21c1d6aa 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -1545,6 +1545,7 @@ public class XmrWalletService { // open wallet config.setNetworkType(getMoneroNetworkType()); config.setServer(connection); + log.info("Opening full wallet " + config.getPath() + " with monerod=" + connection.getUri()); walletFull = MoneroWalletFull.openWallet(config); if (walletFull.getDaemonConnection() != null) walletFull.getDaemonConnection().setPrintStackTrace(PRINT_RPC_STACK_TRACE); log.info("Done opening full wallet " + config.getPath()); @@ -1604,7 +1605,7 @@ public class XmrWalletService { if (!applyProxyUri) connection.setProxyUri(null); // open wallet - log.info("Opening RPC wallet " + config.getPath() + " connected to daemon " + connection.getUri()); + log.info("Opening RPC wallet " + config.getPath() + " with monerod=" + connection.getUri()); config.setServer(connection); walletRpc.openWallet(config); if (walletRpc.getDaemonConnection() != null) walletRpc.getDaemonConnection().setPrintStackTrace(PRINT_RPC_STACK_TRACE);