@aztec/world-state 3.0.0-devnet.2 → 3.0.0-devnet.2-patch.1

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 (49) hide show
  1. package/dest/index.d.ts +1 -1
  2. package/dest/instrumentation/instrumentation.d.ts +1 -1
  3. package/dest/instrumentation/instrumentation.d.ts.map +1 -1
  4. package/dest/native/bench_metrics.d.ts +1 -1
  5. package/dest/native/bench_metrics.d.ts.map +1 -1
  6. package/dest/native/fork_checkpoint.d.ts +1 -1
  7. package/dest/native/fork_checkpoint.d.ts.map +1 -1
  8. package/dest/native/index.d.ts +1 -1
  9. package/dest/native/merkle_trees_facade.d.ts +6 -5
  10. package/dest/native/merkle_trees_facade.d.ts.map +1 -1
  11. package/dest/native/merkle_trees_facade.js +5 -3
  12. package/dest/native/message.d.ts +12 -11
  13. package/dest/native/message.d.ts.map +1 -1
  14. package/dest/native/message.js +14 -13
  15. package/dest/native/native_world_state.d.ts +10 -9
  16. package/dest/native/native_world_state.d.ts.map +1 -1
  17. package/dest/native/native_world_state.js +13 -9
  18. package/dest/native/native_world_state_instance.d.ts +10 -1
  19. package/dest/native/native_world_state_instance.d.ts.map +1 -1
  20. package/dest/native/native_world_state_instance.js +21 -0
  21. package/dest/native/world_state_ops_queue.d.ts +1 -1
  22. package/dest/native/world_state_ops_queue.d.ts.map +1 -1
  23. package/dest/synchronizer/config.d.ts +1 -1
  24. package/dest/synchronizer/errors.d.ts +1 -1
  25. package/dest/synchronizer/errors.d.ts.map +1 -1
  26. package/dest/synchronizer/factory.d.ts +1 -1
  27. package/dest/synchronizer/index.d.ts +1 -1
  28. package/dest/synchronizer/server_world_state_synchronizer.d.ts +7 -25
  29. package/dest/synchronizer/server_world_state_synchronizer.d.ts.map +1 -1
  30. package/dest/synchronizer/server_world_state_synchronizer.js +32 -48
  31. package/dest/test/index.d.ts +1 -1
  32. package/dest/test/utils.d.ts +16 -9
  33. package/dest/test/utils.d.ts.map +1 -1
  34. package/dest/test/utils.js +52 -52
  35. package/dest/testing.d.ts +2 -2
  36. package/dest/testing.d.ts.map +1 -1
  37. package/dest/testing.js +1 -1
  38. package/dest/world-state-db/index.d.ts +1 -1
  39. package/dest/world-state-db/merkle_tree_db.d.ts +8 -9
  40. package/dest/world-state-db/merkle_tree_db.d.ts.map +1 -1
  41. package/package.json +13 -12
  42. package/src/native/merkle_trees_facade.ts +7 -6
  43. package/src/native/message.ts +23 -22
  44. package/src/native/native_world_state.ts +29 -21
  45. package/src/native/native_world_state_instance.ts +28 -0
  46. package/src/synchronizer/server_world_state_synchronizer.ts +59 -73
  47. package/src/test/utils.ts +88 -94
  48. package/src/testing.ts +1 -1
  49. package/src/world-state-db/merkle_tree_db.ts +7 -8
package/src/test/utils.ts CHANGED
@@ -4,122 +4,99 @@ 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/fields';
9
- import { L2Block } from '@aztec/stdlib/block';
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';
16
19
 
17
20
  import type { NativeWorldStateService } from '../native/native_world_state.js';
18
21
 
