Skip to content

Commit 93369c4

Browse files
authored
prompt to fallback if last synced local node is offline on startup
1 parent a219714 commit 93369c4

File tree

5 files changed

+36
-50
lines changed

5 files changed

+36
-50
lines changed

core/src/main/java/haveno/core/api/XmrConnectionService.java

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import haveno.common.app.DevEnv;
2525
import haveno.common.config.BaseCurrencyNetwork;
2626
import haveno.common.config.Config;
27+
import haveno.core.locale.Res;
2728
import haveno.core.trade.HavenoUtils;
2829
import haveno.core.user.Preferences;
2930
import haveno.core.xmr.model.EncryptedConnectionList;
@@ -43,15 +44,13 @@
4344

4445
import org.apache.commons.lang3.exception.ExceptionUtils;
4546

46-
import javafx.beans.property.BooleanProperty;
4747
import javafx.beans.property.IntegerProperty;
4848
import javafx.beans.property.LongProperty;
4949
import javafx.beans.property.ObjectProperty;
5050
import javafx.beans.property.ReadOnlyDoubleProperty;
5151
import javafx.beans.property.ReadOnlyIntegerProperty;
5252
import javafx.beans.property.ReadOnlyLongProperty;
5353
import javafx.beans.property.ReadOnlyObjectProperty;
54-
import javafx.beans.property.SimpleBooleanProperty;
5554
import javafx.beans.property.SimpleIntegerProperty;
5655
import javafx.beans.property.SimpleLongProperty;
5756
import javafx.beans.property.SimpleObjectProperty;
@@ -91,7 +90,7 @@ public final class XmrConnectionService {
9190
private final LongProperty chainHeight = new SimpleLongProperty(0);
9291
private final DownloadListener downloadListener = new DownloadListener();
9392
@Getter
94-
private final BooleanProperty connectionServiceFallbackHandlerActive = new SimpleBooleanProperty();
93+
private final SimpleStringProperty connectionServiceFallbackHandler = new SimpleStringProperty();
9594
@Getter
9695
private final StringProperty connectionServiceErrorMsg = new SimpleStringProperty();
9796
private final LongProperty numUpdates = new SimpleLongProperty(0);
@@ -261,6 +260,7 @@ public MoneroRpcConnection getBestConnection() {
261260

262261
private MoneroRpcConnection getBestConnection(Collection<MoneroRpcConnection> ignoredConnections) {
263262
accountService.checkAccountOpen();
263+
if (!fallbackApplied && lastUsedLocalSyncingNode() && !xmrLocalNode.isDetected()) return null; // user needs to explicitly allow fallback after syncing local node
264264
Set<MoneroRpcConnection> ignoredConnectionsSet = new HashSet<>(ignoredConnections);
265265
addLocalNodeIfIgnored(ignoredConnectionsSet);
266266
MoneroRpcConnection bestConnection = connectionManager.getBestAvailableConnection(ignoredConnectionsSet.toArray(new MoneroRpcConnection[0])); // checks connections
@@ -604,9 +604,6 @@ public void onConnectionChanged(MoneroRpcConnection connection) {
604604
if (coreContext.isApiUser()) connectionManager.setAutoSwitch(connectionList.getAutoSwitch());
605605
else connectionManager.setAutoSwitch(true); // auto switch is always enabled on desktop ui
606606

607-
// start local node if applicable
608-
maybeStartLocalNode();
609-
610607
// update connection
611608
if (connectionManager.getConnection() == null || connectionManager.getAutoSwitch()) {
612609
MoneroRpcConnection bestConnection = getBestConnection();
@@ -619,9 +616,6 @@ public void onConnectionChanged(MoneroRpcConnection connection) {
619616
MoneroRpcConnection connection = new MoneroRpcConnection(config.xmrNode, config.xmrNodeUsername, config.xmrNodePassword).setPriority(1);
620617
if (isProxyApplied(connection)) connection.setProxyUri(getProxyUri());
621618
connectionManager.setConnection(connection);
622-
623-
// start local node if applicable
624-
maybeStartLocalNode();
625619
}
626620

627621
// register connection listener
@@ -634,20 +628,8 @@ public void onConnectionChanged(MoneroRpcConnection connection) {
634628
onConnectionChanged(connectionManager.getConnection());
635629
}
636630

637-
private void maybeStartLocalNode() {
638-
639-
// skip if seed node
640-
if (HavenoUtils.isSeedNode()) return;
641-
642-
// start local node if offline and used as last connection
643-
if (connectionManager.getConnection() != null && xmrLocalNode.equalsUri(connectionManager.getConnection().getUri()) && !xmrLocalNode.isDetected() && !xmrLocalNode.shouldBeIgnored()) {
644-
try {
645-
log.info("Starting local node");
646-
xmrLocalNode.start();
647-
} catch (Exception e) {
648-
log.error("Unable to start local monero node, error={}\n", e.getMessage(), e);
649-
}
650-
}
631+
private boolean lastUsedLocalSyncingNode() {
632+
return connectionManager.getConnection() != null && xmrLocalNode.equalsUri(connectionManager.getConnection().getUri()) && !xmrLocalNode.isDetected() && !xmrLocalNode.shouldBeIgnored();
651633
}
652634

653635
private void onConnectionChanged(MoneroRpcConnection currentConnection) {
@@ -733,12 +715,17 @@ private void doPollDaemon() {
733715
if (isShutDownStarted) return;
734716

735717
// invoke fallback handling on startup error
736-
boolean canFallback = isFixedConnection() || isCustomConnections();
718+
boolean canFallback = isFixedConnection() || isCustomConnections() || lastUsedLocalSyncingNode();
737719
if (lastInfo == null && canFallback) {
738-
if (!connectionServiceFallbackHandlerActive.get() && (lastFallbackInvocation == null || System.currentTimeMillis() - lastFallbackInvocation > FALLBACK_INVOCATION_PERIOD_MS)) {
739-
log.warn("Failed to fetch daemon info from custom connection on startup: " + e.getMessage());
720+
if (connectionServiceFallbackHandler.get() == null || connectionServiceFallbackHandler.equals("") && (lastFallbackInvocation == null || System.currentTimeMillis() - lastFallbackInvocation > FALLBACK_INVOCATION_PERIOD_MS)) {
740721
lastFallbackInvocation = System.currentTimeMillis();
741-
connectionServiceFallbackHandlerActive.set(true);
722+
if (lastUsedLocalSyncingNode()) {
723+
log.warn("Failed to fetch daemon info from local connection on startup: " + e.getMessage());
724+
connectionServiceFallbackHandler.set(Res.get("connectionFallback.localNode"));
725+
} else {
726+
log.warn("Failed to fetch daemon info from custom connection on startup: " + e.getMessage());
727+
connectionServiceFallbackHandler.set(Res.get("connectionFallback.customNode"));
728+
}
742729
}
743730
return;
744731
}

core/src/main/java/haveno/core/app/HavenoSetup.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public class HavenoSetup {
158158
rejectedTxErrorMessageHandler;
159159
@Setter
160160
@Nullable
161-
private Consumer<Boolean> displayMoneroConnectionFallbackHandler;
161+
private Consumer<String> displayMoneroConnectionFallbackHandler;
162162
@Setter
163163
@Nullable
164164
private Consumer<Boolean> displayTorNetworkSettingsHandler;
@@ -430,7 +430,7 @@ private void startP2pNetworkAndWallet(Runnable nextStep) {
430430
getXmrWalletSyncProgress().addListener((observable, oldValue, newValue) -> resetStartupTimeout());
431431

432432
// listen for fallback handling
433-
getConnectionServiceFallbackHandlerActive().addListener((observable, oldValue, newValue) -> {
433+
getConnectionServiceFallbackHandler().addListener((observable, oldValue, newValue) -> {
434434
if (displayMoneroConnectionFallbackHandler == null) return;
435435
displayMoneroConnectionFallbackHandler.accept(newValue);
436436
});
@@ -734,8 +734,8 @@ public StringProperty getConnectionServiceErrorMsg() {
734734
return xmrConnectionService.getConnectionServiceErrorMsg();
735735
}
736736

737-
public BooleanProperty getConnectionServiceFallbackHandlerActive() {
738-
return xmrConnectionService.getConnectionServiceFallbackHandlerActive();
737+
public StringProperty getConnectionServiceFallbackHandler() {
738+
return xmrConnectionService.getConnectionServiceFallbackHandler();
739739
}
740740

741741
public StringProperty getTopErrorMsg() {

core/src/main/resources/i18n/displayStrings.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,8 @@ closedTradesSummaryWindow.totalTradeFeeInXmr.value={0} ({1} of total trade amoun
20582058
walletPasswordWindow.headline=Enter password to unlock
20592059

20602060
connectionFallback.headline=Connection error
2061-
connectionFallback.msg=Error connecting to your custom Monero node(s).\n\nDo you want to try the next best available Monero node?
2061+
connectionFallback.customNode=Error connecting to your custom Monero node(s).\n\nDo you want to use the next best available Monero node?
2062+
connectionFallback.localNode=Error connecting to your last used local node.\n\nDo you want to use the next best available Monero node?
20622063

20632064
torNetworkSettingWindow.header=Tor networks settings
20642065
torNetworkSettingWindow.noBridges=Don't use bridges

core/src/main/resources/i18n/displayStrings_cs.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,7 @@ closedTradesSummaryWindow.totalTradeFeeInXmr.value={0} ({1} z celkového objemu
20572057
walletPasswordWindow.headline=Pro odemknutí zadejte heslo
20582058

20592059
connectionFallback.headline=Chyba připojení
2060-
connectionFallback.msg=Chyba při připojování k vlastním uzlům Monero.\n\nChcete vyzkoušet další nejlepší dostupný uzel Monero?
2060+
connectionFallback.customNode=Chyba při připojování k vlastním uzlům Monero.\n\nChcete vyzkoušet další nejlepší dostupný uzel Monero?
20612061

20622062
torNetworkSettingWindow.header=Nastavení sítě Tor
20632063
torNetworkSettingWindow.noBridges=Nepoužívat most (bridge)

desktop/src/main/java/haveno/desktop/main/MainViewModel.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -335,25 +335,23 @@ private void setupHandlers() {
335335
tacWindow.onAction(acceptedHandler::run).show();
336336
}, 1));
337337

338-
havenoSetup.setDisplayMoneroConnectionFallbackHandler(show -> {
339-
if (moneroConnectionFallbackPopup == null) {
338+
havenoSetup.setDisplayMoneroConnectionFallbackHandler(fallbackMsg -> {
339+
if (fallbackMsg != null && !fallbackMsg.isEmpty()) {
340340
moneroConnectionFallbackPopup = new Popup()
341-
.headLine(Res.get("connectionFallback.headline"))
342-
.warning(Res.get("connectionFallback.msg"))
343-
.closeButtonText(Res.get("shared.no"))
344-
.actionButtonText(Res.get("shared.yes"))
345-
.onAction(() -> {
346-
havenoSetup.getConnectionServiceFallbackHandlerActive().set(false);
347-
new Thread(() -> HavenoUtils.xmrConnectionService.fallbackToBestConnection()).start();
348-
})
349-
.onClose(() -> {
350-
log.warn("User has declined to fallback to the next best available Monero node.");
351-
havenoSetup.getConnectionServiceFallbackHandlerActive().set(false);
352-
});
353-
}
354-
if (show) {
341+
.headLine(Res.get("connectionFallback.headline"))
342+
.warning(fallbackMsg)
343+
.closeButtonText(Res.get("shared.no"))
344+
.actionButtonText(Res.get("shared.yes"))
345+
.onAction(() -> {
346+
havenoSetup.getConnectionServiceFallbackHandler().set("");
347+
new Thread(() -> HavenoUtils.xmrConnectionService.fallbackToBestConnection()).start();
348+
})
349+
.onClose(() -> {
350+
log.warn("User has declined to fallback to the next best available Monero node.");
351+
havenoSetup.getConnectionServiceFallbackHandler().set("");
352+
});
355353
moneroConnectionFallbackPopup.show();
356-
} else if (moneroConnectionFallbackPopup.isDisplayed()) {
354+
} else if (moneroConnectionFallbackPopup != null && moneroConnectionFallbackPopup.isDisplayed()) {
357355
moneroConnectionFallbackPopup.hide();
358356
}
359357
});

0 commit comments

Comments
 (0)