@aztec-labs/world-state 6.0.0-nightly.20260829
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -0
- package/dest/index.d.ts +6 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +5 -0
- package/dest/instrumentation/instrumentation.d.ts +25 -0
- package/dest/instrumentation/instrumentation.d.ts.map +1 -0
- package/dest/instrumentation/instrumentation.js +96 -0
- package/dest/native/bench_metrics.d.ts +23 -0
- package/dest/native/bench_metrics.d.ts.map +1 -0
- package/dest/native/bench_metrics.js +81 -0
- package/dest/native/fork_checkpoint.d.ts +16 -0
- package/dest/native/fork_checkpoint.d.ts.map +1 -0
- package/dest/native/fork_checkpoint.js +38 -0
- package/dest/native/index.d.ts +4 -0
- package/dest/native/index.d.ts.map +1 -0
- package/dest/native/index.js +3 -0
- package/dest/native/ipc_world_state_instance.d.ts +91 -0
- package/dest/native/ipc_world_state_instance.d.ts.map +1 -0
- package/dest/native/ipc_world_state_instance.js +672 -0
- package/dest/native/merkle_trees_facade.d.ts +58 -0
- package/dest/native/merkle_trees_facade.d.ts.map +1 -0
- package/dest/native/merkle_trees_facade.js +250 -0
- package/dest/native/message.d.ts +145 -0
- package/dest/native/message.d.ts.map +1 -0
- package/dest/native/message.js +150 -0
- package/dest/native/native_world_state.d.ts +77 -0
- package/dest/native/native_world_state.d.ts.map +1 -0
- package/dest/native/native_world_state.js +350 -0
- package/dest/native/native_world_state_instance.d.ts +63 -0
- package/dest/native/native_world_state_instance.d.ts.map +1 -0
- package/dest/native/native_world_state_instance.js +1 -0
- package/dest/native/world_state_operation.d.ts +7 -0
- package/dest/native/world_state_operation.d.ts.map +1 -0
- package/dest/native/world_state_operation.js +5 -0
- package/dest/synchronizer/config.d.ts +31 -0
- package/dest/synchronizer/config.d.ts.map +1 -0
- package/dest/synchronizer/config.js +61 -0
- package/dest/synchronizer/errors.d.ts +11 -0
- package/dest/synchronizer/errors.d.ts.map +1 -0
- package/dest/synchronizer/errors.js +12 -0
- package/dest/synchronizer/factory.d.ts +20 -0
- package/dest/synchronizer/factory.d.ts.map +1 -0
- package/dest/synchronizer/factory.js +30 -0
- package/dest/synchronizer/index.d.ts +4 -0
- package/dest/synchronizer/index.d.ts.map +1 -0
- package/dest/synchronizer/index.js +3 -0
- package/dest/synchronizer/server_world_state_synchronizer.d.ts +83 -0
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -0
- package/dest/synchronizer/server_world_state_synchronizer.js +434 -0
- package/dest/test/index.d.ts +2 -0
- package/dest/test/index.d.ts.map +1 -0
- package/dest/test/index.js +1 -0
- package/dest/test/utils.d.ts +35 -0
- package/dest/test/utils.d.ts.map +1 -0
- package/dest/test/utils.js +118 -0
- package/dest/testing.d.ts +11 -0
- package/dest/testing.d.ts.map +1 -0
- package/dest/testing.js +48 -0
- package/dest/world-state-db/index.d.ts +3 -0
- package/dest/world-state-db/index.d.ts.map +1 -0
- package/dest/world-state-db/index.js +1 -0
- package/dest/world-state-db/merkle_tree_db.d.ts +65 -0
- package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -0
- package/dest/world-state-db/merkle_tree_db.js +17 -0
- package/package.json +98 -0
- package/src/index.ts +5 -0
- package/src/instrumentation/instrumentation.ts +150 -0
- package/src/native/bench_metrics.ts +91 -0
- package/src/native/fork_checkpoint.ts +46 -0
- package/src/native/index.ts +3 -0
- package/src/native/ipc_world_state_instance.ts +849 -0
- package/src/native/merkle_trees_facade.ts +375 -0
- package/src/native/message.ts +287 -0
- package/src/native/native_world_state.ts +435 -0
- package/src/native/native_world_state_instance.ts +104 -0
- package/src/native/world_state_operation.ts +33 -0
- package/src/synchronizer/config.ts +106 -0
- package/src/synchronizer/errors.ts +13 -0
- package/src/synchronizer/factory.ts +83 -0
- package/src/synchronizer/index.ts +3 -0
- package/src/synchronizer/server_world_state_synchronizer.ts +525 -0
- package/src/test/index.ts +1 -0
- package/src/test/utils.ts +184 -0
- package/src/testing.ts +65 -0
- package/src/world-state-db/index.ts +3 -0
- package/src/world-state-db/merkle_tree_db.ts +80 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { BlockNumber } from '@aztec-labs/foundation/branded-types';
|
|
2
|
+
import { Fr } from '@aztec-labs/foundation/curves/bn254';
|
|
3
|
+
import { createLogger } from '@aztec-labs/foundation/log';
|
|
4
|
+
import { serializeToBuffer } from '@aztec-labs/foundation/serialize';
|
|
5
|
+
import { sleep } from '@aztec-labs/foundation/sleep';
|
|
6
|
+
import { type IndexedTreeLeafPreimage, SiblingPath } from '@aztec-labs/foundation/trees';
|
|
7
|
+
import { BlockHash } from '@aztec-labs/stdlib/block';
|
|
8
|
+
import type {
|
|
9
|
+
BatchInsertionResult,
|
|
10
|
+
IndexedTreeId,
|
|
11
|
+
MerkleTreeLeafType,
|
|
12
|
+
MerkleTreeReadOperations,
|
|
13
|
+
MerkleTreeWriteOperations,
|
|
14
|
+
SequentialInsertionResult,
|
|
15
|
+
TreeInfo,
|
|
16
|
+
} from '@aztec-labs/stdlib/interfaces/server';
|
|
17
|
+
import {
|
|
18
|
+
MerkleTreeId,
|
|
19
|
+
NullifierLeaf,
|
|
20
|
+
NullifierLeafPreimage,
|
|
21
|
+
PublicDataTreeLeaf,
|
|
22
|
+
PublicDataTreeLeafPreimage,
|
|
23
|
+
} from '@aztec-labs/stdlib/trees';
|
|
24
|
+
import { type BlockHeader, PartialStateReference, StateReference } from '@aztec-labs/stdlib/tx';
|
|
25
|
+
import type { WorldStateRevision } from '@aztec-labs/stdlib/world-state';
|
|
26
|
+
import assert from 'assert';
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
type SerializedIndexedLeaf,
|
|
30
|
+
type SerializedLeafValue,
|
|
31
|
+
blockStateReference,
|
|
32
|
+
treeStateReferenceToSnapshot,
|
|
33
|
+
} from './message.js';
|
|
34
|
+
import type { NativeWorldStateInstance } from './native_world_state_instance.js';
|
|
35
|
+
|
|
36
|
+
export class MerkleTreesFacade implements MerkleTreeReadOperations {
|
|
37
|
+
constructor(
|
|
38
|
+
protected readonly instance: NativeWorldStateInstance,
|
|
39
|
+
protected readonly initialHeader: BlockHeader,
|
|
40
|
+
protected readonly revision: WorldStateRevision,
|
|
41
|
+
) {}
|
|
42
|
+
|
|
43
|
+
getInitialHeader(): BlockHeader {
|
|
44
|
+
return this.initialHeader;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
getRevision(): WorldStateRevision {
|
|
48
|
+
return this.revision;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getIpcPath(): string {
|
|
52
|
+
return this.instance.getIpcPath();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
findLeafIndices(treeId: MerkleTreeId, values: MerkleTreeLeafType<MerkleTreeId>[]): Promise<(bigint | undefined)[]> {
|
|
56
|
+
return this.findLeafIndicesAfter(treeId, values, 0n);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async findSiblingPaths<N extends number>(
|
|
60
|
+
treeId: MerkleTreeId,
|
|
61
|
+
values: MerkleTreeLeafType<MerkleTreeId>[],
|
|
62
|
+
): Promise<({ path: SiblingPath<N>; index: bigint } | undefined)[]> {
|
|
63
|
+
const paths = await this.instance.findSiblingPaths(
|
|
64
|
+
treeId,
|
|
65
|
+
this.revision,
|
|
66
|
+
values.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf))),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return paths.map(path => {
|
|
70
|
+
if (!path) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
return { path: new SiblingPath<N>(path.path.length as N, path.path), index: BigInt(path.index) };
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async findLeafIndicesAfter(
|
|
78
|
+
treeId: MerkleTreeId,
|
|
79
|
+
leaves: MerkleTreeLeafType<MerkleTreeId>[],
|
|
80
|
+
startIndex: bigint,
|
|
81
|
+
): Promise<(bigint | undefined)[]> {
|
|
82
|
+
const indices = await this.instance.findLeafIndices(
|
|
83
|
+
treeId,
|
|
84
|
+
this.revision,
|
|
85
|
+
leaves.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf))),
|
|
86
|
+
startIndex,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return indices.map(index => {
|
|
90
|
+
if (typeof index === 'number' || typeof index === 'bigint') {
|
|
91
|
+
return BigInt(index);
|
|
92
|
+
} else {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async getLeafPreimage(treeId: IndexedTreeId, leafIndex: bigint): Promise<IndexedTreeLeafPreimage | undefined> {
|
|
99
|
+
const resp = await this.instance.getLeafPreimage(treeId, this.revision, leafIndex);
|
|
100
|
+
|
|
101
|
+
return resp ? deserializeIndexedLeaf(resp) : undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async getLeafValue<ID extends MerkleTreeId>(
|
|
105
|
+
treeId: ID,
|
|
106
|
+
leafIndex: bigint,
|
|
107
|
+
): Promise<MerkleTreeLeafType<ID> | undefined> {
|
|
108
|
+
const resp = await this.instance.getLeafValue(treeId, this.revision, leafIndex);
|
|
109
|
+
|
|
110
|
+
if (!resp) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const leaf = deserializeLeafValue(resp);
|
|
115
|
+
if (leaf instanceof Fr) {
|
|
116
|
+
return treeId === MerkleTreeId.ARCHIVE ? (new BlockHash(leaf) as any) : (leaf as any);
|
|
117
|
+
} else {
|
|
118
|
+
return leaf.toBuffer() as any;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async getPreviousValueIndex(
|
|
123
|
+
treeId: IndexedTreeId,
|
|
124
|
+
value: bigint,
|
|
125
|
+
): Promise<{ index: bigint; alreadyPresent: boolean } | undefined> {
|
|
126
|
+
const resp = await this.instance.findLowLeaf(treeId, this.revision, new Fr(value));
|
|
127
|
+
return {
|
|
128
|
+
alreadyPresent: resp.alreadyPresent,
|
|
129
|
+
index: BigInt(resp.index),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async getSiblingPath<N extends number>(treeId: MerkleTreeId, leafIndex: bigint): Promise<SiblingPath<N>> {
|
|
134
|
+
const siblingPath = await this.instance.getSiblingPath(treeId, this.revision, leafIndex);
|
|
135
|
+
|
|
136
|
+
return new SiblingPath(siblingPath.length, siblingPath) as any;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async getStateReference(): Promise<StateReference> {
|
|
140
|
+
const state = await this.instance.getStateReference(this.revision);
|
|
141
|
+
|
|
142
|
+
return new StateReference(
|
|
143
|
+
treeStateReferenceToSnapshot(state[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]),
|
|
144
|
+
new PartialStateReference(
|
|
145
|
+
treeStateReferenceToSnapshot(state[MerkleTreeId.NOTE_HASH_TREE]),
|
|
146
|
+
treeStateReferenceToSnapshot(state[MerkleTreeId.NULLIFIER_TREE]),
|
|
147
|
+
treeStateReferenceToSnapshot(state[MerkleTreeId.PUBLIC_DATA_TREE]),
|
|
148
|
+
),
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async getInitialStateReference(): Promise<StateReference> {
|
|
153
|
+
const state = await this.instance.getInitialStateReference();
|
|
154
|
+
|
|
155
|
+
return new StateReference(
|
|
156
|
+
treeStateReferenceToSnapshot(state[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]),
|
|
157
|
+
new PartialStateReference(
|
|
158
|
+
treeStateReferenceToSnapshot(state[MerkleTreeId.NOTE_HASH_TREE]),
|
|
159
|
+
treeStateReferenceToSnapshot(state[MerkleTreeId.NULLIFIER_TREE]),
|
|
160
|
+
treeStateReferenceToSnapshot(state[MerkleTreeId.PUBLIC_DATA_TREE]),
|
|
161
|
+
),
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async getTreeInfo(treeId: MerkleTreeId): Promise<TreeInfo> {
|
|
166
|
+
return await this.instance.getTreeInfo(treeId, this.revision);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async getBlockNumbersForLeafIndices<ID extends MerkleTreeId>(
|
|
170
|
+
treeId: ID,
|
|
171
|
+
leafIndices: bigint[],
|
|
172
|
+
): Promise<(BlockNumber | undefined)[]> {
|
|
173
|
+
const blockNumbers = await this.instance.getBlockNumbersForLeafIndices(treeId, this.revision, leafIndices);
|
|
174
|
+
|
|
175
|
+
return blockNumbers.map(x => (x === undefined || x === null ? undefined : BlockNumber(Number(x))));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTreeWriteOperations {
|
|
180
|
+
private log = createLogger('world-state:merkle-trees-fork-facade');
|
|
181
|
+
private closePromise: Promise<void> | undefined;
|
|
182
|
+
|
|
183
|
+
constructor(
|
|
184
|
+
instance: NativeWorldStateInstance,
|
|
185
|
+
initialHeader: BlockHeader,
|
|
186
|
+
revision: WorldStateRevision,
|
|
187
|
+
private opts: { closeDelayMs?: number },
|
|
188
|
+
) {
|
|
189
|
+
assert.notEqual(revision.forkId, 0, 'Fork ID must be set');
|
|
190
|
+
assert.equal(revision.includeUncommitted, true, 'Fork must include uncommitted data');
|
|
191
|
+
super(instance, initialHeader, revision);
|
|
192
|
+
}
|
|
193
|
+
async updateArchive(header: BlockHeader): Promise<void> {
|
|
194
|
+
await this.instance.updateArchive(
|
|
195
|
+
this.revision.forkId,
|
|
196
|
+
blockStateReference(header.state),
|
|
197
|
+
(await header.hash()).toBuffer(),
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async appendLeaves<ID extends MerkleTreeId>(treeId: ID, leaves: MerkleTreeLeafType<ID>[]): Promise<void> {
|
|
202
|
+
await this.instance.appendLeaves(
|
|
203
|
+
treeId,
|
|
204
|
+
this.revision.forkId,
|
|
205
|
+
leaves.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf as any))),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async batchInsert<TreeHeight extends number, SubtreeSiblingPathHeight extends number, ID extends IndexedTreeId>(
|
|
210
|
+
treeId: ID,
|
|
211
|
+
rawLeaves: Buffer[],
|
|
212
|
+
subtreeHeight: number,
|
|
213
|
+
): Promise<BatchInsertionResult<TreeHeight, SubtreeSiblingPathHeight>> {
|
|
214
|
+
const leaves = rawLeaves.map((leaf: Buffer) => hydrateLeaf(treeId, leaf)).map(serializeLeaf);
|
|
215
|
+
const resp = await this.instance.batchInsert(treeId, this.revision.forkId, leaves, subtreeHeight);
|
|
216
|
+
|
|
217
|
+
return {
|
|
218
|
+
newSubtreeSiblingPath: new SiblingPath<SubtreeSiblingPathHeight>(
|
|
219
|
+
resp.subtreePath.length as any,
|
|
220
|
+
resp.subtreePath,
|
|
221
|
+
),
|
|
222
|
+
sortedNewLeaves: resp.sortedLeaves
|
|
223
|
+
.map(([leaf]) => leaf)
|
|
224
|
+
.map(deserializeLeafValue)
|
|
225
|
+
.map(serializeToBuffer),
|
|
226
|
+
sortedNewLeavesIndexes: resp.sortedLeaves.map(([, index]) => index),
|
|
227
|
+
lowLeavesWitnessData: resp.lowLeafWitnessData.map(data => ({
|
|
228
|
+
index: BigInt(data.index),
|
|
229
|
+
leafPreimage: deserializeIndexedLeaf(data.leaf),
|
|
230
|
+
siblingPath: new SiblingPath<TreeHeight>(data.path.length as any, data.path),
|
|
231
|
+
})),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async sequentialInsert<TreeHeight extends number, ID extends IndexedTreeId>(
|
|
236
|
+
treeId: ID,
|
|
237
|
+
rawLeaves: Buffer[],
|
|
238
|
+
): Promise<SequentialInsertionResult<TreeHeight>> {
|
|
239
|
+
const leaves = rawLeaves.map((leaf: Buffer) => hydrateLeaf(treeId, leaf)).map(serializeLeaf);
|
|
240
|
+
const resp = await this.instance.sequentialInsert(treeId, this.revision.forkId, leaves);
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
lowLeavesWitnessData: resp.lowLeafWitnessData.map(data => ({
|
|
244
|
+
index: BigInt(data.index),
|
|
245
|
+
leafPreimage: deserializeIndexedLeaf(data.leaf),
|
|
246
|
+
siblingPath: new SiblingPath<TreeHeight>(data.path.length as any, data.path),
|
|
247
|
+
})),
|
|
248
|
+
insertionWitnessData: resp.insertionWitnessData.map(data => ({
|
|
249
|
+
index: BigInt(data.index),
|
|
250
|
+
leafPreimage: deserializeIndexedLeaf(data.leaf),
|
|
251
|
+
siblingPath: new SiblingPath<TreeHeight>(data.path.length as any, data.path),
|
|
252
|
+
})),
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
public close(): Promise<void> {
|
|
257
|
+
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
258
|
+
// Share the in-flight close promise across duplicate dispose calls so DELETE_FORK is sent at most once.
|
|
259
|
+
if (this.closePromise) {
|
|
260
|
+
return this.closePromise;
|
|
261
|
+
}
|
|
262
|
+
this.closePromise = this.doClose();
|
|
263
|
+
return this.closePromise;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private async doClose(): Promise<void> {
|
|
267
|
+
try {
|
|
268
|
+
await this.instance.deleteFork(this.revision.forkId);
|
|
269
|
+
} catch (err: any) {
|
|
270
|
+
// Ignore errors due to native instance being closed during shutdown.
|
|
271
|
+
// This can happen when validators are still processing block proposals while the node is stopping.
|
|
272
|
+
if (err?.message === 'Native instance is closed' || err?.message === 'IPC instance is closed') {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
// Ignore "Fork not found": the native fork was already destroyed by a pending-chain unwind or a
|
|
276
|
+
// historical prune (both call C++ remove_forks_for_block). Fork IDs are monotonic and never reused,
|
|
277
|
+
// so swallowing this on close cannot mask a deletion of a different fork.
|
|
278
|
+
if (err?.message === 'Fork not found') {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
throw err;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async [Symbol.asyncDispose](): Promise<void> {
|
|
286
|
+
if (this.opts.closeDelayMs) {
|
|
287
|
+
void sleep(this.opts.closeDelayMs)
|
|
288
|
+
.then(() => this.close())
|
|
289
|
+
.catch(err => {
|
|
290
|
+
this.log.warn('Error closing MerkleTreesForkFacade after delay', { err });
|
|
291
|
+
});
|
|
292
|
+
} else {
|
|
293
|
+
await this.close();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
public async createCheckpoint(): Promise<number> {
|
|
298
|
+
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
299
|
+
return await this.instance.createCheckpoint(this.revision.forkId);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
public async commitCheckpoint(): Promise<void> {
|
|
303
|
+
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
304
|
+
await this.instance.commitCheckpoint(this.revision.forkId);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
public async revertCheckpoint(): Promise<void> {
|
|
308
|
+
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
309
|
+
await this.instance.revertCheckpoint(this.revision.forkId);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
public async commitAllCheckpointsTo(depth: number): Promise<void> {
|
|
313
|
+
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
314
|
+
await this.instance.commitAllCheckpoints(this.revision.forkId, depth);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
public async revertAllCheckpointsTo(depth: number): Promise<void> {
|
|
318
|
+
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
319
|
+
await this.instance.revertAllCheckpoints(this.revision.forkId, depth);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function hydrateLeaf(treeId: MerkleTreeId, leaf: Fr | BlockHash | Buffer) {
|
|
324
|
+
if (leaf instanceof Fr) {
|
|
325
|
+
return leaf;
|
|
326
|
+
} else if (leaf instanceof BlockHash) {
|
|
327
|
+
return leaf.toFr();
|
|
328
|
+
} else if (treeId === MerkleTreeId.NULLIFIER_TREE) {
|
|
329
|
+
return NullifierLeaf.fromBuffer(leaf);
|
|
330
|
+
} else if (treeId === MerkleTreeId.PUBLIC_DATA_TREE) {
|
|
331
|
+
return PublicDataTreeLeaf.fromBuffer(leaf);
|
|
332
|
+
} else {
|
|
333
|
+
return Fr.fromBuffer(leaf);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function serializeLeaf(leaf: Fr | BlockHash | NullifierLeaf | PublicDataTreeLeaf): SerializedLeafValue {
|
|
338
|
+
if (leaf instanceof BlockHash) {
|
|
339
|
+
return leaf.toBuffer();
|
|
340
|
+
} else if (leaf instanceof Fr) {
|
|
341
|
+
return leaf.toBuffer();
|
|
342
|
+
} else if (leaf instanceof NullifierLeaf) {
|
|
343
|
+
return { nullifier: leaf.nullifier.toBuffer() };
|
|
344
|
+
} else {
|
|
345
|
+
return { value: leaf.value.toBuffer(), slot: leaf.slot.toBuffer() };
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function deserializeLeafValue(leaf: SerializedLeafValue): Fr | NullifierLeaf | PublicDataTreeLeaf {
|
|
350
|
+
if (Buffer.isBuffer(leaf)) {
|
|
351
|
+
return Fr.fromBuffer(leaf);
|
|
352
|
+
} else if ('slot' in leaf) {
|
|
353
|
+
return new PublicDataTreeLeaf(Fr.fromBuffer(leaf.slot), Fr.fromBuffer(leaf.value));
|
|
354
|
+
} else {
|
|
355
|
+
return new NullifierLeaf(Fr.fromBuffer(leaf.nullifier));
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function deserializeIndexedLeaf(leafPreimage: SerializedIndexedLeaf): IndexedTreeLeafPreimage {
|
|
360
|
+
if ('slot' in leafPreimage.leaf) {
|
|
361
|
+
return new PublicDataTreeLeafPreimage(
|
|
362
|
+
new PublicDataTreeLeaf(Fr.fromBuffer(leafPreimage.leaf.slot), Fr.fromBuffer(leafPreimage.leaf.value)),
|
|
363
|
+
Fr.fromBuffer(leafPreimage.nextKey),
|
|
364
|
+
BigInt(leafPreimage.nextIndex),
|
|
365
|
+
);
|
|
366
|
+
} else if ('nullifier' in leafPreimage.leaf) {
|
|
367
|
+
return new NullifierLeafPreimage(
|
|
368
|
+
new NullifierLeaf(Fr.fromBuffer(leafPreimage.leaf.nullifier)),
|
|
369
|
+
Fr.fromBuffer(leafPreimage.nextKey),
|
|
370
|
+
BigInt(leafPreimage.nextIndex),
|
|
371
|
+
);
|
|
372
|
+
} else {
|
|
373
|
+
throw new Error('Invalid leaf type');
|
|
374
|
+
}
|
|
375
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { BlockNumber } from '@aztec-labs/foundation/branded-types';
|
|
2
|
+
import { Fr } from '@aztec-labs/foundation/curves/bn254';
|
|
3
|
+
import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec-labs/stdlib/trees';
|
|
4
|
+
import type { StateReference } from '@aztec-labs/stdlib/tx';
|
|
5
|
+
|
|
6
|
+
export interface WorldStateStatusSummary {
|
|
7
|
+
/** Last block number that can still be unwound. */
|
|
8
|
+
unfinalizedBlockNumber: BlockNumber;
|
|
9
|
+
/** Last block number that is finalized and cannot be unwound. */
|
|
10
|
+
finalizedBlockNumber: BlockNumber;
|
|
11
|
+
/** Oldest block still available for historical queries and forks. */
|
|
12
|
+
oldestHistoricalBlock: BlockNumber;
|
|
13
|
+
/** Whether the trees are in sync with each other */
|
|
14
|
+
treesAreSynched: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface TreeMeta {
|
|
18
|
+
/** The name of the tree */
|
|
19
|
+
name: string;
|
|
20
|
+
/** The depth of the tree */
|
|
21
|
+
depth: number;
|
|
22
|
+
/** The current size of the tree (number of leaves) */
|
|
23
|
+
size: bigint;
|
|
24
|
+
/** The committed size of the tree */
|
|
25
|
+
committedSize: bigint;
|
|
26
|
+
/** The current root of the tree */
|
|
27
|
+
root: Fr;
|
|
28
|
+
/** The tree's initial size */
|
|
29
|
+
initialSize: bigint;
|
|
30
|
+
/** The tree's initial root value */
|
|
31
|
+
initialRoot: Fr;
|
|
32
|
+
/** The current oldest historical block number of the tree */
|
|
33
|
+
oldestHistoricBlock: BlockNumber;
|
|
34
|
+
/** The current unfinalized block number of the tree */
|
|
35
|
+
unfinalizedBlockHeight: BlockNumber;
|
|
36
|
+
/** The current finalized block number of the tree */
|
|
37
|
+
finalizedBlockHeight: BlockNumber;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface DBStats {
|
|
41
|
+
/** The name of the DB */
|
|
42
|
+
name: string;
|
|
43
|
+
/** The total number of key/value pairs in the DB */
|
|
44
|
+
numDataItems: bigint;
|
|
45
|
+
/** The current mapped size of the DB */
|
|
46
|
+
totalUsedSize: bigint;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface TreeDBStats {
|
|
50
|
+
/** The configured max size of the DB mapping file (effectively the max possible size of the DB) */
|
|
51
|
+
mapSize: bigint;
|
|
52
|
+
/** The physical file size of the database on disk */
|
|
53
|
+
physicalFileSize: bigint;
|
|
54
|
+
/** Stats for the 'blocks' DB */
|
|
55
|
+
blocksDBStats: DBStats;
|
|
56
|
+
/** Stats for the 'nodes' DB */
|
|
57
|
+
nodesDBStats: DBStats;
|
|
58
|
+
/** Stats for the 'leaf pre-images' DB */
|
|
59
|
+
leafPreimagesDBStats: DBStats;
|
|
60
|
+
/** Stats for the 'leaf indices' DB */
|
|
61
|
+
leafIndicesDBStats: DBStats;
|
|
62
|
+
/** Stats for the 'block indices' DB */
|
|
63
|
+
blockIndicesDBStats: DBStats;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface WorldStateMeta {
|
|
67
|
+
/** Tree meta for the note hash tree */
|
|
68
|
+
noteHashTreeMeta: TreeMeta;
|
|
69
|
+
/** Tree meta for the message tree */
|
|
70
|
+
messageTreeMeta: TreeMeta;
|
|
71
|
+
/** Tree meta for the archive tree */
|
|
72
|
+
archiveTreeMeta: TreeMeta;
|
|
73
|
+
/** Tree meta for the public data tree */
|
|
74
|
+
publicDataTreeMeta: TreeMeta;
|
|
75
|
+
/** Tree meta for the nullifier tree */
|
|
76
|
+
nullifierTreeMeta: TreeMeta;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface WorldStateDBStats {
|
|
80
|
+
/** Full stats for the note hash tree */
|
|
81
|
+
noteHashTreeStats: TreeDBStats;
|
|
82
|
+
/** Full stats for the message tree */
|
|
83
|
+
messageTreeStats: TreeDBStats;
|
|
84
|
+
/** Full stats for the archive tree */
|
|
85
|
+
archiveTreeStats: TreeDBStats;
|
|
86
|
+
/** Full stats for the public data tree */
|
|
87
|
+
publicDataTreeStats: TreeDBStats;
|
|
88
|
+
/** Full stats for the nullifier tree */
|
|
89
|
+
nullifierTreeStats: TreeDBStats;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface WorldStateStatusFull {
|
|
93
|
+
summary: WorldStateStatusSummary;
|
|
94
|
+
dbStats: WorldStateDBStats;
|
|
95
|
+
meta: WorldStateMeta;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function buildEmptyDBStats() {
|
|
99
|
+
return {
|
|
100
|
+
name: '',
|
|
101
|
+
numDataItems: 0n,
|
|
102
|
+
totalUsedSize: 0n,
|
|
103
|
+
} as DBStats;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function buildEmptyTreeDBStats() {
|
|
107
|
+
return {
|
|
108
|
+
mapSize: 0n,
|
|
109
|
+
physicalFileSize: 0n,
|
|
110
|
+
blocksDBStats: buildEmptyDBStats(),
|
|
111
|
+
nodesDBStats: buildEmptyDBStats(),
|
|
112
|
+
leafIndicesDBStats: buildEmptyDBStats(),
|
|
113
|
+
leafKeysDBStats: buildEmptyDBStats(),
|
|
114
|
+
leafPreimagesDBStats: buildEmptyDBStats(),
|
|
115
|
+
blockIndicesDBStats: buildEmptyDBStats(),
|
|
116
|
+
} as TreeDBStats;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function buildEmptyTreeMeta() {
|
|
120
|
+
return {
|
|
121
|
+
name: '',
|
|
122
|
+
depth: 0,
|
|
123
|
+
size: 0n,
|
|
124
|
+
committedSize: 0n,
|
|
125
|
+
unfinalizedBlockHeight: BlockNumber.ZERO,
|
|
126
|
+
finalizedBlockHeight: BlockNumber.ZERO,
|
|
127
|
+
oldestHistoricBlock: BlockNumber.ZERO,
|
|
128
|
+
root: Fr.ZERO,
|
|
129
|
+
initialRoot: Fr.ZERO,
|
|
130
|
+
initialSize: 0n,
|
|
131
|
+
} as TreeMeta;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function buildEmptyWorldStateMeta() {
|
|
135
|
+
return {
|
|
136
|
+
noteHashTreeMeta: buildEmptyTreeMeta(),
|
|
137
|
+
messageTreeMeta: buildEmptyTreeMeta(),
|
|
138
|
+
publicDataTreeMeta: buildEmptyTreeMeta(),
|
|
139
|
+
nullifierTreeMeta: buildEmptyTreeMeta(),
|
|
140
|
+
archiveTreeMeta: buildEmptyTreeMeta(),
|
|
141
|
+
} as WorldStateMeta;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function buildEmptyWorldStateDBStats() {
|
|
145
|
+
return {
|
|
146
|
+
noteHashTreeStats: buildEmptyTreeDBStats(),
|
|
147
|
+
archiveTreeStats: buildEmptyTreeDBStats(),
|
|
148
|
+
messageTreeStats: buildEmptyTreeDBStats(),
|
|
149
|
+
publicDataTreeStats: buildEmptyTreeDBStats(),
|
|
150
|
+
nullifierTreeStats: buildEmptyTreeDBStats(),
|
|
151
|
+
} as WorldStateDBStats;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function buildEmptyWorldStateSummary() {
|
|
155
|
+
return {
|
|
156
|
+
unfinalizedBlockNumber: BlockNumber.ZERO,
|
|
157
|
+
finalizedBlockNumber: BlockNumber.ZERO,
|
|
158
|
+
oldestHistoricalBlock: BlockNumber.ZERO,
|
|
159
|
+
treesAreSynched: true,
|
|
160
|
+
} as WorldStateStatusSummary;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function buildEmptyWorldStateStatusFull() {
|
|
164
|
+
return {
|
|
165
|
+
meta: buildEmptyWorldStateMeta(),
|
|
166
|
+
dbStats: buildEmptyWorldStateDBStats(),
|
|
167
|
+
summary: buildEmptyWorldStateSummary(),
|
|
168
|
+
} as WorldStateStatusFull;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function sanitizeSummary(summary: WorldStateStatusSummary) {
|
|
172
|
+
summary.finalizedBlockNumber = BlockNumber.fromBigInt(BigInt(summary.finalizedBlockNumber));
|
|
173
|
+
summary.unfinalizedBlockNumber = BlockNumber.fromBigInt(BigInt(summary.unfinalizedBlockNumber));
|
|
174
|
+
summary.oldestHistoricalBlock = BlockNumber.fromBigInt(BigInt(summary.oldestHistoricalBlock));
|
|
175
|
+
return summary;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function sanitizeDBStats(stats: DBStats) {
|
|
179
|
+
stats.numDataItems = BigInt(stats.numDataItems);
|
|
180
|
+
stats.totalUsedSize = BigInt(stats.totalUsedSize);
|
|
181
|
+
return stats;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function sanitizeMeta(meta: TreeMeta) {
|
|
185
|
+
meta.committedSize = BigInt(meta.committedSize);
|
|
186
|
+
meta.finalizedBlockHeight = BlockNumber.fromBigInt(BigInt(meta.finalizedBlockHeight));
|
|
187
|
+
meta.initialSize = BigInt(meta.initialSize);
|
|
188
|
+
meta.oldestHistoricBlock = BlockNumber.fromBigInt(BigInt(meta.oldestHistoricBlock));
|
|
189
|
+
meta.size = BigInt(meta.size);
|
|
190
|
+
meta.unfinalizedBlockHeight = BlockNumber.fromBigInt(BigInt(meta.unfinalizedBlockHeight));
|
|
191
|
+
return meta;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function sanitizeTreeDBStats(stats: TreeDBStats) {
|
|
195
|
+
stats.blocksDBStats = sanitizeDBStats(stats.blocksDBStats);
|
|
196
|
+
stats.leafIndicesDBStats = sanitizeDBStats(stats.leafIndicesDBStats);
|
|
197
|
+
stats.leafPreimagesDBStats = sanitizeDBStats(stats.leafPreimagesDBStats);
|
|
198
|
+
stats.blockIndicesDBStats = sanitizeDBStats(stats.blockIndicesDBStats);
|
|
199
|
+
stats.nodesDBStats = sanitizeDBStats(stats.nodesDBStats);
|
|
200
|
+
stats.mapSize = BigInt(stats.mapSize);
|
|
201
|
+
stats.physicalFileSize = BigInt(stats.physicalFileSize);
|
|
202
|
+
return stats;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function sanitizeWorldStateDBStats(stats: WorldStateDBStats) {
|
|
206
|
+
stats.archiveTreeStats = sanitizeTreeDBStats(stats.archiveTreeStats);
|
|
207
|
+
stats.messageTreeStats = sanitizeTreeDBStats(stats.messageTreeStats);
|
|
208
|
+
stats.noteHashTreeStats = sanitizeTreeDBStats(stats.noteHashTreeStats);
|
|
209
|
+
stats.nullifierTreeStats = sanitizeTreeDBStats(stats.nullifierTreeStats);
|
|
210
|
+
stats.publicDataTreeStats = sanitizeTreeDBStats(stats.publicDataTreeStats);
|
|
211
|
+
return stats;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function sanitizeWorldStateTreeMeta(meta: WorldStateMeta) {
|
|
215
|
+
meta.archiveTreeMeta = sanitizeMeta(meta.archiveTreeMeta);
|
|
216
|
+
meta.messageTreeMeta = sanitizeMeta(meta.messageTreeMeta);
|
|
217
|
+
meta.noteHashTreeMeta = sanitizeMeta(meta.noteHashTreeMeta);
|
|
218
|
+
meta.nullifierTreeMeta = sanitizeMeta(meta.nullifierTreeMeta);
|
|
219
|
+
meta.publicDataTreeMeta = sanitizeMeta(meta.publicDataTreeMeta);
|
|
220
|
+
return meta;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function sanitizeFullStatus(status: WorldStateStatusFull) {
|
|
224
|
+
status.dbStats = sanitizeWorldStateDBStats(status.dbStats);
|
|
225
|
+
status.summary = sanitizeSummary(status.summary);
|
|
226
|
+
status.meta = sanitizeWorldStateTreeMeta(status.meta);
|
|
227
|
+
return status;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export type SerializedLeafValue =
|
|
231
|
+
| Buffer // Fr
|
|
232
|
+
| { nullifier: Buffer } // NullifierLeaf
|
|
233
|
+
| { value: Buffer; slot: Buffer }; // PublicDataTreeLeaf
|
|
234
|
+
|
|
235
|
+
export type SerializedIndexedLeaf = {
|
|
236
|
+
leaf: Exclude<SerializedLeafValue, Buffer>;
|
|
237
|
+
nextIndex: bigint | number;
|
|
238
|
+
nextKey: Buffer; // Fr
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
export interface SerializedSiblingPathAndIndex {
|
|
242
|
+
index: bigint;
|
|
243
|
+
path: Buffer[];
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export interface SerializedBatchInsertionResult {
|
|
247
|
+
lowLeafWitnessData: ReadonlyArray<{
|
|
248
|
+
leaf: SerializedIndexedLeaf;
|
|
249
|
+
index: bigint | number;
|
|
250
|
+
path: Buffer[];
|
|
251
|
+
}>;
|
|
252
|
+
sortedLeaves: ReadonlyArray<readonly [SerializedLeafValue, number]>;
|
|
253
|
+
subtreePath: Buffer[];
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export interface SerializedSequentialInsertionResult {
|
|
257
|
+
lowLeafWitnessData: ReadonlyArray<{
|
|
258
|
+
leaf: SerializedIndexedLeaf;
|
|
259
|
+
index: bigint | number;
|
|
260
|
+
path: Buffer[];
|
|
261
|
+
}>;
|
|
262
|
+
insertionWitnessData: ReadonlyArray<{
|
|
263
|
+
leaf: SerializedIndexedLeaf;
|
|
264
|
+
index: bigint | number;
|
|
265
|
+
path: Buffer[];
|
|
266
|
+
}>;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export type TreeStateReference = readonly [Buffer, number | bigint];
|
|
270
|
+
export type BlockStateReference = Map<Exclude<MerkleTreeId, MerkleTreeId.ARCHIVE>, TreeStateReference>;
|
|
271
|
+
|
|
272
|
+
export function treeStateReferenceToSnapshot([root, size]: TreeStateReference): AppendOnlyTreeSnapshot {
|
|
273
|
+
return new AppendOnlyTreeSnapshot(Fr.fromBuffer(root), Number(size));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function treeStateReference(snapshot: AppendOnlyTreeSnapshot) {
|
|
277
|
+
return [snapshot.root.toBuffer(), BigInt(snapshot.nextAvailableLeafIndex)] as const;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function blockStateReference(state: StateReference): BlockStateReference {
|
|
281
|
+
return new Map([
|
|
282
|
+
[MerkleTreeId.NULLIFIER_TREE, treeStateReference(state.partial.nullifierTree)],
|
|
283
|
+
[MerkleTreeId.NOTE_HASH_TREE, treeStateReference(state.partial.noteHashTree)],
|
|
284
|
+
[MerkleTreeId.PUBLIC_DATA_TREE, treeStateReference(state.partial.publicDataTree)],
|
|
285
|
+
[MerkleTreeId.L1_TO_L2_MESSAGE_TREE, treeStateReference(state.l1ToL2MessageTree)],
|
|
286
|
+
]);
|
|
287
|
+
}
|