19
- export async function mockBlock(
20
- blockNum: number,
21
- size: number,
22
- fork: MerkleTreeWriteOperations,
23
- maxEffects: number | undefined = undefined,
24
- ) {
25
- const l2Block = await L2Block.random(blockNum, size, maxEffects);
26
- const l1ToL2Messages = Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(0).map(Fr.random);
27
-
28
- {
29
- const insertData = async (
30
- treeId: IndexedTreeId,
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
- }
22
+ export async function updateBlockState(block: L2BlockNew, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations) {
23
+ const insertData = async (
24
+ treeId: IndexedTreeId,
25
+ data: Buffer[][],
26
+ subTreeHeight: number,
27
+ fork: MerkleTreeWriteOperations,
28
+ ) => {
29
+ for (const dataBatch of data) {
30
+ await fork.batchInsert(treeId, dataBatch, subTreeHeight);
31
+ }
32
+ };
33
+
34
+ const publicDataInsert = insertData(
35
+ MerkleTreeId.PUBLIC_DATA_TREE,
36
+ block.body.txEffects.map(txEffect => txEffect.publicDataWrites.map(write => write.toBuffer())),
37
+ 0,
38
+ fork,
39
+ );
40
+ const nullifierInsert = insertData(
41
+ MerkleTreeId.NULLIFIER_TREE,
42
+ block.body.txEffects.map(txEffect =>
43
+ padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
44
+ ),
45
+ NULLIFIER_SUBTREE_HEIGHT,
46
+ fork,
47
+ );
48
+ const noteHashesPadded = block.body.txEffects.flatMap(txEffect =>
49
+ padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
50
+ );
51
+
52
+ const l1ToL2MessagesPadded =
53
+ block.indexWithinCheckpoint === 0
54
+ ? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
55
+ : l1ToL2Messages;
56
+
57
+ const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
58
+ const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
59
+ await Promise.all([publicDataInsert, nullifierInsert, noteHashInsert, messageInsert]);
64
60
 
65
61
  const state = await fork.getStateReference();
66
- l2Block.header.state = state;
67
- await fork.updateArchive(l2Block.getBlockHeader());
62
+ block.header.state = state;
63
+ await fork.updateArchive(block.header);
68
64
 
69
65
  const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
70
66
 
71
- l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
67
+ block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
68
+ }
69
+
70
+ export async function mockBlock(
71
+ blockNum: BlockNumber,
72
+ size: number,
73
+ fork: MerkleTreeWriteOperations,
74
+ maxEffects: number | undefined = 1000, // Defaults to the maximum tx effects.
75
+ numL1ToL2Messages: number = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
76
+ isFirstBlockInCheckpoint: boolean = true,
77
+ ) {
78
+ const block = await L2BlockNew.random(blockNum, {
79
+ indexWithinCheckpoint: isFirstBlockInCheckpoint ? 0 : 1,
80
+ txsPerBlock: size,
81
+ txOptions: { maxEffects },
82
+ });
83
+ const l1ToL2Messages = mockL1ToL2Messages(numL1ToL2Messages);
84
+
85
+ await updateBlockState(block, l1ToL2Messages, fork);
72
86
 
73
87
  return {
74
- block: l2Block,
88
+ block,
75
89
  messages: l1ToL2Messages,
76
90
  };
77
91
  }
78
92
 
79
- export async function mockEmptyBlock(blockNum: number, fork: MerkleTreeWriteOperations) {
80
- const l2Block = L2Block.empty();
93
+ export async function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWriteOperations) {
94
+ const l2Block = L2BlockNew.empty();
81
95
  const l1ToL2Messages = Array(16).fill(0).map(Fr.zero);
82
96
 
83
97
  l2Block.header.globalVariables.blockNumber = blockNum;
84
98
 
85
- // Sync the append only trees
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));
99
+ await updateBlockState(l2Block, l1ToL2Messages, fork);
123
100
 
124
101
  return {
125
102
  block: l2Block,
@@ -127,13 +104,18 @@ export async function mockEmptyBlock(blockNum: number, fork: MerkleTreeWriteOper
127
104
  };
128
105
  }
129
106
 
130
- export async function mockBlocks(from: number, count: number, numTxs: number, worldState: NativeWorldStateService) {
131
- const tempFork = await worldState.fork(from - 1);
107
+ export async function mockBlocks(
108
+ from: BlockNumber,
109
+ count: number,
110
+ numTxs: number,
111
+ worldState: NativeWorldStateService,
112
+ ) {
113
+ const tempFork = await worldState.fork(BlockNumber(from - 1));
132
114
 
133
115
  const blocks = [];
134
116
  const messagesArray = [];
135
117
  for (let blockNumber = from; blockNumber < from + count; blockNumber++) {
136
- const { block, messages } = await mockBlock(blockNumber, numTxs, tempFork);
118
+ const { block, messages } = await mockBlock(BlockNumber(blockNumber), numTxs, tempFork);
137
119
  blocks.push(block);
138
120
  messagesArray.push(messages);
139
121
  }
@@ -143,6 +125,18 @@ export async function mockBlocks(from: number, count: number, numTxs: number, wo
143
125
  return { blocks, messages: messagesArray };
144
126
  }
145
127
 
128
+ export async function mockCheckpoint(
129
+ checkpointNumber: CheckpointNumber,
130
+ fork: MerkleTreeWriteOperations,
131
+ options: Partial<Parameters<typeof mockCheckpointAndMessages>[1]> = {},
132
+ ) {
133
+ const { checkpoint, messages } = await mockCheckpointAndMessages(checkpointNumber, options);
134
+ await asyncMap(checkpoint.blocks, async (block, i) => {
135
+ await updateBlockState(block, i === 0 ? messages : [], fork);
136
+ });
137
+ return { checkpoint, messages };
138
+ }
139
+
146
140
  export async function assertSameState(forkA: MerkleTreeReadOperations, forkB: MerkleTreeReadOperations) {
147
141
  const nativeStateRef = await forkA.getStateReference();
148
142
  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/fields';
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,7 +1,8 @@
1
1
  import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/constants';
2
- import type { Fr } from '@aztec/foundation/fields';
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 { L2Block } from '@aztec/stdlib/block';
5
+ import type { L2BlockNew } from '@aztec/stdlib/block';
5
6
  import type { ForkMerkleTreeOperations, MerkleTreeReadOperations } from '@aztec/stdlib/interfaces/server';
6
7
  import type { MerkleTreeId } from '@aztec/stdlib/trees';
7
8
 
@@ -39,10 +40,8 @@ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
39
40
  * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
40
41
  * @param block - The L2 block to handle.
41
42
  * @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
43
  */
45
- handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[], isFirstBlock?: boolean): Promise<WorldStateStatusFull>;
44
+ handleL2BlockAndMessages(block: L2BlockNew, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
46
45
 
47
46
  /**
48
47
  * Gets a handle that allows reading the latest committed state
@@ -54,21 +53,21 @@ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
54
53
  * @param toBlockNumber The block number of the new oldest historical block
55
54
  * @returns The new WorldStateStatus
56
55
  */
57
- removeHistoricalBlocks(toBlockNumber: bigint): Promise<WorldStateStatusFull>;
56
+ removeHistoricalBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
58
57
 
59
58
  /**
60
59
  * Removes all pending blocks down to but not including the given block number
61
60
  * @param toBlockNumber The block number of the new tip of the pending chain,
62
61
  * @returns The new WorldStateStatus
63
62
  */
64
- unwindBlocks(toBlockNumber: bigint): Promise<WorldStateStatusFull>;
63
+ unwindBlocks(toBlockNumber: BlockNumber): Promise<WorldStateStatusFull>;
65
64
 
66
65
  /**
67
66
  * Advances the finalized block number to be the number provided
68
67
  * @param toBlockNumber The block number that is now the tip of the finalized chain
69
68
  * @returns The new WorldStateStatus
70
69
  */
71
- setFinalized(toBlockNumber: bigint): Promise<WorldStateStatusSummary>;
70
+ setFinalized(toBlockNumber: BlockNumber): Promise<WorldStateStatusSummary>;
72
71
 
73
72
  /**
74
73
  * Gets the current status summary of the database.