From d300ce8ce7e1e049062fa4f6aac2fe337da2d658 Mon Sep 17 00:00:00 2001 From: woodser Date: Tue, 6 Aug 2024 11:38:22 -0400 Subject: [PATCH] announce connection change on refresh period change --- .../haveno/core/api/XmrConnectionService.java | 28 +++++++++++++++---- .../main/java/haveno/core/trade/Trade.java | 5 +++- .../core/xmr/wallet/XmrWalletService.java | 15 ++++++---- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index d9f2e22356..48538ef48f 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -101,6 +101,7 @@ public final class XmrConnectionService { private Long lastLogPollErrorTimestamp; private Long syncStartHeight = null; private TaskLooper daemonPollLooper; + private long lastRefreshPeriodMs = 0; @Getter private boolean isShutDownStarted; private List listeners = new ArrayList<>(); @@ -353,7 +354,11 @@ public final class XmrConnectionService { } public long getRefreshPeriodMs() { - return connectionList.getRefreshPeriod() > 0 ? connectionList.getRefreshPeriod() : getDefaultRefreshPeriodMs(); + return connectionList.getRefreshPeriod() > 0 ? connectionList.getRefreshPeriod() : getDefaultRefreshPeriodMs(false); + } + + private long getInternalRefreshPeriodMs() { + return connectionList.getRefreshPeriod() > 0 ? connectionList.getRefreshPeriod() : getDefaultRefreshPeriodMs(true); } public void verifyConnection() { @@ -423,12 +428,16 @@ public final class XmrConnectionService { return connection != null && HavenoUtils.isLocalHost(connection.getUri()); } - private long getDefaultRefreshPeriodMs() { + private long getDefaultRefreshPeriodMs(boolean internal) { MoneroRpcConnection connection = getConnection(); if (connection == null) return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS; if (isConnectionLocalHost(connection)) { - if (lastInfo != null && (lastInfo.isBusySyncing() || (lastInfo.getHeightWithoutBootstrap() != null && lastInfo.getHeightWithoutBootstrap() > 0 && lastInfo.getHeightWithoutBootstrap() < lastInfo.getHeight()))) return REFRESH_PERIOD_HTTP_MS; // refresh slower if syncing or bootstrapped - else return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS; // TODO: announce faster refresh after done syncing + if (internal) return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS; + if (lastInfo != null && (lastInfo.getHeightWithoutBootstrap() != null && lastInfo.getHeightWithoutBootstrap() > 0 && lastInfo.getHeightWithoutBootstrap() < lastInfo.getHeight())) { + return REFRESH_PERIOD_HTTP_MS; // refresh slower if syncing or bootstrapped + } else { + return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS; // TODO: announce faster refresh after done syncing + } } else if (isProxyApplied(connection)) { return REFRESH_PERIOD_ONION_MS; } else { @@ -648,7 +657,7 @@ public final class XmrConnectionService { // update polling doPollDaemon(); if (currentConnection != getConnection()) return; // polling can change connection - UserThread.runAfter(() -> updatePolling(), getRefreshPeriodMs() / 1000); + UserThread.runAfter(() -> updatePolling(), getInternalRefreshPeriodMs() / 1000); // notify listeners in parallel log.info("XmrConnectionService.onConnectionChanged() uri={}, connected={}", currentConnection == null ? null : currentConnection.getUri(), currentConnection == null ? "false" : isConnected); @@ -668,7 +677,7 @@ public final class XmrConnectionService { synchronized (lock) { if (daemonPollLooper != null) daemonPollLooper.stop(); daemonPollLooper = new TaskLooper(() -> pollDaemon()); - daemonPollLooper.start(getRefreshPeriodMs()); + daemonPollLooper.start(getInternalRefreshPeriodMs()); } } @@ -725,6 +734,13 @@ public final class XmrConnectionService { // connected to daemon isConnected = true; + // announce connection change if refresh period changes + if (getRefreshPeriodMs() != lastRefreshPeriodMs) { + lastRefreshPeriodMs = getRefreshPeriodMs(); + onConnectionChanged(getConnection()); // causes new poll + return; + } + // update properties on user thread UserThread.execute(() -> { diff --git a/core/src/main/java/haveno/core/trade/Trade.java b/core/src/main/java/haveno/core/trade/Trade.java index 3aef966b18..4b3aed5bd4 100644 --- a/core/src/main/java/haveno/core/trade/Trade.java +++ b/core/src/main/java/haveno/core/trade/Trade.java @@ -2350,7 +2350,10 @@ public abstract class Trade extends XmrWalletBase implements Tradable, Model { // check if ignored if (isShutDownStarted) return; if (getWallet() == null) return; - if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return; + if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) { + updatePollPeriod(); + return; + } // set daemon connection (must restart monero-wallet-rpc if proxy uri changed) String oldProxyUri = wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getProxyUri(); 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 aebc87dc9f..5262095713 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -1598,12 +1598,15 @@ public class XmrWalletService extends XmrWalletBase { // check if ignored if (wallet == null || isShutDownStarted) return; - if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) return; + if (HavenoUtils.connectionConfigsEqual(connection, wallet.getDaemonConnection())) { + updatePollPeriod(); + return; + } + + // update connection String oldProxyUri = wallet == null || wallet.getDaemonConnection() == null ? null : wallet.getDaemonConnection().getProxyUri(); String newProxyUri = connection == null ? null : connection.getProxyUri(); log.info("Setting daemon connection for main wallet, monerod={}, proxyUri={}", connection == null ? null : connection.getUri(), newProxyUri); - - // update connection if (wallet instanceof MoneroWalletRpc) { if (StringUtils.equals(oldProxyUri, newProxyUri)) { wallet.setDaemonConnection(connection); @@ -1722,14 +1725,14 @@ public class XmrWalletService extends XmrWalletBase { public void updatePollPeriod() { if (isShutDownStarted) return; - setPollPeriod(getPollPeriod()); + setPollPeriodMs(getPollPeriodMs()); } - private long getPollPeriod() { + private long getPollPeriodMs() { return xmrConnectionService.getRefreshPeriodMs(); } - private void setPollPeriod(long pollPeriodMs) { + private void setPollPeriodMs(long pollPeriodMs) { synchronized (walletLock) { if (this.isShutDownStarted) return; if (this.pollPeriodMs != null && this.pollPeriodMs == pollPeriodMs) return;