Skip to content

Commit

Permalink
feat: add log for anchor-util merkel tree depth (#3184)
Browse files Browse the repository at this point in the history
* feat: add log for anchor-util merkel tree depth

* fix: linting
  • Loading branch information
3benbox authored Mar 14, 2024
1 parent e094c48 commit 38f0223
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/anchor-utils/src/merkle/merkle-tree-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,24 @@ export class MerkleTreeFactory<TData, TLeaf extends TData, TMetadata>
const metadata = this.metadataFn ? await this.metadataFn.generateMetadata(nodes) : null

const root = await this.buildLevel(nodes, 0, metadata)

const depth = this.getTreeDepth(root)
console.log(`Merkle tree generated with depth: ${depth}`)

return new MerkleTree<TData, TLeaf, TMetadata>(this.mergeFn, root, nodes, metadata)
}

private getTreeDepth(node: Node<TData>): number {
if (node.left === null && node.right === null) {
return 1
}

const leftDepth = node.left ? this.getTreeDepth(node.left) : 0
const rightDepth = node.right ? this.getTreeDepth(node.right) : 0

return Math.max(leftDepth, rightDepth) + 1
}

private async buildLevel(
elements: Node<TData>[],
currentDepth: number,
Expand Down

0 comments on commit 38f0223

Please sign in to comment.