From 0105c1436a8d9441aff8297abf26f27a6ad3b239 Mon Sep 17 00:00:00 2001 From: woodser Date: Tue, 6 Aug 2024 05:15:36 -0400 Subject: [PATCH] sync wallet directly within 100 blocks of target height --- core/src/main/java/haveno/core/trade/Trade.java | 9 ++++++++- .../java/haveno/core/xmr/wallet/XmrWalletBase.java | 8 +++++++- .../java/haveno/core/xmr/wallet/XmrWalletService.java | 10 ++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/haveno/core/trade/Trade.java b/core/src/main/java/haveno/core/trade/Trade.java index 40121bd2..3aef966b 100644 --- a/core/src/main/java/haveno/core/trade/Trade.java +++ b/core/src/main/java/haveno/core/trade/Trade.java @@ -2631,7 +2631,14 @@ public abstract class Trade extends XmrWalletBase implements Tradable, Model { private void syncWalletIfBehind() { synchronized (walletLock) { if (isWalletBehind()) { - syncWithProgress(); + + // TODO: local tests have timing failures unless sync called directly + if (xmrConnectionService.getTargetHeight() - walletHeight.get() < XmrWalletBase.DIRECT_SYNC_WITHIN_BLOCKS) { + xmrWalletService.syncWallet(wallet); + } else { + syncWithProgress(); + } + walletHeight.set(wallet.getHeight()); } } } diff --git a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletBase.java b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletBase.java index cca9daa0..820af8bb 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletBase.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletBase.java @@ -26,6 +26,7 @@ public class XmrWalletBase { // constants public static final int SYNC_PROGRESS_TIMEOUT_SECONDS = 60; + public static final int DIRECT_SYNC_WITHIN_BLOCKS = 100; // inherited protected MoneroWallet wallet; @@ -59,12 +60,17 @@ public class XmrWalletBase { } public void syncWithProgress() { + syncWithProgress(false); + } + + public void syncWithProgress(boolean repeatSyncToLatestHeight) { synchronized (walletLock) { // set initial state isSyncingWithProgress = true; syncProgressError = null; updateSyncProgress(walletHeight.get()); + long targetHeightAtStart = xmrConnectionService.getTargetHeight(); // test connection changing on startup before wallet synced if (testReconnectOnStartup) { @@ -106,7 +112,7 @@ public class XmrWalletBase { return; } updateSyncProgress(height); - if (height >= xmrConnectionService.getTargetHeight()) { + if (height >= (repeatSyncToLatestHeight ? xmrConnectionService.getTargetHeight() : targetHeightAtStart)) { setWalletSyncedWithProgress(); syncProgressLatch.countDown(); } 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 2fb9ddcd..aba437f7 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -1358,7 +1358,7 @@ public class XmrWalletService extends XmrWalletBase { long time = System.currentTimeMillis(); MoneroRpcConnection sourceConnection = xmrConnectionService.getConnection(); try { - syncWithProgress(); // blocking + syncWithProgress(true); // repeat sync to latest target height } catch (Exception e) { log.warn("Error syncing wallet with progress on startup: " + e.getMessage()); forceCloseMainWallet(); @@ -1768,7 +1768,13 @@ public class XmrWalletService extends XmrWalletBase { // sync wallet if behind daemon if (walletHeight.get() < xmrConnectionService.getTargetHeight()) { synchronized (walletLock) { // avoid long sync from blocking other operations - syncWithProgress(); + + // TODO: local tests have timing failures unless sync called directly + if (xmrConnectionService.getTargetHeight() - walletHeight.get() < XmrWalletBase.DIRECT_SYNC_WITHIN_BLOCKS) { + syncMainWallet(); + } else { + syncWithProgress(); + } } }