Skip to content

Commit

Permalink
switch monerod if disconnected on change, ignore previous for 10 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Jul 8, 2024
1 parent 55b968d commit 4056df7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
17 changes: 12 additions & 5 deletions core/src/main/java/haveno/core/api/XmrConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public final class XmrConnectionService {
@Getter
private boolean isShutDownStarted;
private List<MoneroConnectionManagerListener> listeners = new ArrayList<>();
private Set<MoneroRpcConnection> excludedConnections = new HashSet<>();

@Inject
public XmrConnectionService(P2PService p2PService,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -531,7 +539,6 @@ public void onConnectionChanged(MoneroRpcConnection connection) {

// register connection listener
connectionManager.addListener(this::onConnectionChanged);

isInitialized = true;
}

Expand Down
16 changes: 14 additions & 2 deletions core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
Expand Down

0 comments on commit 4056df7

Please sign in to comment.