import { describe, expect, it, } from "vitest"; import { Node, } from "./node.js"; import { NodeKey, } from "./node-key.js"; import { stringToBytes, } from "./utils/bytes.js"; import { decodeBytes, decodeVarint, } from "./utils/encoding.js"; describe("Node - Hash Computation", () => { describe("computeHash", () => { describe("leaf nodes", () => { it("should compute hash for leaf node", async () => { const node = Node.newLeafNode(stringToBytes("test-key"), stringToBytes("test-value")); const hash = node.computeHash(1n); expect(hash).toBeInstanceOf(Uint8Array); expect(hash.length).toBe(32); expect(node.hash).toBe(hash); // Should be cached }); it("should return cached hash on subsequent calls", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); const hash1 = node.computeHash(1n); const hash2 = node.computeHash(1n); expect(hash1).toBe(hash2); // Same reference }); it("should produce different hashes for different versions", async () => { const node1 = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); const node2 = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); const hash1 = node1.computeHash(1n); const hash2 = node2.computeHash(2n); expect(hash1).not.toEqual(hash2); }); it("should produce different hashes for different keys", async () => { const node1 = Node.newLeafNode(stringToBytes("key1"), stringToBytes("value")); const node2 = Node.newLeafNode(stringToBytes("key2"), stringToBytes("value")); const hash1 = node1.computeHash(1n); const hash2 = node2.computeHash(1n); expect(hash1).not.toEqual(hash2); }); it("should produce different hashes for different values", async () => { const node1 = Node.newLeafNode(stringToBytes("key"), stringToBytes("value1")); const node2 = Node.newLeafNode(stringToBytes("key"), stringToBytes("value2")); const hash1 = node1.computeHash(1n); const hash2 = node2.computeHash(1n); expect(hash1).not.toEqual(hash2); }); it("should throw if leaf node missing key", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.key = null; expect(() => node.computeHash(1n)).toThrow("Leaf node must have key"); }); it("should throw if leaf node missing value", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.value = null; expect(() => node.computeHash(1n)).toThrow("Leaf node must have value"); }); }); describe("branch nodes", () => { it("should compute hash for branch node", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); // Compute child hashes first left.computeHash(1n); right.computeHash(1n); const branch = Node.newBranchNode(left, right); const hash = branch.computeHash(1n); expect(hash).toBeInstanceOf(Uint8Array); expect(hash.length).toBe(32); expect(branch.hash).toBe(hash); // Should be cached }); it("should produce different hashes for different child hashes", async () => { const left1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right1 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); left1.computeHash(1n); right1.computeHash(1n); const branch1 = Node.newBranchNode(left1, right1); const left2 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("3")); // Different value left2.computeHash(1n); right2.computeHash(1n); const branch2 = Node.newBranchNode(left2, right2); const hash1 = branch1.computeHash(1n); const hash2 = branch2.computeHash(1n); expect(hash1).not.toEqual(hash2); }); it("should throw if branch node missing left child", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); branch.leftNode = null; expect(() => branch.computeHash(1n)).toThrow("Branch node must have both children loaded"); }); it("should throw if branch node missing right child", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); branch.rightNode = null; expect(() => branch.computeHash(1n)).toThrow("Branch node must have both children loaded"); }); it("should throw if left child hash not computed", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); right.computeHash(1n); const branch = Node.newBranchNode(left, right); expect(() => branch.computeHash(1n)).toThrow("Left child hash must be computed before parent"); }); it("should throw if right child hash not computed", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); left.computeHash(1n); const branch = Node.newBranchNode(left, right); expect(() => branch.computeHash(1n)).toThrow("Right child hash must be computed before parent"); }); }); describe("hash format", () => { it("should hash leaves in correct format (height || size || version || key || valueHash)", async () => { // Create a simple leaf to verify the hash format const node = Node.newLeafNode( new Uint8Array([0x61]), // "a" new Uint8Array([0x31]), // "1" ); const hash = node.computeHash(1n); // We can't verify exact hash without Go reference, // but we can verify it's deterministic const hash2 = node.computeHash(1n); expect(hash).toEqual(hash2); }); it("should hash branches in correct format (height || size || version || leftHash || rightHash)", async () => { const left = Node.newLeafNode( new Uint8Array([0x61]), new Uint8Array([0x31]), ); const right = Node.newLeafNode( new Uint8Array([0x62]), new Uint8Array([0x32]), ); left.computeHash(1n); right.computeHash(1n); const branch = Node.newBranchNode(left, right); const hash = branch.computeHash(1n); // Verify deterministic const hash2 = branch.computeHash(1n); expect(hash).toEqual(hash2); }); }); describe("multi-level trees", () => { it("should compute hashes for multi-level tree", async () => { // Create a 3-level tree const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const leaf3 = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); const leaf4 = Node.newLeafNode(stringToBytes("d"), stringToBytes("4")); // Compute leaf hashes leaf1.computeHash(1n); leaf2.computeHash(1n); leaf3.computeHash(1n); leaf4.computeHash(1n); // Create branches const branch1 = Node.newBranchNode(leaf1, leaf2); const branch2 = Node.newBranchNode(leaf3, leaf4); branch1.computeHash(1n); branch2.computeHash(1n); // Create root const root = Node.newBranchNode(branch1, branch2); const rootHash = root.computeHash(1n); expect(rootHash).toBeInstanceOf(Uint8Array); expect(rootHash.length).toBe(32); expect(root.hash).toBe(rootHash); }); }); }); }); describe("Node - Serialization", () => { describe("toBytes / fromBytes", () => { describe("leaf nodes", () => { it("should serialize and deserialize leaf node", async () => { const node = Node.newLeafNode(stringToBytes("test-key"), stringToBytes("test-value")); node.nodeKey = new NodeKey(1n, 2); const bytes = node.toBytes(); const restored = Node.fromBytes(node.nodeKey.toBytes(), bytes); expect(restored.key).toEqual(node.key); expect(restored.value).toEqual(node.value); expect(restored.subtreeHeight).toBe(node.subtreeHeight); expect(restored.size).toBe(node.size); expect(restored.nodeKey?.version).toBe(node.nodeKey.version); expect(restored.nodeKey?.nonce).toBe(node.nodeKey.nonce); expect(restored.isLeaf()).toBe(true); }); it("should compute hash after deserialization", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = new NodeKey(1n, 1); node.computeHash(1n); const bytes = node.toBytes(); const restored = Node.fromBytes(node.nodeKey.toBytes(), bytes); // Hash should be computed during deserialization expect(restored.hash).not.toBeNull(); expect(restored.hash).toEqual(node.hash); }); it("should throw if leaf node missing key", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.key = null; expect(() => node.toBytes()).toThrow("Node must have key for serialization"); }); it("should throw if leaf node missing value", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.value = null; expect(() => node.toBytes()).toThrow("Leaf node must have value for serialization"); }); }); describe("branch nodes", () => { it("should serialize and deserialize branch node with v1 children", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); left.nodeKey = new NodeKey(1n, 2); right.nodeKey = new NodeKey(1n, 3); left.computeHash(1n); right.computeHash(1n); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = left.nodeKey.toBytes(); branch.rightNodeKey = right.nodeKey.toBytes(); branch.computeHash(1n); const bytes = branch.toBytes(); const restored = Node.fromBytes(branch.nodeKey.toBytes(), bytes); expect(restored.key).toEqual(branch.key); expect(restored.subtreeHeight).toBe(branch.subtreeHeight); expect(restored.size).toBe(branch.size); expect(restored.hash).toEqual(branch.hash); expect(restored.leftNodeKey).toEqual(branch.leftNodeKey); expect(restored.rightNodeKey).toEqual(branch.rightNodeKey); expect(restored.isLeaf()).toBe(false); }); it("should serialize and deserialize branch node with legacy children", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); left.nodeKey = new NodeKey(1n, 2); right.nodeKey = new NodeKey(1n, 3); left.computeHash(1n); right.computeHash(1n); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); // Use legacy hash-based keys (32 bytes) branch.leftNodeKey = new Uint8Array(32).fill(0xAA); branch.rightNodeKey = new Uint8Array(32).fill(0xBB); branch.computeHash(1n); const bytes = branch.toBytes(); const restored = Node.fromBytes(branch.nodeKey.toBytes(), bytes); expect(restored.key).toEqual(branch.key); expect(restored.leftNodeKey).toEqual(branch.leftNodeKey); expect(restored.rightNodeKey).toEqual(branch.rightNodeKey); }); it("should serialize and deserialize branch node with mixed children", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); left.nodeKey = new NodeKey(1n, 2); right.nodeKey = new NodeKey(1n, 3); left.computeHash(1n); right.computeHash(1n); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); // Left is legacy, right is v1 branch.leftNodeKey = new Uint8Array(32).fill(0xAA); branch.rightNodeKey = right.nodeKey.toBytes(); branch.computeHash(1n); const bytes = branch.toBytes(); const restored = Node.fromBytes(branch.nodeKey.toBytes(), bytes); expect(restored.leftNodeKey).toEqual(branch.leftNodeKey); expect(restored.rightNodeKey).toEqual(branch.rightNodeKey); }); it("should throw if branch node missing hash", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = new NodeKey(1n, 2).toBytes(); branch.rightNodeKey = new NodeKey(1n, 3).toBytes(); expect(() => branch.toBytes()).toThrow("Branch node must have hash for serialization"); }); it("should throw if branch node missing left child key", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); left.computeHash(1n); right.computeHash(1n); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.computeHash(1n); branch.rightNodeKey = new NodeKey(1n, 3).toBytes(); expect(() => branch.toBytes()).toThrow("Branch node must have leftNodeKey for serialization"); }); it("should throw if branch node missing right child key", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); left.computeHash(1n); right.computeHash(1n); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.computeHash(1n); branch.leftNodeKey = new NodeKey(1n, 2).toBytes(); expect(() => branch.toBytes()).toThrow("Branch node must have rightNodeKey for serialization"); }); }); describe("round-trip", () => { it("should handle round-trip for complex tree", async () => { // Create a 2-level tree const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const leaf3 = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); const leaf4 = Node.newLeafNode(stringToBytes("d"), stringToBytes("4")); leaf1.nodeKey = new NodeKey(1n, 4); leaf2.nodeKey = new NodeKey(1n, 5); leaf3.nodeKey = new NodeKey(1n, 6); leaf4.nodeKey = new NodeKey(1n, 7); leaf1.computeHash(1n); leaf2.computeHash(1n); leaf3.computeHash(1n); leaf4.computeHash(1n); const branch1 = Node.newBranchNode(leaf1, leaf2); const branch2 = Node.newBranchNode(leaf3, leaf4); branch1.nodeKey = new NodeKey(1n, 2); branch2.nodeKey = new NodeKey(1n, 3); branch1.leftNodeKey = leaf1.nodeKey.toBytes(); branch1.rightNodeKey = leaf2.nodeKey.toBytes(); branch2.leftNodeKey = leaf3.nodeKey.toBytes(); branch2.rightNodeKey = leaf4.nodeKey.toBytes(); branch1.computeHash(1n); branch2.computeHash(1n); const root = Node.newBranchNode(branch1, branch2); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = branch1.nodeKey.toBytes(); root.rightNodeKey = branch2.nodeKey.toBytes(); root.computeHash(1n); // Serialize and deserialize each node const bytes1 = leaf1.toBytes(); const bytes2 = leaf2.toBytes(); const bytes3 = leaf3.toBytes(); const bytes4 = leaf4.toBytes(); const branchBytes1 = branch1.toBytes(); const branchBytes2 = branch2.toBytes(); const rootBytes = root.toBytes(); const restored1 = Node.fromBytes(leaf1.nodeKey.toBytes(), bytes1); const restored2 = Node.fromBytes(leaf2.nodeKey.toBytes(), bytes2); const restored3 = Node.fromBytes(leaf3.nodeKey.toBytes(), bytes3); const restored4 = Node.fromBytes(leaf4.nodeKey.toBytes(), bytes4); const restoredBranch1 = Node.fromBytes(branch1.nodeKey.toBytes(), branchBytes1); const restoredBranch2 = Node.fromBytes(branch2.nodeKey.toBytes(), branchBytes2); const restoredRoot = Node.fromBytes(root.nodeKey.toBytes(), rootBytes); // Verify all nodes restored correctly expect(restored1.hash).toEqual(leaf1.hash); expect(restored2.hash).toEqual(leaf2.hash); expect(restored3.hash).toEqual(leaf3.hash); expect(restored4.hash).toEqual(leaf4.hash); expect(restoredBranch1.hash).toEqual(branch1.hash); expect(restoredBranch2.hash).toEqual(branch2.hash); expect(restoredRoot.hash).toEqual(root.hash); }); }); describe("error handling", () => { it("should throw on invalid height during deserialization", async () => { const bytes = new Uint8Array([0xFF, 0x7F]); // Height = 255 (out of int8 range) expect(() => Node.fromBytes(new NodeKey(1n, 1).toBytes(), bytes)) .toThrow("Invalid height"); }); it("should throw on invalid mode during deserialization", () => { const node = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); node.computeHash(1n); right.computeHash(1n); const branch = Node.newBranchNode(node, right); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = new NodeKey(1n, 2).toBytes(); branch.rightNodeKey = new NodeKey(1n, 3).toBytes(); branch.computeHash(1n); const bytes = branch.toBytes(); // Use the decoding functions to find the exact position of the mode byte let offset = 0; // Skip height const heightResult = decodeVarint(bytes, offset); offset += heightResult.bytesRead; // Skip size const sizeResult = decodeVarint(bytes, offset); offset += sizeResult.bytesRead; // Skip key const keyResult = decodeBytes(bytes, offset); offset += keyResult.bytesRead; // Skip hash const hashResult = decodeBytes(bytes, offset); offset += hashResult.bytesRead; // Now we're at the mode byte const modeOffset = offset; // Corrupt the mode byte to invalid value 4 bytes[modeOffset] = 4; // Invalid mode (valid values are 0-3) // Expect error - either "Invalid mode" or a deserialization error due to corruption expect(() => Node.fromBytes(branch.nodeKey.toBytes(), bytes)) .toThrow(); }); }); }); });