Skip to content

Commit

Permalink
Add in getClosestPath
Browse files Browse the repository at this point in the history
  • Loading branch information
ejMina226 committed Oct 30, 2024
1 parent 1c1ab2d commit 13f61a1
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/common/src/trees/InMemoryLinkedMerkleTreeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 13f61a1

Please sign in to comment.