@aztec/stdlib 4.1.0-rc.2 → 4.1.0-rc.3

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 (91) hide show
  1. package/dest/abi/decoder.d.ts +2 -2
  2. package/dest/abi/decoder.d.ts.map +1 -1
  3. package/dest/abi/decoder.js +6 -1
  4. package/dest/abi/encoder.d.ts +1 -1
  5. package/dest/abi/encoder.d.ts.map +1 -1
  6. package/dest/abi/encoder.js +28 -1
  7. package/dest/abi/event_metadata_definition.d.ts +3 -1
  8. package/dest/abi/event_metadata_definition.d.ts.map +1 -1
  9. package/dest/abi/event_metadata_definition.js +1 -1
  10. package/dest/abi/utils.d.ts +14 -1
  11. package/dest/abi/utils.d.ts.map +1 -1
  12. package/dest/abi/utils.js +15 -0
  13. package/dest/block/l2_block_source.d.ts +7 -5
  14. package/dest/block/l2_block_source.d.ts.map +1 -1
  15. package/dest/epoch-helpers/index.d.ts +5 -1
  16. package/dest/epoch-helpers/index.d.ts.map +1 -1
  17. package/dest/epoch-helpers/index.js +4 -2
  18. package/dest/interfaces/archiver.js +2 -2
  19. package/dest/interfaces/aztec-node-admin.d.ts +4 -1
  20. package/dest/interfaces/aztec-node-admin.d.ts.map +1 -1
  21. package/dest/interfaces/aztec-node.d.ts +2 -2
  22. package/dest/interfaces/aztec-node.d.ts.map +1 -1
  23. package/dest/interfaces/aztec-node.js +2 -0
  24. package/dest/interfaces/block-builder.d.ts +11 -9
  25. package/dest/interfaces/block-builder.d.ts.map +1 -1
  26. package/dest/interfaces/block-builder.js +6 -4
  27. package/dest/interfaces/merkle_tree_operations.d.ts +9 -19
  28. package/dest/interfaces/merkle_tree_operations.d.ts.map +1 -1
  29. package/dest/interfaces/prover-client.d.ts +6 -1
  30. package/dest/interfaces/prover-client.d.ts.map +1 -1
  31. package/dest/interfaces/prover-client.js +7 -1
  32. package/dest/kernel/hints/build_note_hash_read_request_hints.d.ts +3 -3
  33. package/dest/kernel/hints/build_note_hash_read_request_hints.d.ts.map +1 -1
  34. package/dest/kernel/hints/build_note_hash_read_request_hints.js +13 -10
  35. package/dest/kernel/hints/build_nullifier_read_request_hints.d.ts +3 -3
  36. package/dest/kernel/hints/build_nullifier_read_request_hints.d.ts.map +1 -1
  37. package/dest/kernel/hints/build_nullifier_read_request_hints.js +13 -10
  38. package/dest/kernel/hints/build_transient_data_hints.d.ts +5 -2
  39. package/dest/kernel/hints/build_transient_data_hints.d.ts.map +1 -1
  40. package/dest/kernel/hints/build_transient_data_hints.js +9 -3
  41. package/dest/logs/log_filter.d.ts +4 -1
  42. package/dest/logs/log_filter.d.ts.map +1 -1
  43. package/dest/logs/log_filter.js +2 -1
  44. package/dest/logs/public_log.d.ts +4 -3
  45. package/dest/logs/public_log.d.ts.map +1 -1
  46. package/dest/logs/public_log.js +2 -1
  47. package/dest/messaging/l2_to_l1_membership.d.ts +32 -6
  48. package/dest/messaging/l2_to_l1_membership.d.ts.map +1 -1
  49. package/dest/messaging/l2_to_l1_membership.js +69 -26
  50. package/dest/p2p/checkpoint_proposal.d.ts +1 -1
  51. package/dest/p2p/checkpoint_proposal.d.ts.map +1 -1
  52. package/dest/p2p/checkpoint_proposal.js +13 -11
  53. package/dest/p2p/peer_error.d.ts +3 -1
  54. package/dest/p2p/peer_error.d.ts.map +1 -1
  55. package/dest/p2p/peer_error.js +5 -0
  56. package/dest/tx/tx_receipt.d.ts +7 -2
  57. package/dest/tx/tx_receipt.d.ts.map +1 -1
  58. package/dest/tx/tx_receipt.js +6 -3
  59. package/dest/update-checker/package_version.d.ts +2 -2
  60. package/dest/update-checker/package_version.d.ts.map +1 -1
  61. package/dest/update-checker/package_version.js +16 -3
  62. package/dest/validators/errors.d.ts +6 -1
  63. package/dest/validators/errors.d.ts.map +1 -1
  64. package/dest/validators/errors.js +7 -0
  65. package/dest/versioning/versioning.d.ts +4 -2
  66. package/dest/versioning/versioning.d.ts.map +1 -1
  67. package/dest/versioning/versioning.js +4 -1
  68. package/package.json +9 -9
  69. package/src/abi/decoder.ts +14 -2
  70. package/src/abi/encoder.ts +41 -1
  71. package/src/abi/event_metadata_definition.ts +2 -0
  72. package/src/abi/utils.ts +25 -0
  73. package/src/block/l2_block_source.ts +6 -4
  74. package/src/epoch-helpers/index.ts +3 -0
  75. package/src/interfaces/archiver.ts +2 -2
  76. package/src/interfaces/aztec-node.ts +9 -1
  77. package/src/interfaces/block-builder.ts +13 -11
  78. package/src/interfaces/merkle_tree_operations.ts +8 -18
  79. package/src/interfaces/prover-client.ts +8 -0
  80. package/src/kernel/hints/build_note_hash_read_request_hints.ts +14 -18
  81. package/src/kernel/hints/build_nullifier_read_request_hints.ts +15 -18
  82. package/src/kernel/hints/build_transient_data_hints.ts +17 -2
  83. package/src/logs/log_filter.ts +5 -0
  84. package/src/logs/public_log.ts +4 -2
  85. package/src/messaging/l2_to_l1_membership.ts +98 -33
  86. package/src/p2p/checkpoint_proposal.ts +23 -20
  87. package/src/p2p/peer_error.ts +7 -0
  88. package/src/tx/tx_receipt.ts +6 -1
  89. package/src/update-checker/package_version.ts +19 -6
  90. package/src/validators/errors.ts +9 -0
  91. package/src/versioning/versioning.ts +4 -1
