Skip to content

Commit

Permalink
Check if applySnapshot was called >= 3 times with the same snapshot h…
Browse files Browse the repository at this point in the history
…eight and if so call resyncDaoStateFromResources

Signed-off-by: HenrikJannsen <[email protected]>
  • Loading branch information
HenrikJannsen committed Jun 20, 2024
1 parent 7beb929 commit 449cba4
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

Expand Down Expand Up @@ -81,7 +82,7 @@ public class DaoStateSnapshotService implements DaoSetupService, DaoStateListene
private int daoRequiresRestartHandlerAttempts = 0;
private boolean readyForPersisting = true;
private boolean isParseBlockChainComplete;

private final List<Integer> heightsOfLastAppliedSnapshots = new ArrayList<>();

///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
Expand Down Expand Up @@ -278,7 +279,6 @@ public void applyPersistedSnapshot() {
applySnapshot(true);
}

//TODO check if we get repeated trys with same snapshot failing. Could be an edge case with reorgs
public void revertToLastSnapshot() {
applySnapshot(false);
}
Expand All @@ -290,6 +290,19 @@ private void applySnapshot(boolean fromInitialize) {
return;
}

int chainHeightOfPersistedDaoState = persistedDaoState.getChainHeight();
int numSameAppliedSnapshots = (int) heightsOfLastAppliedSnapshots.stream()
.filter(height -> height == chainHeightOfPersistedDaoState)
.count();
if (numSameAppliedSnapshots >= 3) {
log.warn("We got called applySnapshot the 3rd time with the same snapshot height. " +
"We abort and call resyncDaoStateFromResources.");
resyncDaoStateFromResources();
return;
}

heightsOfLastAppliedSnapshots.add(chainHeightOfPersistedDaoState);

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.");
Expand All @@ -308,7 +321,6 @@ private void applySnapshot(boolean fromInitialize) {
return;
}

int chainHeightOfPersistedDaoState = persistedDaoState.getChainHeight();
if (!isHeightAtLeastGenesisHeight(chainHeightOfPersistedDaoState)) {
log.error("heightOfPersistedLastBlock is below genesis height. This should never happen.");
return;
Expand Down

0 comments on commit 449cba4

Please sign in to comment.