@aztec/world-state 0.0.1-commit.d3ec352c → 0.0.1-commit.f295ac2
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/instrumentation/instrumentation.d.ts +1 -1
- package/dest/instrumentation/instrumentation.d.ts.map +1 -1
- package/dest/instrumentation/instrumentation.js +11 -42
- package/dest/native/merkle_trees_facade.d.ts +8 -3
- package/dest/native/merkle_trees_facade.d.ts.map +1 -1
- package/dest/native/merkle_trees_facade.js +23 -3
- package/dest/native/message.d.ts +2 -2
- package/dest/native/message.d.ts.map +1 -1
- package/dest/native/message.js +1 -1
- package/dest/native/native_world_state.d.ts +6 -4
- package/dest/native/native_world_state.d.ts.map +1 -1
- package/dest/native/native_world_state.js +5 -4
- package/dest/synchronizer/server_world_state_synchronizer.d.ts +5 -4
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +66 -38
- package/dest/test/utils.d.ts +7 -13
- package/dest/test/utils.d.ts.map +1 -1
- package/dest/test/utils.js +42 -79
- package/dest/testing.d.ts +2 -2
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +1 -1
- package/dest/world-state-db/merkle_tree_db.d.ts +6 -8
- package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
- package/package.json +11 -11
- package/src/instrumentation/instrumentation.ts +10 -42
- package/src/native/merkle_trees_facade.ts +26 -2
- package/src/native/message.ts +1 -1
- package/src/native/native_world_state.ts +8 -7
- package/src/synchronizer/server_world_state_synchronizer.ts +69 -49
- package/src/test/utils.ts +68 -126
- package/src/testing.ts +1 -1
- package/src/world-state-db/merkle_tree_db.ts +9 -11
- package/dest/synchronizer/utils.d.ts +0 -11
- package/dest/synchronizer/utils.d.ts.map +0 -1
- package/dest/synchronizer/utils.js +0 -59
- package/src/synchronizer/utils.ts +0 -83
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
-
import { Fr } from '@aztec/foundation/
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
3
4
|
import { serializeToBuffer } from '@aztec/foundation/serialize';
|
|
5
|
+
import { sleep } from '@aztec/foundation/sleep';
|
|
4
6
|
import { type IndexedTreeLeafPreimage, SiblingPath } from '@aztec/foundation/trees';
|
|
5
7
|
import type {
|
|
6
8
|
BatchInsertionResult,
|
|
@@ -204,7 +206,14 @@ export class MerkleTreesFacade implements MerkleTreeReadOperations {
|
|
|
204
206
|
}
|
|
205
207
|
|
|
206
208
|
export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTreeWriteOperations {
|
|
207
|
-
|
|
209
|
+
private log = createLogger('world-state:merkle-trees-fork-facade');
|
|
210
|
+
|
|
211
|
+
constructor(
|
|
212
|
+
instance: NativeWorldStateInstance,
|
|
213
|
+
initialHeader: BlockHeader,
|
|
214
|
+
revision: WorldStateRevision,
|
|
215
|
+
private opts: { closeDelayMs?: number },
|
|
216
|
+
) {
|
|
208
217
|
assert.notEqual(revision.forkId, 0, 'Fork ID must be set');
|
|
209
218
|
assert.equal(revision.includeUncommitted, true, 'Fork must include uncommitted data');
|
|
210
219
|
super(instance, initialHeader, revision);
|
|
@@ -286,6 +295,21 @@ export class MerkleTreesForkFacade extends MerkleTreesFacade implements MerkleTr
|
|
|
286
295
|
await this.instance.call(WorldStateMessageType.DELETE_FORK, { forkId: this.revision.forkId });
|
|
287
296
|
}
|
|
288
297
|
|
|
298
|
+
async [Symbol.dispose](): Promise<void> {
|
|
299
|
+
if (this.opts.closeDelayMs) {
|
|
300
|
+
void sleep(this.opts.closeDelayMs)
|
|
301
|
+
.then(() => this.close())
|
|
302
|
+
.catch(err => {
|
|
303
|
+
if (err && 'message' in err && err.message === 'Native instance is closed') {
|
|
304
|
+
return; // Ignore errors due to native instance being closed
|
|
305
|
+
}
|
|
306
|
+
this.log.warn('Error closing MerkleTreesForkFacade after delay', { err });
|
|
307
|
+
});
|
|
308
|
+
} else {
|
|
309
|
+
await this.close();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
289
313
|
public async createCheckpoint(): Promise<void> {
|
|
290
314
|
assert.notEqual(this.revision.forkId, 0, 'Fork ID must be set');
|
|
291
315
|
await this.instance.call(WorldStateMessageType.CREATE_CHECKPOINT, { forkId: this.revision.forkId });
|
package/src/native/message.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
-
import { Fr } from '@aztec/foundation/
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
3
|
import type { Tuple } from '@aztec/foundation/serialize';
|
|
4
4
|
import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
|
|
5
5
|
import type { StateReference } from '@aztec/stdlib/tx';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
|
|
2
2
|
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
3
|
import { fromEntries, padArrayEnd } from '@aztec/foundation/collection';
|
|
4
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
5
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
5
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
6
6
|
import { tryRmDir } from '@aztec/foundation/fs';
|
|
7
7
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
8
8
|
import type { L2BlockNew } from '@aztec/stdlib/block';
|
|
@@ -159,7 +159,10 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
159
159
|
);
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
public async fork(
|
|
162
|
+
public async fork(
|
|
163
|
+
blockNumber?: BlockNumber,
|
|
164
|
+
opts: { closeDelayMs?: number } = {},
|
|
165
|
+
): Promise<MerkleTreeWriteOperations> {
|
|
163
166
|
const resp = await this.instance.call(WorldStateMessageType.CREATE_FORK, {
|
|
164
167
|
latest: blockNumber === undefined,
|
|
165
168
|
blockNumber: blockNumber ?? BlockNumber.ZERO,
|
|
@@ -173,6 +176,7 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
173
176
|
/* blockNumber=*/ BlockNumber.ZERO,
|
|
174
177
|
/* includeUncommitted=*/ true,
|
|
175
178
|
),
|
|
179
|
+
opts,
|
|
176
180
|
);
|
|
177
181
|
}
|
|
178
182
|
|
|
@@ -180,11 +184,8 @@ export class NativeWorldStateService implements MerkleTreeDatabase {
|
|
|
180
184
|
return this.initialHeader!;
|
|
181
185
|
}
|
|
182
186
|
|
|
183
|
-
public async handleL2BlockAndMessages(
|
|
184
|
-
l2Block
|
|
185
|
-
l1ToL2Messages: Fr[],
|
|
186
|
-
isFirstBlock: boolean,
|
|
187
|
-
): Promise<WorldStateStatusFull> {
|
|
187
|
+
public async handleL2BlockAndMessages(l2Block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
|
|
188
|
+
const isFirstBlock = l2Block.indexWithinCheckpoint === 0;
|
|
188
189
|
if (!isFirstBlock && l1ToL2Messages.length > 0) {
|
|
189
190
|
throw new Error(
|
|
190
191
|
`L1 to L2 messages must be empty for non-first blocks, but got ${l1ToL2Messages.length} messages for block ${l2Block.number}.`,
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
+
import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM, INITIAL_L2_CHECKPOINT_NUM } from '@aztec/constants';
|
|
1
2
|
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
2
|
-
import type { Fr } from '@aztec/foundation/
|
|
3
|
+
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
4
|
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
4
5
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
5
6
|
import { elapsed } from '@aztec/foundation/timer';
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
import {
|
|
8
|
+
GENESIS_CHECKPOINT_HEADER_HASH,
|
|
9
|
+
type L2BlockId,
|
|
10
|
+
type L2BlockNew,
|
|
11
|
+
type L2BlockSource,
|
|
10
12
|
L2BlockStream,
|
|
11
|
-
L2BlockStreamEvent,
|
|
12
|
-
L2BlockStreamEventHandler,
|
|
13
|
-
L2BlockStreamLocalDataProvider,
|
|
14
|
-
L2Tips,
|
|
13
|
+
type L2BlockStreamEvent,
|
|
14
|
+
type L2BlockStreamEventHandler,
|
|
15
|
+
type L2BlockStreamLocalDataProvider,
|
|
16
|
+
type L2Tips,
|
|
15
17
|
} from '@aztec/stdlib/block';
|
|
16
18
|
import {
|
|
17
19
|
WorldStateRunningState,
|
|
@@ -23,14 +25,13 @@ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
|
23
25
|
import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
|
|
24
26
|
import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
|
|
25
27
|
import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
26
|
-
import {
|
|
28
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
27
29
|
|
|
28
30
|
import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
|
|
29
31
|
import type { WorldStateStatusFull } from '../native/message.js';
|
|
30
32
|
import type { MerkleTreeAdminDatabase } from '../world-state-db/merkle_tree_db.js';
|
|
31
33
|
import type { WorldStateConfig } from './config.js';
|
|
32
34
|
import { WorldStateSynchronizerError } from './errors.js';
|
|
33
|
-
import { findFirstBlocksInCheckpoints } from './utils.js';
|
|
34
35
|
|
|
35
36
|
export type { SnapshotDataKeys };
|
|
36
37
|
|
|
@@ -47,7 +48,6 @@ export class ServerWorldStateSynchronizer
|
|
|
47
48
|
private latestBlockNumberAtStart = BlockNumber.ZERO;
|
|
48
49
|
private historyToKeep: number | undefined;
|
|
49
50
|
private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
|
|
50
|
-
private latestBlockHashQuery: { blockNumber: BlockNumber; hash: string | undefined } | undefined = undefined;
|
|
51
51
|
|
|
52
52
|
private syncPromise = promiseWithResolvers<void>();
|
|
53
53
|
protected blockStream: L2BlockStream | undefined;
|
|
@@ -80,8 +80,8 @@ export class ServerWorldStateSynchronizer
|
|
|
80
80
|
return this.merkleTreeDb.getSnapshot(blockNumber);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
public fork(blockNumber?: BlockNumber): Promise<MerkleTreeWriteOperations> {
|
|
84
|
-
return this.merkleTreeDb.fork(blockNumber);
|
|
83
|
+
public fork(blockNumber?: BlockNumber, opts?: { closeDelayMs?: number }): Promise<MerkleTreeWriteOperations> {
|
|
84
|
+
return this.merkleTreeDb.fork(blockNumber, opts);
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
|
|
@@ -127,12 +127,12 @@ export class ServerWorldStateSynchronizer
|
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
protected createBlockStream(): L2BlockStream {
|
|
130
|
-
const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
|
|
131
130
|
const logger = createLogger('world-state:block_stream');
|
|
132
|
-
return new
|
|
131
|
+
return new L2BlockStream(this.l2BlockSource, this, this, logger, {
|
|
133
132
|
proven: this.config.worldStateProvenBlocksOnly,
|
|
134
133
|
pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
|
|
135
134
|
batchSize: this.config.worldStateBlockRequestBatchSize,
|
|
135
|
+
ignoreCheckpoints: true,
|
|
136
136
|
});
|
|
137
137
|
}
|
|
138
138
|
|
|
@@ -161,7 +161,7 @@ export class ServerWorldStateSynchronizer
|
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
public async getLatestBlockNumber() {
|
|
164
|
-
return (await this.getL2Tips()).
|
|
164
|
+
return (await this.getL2Tips()).proposed.number;
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
public async stopSync() {
|
|
@@ -240,27 +240,44 @@ export class ServerWorldStateSynchronizer
|
|
|
240
240
|
if (number === BlockNumber.ZERO) {
|
|
241
241
|
return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
|
|
242
242
|
}
|
|
243
|
-
|
|
244
|
-
this.latestBlockHashQuery = {
|
|
245
|
-
hash: await this.merkleTreeCommitted
|
|
246
|
-
.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number))
|
|
247
|
-
.then(leaf => leaf?.toString()),
|
|
248
|
-
blockNumber: number,
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
return this.latestBlockHashQuery.hash;
|
|
243
|
+
return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then(leaf => leaf?.toString());
|
|
252
244
|
}
|
|
253
245
|
|
|
254
246
|
/** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
|
|
255
247
|
public async getL2Tips(): Promise<L2Tips> {
|
|
256
248
|
const status = await this.merkleTreeDb.getStatusSummary();
|
|
257
|
-
const
|
|
249
|
+
const unfinalizedBlockHashPromise = this.getL2BlockHash(status.unfinalizedBlockNumber);
|
|
250
|
+
const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
|
|
251
|
+
|
|
252
|
+
const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
|
|
253
|
+
const provenBlockHashPromise =
|
|
254
|
+
this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
|
|
255
|
+
|
|
256
|
+
const [unfinalizedBlockHash, finalizedBlockHash, provenBlockHash] = await Promise.all([
|
|
257
|
+
unfinalizedBlockHashPromise,
|
|
258
|
+
finalizedBlockHashPromise,
|
|
259
|
+
provenBlockHashPromise,
|
|
260
|
+
]);
|
|
258
261
|
const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
|
|
259
262
|
|
|
263
|
+
// World state doesn't track checkpointed blocks or checkpoints themselves.
|
|
264
|
+
// but we use a block stream so we need to provide 'local' L2Tips.
|
|
265
|
+
// We configure the block stream to ignore checkpoints and set checkpoint values to genesis here.
|
|
266
|
+
const genesisCheckpointHeaderHash = GENESIS_CHECKPOINT_HEADER_HASH.toString();
|
|
260
267
|
return {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
268
|
+
proposed: latestBlockId,
|
|
269
|
+
checkpointed: {
|
|
270
|
+
block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
|
|
271
|
+
checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
|
|
272
|
+
},
|
|
273
|
+
finalized: {
|
|
274
|
+
block: { number: status.finalizedBlockNumber, hash: finalizedBlockHash ?? '' },
|
|
275
|
+
checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
|
|
276
|
+
},
|
|
277
|
+
proven: {
|
|
278
|
+
block: { number: provenBlockNumber, hash: provenBlockHash ?? '' },
|
|
279
|
+
checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
|
|
280
|
+
},
|
|
264
281
|
};
|
|
265
282
|
}
|
|
266
283
|
|
|
@@ -268,16 +285,16 @@ export class ServerWorldStateSynchronizer
|
|
|
268
285
|
public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
|
|
269
286
|
switch (event.type) {
|
|
270
287
|
case 'blocks-added':
|
|
271
|
-
await this.handleL2Blocks(event.blocks
|
|
288
|
+
await this.handleL2Blocks(event.blocks);
|
|
272
289
|
break;
|
|
273
290
|
case 'chain-pruned':
|
|
274
|
-
await this.handleChainPruned(
|
|
291
|
+
await this.handleChainPruned(event.block.number);
|
|
275
292
|
break;
|
|
276
293
|
case 'chain-proven':
|
|
277
|
-
await this.handleChainProven(
|
|
294
|
+
await this.handleChainProven(event.block.number);
|
|
278
295
|
break;
|
|
279
296
|
case 'chain-finalized':
|
|
280
|
-
await this.handleChainFinalized(
|
|
297
|
+
await this.handleChainFinalized(event.block.number);
|
|
281
298
|
break;
|
|
282
299
|
}
|
|
283
300
|
}
|
|
@@ -290,13 +307,22 @@ export class ServerWorldStateSynchronizer
|
|
|
290
307
|
private async handleL2Blocks(l2Blocks: L2BlockNew[]) {
|
|
291
308
|
this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
|
|
292
309
|
|
|
293
|
-
|
|
310
|
+
// Fetch the L1->L2 messages for the first block in a checkpoint.
|
|
311
|
+
const messagesForBlocks = new Map<BlockNumber, Fr[]>();
|
|
312
|
+
await Promise.all(
|
|
313
|
+
l2Blocks
|
|
314
|
+
.filter(b => b.indexWithinCheckpoint === 0)
|
|
315
|
+
.map(async block => {
|
|
316
|
+
const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
|
|
317
|
+
messagesForBlocks.set(block.number, l1ToL2Messages);
|
|
318
|
+
}),
|
|
319
|
+
);
|
|
294
320
|
|
|
295
321
|
let updateStatus: WorldStateStatusFull | undefined = undefined;
|
|
296
322
|
for (const block of l2Blocks) {
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
323
|
+
const [duration, result] = await elapsed(() =>
|
|
324
|
+
this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []),
|
|
325
|
+
);
|
|
300
326
|
this.log.info(`World state updated with L2 block ${block.number}`, {
|
|
301
327
|
eventName: 'l2-block-handled',
|
|
302
328
|
duration,
|
|
@@ -319,18 +345,13 @@ export class ServerWorldStateSynchronizer
|
|
|
319
345
|
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
320
346
|
* @returns Whether the block handled was produced by this same node.
|
|
321
347
|
*/
|
|
322
|
-
private async handleL2Block(
|
|
323
|
-
l2Block: L2BlockNew,
|
|
324
|
-
l1ToL2Messages: Fr[],
|
|
325
|
-
isFirstBlock: boolean,
|
|
326
|
-
): Promise<WorldStateStatusFull> {
|
|
327
|
-
// If the above check succeeds, we can proceed to handle the block.
|
|
348
|
+
private async handleL2Block(l2Block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
|
|
328
349
|
this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
|
|
329
350
|
blockNumber: l2Block.number,
|
|
330
351
|
blockHash: await l2Block.hash().then(h => h.toString()),
|
|
331
352
|
l1ToL2Messages: l1ToL2Messages.map(msg => msg.toString()),
|
|
332
353
|
});
|
|
333
|
-
const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages
|
|
354
|
+
const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
|
|
334
355
|
|
|
335
356
|
if (this.currentState === WorldStateRunningState.SYNCHING && l2Block.number >= this.latestBlockNumberAtStart) {
|
|
336
357
|
this.setCurrentState(WorldStateRunningState.RUNNING);
|
|
@@ -346,12 +367,12 @@ export class ServerWorldStateSynchronizer
|
|
|
346
367
|
if (this.historyToKeep === undefined) {
|
|
347
368
|
return;
|
|
348
369
|
}
|
|
349
|
-
const newHistoricBlock =
|
|
350
|
-
if (newHistoricBlock <=
|
|
370
|
+
const newHistoricBlock = summary.finalizedBlockNumber - this.historyToKeep + 1;
|
|
371
|
+
if (newHistoricBlock <= 1) {
|
|
351
372
|
return;
|
|
352
373
|
}
|
|
353
374
|
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
|
|
354
|
-
const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
|
|
375
|
+
const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock));
|
|
355
376
|
this.log.debug(`World state summary `, status.summary);
|
|
356
377
|
}
|
|
357
378
|
|
|
@@ -364,7 +385,6 @@ export class ServerWorldStateSynchronizer
|
|
|
364
385
|
private async handleChainPruned(blockNumber: BlockNumber) {
|
|
365
386
|
this.log.warn(`Chain pruned to block ${blockNumber}`);
|
|
366
387
|
const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
|
|
367
|
-
this.latestBlockHashQuery = undefined;
|
|
368
388
|
this.provenBlockNumber = undefined;
|
|
369
389
|
this.instrumentation.updateWorldStateMetrics(status);
|
|
370
390
|
}
|
package/src/test/utils.ts
CHANGED
|
@@ -4,81 +4,89 @@ import {
|
|
|
4
4
|
NULLIFIER_SUBTREE_HEIGHT,
|
|
5
5
|
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
|
|
6
6
|
} from '@aztec/constants';
|
|
7
|
-
import {
|
|
7
|
+
import { asyncMap } from '@aztec/foundation/async-map';
|
|
8
|
+
import { BlockNumber, type CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
|
|
8
9
|
import { padArrayEnd } from '@aztec/foundation/collection';
|
|
9
|
-
import { Fr } from '@aztec/foundation/
|
|
10
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
10
11
|
import { L2BlockNew } from '@aztec/stdlib/block';
|
|
11
|
-
import { Checkpoint } from '@aztec/stdlib/checkpoint';
|
|
12
12
|
import type {
|
|
13
13
|
IndexedTreeId,
|
|
14
14
|
MerkleTreeReadOperations,
|
|
15
15
|
MerkleTreeWriteOperations,
|
|
16
16
|
} from '@aztec/stdlib/interfaces/server';
|
|
17
|
-
import {
|
|
17
|
+
import { mockCheckpointAndMessages, mockL1ToL2Messages } from '@aztec/stdlib/testing';
|
|
18
18
|
import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
|
|
19
|
+
import { BlockHeader } from '@aztec/stdlib/tx';
|
|
19
20
|
|
|
20
21
|
import type { NativeWorldStateService } from '../native/native_world_state.js';
|
|
21
22
|
|
|
22
|
-
export async function
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
) {
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
export async function updateBlockState(block: L2BlockNew, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations) {
|
|
24
|
+
const insertData = async (
|
|
25
|
+
treeId: IndexedTreeId,
|
|
26
|
+
data: Buffer[][],
|
|
27
|
+
subTreeHeight: number,
|
|
28
|
+
fork: MerkleTreeWriteOperations,
|
|
29
|
+
) => {
|
|
30
|
+
for (const dataBatch of data) {
|
|
31
|
+
await fork.batchInsert(treeId, dataBatch, subTreeHeight);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
l2Block.body.txEffects.map(txEffect =>
|
|
54
|
-
padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
|
|
55
|
-
),
|
|
56
|
-
NULLIFIER_SUBTREE_HEIGHT,
|
|
57
|
-
fork,
|
|
58
|
-
);
|
|
59
|
-
const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
|
|
60
|
-
padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
const l1ToL2MessagesPadded = isFirstBlock
|
|
35
|
+
const publicDataInsert = insertData(
|
|
36
|
+
MerkleTreeId.PUBLIC_DATA_TREE,
|
|
37
|
+
block.body.txEffects.map(txEffect => txEffect.publicDataWrites.map(write => write.toBuffer())),
|
|
38
|
+
0,
|
|
39
|
+
fork,
|
|
40
|
+
);
|
|
41
|
+
const nullifierInsert = insertData(
|
|
42
|
+
MerkleTreeId.NULLIFIER_TREE,
|
|
43
|
+
block.body.txEffects.map(txEffect =>
|
|
44
|
+
padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
|
|
45
|
+
),
|
|
46
|
+
NULLIFIER_SUBTREE_HEIGHT,
|
|
47
|
+
fork,
|
|
48
|
+
);
|
|
49
|
+
const noteHashesPadded = block.body.txEffects.flatMap(txEffect =>
|
|
50
|
+
padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const l1ToL2MessagesPadded =
|
|
54
|
+
block.indexWithinCheckpoint === 0
|
|
64
55
|
? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
|
|
65
56
|
: l1ToL2Messages;
|
|
66
57
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
58
|
+
const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
|
|
59
|
+
const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
|
|
60
|
+
await Promise.all([publicDataInsert, nullifierInsert, noteHashInsert, messageInsert]);
|
|
71
61
|
|
|
72
62
|
const state = await fork.getStateReference();
|
|
73
|
-
|
|
74
|
-
await fork.updateArchive(
|
|
63
|
+
block.header = BlockHeader.from({ ...block.header, state });
|
|
64
|
+
await fork.updateArchive(block.header);
|
|
75
65
|
|
|
76
66
|
const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
|
|
77
67
|
|
|
78
|
-
|
|
68
|
+
block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function mockBlock(
|
|
72
|
+
blockNum: BlockNumber,
|
|
73
|
+
size: number,
|
|
74
|
+
fork: MerkleTreeWriteOperations,
|
|
75
|
+
maxEffects: number | undefined = 1000, // Defaults to the maximum tx effects.
|
|
76
|
+
numL1ToL2Messages: number = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
|
|
77
|
+
isFirstBlockInCheckpoint: boolean = true,
|
|
78
|
+
) {
|
|
79
|
+
const block = await L2BlockNew.random(blockNum, {
|
|
80
|
+
indexWithinCheckpoint: isFirstBlockInCheckpoint ? IndexWithinCheckpoint(0) : IndexWithinCheckpoint(1),
|
|
81
|
+
txsPerBlock: size,
|
|
82
|
+
txOptions: { maxEffects },
|
|
83
|
+
});
|
|
84
|
+
const l1ToL2Messages = mockL1ToL2Messages(numL1ToL2Messages);
|
|
85
|
+
|
|
86
|
+
await updateBlockState(block, l1ToL2Messages, fork);
|
|
79
87
|
|
|
80
88
|
return {
|
|
81
|
-
block
|
|
89
|
+
block,
|
|
82
90
|
messages: l1ToL2Messages,
|
|
83
91
|
};
|
|
84
92
|
}
|
|
@@ -89,44 +97,7 @@ export async function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWrit
|
|
|
89
97
|
|
|
90
98
|
l2Block.header.globalVariables.blockNumber = blockNum;
|
|
91
99
|
|
|
92
|
-
|
|
93
|
-
{
|
|
94
|
-
const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
|
|
95
|
-
padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
|
|
96
|
-
);
|
|
97
|
-
await fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
|
|
98
|
-
|
|
99
|
-
const l1ToL2MessagesPadded = padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
|
|
100
|
-
await fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Sync the indexed trees
|
|
104
|
-
{
|
|
105
|
-
// We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
|
|
106
|
-
for (const txEffect of l2Block.body.txEffects) {
|
|
107
|
-
await fork.batchInsert(
|
|
108
|
-
MerkleTreeId.PUBLIC_DATA_TREE,
|
|
109
|
-
txEffect.publicDataWrites.map(write => write.toBuffer()),
|
|
110
|
-
0,
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
const nullifiersPadded = padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX);
|
|
114
|
-
|
|
115
|
-
await fork.batchInsert(
|
|
116
|
-
MerkleTreeId.NULLIFIER_TREE,
|
|
117
|
-
nullifiersPadded.map(nullifier => nullifier.toBuffer()),
|
|
118
|
-
NULLIFIER_SUBTREE_HEIGHT,
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const state = await fork.getStateReference();
|
|
124
|
-
l2Block.header.state = state;
|
|
125
|
-
await fork.updateArchive(l2Block.header);
|
|
126
|
-
|
|
127
|
-
const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
|
|
128
|
-
|
|
129
|
-
l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
|
|
100
|
+
await updateBlockState(l2Block, l1ToL2Messages, fork);
|
|
130
101
|
|
|
131
102
|
return {
|
|
132
103
|
block: l2Block,
|
|
@@ -155,44 +126,15 @@ export async function mockBlocks(
|
|
|
155
126
|
return { blocks, messages: messagesArray };
|
|
156
127
|
}
|
|
157
128
|
|
|
158
|
-
export function mockL1ToL2Messages(numL1ToL2Messages: number) {
|
|
159
|
-
return Array(numL1ToL2Messages).fill(0).map(Fr.random);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
129
|
export async function mockCheckpoint(
|
|
163
130
|
checkpointNumber: CheckpointNumber,
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
numBlocks = 1,
|
|
167
|
-
numTxsPerBlock = 1,
|
|
168
|
-
numL1ToL2Messages = 1,
|
|
169
|
-
fork,
|
|
170
|
-
}: {
|
|
171
|
-
startBlockNumber?: BlockNumber;
|
|
172
|
-
numBlocks?: number;
|
|
173
|
-
numTxsPerBlock?: number;
|
|
174
|
-
numL1ToL2Messages?: number;
|
|
175
|
-
fork?: MerkleTreeWriteOperations;
|
|
176
|
-
} = {},
|
|
131
|
+
fork: MerkleTreeWriteOperations,
|
|
132
|
+
options: Partial<Parameters<typeof mockCheckpointAndMessages>[1]> = {},
|
|
177
133
|
) {
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const { block, messages } = fork
|
|
183
|
-
? await mockBlock(blockNumber, numTxsPerBlock, fork, blockNumber === startBlockNumber ? numL1ToL2Messages : 0)
|
|
184
|
-
: {
|
|
185
|
-
block: await L2BlockNew.random(blockNumber, { txsPerBlock: numTxsPerBlock, slotNumber }),
|
|
186
|
-
messages: mockL1ToL2Messages(numL1ToL2Messages),
|
|
187
|
-
};
|
|
188
|
-
blocksAndMessages.push({ block, messages });
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const messages = blocksAndMessages[0].messages;
|
|
192
|
-
const inHash = computeInHashFromL1ToL2Messages(messages);
|
|
193
|
-
const checkpoint = await Checkpoint.random(checkpointNumber, { numBlocks: 0, slotNumber, inHash });
|
|
194
|
-
checkpoint.blocks = blocksAndMessages.map(({ block }) => block);
|
|
195
|
-
|
|
134
|
+
const { checkpoint, messages } = await mockCheckpointAndMessages(checkpointNumber, options);
|
|
135
|
+
await asyncMap(checkpoint.blocks, async (block, i) => {
|
|
136
|
+
await updateBlockState(block, i === 0 ? messages : [], fork);
|
|
137
|
+
});
|
|
196
138
|
return { checkpoint, messages };
|
|
197
139
|
}
|
|
198
140
|
|
package/src/testing.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GENESIS_ARCHIVE_ROOT } from '@aztec/constants';
|
|
2
|
-
import { Fr } from '@aztec/foundation/
|
|
2
|
+
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';
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/constants';
|
|
2
|
-
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
|
-
import type { Fr } from '@aztec/foundation/
|
|
2
|
+
import type { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
4
|
import type { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
|
|
5
5
|
import type { L2BlockNew } from '@aztec/stdlib/block';
|
|
6
|
-
import type {
|
|
6
|
+
import type {
|
|
7
|
+
ForkMerkleTreeOperations,
|
|
8
|
+
MerkleTreeReadOperations,
|
|
9
|
+
ReadonlyWorldStateAccess,
|
|
10
|
+
} from '@aztec/stdlib/interfaces/server';
|
|
7
11
|
import type { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
8
12
|
|
|
9
13
|
import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
|
|
@@ -35,19 +39,13 @@ export type TreeSnapshots = {
|
|
|
35
39
|
[MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
|
|
36
40
|
};
|
|
37
41
|
|
|
38
|
-
export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
|
|
42
|
+
export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
|
|
39
43
|
/**
|
|
40
44
|
* Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
|
|
41
45
|
* @param block - The L2 block to handle.
|
|
42
46
|
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
43
|
-
* @param isFirstBlock - Whether the block is the first block in a checkpoint. The messages are padded and inserted
|
|
44
|
-
* to the tree for the first block in a checkpoint.
|
|
45
47
|
*/
|
|
46
|
-
handleL2BlockAndMessages(
|
|
47
|
-
block: L2BlockNew,
|
|
48
|
-
l1ToL2Messages: Fr[],
|
|
49
|
-
isFirstBlock: boolean,
|
|
50
|
-
): Promise<WorldStateStatusFull>;
|
|
48
|
+
handleL2BlockAndMessages(block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
|
|
51
49
|
|
|
52
50
|
/**
|
|
53
51
|
* Gets a handle that allows reading the latest committed state
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Fr } from '@aztec/foundation/fields';
|
|
2
|
-
import type { L2BlockNew, L2BlockSource } from '@aztec/stdlib/block';
|
|
3
|
-
import { type L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
4
|
-
/**
|
|
5
|
-
* Determine which blocks in the given array are the first block in a checkpoint.
|
|
6
|
-
* @param blocks - The candidate blocks, sorted by block number in ascending order.
|
|
7
|
-
* @param l2BlockSource - The L2 block source to use to fetch the checkpoints, block headers and L1->L2 messages.
|
|
8
|
-
* @returns A map of block numbers that begin a checkpoint to the L1->L2 messages for that checkpoint.
|
|
9
|
-
*/
|
|
10
|
-
export declare function findFirstBlocksInCheckpoints(blocks: L2BlockNew[], l2BlockSource: L2BlockSource & L1ToL2MessageSource): Promise<Map<number, Fr[]>>;
|
|
11
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zeW5jaHJvbml6ZXIvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxLQUFLLEVBQUUsRUFBRSxFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFDbkQsT0FBTyxLQUFLLEVBQUUsVUFBVSxFQUFFLGFBQWEsRUFBRSxNQUFNLHFCQUFxQixDQUFDO0FBRXJFLE9BQU8sRUFBRSxLQUFLLG1CQUFtQixFQUFtQyxNQUFNLHlCQUF5QixDQUFDO0FBRXBHOzs7OztHQUtHO0FBQ0gsd0JBQXNCLDRCQUE0QixDQUNoRCxNQUFNLEVBQUUsVUFBVSxFQUFFLEVBQ3BCLGFBQWEsRUFBRSxhQUFhLEdBQUcsbUJBQW1CLEdBQ2pELE9BQU8sQ0FBQyxHQUFHLENBQUMsTUFBTSxFQUFFLEVBQUUsRUFBRSxDQUFDLENBQUMsQ0FtRTVCIn0=
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/synchronizer/utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAErE,OAAO,EAAE,KAAK,mBAAmB,EAAmC,MAAM,yBAAyB,CAAC;AAEpG;;;;;GAKG;AACH,wBAAsB,4BAA4B,CAChD,MAAM,EAAE,UAAU,EAAE,EACpB,aAAa,EAAE,aAAa,GAAG,mBAAmB,GACjD,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC,CAmE5B"}
|