@aztec/prover-client 3.0.0-devnet.5 → 3.0.0-devnet.6-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 (109) hide show
  1. package/dest/block-factory/index.d.ts +1 -1
  2. package/dest/block-factory/light.d.ts +8 -8
  3. package/dest/block-factory/light.d.ts.map +1 -1
  4. package/dest/block-factory/light.js +37 -23
  5. package/dest/config.d.ts +1 -1
  6. package/dest/config.js +1 -1
  7. package/dest/index.d.ts +1 -1
  8. package/dest/light/lightweight_checkpoint_builder.d.ts +29 -0
  9. package/dest/light/lightweight_checkpoint_builder.d.ts.map +1 -0
  10. package/dest/light/lightweight_checkpoint_builder.js +108 -0
  11. package/dest/mocks/fixtures.d.ts +1 -4
  12. package/dest/mocks/fixtures.d.ts.map +1 -1
  13. package/dest/mocks/fixtures.js +7 -17
  14. package/dest/mocks/test_context.d.ts +27 -46
  15. package/dest/mocks/test_context.d.ts.map +1 -1
  16. package/dest/mocks/test_context.js +102 -116
  17. package/dest/orchestrator/block-building-helpers.d.ts +19 -21
  18. package/dest/orchestrator/block-building-helpers.d.ts.map +1 -1
  19. package/dest/orchestrator/block-building-helpers.js +93 -118
  20. package/dest/orchestrator/block-proving-state.d.ts +17 -11
  21. package/dest/orchestrator/block-proving-state.d.ts.map +1 -1
  22. package/dest/orchestrator/block-proving-state.js +81 -20
  23. package/dest/orchestrator/checkpoint-proving-state.d.ts +8 -8
  24. package/dest/orchestrator/checkpoint-proving-state.d.ts.map +1 -1
  25. package/dest/orchestrator/checkpoint-proving-state.js +15 -16
  26. package/dest/orchestrator/epoch-proving-state.d.ts +10 -9
  27. package/dest/orchestrator/epoch-proving-state.d.ts.map +1 -1
  28. package/dest/orchestrator/epoch-proving-state.js +6 -6
  29. package/dest/orchestrator/index.d.ts +1 -1
  30. package/dest/orchestrator/orchestrator.d.ts +13 -13
  31. package/dest/orchestrator/orchestrator.d.ts.map +1 -1
  32. package/dest/orchestrator/orchestrator.js +54 -60
  33. package/dest/orchestrator/orchestrator_metrics.d.ts +1 -1
  34. package/dest/orchestrator/orchestrator_metrics.d.ts.map +1 -1
  35. package/dest/orchestrator/tx-proving-state.d.ts +6 -6
  36. package/dest/orchestrator/tx-proving-state.d.ts.map +1 -1
  37. package/dest/orchestrator/tx-proving-state.js +14 -23
  38. package/dest/prover-client/factory.d.ts +1 -1
  39. package/dest/prover-client/index.d.ts +1 -1
  40. package/dest/prover-client/prover-client.d.ts +1 -1
  41. package/dest/prover-client/prover-client.d.ts.map +1 -1
  42. package/dest/prover-client/server-epoch-prover.d.ts +9 -8
  43. package/dest/prover-client/server-epoch-prover.d.ts.map +1 -1
  44. package/dest/prover-client/server-epoch-prover.js +4 -4
  45. package/dest/proving_broker/broker_prover_facade.d.ts +21 -21
  46. package/dest/proving_broker/broker_prover_facade.d.ts.map +1 -1
  47. package/dest/proving_broker/broker_prover_facade.js +7 -6
  48. package/dest/proving_broker/config.d.ts +12 -8
  49. package/dest/proving_broker/config.d.ts.map +1 -1
  50. package/dest/proving_broker/config.js +8 -2
  51. package/dest/proving_broker/factory.d.ts +1 -1
  52. package/dest/proving_broker/fixtures.d.ts +3 -2
  53. package/dest/proving_broker/fixtures.d.ts.map +1 -1
  54. package/dest/proving_broker/fixtures.js +3 -2
  55. package/dest/proving_broker/index.d.ts +1 -1
  56. package/dest/proving_broker/proof_store/factory.d.ts +2 -2
  57. package/dest/proving_broker/proof_store/gcs_proof_store.d.ts +1 -1
  58. package/dest/proving_broker/proof_store/gcs_proof_store.d.ts.map +1 -1
  59. package/dest/proving_broker/proof_store/index.d.ts +1 -1
  60. package/dest/proving_broker/proof_store/inline_proof_store.d.ts +1 -1
  61. package/dest/proving_broker/proof_store/inline_proof_store.d.ts.map +1 -1
  62. package/dest/proving_broker/proof_store/proof_store.d.ts +1 -1
  63. package/dest/proving_broker/proving_agent.d.ts +1 -1
  64. package/dest/proving_broker/proving_agent.d.ts.map +1 -1
  65. package/dest/proving_broker/proving_agent_instrumentation.d.ts +1 -1
  66. package/dest/proving_broker/proving_agent_instrumentation.d.ts.map +1 -1
  67. package/dest/proving_broker/proving_broker.d.ts +2 -2
  68. package/dest/proving_broker/proving_broker.d.ts.map +1 -1
  69. package/dest/proving_broker/proving_broker.js +4 -3
  70. package/dest/proving_broker/proving_broker_database/memory.d.ts +3 -2
  71. package/dest/proving_broker/proving_broker_database/memory.d.ts.map +1 -1
  72. package/dest/proving_broker/proving_broker_database/persisted.d.ts +3 -2
  73. package/dest/proving_broker/proving_broker_database/persisted.d.ts.map +1 -1
  74. package/dest/proving_broker/proving_broker_database/persisted.js +3 -2
  75. package/dest/proving_broker/proving_broker_database.d.ts +3 -2
  76. package/dest/proving_broker/proving_broker_database.d.ts.map +1 -1
  77. package/dest/proving_broker/proving_broker_instrumentation.d.ts +1 -1
  78. package/dest/proving_broker/proving_broker_instrumentation.d.ts.map +1 -1
  79. package/dest/proving_broker/proving_job_controller.d.ts +3 -2
  80. package/dest/proving_broker/proving_job_controller.d.ts.map +1 -1
  81. package/dest/proving_broker/proving_job_controller.js +3 -3
  82. package/dest/proving_broker/rpc.d.ts +4 -4
  83. package/dest/test/mock_proof_store.d.ts +3 -3
  84. package/dest/test/mock_proof_store.d.ts.map +1 -1
  85. package/dest/test/mock_prover.d.ts +5 -6
  86. package/dest/test/mock_prover.d.ts.map +1 -1
  87. package/dest/test/mock_prover.js +3 -3
  88. package/package.json +18 -17
  89. package/src/block-factory/light.ts +40 -43
  90. package/src/config.ts +1 -1
  91. package/src/light/lightweight_checkpoint_builder.ts +144 -0
  92. package/src/mocks/fixtures.ts +7 -30
  93. package/src/mocks/test_context.ts +145 -177
  94. package/src/orchestrator/block-building-helpers.ts +133 -215
  95. package/src/orchestrator/block-proving-state.ts +103 -25
  96. package/src/orchestrator/checkpoint-proving-state.ts +24 -20
  97. package/src/orchestrator/epoch-proving-state.ts +15 -11
  98. package/src/orchestrator/orchestrator.ts +76 -74
  99. package/src/orchestrator/tx-proving-state.ts +24 -33
  100. package/src/prover-client/server-epoch-prover.ts +8 -9
  101. package/src/proving_broker/broker_prover_facade.ts +32 -29
  102. package/src/proving_broker/config.ts +8 -1
  103. package/src/proving_broker/fixtures.ts +8 -3
  104. package/src/proving_broker/proving_broker.ts +4 -3
  105. package/src/proving_broker/proving_broker_database/memory.ts +2 -1
  106. package/src/proving_broker/proving_broker_database/persisted.ts +5 -4
  107. package/src/proving_broker/proving_broker_database.ts +2 -1
  108. package/src/proving_broker/proving_job_controller.ts +5 -4
  109. package/src/test/mock_prover.ts +9 -7
