@aztec/world-state 0.0.1-commit.db765a8 → 0.0.1-commit.ddcf04837
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/dest/native/fork_checkpoint.d.ts +7 -1
- package/dest/native/fork_checkpoint.d.ts.map +1 -1
- package/dest/native/fork_checkpoint.js +15 -3
- package/dest/native/merkle_trees_facade.d.ts +6 -5
- package/dest/native/merkle_trees_facade.d.ts.map +1 -1
- package/dest/native/merkle_trees_facade.js +16 -8
- package/dest/native/message.d.ts +13 -6
- package/dest/native/message.d.ts.map +1 -1
- package/dest/native/native_world_state.d.ts +7 -5
- package/dest/native/native_world_state.d.ts.map +1 -1
- package/dest/native/native_world_state.js +16 -11
- package/dest/native/native_world_state_instance.d.ts +4 -4
- package/dest/native/native_world_state_instance.d.ts.map +1 -1
- package/dest/native/native_world_state_instance.js +7 -6
- package/dest/native/world_state_ops_queue.js +5 -5
- package/dest/synchronizer/config.js +7 -7
- package/dest/synchronizer/factory.d.ts +5 -5
- package/dest/synchronizer/factory.d.ts.map +1 -1
- package/dest/synchronizer/factory.js +6 -5
- package/dest/synchronizer/server_world_state_synchronizer.d.ts +4 -4
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +60 -11
- package/dest/testing.d.ts +4 -3
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +10 -6
- package/dest/world-state-db/merkle_tree_db.d.ts +1 -10
- package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
- package/package.json +9 -10
- package/src/native/fork_checkpoint.ts +19 -3
- package/src/native/merkle_trees_facade.ts +23 -11
- package/src/native/message.ts +14 -5
- package/src/native/native_world_state.ts +14 -20
- package/src/native/native_world_state_instance.ts +8 -4
- package/src/native/world_state_ops_queue.ts +5 -5
- package/src/synchronizer/config.ts +7 -7
- package/src/synchronizer/factory.ts +7 -7
- package/src/synchronizer/server_world_state_synchronizer.ts +66 -13
- package/src/testing.ts +8 -9
- package/src/world-state-db/merkle_tree_db.ts +0 -10
|
@@ -4,6 +4,7 @@ import { createLogger } from '@aztec/foundation/log';
|
|
|
4
4
|
import { serializeToBuffer } from '@aztec/foundation/serialize';
|
|
5
5
|
import { sleep } from '@aztec/foundation/sleep';
|
|
6
6
|
import { type IndexedTreeLeafPreimage, SiblingPath } from '@aztec/foundation/trees';
|
|
7
|
+
import { BlockHash } from '@aztec/stdlib/block';
|
|
7
8
|
import type {
|
|
8
9
|
BatchInsertionResult,
|
|
9
10
|
IndexedTreeId,
|
|
@@ -118,7 +119,7 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
|
|
|
118
119
|
|
|
119
120
|
const leaf = deserializeLeafValue(resp);
|
|
120
121
|
if (leaf instanceof Fr) {
|
|
121
|
-
return leaf as any;
|
|
122
|
+
return treeId === MerkleTreeId.ARCHIVE ? (new BlockHash(leaf) as any) : (leaf as any);
|
|
122
123
|
} else {
|
|
123
124
|
return leaf.toBuffer() as any;
|
|
124
125
|
}
|
|
@@ -228,7 +229,7 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
|
|
|
228
229
|
|
|
229
230
|
async appendLeaves<ID extends MerkleTreeId>(treeId: ID, leaves: MerkleTreeLeafType<ID>[]): Promise<void> {
|
|
230
231
|
await this.instance.call(WorldStateMessageType.APPEND_LEAVES, {
|
|
231
|
-
leaves: leaves.map(leaf => leaf as any),
|
|
232
|
+
leaves: leaves.map(leaf => serializeLeaf(hydrateLeaf(treeId, leaf as any))),
|
|
232
233
|
forkId: this.revision.forkId,
|
|
233
234
|
treeId,
|
|
234
235
|
});
|
|
@@ -319,9 +320,10 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
|
|
|
319
320
|
}
|
|
320
321
|
}
|
|
321
322
|
|
|
322
|
-
public async createCheckpoint(): Promise<
|
|
323
|
+
public async createCheckpoint(): Promise<number> {
|
|
323
324
|
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
324
|
-
await this.instance.call(WorldStateMessageType.CREATE_CHECKPOINT, { forkId: this.revision.forkId });
|
|
325
|
+
const resp = await this.instance.call(WorldStateMessageType.CREATE_CHECKPOINT, { forkId: this.revision.forkId });
|
|
326
|
+
return resp.depth;
|
|
325
327
|
}
|
|
326
328
|
|
|
327
329
|
public async commitCheckpoint(): Promise<void> {
|
|
@@ -334,20 +336,28 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
|
|
|
334
336
|
await this.instance.call(WorldStateMessageType.REVERT_CHECKPOINT, { forkId: this.revision.forkId });
|
|
335
337
|
}
|
|
336
338
|
|
|
337
|
-
public async
|
|
339
|
+
public async commitAllCheckpointsTo(depth: number): Promise<void> {
|
|
338
340
|
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
339
|
-
await this.instance.call(WorldStateMessageType.COMMIT_ALL_CHECKPOINTS, {
|
|
341
|
+
await this.instance.call(WorldStateMessageType.COMMIT_ALL_CHECKPOINTS, {
|
|
342
|
+
forkId: this.revision.forkId,
|
|
343
|
+
depth,
|
|
344
|
+
});
|
|
340
345
|
}
|
|
341
346
|
|
|
342
|
-
public async
|
|
347
|
+
public async revertAllCheckpointsTo(depth: number): Promise<void> {
|
|
343
348
|
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
344
|
-
await this.instance.call(WorldStateMessageType.REVERT_ALL_CHECKPOINTS, {
|
|
349
|
+
await this.instance.call(WorldStateMessageType.REVERT_ALL_CHECKPOINTS, {
|
|
350
|
+
forkId: this.revision.forkId,
|
|
351
|
+
depth,
|
|
352
|
+
});
|
|
345
353
|
}
|
|
346
354
|
}
|
|
347
355
|
|
|
348
|
-
function hydrateLeaf
|
|
356
|
+
function hydrateLeaf(treeId: MerkleTreeId, leaf: Fr | BlockHash | Buffer) {
|
|
349
357
|
if (leaf instanceof Fr) {
|
|
350
358
|
return leaf;
|
|
359
|
+
} else if (leaf instanceof BlockHash) {
|
|
360
|
+
return leaf.toFr();
|
|
351
361
|
} else if (treeId === MerkleTreeId.NULLIFIER_TREE) {
|
|
352
362
|
return NullifierLeaf.fromBuffer(leaf);
|
|
353
363
|
} else if (treeId === MerkleTreeId.PUBLIC_DATA_TREE) {
|
|
@@ -357,8 +367,10 @@ function hydrateLeaf<ID extends MerkleTreeId>(treeId: ID, leaf: Fr | Buffer) {
|
|
|
357
367
|
}
|
|
358
368
|
}
|
|
359
369
|
|
|
360
|
-
export function serializeLeaf(leaf: Fr | NullifierLeaf | PublicDataTreeLeaf): SerializedLeafValue {
|
|
361
|
-
if (leaf instanceof
|
|
370
|
+
export function serializeLeaf(leaf: Fr | BlockHash | NullifierLeaf | PublicDataTreeLeaf): SerializedLeafValue {
|
|
371
|
+
if (leaf instanceof BlockHash) {
|
|
372
|
+
return leaf.toBuffer();
|
|
373
|
+
} else if (leaf instanceof Fr) {
|
|
362
374
|
return leaf.toBuffer();
|
|
363
375
|
} else if (leaf instanceof NullifierLeaf) {
|
|
364
376
|
return { nullifier: leaf.nullifier.toBuffer() };
|
package/src/native/message.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
2
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
3
|
import type { Tuple } from '@aztec/foundation/serialize';
|
|
4
|
-
import type { BlockHash } from '@aztec/stdlib/block';
|
|
5
4
|
import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
|
|
6
5
|
import type { StateReference } from '@aztec/stdlib/tx';
|
|
7
6
|
import type { UInt32 } from '@aztec/stdlib/types';
|
|
@@ -284,6 +283,16 @@ interface WithForkId {
|
|
|
284
283
|
forkId: number;
|
|
285
284
|
}
|
|
286
285
|
|
|
286
|
+
interface CreateCheckpointResponse {
|
|
287
|
+
depth: number;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Request to commit/revert all checkpoints down to a target depth. The resulting depth after the operation equals the given depth. */
|
|
291
|
+
interface CheckpointDepthRequest extends WithForkId {
|
|
292
|
+
/** The target depth after the operation. All checkpoints above this depth are committed/reverted. */
|
|
293
|
+
depth: number;
|
|
294
|
+
}
|
|
295
|
+
|
|
287
296
|
interface WithWorldStateRevision {
|
|
288
297
|
revision: WorldStateRevision;
|
|
289
298
|
}
|
|
@@ -413,7 +422,7 @@ interface UpdateArchiveRequest extends WithForkId {
|
|
|
413
422
|
interface SyncBlockRequest extends WithCanonicalForkId {
|
|
414
423
|
blockNumber: BlockNumber;
|
|
415
424
|
blockStateRef: BlockStateReference;
|
|
416
|
-
blockHeaderHash:
|
|
425
|
+
blockHeaderHash: Buffer;
|
|
417
426
|
paddedNoteHashes: readonly SerializedLeafValue[];
|
|
418
427
|
paddedL1ToL2Messages: readonly SerializedLeafValue[];
|
|
419
428
|
paddedNullifiers: readonly SerializedLeafValue[];
|
|
@@ -487,8 +496,8 @@ export type WorldStateRequest = {
|
|
|
487
496
|
[WorldStateMessageType.CREATE_CHECKPOINT]: WithForkId;
|
|
488
497
|
[WorldStateMessageType.COMMIT_CHECKPOINT]: WithForkId;
|
|
489
498
|
[WorldStateMessageType.REVERT_CHECKPOINT]: WithForkId;
|
|
490
|
-
[WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]:
|
|
491
|
-
[WorldStateMessageType.REVERT_ALL_CHECKPOINTS]:
|
|
499
|
+
[WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: CheckpointDepthRequest;
|
|
500
|
+
[WorldStateMessageType.REVERT_ALL_CHECKPOINTS]: CheckpointDepthRequest;
|
|
492
501
|
|
|
493
502
|
[WorldStateMessageType.COPY_STORES]: CopyStoresRequest;
|
|
494
503
|
|
|
@@ -529,7 +538,7 @@ export type WorldStateResponse = {
|
|
|
529
538
|
|
|
530
539
|
[WorldStateMessageType.GET_STATUS]: WorldStateStatusSummary;
|
|
531
540
|
|
|
532
|
-
[WorldStateMessageType.CREATE_CHECKPOINT]:
|
|
541
|
+
[WorldStateMessageType.CREATE_CHECKPOINT]: CreateCheckpointResponse;
|
|
533
542
|
[WorldStateMessageType.COMMIT_CHECKPOINT]: void;
|
|
534
543
|
[WorldStateMessageType.REVERT_CHECKPOINT]: void;
|
|
535
544
|
[WorldStateMessageType.COMMIT_ALL_CHECKPOINTS]: void;
|
|
@@ -14,8 +14,8 @@ import type {
|
|
|
14
14
|
} from '@aztec/stdlib/interfaces/server';
|
|
15
15
|
import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
|
|
16
16
|
import { MerkleTreeId, NullifierLeaf, type NullifierLeafPreimage, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
|
|
17
|
-
import { BlockHeader, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
|
|
18
|
-
import { WorldStateRevision } from '@aztec/stdlib/world-state';
|
|
17
|
+
import { BlockHeader, GlobalVariables, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
|
|
18
|
+
import { EMPTY_GENESIS_DATA, type GenesisData, WorldStateRevision } from '@aztec/stdlib/world-state';
|
|
19
19
|
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
20
20
|
|
|
21
21
|
import assert from 'assert/strict';
|
|
@@ -53,6 +53,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
53
53
|
protected instance: NativeWorldState,
|
|
54
54
|
protected readonly worldStateInstrumentation: WorldStateInstrumentation,
|
|
55
55
|
protected readonly log: Logger,
|
|
56
|
+
private readonly genesis: GenesisData = EMPTY_GENESIS_DATA,
|
|
56
57
|
private readonly cleanup = () => Promise.resolve(),
|
|
57
58
|
) {}
|
|
58
59
|
|
|
@@ -60,7 +61,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
60
61
|
rollupAddress: EthAddress,
|
|
61
62
|
dataDir: string,
|
|
62
63
|
wsTreeMapSizes: WorldStateTreeMapSizes,
|
|
63
|
-
|
|
64
|
+
genesis: GenesisData = EMPTY_GENESIS_DATA,
|
|
64
65
|
instrumentation = new WorldStateInstrumentation(getTelemetryClient()),
|
|
65
66
|
bindings?: LoggerBindings,
|
|
66
67
|
cleanup = () => Promise.resolve(),
|
|
@@ -73,14 +74,12 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
73
74
|
rollupAddress,
|
|
74
75
|
dataDirectory: worldStateDirectory,
|
|
75
76
|
onOpen: (dir: string) => {
|
|
76
|
-
return Promise.resolve(
|
|
77
|
-
new NativeWorldState(dir, wsTreeMapSizes, prefilledPublicData, instrumentation, bindings),
|
|
78
|
-
);
|
|
77
|
+
return Promise.resolve(new NativeWorldState(dir, wsTreeMapSizes, genesis, instrumentation, bindings));
|
|
79
78
|
},
|
|
80
79
|
});
|
|
81
80
|
|
|
82
81
|
const [instance] = await versionManager.open();
|
|
83
|
-
const worldState = new this(instance, instrumentation, log, cleanup);
|
|
82
|
+
const worldState = new this(instance, instrumentation, log, genesis, cleanup);
|
|
84
83
|
try {
|
|
85
84
|
await worldState.init();
|
|
86
85
|
} catch (e) {
|
|
@@ -94,7 +93,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
94
93
|
static async tmp(
|
|
95
94
|
rollupAddress = EthAddress.ZERO,
|
|
96
95
|
cleanupTmpDir = true,
|
|
97
|
-
|
|
96
|
+
genesis: GenesisData = EMPTY_GENESIS_DATA,
|
|
98
97
|
instrumentation = new WorldStateInstrumentation(getTelemetryClient()),
|
|
99
98
|
bindings?: LoggerBindings,
|
|
100
99
|
): Promise<NativeWorldStateService> {
|
|
@@ -120,15 +119,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
120
119
|
}
|
|
121
120
|
};
|
|
122
121
|
|
|
123
|
-
return this.new(
|
|
124
|
-
rollupAddress,
|
|
125
|
-
dataDir,
|
|
126
|
-
worldStateTreeMapSizes,
|
|
127
|
-
prefilledPublicData,
|
|
128
|
-
instrumentation,
|
|
129
|
-
bindings,
|
|
130
|
-
cleanup,
|
|
131
|
-
);
|
|
122
|
+
return this.new(rollupAddress, dataDir, worldStateTreeMapSizes, genesis, instrumentation, bindings, cleanup);
|
|
132
123
|
}
|
|
133
124
|
|
|
134
125
|
protected async init() {
|
|
@@ -147,7 +138,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
147
138
|
|
|
148
139
|
// the initial header _must_ be the first element in the archive tree
|
|
149
140
|
// if this assertion fails, check that the hashing done in Header in yarn-project matches the initial header hash done in world_state.cpp
|
|
150
|
-
const indices = await committed.findLeafIndices(MerkleTreeId.ARCHIVE, [
|
|
141
|
+
const indices = await committed.findLeafIndices(MerkleTreeId.ARCHIVE, [await this.initialHeader.hash()]);
|
|
151
142
|
const initialHeaderIndex = indices[0];
|
|
152
143
|
assert.strictEqual(initialHeaderIndex, 0n, 'Invalid initial archive state');
|
|
153
144
|
}
|
|
@@ -232,7 +223,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
232
223
|
WorldStateMessageType.SYNC_BLOCK,
|
|
233
224
|
{
|
|
234
225
|
blockNumber: l2Block.number,
|
|
235
|
-
blockHeaderHash: await l2Block.hash(),
|
|
226
|
+
blockHeaderHash: (await l2Block.hash()).toBuffer(),
|
|
236
227
|
paddedL1ToL2Messages: paddedL1ToL2Messages.map(serializeLeaf),
|
|
237
228
|
paddedNoteHashes: paddedNoteHashes.map(serializeLeaf),
|
|
238
229
|
paddedNullifiers: paddedNullifiers.map(serializeLeaf),
|
|
@@ -256,7 +247,10 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
256
247
|
|
|
257
248
|
private async buildInitialHeader(): Promise<BlockHeader> {
|
|
258
249
|
const state = await this.getInitialStateReference();
|
|
259
|
-
return BlockHeader.empty({
|
|
250
|
+
return BlockHeader.empty({
|
|
251
|
+
state,
|
|
252
|
+
globalVariables: GlobalVariables.empty({ timestamp: this.genesis.genesisTimestamp }),
|
|
253
|
+
});
|
|
260
254
|
}
|
|
261
255
|
|
|
262
256
|
private sanitizeAndCacheSummaryFromFull(response: WorldStateStatusFull) {
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
12
12
|
import { NativeWorldState as BaseNativeWorldState, MsgpackChannel } from '@aztec/native';
|
|
13
13
|
import { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
14
|
-
import type
|
|
14
|
+
import { EMPTY_GENESIS_DATA, type GenesisData } from '@aztec/stdlib/world-state';
|
|
15
15
|
|
|
16
16
|
import assert from 'assert';
|
|
17
17
|
import { cpus } from 'os';
|
|
@@ -55,7 +55,7 @@ export class NativeWorldState implements NativeWorldStateInstance {
|
|
|
55
55
|
constructor(
|
|
56
56
|
private readonly dataDir: string,
|
|
57
57
|
private readonly wsTreeMapSizes: WorldStateTreeMapSizes,
|
|
58
|
-
private readonly
|
|
58
|
+
private readonly genesis: GenesisData = EMPTY_GENESIS_DATA,
|
|
59
59
|
private readonly instrumentation: WorldStateInstrumentation,
|
|
60
60
|
bindings?: LoggerBindings,
|
|
61
61
|
private readonly log: Logger = createLogger('world-state:database', bindings),
|
|
@@ -66,7 +66,10 @@ export class NativeWorldState implements NativeWorldStateInstance {
|
|
|
66
66
|
wsTreeMapSizes,
|
|
67
67
|
)} and ${threads} threads.`,
|
|
68
68
|
);
|
|
69
|
-
const prefilledPublicDataBufferArray = prefilledPublicData.map(d => [
|
|
69
|
+
const prefilledPublicDataBufferArray = genesis.prefilledPublicData.map(d => [
|
|
70
|
+
d.slot.toBuffer(),
|
|
71
|
+
d.value.toBuffer(),
|
|
72
|
+
]);
|
|
70
73
|
const ws = new BaseNativeWorldState(
|
|
71
74
|
dataDir,
|
|
72
75
|
{
|
|
@@ -82,6 +85,7 @@ export class NativeWorldState implements NativeWorldStateInstance {
|
|
|
82
85
|
},
|
|
83
86
|
prefilledPublicDataBufferArray,
|
|
84
87
|
DomainSeparator.BLOCK_HEADER_HASH,
|
|
88
|
+
Number(genesis.genesisTimestamp),
|
|
85
89
|
{
|
|
86
90
|
[MerkleTreeId.NULLIFIER_TREE]: wsTreeMapSizes.nullifierTreeMapSizeKb,
|
|
87
91
|
[MerkleTreeId.NOTE_HASH_TREE]: wsTreeMapSizes.noteHashTreeMapSizeKb,
|
|
@@ -104,7 +108,7 @@ export class NativeWorldState implements NativeWorldStateInstance {
|
|
|
104
108
|
return new NativeWorldState(
|
|
105
109
|
this.dataDir,
|
|
106
110
|
this.wsTreeMapSizes,
|
|
107
|
-
this.
|
|
111
|
+
this.genesis,
|
|
108
112
|
this.instrumentation,
|
|
109
113
|
this.log.getBindings(),
|
|
110
114
|
this.log,
|
|
@@ -96,7 +96,7 @@ export class WorldStateOpsQueue {
|
|
|
96
96
|
// then send the request immediately
|
|
97
97
|
// If a mutating request is in flight then we must wait
|
|
98
98
|
// If a mutating request is not in flight but something is queued then it must be a mutating request
|
|
99
|
-
if (this.inFlightMutatingCount
|
|
99
|
+
if (this.inFlightMutatingCount === 0 && this.requests.length === 0) {
|
|
100
100
|
this.sendEnqueuedRequest(op);
|
|
101
101
|
} else {
|
|
102
102
|
this.requests.push(op);
|
|
@@ -122,7 +122,7 @@ export class WorldStateOpsQueue {
|
|
|
122
122
|
--this.inFlightCount;
|
|
123
123
|
|
|
124
124
|
// If there are still requests in flight then do nothing further
|
|
125
|
-
if (this.inFlightCount
|
|
125
|
+
if (this.inFlightCount !== 0) {
|
|
126
126
|
return;
|
|
127
127
|
}
|
|
128
128
|
|
|
@@ -134,7 +134,7 @@ export class WorldStateOpsQueue {
|
|
|
134
134
|
while (this.requests.length > 0) {
|
|
135
135
|
const next = this.requests[0];
|
|
136
136
|
if (next.mutating) {
|
|
137
|
-
if (this.inFlightCount
|
|
137
|
+
if (this.inFlightCount === 0) {
|
|
138
138
|
// send the mutating request
|
|
139
139
|
this.requests.shift();
|
|
140
140
|
this.sendEnqueuedRequest(next);
|
|
@@ -149,7 +149,7 @@ export class WorldStateOpsQueue {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
// If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
|
|
152
|
-
if (this.inFlightCount
|
|
152
|
+
if (this.inFlightCount === 0 && this.stopResolve !== undefined) {
|
|
153
153
|
this.stopResolve();
|
|
154
154
|
}
|
|
155
155
|
}
|
|
@@ -182,7 +182,7 @@ export class WorldStateOpsQueue {
|
|
|
182
182
|
});
|
|
183
183
|
|
|
184
184
|
// If no outstanding requests then immediately resolve the promise
|
|
185
|
-
if (this.requests.length
|
|
185
|
+
if (this.requests.length === 0 && this.inFlightCount === 0 && this.stopResolve !== undefined) {
|
|
186
186
|
this.stopResolve();
|
|
187
187
|
}
|
|
188
188
|
return this.stopPromise;
|
|
@@ -42,41 +42,41 @@ export const worldStateConfigMappings: ConfigMappingsType<WorldStateConfig> = {
|
|
|
42
42
|
},
|
|
43
43
|
worldStateBlockRequestBatchSize: {
|
|
44
44
|
env: 'WS_BLOCK_REQUEST_BATCH_SIZE',
|
|
45
|
-
parseEnv: (val: string
|
|
45
|
+
parseEnv: (val: string) => +val,
|
|
46
46
|
description: 'Size of the batch for each get-blocks request from the synchronizer to the archiver.',
|
|
47
47
|
},
|
|
48
48
|
worldStateDbMapSizeKb: {
|
|
49
49
|
env: 'WS_DB_MAP_SIZE_KB',
|
|
50
|
-
parseEnv: (val: string
|
|
50
|
+
parseEnv: (val: string) => +val,
|
|
51
51
|
description: 'The maximum possible size of the world state DB in KB. Overwrites the general dataStoreMapSizeKb.',
|
|
52
52
|
},
|
|
53
53
|
archiveTreeMapSizeKb: {
|
|
54
54
|
env: 'ARCHIVE_TREE_MAP_SIZE_KB',
|
|
55
|
-
parseEnv: (val: string
|
|
55
|
+
parseEnv: (val: string) => +val,
|
|
56
56
|
description:
|
|
57
57
|
'The maximum possible size of the world state archive tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
58
58
|
},
|
|
59
59
|
nullifierTreeMapSizeKb: {
|
|
60
60
|
env: 'NULLIFIER_TREE_MAP_SIZE_KB',
|
|
61
|
-
parseEnv: (val: string
|
|
61
|
+
parseEnv: (val: string) => +val,
|
|
62
62
|
description:
|
|
63
63
|
'The maximum possible size of the world state nullifier tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
64
64
|
},
|
|
65
65
|
noteHashTreeMapSizeKb: {
|
|
66
66
|
env: 'NOTE_HASH_TREE_MAP_SIZE_KB',
|
|
67
|
-
parseEnv: (val: string
|
|
67
|
+
parseEnv: (val: string) => +val,
|
|
68
68
|
description:
|
|
69
69
|
'The maximum possible size of the world state note hash tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
70
70
|
},
|
|
71
71
|
messageTreeMapSizeKb: {
|
|
72
72
|
env: 'MESSAGE_TREE_MAP_SIZE_KB',
|
|
73
|
-
parseEnv: (val: string
|
|
73
|
+
parseEnv: (val: string) => +val,
|
|
74
74
|
description:
|
|
75
75
|
'The maximum possible size of the world state message tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
76
76
|
},
|
|
77
77
|
publicDataTreeMapSizeKb: {
|
|
78
78
|
env: 'PUBLIC_DATA_TREE_MAP_SIZE_KB',
|
|
79
|
-
parseEnv: (val: string
|
|
79
|
+
parseEnv: (val: string) => +val,
|
|
80
80
|
description:
|
|
81
81
|
'The maximum possible size of the world state public data tree in KB. Overwrites the general worldStateDbMapSizeKb.',
|
|
82
82
|
},
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { LoggerBindings } from '@aztec/foundation/log';
|
|
2
|
-
import type { DataStoreConfig } from '@aztec/kv-store/config';
|
|
3
2
|
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
3
|
+
import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
|
|
4
4
|
import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
5
|
-
import type
|
|
5
|
+
import { EMPTY_GENESIS_DATA, type GenesisData } from '@aztec/stdlib/world-state';
|
|
6
6
|
import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
|
|
7
7
|
|
|
8
8
|
import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
|
|
@@ -21,12 +21,12 @@ export interface WorldStateTreeMapSizes {
|
|
|
21
21
|
export async function createWorldStateSynchronizer(
|
|
22
22
|
config: WorldStateConfig & DataStoreConfig,
|
|
23
23
|
l2BlockSource: L2BlockSource & L1ToL2MessageSource,
|
|
24
|
-
|
|
24
|
+
genesis: GenesisData = EMPTY_GENESIS_DATA,
|
|
25
25
|
client: TelemetryClient = getTelemetryClient(),
|
|
26
26
|
bindings?: LoggerBindings,
|
|
27
27
|
) {
|
|
28
28
|
const instrumentation = new WorldStateInstrumentation(client);
|
|
29
|
-
const merkleTrees = await createWorldState(config,
|
|
29
|
+
const merkleTrees = await createWorldState(config, genesis, instrumentation, bindings);
|
|
30
30
|
return new ServerWorldStateSynchronizer(merkleTrees, l2BlockSource, config, instrumentation);
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -42,7 +42,7 @@ export async function createWorldState(
|
|
|
42
42
|
| 'publicDataTreeMapSizeKb'
|
|
43
43
|
> &
|
|
44
44
|
Pick<DataStoreConfig, 'dataDirectory' | 'dataStoreMapSizeKb' | 'l1Contracts'>,
|
|
45
|
-
|
|
45
|
+
genesis: GenesisData = EMPTY_GENESIS_DATA,
|
|
46
46
|
instrumentation: WorldStateInstrumentation = new WorldStateInstrumentation(getTelemetryClient()),
|
|
47
47
|
bindings?: LoggerBindings,
|
|
48
48
|
) {
|
|
@@ -66,14 +66,14 @@ export async function createWorldState(
|
|
|
66
66
|
config.l1Contracts.rollupAddress,
|
|
67
67
|
dataDirectory,
|
|
68
68
|
wsTreeMapSizes,
|
|
69
|
-
|
|
69
|
+
genesis,
|
|
70
70
|
instrumentation,
|
|
71
71
|
bindings,
|
|
72
72
|
)
|
|
73
73
|
: await NativeWorldStateService.tmp(
|
|
74
74
|
config.l1Contracts.rollupAddress,
|
|
75
75
|
!['true', '1'].includes(process.env.DEBUG_WORLD_STATE!),
|
|
76
|
-
|
|
76
|
+
genesis,
|
|
77
77
|
instrumentation,
|
|
78
78
|
bindings,
|
|
79
79
|
);
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
|
|
2
2
|
import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
3
3
|
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
4
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
5
5
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
6
6
|
import { elapsed } from '@aztec/foundation/timer';
|
|
7
7
|
import {
|
|
8
|
+
type BlockHash,
|
|
9
|
+
GENESIS_BLOCK_HEADER_HASH,
|
|
8
10
|
GENESIS_CHECKPOINT_HEADER_HASH,
|
|
9
11
|
type L2Block,
|
|
10
12
|
type L2BlockId,
|
|
@@ -177,13 +179,10 @@ export class ServerWorldStateSynchronizer
|
|
|
177
179
|
/**
|
|
178
180
|
* Forces an immediate sync.
|
|
179
181
|
* @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
|
|
180
|
-
* @param
|
|
182
|
+
* @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
|
|
181
183
|
* @returns A promise that resolves with the block number the world state was synced to
|
|
182
184
|
*/
|
|
183
|
-
public async syncImmediate(
|
|
184
|
-
targetBlockNumber?: BlockNumber,
|
|
185
|
-
skipThrowIfTargetNotReached?: boolean,
|
|
186
|
-
): Promise<BlockNumber> {
|
|
185
|
+
public async syncImmediate(targetBlockNumber?: BlockNumber, blockHash?: BlockHash): Promise<BlockNumber> {
|
|
187
186
|
if (this.currentState !== WorldStateRunningState.RUNNING) {
|
|
188
187
|
throw new Error(`World State is not running. Unable to perform sync.`);
|
|
189
188
|
}
|
|
@@ -195,7 +194,19 @@ export class ServerWorldStateSynchronizer
|
|
|
195
194
|
// If we have been given a block number to sync to and we have reached that number then return
|
|
196
195
|
const currentBlockNumber = await this.getLatestBlockNumber();
|
|
197
196
|
if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
|
|
198
|
-
|
|
197
|
+
if (blockHash === undefined) {
|
|
198
|
+
return currentBlockNumber;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// If a block hash was provided, verify we're on the expected fork
|
|
202
|
+
const currentHash = await this.getL2BlockHash(targetBlockNumber);
|
|
203
|
+
if (currentHash === blockHash.toString()) {
|
|
204
|
+
return currentBlockNumber;
|
|
205
|
+
}
|
|
206
|
+
// Hash mismatch: a reorg may have occurred, fall through to trigger sync
|
|
207
|
+
this.log.debug(
|
|
208
|
+
`World state block hash mismatch at ${targetBlockNumber} (expected ${blockHash}, got ${currentHash}). Triggering resync.`,
|
|
209
|
+
);
|
|
199
210
|
}
|
|
200
211
|
this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
|
|
201
212
|
|
|
@@ -213,7 +224,7 @@ export class ServerWorldStateSynchronizer
|
|
|
213
224
|
|
|
214
225
|
// If we have been given a block number to sync to and we have not reached that number then fail
|
|
215
226
|
const updatedBlockNumber = await this.getLatestBlockNumber();
|
|
216
|
-
if (
|
|
227
|
+
if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
|
|
217
228
|
throw new WorldStateSynchronizerError(
|
|
218
229
|
`Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`,
|
|
219
230
|
{
|
|
@@ -227,6 +238,24 @@ export class ServerWorldStateSynchronizer
|
|
|
227
238
|
);
|
|
228
239
|
}
|
|
229
240
|
|
|
241
|
+
// If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
|
|
242
|
+
if (blockHash !== undefined && targetBlockNumber !== undefined) {
|
|
243
|
+
const updatedHash = await this.getL2BlockHash(targetBlockNumber);
|
|
244
|
+
if (updatedHash !== blockHash.toString()) {
|
|
245
|
+
throw new WorldStateSynchronizerError(
|
|
246
|
+
`Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`,
|
|
247
|
+
{
|
|
248
|
+
cause: {
|
|
249
|
+
reason: 'block_hash_mismatch',
|
|
250
|
+
targetBlockNumber,
|
|
251
|
+
expectedHash: blockHash.toString(),
|
|
252
|
+
actualHash: updatedHash,
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
230
259
|
return updatedBlockNumber;
|
|
231
260
|
}
|
|
232
261
|
|
|
@@ -263,15 +292,19 @@ export class ServerWorldStateSynchronizer
|
|
|
263
292
|
proposed: latestBlockId,
|
|
264
293
|
checkpointed: {
|
|
265
294
|
block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
|
|
266
|
-
checkpoint: { number:
|
|
295
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
296
|
+
},
|
|
297
|
+
proposedCheckpoint: {
|
|
298
|
+
block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
|
|
299
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
267
300
|
},
|
|
268
301
|
finalized: {
|
|
269
302
|
block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
|
|
270
|
-
checkpoint: { number:
|
|
303
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
271
304
|
},
|
|
272
305
|
proven: {
|
|
273
306
|
block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
|
|
274
|
-
checkpoint: { number:
|
|
307
|
+
checkpoint: { number: INITIAL_CHECKPOINT_NUMBER, hash: genesisCheckpointHeaderHash },
|
|
275
308
|
},
|
|
276
309
|
};
|
|
277
310
|
}
|
|
@@ -360,6 +393,18 @@ export class ServerWorldStateSynchronizer
|
|
|
360
393
|
|
|
361
394
|
private async handleChainFinalized(blockNumber: BlockNumber) {
|
|
362
395
|
this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
|
|
396
|
+
// If the finalized block number is older than the oldest available block in world state,
|
|
397
|
+
// skip entirely. The finalized block number can jump backwards (e.g. when the finalization
|
|
398
|
+
// heuristic changes) and try to read block data that has already been pruned. When this
|
|
399
|
+
// happens, there is nothing useful to do — the native world state is already finalized
|
|
400
|
+
// past this point and pruning has already happened.
|
|
401
|
+
const currentSummary = await this.merkleTreeDb.getStatusSummary();
|
|
402
|
+
if (blockNumber < currentSummary.oldestHistoricalBlock || blockNumber < 1) {
|
|
403
|
+
this.log.trace(
|
|
404
|
+
`Finalized block ${blockNumber} is older than the oldest available block ${currentSummary.oldestHistoricalBlock}. Skipping.`,
|
|
405
|
+
);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
363
408
|
const summary = await this.merkleTreeDb.setFinalized(blockNumber);
|
|
364
409
|
if (this.historyToKeep === undefined) {
|
|
365
410
|
return;
|
|
@@ -393,6 +438,12 @@ export class ServerWorldStateSynchronizer
|
|
|
393
438
|
}
|
|
394
439
|
// Find the block at the start of the checkpoint and remove blocks up to this one
|
|
395
440
|
const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
|
|
441
|
+
if (newHistoricBlock.number <= currentSummary.oldestHistoricalBlock) {
|
|
442
|
+
this.log.debug(
|
|
443
|
+
`Historic block ${newHistoricBlock.number} is not newer than oldest available ${currentSummary.oldestHistoricalBlock}. Skipping prune.`,
|
|
444
|
+
);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
396
447
|
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
|
|
397
448
|
const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
|
|
398
449
|
this.log.debug(`World state summary `, status.summary);
|
|
@@ -405,9 +456,11 @@ export class ServerWorldStateSynchronizer
|
|
|
405
456
|
}
|
|
406
457
|
|
|
407
458
|
private async handleChainPruned(blockNumber: BlockNumber) {
|
|
408
|
-
this.log.
|
|
459
|
+
this.log.info(`Chain pruned to block ${blockNumber}`);
|
|
409
460
|
const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
|
|
410
|
-
this.provenBlockNumber
|
|
461
|
+
if (this.provenBlockNumber !== undefined && this.provenBlockNumber > blockNumber) {
|
|
462
|
+
this.provenBlockNumber = undefined;
|
|
463
|
+
}
|
|
411
464
|
this.instrumentation.updateWorldStateMetrics(status);
|
|
412
465
|
}
|
|
413
466
|
|
package/src/testing.ts
CHANGED
|
@@ -3,22 +3,19 @@ import { Fr } from '@aztec/foundation/curves/bn254';
|
|
|
3
3
|
import { computeFeePayerBalanceLeafSlot } from '@aztec/protocol-contracts/fee-juice';
|
|
4
4
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
5
5
|
import { MerkleTreeId, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
|
|
6
|
+
import type { GenesisData } from '@aztec/stdlib/world-state';
|
|
6
7
|
|
|
7
8
|
import { NativeWorldStateService } from './native/index.js';
|
|
8
9
|
|
|
9
|
-
async function generateGenesisValues(
|
|
10
|
-
if (!prefilledPublicData.length) {
|
|
10
|
+
async function generateGenesisValues(genesis: GenesisData) {
|
|
11
|
+
if (!genesis.prefilledPublicData.length && genesis.genesisTimestamp === 0n) {
|
|
11
12
|
return {
|
|
12
13
|
genesisArchiveRoot: new Fr(GENESIS_ARCHIVE_ROOT),
|
|
13
14
|
};
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
// Create a temporary world state to compute the genesis values.
|
|
17
|
-
const ws = await NativeWorldStateService.tmp(
|
|
18
|
-
undefined /* rollupAddress */,
|
|
19
|
-
true /* cleanupTmpDir */,
|
|
20
|
-
prefilledPublicData,
|
|
21
|
-
);
|
|
18
|
+
const ws = await NativeWorldStateService.tmp(undefined /* rollupAddress */, true /* cleanupTmpDir */, genesis);
|
|
22
19
|
const genesisArchiveRoot = new Fr((await ws.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
|
|
23
20
|
await ws.close();
|
|
24
21
|
|
|
@@ -33,6 +30,7 @@ export async function getGenesisValues(
|
|
|
33
30
|
initialAccounts: AztecAddress[],
|
|
34
31
|
initialAccountFeeJuice = defaultInitialAccountFeeJuice,
|
|
35
32
|
genesisPublicData: PublicDataTreeLeaf[] = [],
|
|
33
|
+
genesisTimestamp: bigint = 0n,
|
|
36
34
|
) {
|
|
37
35
|
// Top up the accounts with fee juice.
|
|
38
36
|
let prefilledPublicData = await Promise.all(
|
|
@@ -46,11 +44,12 @@ export async function getGenesisValues(
|
|
|
46
44
|
|
|
47
45
|
prefilledPublicData.sort((a, b) => (b.slot.lt(a.slot) ? 1 : -1));
|
|
48
46
|
|
|
49
|
-
const
|
|
47
|
+
const genesis: GenesisData = { prefilledPublicData, genesisTimestamp };
|
|
48
|
+
const { genesisArchiveRoot } = await generateGenesisValues(genesis);
|
|
50
49
|
|
|
51
50
|
return {
|
|
52
51
|
genesisArchiveRoot,
|
|
53
|
-
|
|
52
|
+
genesis,
|
|
54
53
|
fundingNeeded: BigInt(initialAccounts.length) * initialAccountFeeJuice.toBigInt(),
|
|
55
54
|
};
|
|
56
55
|
}
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/constants';
|
|
2
2
|
import type { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
3
|
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
|
-
import type { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
|
|
5
4
|
import type { L2Block } from '@aztec/stdlib/block';
|
|
6
5
|
import type {
|
|
7
6
|
ForkMerkleTreeOperations,
|
|
8
7
|
MerkleTreeReadOperations,
|
|
9
8
|
ReadonlyWorldStateAccess,
|
|
10
9
|
} from '@aztec/stdlib/interfaces/server';
|
|
11
|
-
import type { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
12
10
|
|
|
13
11
|
import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
|
|
14
12
|
|
|
@@ -31,14 +29,6 @@ export const INITIAL_NULLIFIER_TREE_SIZE = 2 * MAX_NULLIFIERS_PER_TX;
|
|
|
31
29
|
|
|
32
30
|
export const INITIAL_PUBLIC_DATA_TREE_SIZE = 2 * MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
|
|
33
31
|
|
|
34
|
-
export type TreeSnapshots = {
|
|
35
|
-
[MerkleTreeId.NULLIFIER_TREE]: IndexedTreeSnapshot;
|
|
36
|
-
[MerkleTreeId.NOTE_HASH_TREE]: TreeSnapshot<Fr>;
|
|
37
|
-
[MerkleTreeId.PUBLIC_DATA_TREE]: IndexedTreeSnapshot;
|
|
38
|
-
[MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: TreeSnapshot<Fr>;
|
|
39
|
-
[MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
32
|
export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
|
|
43
33
|
/**
|
|
44
34
|
* Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
|