@aztec/aztec-node 0.1.0-alpha43 → 0.1.0-alpha45

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.
@@ -32,8 +32,10 @@ import {
32
32
  import {
33
33
  MerkleTrees,
34
34
  ServerWorldStateSynchroniser,
35
+ WorldStateConfig,
35
36
  WorldStateSynchroniser,
36
37
  computePublicDataTreeLeafIndex,
38
+ getConfigEnvVars as getWorldStateConfig,
37
39
  } from '@aztec/world-state';
38
40
 
39
41
  import { default as levelup } from 'levelup';
@@ -54,7 +56,6 @@ export class AztecNodeService implements AztecNode {
54
56
  protected unencryptedLogsSource: L2LogsSource,
55
57
  protected contractDataSource: ContractDataSource,
56
58
  protected l1ToL2MessageSource: L1ToL2MessageSource,
57
- protected merkleTreeDB: MerkleTrees,
58
59
  protected worldStateSynchroniser: WorldStateSynchroniser,
59
60
  protected sequencer: SequencerClient,
60
61
  protected chainId: number,
@@ -80,7 +81,8 @@ export class AztecNodeService implements AztecNode {
80
81
 
81
82
  // now create the merkle trees and the world state syncher
82
83
  const merkleTreeDB = await MerkleTrees.new(levelup(createMemDown()), await CircuitsWasm.get());
83
- const worldStateSynchroniser = new ServerWorldStateSynchroniser(merkleTreeDB, archiver);
84
+ const worldStateConfig: WorldStateConfig = getWorldStateConfig();
85
+ const worldStateSynchroniser = new ServerWorldStateSynchroniser(merkleTreeDB, archiver, worldStateConfig);
84
86
 
85
87
  // start both and wait for them to sync from the block source
86
88
  await Promise.all([p2pClient.start(), worldStateSynchroniser.start()]);
@@ -101,7 +103,6 @@ export class AztecNodeService implements AztecNode {
101
103
  archiver,
102
104
  archiver,
103
105
  archiver,
104
- merkleTreeDB,
105
106
  worldStateSynchroniser,
106
107
  sequencer,
107
108
  config.chainId,
@@ -137,11 +138,11 @@ export class AztecNodeService implements AztecNode {
137
138
  }
138
139
 
139
140
  /**
140
- * Method to fetch the current block height.
141
- * @returns The block height as a number.
141
+ * Method to fetch the current block number.
142
+ * @returns The block number.
142
143
  */
143
- public async getBlockHeight(): Promise<number> {
144
- return await this.blockSource.getBlockHeight();
144
+ public async getBlockNumber(): Promise<number> {
145
+ return await this.blockSource.getBlockNumber();
145
146
  }
146
147
 
147
148
  /**
@@ -216,7 +217,6 @@ export class AztecNodeService implements AztecNode {
216
217
  await this.sequencer.stop();
217
218
  await this.p2pClient.stop();
218
219
  await this.worldStateSynchroniser.stop();
219
- await this.merkleTreeDB.stop();
220
220
  await this.blockSource.stop();
221
221
  this.log.info(`Stopped`);
222
222
  }
@@ -243,8 +243,9 @@ export class AztecNodeService implements AztecNode {
243
243
  * @param leafValue - The value to search for.
244
244
  * @returns The index of the given leaf in the contracts tree or undefined if not found.
245
245
  */
246
- public findContractIndex(leafValue: Buffer): Promise<bigint | undefined> {
247
- return this.merkleTreeDB.findLeafIndex(MerkleTreeId.CONTRACT_TREE, leafValue, false);
246
+ public async findContractIndex(leafValue: Buffer): Promise<bigint | undefined> {
247
+ const committedDb = await this.getWorldState();
248
+ return committedDb.findLeafIndex(MerkleTreeId.CONTRACT_TREE, leafValue);
248
249
  }
249
250
 
250
251
  /**
@@ -252,8 +253,9 @@ export class AztecNodeService implements AztecNode {
252
253
  * @param leafIndex - The index of the leaf for which the sibling path is required.
253
254
  * @returns The sibling path for the leaf index.
254
255
  */
255
- public getContractPath(leafIndex: bigint): Promise<SiblingPath<typeof CONTRACT_TREE_HEIGHT>> {
256
- return this.merkleTreeDB.getSiblingPath(MerkleTreeId.CONTRACT_TREE, leafIndex, false);
256
+ public async getContractPath(leafIndex: bigint): Promise<SiblingPath<typeof CONTRACT_TREE_HEIGHT>> {
257
+ const committedDb = await this.getWorldState();
258
+ return committedDb.getSiblingPath(MerkleTreeId.CONTRACT_TREE, leafIndex);
257
259
  }
258
260
 
259
261
  /**
@@ -261,8 +263,9 @@ export class AztecNodeService implements AztecNode {
261
263
  * @param leafValue - The value to search for.
262
264
  * @returns The index of the given leaf in the private data tree or undefined if not found.
263
265
  */
264
- public findCommitmentIndex(leafValue: Buffer): Promise<bigint | undefined> {
265
- return this.merkleTreeDB.findLeafIndex(MerkleTreeId.PRIVATE_DATA_TREE, leafValue, false);
266
+ public async findCommitmentIndex(leafValue: Buffer): Promise<bigint | undefined> {
267
+ const committedDb = await this.getWorldState();
268
+ return committedDb.findLeafIndex(MerkleTreeId.PRIVATE_DATA_TREE, leafValue);
266
269
  }
267
270
 
268
271
  /**
@@ -270,8 +273,9 @@ export class AztecNodeService implements AztecNode {
270
273
  * @param leafIndex - The index of the leaf for which the sibling path is required.
271
274
  * @returns The sibling path for the leaf index.
272
275
  */
273
- public getDataTreePath(leafIndex: bigint): Promise<SiblingPath<typeof PRIVATE_DATA_TREE_HEIGHT>> {
274
- return this.merkleTreeDB.getSiblingPath(MerkleTreeId.PRIVATE_DATA_TREE, leafIndex, false);
276
+ public async getDataTreePath(leafIndex: bigint): Promise<SiblingPath<typeof PRIVATE_DATA_TREE_HEIGHT>> {
277
+ const committedDb = await this.getWorldState();
278
+ return committedDb.getSiblingPath(MerkleTreeId.PRIVATE_DATA_TREE, leafIndex);
275
279
  }
276
280
 
277
281
  /**
@@ -282,12 +286,9 @@ export class AztecNodeService implements AztecNode {
282
286
  */
283
287
  public async getL1ToL2MessageAndIndex(messageKey: Fr): Promise<L1ToL2MessageAndIndex> {
284
288
  // todo: #697 - make this one lookup.
289
+ const committedDb = await this.getWorldState();
285
290
  const message = await this.l1ToL2MessageSource.getConfirmedL1ToL2Message(messageKey);
286
- const index = (await this.merkleTreeDB.findLeafIndex(
287
- MerkleTreeId.L1_TO_L2_MESSAGES_TREE,
288
- messageKey.toBuffer(),
289
- false,
290
- ))!;
291
+ const index = (await committedDb.findLeafIndex(MerkleTreeId.L1_TO_L2_MESSAGES_TREE, messageKey.toBuffer()))!;
291
292
  return Promise.resolve({ message, index });
292
293
  }
293
294
 
@@ -296,8 +297,9 @@ export class AztecNodeService implements AztecNode {
296
297
  * @param leafIndex - Index of the leaf in the tree.
297
298
  * @returns The sibling path.
298
299
  */
299
- public getL1ToL2MessagesTreePath(leafIndex: bigint): Promise<SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>> {
300
- return this.merkleTreeDB.getSiblingPath(MerkleTreeId.L1_TO_L2_MESSAGES_TREE, leafIndex, false);
300
+ public async getL1ToL2MessagesTreePath(leafIndex: bigint): Promise<SiblingPath<typeof L1_TO_L2_MSG_TREE_HEIGHT>> {
301
+ const committedDb = await this.getWorldState();
302
+ return committedDb.getSiblingPath(MerkleTreeId.L1_TO_L2_MESSAGES_TREE, leafIndex);
301
303
  }
302
304
 
303
305
  /**
@@ -308,8 +310,9 @@ export class AztecNodeService implements AztecNode {
308
310
  * Note: Aztec's version of `eth_getStorageAt`.
309
311
  */
310
312
  public async getPublicStorageAt(contract: AztecAddress, slot: bigint): Promise<Buffer | undefined> {
313
+ const committedDb = await this.getWorldState();
311
314
  const leafIndex = computePublicDataTreeLeafIndex(contract, new Fr(slot), await CircuitsWasm.get());
312
- return this.merkleTreeDB.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex, false);
315
+ return committedDb.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex);
313
316
  }
314
317
 
315
318
  /**
@@ -317,16 +320,26 @@ export class AztecNodeService implements AztecNode {
317
320
  * @returns The current committed roots for the data trees.
318
321
  */
319
322
  public async getTreeRoots(): Promise<Record<MerkleTreeId, Fr>> {
320
- const getTreeRoot = async (id: MerkleTreeId) =>
321
- Fr.fromBuffer((await this.merkleTreeDB.getTreeInfo(id, false)).root);
323
+ const committedDb = await this.getWorldState();
324
+ const getTreeRoot = async (id: MerkleTreeId) => Fr.fromBuffer((await committedDb.getTreeInfo(id)).root);
325
+
326
+ const [privateDataTree, nullifierTree, contractTree, l1ToL2MessagesTree, blocksTree, publicDataTree] =
327
+ await Promise.all([
328
+ getTreeRoot(MerkleTreeId.PRIVATE_DATA_TREE),
329
+ getTreeRoot(MerkleTreeId.NULLIFIER_TREE),
330
+ getTreeRoot(MerkleTreeId.CONTRACT_TREE),
331
+ getTreeRoot(MerkleTreeId.L1_TO_L2_MESSAGES_TREE),
332
+ getTreeRoot(MerkleTreeId.BLOCKS_TREE),
333
+ getTreeRoot(MerkleTreeId.PUBLIC_DATA_TREE),
334
+ ]);
322
335
 
323
336
  return {
324
- [MerkleTreeId.CONTRACT_TREE]: await getTreeRoot(MerkleTreeId.CONTRACT_TREE),
325
- [MerkleTreeId.PRIVATE_DATA_TREE]: await getTreeRoot(MerkleTreeId.PRIVATE_DATA_TREE),
326
- [MerkleTreeId.NULLIFIER_TREE]: await getTreeRoot(MerkleTreeId.NULLIFIER_TREE),
327
- [MerkleTreeId.PUBLIC_DATA_TREE]: await getTreeRoot(MerkleTreeId.PUBLIC_DATA_TREE),
328
- [MerkleTreeId.L1_TO_L2_MESSAGES_TREE]: await getTreeRoot(MerkleTreeId.L1_TO_L2_MESSAGES_TREE),
329
- [MerkleTreeId.BLOCKS_TREE]: await getTreeRoot(MerkleTreeId.BLOCKS_TREE),
337
+ [MerkleTreeId.CONTRACT_TREE]: contractTree,
338
+ [MerkleTreeId.PRIVATE_DATA_TREE]: privateDataTree,
339
+ [MerkleTreeId.NULLIFIER_TREE]: nullifierTree,
340
+ [MerkleTreeId.PUBLIC_DATA_TREE]: publicDataTree,
341
+ [MerkleTreeId.L1_TO_L2_MESSAGES_TREE]: l1ToL2MessagesTree,
342
+ [MerkleTreeId.BLOCKS_TREE]: blocksTree,
330
343
  };
331
344
  }
332
345
 
@@ -335,20 +348,41 @@ export class AztecNodeService implements AztecNode {
335
348
  * @returns The current committed block data.
336
349
  */
337
350
  public async getHistoricBlockData(): Promise<HistoricBlockData> {
338
- const getTreeRoot = async (id: MerkleTreeId) =>
339
- Fr.fromBuffer((await this.merkleTreeDB.getTreeInfo(id, false)).root);
340
-
341
- const globalsHash = this.worldStateSynchroniser.latestGlobalVariablesHash;
351
+ const committedDb = await this.getWorldState();
352
+ const [roots, globalsHash] = await Promise.all([this.getTreeRoots(), committedDb.getLatestGlobalVariablesHash()]);
342
353
 
343
354
  return new HistoricBlockData(
344
- await getTreeRoot(MerkleTreeId.PRIVATE_DATA_TREE),
345
- await getTreeRoot(MerkleTreeId.NULLIFIER_TREE),
346
- await getTreeRoot(MerkleTreeId.CONTRACT_TREE),
347
- await getTreeRoot(MerkleTreeId.L1_TO_L2_MESSAGES_TREE),
348
- await getTreeRoot(MerkleTreeId.BLOCKS_TREE),
355
+ roots[MerkleTreeId.PRIVATE_DATA_TREE],
356
+ roots[MerkleTreeId.NULLIFIER_TREE],
357
+ roots[MerkleTreeId.CONTRACT_TREE],
358
+ roots[MerkleTreeId.L1_TO_L2_MESSAGES_TREE],
359
+ roots[MerkleTreeId.BLOCKS_TREE],
349
360
  Fr.ZERO,
350
- await getTreeRoot(MerkleTreeId.PUBLIC_DATA_TREE),
361
+ roots[MerkleTreeId.PUBLIC_DATA_TREE],
351
362
  globalsHash,
352
363
  );
353
364
  }
365
+
366
+ /**
367
+ * Returns an instance of MerkleTreeOperations having first ensured the world state is fully synched
368
+ * @returns An instance of a committed MerkleTreeOperations
369
+ */
370
+ private async getWorldState() {
371
+ try {
372
+ // Attempt to sync the world state if necessary
373
+ await this.syncWorldState();
374
+ } catch (err) {
375
+ this.log.error(`Error getting world state: ${err}`);
376
+ }
377
+ return this.worldStateSynchroniser.getCommitted();
378
+ }
379
+
380
+ /**
381
+ * Ensure we fully sync the world state
382
+ * @returns A promise that fulfils once the world state is synced
383
+ */
384
+ private async syncWorldState() {
385
+ const blockSourceHeight = await this.blockSource.getBlockNumber();
386
+ await this.worldStateSynchroniser.syncImmediate(blockSourceHeight);
387
+ }
354
388
  }