@@ -1,14 +1,14 @@
1
1
  import {
2
- BatchedBlob,
3
2
  BatchedBlobAccumulator,
4
3
  SpongeBlob,
5
4
  computeBlobsHashFromBlobs,
5
+ encodeBlockBlobData,
6
6
  getBlobCommitmentsFromBlobs,
7
7
  getBlobsPerL1Block,
8
8
  } from '@aztec/blob-lib';
9
9
  import {
10
10
  ARCHIVE_HEIGHT,
11
- CIVC_PROOF_LENGTH,
11
+ CHONK_PROOF_LENGTH,
12
12
  MAX_CONTRACT_CLASS_LOGS_PER_TX,
13
13
  MAX_NOTE_HASHES_PER_TX,
14
14
  MAX_NULLIFIERS_PER_TX,
@@ -17,24 +17,18 @@ import {
17
17
  NULLIFIER_SUBTREE_HEIGHT,
18
18
  NULLIFIER_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
19
19
  NULLIFIER_TREE_HEIGHT,
20
- NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
21
20
  PUBLIC_DATA_TREE_HEIGHT,
22
21
  } from '@aztec/constants';
23
22
  import { makeTuple } from '@aztec/foundation/array';
23
+ import { BlockNumber } from '@aztec/foundation/branded-types';
24
24
  import { padArrayEnd } from '@aztec/foundation/collection';
25
- import { sha256Trunc } from '@aztec/foundation/crypto';
26
- import { Fr } from '@aztec/foundation/fields';
27
- import { type Bufferable, type Tuple, assertLength, toFriendlyJSON } from '@aztec/foundation/serialize';
28
- import {
29
- MembershipWitness,
30
- MerkleTreeCalculator,
31
- computeCompressedUnbalancedMerkleTreeRoot,
32
- } from '@aztec/foundation/trees';
25
+ import { Fr } from '@aztec/foundation/curves/bn254';
26
+ import { type Bufferable, assertLength, toFriendlyJSON } from '@aztec/foundation/serialize';
27
+ import { MembershipWitness } from '@aztec/foundation/trees';
33
28
  import { getVkData } from '@aztec/noir-protocol-circuits-types/server/vks';
34
29
  import { getVKIndex, getVKSiblingPath } from '@aztec/noir-protocol-circuits-types/vk-tree';
35
30
  import { computeFeePayerBalanceLeafSlot } from '@aztec/protocol-contracts/fee-juice';
36
- import { Body, L2BlockHeader, getBlockBlobFields } from '@aztec/stdlib/block';
37
- import { getCheckpointBlobFields } from '@aztec/stdlib/checkpoint';
31
+ import { Body } from '@aztec/stdlib/block';
38
32
  import type { MerkleTreeWriteOperations, PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
39
33
  import { ContractClassLogFields } from '@aztec/stdlib/logs';
40
34
  import { Proof, ProofData, RecursiveProof } from '@aztec/stdlib/proofs';
@@ -43,20 +37,18 @@ import {
43
37
  BlockRollupPublicInputs,
44
38
  PrivateBaseRollupHints,
45
39
  PublicBaseRollupHints,
46
- PublicTubePrivateInputs,
40
+ PublicChonkVerifierPrivateInputs,
47
41
  TreeSnapshotDiffHints,
48
42
  } from '@aztec/stdlib/rollup';
49
43
  import {
50
44
  AppendOnlyTreeSnapshot,
51
45
  MerkleTreeId,
52
46
  NullifierLeafPreimage,
53
- PublicDataTreeLeaf,
54
47
  PublicDataTreeLeafPreimage,
55
48
  getTreeHeight,
56
49
  } from '@aztec/stdlib/trees';
57
50
  import {
58
51
  BlockHeader,
59
- ContentCommitment,
60
52
  GlobalVariables,
61
53
  PartialStateReference,
62
54
  type ProcessedTx,
@@ -103,30 +95,7 @@ export const insertSideEffectsAndBuildBaseRollupHints = runInSpan(
103
95
  NOTE_HASH_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
104
96
  );
105
97
 
106
- // Update the note hash trees with the new items being inserted to get the new roots
107
- // that will be used by the next iteration of the base rollup circuit, skipping the empty ones
108
- const noteHashes = padArrayEnd(tx.txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX);
109
- await db.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashes);
110
-
111
- // The read witnesses for a given TX should be generated before the writes of the same TX are applied.
112
- // All reads that refer to writes in the same tx are transient and can be simplified out.
113
- const txPublicDataUpdateRequestInfo = await processPublicDataUpdateRequests(tx, db);
114
-
115
- // Update the nullifier tree, capturing the low nullifier info for each individual operation
116
- const {
117
- lowLeavesWitnessData: nullifierWitnessLeaves,
118
- newSubtreeSiblingPath: nullifiersSubtreeRootSiblingPath,
119
- sortedNewLeaves: sortedNullifiers,
120
- sortedNewLeavesIndexes,
121
- } = await db.batchInsert(
122
- MerkleTreeId.NULLIFIER_TREE,
123
- padArrayEnd(tx.txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(n => n.toBuffer()),
124
- NULLIFIER_SUBTREE_HEIGHT,
125
- );
126
-
127
- if (nullifierWitnessLeaves === undefined) {
128
- throw new Error(`Could not craft nullifier batch insertion proofs`);
129
- }
98
+ const { nullifierInsertionResult, publicDataInsertionResult } = await insertSideEffects(tx, db);
130
99
 
131
100
  const blockHash = await tx.data.constants.anchorBlockHeader.hash();
132
101
  const anchorBlockArchiveSiblingPath = (
@@ -146,52 +115,59 @@ export const insertSideEffectsAndBuildBaseRollupHints = runInSpan(
146
115
  contractClassLogsFields,
147
116
  });
148
117
  } else {
149
- if (
150
- txPublicDataUpdateRequestInfo.lowPublicDataWritesMembershipWitnesses.length > 1 ||
151
- txPublicDataUpdateRequestInfo.lowPublicDataWritesPreimages.length > 1 ||
152
- txPublicDataUpdateRequestInfo.publicDataWritesSiblingPaths.length > 1
153
- ) {
118
+ if (tx.txEffect.publicDataWrites.length > 1) {
154
119
  throw new Error(`More than one public data write in a private only tx`);
155
120
  }
156
121
 
157
122
  // Get hints for reading fee payer's balance in the public data tree.
158
- const feePayerBalanceMembershipWitness = txPublicDataUpdateRequestInfo.lowPublicDataWritesMembershipWitnesses[0];
159
- const feePayerBalanceLeafPreimage = txPublicDataUpdateRequestInfo.lowPublicDataWritesPreimages[0];
123
+ const feePayerBalanceLeafWitnessData = publicDataInsertionResult.lowLeavesWitnessData[0];
124
+ const feePayerBalanceMembershipWitness = MembershipWitness.fromBufferArray<typeof PUBLIC_DATA_TREE_HEIGHT>(
125
+ feePayerBalanceLeafWitnessData.index,
126
+ assertLength(feePayerBalanceLeafWitnessData.siblingPath.toBufferArray(), PUBLIC_DATA_TREE_HEIGHT),
127
+ );
128
+ const feePayerBalanceLeafPreimage = feePayerBalanceLeafWitnessData.leafPreimage as PublicDataTreeLeafPreimage;
160
129
  const leafSlot = await computeFeePayerBalanceLeafSlot(tx.data.feePayer);
161
- if (!feePayerBalanceMembershipWitness || !leafSlot.equals(feePayerBalanceLeafPreimage?.leaf.slot)) {
130
+ if (!leafSlot.equals(feePayerBalanceLeafPreimage.leaf.slot)) {
162
131
  throw new Error(`Cannot find the public data tree leaf for the fee payer's balance`);
163
132
  }
164
133
 
165
- // Extract witness objects from returned data
166
- const nullifierPredecessorMembershipWitnessesWithoutPadding: MembershipWitness<typeof NULLIFIER_TREE_HEIGHT>[] =
167
- nullifierWitnessLeaves.map(l =>
168
- MembershipWitness.fromBufferArray(
134
+ // Get hints for inserting the nullifiers.
135
+ const nullifierLowLeavesWitnessData = nullifierInsertionResult.lowLeavesWitnessData!;
136
+ const nullifierPredecessorPreimages = padArrayEnd(
137
+ nullifierLowLeavesWitnessData.map(l => l.leafPreimage as NullifierLeafPreimage),
138
+ NullifierLeafPreimage.empty(),
139
+ MAX_NULLIFIERS_PER_TX,
140
+ );
141
+ const nullifierPredecessorMembershipWitnesses = padArrayEnd(
142
+ nullifierLowLeavesWitnessData.map(l =>
143
+ MembershipWitness.fromBufferArray<typeof NULLIFIER_TREE_HEIGHT>(
169
144
  l.index,
170
145
  assertLength(l.siblingPath.toBufferArray(), NULLIFIER_TREE_HEIGHT),
171
146
  ),
172
- );
147
+ ),
148
+ makeEmptyMembershipWitness(NULLIFIER_TREE_HEIGHT),
149
+ MAX_NULLIFIERS_PER_TX,
150
+ );
151
+ const sortedNullifiers = assertLength(
152
+ nullifierInsertionResult.sortedNewLeaves.map(n => Fr.fromBuffer(n)),
153
+ MAX_NULLIFIERS_PER_TX,
154
+ );
155
+ const sortedNullifierIndexes = assertLength(
156
+ nullifierInsertionResult.sortedNewLeavesIndexes,
157
+ MAX_NULLIFIERS_PER_TX,
158
+ );
159
+ const nullifierSubtreeRootSiblingPath = assertLength(
160
+ nullifierInsertionResult.newSubtreeSiblingPath.toFields(),
161
+ NULLIFIER_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
162
+ );
173
163
 
174
164
  const treeSnapshotDiffHints = TreeSnapshotDiffHints.from({
175
165
  noteHashSubtreeRootSiblingPath,
176
- nullifierPredecessorPreimages: padArrayEnd(
177
- nullifierWitnessLeaves.map(l => l.leafPreimage as NullifierLeafPreimage),
178
- NullifierLeafPreimage.empty(),
179
- MAX_NULLIFIERS_PER_TX,
180
- ),
181
- nullifierPredecessorMembershipWitnesses: makeTuple(MAX_NULLIFIERS_PER_TX, i =>
182
- i < nullifierPredecessorMembershipWitnessesWithoutPadding.length
183
- ? nullifierPredecessorMembershipWitnessesWithoutPadding[i]
184
- : makeEmptyMembershipWitness(NULLIFIER_TREE_HEIGHT),
185
- ),
186
- sortedNullifiers: assertLength(
187
- sortedNullifiers.map(n => Fr.fromBuffer(n)),
188
- MAX_NULLIFIERS_PER_TX,
189
- ),
190
- sortedNullifierIndexes: assertLength(sortedNewLeavesIndexes, MAX_NULLIFIERS_PER_TX),
191
- nullifierSubtreeRootSiblingPath: assertLength(
192
- nullifiersSubtreeRootSiblingPath.toFields(),
193
- NULLIFIER_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
194
- ),
166
+ nullifierPredecessorPreimages,
167
+ nullifierPredecessorMembershipWitnesses,
168
+ sortedNullifiers,
169
+ sortedNullifierIndexes,
170
+ nullifierSubtreeRootSiblingPath,
195
171
  feePayerBalanceMembershipWitness,
196
172
  });
197
173
 
@@ -217,25 +193,61 @@ export const insertSideEffectsAndBuildBaseRollupHints = runInSpan(
217
193
  },
218
194
  );
219
195
 
220
- export function getCivcProofFromTx(tx: Tx | ProcessedTx) {
196
+ export const insertSideEffects = runInSpan(
197
+ 'BlockBuilderHelpers',
198
+ 'buildBaseRollupHints',
199
+ async (span: Span, tx: ProcessedTx, db: MerkleTreeWriteOperations) => {
200
+ span.setAttribute(Attributes.TX_HASH, tx.hash.toString());
201
+
202
+ // Insert the note hashes. Padded with zeros to the max number of note hashes per tx.
203
+ const noteHashes = padArrayEnd(tx.txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX);
204
+ await db.appendLeaves(MerkleTreeId.NOTE_HASH_TREE, noteHashes);
205
+
206
+ // Insert the nullifiers. Padded with zeros to the max number of nullifiers per tx.
207
+ // Capturing the low nullifier info for each individual operation.
208
+ const nullifierInsertionResult = await db.batchInsert(
209
+ MerkleTreeId.NULLIFIER_TREE,
210
+ padArrayEnd(tx.txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(n => n.toBuffer()),
211
+ NULLIFIER_SUBTREE_HEIGHT,
212
+ );
213
+ if (nullifierInsertionResult.lowLeavesWitnessData === undefined) {
214
+ throw new Error(`Failed to batch insert nullifiers.`);
215
+ }
216
+
217
+ if (tx.txEffect.publicDataWrites.some(write => write.isEmpty())) {
218
+ throw new Error(`Empty public data write in tx: ${toFriendlyJSON(tx)}.`);
219
+ }
220
+ // Insert the public data writes sequentially. No need to pad them to the max array size.
221
+ // Capturing the low leaf info for each individual operation.
222
+ const publicDataInsertionResult = await db.sequentialInsert(
223
+ MerkleTreeId.PUBLIC_DATA_TREE,
224
+ tx.txEffect.publicDataWrites.map(write => write.toBuffer()),
225
+ );
226
+
227
+ return {
228
+ nullifierInsertionResult,
229
+ publicDataInsertionResult,
230
+ };
231
+ },
232
+ );
233
+
234
+ export function getChonkProofFromTx(tx: Tx | ProcessedTx) {
221
235
  const publicInputs = tx.data.publicInputs().toFields();
222
236
 
223
237
  const binaryProof = new Proof(
224
- Buffer.concat(
225
- tx.clientIvcProof.attachPublicInputs(publicInputs).fieldsWithPublicInputs.map(field => field.toBuffer()),
226
- ),
238
+ Buffer.concat(tx.chonkProof.attachPublicInputs(publicInputs).fieldsWithPublicInputs.map(field => field.toBuffer())),
227
239
  publicInputs.length,
228
240
  );
229
- return new RecursiveProof(tx.clientIvcProof.fields, binaryProof, true, CIVC_PROOF_LENGTH);
241
+ return new RecursiveProof(tx.chonkProof.fields, binaryProof, true, CHONK_PROOF_LENGTH);
230
242
  }
231
243
 
232
- export function getPublicTubePrivateInputsFromTx(tx: Tx | ProcessedTx, proverId: Fr) {
244
+ export function getPublicChonkVerifierPrivateInputsFromTx(tx: Tx | ProcessedTx, proverId: Fr) {
233
245
  const proofData = new ProofData(
234
246
  tx.data.toPrivateToPublicKernelCircuitPublicInputs(),
235
- getCivcProofFromTx(tx),
247
+ getChonkProofFromTx(tx),
236
248
  getVkData('HidingKernelToPublic'),
237
249
  );
238
- return new PublicTubePrivateInputs(proofData, proverId);
250
+ return new PublicChonkVerifierPrivateInputs(proofData, proverId);
239
251
  }
240
252
 
241
253
  // Build "hints" as the private inputs for the checkpoint root rollup circuit.
@@ -248,24 +260,15 @@ export const buildBlobHints = (blobFields: Fr[]) => {
248
260
  return { blobCommitments, blobs, blobsHash };
249
261
  };
250
262
 
251
- // Build the data required to prove the txs in an epoch. Currently only used in tests. It assumes 1 block per checkpoint.
252
- export const buildBlobDataFromTxs = async (txsPerCheckpoint: ProcessedTx[][]) => {
253
- const blobFields = txsPerCheckpoint.map(txs => getCheckpointBlobFields([txs.map(tx => tx.txEffect)]));
254
- const finalBlobChallenges = await buildFinalBlobChallenges(blobFields);
255
- return { blobFieldsLengths: blobFields.map(fields => fields.length), finalBlobChallenges };
256
- };
257
-
258
263
  export const buildFinalBlobChallenges = async (blobFieldsPerCheckpoint: Fr[][]) => {
259
- const blobs = blobFieldsPerCheckpoint.map(blobFields => getBlobsPerL1Block(blobFields));
260
- return await BatchedBlob.precomputeBatchedBlobChallenges(blobs);
264
+ return await BatchedBlobAccumulator.precomputeBatchedBlobChallenges(blobFieldsPerCheckpoint);
261
265
  };
262
266
 
263
267
  export const accumulateBlobs = runInSpan(
264
268
  'BlockBuilderHelpers',
265
269
  'accumulateBlobs',
266
270
  async (_span: Span, blobFields: Fr[], startBlobAccumulator: BatchedBlobAccumulator) => {
267
- const blobs = getBlobsPerL1Block(blobFields);
268
- const endBlobAccumulator = await startBlobAccumulator.accumulateBlobs(blobs);
271
+ const endBlobAccumulator = await startBlobAccumulator.accumulateFields(blobFields);
269
272
  return endBlobAccumulator;
270
273
  },
271
274
  );
@@ -278,7 +281,7 @@ export const buildHeaderFromCircuitOutputs = runInSpan(
278
281
  const globalVariables = GlobalVariables.from({
279
282
  chainId: constants.chainId,
280
283
  version: constants.version,
281
- blockNumber: blockRootRollupOutput.previousArchive.nextAvailableLeafIndex,
284
+ blockNumber: BlockNumber(blockRootRollupOutput.previousArchive.nextAvailableLeafIndex),
282
285
  timestamp: blockRootRollupOutput.endTimestamp,
283
286
  slotNumber: constants.slotNumber,
284
287
  coinbase: constants.coinbase,
@@ -305,103 +308,60 @@ export const buildHeaderAndBodyFromTxs = runInSpan(
305
308
  async (
306
309
  span,
307
310
  txs: ProcessedTx[],
311
+ lastArchive: AppendOnlyTreeSnapshot,
312
+ endState: StateReference,
308
313
  globalVariables: GlobalVariables,
309
- l1ToL2Messages: Fr[],
310
- db: MerkleTreeReadOperations,
311
- startSpongeBlob?: SpongeBlob,
314
+ startSpongeBlob: SpongeBlob,
315
+ isFirstBlock: boolean,
312
316
  ) => {
313
317
  span.setAttribute(Attributes.BLOCK_NUMBER, globalVariables.blockNumber);
314
- const stateReference = new StateReference(
315
- await getTreeSnapshot(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, db),
316
- new PartialStateReference(
317
- await getTreeSnapshot(MerkleTreeId.NOTE_HASH_TREE, db),
318
- await getTreeSnapshot(MerkleTreeId.NULLIFIER_TREE, db),
319
- await getTreeSnapshot(MerkleTreeId.PUBLIC_DATA_TREE, db),
320
- ),
321
- );
322
-
323
- const previousArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, db);
324
318
 
325
319
  const txEffects = txs.map(tx => tx.txEffect);
326
320
  const body = new Body(txEffects);
327
321
 
328
- const txOutHashes = txEffects.map(tx => tx.txOutHash());
329
- const outHash = txOutHashes.length === 0 ? Fr.ZERO : new Fr(computeCompressedUnbalancedMerkleTreeRoot(txOutHashes));
330
-
331
- const parityShaRoot = await computeInHashFromL1ToL2Messages(l1ToL2Messages);
332
- const blockBlobFields = body.toBlobFields();
333
- // TODO(#17027): This only works when there's one block per checkpoint.
334
- const blobFields = [new Fr(blockBlobFields.length + 1)].concat(blockBlobFields);
335
- const blobsHash = computeBlobsHashFromBlobs(getBlobsPerL1Block(blobFields));
336
-
337
- const contentCommitment = new ContentCommitment(blobsHash, parityShaRoot, outHash);
338
-
339
- const fees = txEffects.reduce((acc, tx) => acc.add(tx.transactionFee), Fr.ZERO);
340
- const manaUsed = txs.reduce((acc, tx) => acc.add(new Fr(tx.gasUsed.billedGas.l2Gas)), Fr.ZERO);
322
+ const totalFees = txEffects.reduce((acc, tx) => acc.add(tx.transactionFee), Fr.ZERO);
323
+ const totalManaUsed = txs.reduce((acc, tx) => acc.add(new Fr(tx.gasUsed.billedGas.l2Gas)), Fr.ZERO);
324
+
325
+ const { l1ToL2MessageTree, partial } = endState;
326
+
327
+ const blockBlobFields = encodeBlockBlobData({
328
+ blockEndMarker: {
329
+ timestamp: globalVariables.timestamp,
330
+ blockNumber: globalVariables.blockNumber,
331
+ numTxs: txs.length,
332
+ },
333
+ blockEndStateField: {
334
+ l1ToL2MessageNextAvailableLeafIndex: l1ToL2MessageTree.nextAvailableLeafIndex,
335
+ noteHashNextAvailableLeafIndex: partial.noteHashTree.nextAvailableLeafIndex,
336
+ nullifierNextAvailableLeafIndex: partial.nullifierTree.nextAvailableLeafIndex,
337
+ publicDataNextAvailableLeafIndex: partial.publicDataTree.nextAvailableLeafIndex,
338
+ totalManaUsed: totalManaUsed.toBigInt(),
339
+ },
340
+ lastArchiveRoot: lastArchive.root,
341
+ noteHashRoot: partial.noteHashTree.root,
342
+ nullifierRoot: partial.nullifierTree.root,
343
+ publicDataRoot: partial.publicDataTree.root,
344
+ l1ToL2MessageRoot: isFirstBlock ? l1ToL2MessageTree.root : undefined,
345
+ txs: body.toTxBlobData(),
346
+ });
341
347
 
342
- const endSpongeBlob = startSpongeBlob?.clone() ?? (await SpongeBlob.init(blobFields.length));
348
+ const endSpongeBlob = startSpongeBlob.clone();
343
349
  await endSpongeBlob.absorb(blockBlobFields);
344
350
  const spongeBlobHash = await endSpongeBlob.squeeze();
345
351
 
346
- const header = new L2BlockHeader(
347
- previousArchive,
348
- contentCommitment,
349
- stateReference,
350
- globalVariables,
351
- fees,
352
- manaUsed,
352
+ const header = BlockHeader.from({
353
+ lastArchive,
354
+ state: endState,
353
355
  spongeBlobHash,
354
- );
355
-
356
- return { header, body };
357
- },
358
- );
359
-
360
- export const buildBlockHeaderFromTxs = runInSpan(
361
- 'BlockBuilderHelpers',
362
- 'buildBlockHeaderFromTxs',
363
- async (
364
- span,
365
- txs: ProcessedTx[],
366
- globalVariables: GlobalVariables,
367
- startSpongeBlob: SpongeBlob,
368
- db: MerkleTreeReadOperations,
369
- ) => {
370
- span.setAttribute(Attributes.BLOCK_NUMBER, globalVariables.blockNumber);
371
- const stateReference = new StateReference(
372
- await getTreeSnapshot(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, db),
373
- new PartialStateReference(
374
- await getTreeSnapshot(MerkleTreeId.NOTE_HASH_TREE, db),
375
- await getTreeSnapshot(MerkleTreeId.NULLIFIER_TREE, db),
376
- await getTreeSnapshot(MerkleTreeId.PUBLIC_DATA_TREE, db),
377
- ),
378
- );
379
-
380
- const previousArchive = await getTreeSnapshot(MerkleTreeId.ARCHIVE, db);
381
-
382
- const blobFields = getBlockBlobFields(txs.map(tx => tx.txEffect));
383
- const endSpongeBlob = startSpongeBlob.clone();
384
- await endSpongeBlob.absorb(blobFields);
385
- const spongeBlobHash = await endSpongeBlob.squeeze();
386
-
387
- const txEffects = txs.map(tx => tx.txEffect);
388
- const fees = txEffects.reduce((acc, tx) => acc.add(tx.transactionFee), Fr.ZERO);
389
- const manaUsed = txs.reduce((acc, tx) => acc.add(new Fr(tx.gasUsed.billedGas.l2Gas)), Fr.ZERO);
356
+ globalVariables,
357
+ totalFees,
358
+ totalManaUsed,
359
+ });
390
360
 
391
- return new BlockHeader(previousArchive, stateReference, spongeBlobHash, globalVariables, fees, manaUsed);
361
+ return { header, body, blockBlobFields };
392
362
  },
393
363
  );
394
364
 
395
- /** Computes the inHash for a block's ContentCommitment given its l1 to l2 messages. */
396
- export async function computeInHashFromL1ToL2Messages(unpaddedL1ToL2Messages: Fr[]): Promise<Fr> {
397
- const l1ToL2Messages = padArrayEnd<Fr, number>(unpaddedL1ToL2Messages, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);
398
- const hasher = (left: Buffer, right: Buffer) =>
399
- Promise.resolve(sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>);
400
- const parityHeight = Math.ceil(Math.log2(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP));
401
- const parityCalculator = await MerkleTreeCalculator.create(parityHeight, Fr.ZERO.toBuffer(), hasher);
402
- return new Fr(await parityCalculator.computeTreeRoot(l1ToL2Messages.map(msg => msg.toBuffer())));
403
- }
404
-
405
365
  export async function getLastSiblingPath<TID extends MerkleTreeId>(treeId: TID, db: MerkleTreeReadOperations) {
406
366
  const { size } = await db.getTreeInfo(treeId);
407
367
  const path = await db.getSiblingPath(treeId, size - 1n);
@@ -427,48 +387,6 @@ export function makeEmptyMembershipWitness<N extends number>(height: N) {
427
387
  );
428
388
  }
429
389
 
430
- const processPublicDataUpdateRequests = runInSpan(
431
- 'BlockBuilderHelpers',
432
- 'processPublicDataUpdateRequests',
433
- async (span, tx: ProcessedTx, db: MerkleTreeWriteOperations) => {
434
- span.setAttribute(Attributes.TX_HASH, tx.hash.toString());
435
- const allPublicDataWrites = tx.txEffect.publicDataWrites.map(
436
- ({ leafSlot, value }) => new PublicDataTreeLeaf(leafSlot, value),
437
- );
438
-
439
- const { lowLeavesWitnessData, insertionWitnessData } = await db.sequentialInsert(
440
- MerkleTreeId.PUBLIC_DATA_TREE,
441
- allPublicDataWrites.map(write => {
442
- if (write.isEmpty()) {
443
- throw new Error(`Empty public data write in tx: ${toFriendlyJSON(tx)}`);
444
- }
445
- return write.toBuffer();
446
- }),
447
- );
448
-
449
- const lowPublicDataWritesPreimages = lowLeavesWitnessData.map(
450
- lowLeafWitness => lowLeafWitness.leafPreimage as PublicDataTreeLeafPreimage,
451
- );
452
- const lowPublicDataWritesMembershipWitnesses = lowLeavesWitnessData.map(lowLeafWitness =>
453
- MembershipWitness.fromBufferArray<typeof PUBLIC_DATA_TREE_HEIGHT>(
454
- lowLeafWitness.index,
455
- assertLength(lowLeafWitness.siblingPath.toBufferArray(), PUBLIC_DATA_TREE_HEIGHT),
456
- ),
457
- );
458
- const publicDataWritesSiblingPaths = insertionWitnessData.map(w => {
459
- const insertionSiblingPath = w.siblingPath.toFields();
460
- assertLength(insertionSiblingPath, PUBLIC_DATA_TREE_HEIGHT);
461
- return insertionSiblingPath as Tuple<Fr, typeof PUBLIC_DATA_TREE_HEIGHT>;
462
- });
463
-
464
- return {
465
- lowPublicDataWritesPreimages,
466
- lowPublicDataWritesMembershipWitnesses,
467
- publicDataWritesSiblingPaths,
468
- };
469
- },
470
- );
471
-
472
390
  export async function getSubtreeSiblingPath(
473
391
  treeId: MerkleTreeId,
474
392
  subtreeHeight: number,
@@ -1,4 +1,4 @@
1
- import type { SpongeBlob } from '@aztec/blob-lib';
1
+ import { type BlockBlobData, type BlockEndBlobData, type SpongeBlob, encodeBlockEndBlobData } from '@aztec/blob-lib';
2
2
  import {
3
3
  type ARCHIVE_HEIGHT,
4
4
  type L1_TO_L2_MSG_SUBTREE_ROOT_SIBLING_PATH_LENGTH,
@@ -6,7 +6,8 @@ import {
6
6
  type NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH,
7
7
  NUM_BASE_PARITY_PER_ROOT_PARITY,
8
8
  } from '@aztec/constants';
9
- import { Fr } from '@aztec/foundation/fields';
9
+ import { BlockNumber } from '@aztec/foundation/branded-types';
10
+ import { Fr } from '@aztec/foundation/curves/bn254';
10
11
  import { type Tuple, assertLength } from '@aztec/foundation/serialize';
11
12
  import { type TreeNodeLocation, UnbalancedTreeStore } from '@aztec/foundation/trees';
12
13
  import type { PublicInputsAndRecursiveProof } from '@aztec/stdlib/interfaces/server';
@@ -25,7 +26,7 @@ import {
25
26
  } from '@aztec/stdlib/rollup';
26
27
  import type { CircuitName } from '@aztec/stdlib/stats';
27
28
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
28
- import { type BlockHeader, GlobalVariables } from '@aztec/stdlib/tx';
29
+ import { BlockHeader, GlobalVariables, StateReference } from '@aztec/stdlib/tx';
29
30
  import type { UInt64 } from '@aztec/stdlib/types';
30
31
 
31
32
  import { buildHeaderFromCircuitOutputs, toProofData } from './block-building-helpers.js';
@@ -54,6 +55,7 @@ export class BlockProvingState {
54
55
  | ProofState<BlockRollupPublicInputs, typeof NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH>
55
56
  | undefined;
56
57
  private builtBlockHeader: BlockHeader | undefined;
58
+ private endState: StateReference | undefined;
57
59
  private endSpongeBlob: SpongeBlob | undefined;
58
60
  private txs: TxProvingState[] = [];
59
61
  private isFirstBlock: boolean;
@@ -61,7 +63,7 @@ export class BlockProvingState {
61
63
 
62
64
  constructor(
63
65
  public readonly index: number,
64
- public readonly blockNumber: number,
66
+ public readonly blockNumber: BlockNumber,
65
67
  public readonly totalNumTxs: number,
66
68
  private readonly constants: CheckpointConstantData,
67
69
  private readonly timestamp: UInt64,
@@ -200,30 +202,34 @@ export class BlockProvingState {
200
202
  return this.blockRootProof?.provingOutput?.inputs;
201
203
  }
202
204
 
203
- public setBuiltBlockHeader(blockHeader: BlockHeader) {
204
- this.builtBlockHeader = blockHeader;
205
- }
205
+ public async buildBlockHeader() {
206
+ if (this.isAcceptingTxs()) {
207
+ throw new Error('All txs must be added to the block before building the header.');
208
+ }
209
+ if (!this.endState) {
210
+ throw new Error('Call `setEndState` first.');
211
+ }
212
+ if (!this.endSpongeBlob) {
213
+ throw new Error('Call `setEndSpongeBlob` first.');
214
+ }
215
+
216
+ const endSpongeBlob = this.endSpongeBlob.clone();
217
+ const endSpongeBlobHash = await endSpongeBlob.squeeze();
218
+
219
+ this.builtBlockHeader = new BlockHeader(
220
+ this.lastArchiveTreeSnapshot,
221
+ this.endState,
222
+ endSpongeBlobHash,
223
+ this.#getGlobalVariables(),
224
+ this.#getTotalFees(),
225
+ new Fr(this.#getTotalManaUsed()),
226
+ );
206
227
 
207
- public getBuiltBlockHeader() {
208
228
  return this.builtBlockHeader;
209
229
  }
210
230
 
211
- public getGlobalVariables() {
212
- if (this.txs.length) {
213
- return this.txs[0].processedTx.globalVariables;
214
- }
215
-
216
- const constants = this.constants;
217
- return GlobalVariables.from({
218
- chainId: constants.chainId,
219
- version: constants.version,
220
- blockNumber: this.blockNumber,
221
- slotNumber: constants.slotNumber,
222
- timestamp: this.timestamp,
223
- coinbase: constants.coinbase,
224
- feeRecipient: constants.feeRecipient,
225
- gasFees: constants.gasFees,
226
- });
231
+ public getBuiltBlockHeader() {
232
+ return this.builtBlockHeader;
227
233
  }
228
234
 
229
235
  public getStartSpongeBlob() {
@@ -238,6 +244,52 @@ export class BlockProvingState {
238
244
  return this.endSpongeBlob;
239
245
  }
240
246
 
247
+ public setEndState(endState: StateReference) {
248
+ this.endState = endState;
249
+ }
250
+
251
+ public hasEndState() {
252
+ return !!this.endState;
253
+ }
254
+
255
+ public getBlockEndBlobFields(): Fr[] {
256
+ return encodeBlockEndBlobData(this.getBlockEndBlobData());
257
+ }
258
+
259
+ getBlockEndBlobData(): BlockEndBlobData {
260
+ if (!this.endState) {
261
+ throw new Error('Call `setEndState` first.');
262
+ }
263
+
264
+ const partial = this.endState.partial;
265
+ return {
266
+ blockEndMarker: {
267
+ numTxs: this.totalNumTxs,
268
+ timestamp: this.timestamp,
269
+ blockNumber: this.blockNumber,
270
+ },
271
+ blockEndStateField: {
272
+ l1ToL2MessageNextAvailableLeafIndex: this.newL1ToL2MessageTreeSnapshot.nextAvailableLeafIndex,
273
+ noteHashNextAvailableLeafIndex: partial.noteHashTree.nextAvailableLeafIndex,
274
+ nullifierNextAvailableLeafIndex: partial.nullifierTree.nextAvailableLeafIndex,
275
+ publicDataNextAvailableLeafIndex: partial.publicDataTree.nextAvailableLeafIndex,
276
+ totalManaUsed: this.#getTotalManaUsed(),
277
+ },
278
+ lastArchiveRoot: this.lastArchiveTreeSnapshot.root,
279
+ noteHashRoot: partial.noteHashTree.root,
280
+ nullifierRoot: partial.nullifierTree.root,
281
+ publicDataRoot: partial.publicDataTree.root,
282
+ l1ToL2MessageRoot: this.isFirstBlock ? this.newL1ToL2MessageTreeSnapshot.root : undefined,
283
+ };
284
+ }
285
+
286
+ public getBlockBlobData(): BlockBlobData {
287
+ return {
288
+ ...this.getBlockEndBlobData(),
289
+ txs: this.getTxEffects().map(t => t.toTxBlobData()),
290
+ };
291
+ }
292
+
241
293
  public getTxEffects() {
242
294
  return this.txs.map(t => t.processedTx.txEffect);
243
295
  }
@@ -295,7 +347,6 @@ export class BlockProvingState {
295
347
  this.lastArchiveTreeSnapshot,
296
348
  this.headerOfLastBlockInPreviousCheckpoint.state,
297
349
  this.constants,
298
- this.startSpongeBlob,
299
350
  this.timestamp,
300
351
  this.lastL1ToL2MessageSubtreeRootSiblingPath,
301
352
  this.lastArchiveSiblingPath,
@@ -392,4 +443,31 @@ export class BlockProvingState {
392
443
  ? [this.baseOrMergeProofs.getNode(rootLocation)?.provingOutput]
393
444
  : this.baseOrMergeProofs.getChildren(rootLocation).map(c => c?.provingOutput);
394
445
  }
446
+
447
+ #getGlobalVariables() {
448
+ if (this.txs.length) {
449
+ return this.txs[0].processedTx.globalVariables;
450
+ }
451
+
452
+ const constants = this.constants;
453
+
454
+ return GlobalVariables.from({
455
+ chainId: constants.chainId,
456
+ version: constants.version,
457
+ blockNumber: this.blockNumber,
458
+ slotNumber: constants.slotNumber,
459
+ timestamp: this.timestamp,
460
+ coinbase: constants.coinbase,
461
+ feeRecipient: constants.feeRecipient,
462
+ gasFees: constants.gasFees,
463
+ });
464
+ }
465
+
466
+ #getTotalFees() {
467
+ return this.txs.reduce((acc, tx) => acc.add(tx.processedTx.txEffect.transactionFee), Fr.ZERO);
468
+ }
469
+
470
+ #getTotalManaUsed() {
471
+ return this.txs.reduce((acc, tx) => acc + BigInt(tx.processedTx.gasUsed.billedGas.l2Gas), 0n);
472
+ }
395
473
  }