Skip to content

Commit

Permalink
Roll back merkle tree if note size mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
dgca committed Sep 13, 2023
1 parent d8210d9 commit 738a832
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 14 additions & 3 deletions example/src/Client/utils/BlockProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
LightBlock,
LightStreamerClient,
} from "../../../../src/models/lightstreamer";
import { addNotesToMerkleTree, revertToNoteSize } from "./merkle";
import {
addNotesToMerkleTree,
getNotesTreeSize,
revertToNoteSize,
} from "./merkle";
import { logThrottled } from "./logThrottled";
import { AccountsManager } from "./AccountsManager";

Expand Down Expand Up @@ -158,8 +162,6 @@ export class BlockProcessor {
return;
}

await this.blockCache.cacheBlock(block);

const notes: Buffer[] = [];

for (const transaction of block.transactions) {
Expand All @@ -168,7 +170,16 @@ export class BlockProcessor {
}
}

const prevBlockNoteSize = block.noteSize - notes.length;
const notesTreeSize = await getNotesTreeSize();

if (notesTreeSize > prevBlockNoteSize) {
await revertToNoteSize(prevBlockNoteSize);
}

await addNotesToMerkleTree(notes);

await this.blockCache.cacheBlock(block);
}

private async _checkForReorg(block: LightBlock) {
Expand Down
2 changes: 2 additions & 0 deletions example/src/Client/utils/merkle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ export const addNotesToMerkleTree = async (notes: Buffer[]) => {
export const revertToNoteSize = async (noteSize: number) => {
await notesTree.truncate(noteSize);
};

export const getNotesTreeSize = () => notesTree.size();

0 comments on commit 738a832

Please sign in to comment.