@aztec/world-state 0.0.1-commit.9593d84 → 0.0.1-commit.96bb3f7
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 +10 -4
- package/dest/native/merkle_trees_facade.d.ts.map +1 -1
- package/dest/native/merkle_trees_facade.js +25 -4
- package/dest/native/message.d.ts +12 -11
- package/dest/native/message.d.ts.map +1 -1
- package/dest/native/message.js +14 -13
- package/dest/native/native_world_state.d.ts +12 -9
- package/dest/native/native_world_state.d.ts.map +1 -1
- package/dest/native/native_world_state.js +13 -9
- package/dest/synchronizer/server_world_state_synchronizer.d.ts +10 -15
- package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
- package/dest/synchronizer/server_world_state_synchronizer.js +70 -58
- package/dest/test/utils.d.ts +16 -9
- package/dest/test/utils.d.ts.map +1 -1
- package/dest/test/utils.js +56 -52
- 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 +10 -11
- package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
- package/package.json +12 -12
- package/src/instrumentation/instrumentation.ts +10 -42
- package/src/native/merkle_trees_facade.ts +29 -4
- package/src/native/message.ts +23 -22
- package/src/native/native_world_state.ts +32 -20
- package/src/synchronizer/server_world_state_synchronizer.ts +88 -86
- package/src/test/utils.ts +88 -93
- package/src/testing.ts +1 -1
- package/src/world-state-db/merkle_tree_db.ts +13 -14
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import type { Fr } from '@aztec/foundation/
|
|
1
|
+
import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM, INITIAL_L2_CHECKPOINT_NUM } from '@aztec/constants';
|
|
2
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
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
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
L2BlockSource,
|
|
7
|
+
import {
|
|
8
|
+
GENESIS_CHECKPOINT_HEADER_HASH,
|
|
9
|
+
type L2BlockId,
|
|
10
|
+
type L2BlockNew,
|
|
11
|
+
type L2BlockSource,
|
|
12
12
|
L2BlockStream,
|
|
13
|
-
L2BlockStreamEvent,
|
|
14
|
-
L2BlockStreamEventHandler,
|
|
15
|
-
L2BlockStreamLocalDataProvider,
|
|
16
|
-
L2Tips,
|
|
13
|
+
type L2BlockStreamEvent,
|
|
14
|
+
type L2BlockStreamEventHandler,
|
|
15
|
+
type L2BlockStreamLocalDataProvider,
|
|
16
|
+
type L2Tips,
|
|
17
17
|
} from '@aztec/stdlib/block';
|
|
18
18
|
import {
|
|
19
19
|
WorldStateRunningState,
|
|
@@ -25,7 +25,7 @@ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
|
25
25
|
import type { SnapshotDataKeys } from '@aztec/stdlib/snapshots';
|
|
26
26
|
import type { L2BlockHandledStats } from '@aztec/stdlib/stats';
|
|
27
27
|
import { MerkleTreeId, type MerkleTreeReadOperations, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
|
|
28
|
-
import {
|
|
28
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
29
29
|
|
|
30
30
|
import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
|
|
31
31
|
import type { WorldStateStatusFull } from '../native/message.js';
|
|
@@ -45,17 +45,17 @@ export class ServerWorldStateSynchronizer
|
|
|
45
45
|
{
|
|
46
46
|
private readonly merkleTreeCommitted: MerkleTreeReadOperations;
|
|
47
47
|
|
|
48
|
-
private latestBlockNumberAtStart =
|
|
48
|
+
private latestBlockNumberAtStart = BlockNumber.ZERO;
|
|
49
49
|
private historyToKeep: number | undefined;
|
|
50
50
|
private currentState: WorldStateRunningState = WorldStateRunningState.IDLE;
|
|
51
|
-
private latestBlockHashQuery: { blockNumber:
|
|
51
|
+
private latestBlockHashQuery: { blockNumber: BlockNumber; hash: string | undefined } | undefined = undefined;
|
|
52
52
|
|
|
53
53
|
private syncPromise = promiseWithResolvers<void>();
|
|
54
54
|
protected blockStream: L2BlockStream | undefined;
|
|
55
55
|
|
|
56
56
|
// WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
|
|
57
57
|
// store the proven block number here, in the synchronizer, so that we don't end up spamming the logs with 'chain-proved' events
|
|
58
|
-
private provenBlockNumber:
|
|
58
|
+
private provenBlockNumber: BlockNumber | undefined;
|
|
59
59
|
|
|
60
60
|
constructor(
|
|
61
61
|
private readonly merkleTreeDb: MerkleTreeAdminDatabase,
|
|
@@ -77,12 +77,12 @@ export class ServerWorldStateSynchronizer
|
|
|
77
77
|
return this.merkleTreeDb.getCommitted();
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
public getSnapshot(blockNumber:
|
|
80
|
+
public getSnapshot(blockNumber: BlockNumber): MerkleTreeReadOperations {
|
|
81
81
|
return this.merkleTreeDb.getSnapshot(blockNumber);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
public fork(blockNumber?: number): Promise<MerkleTreeWriteOperations> {
|
|
85
|
-
return this.merkleTreeDb.fork(blockNumber);
|
|
84
|
+
public fork(blockNumber?: BlockNumber, opts?: { closeDelayMs?: number }): Promise<MerkleTreeWriteOperations> {
|
|
85
|
+
return this.merkleTreeDb.fork(blockNumber, opts);
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
public backupTo(dstPath: string, compact?: boolean): Promise<Record<Exclude<SnapshotDataKeys, 'archiver'>, string>> {
|
|
@@ -102,9 +102,11 @@ export class ServerWorldStateSynchronizer
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
// Get the current latest block number
|
|
105
|
-
this.latestBlockNumberAtStart =
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
this.latestBlockNumberAtStart = BlockNumber(
|
|
106
|
+
await (this.config.worldStateProvenBlocksOnly
|
|
107
|
+
? this.l2BlockSource.getProvenBlockNumber()
|
|
108
|
+
: this.l2BlockSource.getBlockNumber()),
|
|
109
|
+
);
|
|
108
110
|
|
|
109
111
|
const blockToDownloadFrom = (await this.getLatestBlockNumber()) + 1;
|
|
110
112
|
|
|
@@ -126,12 +128,12 @@ export class ServerWorldStateSynchronizer
|
|
|
126
128
|
}
|
|
127
129
|
|
|
128
130
|
protected createBlockStream(): L2BlockStream {
|
|
129
|
-
const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
|
|
130
131
|
const logger = createLogger('world-state:block_stream');
|
|
131
|
-
return new
|
|
132
|
+
return new L2BlockStream(this.l2BlockSource, this, this, logger, {
|
|
132
133
|
proven: this.config.worldStateProvenBlocksOnly,
|
|
133
134
|
pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
|
|
134
135
|
batchSize: this.config.worldStateBlockRequestBatchSize,
|
|
136
|
+
ignoreCheckpoints: true,
|
|
135
137
|
});
|
|
136
138
|
}
|
|
137
139
|
|
|
@@ -147,10 +149,10 @@ export class ServerWorldStateSynchronizer
|
|
|
147
149
|
public async status(): Promise<WorldStateSynchronizerStatus> {
|
|
148
150
|
const summary = await this.merkleTreeDb.getStatusSummary();
|
|
149
151
|
const status: WorldStateSyncStatus = {
|
|
150
|
-
latestBlockNumber:
|
|
151
|
-
latestBlockHash: (await this.getL2BlockHash(
|
|
152
|
-
finalizedBlockNumber:
|
|
153
|
-
oldestHistoricBlockNumber:
|
|
152
|
+
latestBlockNumber: summary.unfinalizedBlockNumber,
|
|
153
|
+
latestBlockHash: (await this.getL2BlockHash(summary.unfinalizedBlockNumber)) ?? '',
|
|
154
|
+
finalizedBlockNumber: summary.finalizedBlockNumber,
|
|
155
|
+
oldestHistoricBlockNumber: summary.oldestHistoricalBlock,
|
|
154
156
|
treesAreSynched: summary.treesAreSynched,
|
|
155
157
|
};
|
|
156
158
|
return {
|
|
@@ -160,7 +162,7 @@ export class ServerWorldStateSynchronizer
|
|
|
160
162
|
}
|
|
161
163
|
|
|
162
164
|
public async getLatestBlockNumber() {
|
|
163
|
-
return (await this.getL2Tips()).
|
|
165
|
+
return (await this.getL2Tips()).proposed.number;
|
|
164
166
|
}
|
|
165
167
|
|
|
166
168
|
public async stopSync() {
|
|
@@ -184,7 +186,10 @@ export class ServerWorldStateSynchronizer
|
|
|
184
186
|
* @param skipThrowIfTargetNotReached - Whether to skip throwing if the target block number is not reached.
|
|
185
187
|
* @returns A promise that resolves with the block number the world state was synced to
|
|
186
188
|
*/
|
|
187
|
-
public async syncImmediate(
|
|
189
|
+
public async syncImmediate(
|
|
190
|
+
targetBlockNumber?: BlockNumber,
|
|
191
|
+
skipThrowIfTargetNotReached?: boolean,
|
|
192
|
+
): Promise<BlockNumber> {
|
|
188
193
|
if (this.currentState !== WorldStateRunningState.RUNNING) {
|
|
189
194
|
throw new Error(`World State is not running. Unable to perform sync.`);
|
|
190
195
|
}
|
|
@@ -202,7 +207,7 @@ export class ServerWorldStateSynchronizer
|
|
|
202
207
|
|
|
203
208
|
// If the archiver is behind the target block, force an archiver sync
|
|
204
209
|
if (targetBlockNumber) {
|
|
205
|
-
const archiverLatestBlock = await this.l2BlockSource.getBlockNumber();
|
|
210
|
+
const archiverLatestBlock = BlockNumber(await this.l2BlockSource.getBlockNumber());
|
|
206
211
|
if (archiverLatestBlock < targetBlockNumber) {
|
|
207
212
|
this.log.debug(`Archiver is at ${archiverLatestBlock} behind target block ${targetBlockNumber}.`);
|
|
208
213
|
await this.l2BlockSource.syncImmediate();
|
|
@@ -232,8 +237,8 @@ export class ServerWorldStateSynchronizer
|
|
|
232
237
|
}
|
|
233
238
|
|
|
234
239
|
/** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */
|
|
235
|
-
public async getL2BlockHash(number:
|
|
236
|
-
if (number ===
|
|
240
|
+
public async getL2BlockHash(number: BlockNumber): Promise<string | undefined> {
|
|
241
|
+
if (number === BlockNumber.ZERO) {
|
|
237
242
|
return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
|
|
238
243
|
}
|
|
239
244
|
if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
|
|
@@ -250,13 +255,27 @@ export class ServerWorldStateSynchronizer
|
|
|
250
255
|
/** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */
|
|
251
256
|
public async getL2Tips(): Promise<L2Tips> {
|
|
252
257
|
const status = await this.merkleTreeDb.getStatusSummary();
|
|
253
|
-
const unfinalizedBlockHash = await this.getL2BlockHash(
|
|
254
|
-
const latestBlockId: L2BlockId = { number:
|
|
258
|
+
const unfinalizedBlockHash = await this.getL2BlockHash(status.unfinalizedBlockNumber);
|
|
259
|
+
const latestBlockId: L2BlockId = { number: status.unfinalizedBlockNumber, hash: unfinalizedBlockHash! };
|
|
255
260
|
|
|
261
|
+
// World state doesn't track checkpointed blocks or checkpoints themselves.
|
|
262
|
+
// but we use a block stream so we need to provide 'local' L2Tips.
|
|
263
|
+
// We configure the block stream to ignore checkpoints and set checkpoint values to genesis here.
|
|
264
|
+
const genesisCheckpointHeaderHash = GENESIS_CHECKPOINT_HEADER_HASH.toString();
|
|
256
265
|
return {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
266
|
+
proposed: latestBlockId,
|
|
267
|
+
checkpointed: {
|
|
268
|
+
block: { number: INITIAL_L2_BLOCK_NUM, hash: GENESIS_BLOCK_HEADER_HASH.toString() },
|
|
269
|
+
checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
|
|
270
|
+
},
|
|
271
|
+
finalized: {
|
|
272
|
+
block: { number: status.finalizedBlockNumber, hash: '' },
|
|
273
|
+
checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
|
|
274
|
+
},
|
|
275
|
+
proven: {
|
|
276
|
+
block: { number: this.provenBlockNumber ?? status.finalizedBlockNumber, hash: '' },
|
|
277
|
+
checkpoint: { number: INITIAL_L2_CHECKPOINT_NUM, hash: genesisCheckpointHeaderHash },
|
|
278
|
+
}, // TODO(palla/reorg): Using finalized as proven for now
|
|
260
279
|
};
|
|
261
280
|
}
|
|
262
281
|
|
|
@@ -264,7 +283,7 @@ export class ServerWorldStateSynchronizer
|
|
|
264
283
|
public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
|
|
265
284
|
switch (event.type) {
|
|
266
285
|
case 'blocks-added':
|
|
267
|
-
await this.handleL2Blocks(event.blocks
|
|
286
|
+
await this.handleL2Blocks(event.blocks);
|
|
268
287
|
break;
|
|
269
288
|
case 'chain-pruned':
|
|
270
289
|
await this.handleChainPruned(event.block.number);
|
|
@@ -283,22 +302,32 @@ export class ServerWorldStateSynchronizer
|
|
|
283
302
|
* @param l2Blocks - The L2 blocks to handle.
|
|
284
303
|
* @returns Whether the block handled was produced by this same node.
|
|
285
304
|
*/
|
|
286
|
-
private async handleL2Blocks(l2Blocks:
|
|
305
|
+
private async handleL2Blocks(l2Blocks: L2BlockNew[]) {
|
|
287
306
|
this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1)!.number}`);
|
|
288
307
|
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
|
|
308
|
+
// Fetch the L1->L2 messages for the first block in a checkpoint.
|
|
309
|
+
const messagesForBlocks = new Map<BlockNumber, Fr[]>();
|
|
310
|
+
await Promise.all(
|
|
311
|
+
l2Blocks
|
|
312
|
+
.filter(b => b.indexWithinCheckpoint === 0)
|
|
313
|
+
.map(async block => {
|
|
314
|
+
const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
|
|
315
|
+
messagesForBlocks.set(block.number, l1ToL2Messages);
|
|
316
|
+
}),
|
|
317
|
+
);
|
|
292
318
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
319
|
+
let updateStatus: WorldStateStatusFull | undefined = undefined;
|
|
320
|
+
for (const block of l2Blocks) {
|
|
321
|
+
const [duration, result] = await elapsed(() =>
|
|
322
|
+
this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []),
|
|
323
|
+
);
|
|
324
|
+
this.log.info(`World state updated with L2 block ${block.number}`, {
|
|
296
325
|
eventName: 'l2-block-handled',
|
|
297
326
|
duration,
|
|
298
|
-
unfinalizedBlockNumber: result.summary.unfinalizedBlockNumber,
|
|
299
|
-
finalizedBlockNumber: result.summary.finalizedBlockNumber,
|
|
300
|
-
oldestHistoricBlock: result.summary.oldestHistoricalBlock,
|
|
301
|
-
...
|
|
327
|
+
unfinalizedBlockNumber: BigInt(result.summary.unfinalizedBlockNumber),
|
|
328
|
+
finalizedBlockNumber: BigInt(result.summary.finalizedBlockNumber),
|
|
329
|
+
oldestHistoricBlock: BigInt(result.summary.oldestHistoricalBlock),
|
|
330
|
+
...block.getStats(),
|
|
302
331
|
} satisfies L2BlockHandledStats);
|
|
303
332
|
updateStatus = result;
|
|
304
333
|
}
|
|
@@ -314,14 +343,7 @@ export class ServerWorldStateSynchronizer
|
|
|
314
343
|
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
315
344
|
* @returns Whether the block handled was produced by this same node.
|
|
316
345
|
*/
|
|
317
|
-
private async handleL2Block(l2Block:
|
|
318
|
-
// First we check that the L1 to L2 messages hash to the block inHash.
|
|
319
|
-
// Note that we cannot optimize this check by checking the root of the subtree after inserting the messages
|
|
320
|
-
// to the real L1_TO_L2_MESSAGE_TREE (like we do in merkleTreeDb.handleL2BlockAndMessages(...)) because that
|
|
321
|
-
// tree uses pedersen and we don't have access to the converted root.
|
|
322
|
-
await this.verifyMessagesHashToInHash(l1ToL2Messages, l2Block.header.contentCommitment.inHash);
|
|
323
|
-
|
|
324
|
-
// If the above check succeeds, we can proceed to handle the block.
|
|
346
|
+
private async handleL2Block(l2Block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull> {
|
|
325
347
|
this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
|
|
326
348
|
blockNumber: l2Block.number,
|
|
327
349
|
blockHash: await l2Block.hash().then(h => h.toString()),
|
|
@@ -337,30 +359,30 @@ export class ServerWorldStateSynchronizer
|
|
|
337
359
|
return result;
|
|
338
360
|
}
|
|
339
361
|
|
|
340
|
-
private async handleChainFinalized(blockNumber:
|
|
362
|
+
private async handleChainFinalized(blockNumber: BlockNumber) {
|
|
341
363
|
this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
|
|
342
|
-
const summary = await this.merkleTreeDb.setFinalized(
|
|
364
|
+
const summary = await this.merkleTreeDb.setFinalized(blockNumber);
|
|
343
365
|
if (this.historyToKeep === undefined) {
|
|
344
366
|
return;
|
|
345
367
|
}
|
|
346
|
-
const newHistoricBlock = summary.finalizedBlockNumber -
|
|
368
|
+
const newHistoricBlock = summary.finalizedBlockNumber - this.historyToKeep + 1;
|
|
347
369
|
if (newHistoricBlock <= 1) {
|
|
348
370
|
return;
|
|
349
371
|
}
|
|
350
372
|
this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
|
|
351
|
-
const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
|
|
373
|
+
const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock));
|
|
352
374
|
this.log.debug(`World state summary `, status.summary);
|
|
353
375
|
}
|
|
354
376
|
|
|
355
|
-
private handleChainProven(blockNumber:
|
|
356
|
-
this.provenBlockNumber =
|
|
377
|
+
private handleChainProven(blockNumber: BlockNumber) {
|
|
378
|
+
this.provenBlockNumber = blockNumber;
|
|
357
379
|
this.log.debug(`Proven chain is now at block ${blockNumber}`);
|
|
358
380
|
return Promise.resolve();
|
|
359
381
|
}
|
|
360
382
|
|
|
361
|
-
private async handleChainPruned(blockNumber:
|
|
383
|
+
private async handleChainPruned(blockNumber: BlockNumber) {
|
|
362
384
|
this.log.warn(`Chain pruned to block ${blockNumber}`);
|
|
363
|
-
const status = await this.merkleTreeDb.unwindBlocks(
|
|
385
|
+
const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
|
|
364
386
|
this.latestBlockHashQuery = undefined;
|
|
365
387
|
this.provenBlockNumber = undefined;
|
|
366
388
|
this.instrumentation.updateWorldStateMetrics(status);
|
|
@@ -374,24 +396,4 @@ export class ServerWorldStateSynchronizer
|
|
|
374
396
|
this.currentState = newState;
|
|
375
397
|
this.log.debug(`Moved to state ${WorldStateRunningState[this.currentState]}`);
|
|
376
398
|
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Verifies that the L1 to L2 messages hash to the block inHash.
|
|
380
|
-
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
381
|
-
* @param inHash - The inHash of the block.
|
|
382
|
-
* @throws If the L1 to L2 messages do not hash to the block inHash.
|
|
383
|
-
*/
|
|
384
|
-
protected async verifyMessagesHashToInHash(l1ToL2Messages: Fr[], inHash: Fr) {
|
|
385
|
-
const treeCalculator = await MerkleTreeCalculator.create(
|
|
386
|
-
L1_TO_L2_MSG_SUBTREE_HEIGHT,
|
|
387
|
-
Buffer.alloc(32),
|
|
388
|
-
(lhs, rhs) => Promise.resolve(new SHA256Trunc().hash(lhs, rhs)),
|
|
389
|
-
);
|
|
390
|
-
|
|
391
|
-
const root = await treeCalculator.computeTreeRoot(l1ToL2Messages.map(msg => msg.toBuffer()));
|
|
392
|
-
|
|
393
|
-
if (!root.equals(inHash.toBuffer())) {
|
|
394
|
-
throw new Error('Obtained L1 to L2 messages failed to be hashed to the block inHash');
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
399
|
}
|
package/src/test/utils.ts
CHANGED
|
@@ -4,122 +4,100 @@ import {
|
|
|
4
4
|
NULLIFIER_SUBTREE_HEIGHT,
|
|
5
5
|
NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
|
|
6
6
|
} from '@aztec/constants';
|
|
7
|
+
import { asyncMap } from '@aztec/foundation/async-map';
|
|
8
|
+
import { BlockNumber, type CheckpointNumber } from '@aztec/foundation/branded-types';
|
|
7
9
|
import { padArrayEnd } from '@aztec/foundation/collection';
|
|
8
|
-
import { Fr } from '@aztec/foundation/
|
|
9
|
-
import {
|
|
10
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
11
|
+
import { L2BlockNew } from '@aztec/stdlib/block';
|
|
10
12
|
import type {
|
|
11
13
|
IndexedTreeId,
|
|
12
14
|
MerkleTreeReadOperations,
|
|
13
15
|
MerkleTreeWriteOperations,
|
|
14
16
|
} from '@aztec/stdlib/interfaces/server';
|
|
17
|
+
import { mockCheckpointAndMessages, mockL1ToL2Messages } from '@aztec/stdlib/testing';
|
|
15
18
|
import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
|
|
19
|
+
import { BlockHeader } from '@aztec/stdlib/tx';
|
|
16
20
|
|
|
17
21
|
import type { NativeWorldStateService } from '../native/native_world_state.js';
|
|
18
22
|
|
|
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
|
+
};
|
|
34
|
+
|
|
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
|
|
55
|
+
? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
|
|
56
|
+
: l1ToL2Messages;
|
|
57
|
+
|
|
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]);
|
|
61
|
+
|
|
62
|
+
const state = await fork.getStateReference();
|
|
63
|
+
block.header = BlockHeader.from({ ...block.header, state });
|
|
64
|
+
await fork.updateArchive(block.header);
|
|
65
|
+
|
|
66
|
+
const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
|
|
67
|
+
|
|
68
|
+
block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
|
|
69
|
+
}
|
|
70
|
+
|
|
19
71
|
export async function mockBlock(
|
|
20
|
-
blockNum:
|
|
72
|
+
blockNum: BlockNumber,
|
|
21
73
|
size: number,
|
|
22
74
|
fork: MerkleTreeWriteOperations,
|
|
23
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,
|
|
24
78
|
) {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
data: Buffer[][],
|
|
32
|
-
subTreeHeight: number,
|
|
33
|
-
fork: MerkleTreeWriteOperations,
|
|
34
|
-
) => {
|
|
35
|
-
for (const dataBatch of data) {
|
|
36
|
-
await fork.batchInsert(treeId, dataBatch, subTreeHeight);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const publicDataInsert = insertData(
|
|
41
|
-
MerkleTreeId.PUBLIC_DATA_TREE,
|
|
42
|
-
l2Block.body.txEffects.map(txEffect => txEffect.publicDataWrites.map(write => write.toBuffer())),
|
|
43
|
-
0,
|
|
44
|
-
fork,
|
|
45
|
-
);
|
|
46
|
-
const nullifierInsert = insertData(
|
|
47
|
-
MerkleTreeId.NULLIFIER_TREE,
|
|
48
|
-
l2Block.body.txEffects.map(txEffect =>
|
|
49
|
-
padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
|
|
50
|
-
),
|
|
51
|
-
NULLIFIER_SUBTREE_HEIGHT,
|
|
52
|
-
fork,
|
|
53
|
-
);
|
|
54
|
-
const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
|
|
55
|
-
padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
const l1ToL2MessagesPadded = padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
|
|
59
|
-
|
|
60
|
-
const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
|
|
61
|
-
const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
|
|
62
|
-
await Promise.all([publicDataInsert, nullifierInsert, noteHashInsert, messageInsert]);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const state = await fork.getStateReference();
|
|
66
|
-
l2Block.header.state = state;
|
|
67
|
-
await fork.updateArchive(l2Block.getBlockHeader());
|
|
68
|
-
|
|
69
|
-
const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
|
|
79
|
+
const block = await L2BlockNew.random(blockNum, {
|
|
80
|
+
indexWithinCheckpoint: isFirstBlockInCheckpoint ? 0 : 1,
|
|
81
|
+
txsPerBlock: size,
|
|
82
|
+
txOptions: { maxEffects },
|
|
83
|
+
});
|
|
84
|
+
const l1ToL2Messages = mockL1ToL2Messages(numL1ToL2Messages);
|
|
70
85
|
|
|
71
|
-
|
|
86
|
+
await updateBlockState(block, l1ToL2Messages, fork);
|
|
72
87
|
|
|
73
88
|
return {
|
|
74
|
-
block
|
|
89
|
+
block,
|
|
75
90
|
messages: l1ToL2Messages,
|
|
76
91
|
};
|
|
77
92
|
}
|
|
78
93
|
|
|
79
|
-
export async function mockEmptyBlock(blockNum:
|
|
80
|
-
const l2Block =
|
|
94
|
+
export async function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWriteOperations) {
|
|
95
|
+
const l2Block = L2BlockNew.empty();
|
|
81
96
|
const l1ToL2Messages = Array(16).fill(0).map(Fr.zero);
|
|
82
97
|
|
|
83
98
|
l2Block.header.globalVariables.blockNumber = blockNum;
|
|
84
99
|
|
|
85
|
-
|
|
86
|
-
{
|
|
87
|
-
const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
|
|
88
|
-
padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
|
|
89
|
-
);
|
|
90
|
-
await fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
|
|
91
|
-
|
|
92
|
-
const l1ToL2MessagesPadded = padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
|
|
93
|
-
await fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Sync the indexed trees
|
|
97
|
-
{
|
|
98
|
-
// We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
|
|
99
|
-
for (const txEffect of l2Block.body.txEffects) {
|
|
100
|
-
await fork.batchInsert(
|
|
101
|
-
MerkleTreeId.PUBLIC_DATA_TREE,
|
|
102
|
-
txEffect.publicDataWrites.map(write => write.toBuffer()),
|
|
103
|
-
0,
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
const nullifiersPadded = padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX);
|
|
107
|
-
|
|
108
|
-
await fork.batchInsert(
|
|
109
|
-
MerkleTreeId.NULLIFIER_TREE,
|
|
110
|
-
nullifiersPadded.map(nullifier => nullifier.toBuffer()),
|
|
111
|
-
NULLIFIER_SUBTREE_HEIGHT,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const state = await fork.getStateReference();
|
|
117
|
-
l2Block.header.state = state;
|
|
118
|
-
await fork.updateArchive(l2Block.getBlockHeader());
|
|
119
|
-
|
|
120
|
-
const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
|
|
121
|
-
|
|
122
|
-
l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
|
|
100
|
+
await updateBlockState(l2Block, l1ToL2Messages, fork);
|
|
123
101
|
|
|
124
102
|
return {
|
|
125
103
|
block: l2Block,
|
|
@@ -127,13 +105,18 @@ export async function mockEmptyBlock(blockNum: number, fork: MerkleTreeWriteOper
|
|
|
127
105
|
};
|
|
128
106
|
}
|
|
129
107
|
|
|
130
|
-
export async function mockBlocks(
|
|
131
|
-
|
|
108
|
+
export async function mockBlocks(
|
|
109
|
+
from: BlockNumber,
|
|
110
|
+
count: number,
|
|
111
|
+
numTxs: number,
|
|
112
|
+
worldState: NativeWorldStateService,
|
|
113
|
+
) {
|
|
114
|
+
const tempFork = await worldState.fork(BlockNumber(from - 1));
|
|
132
115
|
|
|
133
116
|
const blocks = [];
|
|
134
117
|
const messagesArray = [];
|
|
135
118
|
for (let blockNumber = from; blockNumber < from + count; blockNumber++) {
|
|
136
|
-
const { block, messages } = await mockBlock(blockNumber, numTxs, tempFork);
|
|
119
|
+
const { block, messages } = await mockBlock(BlockNumber(blockNumber), numTxs, tempFork);
|
|
137
120
|
blocks.push(block);
|
|
138
121
|
messagesArray.push(messages);
|
|
139
122
|
}
|
|
@@ -143,6 +126,18 @@ export async function mockBlocks(from: number, count: number, numTxs: number, wo
|
|
|
143
126
|
return { blocks, messages: messagesArray };
|
|
144
127
|
}
|
|
145
128
|
|
|
129
|
+
export async function mockCheckpoint(
|
|
130
|
+
checkpointNumber: CheckpointNumber,
|
|
131
|
+
fork: MerkleTreeWriteOperations,
|
|
132
|
+
options: Partial<Parameters<typeof mockCheckpointAndMessages>[1]> = {},
|
|
133
|
+
) {
|
|
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
|
+
});
|
|
138
|
+
return { checkpoint, messages };
|
|
139
|
+
}
|
|
140
|
+
|
|
146
141
|
export async function assertSameState(forkA: MerkleTreeReadOperations, forkB: MerkleTreeReadOperations) {
|
|
147
142
|
const nativeStateRef = await forkA.getStateReference();
|
|
148
143
|
const nativeArchive = await forkA.getTreeInfo(MerkleTreeId.ARCHIVE);
|
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,8 +1,13 @@
|
|
|
1
1
|
import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/constants';
|
|
2
|
-
import type {
|
|
2
|
+
import type { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
4
|
import type { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
5
|
+
import type { L2BlockNew } from '@aztec/stdlib/block';
|
|
6
|
+
import type {
|
|
7
|
+
ForkMerkleTreeOperations,
|
|
8
|
+
MerkleTreeReadOperations,
|
|
9
|
+
ReadonlyWorldStateAccess,
|
|
10
|
+
} from '@aztec/stdlib/interfaces/server';
|
|
6
11
|
import type { MerkleTreeId } from '@aztec/stdlib/trees';
|
|
7
12
|
|
|
8
13
|
import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
|
|
@@ -34,19 +39,13 @@ export type TreeSnapshots = {
|
|
|
34
39
|
[MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
|
|
35
40
|
};
|
|
36
41
|
|
|
37
|
-
export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
|
|
42
|
+
export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
|
|
38
43
|
/**
|
|
39
44
|
* Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
|
|
40
45
|
* @param block - The L2 block to handle.
|
|
41
46
|
* @param l1ToL2Messages - The L1 to L2 messages for the block.
|
|
42
|
-
* @param isFirstBlock - Whether the block is the first block in a checkpoint. Temporary hack to only insert l1 to l2
|
|
43
|
-
* messages for the first block in a checkpoint. TODO(#17027) Remove this.
|
|
44
47
|
*/
|
|
45
|
-
handleL2BlockAndMessages(
|
|
46
|
-
block: L2Block | L2BlockNew,
|
|
47
|
-
l1ToL2Messages: Fr[],
|
|
48
|
-
isFirstBlock?: boolean,
|
|
49
|
-
): Promise<WorldStateStatusFull>;
|
|
48
|
+
handleL2BlockAndMessages(block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
|
|
50
49
|
|
|
51
50
|
/**
|
|
52
51
|
* Gets a handle that allows reading the latest committed state
|
|
@@ -58,21 +57,21 @@ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
|
|
|
58
57
|
* @param toBlockNumber The block number of the new oldest historical block
|
|
59
58
|
* @returns The new WorldStateStatus
|
|
60
59
|
*/
|
|
61
|
-
removeHistoricalBlocks(toBlockNumber:
|
|
60
|
+
removeHistoricalBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
|
|
62
61
|
|
|
63
62
|
/**
|
|
64
63
|
* Removes all pending blocks down to but not including the given block number
|
|
65
64
|
* @param toBlockNumber The block number of the new tip of the pending chain,
|
|
66
65
|
* @returns The new WorldStateStatus
|
|
67
66
|
*/
|
|
68
|
-
unwindBlocks(toBlockNumber:
|
|
67
|
+
unwindBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
|
|
69
68
|
|
|
70
69
|
/**
|
|
71
70
|
* Advances the finalized block number to be the number provided
|
|
72
71
|
* @param toBlockNumber The block number that is now the tip of the finalized chain
|
|
73
72
|
* @returns The new WorldStateStatus
|
|
74
73
|
*/
|
|
75
|
-
setFinalized(toBlockNumber:
|
|
74
|
+
setFinalized(toBlockNumber: BlockNumber): Promise<WorldStateStatusSummary>;
|
|
76
75
|
|
|
77
76
|
/**
|
|
78
77
|
* Gets the current status summary of the database.
|