From 13f61a1dc7b3821f644f9cadc990c1c79249344d Mon Sep 17 00:00:00 2001 From: ejMina226 <118474890+ejMina226@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:24:42 +0000 Subject: [PATCH] Add in getClosestPath --- .../src/trees/InMemoryLinkedMerkleTreeStorage.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/common/src/trees/InMemoryLinkedMerkleTreeStorage.ts b/packages/common/src/trees/InMemoryLinkedMerkleTreeStorage.ts index 07d1f146..54ec04df 100644 --- a/packages/common/src/trees/InMemoryLinkedMerkleTreeStorage.ts +++ b/packages/common/src/trees/InMemoryLinkedMerkleTreeStorage.ts @@ -23,6 +23,19 @@ export class InMemoryLinkedMerkleTreeStorage implements LinkedMerkleTreeStore { return this.leaves[index.toString()]; } + // This gets the leaf with the closest path. + public getClosestPath(path: number): LinkedLeaf { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + let largestLeaf = this.getLeaf(0n) as LinkedLeaf; + while (largestLeaf.nextPath < path) { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + const nextIndex = this.getLeafIndex(largestLeaf.nextPath) as bigint; + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions + largestLeaf = this.getLeaf(nextIndex) as LinkedLeaf; + } + return largestLeaf; + } + public setLeaf(index: bigint, value: LinkedLeaf): void { this.leaves[index.toString()] = value; }