diff --git a/core/src/main/java/bisq/core/dao/state/DaoStateSnapshotService.java b/core/src/main/java/bisq/core/dao/state/DaoStateSnapshotService.java index 37032e3f92..fac582c7a1 100644 --- a/core/src/main/java/bisq/core/dao/state/DaoStateSnapshotService.java +++ b/core/src/main/java/bisq/core/dao/state/DaoStateSnapshotService.java @@ -277,18 +277,20 @@ public class DaoStateSnapshotService implements DaoSetupService, DaoStateListene applySnapshot(false); } - public void applySnapshot(boolean fromInitialize) { - DaoState persistedBsqState = daoStateStorageService.getPersistedBsqState(); - if (persistedBsqState == null) { + private void applySnapshot(boolean fromInitialize) { + DaoState persistedDaoState = daoStateStorageService.getPersistedBsqState(); + if (persistedDaoState == null) { log.info("Try to apply snapshot but no stored snapshot available. That is expected at first blocks."); return; } - if (persistedBsqState.getBlocks().isEmpty()) { + int chainHeightOfPersistedDaoState = persistedDaoState.getChainHeight(); + + if (persistedDaoState.getBlocks().isEmpty()) { if (fromInitialize) { log.info("No Bsq blocks in DaoState. Expected if no data are provided yet from resources or persisted data."); } else { - log.info("We got a reorg and we want to apply the snapshot but it is empty. " + + log.info("We got a reorg or error and we want to apply the snapshot but it is empty. " + "That is expected in the first blocks until the first snapshot has been created. " + "We remove all dao store files and shutdown. " + "After a restart resource files will be applied if available."); @@ -297,31 +299,32 @@ public class DaoStateSnapshotService implements DaoSetupService, DaoStateListene return; } - int chainHeightOfPersisted = persistedBsqState.getChainHeight(); - int heightOfLastBlock = persistedBsqState.getLastBlock().getHeight(); - if (heightOfLastBlock != chainHeightOfPersisted) { - log.warn("chainHeightOfPersisted must be same as heightOfLastBlock. heightOfLastBlock={}, chainHeightOfPersisted={}", - heightOfLastBlock, chainHeightOfPersisted); + if (!daoStateStorageService.isChainHeighMatchingLastBlockHeight()) { resyncDaoStateFromResources(); return; } - if (isHeightAtLeastGenesisHeight(heightOfLastBlock)) { - if (chainHeightOfLastAppliedSnapshot != chainHeightOfPersisted) { - chainHeightOfLastAppliedSnapshot = chainHeightOfPersisted; - daoStateService.applySnapshot(persistedBsqState); - LinkedList persistedDaoStateHashChain = daoStateStorageService.getPersistedDaoStateHashChain(); - daoStateMonitoringService.applySnapshot(persistedDaoStateHashChain); - daoStateStorageService.releaseMemory(); - } else { - // The reorg might have been caused by the previous parsing which might contains a range of - // blocks. - log.warn("We applied already a snapshot with chainHeight {}. " + - "We remove all dao store files and shutdown. After a restart resource files will " + - "be applied if available.", - chainHeightOfLastAppliedSnapshot); - resyncDaoStateFromResources(); - } + + if (!isHeightAtLeastGenesisHeight(chainHeightOfPersistedDaoState)) { + log.error("heightOfPersistedLastBlock is below genesis height. This should never happen."); + return; } + + if (chainHeightOfLastAppliedSnapshot == chainHeightOfPersistedDaoState) { + // The reorg might have been caused by the previous parsing which might contains a range of + // blocks. + log.warn("We applied already a snapshot with chainHeight {}. " + + "We remove all dao store files and shutdown. After a restart resource files will " + + "be applied if available.", + chainHeightOfLastAppliedSnapshot); + resyncDaoStateFromResources(); + return; + } + + chainHeightOfLastAppliedSnapshot = chainHeightOfPersistedDaoState; + daoStateService.applySnapshot(persistedDaoState); + LinkedList persistedDaoStateHashChain = daoStateStorageService.getPersistedDaoStateHashChain(); + daoStateMonitoringService.applySnapshot(persistedDaoStateHashChain); + daoStateStorageService.releaseMemory(); } diff --git a/core/src/main/java/bisq/core/dao/state/storage/DaoStateStorageService.java b/core/src/main/java/bisq/core/dao/state/storage/DaoStateStorageService.java index 40daa55213..08a05bb7ae 100644 --- a/core/src/main/java/bisq/core/dao/state/storage/DaoStateStorageService.java +++ b/core/src/main/java/bisq/core/dao/state/storage/DaoStateStorageService.java @@ -180,6 +180,19 @@ public class DaoStateStorageService extends StoreService { return new DaoState(); } + public boolean isChainHeighMatchingLastBlockHeight() { + DaoState persistedDaoState = getPersistedBsqState(); + int heightOfPersistedLastBlock = persistedDaoState.getLastBlock().getHeight(); + int chainHeightOfPersistedDaoState = persistedDaoState.getChainHeight(); + boolean isMatching = heightOfPersistedLastBlock == chainHeightOfPersistedDaoState; + if (!isMatching) { + log.warn("heightOfPersistedLastBlock is not same as chainHeightOfPersistedDaoState.\n" + + "heightOfPersistedLastBlock={}; chainHeightOfPersistedDaoState={}", + heightOfPersistedLastBlock, chainHeightOfPersistedDaoState); + } + return isMatching; + } + public LinkedList getPersistedDaoStateHashChain() { return store.getDaoStateHashChain(); }