Skip to content

Commit

Permalink
fallback from custom node to provided nodes on startup error #923
Browse files Browse the repository at this point in the history
Co-authored-by: nsec1 <[email protected]>
  • Loading branch information
woodser and nsec1 committed Jun 23, 2024
1 parent 4494af8 commit 26a5ffc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
22 changes: 16 additions & 6 deletions core/src/main/java/haveno/core/api/XmrConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ private void onConnectionChanged(MoneroRpcConnection currentConnection) {

// update polling
doPollDaemon();
if (currentConnection != getConnection()) return; // polling can change connection
UserThread.runAfter(() -> updatePolling(), getRefreshPeriodMs() / 1000);

// notify listeners in parallel
Expand Down Expand Up @@ -607,13 +608,22 @@ private void doPollDaemon() {
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
Expand Down
22 changes: 11 additions & 11 deletions core/src/main/java/haveno/core/user/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ private void setupPreferences() {
cssThemeProperty.set(prefPayload.getCssTheme());


// if no valid Bitcoin block explorer is set, select the 1st valid Bitcoin block explorer
ArrayList<BlockChainExplorer> btcExplorers = getBlockChainExplorers();
// if no valid Monero block explorer is set, select the 1st valid Monero block explorer
ArrayList<BlockChainExplorer> 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());
Expand All @@ -278,8 +278,8 @@ private void setupPreferences() {

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());
Expand Down Expand Up @@ -567,8 +567,8 @@ public void setSortMarketCurrenciesNumerically(boolean sortMarketCurrenciesNumer
requestPersistence();
}

public void setMoneroNodes(String bitcoinNodes) {
prefPayload.setMoneroNodes(bitcoinNodes);
public void setMoneroNodes(String moneroNodes) {
prefPayload.setMoneroNodes(moneroNodes);
requestPersistence();
}

Expand Down Expand Up @@ -658,8 +658,8 @@ public void setUseTorForXmrOrdinal(int useTorForXmrOrdinal) {
requestPersistence();
}

public void setMoneroNodesOptionOrdinal(int bitcoinNodesOptionOrdinal) {
prefPayload.setMoneroNodesOptionOrdinal(bitcoinNodesOptionOrdinal);
public void setMoneroNodesOptionOrdinal(int moneroNodesOptionOrdinal) {
prefPayload.setMoneroNodesOptionOrdinal(moneroNodesOptionOrdinal);
requestPersistence();
}

Expand Down Expand Up @@ -892,7 +892,7 @@ private interface ExcludesDelegateMethods {

void setSortMarketCurrenciesNumerically(boolean sortMarketCurrenciesNumerically);

void setMoneroNodes(String bitcoinNodes);
void setMoneroNodes(String moneroNodes);

void setUseCustomWithdrawalTxFee(boolean useCustomWithdrawalTxFee);

Expand Down Expand Up @@ -926,7 +926,7 @@ private interface ExcludesDelegateMethods {

void setUseTorForXmrOrdinal(int useTorForXmrOrdinal);

void setMoneroNodesOptionOrdinal(int bitcoinNodesOption);
void setMoneroNodesOptionOrdinal(int moneroNodesOption);

void setReferralId(String referralId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,7 @@ private MoneroWalletFull openWalletFull(MoneroWalletConfig config, boolean apply
// 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());
Expand Down Expand Up @@ -1604,7 +1605,7 @@ private MoneroWalletRpc openWalletRpc(MoneroWalletConfig config, Integer port, b
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);
Expand Down

0 comments on commit 26a5ffc

Please sign in to comment.