From 58787a1d3122d24a7dd6b4bab15aa00c94f02db9 Mon Sep 17 00:00:00 2001 From: woodser Date: Sat, 12 Oct 2024 06:18:16 -0400 Subject: [PATCH] reclassify public nodes and use if no provided nodes available (#1315) --- .../haveno/core/api/XmrConnectionService.java | 11 ++++++++--- .../main/java/haveno/core/user/Preferences.java | 12 +++++++++++- .../java/haveno/core/xmr/nodes/XmrNodes.java | 17 ++++++++--------- .../java/haveno/core/user/PreferencesTest.java | 2 +- .../settings/network/NetworkSettingsView.java | 14 ++++++++++++-- .../haveno/desktop/maker/PreferenceMakers.java | 3 ++- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index db970c04..7d48a65a 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -718,10 +718,15 @@ public final class XmrConnectionService { // skip handling if shutting down if (isShutDownStarted) return; - // fallback to provided nodes if custom connection fails on startup + // fallback to provided or public 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()); + if (xmrNodes.getProvidedXmrNodes().isEmpty()) { + log.warn("Failed to fetch daemon info from custom node on startup, falling back to public nodes: " + e.getMessage()); + preferences.setMoneroNodesOptionOrdinal(XmrNodes.MoneroNodesOption.PUBLIC.ordinal()); + } else { + 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; } diff --git a/core/src/main/java/haveno/core/user/Preferences.java b/core/src/main/java/haveno/core/user/Preferences.java index 3f8a80f9..209dc7fb 100644 --- a/core/src/main/java/haveno/core/user/Preferences.java +++ b/core/src/main/java/haveno/core/user/Preferences.java @@ -38,6 +38,7 @@ import haveno.core.payment.PaymentAccountUtil; import haveno.core.xmr.XmrNodeSettings; import haveno.core.xmr.nodes.XmrNodes; import haveno.core.xmr.nodes.XmrNodes.MoneroNodesOption; +import haveno.core.xmr.nodes.XmrNodesSetupPreferences; import haveno.core.xmr.wallet.Restrictions; import haveno.network.p2p.network.BridgeAddressProvider; import java.util.ArrayList; @@ -130,6 +131,7 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid private final PersistenceManager persistenceManager; private final Config config; private final String xmrNodesFromOptions; + private final XmrNodes xmrNodes; @Getter private final BooleanProperty useStandbyModeProperty = new SimpleBooleanProperty(prefPayload.isUseStandbyMode()); @Getter @@ -142,11 +144,13 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid @Inject public Preferences(PersistenceManager persistenceManager, Config config, - @Named(Config.XMR_NODES) String xmrNodesFromOptions) { + @Named(Config.XMR_NODES) String xmrNodesFromOptions, + XmrNodes xmrNodes) { this.persistenceManager = persistenceManager; this.config = config; this.xmrNodesFromOptions = xmrNodesFromOptions; + this.xmrNodes = xmrNodes; useAnimationsProperty.addListener((ov) -> { prefPayload.setUseAnimations(useAnimationsProperty.get()); @@ -284,6 +288,12 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid if (config.useTorForXmrOptionSetExplicitly) setUseTorForXmr(config.useTorForXmr); + // switch to public nodes if no provided nodes available + if (getMoneroNodesOptionOrdinal() == XmrNodes.MoneroNodesOption.PROVIDED.ordinal() && xmrNodes.selectPreferredNodes(new XmrNodesSetupPreferences(this)).isEmpty()) { + log.warn("No provided nodes available, switching to public nodes"); + setMoneroNodesOptionOrdinal(XmrNodes.MoneroNodesOption.PUBLIC.ordinal()); + } + if (xmrNodesFromOptions != null && !xmrNodesFromOptions.isEmpty()) { if (getMoneroNodes() != null && !getMoneroNodes().equals(xmrNodesFromOptions)) { log.warn("The Monero node(s) from the program argument and the one(s) persisted in the UI are different. " + diff --git a/core/src/main/java/haveno/core/xmr/nodes/XmrNodes.java b/core/src/main/java/haveno/core/xmr/nodes/XmrNodes.java index 643a5fae..97217b0f 100644 --- a/core/src/main/java/haveno/core/xmr/nodes/XmrNodes.java +++ b/core/src/main/java/haveno/core/xmr/nodes/XmrNodes.java @@ -83,16 +83,15 @@ public class XmrNodes { ); case XMR_MAINNET: return Arrays.asList( - new XmrNode(MoneroNodesOption.PROVIDED, null, null, "127.0.0.1", 18081, 1, "@local"), - new XmrNode(MoneroNodesOption.PROVIDED, null, null, "xmr-node.cakewallet.com", 18081, 2, "@cakewallet"), - new XmrNode(MoneroNodesOption.PROVIDED, null, null, "node.community.rino.io", 18081, 2, "@RINOwallet"), - new XmrNode(MoneroNodesOption.PROVIDED, null, null, "nodes.hashvault.pro", 18080, 2, "@HashVault"), - new XmrNode(MoneroNodesOption.PROVIDED, null, null, "p2pmd.xmrvsbeast.com", 18080, 2, "@xmrvsbeast"), - new XmrNode(MoneroNodesOption.PROVIDED, null, null, "node.monerodevs.org", 18089, 2, "@monerodevs.org"), - new XmrNode(MoneroNodesOption.PROVIDED, null, null, "nodex.monerujo.io", 18081, 2, "@monerujo.io"), + new XmrNode(MoneroNodesOption.PUBLIC, null, null, "127.0.0.1", 18081, 1, "@local"), + new XmrNode(MoneroNodesOption.PUBLIC, null, null, "xmr-node.cakewallet.com", 18081, 2, "@cakewallet"), + new XmrNode(MoneroNodesOption.PUBLIC, null, null, "node.community.rino.io", 18081, 2, "@RINOwallet"), + new XmrNode(MoneroNodesOption.PUBLIC, null, null, "nodes.hashvault.pro", 18080, 2, "@HashVault"), + new XmrNode(MoneroNodesOption.PUBLIC, null, null, "p2pmd.xmrvsbeast.com", 18080, 2, "@xmrvsbeast"), + new XmrNode(MoneroNodesOption.PUBLIC, null, null, "node.monerodevs.org", 18089, 2, "@monerodevs.org"), + new XmrNode(MoneroNodesOption.PUBLIC, null, null, "nodex.monerujo.io", 18081, 2, "@monerujo.io"), new XmrNode(MoneroNodesOption.PUBLIC, null, null, "rucknium.me", 18081, 2, "@Rucknium"), - new XmrNode(MoneroNodesOption.PUBLIC, null, null, "node.sethforprivacy.com", 18089, 2, "@sethforprivacy"), - new XmrNode(MoneroNodesOption.PUBLIC, null, null, "node3.monerodevs.org", 18089, 2, "@monerodevs.org") + new XmrNode(MoneroNodesOption.PUBLIC, null, null, "node.sethforprivacy.com", 18089, 2, "@sethforprivacy") ); default: throw new IllegalStateException("Unexpected base currency network: " + Config.baseCurrencyNetwork()); diff --git a/core/src/test/java/haveno/core/user/PreferencesTest.java b/core/src/test/java/haveno/core/user/PreferencesTest.java index d4fa98fb..365d5473 100644 --- a/core/src/test/java/haveno/core/user/PreferencesTest.java +++ b/core/src/test/java/haveno/core/user/PreferencesTest.java @@ -58,7 +58,7 @@ public class PreferencesTest { Config config = new Config(); XmrLocalNode xmrLocalNode = new XmrLocalNode(config, preferences); preferences = new Preferences( - persistenceManager, config, null); + persistenceManager, config, null, null); } @Test diff --git a/desktop/src/main/java/haveno/desktop/main/settings/network/NetworkSettingsView.java b/desktop/src/main/java/haveno/desktop/main/settings/network/NetworkSettingsView.java index b3409371..931a6e35 100644 --- a/desktop/src/main/java/haveno/desktop/main/settings/network/NetworkSettingsView.java +++ b/desktop/src/main/java/haveno/desktop/main/settings/network/NetworkSettingsView.java @@ -282,6 +282,12 @@ public class NetworkSettingsView extends ActivatableView { }; filterPropertyListener = (observable, oldValue, newValue) -> applyPreventPublicXmrNetwork(); + // disable radio buttons if no nodes available + if (xmrNodes.getProvidedXmrNodes().isEmpty()) { + useProvidedNodesRadio.setDisable(true); + } + usePublicNodesRadio.setDisable(isPublicNodesDisabled()); + //TODO sorting needs other NetworkStatisticListItem as columns type /* creationDateColumn.setComparator((o1, o2) -> o1.statistic.getCreationDate().compareTo(o2.statistic.getCreationDate())); @@ -433,7 +439,7 @@ public class NetworkSettingsView extends ActivatableView { } private void onMoneroPeersToggleSelected(boolean calledFromUser) { - usePublicNodesRadio.setDisable(isPreventPublicXmrNetwork()); + usePublicNodesRadio.setDisable(isPublicNodesDisabled()); XmrNodes.MoneroNodesOption currentMoneroNodesOption = XmrNodes.MoneroNodesOption.values()[preferences.getMoneroNodesOptionOrdinal()]; @@ -493,7 +499,7 @@ public class NetworkSettingsView extends ActivatableView { private void applyPreventPublicXmrNetwork() { final boolean preventPublicXmrNetwork = isPreventPublicXmrNetwork(); - usePublicNodesRadio.setDisable(xmrLocalNode.shouldBeUsed() || preventPublicXmrNetwork); + usePublicNodesRadio.setDisable(isPublicNodesDisabled()); if (preventPublicXmrNetwork && selectedMoneroNodesOption == XmrNodes.MoneroNodesOption.PUBLIC) { selectedMoneroNodesOption = XmrNodes.MoneroNodesOption.PROVIDED; preferences.setMoneroNodesOptionOrdinal(selectedMoneroNodesOption.ordinal()); @@ -502,6 +508,10 @@ public class NetworkSettingsView extends ActivatableView { } } + private boolean isPublicNodesDisabled() { + return xmrNodes.getPublicXmrNodes().isEmpty() || isPreventPublicXmrNetwork(); + } + private void updateP2PTable() { if (connectionService.isShutDownStarted()) return; // ignore if shutting down p2pPeersTableView.getItems().forEach(P2pNetworkListItem::cleanup); diff --git a/desktop/src/test/java/haveno/desktop/maker/PreferenceMakers.java b/desktop/src/test/java/haveno/desktop/maker/PreferenceMakers.java index 01cbb16c..bdc89626 100644 --- a/desktop/src/test/java/haveno/desktop/maker/PreferenceMakers.java +++ b/desktop/src/test/java/haveno/desktop/maker/PreferenceMakers.java @@ -39,7 +39,8 @@ public class PreferenceMakers { public static final Instantiator Preferences = lookup -> new Preferences( lookup.valueOf(storage, new SameValueDonor(null)), lookup.valueOf(config, new SameValueDonor(null)), - lookup.valueOf(useTorFlagFromOptions, new SameValueDonor(null)) + lookup.valueOf(useTorFlagFromOptions, new SameValueDonor(null)), + null ); public static final Preferences empty = make(a(Preferences));