Skip to content

Commit

Permalink
Fix exception: heightOfLastBlock must match chainHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacxx committed Feb 2, 2022
1 parent fcb13ed commit 33644b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import bisq.core.trade.DelayedPayoutAddressProvider;
import bisq.core.user.Preferences;

import bisq.common.UserThread;
import bisq.common.config.Config;
import bisq.common.util.GcUtil;

Expand All @@ -48,8 +49,6 @@

import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkArgument;

/**
* Manages periodical snapshots of the DaoState.
* At startup we apply a snapshot if available.
Expand Down Expand Up @@ -79,6 +78,7 @@ public class DaoStateSnapshotService implements DaoSetupService, DaoStateListene
@Setter
@Nullable
private Runnable daoRequiresRestartHandler;
private int daoRequiresRestartHandlerAttempts = 0;
private boolean readyForPersisting = true;
private boolean isParseBlockChainComplete;

Expand Down Expand Up @@ -281,8 +281,11 @@ public void applySnapshot(boolean fromReorg) {
int chainHeightOfPersisted = persistedBsqState.getChainHeight();
if (!persistedBsqState.getBlocks().isEmpty()) {
int heightOfLastBlock = persistedBsqState.getLastBlock().getHeight();
checkArgument(heightOfLastBlock == chainHeightOfPersisted,
"chainHeightOfPersisted must be same as heightOfLastBlock");
if (heightOfLastBlock != chainHeightOfPersisted) {
log.warn("chainHeightOfPersisted must be same as heightOfLastBlock");
resyncDaoStateFromResources();
return;
}
if (isValidHeight(heightOfLastBlock)) {
if (chainHeightOfLastApplySnapshot != chainHeightOfPersisted) {
chainHeightOfLastApplySnapshot = chainHeightOfPersisted;
Expand Down Expand Up @@ -324,10 +327,18 @@ private boolean isValidHeight(int heightOfLastBlock) {

private void resyncDaoStateFromResources() {
log.info("resyncDaoStateFromResources called");
if (daoRequiresRestartHandler == null && ++daoRequiresRestartHandlerAttempts <= 3) {
log.warn("daoRequiresRestartHandler has not been initialized yet, will try again in 10 seconds");
UserThread.runAfter(this::resyncDaoStateFromResources, 10); // a delay for the app to init
return;
}
try {
daoStateStorageService.resyncDaoStateFromResources(storageDir);

if (daoRequiresRestartHandler != null) {
// the restart handler informs the user of the need to restart bisq (in desktop mode)
if (daoRequiresRestartHandler == null) {
log.error("daoRequiresRestartHandler COULD NOT be called as it has not been initialized yet");
} else {
log.info("calling daoRequiresRestartHandler...");
daoRequiresRestartHandler.run();
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@

import lombok.extern.slf4j.Slf4j;

import static com.google.common.base.Preconditions.checkArgument;

/**
* Manages persistence of the daoState.
*/
Expand Down Expand Up @@ -154,8 +152,10 @@ protected void readFromResources(String postFix, Runnable completeHandler) {
list = bsqBlocksStorageService.readBlocks(chainHeight);
if (!list.isEmpty()) {
int heightOfLastBlock = list.getLast().getHeight();
checkArgument(heightOfLastBlock == chainHeight,
"heightOfLastBlock must match chainHeight");
if (heightOfLastBlock != chainHeight) {
log.warn("heightOfLastBlock {} must match chainHeight {}", heightOfLastBlock, chainHeight);
// this error scenario is handled by DaoStateSnapshotService, it will resync from resources & reboot
}
}
} else {
list = bsqBlocksStorageService.migrateBlocks(daoStateAsProto.getBlocksList());
Expand Down

0 comments on commit 33644b0

Please sign in to comment.