@@ -3,6 +3,9 @@ import type { EpochNumber } from '@aztec/foundation/branded-types';
3
3
  import { Fr } from '@aztec/foundation/curves/bn254';
4
4
  import { SiblingPath, UnbalancedMerkleTreeCalculator, computeUnbalancedShaRoot } from '@aztec/foundation/trees';
5
5
 
6
+ import type { AztecNode } from '../interfaces/aztec-node.js';
7
+ import { TxHash } from '../tx/tx_hash.js';
8
+
6
9
  /**
7
10
  * # L2-to-L1 Message Tree Structure and Leaf IDs
8
11
  *
@@ -92,59 +95,94 @@ export function getL2ToL1MessageLeafId(
92
95
  return 2n ** BigInt(membershipWitness.siblingPath.pathSize) + membershipWitness.leafIndex;
93
96
  }
94
97
 
95
- export interface MessageRetrieval {
96
- getL2ToL1Messages(epoch: EpochNumber): Promise<Fr[][][][]>;
97
- }
98
-
99
98
  export type L2ToL1MembershipWitness = {
100
99
  root: Fr;
101
100
  leafIndex: bigint;
102
101
  siblingPath: SiblingPath<number>;
102
+ epochNumber: EpochNumber;
103
103
  };
104
104
 
105
+ /**
106
+ * Computes the L2 to L1 membership witness for a given message in a transaction.
107
+ *
108
+ * @param node - The Aztec node to query for block/tx/epoch data.
109
+ * @param message - The L2 to L1 message hash to prove membership of.
110
+ * @param txHash - The hash of the transaction that emitted the message.
111
+ * @param messageIndexInTx - Optional index of the message within the transaction's L2-to-L1 messages.
112
+ * If not provided, the message is found by scanning the tx's messages (throws if duplicates exist).
113
+ * @returns The membership witness and epoch number, or undefined if the tx is not yet in a block/epoch.
114
+ */
105
115
  export async function computeL2ToL1MembershipWitness(
106
- messageRetriever: MessageRetrieval,
107
- epoch: EpochNumber,
116
+ node: Pick<
117
+ AztecNode,
118
+ 'getL2ToL1Messages' | 'getTxReceipt' | 'getTxEffect' | 'getBlock' | 'getCheckpointsDataForEpoch'
119
+ >,
108
120
  message: Fr,
121
+ txHash: TxHash,
122
+ messageIndexInTx?: number,
109
123
  ): Promise<L2ToL1MembershipWitness | undefined> {
110
- const messagesInEpoch = await messageRetriever.getL2ToL1Messages(epoch);
111
- if (messagesInEpoch.length === 0) {
124
+ const { epochNumber, blockNumber } = await node.getTxReceipt(txHash);
125
+ if (epochNumber === undefined || blockNumber === undefined) {
126
+ return undefined;
127
+ }
128
+
129
+ const [messagesInEpoch, block, txEffect, checkpointsData] = await Promise.all([
130
+ node.getL2ToL1Messages(epochNumber),
131
+ node.getBlock(blockNumber),
132
+ node.getTxEffect(txHash),
133
+ node.getCheckpointsDataForEpoch(epochNumber),
134
+ ]);
135
+
136
+ if (messagesInEpoch.length === 0 || !block || !txEffect) {
137
+ return undefined;
138
+ }
139
+
140
+ const checkpointIndex = checkpointsData.findIndex(c => c.checkpointNumber === block.checkpointNumber);
141
+ if (checkpointIndex === -1) {
112
142
  return undefined;
113
143
  }
114
144
 
115
- return computeL2ToL1MembershipWitnessFromMessagesInEpoch(messagesInEpoch, message);
145
+ const blockIndex = block.indexWithinCheckpoint;
146
+ const txIndex = txEffect.txIndexInBlock;
147
+
148
+ const { root, leafIndex, siblingPath } = computeL2ToL1MembershipWitnessFromMessagesInEpoch(
149
+ messagesInEpoch,
150
+ message,
151
+ checkpointIndex,
152
+ blockIndex,
153
+ txIndex,
154
+ messageIndexInTx,
155
+ );
156
+ return { epochNumber, root, leafIndex, siblingPath };
116
157
  }
117
158
 
118
- // TODO: Allow to specify the message to consume by its index or by an offset, in case there are multiple messages with
119
- // the same value.
159
+ /**
160
+ * Computes a membership witness for a message in the epoch's L2-to-L1 message tree, given explicit position indices.
161
+ *
162
+ * @param messagesInEpoch - All L2-to-L1 messages in the epoch, organized as checkpoints → blocks → txs → messages.
163
+ * @param message - The message hash to prove membership of.
164
+ * @param checkpointIndex - Index of the checkpoint within the epoch's message array.
165
+ * @param blockIndex - Index of the block within the checkpoint.
166
+ * @param txIndex - Index of the transaction within the block.
167
+ * @param messageIndexInTx - Optional index of the message within the transaction's messages.
168
+ * If not provided, the message is found by scanning (throws if duplicates exist within the tx).
169
+ */
170
+ /** @internal Exported for testing only. */
120
171
  export function computeL2ToL1MembershipWitnessFromMessagesInEpoch(
121
172
  messagesInEpoch: Fr[][][][],
122
173
  message: Fr,
123
- ): L2ToL1MembershipWitness {
124
- // Find the index of the message in the tx, index of the tx in the block, and index of the block in the epoch.
125
- let messageIndexInTx = -1;
126
- let txIndex = -1;
127
- let blockIndex = -1;
128
- const checkpointIndex = messagesInEpoch.findIndex(messagesInCheckpoint => {
129
- blockIndex = messagesInCheckpoint.findIndex(messagesInBlock => {
130
- txIndex = messagesInBlock.findIndex(messagesInTx => {
131
- messageIndexInTx = messagesInTx.findIndex(msg => msg.equals(message));
132
- return messageIndexInTx !== -1;
133
- });
134
- return txIndex !== -1;
135
- });
136
- return blockIndex !== -1;
137
- });
138
-
139
- if (checkpointIndex === -1) {
140
- throw new Error('The L2ToL1Message you are trying to prove inclusion of does not exist');
141
- }
174
+ checkpointIndex: number,
175
+ blockIndex: number,
176
+ txIndex: number,
177
+ messageIndexInTx?: number,
178
+ ): { root: Fr; leafIndex: bigint; siblingPath: SiblingPath<number> } {
179
+ const messagesInTx = messagesInEpoch[checkpointIndex][blockIndex][txIndex];
180
+ const resolvedMessageIndex = resolveMessageIndex(messagesInTx, message, messageIndexInTx);
142
181
 
143
182
  // Build the tx tree.
144
- const messagesInTx = messagesInEpoch[checkpointIndex][blockIndex][txIndex];
145
183
  const txTree = UnbalancedMerkleTreeCalculator.create(messagesInTx.map(msg => msg.toBuffer()));
146
184
  // Get the sibling path of the target message in the tx tree.
147
- const pathToMessageInTxSubtree = txTree.getSiblingPathByLeafIndex(messageIndexInTx);
185
+ const pathToMessageInTxSubtree = txTree.getSiblingPathByLeafIndex(resolvedMessageIndex);
148
186
 
149
187
  // Build the tree of the block containing the target message.
150
188
  const blockTree = buildBlockTree(messagesInEpoch[checkpointIndex][blockIndex]);
@@ -189,7 +227,7 @@ export function computeL2ToL1MembershipWitnessFromMessagesInEpoch(
189
227
  // Compute the combined index.
190
228
  // It is the index of the message in the balanced tree (by filling up the wonky tree with empty nodes) at its current
191
229
  // height. It's used to validate the membership proof.
192
- const messageLeafPosition = txTree.getLeafLocation(messageIndexInTx);
230
+ const messageLeafPosition = txTree.getLeafLocation(resolvedMessageIndex);
193
231
  const txLeafPosition = blockTree.getLeafLocation(txIndex);
194
232
  const blockLeafPosition = checkpointTree.getLeafLocation(blockIndex);
195
233
  const checkpointLeafPosition = epochTree.getLeafLocation(checkpointIndex);
@@ -207,6 +245,33 @@ export function computeL2ToL1MembershipWitnessFromMessagesInEpoch(
207
245
  };
208
246
  }
209
247
 
248
+ function resolveMessageIndex(messagesInTx: Fr[], message: Fr, messageIndexInTx?: number): number {
249
+ if (messageIndexInTx !== undefined) {
250
+ if (!messagesInTx[messageIndexInTx]?.equals(message)) {
251
+ throw new Error(`Message at index ${messageIndexInTx} in tx does not match the expected message ${message}`);
252
+ }
253
+ return messageIndexInTx;
254
+ }
255
+
256
+ const indices = messagesInTx.reduce<number[]>((acc, msg, i) => {
257
+ if (msg.equals(message)) {
258
+ acc.push(i);
259
+ }
260
+ return acc;
261
+ }, []);
262
+
263
+ if (indices.length === 0) {
264
+ throw new Error('The L2ToL1Message you are trying to prove inclusion of does not exist');
265
+ }
266
+ if (indices.length > 1) {
267
+ throw new Error(
268
+ `Multiple messages with the same value ${message} found in tx (indices: ${indices.join(', ')}). ` +
269
+ `Provide messageIndexInTx to disambiguate.`,
270
+ );
271
+ }
272
+ return indices[0];
273
+ }
274
+
210
275
  function buildCheckpointTree(messagesInCheckpoint: Fr[][][]) {
211
276
  const blockOutHashes = messagesInCheckpoint.map(messagesInBlock => buildBlockTree(messagesInBlock).getRoot());
212
277
  return buildCompressedTree(blockOutHashes);
@@ -178,29 +178,32 @@ export class CheckpointProposal extends Gossipable {
178
178
  blockNumber: lastBlockInfo?.blockHeader?.globalVariables.blockNumber ?? BlockNumber(0),
179
179
  dutyType: DutyType.CHECKPOINT_PROPOSAL,
180
180
  };
181
- const checkpointSignature = await payloadSigner(checkpointHash, checkpointContext);
182
181
 
183
- if (!lastBlockInfo) {
184
- return new CheckpointProposal(checkpointHeader, archiveRoot, feeAssetPriceModifier, checkpointSignature);
182
+ if (lastBlockInfo) {
183
+ // Sign block proposal before signing checkpoint proposal to ensure HA protection
184
+ const lastBlockProposal = await BlockProposal.createProposalFromSigner(
185
+ lastBlockInfo.blockHeader,
186
+ lastBlockInfo.indexWithinCheckpoint,
187
+ checkpointHeader.inHash,
188
+ archiveRoot,
189
+ lastBlockInfo.txHashes,
190
+ lastBlockInfo.txs,
191
+ payloadSigner,
192
+ );
193
+
194
+ const checkpointSignature = await payloadSigner(checkpointHash, checkpointContext);
195
+
196
+ return new CheckpointProposal(checkpointHeader, archiveRoot, feeAssetPriceModifier, checkpointSignature, {
197
+ blockHeader: lastBlockInfo.blockHeader,
198
+ indexWithinCheckpoint: lastBlockInfo.indexWithinCheckpoint,
199
+ txHashes: lastBlockInfo.txHashes,
200
+ signature: lastBlockProposal.signature,
201
+ signedTxs: lastBlockProposal.signedTxs,
202
+ });
185
203
  }
186
204
 
187
- const lastBlockProposal = await BlockProposal.createProposalFromSigner(
188
- lastBlockInfo.blockHeader,
189
- lastBlockInfo.indexWithinCheckpoint,
190
- checkpointHeader.inHash,
191
- archiveRoot,
192
- lastBlockInfo.txHashes,
193
- lastBlockInfo.txs,
194
- payloadSigner,
195
- );
196
-
197
- return new CheckpointProposal(checkpointHeader, archiveRoot, feeAssetPriceModifier, checkpointSignature, {
198
- blockHeader: lastBlockInfo.blockHeader,
199
- indexWithinCheckpoint: lastBlockInfo.indexWithinCheckpoint,
200
- txHashes: lastBlockInfo.txHashes,
201
- signature: lastBlockProposal.signature,
202
- signedTxs: lastBlockProposal.signedTxs,
203
- });
205
+ const checkpointSignature = await payloadSigner(checkpointHash, checkpointContext);
206
+ return new CheckpointProposal(checkpointHeader, archiveRoot, feeAssetPriceModifier, checkpointSignature);
204
207
  }
205
208
 
206
209
  /**
@@ -15,3 +15,10 @@ export enum PeerErrorSeverity {
15
15
  */
16
16
  HighToleranceError = 'HighToleranceError',
17
17
  }
18
+
19
+ /** Severities ordered from mildest to harshest. */
20
+ export const PeerErrorSeverityByHarshness = [
21
+ PeerErrorSeverity.HighToleranceError,
22
+ PeerErrorSeverity.MidToleranceError,
23
+ PeerErrorSeverity.LowToleranceError,
24
+ ] as const;
@@ -1,4 +1,4 @@
1
- import { BlockNumber, BlockNumberSchema } from '@aztec/foundation/branded-types';
1
+ import { BlockNumber, BlockNumberSchema, EpochNumber, EpochNumberSchema } from '@aztec/foundation/branded-types';
2
2
 
3
3
  import { z } from 'zod';
4
4
 
@@ -58,6 +58,8 @@ export class TxReceipt {
58
58
  public blockHash?: BlockHash,
59
59
  /** The block number in which the transaction was included. */
60
60
  public blockNumber?: BlockNumber,
61
+ /** The epoch number in which the transaction was included. */
62
+ public epochNumber?: EpochNumber,
61
63
  /**
62
64
  * Debug logs collected during public function execution. Served only when the node is in test mode and placed on
63
65
  * the receipt only because it's a convenient place for it (the logs are printed out by the wallet when a mined
@@ -109,6 +111,7 @@ export class TxReceipt {
109
111
  error: z.string().optional(),
110
112
  blockHash: BlockHash.schema.optional(),
111
113
  blockNumber: BlockNumberSchema.optional(),
114
+ epochNumber: EpochNumberSchema.optional(),
112
115
  transactionFee: schemas.BigInt.optional(),
113
116
  debugLogs: z.array(DebugLog.schema).optional(),
114
117
  })
@@ -123,6 +126,7 @@ export class TxReceipt {
123
126
  transactionFee?: bigint;
124
127
  blockHash?: BlockHash;
125
128
  blockNumber?: BlockNumber;
129
+ epochNumber?: EpochNumber;
126
130
  debugLogs?: DebugLog[];
127
131
  }) {
128
132
  return new TxReceipt(
@@ -133,6 +137,7 @@ export class TxReceipt {
133
137
  fields.transactionFee,
134
138
  fields.blockHash,
135
139
  fields.blockNumber,
140
+ fields.epochNumber,
136
141
  fields.debugLogs,
137
142
  );
138
143
  }
@@ -3,15 +3,28 @@ import { fileURLToPath } from '@aztec/foundation/url';
3
3
  import { readFileSync } from 'fs';
4
4
  import { dirname, resolve } from 'path';
5
5
 
6
- /** Returns the package version from the release-please manifest, or undefined if not found. */
6
+ /** Returns the package version from the release-please manifest or the package.json, or undefined if not found. */
7
7
  export function getPackageVersion(): string | undefined {
8
+ const dir = dirname(fileURLToPath(import.meta.url));
9
+
10
+ // Try the release-please manifest first (works in dev/repo checkout).
8
11
  try {
9
- const releasePleaseManifestPath = resolve(
10
- dirname(fileURLToPath(import.meta.url)),
11
- '../../../../.release-please-manifest.json',
12
- );
12
+ const releasePleaseManifestPath = resolve(dir, '../../../../.release-please-manifest.json');
13
13
  return JSON.parse(readFileSync(releasePleaseManifestPath).toString())['.'];
14
14
  } catch {
15
- return undefined;
15
+ // Not in a repo checkout, fall through.
16
+ }
17
+
18
+ // Fall back to the stdlib package.json version (works in npm-installed packages).
19
+ try {
20
+ const packageJsonPath = resolve(dir, '../../package.json');
21
+ const version = JSON.parse(readFileSync(packageJsonPath).toString()).version;
22
+ if (version && version !== '0.1.0') {
23
+ return version;
24
+ }
25
+ } catch {
26
+ // No package.json found either.
16
27
  }
28
+
29
+ return undefined;
17
30
  }
@@ -36,6 +36,15 @@ export class FailedToReExecuteTransactionsError extends ValidatorError {
36
36
  }
37
37
  }
38
38
 
39
+ export class ReExInitialStateMismatchError extends ValidatorError {
40
+ constructor(
41
+ public readonly expectedArchiveRoot: Fr,
42
+ public readonly actualArchiveRoot: Fr,
43
+ ) {
44
+ super('Re-execution initial state mismatch');
45
+ }
46
+ }
47
+
39
48
  export class ReExStateMismatchError extends ValidatorError {
40
49
  constructor(
41
50
  public readonly expectedArchiveRoot: Fr,
@@ -115,7 +115,7 @@ export function validatePartialComponentVersionsMatch(
115
115
  }
116
116
 
117
117
  /** Returns a Koa middleware that injects the versioning info as headers. */
118
- export function getVersioningMiddleware(versions: Partial<ComponentsVersions>) {
118
+ export function getVersioningMiddleware(versions: Partial<ComponentsVersions>, opts?: { packageVersion?: string }) {
119
119
  return async (ctx: Koa.Context, next: () => Promise<void>) => {
120
120
  try {
121
121
  await next();
@@ -128,6 +128,9 @@ export function getVersioningMiddleware(versions: Partial<ComponentsVersions>) {
128
128
  ctx.set(`x-aztec-${key}`, value.toString());
129
129
  }
130
130
  }
131
+ if (opts?.packageVersion) {
132
+ ctx.set('x-aztec-packageVersion', opts.packageVersion);
133
+ }
131
134
  }
132
135
  };
133
136
  }