Refactor applySnapshot method: early return, extract method

Signed-off-by: HenrikJannsen <boilingfrog@gmx.com>
This commit is contained in:
HenrikJannsen 2024-06-28 19:42:00 +07:00
parent 343ef5e6cc
commit 04ea4ae053
No known key found for this signature in database
GPG Key ID: 02AA2BAE387C8307
2 changed files with 42 additions and 26 deletions

View File

@ -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<DaoStateHash> 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<DaoStateHash> persistedDaoStateHashChain = daoStateStorageService.getPersistedDaoStateHashChain();
daoStateMonitoringService.applySnapshot(persistedDaoStateHashChain);
daoStateStorageService.releaseMemory();
}

View File

@ -180,6 +180,19 @@ public class DaoStateStorageService extends StoreService<DaoStateStore> {
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<DaoStateHash> getPersistedDaoStateHashChain() {
return store.getDaoStateHashChain();
}