From 26da6cbbe3e24e312484ad66b3bf49a5a57b5b11 Mon Sep 17 00:00:00 2001 From: jmacxx <47253594+jmacxx@users.noreply.github.com> Date: Tue, 30 Mar 2021 22:42:48 -0500 Subject: [PATCH] use semaphore for SPV resync --- .../main/java/bisq/core/app/BisqSetup.java | 33 +++++++++++++++++-- .../java/bisq/core/app/WalletAppSetup.java | 3 +- .../java/bisq/desktop/main/MainViewModel.java | 2 +- .../main/java/bisq/desktop/util/GUIUtil.java | 3 +- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/bisq/core/app/BisqSetup.java b/core/src/main/java/bisq/core/app/BisqSetup.java index ab82d9efc0d..de6db2c2ba9 100644 --- a/core/src/main/java/bisq/core/app/BisqSetup.java +++ b/core/src/main/java/bisq/core/app/BisqSetup.java @@ -103,6 +103,7 @@ @Singleton public class BisqSetup { private static final String VERSION_FILE_NAME = "version"; + private static final String RESYNC_SPV_FILE_NAME = "resyncSpv"; public interface BisqSetupListener { default void onInitP2pNetwork() { @@ -325,7 +326,7 @@ private void step4() { private void maybeReSyncSPVChain() { // We do the delete of the spv file at startup before BitcoinJ is initialized to avoid issues with locked files under Windows. - if (preferences.isResyncSpvRequested()) { + if (getResyncSpvSemaphore()) { try { walletsSetup.reSyncSPVChain(); @@ -424,7 +425,7 @@ private void initWallet() { walletsManager.setAesKey(aesKey); walletsSetup.getWalletConfig().maybeAddSegwitKeychain(walletsSetup.getWalletConfig().btcWallet(), aesKey); - if (preferences.isResyncSpvRequested()) { + if (getResyncSpvSemaphore()) { if (showFirstPopupIfResyncSPVRequestedHandler != null) showFirstPopupIfResyncSPVRequestedHandler.run(); } else { @@ -438,6 +439,7 @@ private void initWallet() { }; walletAppSetup.init(chainFileLockedExceptionHandler, spvFileCorruptedHandler, + getResyncSpvSemaphore(), showFirstPopupIfResyncSPVRequestedHandler, showPopupIfInvalidBtcConfigHandler, walletPasswordHandler, @@ -542,6 +544,33 @@ public static String getLastBisqVersion() { return null; } + @Nullable + public static boolean getResyncSpvSemaphore() { + File resyncSpvSemaphore = new File(Config.appDataDir(), RESYNC_SPV_FILE_NAME); + return resyncSpvSemaphore.exists(); + } + + public static void setResyncSpvSemaphore(boolean isResyncSpvRequested) { + File resyncSpvSemaphore = new File(Config.appDataDir(), RESYNC_SPV_FILE_NAME); + if (isResyncSpvRequested) { + if (!resyncSpvSemaphore.exists()) { + try { + if (!resyncSpvSemaphore.createNewFile()) { + log.error("ResyncSpv file could not be created"); + } + } catch (IOException e) { + e.printStackTrace(); + log.error("ResyncSpv file could not be created. {}", e.toString()); + } + } + } else { + resyncSpvSemaphore.delete(); + } + } + + + + private static File getVersionFile() { return new File(Config.appDataDir(), VERSION_FILE_NAME); } diff --git a/core/src/main/java/bisq/core/app/WalletAppSetup.java b/core/src/main/java/bisq/core/app/WalletAppSetup.java index 8f0fd15f1d7..b54e6706ada 100644 --- a/core/src/main/java/bisq/core/app/WalletAppSetup.java +++ b/core/src/main/java/bisq/core/app/WalletAppSetup.java @@ -105,6 +105,7 @@ public WalletAppSetup(CoreContext coreContext, void init(@Nullable Consumer chainFileLockedExceptionHandler, @Nullable Consumer spvFileCorruptedHandler, + boolean isSpvResyncRequested, @Nullable Runnable showFirstPopupIfResyncSPVRequestedHandler, @Nullable Runnable showPopupIfInvalidBtcConfigHandler, Runnable walletPasswordHandler, @@ -179,7 +180,7 @@ void init(@Nullable Consumer chainFileLockedExceptionHandler, if (walletsManager.areWalletsEncrypted() && !coreContext.isApiUser()) { walletPasswordHandler.run(); } else { - if (preferences.isResyncSpvRequested() && !coreContext.isApiUser()) { + if (isSpvResyncRequested && !coreContext.isApiUser()) { if (showFirstPopupIfResyncSPVRequestedHandler != null) showFirstPopupIfResyncSPVRequestedHandler.run(); } else { diff --git a/desktop/src/main/java/bisq/desktop/main/MainViewModel.java b/desktop/src/main/java/bisq/desktop/main/MainViewModel.java index 1a97544c14d..e787e6ad227 100644 --- a/desktop/src/main/java/bisq/desktop/main/MainViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/MainViewModel.java @@ -575,7 +575,7 @@ private void showFirstPopupIfResyncSPVRequested() { private void showSecondPopupIfResyncSPVRequested(Popup firstPopup) { firstPopup.hide(); - preferences.setResyncSpvRequested(false); + BisqSetup.setResyncSpvSemaphore(false); new Popup().information(Res.get("settings.net.reSyncSPVAfterRestartCompleted")) .hideCloseButton() .useShutDownButton() diff --git a/desktop/src/main/java/bisq/desktop/util/GUIUtil.java b/desktop/src/main/java/bisq/desktop/util/GUIUtil.java index 1cd240f5a94..8f4beb8c122 100644 --- a/desktop/src/main/java/bisq/desktop/util/GUIUtil.java +++ b/desktop/src/main/java/bisq/desktop/util/GUIUtil.java @@ -30,6 +30,7 @@ import bisq.core.account.witness.AccountAgeWitness; import bisq.core.account.witness.AccountAgeWitnessService; +import bisq.core.app.BisqSetup; import bisq.core.btc.setup.WalletsSetup; import bisq.core.locale.Country; import bisq.core.locale.CountryUtil; @@ -823,7 +824,7 @@ public static void reSyncSPVChain(Preferences preferences) { .useShutDownButton() .actionButtonText(Res.get("shared.shutDown")) .onAction(() -> { - preferences.setResyncSpvRequested(true); + BisqSetup.setResyncSpvSemaphore(true); UserThread.runAfter(BisqApp.getShutDownHandler(), 100, TimeUnit.MILLISECONDS); }) .closeButtonText(Res.get("shared.cancel"))