From ea536bb4eeb7232c73d3035e947133801a39bca7 Mon Sep 17 00:00:00 2001 From: woodser <13068859+woodser@users.noreply.github.com> Date: Thu, 5 Jun 2025 07:00:49 -0400 Subject: [PATCH] fix npe on startup routine when monero node is not synced --- .../core/xmr/wallet/XmrWalletService.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) 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 26f300b7c0..65cd28d3d8 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -1394,10 +1394,10 @@ public class XmrWalletService extends XmrWalletBase { maybeInitMainWallet(sync, MAX_SYNC_ATTEMPTS); } - private void maybeInitMainWallet(boolean sync, int numSyncAttempts) { + private void maybeInitMainWallet(boolean sync, int numSyncAttemptsRemaining) { ThreadUtils.execute(() -> { try { - doMaybeInitMainWallet(sync, MAX_SYNC_ATTEMPTS); + doMaybeInitMainWallet(sync, numSyncAttemptsRemaining); } catch (Exception e) { if (isShutDownStarted) return; log.warn("Error initializing main wallet: {}\n", e.getMessage(), e); @@ -1407,7 +1407,7 @@ public class XmrWalletService extends XmrWalletBase { }, THREAD_ID); } - private void doMaybeInitMainWallet(boolean sync, int numSyncAttempts) { + private void doMaybeInitMainWallet(boolean sync, int numSyncAttemptsRemaining) { synchronized (walletLock) { if (isShutDownStarted) return; @@ -1435,7 +1435,7 @@ public class XmrWalletService extends XmrWalletBase { // sync main wallet if applicable // TODO: error handling and re-initialization is jenky, refactor - if (sync && numSyncAttempts > 0) { + if (sync && numSyncAttemptsRemaining > 0) { try { // switch connection if disconnected @@ -1454,13 +1454,14 @@ public class XmrWalletService extends XmrWalletBase { if (wallet != null) log.warn("Error syncing wallet with progress on startup: " + e.getMessage()); forceCloseMainWallet(); requestSwitchToNextBestConnection(sourceConnection); - maybeInitMainWallet(true, numSyncAttempts - 1); // re-initialize wallet and sync again + maybeInitMainWallet(true, numSyncAttemptsRemaining - 1); // re-initialize wallet and sync again return; } log.info("Done syncing main wallet in " + (System.currentTimeMillis() - time) + " ms"); // poll wallet doPollWallet(true); + if (getBalance() == null) throw new RuntimeException("Balance is null after polling main wallet"); if (walletInitListener != null) xmrConnectionService.downloadPercentageProperty().removeListener(walletInitListener); // log wallet balances @@ -1488,9 +1489,9 @@ public class XmrWalletService extends XmrWalletBase { saveWallet(false); } catch (Exception e) { if (isClosingWallet || isShutDownStarted || HavenoUtils.havenoSetup.getWalletInitialized().get()) return; // ignore if wallet closing, shut down started, or app already initialized - log.warn("Error initially syncing main wallet: {}", e.getMessage()); - if (numSyncAttempts <= 1) { - log.warn("Failed to sync main wallet. Opening app without syncing", numSyncAttempts); + log.warn("Error initially syncing main wallet, numSyncAttemptsRemaining={}", numSyncAttemptsRemaining, e); + if (numSyncAttemptsRemaining <= 1) { + log.warn("Failed to sync main wallet. Opening app without syncing."); HavenoUtils.havenoSetup.getWalletInitialized().set(true); saveWallet(false); @@ -1501,7 +1502,7 @@ public class XmrWalletService extends XmrWalletBase { } else { log.warn("Trying again in {} seconds", xmrConnectionService.getRefreshPeriodMs() / 1000); UserThread.runAfter(() -> { - maybeInitMainWallet(true, numSyncAttempts - 1); + maybeInitMainWallet(true, numSyncAttemptsRemaining - 1); }, xmrConnectionService.getRefreshPeriodMs() / 1000); } } @@ -2063,15 +2064,15 @@ public class XmrWalletService extends XmrWalletBase { } } saveWalletWithDelay(); - } - // cache wallet info last - synchronized (walletLock) { - if (wallet != null && !isShutDownStarted) { - try { - cacheWalletInfo(); - } catch (Exception e) { - log.warn("Error caching wallet info: " + e.getMessage() + "\n", e); + // cache wallet info last + synchronized (walletLock) { + if (wallet != null && !isShutDownStarted) { + try { + cacheWalletInfo(); + } catch (Exception e) { + log.warn("Error caching wallet info: " + e.getMessage() + "\n", e); + } } } }