handle closing, stopping, deleting native and rpc wallet

This commit is contained in:
woodser 2024-04-03 14:49:52 -04:00
parent 01dd6a8af9
commit 60d82520c0
2 changed files with 40 additions and 38 deletions

View file

@ -924,19 +924,10 @@ public abstract class Trade implements Tradable, Model {
} }
} }
private void stopWallet() { private void forceCloseWallet() {
synchronized (walletLock) {
if (wallet == null) throw new RuntimeException("Trade wallet to close is not open for trade " + getId());
stopPolling();
xmrWalletService.stopWallet(wallet, wallet.getPath(), true);
wallet = null;
}
}
public void forceStopWallet() {
if (wallet != null) { if (wallet != null) {
log.warn("Force stopping wallet for {} {}", getClass().getSimpleName(), getId()); log.warn("Force closing wallet for {} {}", getClass().getSimpleName(), getId());
xmrWalletService.stopWallet(wallet, wallet.getPath(), true); xmrWalletService.forceCloseWallet(wallet, wallet.getPath());
wallet = null; wallet = null;
} }
} }
@ -969,8 +960,8 @@ public abstract class Trade implements Tradable, Model {
throw new IllegalStateException("Refusing to delete wallet for " + getClass().getSimpleName() + " " + getId() + " because it has a balance"); throw new IllegalStateException("Refusing to delete wallet for " + getClass().getSimpleName() + " " + getId() + " because it has a balance");
} }
// force stop wallet // force close wallet
stopWallet(); forceCloseWallet();
// delete wallet // delete wallet
log.info("Deleting wallet for {} {}", getClass().getSimpleName(), getId()); log.info("Deleting wallet for {} {}", getClass().getSimpleName(), getId());
@ -1349,11 +1340,10 @@ public abstract class Trade implements Tradable, Model {
ThreadUtils.awaitTasks(shutDownThreads); ThreadUtils.awaitTasks(shutDownThreads);
} }
// save wallet // save and close
if (wallet != null) { if (wallet != null) {
try { try {
xmrWalletService.saveWallet(wallet); closeWallet();
stopWallet();
} catch (Exception e) { } catch (Exception e) {
// warning will be logged for main wallet, so skip logging here // warning will be logged for main wallet, so skip logging here
//log.warn("Error closing monero-wallet-rpc subprocess for {} {}: {}. Was Haveno stopped manually with ctrl+c?", getClass().getSimpleName(), getId(), e.getMessage()); //log.warn("Error closing monero-wallet-rpc subprocess for {} {}: {}. Was Haveno stopped manually with ctrl+c?", getClass().getSimpleName(), getId(), e.getMessage());
@ -1368,8 +1358,8 @@ public abstract class Trade implements Tradable, Model {
log.warn("Error shutting down {} {}: {}", getClass().getSimpleName(), getId(), e.getMessage()); log.warn("Error shutting down {} {}: {}", getClass().getSimpleName(), getId(), e.getMessage());
e.printStackTrace(); e.printStackTrace();
// force stop wallet // force close wallet
forceStopWallet(); forceCloseWallet();
} }
// backup trade wallet if applicable // backup trade wallet if applicable
@ -2222,7 +2212,7 @@ public abstract class Trade implements Tradable, Model {
log.warn("Force restarting trade wallet for {} {}", getClass().getSimpleName(), getId()); log.warn("Force restarting trade wallet for {} {}", getClass().getSimpleName(), getId());
if (isShutDownStarted || restartInProgress) return; if (isShutDownStarted || restartInProgress) return;
restartInProgress = true; restartInProgress = true;
forceStopWallet(); forceCloseWallet();
if (!isShutDownStarted) wallet = getWallet(); if (!isShutDownStarted) wallet = getWallet();
restartInProgress = false; restartInProgress = false;
if (!isShutDownStarted) ThreadUtils.execute(() -> tryInitSyncing(), getId()); if (!isShutDownStarted) ThreadUtils.execute(() -> tryInitSyncing(), getId());

View file

@ -339,12 +339,19 @@ public class XmrWalletService {
} }
private MoneroWalletConfig getWalletConfig(String walletName) { private MoneroWalletConfig getWalletConfig(String walletName) {
String walletConfigPath = (isNativeLibraryApplied() ? walletDir.getPath() + File.separator : "") + walletName; MoneroWalletConfig config = new MoneroWalletConfig().setPath(getWalletPath(walletName)).setPassword(getWalletPassword());
MoneroWalletConfig config = new MoneroWalletConfig().setPath(walletConfigPath).setPassword(getWalletPassword());
if (isNativeLibraryApplied()) config.setNetworkType(getMoneroNetworkType()); if (isNativeLibraryApplied()) config.setNetworkType(getMoneroNetworkType());
return config; return config;
} }
private String getWalletPath(String walletName) {
return (isNativeLibraryApplied() ? walletDir.getPath() + File.separator : "") + walletName;
}
private static String getWalletName(String walletPath) {
return walletPath.substring(walletPath.lastIndexOf(File.separator) + 1);
}
private boolean isNativeLibraryApplied() { private boolean isNativeLibraryApplied() {
return useNativeXmrWallet && MoneroUtils.isNativeLibraryLoaded(); return useNativeXmrWallet && MoneroUtils.isNativeLibraryLoaded();
} }
@ -374,7 +381,7 @@ public class XmrWalletService {
public void saveWallet(MoneroWallet wallet, boolean backup) { public void saveWallet(MoneroWallet wallet, boolean backup) {
wallet.save(); wallet.save();
if (backup) backupWallet(wallet.getPath()); if (backup) backupWallet(getWalletName(wallet.getPath()));
} }
public void closeWallet(MoneroWallet wallet, boolean save) { public void closeWallet(MoneroWallet wallet, boolean save) {
@ -383,27 +390,26 @@ public class XmrWalletService {
String path = wallet.getPath(); String path = wallet.getPath();
try { try {
wallet.close(save); wallet.close(save);
if (save) backupWallet(path); if (save) backupWallet(getWalletName(path));
} catch (MoneroError e) { } catch (MoneroError e) {
err = e; err = e;
} }
stopWallet(wallet, path);
// stop wallet rpc instance if applicable
if (wallet instanceof MoneroWalletRpc) MONERO_WALLET_RPC_MANAGER.stopInstance((MoneroWalletRpc) wallet, path, false);
if (err != null) throw err; if (err != null) throw err;
} }
public void stopWallet(MoneroWallet wallet, String path) { public void forceCloseWallet(MoneroWallet wallet, String path) {
stopWallet(wallet, path, false);
}
public void stopWallet(MoneroWallet wallet, String path, boolean force) {
// only wallet rpc process needs stopped
if (wallet instanceof MoneroWalletRpc) { if (wallet instanceof MoneroWalletRpc) {
MONERO_WALLET_RPC_MANAGER.stopInstance((MoneroWalletRpc) wallet, path, force); MONERO_WALLET_RPC_MANAGER.stopInstance((MoneroWalletRpc) wallet, path, true);
} else {
wallet.close(false);
} }
} }
public void deleteWallet(String walletName) { public void deleteWallet(String walletName) {
assertNotPath(walletName);
log.info("{}.deleteWallet({})", getClass().getSimpleName(), walletName); log.info("{}.deleteWallet({})", getClass().getSimpleName(), walletName);
if (!walletExists(walletName)) throw new Error("Wallet does not exist at path: " + walletName); if (!walletExists(walletName)) throw new Error("Wallet does not exist at path: " + walletName);
String path = walletDir.toString() + File.separator + walletName; String path = walletDir.toString() + File.separator + walletName;
@ -413,17 +419,23 @@ public class XmrWalletService {
} }
public void backupWallet(String walletName) { public void backupWallet(String walletName) {
assertNotPath(walletName);
FileUtil.rollingBackup(walletDir, walletName, NUM_MAX_WALLET_BACKUPS); FileUtil.rollingBackup(walletDir, walletName, NUM_MAX_WALLET_BACKUPS);
FileUtil.rollingBackup(walletDir, walletName + KEYS_FILE_POSTFIX, NUM_MAX_WALLET_BACKUPS); FileUtil.rollingBackup(walletDir, walletName + KEYS_FILE_POSTFIX, NUM_MAX_WALLET_BACKUPS);
FileUtil.rollingBackup(walletDir, walletName + ADDRESS_FILE_POSTFIX, NUM_MAX_WALLET_BACKUPS); FileUtil.rollingBackup(walletDir, walletName + ADDRESS_FILE_POSTFIX, NUM_MAX_WALLET_BACKUPS);
} }
public void deleteWalletBackups(String walletName) { public void deleteWalletBackups(String walletName) {
assertNotPath(walletName);
FileUtil.deleteRollingBackup(walletDir, walletName); FileUtil.deleteRollingBackup(walletDir, walletName);
FileUtil.deleteRollingBackup(walletDir, walletName + KEYS_FILE_POSTFIX); FileUtil.deleteRollingBackup(walletDir, walletName + KEYS_FILE_POSTFIX);
FileUtil.deleteRollingBackup(walletDir, walletName + ADDRESS_FILE_POSTFIX); FileUtil.deleteRollingBackup(walletDir, walletName + ADDRESS_FILE_POSTFIX);
} }
private static void assertNotPath(String name) {
if (name.contains(File.separator)) throw new IllegalArgumentException("Path not expected: " + name);
}
public MoneroTxWallet createTx(List<MoneroDestination> destinations) { public MoneroTxWallet createTx(List<MoneroDestination> destinations) {
synchronized (walletLock) { synchronized (walletLock) {
try { try {
@ -1001,7 +1013,7 @@ public class XmrWalletService {
return walletFull; return walletFull;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
if (walletFull != null) stopWallet(walletFull, config.getPath()); if (walletFull != null) forceCloseWallet(walletFull, config.getPath());
throw new IllegalStateException("Could not create wallet '" + config.getPath() + "'"); throw new IllegalStateException("Could not create wallet '" + config.getPath() + "'");
} }
} }
@ -1023,7 +1035,7 @@ public class XmrWalletService {
return walletFull; return walletFull;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
if (walletFull != null) stopWallet(walletFull, config.getPath()); if (walletFull != null) forceCloseWallet(walletFull, config.getPath());
throw new IllegalStateException("Could not open full wallet '" + config.getPath() + "'"); throw new IllegalStateException("Could not open full wallet '" + config.getPath() + "'");
} }
} }
@ -1055,7 +1067,7 @@ public class XmrWalletService {
return walletRpc; return walletRpc;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
if (walletRpc != null) stopWallet(walletRpc, config.getPath()); if (walletRpc != null) forceCloseWallet(walletRpc, config.getPath());
throw new IllegalStateException("Could not create wallet '" + config.getPath() + "'. Please close Haveno, stop all monero-wallet-rpc processes, and restart Haveno."); throw new IllegalStateException("Could not create wallet '" + config.getPath() + "'. Please close Haveno, stop all monero-wallet-rpc processes, and restart Haveno.");
} }
} }
@ -1084,7 +1096,7 @@ public class XmrWalletService {
return walletRpc; return walletRpc;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
if (walletRpc != null) stopWallet(walletRpc, config.getPath()); if (walletRpc != null) forceCloseWallet(walletRpc, config.getPath());
throw new IllegalStateException("Could not open wallet '" + config.getPath() + "'. Please close Haveno, stop all monero-wallet-rpc processes, and restart Haveno."); throw new IllegalStateException("Could not open wallet '" + config.getPath() + "'. Please close Haveno, stop all monero-wallet-rpc processes, and restart Haveno.");
} }
} }
@ -1203,7 +1215,7 @@ public class XmrWalletService {
wallet = null; wallet = null;
} }
} catch (Exception e) { } catch (Exception e) {
log.warn("Error closing main monero-wallet-rpc subprocess: {}. Was Haveno stopped manually with ctrl+c?", e.getMessage()); log.warn("Error closing main wallet: {}. Was Haveno stopped manually with ctrl+c?", e.getMessage());
} }
} }
} }