@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
package/src/test/utils.ts CHANGED
@@ -4,129 +4,109 @@ import {
4
4
  NULLIFIER_SUBTREE_HEIGHT,
5
5
  NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
6
6
  } from '@aztec/constants';
7
- import { BlockNumber, type CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
7
+ import { asyncMap } from '@aztec/foundation/async-map';
8
+ import { BlockNumber, type CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
8
9
  import { padArrayEnd } from '@aztec/foundation/collection';
9
- import { Fr } from '@aztec/foundation/fields';
10
- import { L2BlockNew } from '@aztec/stdlib/block';
11
- import { Checkpoint } from '@aztec/stdlib/checkpoint';
10
+ import { Fr } from '@aztec/foundation/curves/bn254';
11
+ import { L2Block } from '@aztec/stdlib/block';
12
12
  import type {
13
13
  IndexedTreeId,
14
14
  MerkleTreeReadOperations,
15
15
  MerkleTreeWriteOperations,
16
16
  } from '@aztec/stdlib/interfaces/server';
17
- import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
17
+ import { mockCheckpointAndMessages, mockL1ToL2Messages } from '@aztec/stdlib/testing';
18
18
  import { AppendOnlyTreeSnapshot, MerkleTreeId } from '@aztec/stdlib/trees';
19
+ import { BlockHeader } from '@aztec/stdlib/tx';
19
20
 
20
21
  import type { NativeWorldStateService } from '../native/native_world_state.js';
21
22
 
22
- export async function mockBlock(
23
- blockNum: BlockNumber,
24
- size: number,
25
- fork: MerkleTreeWriteOperations,
26
- maxEffects: number | undefined = 1000, // Defaults to the maximum tx effects.
27
- numL1ToL2Messages: number = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
28
- isFirstBlock: boolean = true,
29
- ) {
30
- const l2Block = await L2BlockNew.random(blockNum, { txsPerBlock: size, txOptions: { maxEffects } });
31
- const l1ToL2Messages = mockL1ToL2Messages(numL1ToL2Messages);
23
+ export async function updateBlockState(block: L2Block, l1ToL2Messages: Fr[], fork: MerkleTreeWriteOperations) {
24
+ const insertData = async (
25
+ treeId: IndexedTreeId,
26
+ data: Buffer[][],
27
+ subTreeHeight: number,
28
+ fork: MerkleTreeWriteOperations,
29
+ ) => {
30
+ for (const dataBatch of data) {
31
+ await fork.batchInsert(treeId, dataBatch, subTreeHeight);
32
+ }
33
+ };
32
34
 
33
- {
34
- const insertData = async (
35
- treeId: IndexedTreeId,
36
- data: Buffer[][],
37
- subTreeHeight: number,
38
- fork: MerkleTreeWriteOperations,
39
- ) => {
40
- for (const dataBatch of data) {
41
- await fork.batchInsert(treeId, dataBatch, subTreeHeight);
42
- }
43
- };
44
-
45
- const publicDataInsert = insertData(
46
- MerkleTreeId.PUBLIC_DATA_TREE,
47
- l2Block.body.txEffects.map(txEffect => txEffect.publicDataWrites.map(write => write.toBuffer())),
48
- 0,
49
- fork,
50
- );
51
- const nullifierInsert = insertData(
52
- MerkleTreeId.NULLIFIER_TREE,
53
- l2Block.body.txEffects.map(txEffect =>
54
- padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
55
- ),
56
- NULLIFIER_SUBTREE_HEIGHT,
57
- fork,
58
- );
59
- const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
60
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
61
- );
62
-
63
- const l1ToL2MessagesPadded = isFirstBlock
35
+ const publicDataInsert = insertData(
36
+ MerkleTreeId.PUBLIC_DATA_TREE,
37
+ block.body.txEffects.map(txEffect => txEffect.publicDataWrites.map(write => write.toBuffer())),
38
+ 0,
39
+ fork,
40
+ );
41
+ const nullifierInsert = insertData(
42
+ MerkleTreeId.NULLIFIER_TREE,
43
+ block.body.txEffects.map(txEffect =>
44
+ padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
45
+ ),
46
+ NULLIFIER_SUBTREE_HEIGHT,
47
+ fork,
48
+ );
49
+ const noteHashesPadded = block.body.txEffects.flatMap(txEffect =>
50
+ padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
51
+ );
52
+
53
+ const l1ToL2MessagesPadded =
54
+ block.indexWithinCheckpoint === 0
64
55
  ? padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP)
65
56
  : l1ToL2Messages;
66
57
 
67
- const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
68
- const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
69
- await Promise.all([publicDataInsert, nullifierInsert, noteHashInsert, messageInsert]);
70
- }
58
+ const noteHashInsert = fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
59
+ const messageInsert = fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
60
+ await Promise.all([publicDataInsert, nullifierInsert, noteHashInsert, messageInsert]);
71
61
 
72
62
  const state = await fork.getStateReference();
73
- l2Block.header.state = state;
74
- await fork.updateArchive(l2Block.header);
63
+
64
+ // Capture the archive root *before* appending this block so the header's lastArchive chains off the previous block.
65
+ // sync_block now verifies lastArchive against the committed archive root, so a block carrying an unchained value
66
+ // (e.g. the random lastArchive from L2Block.random) would be rejected as a divergence from the canonical chain.
67
+ const previousArchive = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
68
+ block.header = BlockHeader.from({
69
+ ...block.header,
70
+ state,
71
+ lastArchive: new AppendOnlyTreeSnapshot(Fr.fromBuffer(previousArchive.root), Number(previousArchive.size)),
72
+ });
73
+ await fork.updateArchive(block.header);
75
74
 
76
75
  const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
77
76
 
78
- l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
77
+ block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
78
+ }
79
+
80
+ export async function mockBlock(
81
+ blockNum: BlockNumber,
82
+ size: number,
83
+ fork: MerkleTreeWriteOperations,
84
+ maxEffects: number | undefined = 1000, // Defaults to the maximum tx effects.
85
+ numL1ToL2Messages: number = NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
86
+ isFirstBlockInCheckpoint: boolean = true,
87
+ ) {
88
+ const block = await L2Block.random(blockNum, {
89
+ indexWithinCheckpoint: isFirstBlockInCheckpoint ? IndexWithinCheckpoint(0) : IndexWithinCheckpoint(1),
90
+ txsPerBlock: size,
91
+ txOptions: { maxEffects },
92
+ });
93
+ const l1ToL2Messages = mockL1ToL2Messages(numL1ToL2Messages);
94
+
95
+ await updateBlockState(block, l1ToL2Messages, fork);
79
96
 
80
97
  return {
81
- block: l2Block,
98
+ block,
82
99
  messages: l1ToL2Messages,
83
100
  };
84
101
  }
85
102
 
86
103
  export async function mockEmptyBlock(blockNum: BlockNumber, fork: MerkleTreeWriteOperations) {
87
- const l2Block = L2BlockNew.empty();
104
+ const l2Block = L2Block.empty();
88
105
  const l1ToL2Messages = Array(16).fill(0).map(Fr.zero);
89
106
 
90
107
  l2Block.header.globalVariables.blockNumber = blockNum;
91
108
 
92
- // Sync the append only trees
93
- {
94
- const noteHashesPadded = l2Block.body.txEffects.flatMap(txEffect =>
95
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
96
- );
97
- await fork.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashesPadded);
98
-
99
- const l1ToL2MessagesPadded = padArrayEnd<Fr, number>(l1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
100
- await fork.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2MessagesPadded);
101
- }
102
-
103
- // Sync the indexed trees
104
- {
105
- // We insert the public data tree leaves with one batch per tx to avoid updating the same key twice
106
- for (const txEffect of l2Block.body.txEffects) {
107
- await fork.batchInsert(
108
- MerkleTreeId.PUBLIC_DATA_TREE,
109
- txEffect.publicDataWrites.map(write => write.toBuffer()),
110
- 0,
111
- );
112
-
113
- const nullifiersPadded = padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX);
114
-
115
- await fork.batchInsert(
116
- MerkleTreeId.NULLIFIER_TREE,
117
- nullifiersPadded.map(nullifier => nullifier.toBuffer()),
118
- NULLIFIER_SUBTREE_HEIGHT,
119
- );
120
- }
121
- }
122
-
123
- const state = await fork.getStateReference();
124
- l2Block.header.state = state;
125
- await fork.updateArchive(l2Block.header);
126
-
127
- const archiveState = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
128
-
129
- l2Block.archive = new AppendOnlyTreeSnapshot(Fr.fromBuffer(archiveState.root), Number(archiveState.size));
109
+ await updateBlockState(l2Block, l1ToL2Messages, fork);
130
110
 
