From 9b9b295045a56f5c40491bd78622a8cd16c2064b Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 17 Nov 2021 17:13:41 +0200 Subject: [PATCH] force save of temp backup if refreshed wallet transactions are less --- .../com/sparrowwallet/sparrow/io/Storage.java | 28 +++++++++++-------- .../sparrow/wallet/WalletForm.java | 4 ++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index 12c4f89e..f73a4454 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -20,6 +20,7 @@ import java.nio.file.attribute.PosixFilePermissions; import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; import java.text.DateFormat; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; @@ -173,24 +174,27 @@ public class Storage { deleteBackups(null); } - public void deleteTempBackups() { + public void deleteTempBackups(boolean forceSave) { File[] backups = getBackups(Storage.TEMP_BACKUP_PREFIX); - if(backups.length > 0) { - try { - Date date = BACKUP_DATE_FORMAT.parse(getBackupDate(backups[0].getName())); - ProcessHandle.Info processInfo = ProcessHandle.current().info(); - if(processInfo.startInstant().isPresent() && processInfo.startInstant().get().isAfter(date.toInstant())) { - File permanent = new File(backups[0].getParent(), backups[0].getName().substring(Storage.TEMP_BACKUP_PREFIX.length() + 1)); - backups[0].renameTo(permanent); - } - } catch(Exception e) { - log.error("Error copying temporary to permanent backup", e); - } + if(backups.length > 0 && (forceSave || hasStartedSince(backups[0]))) { + File permanent = new File(backups[0].getParent(), backups[0].getName().substring(Storage.TEMP_BACKUP_PREFIX.length() + 1)); + backups[0].renameTo(permanent); } deleteBackups(Storage.TEMP_BACKUP_PREFIX); } + private boolean hasStartedSince(File lastBackup) { + try { + Date date = BACKUP_DATE_FORMAT.parse(getBackupDate(lastBackup.getName())); + ProcessHandle.Info processInfo = ProcessHandle.current().info(); + return (processInfo.startInstant().isPresent() && processInfo.startInstant().get().isAfter(date.toInstant())); + } catch(Exception e) { + log.error("Error parsing date for backup file " + lastBackup.getName(), e); + return false; + } + } + private void deleteBackups(String prefix) { File[] backups = getBackups(prefix); for(File backup : backups) { diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java index b2fa324f..6f6550da 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -195,7 +195,9 @@ public class WalletForm { } } - storage.deleteTempBackups(); + //Force saving the backup if the current wallet has fewer transactions than the past wallet (i.e. incomplete load) + storage.deleteTempBackups(wallet.getTransactions().size() < pastWallet.getTransactions().size()); + return changedEntries; }