use semaphore for SPV resync

This commit is contained in:
jmacxx 2021-03-30 22:42:48 -05:00
parent 58d93f6e04
commit 26da6cbbe3
No known key found for this signature in database
GPG key ID: 155297BABFE94A1B
4 changed files with 36 additions and 5 deletions

View file

@ -103,6 +103,7 @@ import javax.annotation.Nullable;
@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 @@ public class BisqSetup {
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 @@ public class BisqSetup {
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 @@ public class BisqSetup {
};
walletAppSetup.init(chainFileLockedExceptionHandler,
spvFileCorruptedHandler,
getResyncSpvSemaphore(),
showFirstPopupIfResyncSPVRequestedHandler,
showPopupIfInvalidBtcConfigHandler,
walletPasswordHandler,
@ -542,6 +544,33 @@ public class BisqSetup {
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);
}

View file

@ -105,6 +105,7 @@ public class WalletAppSetup {
void init(@Nullable Consumer<String> chainFileLockedExceptionHandler,
@Nullable Consumer<String> spvFileCorruptedHandler,
boolean isSpvResyncRequested,
@Nullable Runnable showFirstPopupIfResyncSPVRequestedHandler,
@Nullable Runnable showPopupIfInvalidBtcConfigHandler,
Runnable walletPasswordHandler,
@ -179,7 +180,7 @@ public class WalletAppSetup {
if (walletsManager.areWalletsEncrypted() && !coreContext.isApiUser()) {
walletPasswordHandler.run();
} else {
if (preferences.isResyncSpvRequested() && !coreContext.isApiUser()) {
if (isSpvResyncRequested && !coreContext.isApiUser()) {
if (showFirstPopupIfResyncSPVRequestedHandler != null)
showFirstPopupIfResyncSPVRequestedHandler.run();
} else {

View file

@ -575,7 +575,7 @@ public class MainViewModel implements ViewModel, BisqSetup.BisqSetupListener {
private void showSecondPopupIfResyncSPVRequested(Popup firstPopup) {
firstPopup.hide();
preferences.setResyncSpvRequested(false);
BisqSetup.setResyncSpvSemaphore(false);
new Popup().information(Res.get("settings.net.reSyncSPVAfterRestartCompleted"))
.hideCloseButton()
.useShutDownButton()

View file

@ -30,6 +30,7 @@ import bisq.desktop.main.overlays.popups.Popup;
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 class GUIUtil {
.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"))