Skip to content

Commit

Permalink
Fix normalizeMerkleBranch
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 committed Sep 9, 2024
1 parent b910a73 commit 6d5bb8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/light-client/src/utils/normalizeMerkleBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export const SYNC_COMMITTEES_INDEX = 11;
* unmodified ``branch``
*/
export function normalizeMerkleBranch(branch: Uint8Array[], depth: number): Uint8Array[] {
const numBytes = Math.floor(branch.length / 8);
const numExtraBytesRequired = depth - numBytes;
const numExtraDepth = depth - branch.length;

return [...Array.from({length: numExtraBytesRequired}, () => ZERO_HASH), ...branch];
return [...Array.from({length: numExtraDepth}, () => ZERO_HASH), ...branch];
}
16 changes: 16 additions & 0 deletions packages/light-client/test/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {describe, it, expect} from "vitest";
import {isValidMerkleBranch} from "../../src/utils/verifyMerkleBranch.js";
import {computeMerkleBranch} from "../utils/utils.js";
import {normalizeMerkleBranch} from "../../src/utils/normalizeMerkleBranch.js";
import {ZERO_HASH} from "../../src/spec/utils.js";

describe("utils", () => {
it("constructMerkleBranch", () => {
Expand All @@ -11,4 +13,18 @@ describe("utils", () => {

expect(isValidMerkleBranch(leaf, proof, depth, index, root)).toBe(true);
});
it("normalizeMerkleBranch", () => {
const branch: Uint8Array[] = [];
const branchDepth = 5;
const newDepth = 7;

for (let i = 0; i < branchDepth; i++) {
branch.push(new Uint8Array(Array.from({length: 32}, () => i)));
}

const normalizedBranch = normalizeMerkleBranch(branch, newDepth);
const expectedNormalizedBranch = [ZERO_HASH, ZERO_HASH, ...branch];

expect(normalizedBranch).toEqual(expectedNormalizedBranch);
});
});

0 comments on commit 6d5bb8f

Please sign in to comment.