fix npe on startup routine when monero node is not synced

This commit is contained in:
woodser 2025-06-05 07:00:49 -04:00 committed by woodser
parent a4345ae709
commit ea536bb4ee

View file

@ -1394,10 +1394,10 @@ public class XmrWalletService extends XmrWalletBase {
maybeInitMainWallet(sync, MAX_SYNC_ATTEMPTS); maybeInitMainWallet(sync, MAX_SYNC_ATTEMPTS);
} }
private void maybeInitMainWallet(boolean sync, int numSyncAttempts) { private void maybeInitMainWallet(boolean sync, int numSyncAttemptsRemaining) {
ThreadUtils.execute(() -> { ThreadUtils.execute(() -> {
try { try {
doMaybeInitMainWallet(sync, MAX_SYNC_ATTEMPTS); doMaybeInitMainWallet(sync, numSyncAttemptsRemaining);
} catch (Exception e) { } catch (Exception e) {
if (isShutDownStarted) return; if (isShutDownStarted) return;
log.warn("Error initializing main wallet: {}\n", e.getMessage(), e); log.warn("Error initializing main wallet: {}\n", e.getMessage(), e);
@ -1407,7 +1407,7 @@ public class XmrWalletService extends XmrWalletBase {
}, THREAD_ID); }, THREAD_ID);
} }
private void doMaybeInitMainWallet(boolean sync, int numSyncAttempts) { private void doMaybeInitMainWallet(boolean sync, int numSyncAttemptsRemaining) {
synchronized (walletLock) { synchronized (walletLock) {
if (isShutDownStarted) return; if (isShutDownStarted) return;
@ -1435,7 +1435,7 @@ public class XmrWalletService extends XmrWalletBase {
// sync main wallet if applicable // sync main wallet if applicable
// TODO: error handling and re-initialization is jenky, refactor // TODO: error handling and re-initialization is jenky, refactor
if (sync && numSyncAttempts > 0) { if (sync && numSyncAttemptsRemaining > 0) {
try { try {
// switch connection if disconnected // 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()); if (wallet != null) log.warn("Error syncing wallet with progress on startup: " + e.getMessage());
forceCloseMainWallet(); forceCloseMainWallet();
requestSwitchToNextBestConnection(sourceConnection); requestSwitchToNextBestConnection(sourceConnection);
maybeInitMainWallet(true, numSyncAttempts - 1); // re-initialize wallet and sync again maybeInitMainWallet(true, numSyncAttemptsRemaining - 1); // re-initialize wallet and sync again
return; return;
} }
log.info("Done syncing main wallet in " + (System.currentTimeMillis() - time) + " ms"); log.info("Done syncing main wallet in " + (System.currentTimeMillis() - time) + " ms");
// poll wallet // poll wallet
doPollWallet(true); doPollWallet(true);
if (getBalance() == null) throw new RuntimeException("Balance is null after polling main wallet");
if (walletInitListener != null) xmrConnectionService.downloadPercentageProperty().removeListener(walletInitListener); if (walletInitListener != null) xmrConnectionService.downloadPercentageProperty().removeListener(walletInitListener);
// log wallet balances // log wallet balances
@ -1488,9 +1489,9 @@ public class XmrWalletService extends XmrWalletBase {
saveWallet(false); saveWallet(false);
} catch (Exception e) { } catch (Exception e) {
if (isClosingWallet || isShutDownStarted || HavenoUtils.havenoSetup.getWalletInitialized().get()) return; // ignore if wallet closing, shut down started, or app already initialized 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()); log.warn("Error initially syncing main wallet, numSyncAttemptsRemaining={}", numSyncAttemptsRemaining, e);
if (numSyncAttempts <= 1) { if (numSyncAttemptsRemaining <= 1) {
log.warn("Failed to sync main wallet. Opening app without syncing", numSyncAttempts); log.warn("Failed to sync main wallet. Opening app without syncing.");
HavenoUtils.havenoSetup.getWalletInitialized().set(true); HavenoUtils.havenoSetup.getWalletInitialized().set(true);
saveWallet(false); saveWallet(false);
@ -1501,7 +1502,7 @@ public class XmrWalletService extends XmrWalletBase {
} else { } else {
log.warn("Trying again in {} seconds", xmrConnectionService.getRefreshPeriodMs() / 1000); log.warn("Trying again in {} seconds", xmrConnectionService.getRefreshPeriodMs() / 1000);
UserThread.runAfter(() -> { UserThread.runAfter(() -> {
maybeInitMainWallet(true, numSyncAttempts - 1); maybeInitMainWallet(true, numSyncAttemptsRemaining - 1);
}, xmrConnectionService.getRefreshPeriodMs() / 1000); }, xmrConnectionService.getRefreshPeriodMs() / 1000);
} }
} }
@ -2063,7 +2064,6 @@ public class XmrWalletService extends XmrWalletBase {
} }
} }
saveWalletWithDelay(); saveWalletWithDelay();
}
// cache wallet info last // cache wallet info last
synchronized (walletLock) { synchronized (walletLock) {
@ -2076,6 +2076,7 @@ public class XmrWalletService extends XmrWalletBase {
} }
} }
} }
}
private MoneroSyncResult syncMainWallet() { private MoneroSyncResult syncMainWallet() {
synchronized (walletLock) { synchronized (walletLock) {