From cccd9cf094fe5bb8406dfa1358246de6de34ac26 Mon Sep 17 00:00:00 2001 From: woodser Date: Mon, 23 Dec 2024 08:35:39 -0500 Subject: [PATCH] fix null wallet on error handling --- .../main/java/haveno/core/trade/Trade.java | 11 ++------ .../haveno/core/xmr/wallet/XmrWalletBase.java | 16 ++++++++++- .../core/xmr/wallet/XmrWalletService.java | 27 +++++++++---------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/haveno/core/trade/Trade.java b/core/src/main/java/haveno/core/trade/Trade.java index e88f70bda9..c5698a61c3 100644 --- a/core/src/main/java/haveno/core/trade/Trade.java +++ b/core/src/main/java/haveno/core/trade/Trade.java @@ -852,14 +852,6 @@ public abstract class Trade extends XmrWalletBase implements Tradable, Model { } } - public boolean requestSwitchToNextBestConnection(MoneroRpcConnection sourceConnection) { - if (xmrConnectionService.requestSwitchToNextBestConnection(sourceConnection)) { - onConnectionChanged(xmrConnectionService.getConnection()); // change connection on same thread - return true; - } - return false; - } - public boolean isIdling() { return this instanceof ArbitratorTrade && isDepositsConfirmed() && walletExists() && pollNormalStartTimeMs == null; // arbitrator idles trade after deposits confirm unless overriden } @@ -2386,7 +2378,8 @@ public abstract class Trade extends XmrWalletBase implements Tradable, Model { return tradeVolumeProperty; } - private void onConnectionChanged(MoneroRpcConnection connection) { + @Override + protected void onConnectionChanged(MoneroRpcConnection connection) { synchronized (walletLock) { // use current connection 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 81eada3295..06f2e17112 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletBase.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletBase.java @@ -17,6 +17,7 @@ import javafx.beans.property.LongProperty; import javafx.beans.property.SimpleLongProperty; import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import monero.common.MoneroRpcConnection; import monero.common.TaskLooper; import monero.daemon.model.MoneroTx; import monero.wallet.MoneroWallet; @@ -24,7 +25,7 @@ import monero.wallet.MoneroWalletFull; import monero.wallet.model.MoneroWalletListener; @Slf4j -public class XmrWalletBase { +public abstract class XmrWalletBase { // constants public static final int SYNC_PROGRESS_TIMEOUT_SECONDS = 120; @@ -137,6 +138,19 @@ public class XmrWalletBase { } } + public boolean requestSwitchToNextBestConnection(MoneroRpcConnection sourceConnection) { + if (xmrConnectionService.requestSwitchToNextBestConnection(sourceConnection)) { + onConnectionChanged(xmrConnectionService.getConnection()); // change connection on same thread + return true; + } + return false; + } + + protected abstract void onConnectionChanged(MoneroRpcConnection connection); + + + // ------------------------------ PRIVATE HELPERS ------------------------- + private void updateSyncProgress(long height, long targetHeight) { resetSyncProgressTimeout(); UserThread.execute(() -> { 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 ccd8d491ce..fe085de1bb 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -1334,7 +1334,7 @@ public class XmrWalletService extends XmrWalletBase { maybeInitMainWallet(sync, MAX_SYNC_ATTEMPTS); } - private void maybeInitMainWallet(boolean sync, int numAttempts) { + private void maybeInitMainWallet(boolean sync, int numSyncAttempts) { ThreadUtils.execute(() -> { try { doMaybeInitMainWallet(sync, MAX_SYNC_ATTEMPTS); @@ -1346,7 +1346,7 @@ public class XmrWalletService extends XmrWalletBase { }, THREAD_ID); } - private void doMaybeInitMainWallet(boolean sync, int numAttempts) { + private void doMaybeInitMainWallet(boolean sync, int numSyncAttempts) { synchronized (walletLock) { if (isShutDownStarted) return; @@ -1374,7 +1374,7 @@ public class XmrWalletService extends XmrWalletBase { // sync main wallet if applicable // TODO: error handling and re-initialization is jenky, refactor - if (sync && numAttempts > 0) { + if (sync && numSyncAttempts > 0) { try { // switch connection if disconnected @@ -1393,7 +1393,7 @@ public class XmrWalletService extends XmrWalletBase { log.warn("Error syncing wallet with progress on startup: " + e.getMessage()); forceCloseMainWallet(); requestSwitchToNextBestConnection(sourceConnection); - maybeInitMainWallet(true, numAttempts - 1); // re-initialize wallet and sync again + maybeInitMainWallet(true, numSyncAttempts - 1); // re-initialize wallet and sync again return; } log.info("Done syncing main wallet in " + (System.currentTimeMillis() - time) + " ms"); @@ -1428,8 +1428,8 @@ public class XmrWalletService extends XmrWalletBase { } 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 (numAttempts <= 1) { - log.warn("Failed to sync main wallet. Opening app without syncing", numAttempts); + if (numSyncAttempts <= 1) { + log.warn("Failed to sync main wallet. Opening app without syncing", numSyncAttempts); HavenoUtils.havenoSetup.getWalletInitialized().set(true); saveMainWallet(false); @@ -1440,7 +1440,7 @@ public class XmrWalletService extends XmrWalletBase { } else { log.warn("Trying again in {} seconds", xmrConnectionService.getRefreshPeriodMs() / 1000); UserThread.runAfter(() -> { - maybeInitMainWallet(true, numAttempts - 1); + maybeInitMainWallet(true, numSyncAttempts - 1); }, xmrConnectionService.getRefreshPeriodMs() / 1000); } } @@ -1754,7 +1754,8 @@ public class XmrWalletService extends XmrWalletBase { return MONERO_WALLET_RPC_MANAGER.startInstance(cmd); } - private void onConnectionChanged(MoneroRpcConnection connection) { + @Override + protected void onConnectionChanged(MoneroRpcConnection connection) { synchronized (walletLock) { // use current connection @@ -1858,13 +1859,13 @@ public class XmrWalletService extends XmrWalletBase { log.warn("Force restarting main wallet"); if (isClosingWallet) return; forceCloseMainWallet(); - maybeInitMainWallet(true); + doMaybeInitMainWallet(true, MAX_SYNC_ATTEMPTS); } public void handleWalletError(Exception e, MoneroRpcConnection sourceConnection) { if (HavenoUtils.isUnresponsive(e)) forceCloseMainWallet(); // wallet can be stuck a while - if (xmrConnectionService.isConnected()) requestSwitchToNextBestConnection(sourceConnection); - getWallet(); // re-open wallet + requestSwitchToNextBestConnection(sourceConnection); + if (wallet == null) doMaybeInitMainWallet(true, MAX_SYNC_ATTEMPTS); } private void startPolling() { @@ -2033,10 +2034,6 @@ public class XmrWalletService extends XmrWalletBase { return requestSwitchToNextBestConnection(null); } - public boolean requestSwitchToNextBestConnection(MoneroRpcConnection sourceConnection) { - return xmrConnectionService.requestSwitchToNextBestConnection(sourceConnection); - } - private void onNewBlock(long height) { UserThread.execute(() -> { walletHeight.set(height);