Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use semaphore for SPV resync #5380

Merged
merged 1 commit into from Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions core/src/main/java/bisq/core/app/BisqSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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 {
Expand All @@ -438,6 +439,7 @@ private void initWallet() {
};
walletAppSetup.init(chainFileLockedExceptionHandler,
spvFileCorruptedHandler,
getResyncSpvSemaphore(),
showFirstPopupIfResyncSPVRequestedHandler,
showPopupIfInvalidBtcConfigHandler,
walletPasswordHandler,
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/bisq/core/app/WalletAppSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public WalletAppSetup(CoreContext coreContext,

void init(@Nullable Consumer<String> chainFileLockedExceptionHandler,
@Nullable Consumer<String> spvFileCorruptedHandler,
boolean isSpvResyncRequested,
@Nullable Runnable showFirstPopupIfResyncSPVRequestedHandler,
@Nullable Runnable showPopupIfInvalidBtcConfigHandler,
Runnable walletPasswordHandler,
Expand Down Expand Up @@ -179,7 +180,7 @@ void init(@Nullable Consumer<String> chainFileLockedExceptionHandler,
if (walletsManager.areWalletsEncrypted() && !coreContext.isApiUser()) {
walletPasswordHandler.run();
} else {
if (preferences.isResyncSpvRequested() && !coreContext.isApiUser()) {
if (isSpvResyncRequested && !coreContext.isApiUser()) {
if (showFirstPopupIfResyncSPVRequestedHandler != null)
showFirstPopupIfResyncSPVRequestedHandler.run();
} else {
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/main/java/bisq/desktop/main/MainViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion desktop/src/main/java/bisq/desktop/util/GUIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"))
Expand Down