Skip to content

Commit

Permalink
fix block disconnect and update rollback logic (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
jowparks authored Apr 8, 2024
1 parent 9883a45 commit f1a5af7
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,32 @@ export class LightBlockCache {
this.cacheBlock(block);
} else if (content.type === "disconnected") {
logger.warn(`Removing block ${content.block.sequence}...`);
await this.db.put("head", content.block.previousBlockHash);
await this.db.put(
"headSequence",
(content.block.sequence - 1).toString(),
);
await this.db.del(content.block.sequence);
await this.db.del(content.block.hash);
const block = lightBlock(content);
await this.db.put("head", block.hash);
await this.db.put("headSequence", (block.sequence - 1).toString());
await this.db.del(block.sequence);
await this.db.del(block.hash);
}
}
}
}

private async rollbackHead(): Promise<void> {
const head = await this.getHeadSequence();
let head = await this.getHeadSequence();
if (!head) {
logger.error("Head sequence is not set. Cannot rollback.");
return;
}
const rollbackHead = head - 1;
await this.db.put("head", rollbackHead.toString());
logger.info(`Rolled back head to block sequence ${rollbackHead}`);
let block = null;
while (!block) {
block = await this.getBlockBySequence(head);
if (!block) {
head -= 1;
}
}
await this.db.put("headSequence", head.toString());
await this.db.put("head", block.hash);
logger.info(`Rolled back head to block sequence ${head}`);
}

async cacheBlock(block: LightBlock): Promise<void> {
Expand Down

0 comments on commit f1a5af7

Please sign in to comment.