Skip to content

Commit

Permalink
fix null wallet on error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Dec 27, 2024
1 parent 018ac61 commit cccd9cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
11 changes: 2 additions & 9 deletions core/src/main/java/haveno/core/trade/Trade.java
Original file line number Diff line number Diff line change
Expand Up @@ -852,14 +852,6 @@ public boolean isWalletConnectedToDaemon() {
}
}

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
}
Expand Down Expand Up @@ -2386,7 +2378,8 @@ private ObjectProperty<Volume> getVolumeProperty() {
return tradeVolumeProperty;
}

private void onConnectionChanged(MoneroRpcConnection connection) {
@Override
protected void onConnectionChanged(MoneroRpcConnection connection) {
synchronized (walletLock) {

// use current connection
Expand Down
16 changes: 15 additions & 1 deletion core/src/main/java/haveno/core/xmr/wallet/XmrWalletBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
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;
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;
Expand Down Expand Up @@ -137,6 +138,19 @@ public void onSyncProgress(long height, long startHeight, long endHeight, double
}
}

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(() -> {
Expand Down
27 changes: 12 additions & 15 deletions core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ private void maybeInitMainWallet(boolean sync) {
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);
Expand All @@ -1346,7 +1346,7 @@ private void maybeInitMainWallet(boolean sync, int numAttempts) {
}, THREAD_ID);
}

private void doMaybeInitMainWallet(boolean sync, int numAttempts) {
private void doMaybeInitMainWallet(boolean sync, int numSyncAttempts) {
synchronized (walletLock) {
if (isShutDownStarted) return;

Expand Down Expand Up @@ -1374,7 +1374,7 @@ private void doMaybeInitMainWallet(boolean sync, int numAttempts) {

// 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
Expand All @@ -1393,7 +1393,7 @@ private void doMaybeInitMainWallet(boolean sync, int numAttempts) {
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");
Expand Down Expand Up @@ -1428,8 +1428,8 @@ private void doMaybeInitMainWallet(boolean sync, int numAttempts) {
} 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);

Expand All @@ -1440,7 +1440,7 @@ private void doMaybeInitMainWallet(boolean sync, int numAttempts) {
} else {
log.warn("Trying again in {} seconds", xmrConnectionService.getRefreshPeriodMs() / 1000);
UserThread.runAfter(() -> {
maybeInitMainWallet(true, numAttempts - 1);
maybeInitMainWallet(true, numSyncAttempts - 1);
}, xmrConnectionService.getRefreshPeriodMs() / 1000);
}
}
Expand Down Expand Up @@ -1754,7 +1754,8 @@ private MoneroWalletRpc startWalletRpcInstance(Integer port, boolean applyProxyU
return MONERO_WALLET_RPC_MANAGER.startInstance(cmd);
}

private void onConnectionChanged(MoneroRpcConnection connection) {
@Override
protected void onConnectionChanged(MoneroRpcConnection connection) {
synchronized (walletLock) {

// use current connection
Expand Down Expand Up @@ -1858,13 +1859,13 @@ public void forceRestartMainWallet() {
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() {
Expand Down Expand Up @@ -2033,10 +2034,6 @@ private boolean requestSwitchToNextBestConnection() {
return requestSwitchToNextBestConnection(null);
}

public boolean requestSwitchToNextBestConnection(MoneroRpcConnection sourceConnection) {
return xmrConnectionService.requestSwitchToNextBestConnection(sourceConnection);
}

private void onNewBlock(long height) {
UserThread.execute(() -> {
walletHeight.set(height);
Expand Down

0 comments on commit cccd9cf

Please sign in to comment.