@aztec/world-state 0.0.1-commit.d3ec352c → 0.0.1-commit.d58ff9d0

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.
Files changed (80) hide show
  1. package/dest/index.d.ts +2 -1
  2. package/dest/index.d.ts.map +1 -1
  3. package/dest/index.js +1 -0
  4. package/dest/instrumentation/instrumentation.d.ts +4 -3
  5. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  6. package/dest/instrumentation/instrumentation.js +21 -50
  7. package/dest/native/fork_checkpoint.d.ts +7 -1
  8. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  9. package/dest/native/fork_checkpoint.js +15 -3
  10. package/dest/native/index.d.ts +2 -1
  11. package/dest/native/index.d.ts.map +1 -1
  12. package/dest/native/index.js +1 -0
  13. package/dest/native/ipc_world_state_instance.d.ts +89 -0
  14. package/dest/native/ipc_world_state_instance.d.ts.map +1 -0
  15. package/dest/native/ipc_world_state_instance.js +661 -0
  16. package/dest/native/merkle_trees_facade.d.ts +18 -9
  17. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  18. package/dest/native/merkle_trees_facade.js +90 -113
  19. package/dest/native/message.d.ts +15 -222
  20. package/dest/native/message.d.ts.map +1 -1
  21. package/dest/native/message.js +1 -43
  22. package/dest/native/native_world_state.d.ts +26 -12
  23. package/dest/native/native_world_state.d.ts.map +1 -1
  24. package/dest/native/native_world_state.js +126 -62
  25. package/dest/native/native_world_state_instance.d.ts +56 -42
  26. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  27. package/dest/native/native_world_state_instance.js +1 -203
  28. package/dest/native/world_state_operation.d.ts +7 -0
  29. package/dest/native/world_state_operation.d.ts.map +1 -0
  30. package/dest/native/world_state_operation.js +5 -0
  31. package/dest/synchronizer/config.d.ts +3 -5
  32. package/dest/synchronizer/config.d.ts.map +1 -1
  33. package/dest/synchronizer/config.js +15 -18
  34. package/dest/synchronizer/errors.d.ts +8 -1
  35. package/dest/synchronizer/errors.d.ts.map +1 -1
  36. package/dest/synchronizer/errors.js +8 -1
  37. package/dest/synchronizer/factory.d.ts +6 -5
  38. package/dest/synchronizer/factory.d.ts.map +1 -1
  39. package/dest/synchronizer/factory.js +7 -6
  40. package/dest/synchronizer/index.d.ts +2 -1
  41. package/dest/synchronizer/index.d.ts.map +1 -1
  42. package/dest/synchronizer/index.js +1 -0
  43. package/dest/synchronizer/server_world_state_synchronizer.d.ts +16 -10
  44. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  45. package/dest/synchronizer/server_world_state_synchronizer.js +170 -57
  46. package/dest/test/utils.d.ts +11 -17
  47. package/dest/test/utils.d.ts.map +1 -1
  48. package/dest/test/utils.js +49 -81
  49. package/dest/testing.d.ts +5 -4
  50. package/dest/testing.d.ts.map +1 -1
  51. package/dest/testing.js +15 -8
  52. package/dest/world-state-db/merkle_tree_db.d.ts +7 -18
  53. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  54. package/package.json +14 -12
  55. package/src/index.ts +1 -0
  56. package/src/instrumentation/instrumentation.ts +23 -59
  57. package/src/native/fork_checkpoint.ts +19 -3
  58. package/src/native/index.ts +1 -0
  59. package/src/native/ipc_world_state_instance.ts +834 -0
  60. package/src/native/merkle_trees_facade.ts +119 -106
  61. package/src/native/message.ts +14 -287
  62. package/src/native/native_world_state.ts +159 -99
  63. package/src/native/native_world_state_instance.ts +94 -279
  64. package/src/native/world_state_operation.ts +33 -0
  65. package/src/synchronizer/config.ts +16 -23
  66. package/src/synchronizer/errors.ts +8 -0
  67. package/src/synchronizer/factory.ts +18 -11
  68. package/src/synchronizer/index.ts +1 -0
  69. package/src/synchronizer/server_world_state_synchronizer.ts +203 -78
  70. package/src/test/utils.ts +79 -128
  71. package/src/testing.ts +13 -11
  72. package/src/world-state-db/merkle_tree_db.ts +10 -22
  73. package/dest/native/world_state_ops_queue.d.ts +0 -19
  74. package/dest/native/world_state_ops_queue.d.ts.map +0 -1
  75. package/dest/native/world_state_ops_queue.js +0 -146
  76. package/dest/synchronizer/utils.d.ts +0 -11
  77. package/dest/synchronizer/utils.d.ts.map +0 -1
  78. package/dest/synchronizer/utils.js +0 -59
  79. package/src/native/world_state_ops_queue.ts +0 -190
  80. package/src/synchronizer/utils.ts +0 -83
@@ -1,13 +1,13 @@
1
- import { BlockNumber } from '@aztec/foundation/branded-types';
1
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
2
  import { createLogger } from '@aztec/foundation/log';
3
3
  import { promiseWithResolvers } from '@aztec/foundation/promise';
4
4
  import { elapsed } from '@aztec/foundation/timer';
5
+ import { EventDrivenL2BlockStream } from '@aztec/stdlib/block';
5
6
  import { WorldStateRunningState } from '@aztec/stdlib/interfaces/server';
6
7
  import { MerkleTreeId } from '@aztec/stdlib/trees';
