From 481b6c871a789eddf754ff69af6259a7f0732482 Mon Sep 17 00:00:00 2001 From: woodser Date: Mon, 29 Jan 2024 06:53:55 -0500 Subject: [PATCH] backup wallets on shut down, skip when open on windows --- .../main/java/haveno/core/trade/Trade.java | 19 +++++++++++++++++-- .../core/xmr/wallet/XmrWalletService.java | 6 +++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/haveno/core/trade/Trade.java b/core/src/main/java/haveno/core/trade/Trade.java index 7348464718..7b1741feb8 100644 --- a/core/src/main/java/haveno/core/trade/Trade.java +++ b/core/src/main/java/haveno/core/trade/Trade.java @@ -876,7 +876,19 @@ public abstract class Trade implements Tradable, Model { public void saveWallet() { synchronized (walletLock) { if (wallet == null) throw new RuntimeException("Trade wallet is not open for trade " + getId()); - xmrWalletService.saveWallet(wallet, !isArbitrator()); // skip backup if arbitrator + xmrWalletService.saveWallet(wallet); + maybeBackupWallet(); + } + } + + private void maybeBackupWallet() { + boolean createBackup = !isArbitrator() && !(Utilities.isWindows() && isWalletOpen()); // create backup unless arbitrator or windows and wallet is open (cannot copy file while open on windows) + if (createBackup) xmrWalletService.backupWallet(getWalletName()); + } + + private boolean isWalletOpen() { + synchronized (walletLock) { + return wallet != null; } } @@ -1313,7 +1325,7 @@ public abstract class Trade implements Tradable, Model { // save wallet if (wallet != null) { try { - xmrWalletService.saveWallet(wallet, false); // skip backup + xmrWalletService.saveWallet(wallet); stopWallet(); } catch (Exception e) { // warning will be logged for main wallet, so skip logging here @@ -1333,6 +1345,9 @@ public abstract class Trade implements Tradable, Model { forceStopWallet(); } + // backup trade wallet if applicable + maybeBackupWallet(); + // de-initialize if (idlePayoutSyncer != null) { xmrWalletService.removeWalletListener(idlePayoutSyncer); 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 9111aad0f1..44528ee9ec 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -220,7 +220,7 @@ public class XmrWalletService { } public void saveMainWallet() { - saveMainWallet(true); + saveMainWallet(!(Utilities.isWindows() && wallet != null)); } public void saveMainWallet(boolean backup) { @@ -324,6 +324,10 @@ public class XmrWalletService { return syncWallet(wallet); } + public void saveWallet(MoneroWallet wallet) { + saveWallet(wallet, false); + } + public void saveWallet(MoneroWallet wallet, boolean backup) { wallet.save(); if (backup) backupWallet(wallet.getPath());