Skip to content

Commit 6d5bb8f

Browse files
committed
Fix normalizeMerkleBranch
1 parent b910a73 commit 6d5bb8f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/light-client/src/utils/normalizeMerkleBranch.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export const SYNC_COMMITTEES_INDEX = 11;
99
* unmodified ``branch``
1010
*/
1111
export function normalizeMerkleBranch(branch: Uint8Array[], depth: number): Uint8Array[] {
12-
const numBytes = Math.floor(branch.length / 8);
13-
const numExtraBytesRequired = depth - numBytes;
12+
const numExtraDepth = depth - branch.length;
1413

15-
return [...Array.from({length: numExtraBytesRequired}, () => ZERO_HASH), ...branch];
14+
return [...Array.from({length: numExtraDepth}, () => ZERO_HASH), ...branch];
1615
}

packages/light-client/test/unit/utils.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {describe, it, expect} from "vitest";
22
import {isValidMerkleBranch} from "../../src/utils/verifyMerkleBranch.js";
33
import {computeMerkleBranch} from "../utils/utils.js";
4+
import {normalizeMerkleBranch} from "../../src/utils/normalizeMerkleBranch.js";
5+
import {ZERO_HASH} from "../../src/spec/utils.js";
46

57
describe("utils", () => {
68
it("constructMerkleBranch", () => {
@@ -11,4 +13,18 @@ describe("utils", () => {
1113

1214
expect(isValidMerkleBranch(leaf, proof, depth, index, root)).toBe(true);
1315
});
16+
it("normalizeMerkleBranch", () => {
17+
const branch: Uint8Array[] = [];
18+
const branchDepth = 5;
19+
const newDepth = 7;
20+
21+
for (let i = 0; i < branchDepth; i++) {
22+
branch.push(new Uint8Array(Array.from({length: 32}, () => i)));
23+
}
24+
25+
const normalizedBranch = normalizeMerkleBranch(branch, newDepth);
26+
const expectedNormalizedBranch = [ZERO_HASH, ZERO_HASH, ...branch];
27+
28+
expect(normalizedBranch).toEqual(expectedNormalizedBranch);
29+
});
1430
});

0 commit comments

Comments
 (0)