7
- import { TraceableL2BlockStream, getTelemetryClient } from '@aztec/telemetry-client';
8
+ import { getTelemetryClient } from '@aztec/telemetry-client';
8
9
  import { WorldStateInstrumentation } from '../instrumentation/instrumentation.js';
9
10
  import { WorldStateSynchronizerError } from './errors.js';
10
- import { findFirstBlocksInCheckpoints } from './utils.js';
11
11
  /**
12
12
  * Synchronizes the world state with the L2 blocks from a L2BlockSource via a block stream.
13
13
  * The synchronizer will download the L2 blocks from the L2BlockSource and update the merkle trees.
@@ -22,7 +22,6 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
22
22
  latestBlockNumberAtStart;
23
23
  historyToKeep;
24
24
  currentState;
25
- latestBlockHashQuery;
26
25
  syncPromise;
27
26
  blockStream;
28
27
  // WorldState doesn't track the proven block number, it only tracks the latest tips of the pending chain and the finalized chain
@@ -36,10 +35,9 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
36
35
  this.log = log;
37
36
  this.latestBlockNumberAtStart = BlockNumber.ZERO;
38
37
  this.currentState = WorldStateRunningState.IDLE;
39
- this.latestBlockHashQuery = undefined;
40
38
  this.syncPromise = promiseWithResolvers();
41
39
  this.merkleTreeCommitted = this.merkleTreeDb.getCommitted();
42
- this.historyToKeep = config.worldStateBlockHistory < 1 ? undefined : config.worldStateBlockHistory;
40
+ this.historyToKeep = config.worldStateCheckpointHistory < 1 ? undefined : config.worldStateCheckpointHistory;
43
41
  this.log.info(`Created world state synchroniser with block history of ${this.historyToKeep === undefined ? 'infinity' : this.historyToKeep}`);
44
42
  }
45
43
  getCommitted() {
@@ -48,8 +46,41 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
48
46
  getSnapshot(blockNumber) {
49
47
  return this.merkleTreeDb.getSnapshot(blockNumber);
50
48
  }
51
- fork(blockNumber) {
52
- return this.merkleTreeDb.fork(blockNumber);
49
+ async getVerifiedSnapshot(blockNumber, blockHash) {
50
+ const snapshot = this.merkleTreeDb.getSnapshot(blockNumber);
51
+ // Block 0's snapshot is the pre-genesis archive view (size 0), so archive leaf 0 is not visible from it;
52
+ // verify against the initial header hash instead. For later blocks, read archive leaf `blockNumber` from the
53
+ // snapshot's own view so the exact handle we return is validated against the requested fork.
54
+ const actualHash = blockNumber === BlockNumber.ZERO ? (await this.merkleTreeCommitted.getInitialHeader().hash()).toString() : (await snapshot.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(blockNumber)))?.toString();
55
+ if (actualHash === undefined) {
56
+ // A missing archive leaf means either the block's history has been pruned away (permanent: the block predates
57
+ // the oldest historical block kept by world state) or a reorg flipped the fork between the sync and this read
58
+ // (transient). Only the latter is worth retrying, so it alone surfaces as WorldStateSynchronizerError.
59
+ const { oldestHistoricalBlock } = await this.merkleTreeDb.getStatusSummary();
60
+ if (blockNumber < oldestHistoricalBlock) {
61
+ throw new Error(`Unable to find leaf for block ${blockNumber} in the archive tree: world state history has been pruned to block ${oldestHistoricalBlock}`);
62
+ }
63
+ throw new WorldStateSynchronizerError(`Unable to read block hash at block ${blockNumber} to verify snapshot`, {
64
+ cause: {
65
+ reason: 'block_not_available',
66
+ targetBlockNumber: blockNumber
67
+ }
68
+ });
69
+ }
70
+ if (actualHash !== blockHash.toString()) {
71
+ throw new WorldStateSynchronizerError(`Block hash mismatch at block ${blockNumber} (expected ${blockHash} but got ${actualHash})`, {
72
+ cause: {
73
+ reason: 'block_hash_mismatch',
74
+ targetBlockNumber: blockNumber,
75
+ expectedHash: blockHash.toString(),
76
+ actualHash
77
+ }
78
+ });
79
+ }
80
+ return snapshot;
81
+ }
82
+ fork(blockNumber, opts) {
83
+ return this.merkleTreeDb.fork(blockNumber, opts);
53
84
  }
54
85
  backupTo(dstPath, compact) {
55
86
  return this.merkleTreeDb.backupTo(dstPath, compact);
@@ -65,7 +96,7 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
65
96
  return this.syncPromise;
66
97
  }
67
98
  // Get the current latest block number
68
- this.latestBlockNumberAtStart = BlockNumber(await (this.config.worldStateProvenBlocksOnly ? this.l2BlockSource.getProvenBlockNumber() : this.l2BlockSource.getBlockNumber()));
99
+ this.latestBlockNumberAtStart = BlockNumber(await this.l2BlockSource.getBlockNumber());
69
100
  const blockToDownloadFrom = await this.getLatestBlockNumber() + 1;
70
101
  if (blockToDownloadFrom <= this.latestBlockNumberAtStart) {
71
102
  // If there are blocks to be retrieved, go to a synching state
@@ -83,12 +114,11 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
83
114
  return this.syncPromise.promise;
84
115
  }
85
116
  createBlockStream() {
86
- const tracer = this.instrumentation.telemetry.getTracer('WorldStateL2BlockStream');
87
117
  const logger = createLogger('world-state:block_stream');
88
- return new TraceableL2BlockStream(this.l2BlockSource, this, this, tracer, 'WorldStateL2BlockStream', logger, {
89
- proven: this.config.worldStateProvenBlocksOnly,
118
+ return new EventDrivenL2BlockStream(this.l2BlockSource, this, this, logger, {
90
119
  pollIntervalMS: this.config.worldStateBlockCheckIntervalMS,
91
- batchSize: this.config.worldStateBlockRequestBatchSize
120
+ batchSize: this.config.worldStateBlockRequestBatchSize,
121
+ ignoreCheckpoints: true
92
122
  });
93
123
  }
94
124
  async stop() {
@@ -114,7 +144,7 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
114
144
  };
115
145
  }
116
146
  async getLatestBlockNumber() {
117
- return (await this.getL2Tips()).latest.number;
147
+ return (await this.getL2Tips()).proposed.number;
118
148
  }
119
149
  async stopSync() {
120
150
  this.log.debug('Stopping sync...');
@@ -132,9 +162,9 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
132
162
  /**
133
163
  * Forces an immediate sync.
134
164
  * @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
135
- * @param skipThrowIfTargetNotReached - Whether to skip throwing if the target block number is not reached.
165
+ * @param blockHash - If provided, verifies the block at targetBlockNumber matches this hash. On mismatch, triggers a resync (reorg detection).
136
166
  * @returns A promise that resolves with the block number the world state was synced to
137
- */ async syncImmediate(targetBlockNumber, skipThrowIfTargetNotReached) {
167
+ */ async syncImmediate(targetBlockNumber, blockHash) {
138
168
  if (this.currentState !== WorldStateRunningState.RUNNING) {
139
169
  throw new Error(`World State is not running. Unable to perform sync.`);
140
170
  }
@@ -144,7 +174,16 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
144
174
  // If we have been given a block number to sync to and we have reached that number then return
145
175
  const currentBlockNumber = await this.getLatestBlockNumber();
146
176
  if (targetBlockNumber !== undefined && targetBlockNumber <= currentBlockNumber) {
147
- return currentBlockNumber;
177
+ if (blockHash === undefined) {
178
+ return currentBlockNumber;
179
+ }
180
+ // If a block hash was provided, verify we're on the expected fork
181
+ const currentHash = await this.getL2BlockHash(targetBlockNumber);
182
+ if (currentHash === blockHash.toString()) {
183
+ return currentBlockNumber;
184
+ }
185
+ // Hash mismatch: a reorg may have occurred, fall through to trigger sync
186
+ this.log.debug(`World state block hash mismatch at ${targetBlockNumber} (expected ${blockHash}, got ${currentHash}). Triggering resync.`);
148
187
  }
149
188
  this.log.debug(`World State at ${currentBlockNumber} told to sync to ${targetBlockNumber ?? 'latest'}`);
150
189
  // If the archiver is behind the target block, force an archiver sync
@@ -159,7 +198,7 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
159
198
  await this.blockStream.sync();
160
199
  // If we have been given a block number to sync to and we have not reached that number then fail
161
200
  const updatedBlockNumber = await this.getLatestBlockNumber();
162
- if (!skipThrowIfTargetNotReached && targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
201
+ if (targetBlockNumber !== undefined && targetBlockNumber > updatedBlockNumber) {
163
202
  throw new WorldStateSynchronizerError(`Unable to sync to block number ${targetBlockNumber} (last synced is ${updatedBlockNumber})`, {
164
203
  cause: {
165
204
  reason: 'block_not_available',
@@ -169,53 +208,86 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
169
208
  }
170
209
  });
171
210
  }
211
+ // If a block hash was provided, verify we're on the expected fork after syncing, throw otherwise
212
+ if (blockHash !== undefined && targetBlockNumber !== undefined) {
213
+ const updatedHash = await this.getL2BlockHash(targetBlockNumber);
214
+ if (updatedHash !== blockHash.toString()) {
215
+ throw new WorldStateSynchronizerError(`Block hash mismatch at block ${targetBlockNumber} (expected ${blockHash} but got ${updatedHash})`, {
216
+ cause: {
217
+ reason: 'block_hash_mismatch',
218
+ targetBlockNumber,
219
+ expectedHash: blockHash.toString(),
220
+ actualHash: updatedHash
221
+ }
222
+ });
223
+ }
224
+ }
172
225
  return updatedBlockNumber;
173
226
  }
174
227
  /** Returns the L2 block hash for a given number. Used by the L2BlockStream for detecting reorgs. */ async getL2BlockHash(number) {
175
228
  if (number === BlockNumber.ZERO) {
176
229
  return (await this.merkleTreeCommitted.getInitialHeader().hash()).toString();
177
230
  }
178
- if (this.latestBlockHashQuery?.hash === undefined || number !== this.latestBlockHashQuery.blockNumber) {
179
- this.latestBlockHashQuery = {
180
- hash: await this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then((leaf)=>leaf?.toString()),
181
- blockNumber: number
182
- };
183
- }
184
- return this.latestBlockHashQuery.hash;
231
+ return this.merkleTreeCommitted.getLeafValue(MerkleTreeId.ARCHIVE, BigInt(number)).then((leaf)=>leaf?.toString());
185
232
  }
186
- /** Returns the latest L2 block number for each tip of the chain (latest, proven, finalized). */ async getL2Tips() {
233
+ /**
234
+ * Returns the proposed, proven, and finalized block tips of the chain. World state drives its block stream with
235
+ * `ignoreCheckpoints`, so it does not track checkpointed blocks or checkpoints and omits `checkpointed` from the tips
236
+ * it reports.
237
+ */ async getL2Tips() {
187
238
  const status = await this.merkleTreeDb.getStatusSummary();
188
- const unfinalizedBlockHash = await this.getL2BlockHash(status.unfinalizedBlockNumber);
189
- const latestBlockId = {
190
- number: status.unfinalizedBlockNumber,
191
- hash: unfinalizedBlockHash
192
- };
239
+ const unfinalizedBlockHashPromise = this.getL2BlockHash(status.unfinalizedBlockNumber);
240
+ const finalizedBlockHashPromise = this.getL2BlockHash(status.finalizedBlockNumber);
241
+ const provenBlockNumber = this.provenBlockNumber ?? status.finalizedBlockNumber;
242
+ const provenBlockHashPromise = this.provenBlockNumber === undefined ? finalizedBlockHashPromise : this.getL2BlockHash(this.provenBlockNumber);
243
+ const [unfinalizedBlockHash, finalizedBlockHash, provenBlockHash] = await Promise.all([
244
+ unfinalizedBlockHashPromise,
245
+ finalizedBlockHashPromise,
246
+ provenBlockHashPromise
247
+ ]);
193
248
  return {
194
- latest: latestBlockId,
249
+ proposed: {
250
+ number: status.unfinalizedBlockNumber,
251
+ hash: unfinalizedBlockHash
252
+ },
195
253
  finalized: {
196
- number: status.finalizedBlockNumber,
197
- hash: ''
254
+ block: {
255
+ number: status.finalizedBlockNumber,
256
+ hash: finalizedBlockHash
257
+ }
198
258
  },
199
259
  proven: {
200
- number: this.provenBlockNumber ?? status.finalizedBlockNumber,
201
- hash: ''
260
+ block: {
261
+ number: provenBlockNumber,
262
+ hash: provenBlockHash
263
+ }
202
264
  }
203
265
  };
204
266
  }
205
267
  /** Handles an event emitted by the block stream. */ async handleBlockStreamEvent(event) {
206
268
  switch(event.type){
207
269
  case 'blocks-added':
208
- await this.handleL2Blocks(event.blocks.map((b)=>b.block.toL2Block()));
270
+ await this.handleL2Blocks(event.blocks);
209
271
  break;
210
272
  case 'chain-pruned':
211
- await this.handleChainPruned(BlockNumber(event.block.number));
273
+ await this.handleChainPruned(event.block.number);
212
274
  break;
213
275
  case 'chain-proven':
214
- await this.handleChainProven(BlockNumber(event.block.number));
276
+ await this.handleChainProven(event.block.number);
215
277
  break;
216
278
  case 'chain-finalized':
217
- await this.handleChainFinalized(BlockNumber(event.block.number));
279
+ await this.handleChainFinalized(event.block.number);
218
280
  break;
281
+ // World state runs in block mode with ignoreCheckpoints: it tracks tips via blocks-added/pruned/proven/finalized
282
+ // and ignores the thin tip events (it never anchors on them).
283
+ case 'chain-proposed':
284
+ case 'chain-checkpointed':
285
+ break;
286
+ default:
287
+ {
288
+ const _ = event;
289
+ break;
290
+ }
219
291
  }
220
292
  }
221
293
  /**
@@ -223,13 +295,16 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
223
295
  * @param l2Blocks - The L2 blocks to handle.
224
296
  * @returns Whether the block handled was produced by this same node.
225
297
  */ async handleL2Blocks(l2Blocks) {
226
- this.log.trace(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1).number}`);
227
- const firstBlocksInCheckpoints = await findFirstBlocksInCheckpoints(l2Blocks, this.l2BlockSource);
298
+ this.log.debug(`Handling L2 blocks ${l2Blocks[0].number} to ${l2Blocks.at(-1).number}`);
299
+ // Fetch the L1->L2 messages for the first block in a checkpoint.
300
+ const messagesForBlocks = new Map();
301
+ await Promise.all(l2Blocks.filter((b)=>b.indexWithinCheckpoint === 0).map(async (block)=>{
302
+ const l1ToL2Messages = await this.l2BlockSource.getL1ToL2Messages(block.checkpointNumber);
303
+ messagesForBlocks.set(block.number, l1ToL2Messages);
304
+ }));
228
305
  let updateStatus = undefined;
229
306
  for (const block of l2Blocks){
230
- const l1ToL2Messages = firstBlocksInCheckpoints.get(block.number) ?? [];
231
- const isFirstBlock = firstBlocksInCheckpoints.has(block.number);
232
- const [duration, result] = await elapsed(()=>this.handleL2Block(block, l1ToL2Messages, isFirstBlock));
307
+ const [duration, result] = await elapsed(()=>this.handleL2Block(block, messagesForBlocks.get(block.number) ?? []));
233
308
  this.log.info(`World state updated with L2 block ${block.number}`, {
234
309
  eventName: 'l2-block-handled',
235
310
  duration,
@@ -250,14 +325,15 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
250
325
  * @param l2Block - The L2 block to handle.
251
326
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
252
327
  * @returns Whether the block handled was produced by this same node.
253
- */ async handleL2Block(l2Block, l1ToL2Messages, isFirstBlock) {
254
- // If the above check succeeds, we can proceed to handle the block.
255
- this.log.trace(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
328
+ */ async handleL2Block(l2Block, l1ToL2Messages) {
329
+ this.log.debug(`Pushing L2 block ${l2Block.number} to merkle tree db `, {
256
330
  blockNumber: l2Block.number,
257
331
  blockHash: await l2Block.hash().then((h)=>h.toString()),
258
- l1ToL2Messages: l1ToL2Messages.map((msg)=>msg.toString())
332
+ l1ToL2Messages: l1ToL2Messages.map((msg)=>msg.toString()),
333
+ blockHeader: l2Block.header.toInspect(),
334
+ blockStats: l2Block.getStats()
259
335
  });
260
- const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages, isFirstBlock);
336
+ const result = await this.merkleTreeDb.handleL2BlockAndMessages(l2Block, l1ToL2Messages);
261
337
  if (this.currentState === WorldStateRunningState.SYNCHING && l2Block.number >= this.latestBlockNumberAtStart) {
262
338
  this.setCurrentState(WorldStateRunningState.RUNNING);
263
339
  this.syncPromise.resolve();
@@ -266,16 +342,52 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
266
342
  }
267
343
  async handleChainFinalized(blockNumber) {
268
344
  this.log.verbose(`Finalized chain is now at block ${blockNumber}`);
345
+ // If the finalized block number is older than the oldest available block in world state,
346
+ // skip entirely. The finalized block number can jump backwards (e.g. when the finalization
347
+ // heuristic changes) and try to read block data that has already been pruned. When this
348
+ // happens, there is nothing useful to do — the native world state is already finalized
349
+ // past this point and pruning has already happened.
350
+ const currentSummary = await this.merkleTreeDb.getStatusSummary();
351
+ if (blockNumber < currentSummary.oldestHistoricalBlock || blockNumber < 1) {
352
+ this.log.trace(`Finalized block ${blockNumber} is older than the oldest available block ${currentSummary.oldestHistoricalBlock}. Skipping.`);
353
+ return;
354
+ }
269
355
  const summary = await this.merkleTreeDb.setFinalized(blockNumber);
270
356
  if (this.historyToKeep === undefined) {
271
357
  return;
272
358
  }
273
- const newHistoricBlock = BlockNumber(summary.finalizedBlockNumber - this.historyToKeep + 1);
274
- if (newHistoricBlock <= BlockNumber(1)) {
359
+ const finalisedBlockData = await this.l2BlockSource.getBlockData({
360
+ number: summary.finalizedBlockNumber
361
+ });
362
+ if (finalisedBlockData === undefined) {
363
+ this.log.warn(`Failed to retrieve checkpointed block for finalized block number: ${summary.finalizedBlockNumber}`);
364
+ return;
365
+ }
366
+ // Compute the required historic checkpoint number
367
+ const newHistoricCheckpointNumber = finalisedBlockData.checkpointNumber - this.historyToKeep + 1;
368
+ if (newHistoricCheckpointNumber <= 1) {
275
369
  return;
276
370
  }
277
- this.log.verbose(`Pruning historic blocks to ${newHistoricBlock}`);
278
- const status = await this.merkleTreeDb.removeHistoricalBlocks(newHistoricBlock);
371
+ // Retrieve the historic checkpoint
372
+ const historicCheckpoint = await this.l2BlockSource.getCheckpoint({
373
+ number: CheckpointNumber(newHistoricCheckpointNumber)
374
+ });
375
+ if (!historicCheckpoint) {
376
+ this.log.warn(`Failed to retrieve checkpoint number ${newHistoricCheckpointNumber} from Archiver`);
377
+ return;
378
+ }
379
+ if (historicCheckpoint.checkpoint.blocks.length === 0 || historicCheckpoint.checkpoint.blocks[0] === undefined) {
380
+ this.log.warn(`Retrieved checkpoint number ${newHistoricCheckpointNumber} has no blocks!`);
381
+ return;
382
+ }
383
+ // Find the block at the start of the checkpoint and remove blocks up to this one
384
+ const newHistoricBlock = historicCheckpoint.checkpoint.blocks[0];
385
+ if (newHistoricBlock.number <= currentSummary.oldestHistoricalBlock) {
386
+ this.log.debug(`Historic block ${newHistoricBlock.number} is not newer than oldest available ${currentSummary.oldestHistoricalBlock}. Skipping prune.`);
387
+ return;
388
+ }
389
+ this.log.verbose(`Pruning historic blocks to ${newHistoricBlock.number}`);
390
+ const status = await this.merkleTreeDb.removeHistoricalBlocks(BlockNumber(newHistoricBlock.number));
279
391
  this.log.debug(`World state summary `, status.summary);
280
392
  }
281
393
  handleChainProven(blockNumber) {
@@ -284,10 +396,11 @@ import { findFirstBlocksInCheckpoints } from './utils.js';
284
396
  return Promise.resolve();
285
397
  }
286
398
  async handleChainPruned(blockNumber) {
287
- this.log.warn(`Chain pruned to block ${blockNumber}`);
399
+ this.log.info(`Chain pruned to block ${blockNumber}`);
288
400
  const status = await this.merkleTreeDb.unwindBlocks(blockNumber);
289
- this.latestBlockHashQuery = undefined;
290
- this.provenBlockNumber = undefined;
401
+ if (this.provenBlockNumber !== undefined && this.provenBlockNumber > blockNumber) {
402
+ this.provenBlockNumber = undefined;
403
+ }
291
404
  this.instrumentation.updateWorldStateMetrics(status);
292
405
  }
293
406
  /**
@@ -1,32 +1,26 @@
1
1
  import { BlockNumber, type CheckpointNumber } from '@aztec/foundation/branded-types';
2
- import { Fr } from '@aztec/foundation/fields';
3
- import { L2BlockNew } from '@aztec/stdlib/block';
4
- import { Checkpoint } from '@aztec/stdlib/checkpoint';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { L2Block } from '@aztec/stdlib/block';
5
4
  import type { MerkleTreeReadOperations, MerkleTreeWriteOperations } from '@aztec/stdlib/interfaces/server';
5
+ import { mockCheckpointAndMessages } from '@aztec/stdlib/testing';
6
6
  import type { NativeWorldStateService } from '../native/native_world_state.js';
7
- export declare function mockBlock(blockNum: BlockNumber, size: number, fork: MerkleTreeWriteOperations, maxEffects?: number | undefined, numL1ToL2Messages?: number, isFirstBlock?: boolean): Promise<{
8
- block: L2BlockNew;
7
+ export declare function updateBlockState(block: L2Block, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations): Promise<void>;
8
+ export declare function mockBlock(blockNum: BlockNumber, size: number, fork: MerkleTreeWriteOperations, maxEffects?: number | undefined, numL1ToL2Messages?: number, isFirstBlockInCheckpoint?: boolean): Promise<{
9
+ block: L2Block;
9
10
  messages: Fr[];
10
11
  }>;
11
12
  export declare function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWriteOperations): Promise<{
12
- block: L2BlockNew;
13
+ block: L2Block;
13
14
  messages: Fr[];
14
15
  }>;
15
16
  export declare function mockBlocks(from: BlockNumber, count: number, numTxs: number, worldState: NativeWorldStateService): Promise<{
16
- blocks: L2BlockNew[];
17
+ blocks: L2Block[];
17
18
  messages: Fr[][];
18
19
  }>;
19
- export declare function mockL1ToL2Messages(numL1ToL2Messages: number): Fr[];
20
- export declare function mockCheckpoint(checkpointNumber: CheckpointNumber, { startBlockNumber, numBlocks, numTxsPerBlock, numL1ToL2Messages, fork }?: {
21
- startBlockNumber?: BlockNumber;
22
- numBlocks?: number;
23
- numTxsPerBlock?: number;
24
- numL1ToL2Messages?: number;
25
- fork?: MerkleTreeWriteOperations;
26
- }): Promise<{
27
- checkpoint: Checkpoint;
20
+ export declare function mockCheckpoint(checkpointNumber: CheckpointNumber, fork: MerkleTreeWriteOperations, options?: Partial<Parameters<typeof mockCheckpointAndMessages>[1]>): Promise<{
21
+ checkpoint: import("@aztec/stdlib/checkpoint").Checkpoint;
28
22
  messages: Fr[];
29
23
  }>;
30
24
  export declare function assertSameState(forkA: MerkleTreeReadOperations, forkB: MerkleTreeReadOperations): Promise<void>;
31
25
  export declare function compareChains(left: MerkleTreeReadOperations, right: MerkleTreeReadOperations): Promise<void>;
32
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy90ZXN0L3V0aWxzLnRzIl0sIm5hbWVzIjpbXSwibWFwc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eAAe,CAAC,KAAK,EAAE,wBAAwB,EAAE,KAAK,EAAE,wBAAwB,iBAQrG;AAED,wBAAsB,aAAa,CAAC,IAAI,EAAE,wBAAwB,EAAE,KAAK,EAAE,wBAAwB,iBAYlG"}
@@ -1,73 +1,64 @@
1
1
  import { MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, NULLIFIER_SUBTREE_HEIGHT, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
2
- import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
2
+ import { asyncMap } from '@aztec/foundation/async-map';
3
+ import { BlockNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
3
4
  import { padArrayEnd } from '@aztec/foundation/collection';
4
- import { Fr } from '@aztec/foundation/fields';
5
- import { L2BlockNew } from '@aztec/stdlib/block';
6
- import { Checkpoint } from '@aztec/stdlib/checkpoint';
7
- import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
5
+ import { Fr } from '@aztec/foundation/curves/bn254';
6
+ import { L2Block } from '@aztec/stdlib/block';
7
+ import { mockCheckpointAndMessages, mockL1ToL2Messages } from '@aztec/stdlib/testing';
8
8
  import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
9
- export async function mockBlock(blockNum, size, fork, maxEffects = 1000, numL1ToL2Messages = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, isFirstBlock = true) {
10
- const l2Block = await L2BlockNew.random(blockNum, {
9
+ import { BlockHeader } from '@aztec/stdlib/tx';
10
+ export async function updateBlockState(block, l1ToL2Messages, fork) {
11
+ const insertData = async (treeId, data, subTreeHeight, fork)=>{
12
+ for (const dataBatch of data){
13
+ await fork.batchInsert(treeId, dataBatch, subTreeHeight);
14
+ }
15
+ };
16
+ const publicDataInsert = insertData(MerkleTreeId.PUBLIC_DATA_TREE, block.body.txEffects.map((txEffect)=>txEffect.publicDataWrites.map((write)=>write.toBuffer())), 0, fork);
17
+ const nullifierInsert = insertData(MerkleTreeId.NULLIFIER_TREE, block.body.txEffects.map((txEffect)=>padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map((nullifier)=>nullifier.toBuffer())), NULLIFIER_SUBTREE_HEIGHT, fork);
18
+ const noteHashesPadded = block.body.txEffects.flatMap((txEffect)=>padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX));
19
+ const l1ToL2MessagesPadded = block.indexWithinCheckpoint === 0 ? padArrayEnd(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP) : l1ToL2Messages;
20
+ const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
21
+ const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
22
+ await Promise.all([
23
+ publicDataInsert,
24
+ nullifierInsert,
25
+ noteHashInsert,
26
+ messageInsert
27
+ ]);
28
+ const state = await fork.getStateReference();
29
+ // Capture the archive root *before* appending this block so the header's lastArchive chains off the previous block.
30
+ // sync_block now verifies lastArchive against the committed archive root, so a block carrying an unchained value
31
+ // (e.g. the random lastArchive from L2Block.random) would be rejected as a divergence from the canonical chain.
32
+ const previousArchive = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
33
+ block.header = BlockHeader.from({
34
+ ...block.header,
35
+ state,
36
+ lastArchive: new AppendOnlyTreeSnapshot(Fr.fromBuffer(previousArchive.root), Number(previousArchive.size))
37
+ });
38
+ await fork.updateArchive(block.header);
39
+ const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
40
+ block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
41
+ }
42
+ export async function mockBlock(blockNum, size, fork, maxEffects = 1000, numL1ToL2Messages = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, isFirstBlockInCheckpoint = true) {
43
+ const block = await L2Block.random(blockNum, {
44
+ indexWithinCheckpoint: isFirstBlockInCheckpoint ? IndexWithinCheckpoint(0) : IndexWithinCheckpoint(1),
11
45
  txsPerBlock: size,
12
46
  txOptions: {
13
47
  maxEffects
14
48
  }
15
49
  });
16
50
  const l1ToL2Messages = mockL1ToL2Messages(numL1ToL2Messages);
17
- {
18
- const insertData = async (treeId, data, subTreeHeight, fork)=>{
19
- for (const dataBatch of data){
20
- await fork.batchInsert(treeId, dataBatch, subTreeHeight);
21
- }
22
- };
23
- const publicDataInsert = insertData(MerkleTreeId.PUBLIC_DATA_TREE, l2Block.body.txEffects.map((txEffect)=>txEffect.publicDataWrites.map((write)=>write.toBuffer())), 0, fork);
24
- const nullifierInsert = insertData(MerkleTreeId.NULLIFIER_TREE, l2Block.body.txEffects.map((txEffect)=>padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map((nullifier)=>nullifier.toBuffer())), NULLIFIER_SUBTREE_HEIGHT, fork);
25
- const noteHashesPadded = l2Block.body.txEffects.flatMap((txEffect)=>padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX));
26
- const l1ToL2MessagesPadded = isFirstBlock ? padArrayEnd(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP) : l1ToL2Messages;
27
- const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
28
- const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
29
- await Promise.all([
30
- publicDataInsert,
31
- nullifierInsert,
32
- noteHashInsert,
33
- messageInsert
34
- ]);
35
- }
36
- const state = await fork.getStateReference();
37
- l2Block.header.state = state;
38
- await fork.updateArchive(l2Block.header);
39
- const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
40
- l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
51
+ await updateBlockState(block, l1ToL2Messages, fork);
41
52
  return {
42
- block: l2Block,
53
+ block,
43
54
  messages: l1ToL2Messages
44
55
  };
45
56
  }
46
57
  export async function mockEmptyBlock(blockNum, fork) {
47
- const l2Block = L2BlockNew.empty();
58
+ const l2Block = L2Block.empty();
48
59
  const l1ToL2Messages = Array(16).fill(0).map(Fr.zero);
49
60
  l2Block.header.globalVariables.blockNumber = blockNum;
50
- // Sync the append only trees
51
- {
52
- const noteHashesPadded = l2Block.body.txEffects.flatMap((txEffect)=>padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX));
53
- await fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
54
- const l1ToL2MessagesPadded = padArrayEnd(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
55
- await fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
56
- }
57
- // Sync the indexed trees
58
- {
59
- // We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
60
- for (const txEffect of l2Block.body.txEffects){
61
- await fork.batchInsert(MerkleTreeId.PUBLIC_DATA_TREE, txEffect.publicDataWrites.map((write)=>write.toBuffer()), 0);
62
- const nullifiersPadded = padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX);
63
- await fork.batchInsert(MerkleTreeId.NULLIFIER_TREE, nullifiersPadded.map((nullifier)=>nullifier.toBuffer()), NULLIFIER_SUBTREE_HEIGHT);
64
- }
65
- }
66
- const state = await fork.getStateReference();
67
- l2Block.header.state = state;
68
- await fork.updateArchive(l2Block.header);
69
- const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
70
- l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
61
+ await updateBlockState(l2Block, l1ToL2Messages, fork);
71
62
  return {
72
63
  block: l2Block,
73
64
  messages: l1ToL2Messages
@@ -88,34 +79,11 @@ export async function mockBlocks(from, count, numTxs, worldState) {
88
79
  messages: messagesArray
89
80
  };
90
81
  }
91
- export function mockL1ToL2Messages(numL1ToL2Messages) {
92
- return Array(numL1ToL2Messages).fill(0).map(Fr.random);
93
- }
94
- export async function mockCheckpoint(checkpointNumber, { startBlockNumber = BlockNumber(1), numBlocks = 1, numTxsPerBlock = 1, numL1ToL2Messages = 1, fork } = {}) {
95
- const slotNumber = SlotNumber(checkpointNumber * 10);
96
- const blocksAndMessages = [];
97
- for(let i = 0; i < numBlocks; i++){
98
- const blockNumber = BlockNumber(startBlockNumber + i);
99
- const { block, messages } = fork ? await mockBlock(blockNumber, numTxsPerBlock, fork, blockNumber === startBlockNumber ? numL1ToL2Messages : 0) : {
100
- block: await L2BlockNew.random(blockNumber, {
101
- txsPerBlock: numTxsPerBlock,
102
- slotNumber
103
- }),
104
- messages: mockL1ToL2Messages(numL1ToL2Messages)
105
- };
106
- blocksAndMessages.push({
107
- block,
108
- messages
109
- });
110
- }
111
- const messages = blocksAndMessages[0].messages;
112
- const inHash = computeInHashFromL1ToL2Messages(messages);
113
- const checkpoint = await Checkpoint.random(checkpointNumber, {
114
- numBlocks: 0,
115
- slotNumber,
116
- inHash
82
+ export async function mockCheckpoint(checkpointNumber, fork, options = {}) {
83
+ const { checkpoint, messages } = await mockCheckpointAndMessages(checkpointNumber, options);
84
+ await asyncMap(checkpoint.blocks, async (block, i)=>{
85
+ await updateBlockState(block, i === 0 ? messages : [], fork);
117
86
  });
118
- checkpoint.blocks = blocksAndMessages.map(({ block })=>block);
119
87
  return {
120
88
  checkpoint,
121
89
  messages
package/dest/testing.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
3
3
  import { PublicDataTreeLeaf } from '@aztec/stdlib/trees';
4
+ import type { GenesisData } from '@aztec/stdlib/world-state';
4
5
  export declare const defaultInitialAccountFeeJuice: Fr;
5
- export declare function getGenesisValues(initialAccounts: AztecAddress[], initialAccountFeeJuice?: Fr, genesisPublicData?: PublicDataTreeLeaf[]): Promise<{
6
+ export declare function getGenesisValues(initialAccounts: AztecAddress[], initialAccountFeeJuice?: Fr, genesisPublicData?: PublicDataTreeLeaf[], genesisTimestamp?: bigint): Promise<{
6
7
  genesisArchiveRoot: Fr;
7
- prefilledPublicData: PublicDataTreeLeaf[];
8
+ genesis: GenesisData;
8
9
  fundingNeeded: bigint;
9
10
  }>;
10
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdGluZy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL3Rlc3RpbmcudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLEVBQUUsRUFBRSxNQUFNLDBCQUEwQixDQUFDO0FBRTlDLE9BQU8sS0FBSyxFQUFFLFlBQVksRUFBRSxNQUFNLDZCQUE2QixDQUFDO0FBQ2hFLE9BQU8sRUFBZ0Isa0JBQWtCLEVBQUUsTUFBTSxxQkFBcUIsQ0FBQztBQXlCdkUsZUFBTyxNQUFNLDZCQUE2QixJQUFxQixDQUFDO0FBRWhFLHdCQUFzQixnQkFBZ0IsQ0FDcEMsZUFBZSxFQUFFLFlBQVksRUFBRSxFQUMvQixzQkFBc0IsS0FBZ0MsRUFDdEQsaUJBQWlCLEdBQUUsa0JBQWtCLEVBQU87Ozs7R0FxQjdDIn0=
11
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdGluZy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL3Rlc3RpbmcudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLEVBQUUsRUFBRSxNQUFNLGdDQUFnQyxDQUFDO0FBRXBELE9BQU8sS0FBSyxFQUFFLFlBQVksRUFBRSxNQUFNLDZCQUE2QixDQUFDO0FBQ2hFLE9BQU8sRUFBZ0Isa0JBQWtCLEVBQUUsTUFBTSxxQkFBcUIsQ0FBQztBQUN2RSxPQUFPLEtBQUssRUFBRSxXQUFXLEVBQUUsTUFBTSwyQkFBMkIsQ0FBQztBQXdCN0QsZUFBTyxNQUFNLDZCQUE2QixJQUFxQixDQUFDO0FBRWhFLHdCQUFzQixnQkFBZ0IsQ0FDcEMsZUFBZSxFQUFFLFlBQVksRUFBRSxFQUMvQixzQkFBc0IsS0FBZ0MsRUFDdEQsaUJBQWlCLEdBQUUsa0JBQWtCLEVBQU8sRUFDNUMsZ0JBQWdCLEdBQUUsTUFBVzs7OztHQXNCOUIifQ==
@@ -1 +1 @@
1
- {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAE9C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAgB,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAyBvE,eAAO,MAAM,6BAA6B,IAAqB,CAAC;AAEhE,wBAAsB,gBAAgB,CACpC,eAAe,EAAE,YAAY,EAAE,EAC/B,sBAAsB,KAAgC,EACtD,iBAAiB,GAAE,kBAAkB,EAAO;;;;GAqB7C"}
1
+ {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,gCAAgC,CAAC;AAEpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAgB,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAwB7D,eAAO,MAAM,6BAA6B,IAAqB,CAAC;AAEhE,wBAAsB,gBAAgB,CACpC,eAAe,EAAE,YAAY,EAAE,EAC/B,sBAAsB,KAAgC,EACtD,iBAAiB,GAAE,kBAAkB,EAAO,EAC5C,gBAAgB,GAAE,MAAW;;;;GAsB9B"}