Skip to content

Commit

Permalink
Add check that height of last block matches chainHeight from daoState
Browse files Browse the repository at this point in the history
  • Loading branch information
chimp1984 committed Oct 31, 2021
1 parent d46d465 commit 401054f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ public void onParseBlockChainComplete() {
int chainHeight = daoStateService.getChainHeight();
log.info("Create snapshot at height {}", chainHeight);
// We do not keep the data in our fields to enable gc as soon its released in the store
daoStateStorageService.requestPersistence(getDaoStateForSnapshot(),
getBlocksForSnapshot(),
getHashChainForSnapshot(),

protobuf.DaoState daoStateForSnapshot = getDaoStateForSnapshot();
List<Block> blocksForSnapshot = getBlocksForSnapshot();
LinkedList<DaoStateHash> hashChainForSnapshot = getHashChainForSnapshot();
daoStateStorageService.requestPersistence(daoStateForSnapshot,
blocksForSnapshot,
hashChainForSnapshot,
() -> {
GcUtil.maybeReleaseMemory();
log.info("Persisted daoState after parsing completed at height {}. Took {} ms",
Expand Down Expand Up @@ -245,8 +249,8 @@ public void applySnapshot(boolean fromReorg) {
int chainHeightOfPersisted = persistedBsqState.getChainHeight();
if (!persistedBsqState.getBlocks().isEmpty()) {
int heightOfLastBlock = persistedBsqState.getLastBlock().getHeight();
int chainHeight = persistedBsqState.getChainHeight();
checkArgument(heightOfLastBlock == chainHeight, "chainHeight must be same as heightOfLastBlock");
checkArgument(heightOfLastBlock == chainHeightOfPersisted,
"chainHeightOfPersisted must be same as heightOfLastBlock");
if (isValidHeight(heightOfLastBlock)) {
if (chainHeightOfLastApplySnapshot != chainHeightOfPersisted) {
chainHeightOfLastApplySnapshot = chainHeightOfPersisted;
Expand All @@ -269,6 +273,8 @@ public void applySnapshot(boolean fromReorg) {
"We remove all dao store files and shutdown. " +
"After a restart resource files will be applied if available.");
resyncDaoStateFromResources();
} else {
log.info("No Bsq blocks in DaoState. Expected if not data are provided yet from resources or persisted data.");
}
} else {
log.info("Try to apply snapshot but no stored snapshot available. That is expected at first blocks.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

@Slf4j
public class BlocksPersistence {
public static final int BUCKET_SIZE = 1000; // results in about 1 MB files and about 1 new file per week
Expand Down Expand Up @@ -69,7 +67,7 @@ public void writeBlocks(List<BaseBlock> protobufBlocks) {
int first = bucketIndex * BUCKET_SIZE - BUCKET_SIZE + 1;
int last = bucketIndex * BUCKET_SIZE;
File storageFile = new File(storageDir, fileName + "_" + first + "-" + last);
writeToDisk(storageFile, new BsqBlockStore(temp), null);
writeToDisk(storageFile, new BsqBlockStore(temp));
temp = new ArrayList<>();
}
}
Expand All @@ -78,7 +76,7 @@ public void writeBlocks(List<BaseBlock> protobufBlocks) {
int first = bucketIndex * BUCKET_SIZE - BUCKET_SIZE + 1;
int last = bucketIndex * BUCKET_SIZE;
File storageFile = new File(storageDir, fileName + "_" + first + "-" + last);
writeToDisk(storageFile, new BsqBlockStore(temp), null);
writeToDisk(storageFile, new BsqBlockStore(temp));

}
log.info("Write {} blocks to disk took {} msec", protobufBlocks.size(), System.currentTimeMillis() - ts);
Expand Down Expand Up @@ -130,9 +128,7 @@ private List<BaseBlock> readBucket(int bucketIndex) {
}
}

private void writeToDisk(File storageFile,
BsqBlockStore bsqBlockStore,
@Nullable Runnable completeHandler) {
private void writeToDisk(File storageFile, BsqBlockStore bsqBlockStore) {
File tempFile = null;
FileOutputStream fileOutputStream = null;
try {
Expand Down Expand Up @@ -177,9 +173,6 @@ private void writeToDisk(File storageFile,
e.printStackTrace();
log.error("Cannot close resources." + e.getMessage());
}
if (completeHandler != null) {
completeHandler.run();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

import lombok.extern.slf4j.Slf4j;

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

/**
* Manages persistence of the daoState.
*/
Expand Down Expand Up @@ -125,7 +127,11 @@ protected void readFromResources(String postFix, Runnable completeHandler) {
if (daoStateAsProto != null) {
LinkedList<Block> list;
if (daoStateAsProto.getBlocksList().isEmpty()) {
list = bsqBlocksStorageService.readBlocks(daoStateAsProto.getChainHeight());
int chainHeight = daoStateAsProto.getChainHeight();
list = bsqBlocksStorageService.readBlocks(chainHeight);
int heightOfLastBlock = list.getLast().getHeight();
checkArgument(heightOfLastBlock == chainHeight,
"heightOfLastBlock must match chainHeight");
} else {
list = bsqBlocksStorageService.migrateBlocks(daoStateAsProto.getBlocksList());
}
Expand Down

0 comments on commit 401054f

Please sign in to comment.