-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree_base.ts
300 lines (267 loc) · 8.3 KB
/
tree_base.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// import { LevelUp, LevelUpChain } from 'levelup';
import { ChainedBatch, Level } from "level";
import { BaseSiblingPath, SiblingPath } from './types/index.js';
import { Hasher } from './types/index.js';
import { IMerkleTree } from './interfaces/merkle_tree.js';
import { toBigIntLE, toBufferLE, bufferToInt256, int256ToBuffer } from '../utils.js';
import { Field } from 'o1js';
const MAX_DEPTH = 254;
/**
* cache key as a index
* @param name
* @param level
* @param index
* @returns
*/
const indexToKeyHash = (name: string, level: number, index: bigint) =>
`${name}:${level}:${index}`;
const encodeMeta = (root: Field, depth: number, size: bigint) => {
const rootBuf = int256ToBuffer(root.toBigInt()); // 32-bytes buffer
const data = Buffer.alloc(32 + 4);
rootBuf.copy(data);
data.writeUInt32LE(depth, 32);
return Buffer.concat([data, toBufferLE(size, 32)]);
};
export const decodeMeta = (meta: Buffer) => {
const root = Field(bufferToInt256(meta.subarray(0, 32)));
const depth = meta.readUInt32LE(32);
const size = toBigIntLE(meta.subarray(36));
return {
root,
depth,
size,
};
};
export const INITIAL_LEAF = Field(0);
/**
* A Merkle tree implementation that uses a LevelDB database to store the tree.
*/
export abstract class TreeBase implements IMerkleTree {
protected readonly maxIndex: bigint;
protected cachedSize?: bigint;
private root!: Field;
private zeroHashes: Field[] = [];
private cache: { [key: string]: Buffer } = {};
public constructor(
protected db: Level<string, Buffer>,
protected hasher: Hasher,
private name: string,
private depth: number,
protected size: bigint = 0n,
root?: Field
) {
if (!(depth >= 1 && depth <= MAX_DEPTH)) {
throw Error('Invalid depth');
}
// Compute the zero values at each layer.
let current = INITIAL_LEAF;
for (let i = depth - 1; i >= 0; --i) {
this.zeroHashes[i] = current;
current = hasher.compress(current, current);
}
this.root = root ? root : current;
this.maxIndex = 2n ** BigInt(depth) - 1n;
}
/**
* Returns the root of the tree.
* @param includeUncommitted - If true, root incorporating uncomitted changes is returned.
* @returns The root of the tree.
*/
public getRoot(includeUncommitted: boolean): Field {
if (!includeUncommitted) {
return this.root;
} else {
let tmpRootBuffer = this.cache[indexToKeyHash(this.name, 0, 0n)];
if (tmpRootBuffer) {
return Field(tmpRootBuffer.toString());
} else {
return this.root;
}
}
}
/**
* Returns the number of leaves in the tree.
* @param includeUncommitted - If true, the returned number of leaves includes uncomitted changes.
* @returns The number of leaves in the tree.
*/
public getNumLeaves(includeUncommitted: boolean) {
return !includeUncommitted ? this.size : this.cachedSize ?? this.size;
}
/**
* Returns the name of the tree.
* @returns The name of the tree.
*/
public getName(): string {
return this.name;
}
/**
* Returns the depth of the tree.
* @returns The depth of the tree.
*/
public getDepth(): number {
return this.depth;
}
/**
* Returns a sibling path for the element at the given index.
* @param index - The index of the element.
* @param includeUncommitted - Indicates whether to get a sibling path incorporating uncommitted changes.
* @returns A sibling path for the element at the given index.
* Note: The sibling path is an array of sibling hashes, with the lowest hash (leaf hash) first, and the highest hash last.
*/
public async getSiblingPath(
index: bigint,
includeUncommitted: boolean
): Promise<BaseSiblingPath> {
const path: Field[] = [];
let level = this.depth;
while (level > 0) {
const isRight = index & 0x01n;
const sibling = await this.getLatestValueAtIndex(
level,
isRight ? index - 1n : index + 1n,
includeUncommitted
);
path.push(sibling);
level -= 1;
index >>= 1n;
}
class SiblingPath_ extends SiblingPath(this.depth) {}
return new SiblingPath_(path);
}
/**
* Commits the changes to the database.
* @returns Empty promise.
*/
public async commit(): Promise<void> {
const batch = this.db.batch();
const keys = Object.getOwnPropertyNames(this.cache);
for (const key of keys) {
batch.put(key, this.cache[key]);
}
await this.writeMeta(batch);
await batch.write();
this.size = this.getNumLeaves(true);
this.root = this.getRoot(true);
this.clearCache();
}
/**
* Rolls back the not-yet-committed changes.
* @returns Empty promise.
*/
public rollback(): Promise<void> {
this.clearCache();
return Promise.resolve();
}
/**
* Gets the value at the given index.
* @param index - The index of the leaf.
* @param includeUncommitted - Indicates whether to include uncommitted changes.
* @returns Leaf value at the given index or undefined.
*/
public getLeafValue(
index: bigint,
includeUncommitted: boolean
): Promise<Field | undefined> {
return this.getLatestValueAtIndex(this.depth, index, includeUncommitted);
}
/**
* Clears the cache.
*/
private clearCache() {
this.cache = {};
this.cachedSize = undefined;
}
/**
* Adds a leaf and all the hashes above it to the cache.
* @param leaf - Leaf to add to cache.
* @param index - Index of the leaf (used to derive the cache key).
*/
protected async addLeafToCacheAndHashToRoot(leaf: Field, index: bigint) {
const key = indexToKeyHash(this.name, this.depth, index);
let current = leaf;
this.cache[key] = Buffer.from(current.toString());
let level = this.depth;
while (level > 0) {
const isRight = index & 0x01n;
const sibling = await this.getLatestValueAtIndex(
level,
isRight ? index - 1n : index + 1n,
true
);
const lhs = isRight ? sibling : current;
const rhs = isRight ? current : sibling;
current = this.hasher.compress(lhs, rhs);
level -= 1;
index >>= 1n;
const cacheKey = indexToKeyHash(this.name, level, index);
this.cache[cacheKey] = Buffer.from(current.toString());
}
}
/**
* Returns the latest value at the given index.
* @param level - The level of the tree.
* @param index - The index of the element.
* @param includeUncommitted - Indicates, whether to get include uncomitted changes.
* @returns The latest value at the given index.
* Note: If the value is not in the cache, it will be fetched from the database.
*/
private async getLatestValueAtIndex(
level: number,
index: bigint,
includeUncommitted: boolean
): Promise<Field> {
const key = indexToKeyHash(this.name, level, index);
if (includeUncommitted && this.cache[key] !== undefined) {
return Field(this.cache[key].toString());
}
const committed = await this.dbGet(key);
if (committed !== undefined) {
return committed;
}
return this.zeroHashes[level - 1];
}
/**
* Gets a value from db by key.
* @param key - The key to by which to get the value.
* @returns A value from the db based on the key.
*/
private async dbGet(key: string): Promise<Field | undefined> {
const buf = await this.db.get(key).catch(() => {});
if (buf !== undefined) {
return Field(buf.toString());
}
return undefined;
}
/**
* Initializes the tree.
* @param prefilledSize - A number of leaves that are prefilled with values.
* @returns Empty promise.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async init(prefilledSize: number): Promise<void> {
// prefilledSize is used only by Indexed Tree.
await this.writeMeta();
}
/**
* Initializes the tree from the database.
*/
public async initFromDb(): Promise<void> {
// Implemented only by Inedexed Tree to populate the leaf cache.
}
/**
* Writes meta data to the provided batch.
* @param batch - The batch to which to write the meta data.
*/
protected async writeMeta(batch?: ChainedBatch<Level<string, Buffer>, string, Buffer>) {
const data = encodeMeta(
this.getRoot(true),
this.depth,
this.getNumLeaves(true)
);
if (batch) {
batch.put(this.name, data);
} else {
await this.db.put(this.name, data);
}
}
}