Skip to content

Commit

Permalink
merkle: optimization, isZeroTree now requires tree level to eliminate…
Browse files Browse the repository at this point in the history
… searching array
  • Loading branch information
kaxxa123 committed Sep 10, 2024
1 parent 42e3b86 commit 1d0edd2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion merkle/src/draw_merkle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class TreeDisplay extends SMT {
//
// buffer - buffer to which the subtree is to be written.
_drawTreeLevel(parent: string, level: number, horizIdx: number, totalwidth: number, buffer: Buffer) {
if (this.isZeroTree(parent)) {
if (this.isZeroTree(parent, level)) {
this._drawNode(" 0 ", level, horizIdx, totalwidth, buffer);

if (level < this.LEVELS_TOTAL()) {
Expand Down
9 changes: 5 additions & 4 deletions merkle/src/sparse_merkle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export class SMT {
// Returns
// Array of hashes of all possible zero subtrees
// starting from a zero root and ending with a
// zero leaf.
// zero leaf. Returned array will be
// (LEVELS_TOTAL+1) long.
private _computeZeroTree(): string[] {
const cache = []
let lastHash = this.HASH_ZERO();
Expand All @@ -160,8 +161,8 @@ export class SMT {
//
// Returns
// true if the hash is one of the zero subtree hashes
isZeroTree(hash: string): boolean {
return (this._hashZeroTree.indexOf(hash) != -1);
isZeroTree(hash: string, level: number): boolean {
return (this._hashZeroTree[level] == hash);
}

// Get leaf lowest index/address.
Expand Down Expand Up @@ -203,7 +204,7 @@ export class SMT {
// Read adjecent nodes
for (let pos = 0; pos < this.LEVELS_TOTAL(); ++pos) {

zero = zero || (this.isZeroTree(node));
zero = zero || (this.isZeroTree(node, pos));
if (zero) {
node = this.HASH_ZERO_TREE(pos + 1);
toRead.push(node);
Expand Down

0 comments on commit 1d0edd2

Please sign in to comment.