-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathstandard_tree.ts
49 lines (43 loc) · 1.66 KB
/
standard_tree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Timer } from '@aztec/foundation/timer';
import { TreeInsertionStats } from '@aztec/types/stats';
import { AppendOnlySnapshotBuilder, TreeSnapshot } from '../index.js';
import { AppendOnlyTree } from '../interfaces/append_only_tree.js';
import { TreeBase } from '../tree_base.js';
/**
* A Merkle tree implementation that uses a LevelDB database to store the tree.
*/
export class StandardTree extends TreeBase implements AppendOnlyTree {
#snapshotBuilder = new AppendOnlySnapshotBuilder(this.db, this, this.hasher);
/**
* Appends the given leaves to the tree.
* @param leaves - The leaves to append.
* @returns Empty promise.
*/
public async appendLeaves(leaves: Buffer[]): Promise<void> {
const timer = new Timer();
await super.appendLeaves(leaves);
this.log(`Inserted ${leaves.length} leaves into ${this.getName()} tree`, {
eventName: 'tree-insertion',
duration: timer.ms(),
batchSize: leaves.length,
treeName: this.getName(),
treeDepth: this.getDepth(),
treeType: 'append-only',
} satisfies TreeInsertionStats);
}
public snapshot(block: number): Promise<TreeSnapshot> {
return this.#snapshotBuilder.snapshot(block);
}
public getSnapshot(block: number): Promise<TreeSnapshot> {
return this.#snapshotBuilder.getSnapshot(block);
}
public async findLeafIndex(value: Buffer, includeUncommitted: boolean): Promise<bigint | undefined> {
for (let i = 0n; i < this.getNumLeaves(includeUncommitted); i++) {
const currentValue = await this.getLeafValue(i, includeUncommitted);
if (currentValue && currentValue.equals(value)) {
return i;
}
}
return undefined;
}
}