131
111
  return {
132
112
  block: l2Block,
@@ -155,44 +135,15 @@ export async function mockBlocks(
155
135
  return { blocks, messages: messagesArray };
156
136
  }
157
137
 
158
- export function mockL1ToL2Messages(numL1ToL2Messages: number) {
159
- return Array(numL1ToL2Messages).fill(0).map(Fr.random);
160
- }
161
-
162
138
  export async function mockCheckpoint(
163
139
  checkpointNumber: CheckpointNumber,
164
- {
165
- startBlockNumber = BlockNumber(1),
166
- numBlocks = 1,
167
- numTxsPerBlock = 1,
168
- numL1ToL2Messages = 1,
169
- fork,
170
- }: {
171
- startBlockNumber?: BlockNumber;
172
- numBlocks?: number;
173
- numTxsPerBlock?: number;
174
- numL1ToL2Messages?: number;
175
- fork?: MerkleTreeWriteOperations;
176
- } = {},
140
+ fork: MerkleTreeWriteOperations,
141
+ options: Partial<Parameters<typeof mockCheckpointAndMessages>[1]> = {},
177
142
  ) {
178
- const slotNumber = SlotNumber(checkpointNumber * 10);
179
- const blocksAndMessages = [];
180
- for (let i = 0; i < numBlocks; i++) {
181
- const blockNumber = BlockNumber(startBlockNumber + i);
182
- const { block, messages } = fork
183
- ? await mockBlock(blockNumber, numTxsPerBlock, fork, blockNumber === startBlockNumber ? numL1ToL2Messages : 0)
184
- : {
185
- block: await L2BlockNew.random(blockNumber, { txsPerBlock: numTxsPerBlock, slotNumber }),
186
- messages: mockL1ToL2Messages(numL1ToL2Messages),
187
- };
188
- blocksAndMessages.push({ block, messages });
189
- }
190
-
191
- const messages = blocksAndMessages[0].messages;
192
- const inHash = computeInHashFromL1ToL2Messages(messages);
193
- const checkpoint = await Checkpoint.random(checkpointNumber, { numBlocks: 0, slotNumber, inHash });
194
- checkpoint.blocks = blocksAndMessages.map(({ block }) => block);
195
-
143
+ const { checkpoint, messages } = await mockCheckpointAndMessages(checkpointNumber, options);
144
+ await asyncMap(checkpoint.blocks, async (block, i) => {
145
+ await updateBlockState(block, i === 0 ? messages : [], fork);
146
+ });
196
147
  return { checkpoint, messages };
197
148
  }
198
149
 
package/src/testing.ts CHANGED
@@ -1,24 +1,24 @@
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';
6
+ import type { GenesisData } from '@aztec/stdlib/world-state';
6
7
 
7
8
  import { NativeWorldStateService } from './native/index.js';
8
9
 
9
- async function generateGenesisValues(prefilledPublicData: PublicDataTreeLeaf[]) {
10
- if (!prefilledPublicData.length) {
10
+ async function generateGenesisValues(genesis: GenesisData) {
11
+ if (!genesis.prefilledPublicData.length && genesis.genesisTimestamp === 0n) {
11
12
  return {
12
13
  genesisArchiveRoot: new Fr(GENESIS_ARCHIVE_ROOT),
13
14
  };
14
15
  }
15
16
 
16
- // Create a temporary world state to compute the genesis values.
17
- const ws = await NativeWorldStateService.tmp(
18
- undefined /* rollupAddress */,
19
- true /* cleanupTmpDir */,
20
- prefilledPublicData,
21
- );
17
+ // Compute the genesis values on a throwaway world state. The archive root derives only from the
18
+ // prefilled public data and the genesis timestamp, so the fsync-off ephemeral store (no version
19
+ // manager, no crash-recoverability) produces an identical root while skipping the fsync overhead
20
+ // that `tmp` pays. close() removes the tmpdir.
21
+ const ws = await NativeWorldStateService.ephemeral(genesis);
22
22
  const genesisArchiveRoot = new Fr((await ws.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
23
23
  await ws.close();
24
24
 
@@ -33,6 +33,7 @@ export async function getGenesisValues(
33
33
  initialAccounts: AztecAddress[],
34
34
  initialAccountFeeJuice = defaultInitialAccountFeeJuice,
35
35
  genesisPublicData: PublicDataTreeLeaf[] = [],
36
+ genesisTimestamp: bigint = 0n,
36
37
  ) {
37
38
  // Top up the accounts with fee juice.
38
39
  let prefilledPublicData = await Promise.all(
@@ -46,11 +47,12 @@ export async function getGenesisValues(
46
47
 
47
48
  prefilledPublicData.sort((a, b) => (b.slot.lt(a.slot) ? 1 : -1));
48
49
 
49
- const { genesisArchiveRoot } = await generateGenesisValues(prefilledPublicData);
50
+ const genesis: GenesisData = { prefilledPublicData, genesisTimestamp };
51
+ const { genesisArchiveRoot } = await generateGenesisValues(genesis);
50
52
 
51
53
  return {
52
54
  genesisArchiveRoot,
53
- prefilledPublicData,
55
+ genesis,
54
56
  fundingNeeded: BigInt(initialAccounts.length) * initialAccountFeeJuice.toBigInt(),
55
57
  };
56
58
  }
@@ -1,10 +1,12 @@
1
1
  import { MAX_NULLIFIERS_PER_TX, MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX } from '@aztec/constants';
2
- import { BlockNumber } from '@aztec/foundation/branded-types';
3
- import type { Fr } from '@aztec/foundation/fields';
4
- import type { IndexedTreeSnapshot, TreeSnapshot } from '@aztec/merkle-tree';
5
- import type { L2BlockNew } from '@aztec/stdlib/block';
6
- import type { ForkMerkleTreeOperations, MerkleTreeReadOperations } from '@aztec/stdlib/interfaces/server';
7
- import type { MerkleTreeId } from '@aztec/stdlib/trees';
2
+ import type { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import type { Fr } from '@aztec/foundation/curves/bn254';
4
+ import type { L2Block } from '@aztec/stdlib/block';
5
+ import type {
6
+ ForkMerkleTreeOperations,
7
+ MerkleTreeReadOperations,
8
+ ReadonlyWorldStateAccess,
9
+ } from '@aztec/stdlib/interfaces/server';
8
10
 
9
11
  import type { WorldStateStatusFull, WorldStateStatusSummary } from '../native/message.js';
10
12
 
@@ -27,27 +29,13 @@ export const INITIAL_NULLIFIER_TREE_SIZE = 2 * MAX_NULLIFIERS_PER_TX;
27
29
 
28
30
  export const INITIAL_PUBLIC_DATA_TREE_SIZE = 2 * MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX;
29
31
 
30
- export type TreeSnapshots = {
31
- [MerkleTreeId.NULLIFIER_TREE]: IndexedTreeSnapshot;
32
- [MerkleTreeId.NOTE_HASH_TREE]: TreeSnapshot<Fr>;
33
- [MerkleTreeId.PUBLIC_DATA_TREE]: IndexedTreeSnapshot;
34
- [MerkleTreeId.L1_TO_L2_MESSAGE_TREE]: TreeSnapshot<Fr>;
35
- [MerkleTreeId.ARCHIVE]: TreeSnapshot<Fr>;
36
- };
37
-
38
- export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations {
32
+ export interface MerkleTreeAdminDatabase extends ForkMerkleTreeOperations, ReadonlyWorldStateAccess {
39
33
  /**
40
34
  * Handles a single L2 block (i.e. Inserts the new note hashes into the merkle tree).
41
35
  * @param block - The L2 block to handle.
42
36
  * @param l1ToL2Messages - The L1 to L2 messages for the block.
43
- * @param isFirstBlock - Whether the block is the first block in a checkpoint. The messages are padded and inserted
44
- * to the tree for the first block in a checkpoint.
45
37
  */
46
- handleL2BlockAndMessages(
47
- block: L2BlockNew,
48
- l1ToL2Messages: Fr[],
49
- isFirstBlock: boolean,
50
- ): Promise<WorldStateStatusFull>;
38
+ handleL2BlockAndMessages(block: L2Block, l1ToL2Messages: Fr[]): Promise<WorldStateStatusFull>;
51
39
 
52
40
  /**
53
41
  * Gets a handle that allows reading the latest committed state
@@ -1,19 +0,0 @@
1
- import { WorldStateMessageType } from './message.js';
2
- export declare const MUTATING_MSG_TYPES: Set<WorldStateMessageType>;
3
- export declare class WorldStateOpsQueue {
4
- private requests;
5
- private inFlightMutatingCount;
6
- private inFlightCount;
7
- private stopPromise?;
8
- private stopResolve?;
9
- private requestId;
10
- private ops;
11
- execute(request: () => Promise<any>, messageType: WorldStateMessageType, committedOnly: boolean): Promise<any>;
12
- private executeMutating;
13
- private executeNonMutatingUncommitted;
14
- private executeNonMutatingCommitted;
15
- private checkAndEnqueue;
16
- private sendEnqueuedRequest;
17
- stop(): Promise<void>;
18
- }
19
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid29ybGRfc3RhdGVfb3BzX3F1ZXVlLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvbmF0aXZlL3dvcmxkX3N0YXRlX29wc19xdWV1ZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFQSxPQUFPLEVBQUUscUJBQXFCLEVBQUUsTUFBTSxjQUFjLENBQUM7QUF5QnJELGVBQU8sTUFBTSxrQkFBa0IsNEJBZ0I3QixDQUFDO0FBR0gscUJBQWEsa0JBQWtCO0lBQzdCLE9BQU8sQ0FBQyxRQUFRLENBQXNCO0lBQ3RDLE9BQU8sQ0FBQyxxQkFBcUIsQ0FBSztJQUNsQyxPQUFPLENBQUMsYUFBYSxDQUFLO0lBQzFCLE9BQU8sQ0FBQyxXQUFXLENBQUMsQ0FBZ0I7SUFDcEMsT0FBTyxDQUFDLFdBQVcsQ0FBQyxDQUFhO0lBQ2pDLE9BQU8sQ0FBQyxTQUFTLENBQUs7SUFDdEIsT0FBTyxDQUFDLEdBQUcsQ0FBd0M7SUFJNUMsT0FBTyxDQUFDLE9BQU8sRUFBRSxNQUFNLE9BQU8sQ0FBQyxHQUFHLENBQUMsRUFBRSxXQUFXLEVBQUUscUJBQXFCLEVBQUUsYUFBYSxFQUFFLE9BQU8sZ0JBc0JyRztJQUdELE9BQU8sQ0FBQyxlQUFlO0lBV3ZCLE9BQU8sQ0FBQyw2QkFBNkI7SUFZckMsT0FBTyxDQUFDLDJCQUEyQjtJQVVuQyxPQUFPLENBQUMsZUFBZTtJQXlDdkIsT0FBTyxDQUFDLG1CQUFtQjtJQWdCcEIsSUFBSSxrQkFnQlY7Q0FDRiJ9
@@ -1 +0,0 @@
1
- {"version":3,"file":"world_state_ops_queue.d.ts","sourceRoot":"","sources":["../../src/native/world_state_ops_queue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAyBrD,eAAO,MAAM,kBAAkB,4BAgB7B,CAAC;AAGH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,QAAQ,CAAsB;IACtC,OAAO,CAAC,qBAAqB,CAAK;IAClC,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,WAAW,CAAC,CAAgB;IACpC,OAAO,CAAC,WAAW,CAAC,CAAa;IACjC,OAAO,CAAC,SAAS,CAAK;IACtB,OAAO,CAAC,GAAG,CAAwC;IAI5C,OAAO,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,EAAE,WAAW,EAAE,qBAAqB,EAAE,aAAa,EAAE,OAAO,gBAsBrG;IAGD,OAAO,CAAC,eAAe;IAWvB,OAAO,CAAC,6BAA6B;IAYrC,OAAO,CAAC,2BAA2B;IAUnC,OAAO,CAAC,eAAe;IAyCvB,OAAO,CAAC,mBAAmB;IAgBpB,IAAI,kBAgBV;CACF"}
@@ -1,146 +0,0 @@
1
- import { promiseWithResolvers } from '@aztec/foundation/promise';
2
- import { WorldStateMessageType } from './message.js';
3
- // These are the set of message types that implement mutating operations
4
- // Messages of these types require exclusive access to their given forks
5
- export const MUTATING_MSG_TYPES = new Set([
6
- WorldStateMessageType.APPEND_LEAVES,
7
- WorldStateMessageType.BATCH_INSERT,
8
- WorldStateMessageType.SEQUENTIAL_INSERT,
9
- WorldStateMessageType.UPDATE_ARCHIVE,
10
- WorldStateMessageType.COMMIT,
11
- WorldStateMessageType.ROLLBACK,
12
- WorldStateMessageType.SYNC_BLOCK,
13
- WorldStateMessageType.CREATE_FORK,
14
- WorldStateMessageType.DELETE_FORK,
15
- WorldStateMessageType.FINALIZE_BLOCKS,
16
- WorldStateMessageType.UNWIND_BLOCKS,
17
- WorldStateMessageType.REMOVE_HISTORICAL_BLOCKS,
18
- WorldStateMessageType.CREATE_CHECKPOINT,
19
- WorldStateMessageType.COMMIT_CHECKPOINT,
20
- WorldStateMessageType.REVERT_CHECKPOINT
21
- ]);
22
- // This class implements the per-fork operation queue
23
- export class WorldStateOpsQueue {
24
- requests = [];
25
- inFlightMutatingCount = 0;
26
- inFlightCount = 0;
27
- stopPromise;
28
- stopResolve;
29
- requestId = 0;
30
- ops = new Map();
31
- // The primary public api, this is where an operation is queued
32
- // We return a promise that will ultimately be resolved/rejected with the response/error generated by the 'request' argument
33
- execute(request, messageType, committedOnly) {
34
- if (this.stopResolve !== undefined) {
35
- throw new Error('Unable to send request to world state, queue already stopped');
36
- }
37
- const op = {
38
- requestId: this.requestId++,
39
- mutating: MUTATING_MSG_TYPES.has(messageType),
40
- request,
41
- promise: promiseWithResolvers()
42
- };
43
- this.ops.set(op.requestId, op);
44
- // Perform the appropriate action based upon the queueing rules
45
- if (op.mutating) {
46
- this.executeMutating(op);
47
- } else if (committedOnly === false) {
48
- this.executeNonMutatingUncommitted(op);
49
- } else {
50
- this.executeNonMutatingCommitted(op);
51
- }
52
- return op.promise.promise;
53
- }
54
- // Mutating requests need exclusive access
55
- executeMutating(op) {
56
- // If nothing is in flight then we send the request immediately
57
- // Otherwise add to the queue
58
- if (this.inFlightCount === 0) {
59
- this.sendEnqueuedRequest(op);
60
- } else {
61
- this.requests.push(op);
62
- }
63
- }
64
- // Non mutating requests including uncommitted state
65
- executeNonMutatingUncommitted(op) {
66
- // If there are no mutating requests in flight and there is nothing queued
67
- // then send the request immediately
68
- // If a mutating request is in flight then we must wait
69
- // If a mutating request is not in flight but something is queued then it must be a mutating request
70
- if (this.inFlightMutatingCount == 0 && this.requests.length == 0) {
71
- this.sendEnqueuedRequest(op);
72
- } else {
73
- this.requests.push(op);
74
- }
75
- }
76
- executeNonMutatingCommitted(op) {
77
- // This is a non-mutating request for committed data
78
- // It can always be sent
79
- op.request().then(op.promise.resolve, op.promise.reject).finally(()=>{
80
- this.ops.delete(op.requestId);
81
- });
82
- }
83
- checkAndEnqueue(completedOp) {
84
- // As request has completed
85
- // First we decrements the relevant in flight counters
86
- if (completedOp.mutating) {
87
- --this.inFlightMutatingCount;
88
- }
89
- --this.inFlightCount;
90
- // If there are still requests in flight then do nothing further
91
- if (this.inFlightCount != 0) {
92
- return;
93
- }
94
- // No requests in flight, send next queued requests
95
- // We loop and send:
96
- // 1 mutating request if it is next in the queue
97
- // As many non-mutating requests as we encounter until
98
- // we exhaust the queue or we reach a mutating request
99
- while(this.requests.length > 0){
100
- const next = this.requests[0];
101
- if (next.mutating) {
102
- if (this.inFlightCount == 0) {
103
- // send the mutating request
104
- this.requests.shift();
105
- this.sendEnqueuedRequest(next);
106
- }
107
- break;
108
- } else {
109
- // not mutating, send and go round again
110
- this.requests.shift();
111
- this.sendEnqueuedRequest(next);
112
- }
113
- }
114
- // If the queue is empty, there is nothing in flight and we have been told to stop, then resolve the stop promise
115
- if (this.inFlightCount == 0 && this.stopResolve !== undefined) {
116
- this.stopResolve();
117
- }
118
- }
119
- sendEnqueuedRequest(op) {
120
- // Here we increment the in flight counts before sending
121
- ++this.inFlightCount;
122
- if (op.mutating) {
123
- ++this.inFlightMutatingCount;
124
- }
125
- // Make the request and pass the response/error through to the stored promise
126
- op.request().then(op.promise.resolve, op.promise.reject).finally(()=>{
127
- this.checkAndEnqueue(op);
128
- this.ops.delete(op.requestId);
129
- });
130
- }
131
- stop() {
132
- // If there is already a stop promise then return it
133
- if (this.stopPromise) {
134
- return this.stopPromise;
135
- }
136
- // Otherwise create a new one and capture the resolve method
137
- this.stopPromise = new Promise((resolve)=>{
138
- this.stopResolve = resolve;
139
- });
140
- // If no outstanding requests then immediately resolve the promise
141
- if (this.requests.length == 0 && this.inFlightCount == 0 && this.stopResolve !== undefined) {
142
- this.stopResolve();
143
- }
144
- return this.stopPromise;
145
- }
146
- }
@@ -1,11 +0,0 @@
1
- import type { Fr } from '@aztec/foundation/fields';
2
- import type { L2BlockNew, L2BlockSource } from '@aztec/stdlib/block';
3
- import { type L1ToL2MessageSource } from '@aztec/stdlib/messaging';
4
- /**
5
- * Determine which blocks in the given array are the first block in a checkpoint.
6
- * @param blocks - The candidate blocks, sorted by block number in ascending order.
7
- * @param l2BlockSource - The L2 block source to use to fetch the checkpoints, block headers and L1->L2 messages.
8
- * @returns A map of block numbers that begin a checkpoint to the L1->L2 messages for that checkpoint.
9
- */
10
- export declare function findFirstBlocksInCheckpoints(blocks: L2BlockNew[], l2BlockSource: L2BlockSource & L1ToL2MessageSource): Promise<Map<number, Fr[]>>;
11
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zeW5jaHJvbml6ZXIvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxLQUFLLEVBQUUsRUFBRSxFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFDbkQsT0FBTyxLQUFLLEVBQUUsVUFBVSxFQUFFLGFBQWEsRUFBRSxNQUFNLHFCQUFxQixDQUFDO0FBRXJFLE9BQU8sRUFBRSxLQUFLLG1CQUFtQixFQUFtQyxNQUFNLHlCQUF5QixDQUFDO0FBRXBHOzs7OztHQUtHO0FBQ0gsd0JBQXNCLDRCQUE0QixDQUNoRCxNQUFNLEVBQUUsVUFBVSxFQUFFLEVBQ3BCLGFBQWEsRUFBRSxhQUFhLEdBQUcsbUJBQW1CLEdBQ2pELE9BQU8sQ0FBQyxHQUFHLENBQUMsTUFBTSxFQUFFLEVBQUUsRUFBRSxDQUFDLENBQUMsQ0FtRTVCIn0=
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/synchronizer/utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAErE,OAAO,EAAE,KAAK,mBAAmB,EAAmC,MAAM,yBAAyB,CAAC;AAEpG;;;;;GAKG;AACH,wBAAsB,4BAA4B,CAChD,MAAM,EAAE,UAAU,EAAE,EACpB,aAAa,EAAE,aAAa,GAAG,mBAAmB,GACjD,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC,CAmE5B"}
@@ -1,59 +0,0 @@
1
- import { BlockNumber } from '@aztec/foundation/branded-types';
2
- import { computeInHashFromL1ToL2Messages } from '@aztec/stdlib/messaging';
3
- /**
4
- * Determine which blocks in the given array are the first block in a checkpoint.
5
- * @param blocks - The candidate blocks, sorted by block number in ascending order.
6
- * @param l2BlockSource - The L2 block source to use to fetch the checkpoints, block headers and L1->L2 messages.
7
- * @returns A map of block numbers that begin a checkpoint to the L1->L2 messages for that checkpoint.
8
- */ export async function findFirstBlocksInCheckpoints(blocks, l2BlockSource) {
9
- // Select the blocks that are the final block within each group of identical slot numbers.
10
- let seenSlot;
11
- const maybeLastBlocks = [
12
- ...blocks
13
- ].reverse().filter((b)=>{
14
- if (b.header.globalVariables.slotNumber !== seenSlot) {
15
- seenSlot = b.header.globalVariables.slotNumber;
16
- return true;
17
- }
18
- return false;
19
- }).reverse();
20
- // Try to fetch the checkpoints for those blocks. If undefined (which should only occur for blocks.at(-1)),
21
- // then the block is not the last one in a checkpoint.
22
- // If we are not checking the inHashes below, only blocks.at(-1) would need its checkpoint header fetched.
23
- const checkpointedBlocks = (await Promise.all(maybeLastBlocks.map(async (b)=>({
24
- blockNumber: b.number,
25
- // A checkpoint's archive root is the archive root of its last block.
26
- checkpoint: await l2BlockSource.getCheckpointByArchive(b.archive.root)
27
- })))).filter((b)=>b.checkpoint !== undefined);
28
- // Verify that the L1->L2 messages hash to the checkpoint's inHash.
29
- const checkpointedL1ToL2Messages = await Promise.all(checkpointedBlocks.map((b)=>l2BlockSource.getL1ToL2MessagesForCheckpoint(b.checkpoint.number)));
30
- checkpointedBlocks.forEach((b, i)=>{
31
- const computedInHash = computeInHashFromL1ToL2Messages(checkpointedL1ToL2Messages[i]);
32
- const inHash = b.checkpoint.header.contentCommitment.inHash;
33
- if (!computedInHash.equals(inHash)) {
34
- throw new Error('Obtained L1 to L2 messages failed to be hashed to the checkpoint inHash');
35
- }
36
- });
37
- // Compute the first block numbers, which should be right after each checkpointed block. Exclude blocks that haven't
38
- // been added yet.
39
- const firstBlockNumbers = checkpointedBlocks.map((b)=>BlockNumber(b.blockNumber + 1)).filter((n)=>n <= blocks.at(-1).number);
40
- // Check if blocks[0] is the first block in a checkpoint.
41
- if (blocks[0].number === 1) {
42
- firstBlockNumbers.push(blocks[0].number);
43
- } else {
44
- const lastBlockHeader = await l2BlockSource.getBlockHeader(BlockNumber(blocks[0].number - 1));
45
- if (!lastBlockHeader) {
46
- throw new Error(`Failed to get block ${blocks[0].number - 1}`);
47
- }
48
- if (lastBlockHeader.globalVariables.slotNumber !== blocks[0].header.globalVariables.slotNumber) {
49
- firstBlockNumbers.push(blocks[0].number);
50
- }
51
- }
52
- // Fetch the L1->L2 messages for the first blocks and assign them to the map.
53
- const messagesByBlockNumber = new Map();
54
- await Promise.all(firstBlockNumbers.map(async (blockNumber)=>{
55
- const l1ToL2Messages = await l2BlockSource.getL1ToL2Messages(blockNumber);
56
- messagesByBlockNumber.set(blockNumber, l1ToL2Messages);
57
- }));
58
- return messagesByBlockNumber;
59
- }