From 1adf7bd73fccbdcba8d7e7cdffcee18cce577c06 Mon Sep 17 00:00:00 2001 From: napoly Date: Sun, 18 Dec 2022 16:15:55 +0100 Subject: [PATCH] avoid redundant wallet backups, max 10 backups (#505) Co-authored-by: woodser --- .../core/btc/wallet/XmrWalletService.java | 31 +++++++++++-------- .../bisq/core/offer/OpenOfferManager.java | 2 +- .../java/bisq/core/trade/TradeManager.java | 4 +-- .../tasks/MaybeSendSignContractRequest.java | 1 - .../tasks/ProcessInitMultisigRequest.java | 2 +- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/bisq/core/btc/wallet/XmrWalletService.java b/core/src/main/java/bisq/core/btc/wallet/XmrWalletService.java index 8b1dfe103e..456db74691 100644 --- a/core/src/main/java/bisq/core/btc/wallet/XmrWalletService.java +++ b/core/src/main/java/bisq/core/btc/wallet/XmrWalletService.java @@ -85,6 +85,7 @@ public class XmrWalletService { private static final String MONERO_MULTISIG_WALLET_PREFIX = "xmr_multisig_trade_"; public static final double MINER_FEE_TOLERANCE = 0.25; // miner fee must be within percent of estimated fee private static final double SECURITY_DEPOSIT_TOLERANCE = Config.baseCurrencyNetwork() == BaseCurrencyNetwork.XMR_LOCAL ? 0.25 : 0.05; // security deposit absorbs miner fee up to percent + private static final int NUM_MAX_BACKUP_WALLETS = 10; private final CoreAccountService accountService; private final CoreMoneroConnectionsService connectionsService; @@ -166,8 +167,12 @@ public class XmrWalletService { return wallet; } - public void saveWallet() { - saveWallet(getWallet()); + public void saveMainWallet() { + saveMainWallet(true); + } + + public void saveMainWallet(boolean backup) { + saveWallet(getWallet(), backup); } public boolean isWalletReady() { @@ -241,13 +246,13 @@ public class XmrWalletService { return; } if (!multisigWallets.containsKey(tradeId)) throw new RuntimeException("Multisig wallet to save was not previously opened for trade " + tradeId); - saveWallet(multisigWallets.get(tradeId)); + saveWallet(multisigWallets.get(tradeId), true); } } - private void saveWallet(MoneroWallet wallet) { + private void saveWallet(MoneroWallet wallet, boolean backup) { wallet.save(); - backupWallet(wallet.getPath()); + if (backup) backupWallet(wallet.getPath()); } public void closeMultisigWallet(String tradeId) { @@ -340,7 +345,7 @@ public class XmrWalletService { // freeze inputs for (MoneroOutput input : tradeTx.getInputs()) wallet.freezeOutput(input.getKeyImage().getHex()); - wallet.save(); + saveMainWallet(); return tradeTx; } } @@ -412,7 +417,7 @@ public class XmrWalletService { /** * Get the tx fee estimate based on its weight. - * + * * @param txWeight - the tx weight * @return the tx fee estimate */ @@ -516,7 +521,7 @@ public class XmrWalletService { wallet.sync(); // blocking wallet.startSyncing(connectionsService.getDefaultRefreshPeriodMs()); // start syncing wallet in background connectionsService.doneDownload(); // TODO: using this to signify both daemon and wallet synced, refactor sync handling of both - saveWallet(wallet); + saveMainWallet(false); // skip backup on open } catch (Exception e) { e.printStackTrace(); } @@ -653,7 +658,7 @@ public class XmrWalletService { tasks.add(() -> { try { wallet.changePassword(oldPassword, newPassword); - saveWallet(wallet); + saveMainWallet(); } catch (Exception e) { e.printStackTrace(); throw e; @@ -667,7 +672,7 @@ public class XmrWalletService { MoneroWallet multisigWallet = getMultisigWallet(tradeId); // TODO (woodser): this unnecessarily connects and syncs unopen wallets and leaves open if (multisigWallet == null) return; multisigWallet.changePassword(oldPassword, newPassword); - saveWallet(multisigWallet); + saveMultisigWallet(tradeId); }); } @@ -726,9 +731,9 @@ public class XmrWalletService { } private void backupWallet(String walletName) { - FileUtil.rollingBackup(walletDir, walletName, 20); - FileUtil.rollingBackup(walletDir, walletName + ".keys", 20); - FileUtil.rollingBackup(walletDir, walletName + ".address.txt", 20); + FileUtil.rollingBackup(walletDir, walletName, NUM_MAX_BACKUP_WALLETS); + FileUtil.rollingBackup(walletDir, walletName + ".keys", NUM_MAX_BACKUP_WALLETS); + FileUtil.rollingBackup(walletDir, walletName + ".address.txt", NUM_MAX_BACKUP_WALLETS); } private void deleteBackupWallets(String walletName) { diff --git a/core/src/main/java/bisq/core/offer/OpenOfferManager.java b/core/src/main/java/bisq/core/offer/OpenOfferManager.java index 2987206e24..07de48dc85 100644 --- a/core/src/main/java/bisq/core/offer/OpenOfferManager.java +++ b/core/src/main/java/bisq/core/offer/OpenOfferManager.java @@ -568,7 +568,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe Offer offer = openOffer.getOffer(); if (offer.getOfferPayload().getReserveTxKeyImages() != null) { for (String frozenKeyImage : offer.getOfferPayload().getReserveTxKeyImages()) xmrWalletService.getWallet().thawOutput(frozenKeyImage); - xmrWalletService.getWallet().save(); + xmrWalletService.saveMainWallet(); } offer.setState(Offer.State.REMOVED); openOffer.setState(OpenOffer.State.CANCELED); diff --git a/core/src/main/java/bisq/core/trade/TradeManager.java b/core/src/main/java/bisq/core/trade/TradeManager.java index ced7ed61db..ce5f2df923 100644 --- a/core/src/main/java/bisq/core/trade/TradeManager.java +++ b/core/src/main/java/bisq/core/trade/TradeManager.java @@ -317,7 +317,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi log.info("Thawing output which is not reserved for offer or trade: " + unreservedFrozenKeyImage); xmrWalletService.getWallet().thawOutput(unreservedFrozenKeyImage); } - xmrWalletService.getWallet().save(); + if (!frozenKeyImages.isEmpty()) xmrWalletService.saveMainWallet(); } public TradeProtocol getTradeProtocol(Trade trade) { @@ -1071,7 +1071,7 @@ public class TradeManager implements PersistedDataHost, DecryptedDirectMessageLi // unreserve key images if (trade instanceof TakerTrade && trade.getSelf().getReserveTxKeyImages() != null) { for (String keyImage : trade.getSelf().getReserveTxKeyImages()) xmrWalletService.getWallet().thawOutput(keyImage); - xmrWalletService.getWallet().save(); + xmrWalletService.saveMainWallet(); } // remove trade diff --git a/core/src/main/java/bisq/core/trade/protocol/tasks/MaybeSendSignContractRequest.java b/core/src/main/java/bisq/core/trade/protocol/tasks/MaybeSendSignContractRequest.java index 4b9f914b52..e0551d5676 100644 --- a/core/src/main/java/bisq/core/trade/protocol/tasks/MaybeSendSignContractRequest.java +++ b/core/src/main/java/bisq/core/trade/protocol/tasks/MaybeSendSignContractRequest.java @@ -135,7 +135,6 @@ public class MaybeSendSignContractRequest extends TradeTask { private void completeAux() { trade.setState(State.CONTRACT_SIGNATURE_REQUESTED); processModel.getTradeManager().requestPersistence(); - processModel.getXmrWalletService().saveWallet(); complete(); } } diff --git a/core/src/main/java/bisq/core/trade/protocol/tasks/ProcessInitMultisigRequest.java b/core/src/main/java/bisq/core/trade/protocol/tasks/ProcessInitMultisigRequest.java index 1009a7d305..236e006292 100644 --- a/core/src/main/java/bisq/core/trade/protocol/tasks/ProcessInitMultisigRequest.java +++ b/core/src/main/java/bisq/core/trade/protocol/tasks/ProcessInitMultisigRequest.java @@ -122,7 +122,7 @@ public class ProcessInitMultisigRequest extends TradeTask { log.info("Importing exchanged multisig hex for trade {}", trade.getId()); MoneroMultisigInitResult result = multisigWallet.exchangeMultisigKeys(Arrays.asList(peers[0].getExchangedMultisigHex(), peers[1].getExchangedMultisigHex()), xmrWalletService.getWalletPassword()); processModel.setMultisigAddress(result.getAddress()); - processModel.getProvider().getXmrWalletService().saveMultisigWallet(trade.getId()); // save multisig wallet once it's created + trade.saveWallet(); // save multisig wallet on completion trade.setStateIfValidTransitionTo(Trade.State.MULTISIG_COMPLETED); }