@aztec/archiver 0.0.1-commit.fcb71a6 → 0.0.1-commit.fffb133c

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 (192) hide show
  1. package/README.md +156 -22
  2. package/dest/archiver.d.ts +135 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +768 -0
  5. package/dest/{archiver/config.d.ts → config.d.ts} +9 -1
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/{archiver/config.js → config.js} +11 -2
  8. package/dest/errors.d.ts +41 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/{archiver/errors.js → errors.js} +8 -0
  11. package/dest/factory.d.ts +7 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +87 -8
  14. package/dest/index.d.ts +10 -4
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +8 -3
  17. package/dest/interfaces.d.ts +9 -0
  18. package/dest/interfaces.d.ts.map +1 -0
  19. package/dest/interfaces.js +3 -0
  20. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.d.ts +1 -1
  21. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  22. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.js +2 -2
  23. package/dest/{archiver/l1 → l1}/calldata_retriever.d.ts +3 -3
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/{archiver/l1 → l1}/calldata_retriever.js +2 -2
  26. package/dest/l1/data_retrieval.d.ts +88 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/{archiver/l1 → l1}/data_retrieval.js +35 -54
  29. package/dest/{archiver/l1 → l1}/debug_tx.d.ts +1 -1
  30. package/dest/l1/debug_tx.d.ts.map +1 -0
  31. package/dest/{archiver/l1 → l1}/spire_proposer.d.ts +1 -1
  32. package/dest/l1/spire_proposer.d.ts.map +1 -0
  33. package/dest/{archiver/l1 → l1}/trace_tx.d.ts +1 -1
  34. package/dest/l1/trace_tx.d.ts.map +1 -0
  35. package/dest/l1/types.d.ts +12 -0
  36. package/dest/l1/types.d.ts.map +1 -0
  37. package/dest/{archiver/l1 → l1}/validate_trace.d.ts +1 -1
  38. package/dest/l1/validate_trace.d.ts.map +1 -0
  39. package/dest/{archiver/l1 → l1}/validate_trace.js +1 -1
  40. package/dest/modules/data_source_base.d.ts +84 -0
  41. package/dest/modules/data_source_base.d.ts.map +1 -0
  42. package/dest/modules/data_source_base.js +260 -0
  43. package/dest/modules/data_store_updater.d.ts +73 -0
  44. package/dest/modules/data_store_updater.d.ts.map +1 -0
  45. package/dest/modules/data_store_updater.js +302 -0
  46. package/dest/modules/instrumentation.d.ts +37 -0
  47. package/dest/modules/instrumentation.d.ts.map +1 -0
  48. package/dest/{archiver → modules}/instrumentation.js +15 -63
  49. package/dest/modules/l1_synchronizer.d.ts +75 -0
  50. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  51. package/dest/modules/l1_synchronizer.js +1113 -0
  52. package/dest/modules/validation.d.ts +17 -0
  53. package/dest/modules/validation.d.ts.map +1 -0
  54. package/dest/{archiver → modules}/validation.js +7 -1
  55. package/dest/store/block_store.d.ts +192 -0
  56. package/dest/store/block_store.d.ts.map +1 -0
  57. package/dest/{archiver/kv_archiver_store → store}/block_store.js +162 -48
  58. package/dest/store/contract_class_store.d.ts +18 -0
  59. package/dest/store/contract_class_store.d.ts.map +1 -0
  60. package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +1 -1
  61. package/dest/store/contract_instance_store.d.ts +24 -0
  62. package/dest/store/contract_instance_store.d.ts.map +1 -0
  63. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +1 -1
  64. package/dest/store/kv_archiver_store.d.ts +340 -0
  65. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  66. package/dest/store/kv_archiver_store.js +447 -0
  67. package/dest/store/log_store.d.ts +54 -0
  68. package/dest/store/log_store.d.ts.map +1 -0
  69. package/dest/{archiver/kv_archiver_store → store}/log_store.js +89 -54
  70. package/dest/{archiver/kv_archiver_store → store}/message_store.d.ts +1 -1
  71. package/dest/store/message_store.d.ts.map +1 -0
  72. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  73. package/dest/structs/data_retrieval.d.ts.map +1 -0
  74. package/dest/structs/inbox_message.d.ts +15 -0
  75. package/dest/structs/inbox_message.d.ts.map +1 -0
  76. package/dest/{archiver/structs → structs}/published.d.ts +1 -1
  77. package/dest/structs/published.d.ts.map +1 -0
  78. package/dest/test/fake_l1_state.d.ts +190 -0
  79. package/dest/test/fake_l1_state.d.ts.map +1 -0
  80. package/dest/test/fake_l1_state.js +383 -0
  81. package/dest/test/index.d.ts +2 -1
  82. package/dest/test/index.d.ts.map +1 -1
  83. package/dest/test/index.js +1 -0
  84. package/dest/test/mock_archiver.d.ts +2 -2
  85. package/dest/test/mock_archiver.d.ts.map +1 -1
  86. package/dest/test/mock_archiver.js +1 -2
  87. package/dest/test/mock_l1_to_l2_message_source.d.ts +2 -2
  88. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  89. package/dest/test/mock_l1_to_l2_message_source.js +12 -3
  90. package/dest/test/mock_l2_block_source.d.ts +22 -15
  91. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  92. package/dest/test/mock_l2_block_source.js +163 -57
  93. package/dest/test/mock_structs.d.ts +76 -2
  94. package/dest/test/mock_structs.d.ts.map +1 -1
  95. package/dest/test/mock_structs.js +133 -2
  96. package/package.json +15 -17
  97. package/src/archiver.ts +523 -0
  98. package/src/{archiver/config.ts → config.ts} +13 -2
  99. package/src/{archiver/errors.ts → errors.ts} +12 -0
  100. package/src/factory.ts +122 -8
  101. package/src/index.ts +10 -3
  102. package/src/interfaces.ts +9 -0
  103. package/src/{archiver/l1 → l1}/bin/retrieve-calldata.ts +7 -2
  104. package/src/{archiver/l1 → l1}/calldata_retriever.ts +3 -3
  105. package/src/{archiver/l1 → l1}/data_retrieval.ts +56 -73
  106. package/src/{archiver/l1 → l1}/validate_trace.ts +1 -1
  107. package/src/modules/data_source_base.ts +367 -0
  108. package/src/modules/data_store_updater.ts +423 -0
  109. package/src/{archiver → modules}/instrumentation.ts +16 -65
  110. package/src/modules/l1_synchronizer.ts +931 -0
  111. package/src/{archiver → modules}/validation.ts +11 -6
  112. package/src/{archiver/kv_archiver_store → store}/block_store.ts +210 -66
  113. package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +1 -1
  114. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +1 -1
  115. package/src/{archiver/kv_archiver_store → store}/kv_archiver_store.ts +236 -35
  116. package/src/{archiver/kv_archiver_store → store}/log_store.ts +145 -86
  117. package/src/test/fake_l1_state.ts +599 -0
  118. package/src/test/index.ts +1 -0
  119. package/src/test/mock_archiver.ts +2 -2
  120. package/src/test/mock_l1_to_l2_message_source.ts +10 -4
  121. package/src/test/mock_l2_block_source.ts +173 -67
  122. package/src/test/mock_structs.ts +247 -2
  123. package/dest/archiver/archiver.d.ts +0 -304
  124. package/dest/archiver/archiver.d.ts.map +0 -1
  125. package/dest/archiver/archiver.js +0 -1645
  126. package/dest/archiver/archiver_store.d.ts +0 -308
  127. package/dest/archiver/archiver_store.d.ts.map +0 -1
  128. package/dest/archiver/archiver_store.js +0 -4
  129. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  130. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  131. package/dest/archiver/archiver_store_test_suite.js +0 -2770
  132. package/dest/archiver/config.d.ts.map +0 -1
  133. package/dest/archiver/errors.d.ts +0 -36
  134. package/dest/archiver/errors.d.ts.map +0 -1
  135. package/dest/archiver/index.d.ts +0 -7
  136. package/dest/archiver/index.d.ts.map +0 -1
  137. package/dest/archiver/index.js +0 -4
  138. package/dest/archiver/instrumentation.d.ts +0 -37
  139. package/dest/archiver/instrumentation.d.ts.map +0 -1
  140. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -157
  141. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  142. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  143. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  144. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  145. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  146. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -158
  147. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  148. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -313
  149. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -45
  150. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  151. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  152. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +0 -1
  153. package/dest/archiver/l1/calldata_retriever.d.ts.map +0 -1
  154. package/dest/archiver/l1/data_retrieval.d.ts +0 -90
  155. package/dest/archiver/l1/data_retrieval.d.ts.map +0 -1
  156. package/dest/archiver/l1/debug_tx.d.ts.map +0 -1
  157. package/dest/archiver/l1/spire_proposer.d.ts.map +0 -1
  158. package/dest/archiver/l1/trace_tx.d.ts.map +0 -1
  159. package/dest/archiver/l1/types.d.ts +0 -12
  160. package/dest/archiver/l1/types.d.ts.map +0 -1
  161. package/dest/archiver/l1/validate_trace.d.ts.map +0 -1
  162. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  163. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  164. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  165. package/dest/archiver/structs/published.d.ts.map +0 -1
  166. package/dest/archiver/validation.d.ts +0 -17
  167. package/dest/archiver/validation.d.ts.map +0 -1
  168. package/dest/rpc/index.d.ts +0 -9
  169. package/dest/rpc/index.d.ts.map +0 -1
  170. package/dest/rpc/index.js +0 -15
  171. package/src/archiver/archiver.ts +0 -2157
  172. package/src/archiver/archiver_store.ts +0 -372
  173. package/src/archiver/archiver_store_test_suite.ts +0 -2843
  174. package/src/archiver/index.ts +0 -6
  175. package/src/rpc/index.ts +0 -16
  176. /package/dest/{archiver/l1 → l1}/debug_tx.js +0 -0
  177. /package/dest/{archiver/l1 → l1}/spire_proposer.js +0 -0
  178. /package/dest/{archiver/l1 → l1}/trace_tx.js +0 -0
  179. /package/dest/{archiver/l1 → l1}/types.js +0 -0
  180. /package/dest/{archiver/kv_archiver_store → store}/message_store.js +0 -0
  181. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  182. /package/dest/{archiver/structs → structs}/inbox_message.js +0 -0
  183. /package/dest/{archiver/structs → structs}/published.js +0 -0
  184. /package/src/{archiver/l1 → l1}/README.md +0 -0
  185. /package/src/{archiver/l1 → l1}/debug_tx.ts +0 -0
  186. /package/src/{archiver/l1 → l1}/spire_proposer.ts +0 -0
  187. /package/src/{archiver/l1 → l1}/trace_tx.ts +0 -0
  188. /package/src/{archiver/l1 → l1}/types.ts +0 -0
  189. /package/src/{archiver/kv_archiver_store → store}/message_store.ts +0 -0
  190. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
  191. /package/src/{archiver/structs → structs}/inbox_message.ts +0 -0
  192. /package/src/{archiver/structs → structs}/published.ts +0 -0
@@ -0,0 +1,599 @@
1
+ import type { BlobClientInterface } from '@aztec/blob-client/client';
2
+ import { type Blob, getBlobsPerL1Block, getPrefixedEthBlobCommitments } from '@aztec/blob-lib';
3
+ import type { CheckpointProposedLog, InboxContract, MessageSentLog, RollupContract } from '@aztec/ethereum/contracts';
4
+ import { MULTI_CALL_3_ADDRESS } from '@aztec/ethereum/contracts';
5
+ import type { ViemPublicClient } from '@aztec/ethereum/types';
6
+ import { type BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
7
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
8
+ import { Secp256k1Signer } from '@aztec/foundation/crypto/secp256k1-signer';
9
+ import { Fr } from '@aztec/foundation/curves/bn254';
10
+ import { EthAddress } from '@aztec/foundation/eth-address';
11
+ import { createLogger } from '@aztec/foundation/log';
12
+ import { RollupAbi } from '@aztec/l1-artifacts';
13
+ import { CommitteeAttestation, CommitteeAttestationsAndSigners, L2Block } from '@aztec/stdlib/block';
14
+ import { Checkpoint } from '@aztec/stdlib/checkpoint';
15
+ import { getSlotAtTimestamp } from '@aztec/stdlib/epoch-helpers';
16
+ import { InboxLeaf } from '@aztec/stdlib/messaging';
17
+ import {
18
+ makeAndSignCommitteeAttestationsAndSigners,
19
+ makeCheckpointAttestationFromCheckpoint,
20
+ mockCheckpointAndMessages,
21
+ } from '@aztec/stdlib/testing';
22
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
23
+
24
+ import { type MockProxy, mock } from 'jest-mock-extended';
25
+ import { type FormattedBlock, type Transaction, encodeFunctionData, multicall3Abi, toHex } from 'viem';
26
+
27
+ import { updateRollingHash } from '../structs/inbox_message.js';
28
+
29
+ /** Configuration for the fake L1 state. */
30
+ export type FakeL1StateConfig = {
31
+ /** Genesis archive root. */
32
+ genesisArchiveRoot: Fr;
33
+ /** L1 start block number. */
34
+ l1StartBlock: bigint;
35
+ /** L1 genesis time in seconds. */
36
+ l1GenesisTime: bigint;
37
+ /** Ethereum slot duration in seconds. */
38
+ ethereumSlotDuration: number;
39
+ /** Rollup address for mock contracts. */
40
+ rollupAddress: EthAddress;
41
+ /** Inbox address for mock contracts. */
42
+ inboxAddress: EthAddress;
43
+ /** Aztec slot duration in seconds */
44
+ slotDuration: number;
45
+ };
46
+
47
+ /** Options for adding a checkpoint. */
48
+ type AddCheckpointOptions = {
49
+ /** L1 block number where checkpoint was proposed. */
50
+ l1BlockNumber: bigint;
51
+ /** Number of L2 blocks in the checkpoint. Default: 1 */
52
+ numBlocks?: number;
53
+ /** Or the actual blocks for the checkpoint */
54
+ blocks?: L2Block[];
55
+ /** Number of transactions per block. Default: 4 */
56
+ txsPerBlock?: number;
57
+ /** Max number of effects per tx (for generating large blobs). Default: undefined */
58
+ maxEffects?: number;
59
+ /** Signers for attestations. Default: none */
60
+ signers?: Secp256k1Signer[];
61
+ /** Override slot number. */
62
+ slotNumber?: SlotNumber;
63
+ /** Override previous archive. */
64
+ previousArchive?: AppendOnlyTreeSnapshot;
65
+ /** Timestamp for the checkpoint. */
66
+ timestamp?: bigint;
67
+ /** Number of L1-to-L2 messages. Default: 3 */
68
+ numL1ToL2Messages?: number;
69
+ /** L1 block number where messages were sent. Default: l1BlockNumber - 3 */
70
+ messagesL1BlockNumber?: bigint;
71
+ };
72
+
73
+ /** Result from adding a checkpoint. */
74
+ type AddCheckpointResult = {
75
+ /** The checkpoint that was created. */
76
+ checkpoint: Checkpoint;
77
+ /** The L1-to-L2 messages for this checkpoint. */
78
+ messages: Fr[];
79
+ };
80
+
81
+ /** Data stored for a checkpoint. */
82
+ type CheckpointData = {
83
+ checkpointNumber: CheckpointNumber;
84
+ checkpoint: Checkpoint;
85
+ l1BlockNumber: bigint;
86
+ tx: Transaction;
87
+ blobHashes: `0x${string}`[];
88
+ blobs: Blob[];
89
+ signers: Secp256k1Signer[];
90
+ /** If true, archiveAt will ignore it */
91
+ pruned?: boolean;
92
+ };
93
+
94
+ /** Data stored for a message. */
95
+ type MessageData = {
96
+ l1BlockNumber: bigint;
97
+ checkpointNumber: CheckpointNumber;
98
+ index: bigint;
99
+ leaf: Fr;
100
+ rollingHash: Buffer16;
101
+ };
102
+
103
+ /**
104
+ * Stateful fake for L1 data used by the archiver.
105
+ *
106
+ * This class simulates the L1 blockchain state that the archiver reads from:
107
+ * - RollupContract: status(), archiveAt(), getVersion(), getTargetCommitteeSize(), CheckpointProposed events
108
+ * - InboxContract: getState(), MessageSent events
109
+ * - PublicClient: getBlockNumber(), getBlock(), getTransaction()
110
+ * - BlobClient: getBlobSidecar()
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * const fake = new FakeL1State(config);
115
+ *
116
+ * // Add checkpoint (creates messages automatically)
117
+ * const { checkpoint, messages } = await fake.addCheckpoint(CheckpointNumber(1), { l1BlockNumber: 101n });
118
+ * fake.setL1BlockNumber(105n);
119
+ *
120
+ * // Status auto-updated
121
+ * expect(fake.getRollupStatus().pendingCheckpointNumber).toEqual(CheckpointNumber(1));
122
+ * ```
123
+ */
124
+ export class FakeL1State {
125
+ private readonly log = createLogger('archiver:test:fake-l1');
126
+ private l1BlockNumber: bigint;
127
+ private checkpoints: CheckpointData[] = [];
128
+ private messages: MessageData[] = [];
129
+ private messagesRollingHash: Buffer16 = Buffer16.ZERO;
130
+ private lastArchive: AppendOnlyTreeSnapshot;
131
+ private provenCheckpointNumber: CheckpointNumber = CheckpointNumber(0);
132
+ private targetCommitteeSize: number = 0;
133
+ private version: bigint = 1n;
134
+
135
+ // Computed from checkpoints based on L1 block visibility
136
+ private pendingCheckpointNumber: CheckpointNumber = CheckpointNumber(0);
137
+
138
+ constructor(private readonly config: FakeL1StateConfig) {
139
+ this.l1BlockNumber = config.l1StartBlock;
140
+ this.lastArchive = new AppendOnlyTreeSnapshot(config.genesisArchiveRoot, 1);
141
+ }
142
+
143
+ /**
144
+ * Adds messages for a checkpoint. Returns the message leaves.
145
+ * Auto-updates rolling hash.
146
+ *
147
+ * Note: For most use cases, use `addCheckpoint` which creates both checkpoint and messages.
148
+ * Use this method only when you need to add messages without creating a checkpoint (e.g., for reorg tests).
149
+ */
150
+ addMessages(checkpointNumber: CheckpointNumber, l1BlockNumber: bigint, messageLeaves: Fr[]): void {
151
+ messageLeaves.forEach((leaf, i) => {
152
+ const index = InboxLeaf.smallestIndexForCheckpoint(checkpointNumber) + BigInt(i);
153
+ this.messagesRollingHash = updateRollingHash(this.messagesRollingHash, leaf);
154
+
155
+ this.messages.push({
156
+ l1BlockNumber,
157
+ checkpointNumber,
158
+ index,
159
+ leaf,
160
+ rollingHash: this.messagesRollingHash,
161
+ });
162
+ });
163
+ }
164
+
165
+ /**
166
+ * Creates blocks for a checkpoint without adding them to L1 state.
167
+ * Useful for creating blocks to pass to addBlock() for testing provisional block handling.
168
+ * Returns the blocks directly.
169
+ */
170
+ public async makeBlocks(checkpointNumber: CheckpointNumber, options: Partial<AddCheckpointOptions>) {
171
+ return (await this.makeCheckpointAndMessages(checkpointNumber, options)).checkpoint.blocks;
172
+ }
173
+
174
+ /**
175
+ * Creates and adds a checkpoint with its L1-to-L2 messages.
176
+ * Returns both the checkpoint and the message leaves.
177
+ * Auto-chains from lastArchive, auto-updates pending status if L1 block >= checkpoint's L1 block.
178
+ */
179
+ public async addCheckpoint(
180
+ checkpointNumber: CheckpointNumber,
181
+ options: AddCheckpointOptions,
182
+ ): Promise<AddCheckpointResult> {
183
+ this.log.warn(`Adding checkpoint ${checkpointNumber}`);
184
+
185
+ const { l1BlockNumber, signers = [], messagesL1BlockNumber = l1BlockNumber - 3n } = options;
186
+
187
+ // Create the checkpoint using the stdlib helper
188
+ const { checkpoint, messages, lastArchive } = await this.makeCheckpointAndMessages(checkpointNumber, {
189
+ ...options,
190
+ numL1ToL2Messages: options.numL1ToL2Messages ?? 3,
191
+ });
192
+
193
+ // Store the messages internally so they match the checkpoint's inHash
194
+ this.addMessages(checkpointNumber, messagesL1BlockNumber, messages);
195
+
196
+ // Create the transaction and blobs
197
+ const tx = this.makeRollupTx(checkpoint, signers);
198
+ const blobHashes = this.makeVersionedBlobHashes(checkpoint);
199
+ const blobs = this.makeBlobsFromCheckpoint(checkpoint);
200
+
201
+ // Store the checkpoint data
202
+ this.checkpoints.push({
203
+ checkpointNumber,
204
+ checkpoint,
205
+ l1BlockNumber,
206
+ tx,
207
+ blobHashes,
208
+ blobs,
209
+ signers,
210
+ });
211
+
212
+ // Update last archive for auto-chaining
213
+ this.lastArchive = lastArchive ?? checkpoint.blocks.at(-1)!.archive;
214
+
215
+ // Auto-update pending checkpoint number
216
+ this.updatePendingCheckpointNumber();
217
+
218
+ return { checkpoint, messages };
219
+ }
220
+
221
+ /** Creates a checkpoint and messages without adding them to L1 state. */
222
+ private makeCheckpointAndMessages(checkpointNumber: CheckpointNumber, options: Partial<AddCheckpointOptions>) {
223
+ const {
224
+ numBlocks = 1,
225
+ txsPerBlock = 4,
226
+ maxEffects,
227
+ slotNumber,
228
+ previousArchive = this.lastArchive,
229
+ timestamp,
230
+ l1BlockNumber,
231
+ numL1ToL2Messages = 0,
232
+ blocks,
233
+ } = options;
234
+
235
+ return mockCheckpointAndMessages(checkpointNumber, {
236
+ startBlockNumber: this.getNextBlockNumber(checkpointNumber),
237
+ numBlocks,
238
+ blocks,
239
+ numTxsPerBlock: txsPerBlock,
240
+ numL1ToL2Messages,
241
+ previousArchive,
242
+ slotNumber: slotNumber ?? (l1BlockNumber !== undefined ? this.getL2SlotAtL1Block(l1BlockNumber) : undefined),
243
+ timestamp: timestamp ?? (l1BlockNumber !== undefined ? this.getTimestampAtL1Block(l1BlockNumber) : undefined),
244
+ ...(maxEffects !== undefined ? { maxEffects } : {}),
245
+ });
246
+ }
247
+
248
+ /** Returns the L2 slot at the given L1 block (assuming all L1 blocks are mined) */
249
+ public getL2SlotAtL1Block(l1BlockNumber: bigint): SlotNumber {
250
+ const timestamp = this.getTimestampAtL1Block(l1BlockNumber);
251
+ return getSlotAtTimestamp(timestamp, this.config);
252
+ }
253
+
254
+ /** Returns the timestamp at the given L1 block (assuming all L1 blocks are mined) */
255
+ public getTimestampAtL1Block(l1BlockNumber: bigint): bigint {
256
+ const { l1GenesisTime, l1StartBlock, ethereumSlotDuration } = this.config;
257
+ return l1GenesisTime + (l1BlockNumber - l1StartBlock) * BigInt(ethereumSlotDuration);
258
+ }
259
+
260
+ /**
261
+ * Sets the current L1 block number.
262
+ * Auto-updates pending checkpoint number based on visible checkpoints.
263
+ */
264
+ setL1BlockNumber(blockNumber: bigint): void {
265
+ this.l1BlockNumber = blockNumber;
266
+ this.updatePendingCheckpointNumber();
267
+ }
268
+
269
+ /** Marks a checkpoint as proven. Updates provenCheckpointNumber. */
270
+ markCheckpointAsProven(checkpointNumber: CheckpointNumber): void {
271
+ this.provenCheckpointNumber = checkpointNumber;
272
+ }
273
+
274
+ /** Sets the target committee size for attestation validation. */
275
+ setTargetCommitteeSize(size: number): void {
276
+ this.targetCommitteeSize = size;
277
+ }
278
+
279
+ /**
280
+ * Removes all entries for a checkpoint number (simulates L1 reorg or prune).
281
+ * Note: Does NOT remove messages for this checkpoint (use numL1ToL2Messages: 0 when re-adding).
282
+ * Auto-updates pending status.
283
+ */
284
+ removeCheckpoint(checkpointNumber: CheckpointNumber): void {
285
+ this.checkpoints = this.checkpoints.filter(cpData => cpData.checkpointNumber !== checkpointNumber);
286
+ this.updatePendingCheckpointNumber();
287
+ }
288
+
289
+ /**
290
+ * Removes messages after a given total index (simulates L1 reorg).
291
+ * Auto-updates rolling hash.
292
+ */
293
+ removeMessagesAfter(totalIndex: number): void {
294
+ this.messages = this.messages.slice(0, totalIndex);
295
+ // Recalculate rolling hash
296
+ this.messagesRollingHash = this.messages.reduce((hash, msg) => updateRollingHash(hash, msg.leaf), Buffer16.ZERO);
297
+ }
298
+
299
+ /**
300
+ * Simulates a pruned checkpoint by marking all entries with this number as pruned.
301
+ * The event will still be returned, but archiveAt() will return the previous checkpoint's archive
302
+ * (or genesis if no previous checkpoint), causing a mismatch that the archiver will detect.
303
+ */
304
+ markCheckpointAsPruned(checkpointNumber: CheckpointNumber): void {
305
+ for (const cpData of this.checkpoints) {
306
+ if (cpData.checkpointNumber === checkpointNumber) {
307
+ cpData.pruned = true;
308
+ }
309
+ }
310
+ this.updatePendingCheckpointNumber();
311
+ }
312
+
313
+ /**
314
+ * Moves messages at a given L1 block to a new L1 block.
315
+ * Useful for simulating partial message visibility (messages at higher L1 blocks won't be fetched).
316
+ */
317
+ moveMessagesToL1Block(fromL1Block: bigint, toL1Block: bigint): void {
318
+ this.messages.forEach(msg => {
319
+ if (msg.l1BlockNumber === fromL1Block) {
320
+ msg.l1BlockNumber = toL1Block;
321
+ }
322
+ });
323
+ }
324
+
325
+ /**
326
+ * Moves a specific message (by index in the messages array) to a new L1 block.
327
+ */
328
+ moveMessageAtIndexToL1Block(index: number, toL1Block: bigint): void {
329
+ if (this.messages[index]) {
330
+ this.messages[index].l1BlockNumber = toL1Block;
331
+ }
332
+ }
333
+
334
+ /** Gets current rollup status. */
335
+ getRollupStatus(): {
336
+ provenCheckpointNumber: CheckpointNumber;
337
+ pendingCheckpointNumber: CheckpointNumber;
338
+ provenArchive: Fr;
339
+ pendingArchive: Fr;
340
+ } {
341
+ return {
342
+ provenCheckpointNumber: this.provenCheckpointNumber,
343
+ pendingCheckpointNumber: this.pendingCheckpointNumber,
344
+ provenArchive: this.getArchiveAt(this.provenCheckpointNumber),
345
+ pendingArchive: this.getArchiveAt(this.pendingCheckpointNumber),
346
+ };
347
+ }
348
+
349
+ /** Gets the last archive (for manual chaining if needed). */
350
+ getLastArchive(): AppendOnlyTreeSnapshot {
351
+ return this.lastArchive;
352
+ }
353
+
354
+ /** Gets the latest checkpoint entry by number (returns the last added one). */
355
+ getCheckpoint(checkpointNumber: CheckpointNumber): Checkpoint | undefined {
356
+ return this.checkpoints.findLast(cpData => cpData.checkpointNumber === checkpointNumber)?.checkpoint;
357
+ }
358
+
359
+ /** Gets messages for a checkpoint. */
360
+ getMessages(checkpointNumber: CheckpointNumber): Fr[] {
361
+ return this.messages.filter(m => m.checkpointNumber === checkpointNumber).map(m => m.leaf);
362
+ }
363
+
364
+ /** Gets the blobs for a checkpoint. */
365
+ getCheckpointBlobs(checkpointNumber: CheckpointNumber): Blob[] {
366
+ return this.checkpoints.findLast(cpData => cpData.checkpointNumber === checkpointNumber)?.blobs ?? [];
367
+ }
368
+
369
+ /** Creates mock RollupContract that reads from this fake state. */
370
+ createMockRollupContract(_publicClient: MockProxy<ViemPublicClient>): MockProxy<RollupContract> {
371
+ const mockRollup = mock<RollupContract>();
372
+
373
+ mockRollup.getVersion.mockImplementation(() => Promise.resolve(this.version));
374
+ mockRollup.getTargetCommitteeSize.mockImplementation(() => Promise.resolve(this.targetCommitteeSize));
375
+ mockRollup.archiveAt.mockImplementation((cpNum: CheckpointNumber) => Promise.resolve(this.getArchiveAt(cpNum)));
376
+ mockRollup.status.mockImplementation((localCheckpointNum: CheckpointNumber) => {
377
+ const status = this.getRollupStatus();
378
+ return Promise.resolve({
379
+ provenCheckpointNumber: status.provenCheckpointNumber,
380
+ provenArchive: status.provenArchive,
381
+ pendingCheckpointNumber: status.pendingCheckpointNumber,
382
+ pendingArchive: status.pendingArchive,
383
+ archiveOfMyCheckpoint: this.getArchiveAt(localCheckpointNum),
384
+ });
385
+ });
386
+
387
+ // Mock the wrapper method for fetching checkpoint events
388
+ mockRollup.getCheckpointProposedEvents.mockImplementation((fromBlock: bigint, toBlock: bigint) =>
389
+ Promise.resolve(this.getCheckpointProposedLogs(fromBlock, toBlock)),
390
+ );
391
+
392
+ Object.defineProperty(mockRollup, 'address', { get: () => this.config.rollupAddress.toString() });
393
+
394
+ return mockRollup;
395
+ }
396
+
397
+ /** Creates mock InboxContract that reads from this fake state. */
398
+ createMockInboxContract(_publicClient: MockProxy<ViemPublicClient>): MockProxy<InboxContract> {
399
+ const mockInbox = mock<InboxContract>();
400
+
401
+ mockInbox.getState.mockImplementation(() =>
402
+ Promise.resolve({
403
+ messagesRollingHash: this.messagesRollingHash,
404
+ totalMessagesInserted: BigInt(this.messages.length),
405
+ treeInProgress: 0n,
406
+ }),
407
+ );
408
+
409
+ // Mock the wrapper methods for fetching message events
410
+ mockInbox.getMessageSentEvents.mockImplementation((fromBlock: bigint, toBlock: bigint) =>
411
+ Promise.resolve(this.getMessageSentLogs(fromBlock, toBlock)),
412
+ );
413
+
414
+ mockInbox.getMessageSentEventByHash.mockImplementation((hash: string, fromBlock: bigint, toBlock: bigint) =>
415
+ Promise.resolve(this.getMessageSentLogs(fromBlock, toBlock, hash)),
416
+ );
417
+
418
+ return mockInbox;
419
+ }
420
+
421
+ /** Creates mock PublicClient that reads from this fake state. */
422
+ createMockPublicClient(): MockProxy<ViemPublicClient> {
423
+ const publicClient = mock<ViemPublicClient>();
424
+
425
+ publicClient.getChainId.mockResolvedValue(1);
426
+ publicClient.getBlockNumber.mockImplementation(() => Promise.resolve(this.l1BlockNumber));
427
+
428
+ // Use async function pattern that existing test uses for getBlock
429
+
430
+ publicClient.getBlock.mockImplementation((async (args: { blockNumber?: bigint } = {}) => {
431
+ const blockNum = args.blockNumber ?? (await publicClient.getBlockNumber());
432
+ return {
433
+ number: blockNum,
434
+ timestamp: BigInt(blockNum) * BigInt(this.config.ethereumSlotDuration) + this.config.l1GenesisTime,
435
+ hash: Buffer32.fromBigInt(blockNum).toString(),
436
+ } as FormattedBlock;
437
+ }) as any);
438
+
439
+ // Use the same pattern as existing test for getTransaction
440
+ publicClient.getTransaction.mockImplementation((args: { hash?: `0x${string}` }) =>
441
+ Promise.resolve(
442
+ args.hash ? (this.checkpoints.find(cpData => cpData.tx.hash === args.hash)?.tx as any) : undefined,
443
+ ),
444
+ );
445
+
446
+ return publicClient;
447
+ }
448
+
449
+ /** Creates mock BlobClient that reads from this fake state. */
450
+ createMockBlobClient(): MockProxy<BlobClientInterface> {
451
+ const blobClient = mock<BlobClientInterface>();
452
+
453
+ // The blockId is the transaction's blockHash, which we set to the checkpoint's archive root
454
+ blobClient.getBlobSidecar.mockImplementation((blockId: `0x${string}`) =>
455
+ Promise.resolve(
456
+ this.checkpoints.find(cpData => cpData.checkpoint.archive.root.toString() === blockId)?.blobs ?? [],
457
+ ),
458
+ );
459
+
460
+ blobClient.testSources.mockResolvedValue(undefined);
461
+
462
+ return blobClient;
463
+ }
464
+
465
+ private updatePendingCheckpointNumber(): void {
466
+ this.pendingCheckpointNumber = this.checkpoints
467
+ .filter(cpData => cpData.l1BlockNumber <= this.l1BlockNumber && !cpData.pruned)
468
+ .reduce((max, cpData) => (cpData.checkpointNumber > max ? cpData.checkpointNumber : max), CheckpointNumber(0));
469
+ }
470
+
471
+ private getArchiveAt(checkpointNumber: CheckpointNumber): Fr {
472
+ if (checkpointNumber === 0) {
473
+ return this.config.genesisArchiveRoot;
474
+ }
475
+ // Find the latest non-pruned entry for this checkpoint number
476
+ const entries = this.checkpoints.filter(cpData => cpData.checkpointNumber === checkpointNumber);
477
+ const latestEntry = entries.at(-1);
478
+
479
+ if (!latestEntry || latestEntry.pruned) {
480
+ // For pruned or missing checkpoints, return the previous non-pruned checkpoint's archive
481
+ return this.getArchiveAt(CheckpointNumber(checkpointNumber - 1));
482
+ }
483
+ return latestEntry.checkpoint.archive.root;
484
+ }
485
+
486
+ private getNextBlockNumber(checkpointNumber: CheckpointNumber): BlockNumber {
487
+ const lastBlockNumber = this.checkpoints
488
+ .filter(cpData => cpData.checkpointNumber < checkpointNumber)
489
+ .flatMap(cpData => cpData.checkpoint.blocks.map(b => b.number))
490
+ .reduce((max, num) => Math.max(max, num), 0);
491
+ return (lastBlockNumber + 1) as BlockNumber;
492
+ }
493
+
494
+ private getCheckpointProposedLogs(fromBlock: bigint, toBlock: bigint): CheckpointProposedLog[] {
495
+ return this.checkpoints
496
+ .filter(cpData => cpData.l1BlockNumber >= fromBlock && cpData.l1BlockNumber <= toBlock)
497
+ .map(cpData => ({
498
+ l1BlockNumber: cpData.l1BlockNumber,
499
+ l1BlockHash: Buffer32.fromBigInt(cpData.l1BlockNumber),
500
+ l1TransactionHash: cpData.tx.hash as `0x${string}`,
501
+ args: {
502
+ checkpointNumber: cpData.checkpointNumber,
503
+ archive: cpData.checkpoint.archive.root,
504
+ versionedBlobHashes: cpData.blobHashes.map(h => Buffer.from(h.slice(2), 'hex')),
505
+ // These are intentionally undefined to skip hash validation in the archiver
506
+ // (validation is skipped when these fields are falsy)
507
+ payloadDigest: undefined,
508
+ attestationsHash: undefined,
509
+ },
510
+ }));
511
+ }
512
+
513
+ private getMessageSentLogs(fromBlock?: bigint, toBlock?: bigint, hashFilter?: string): MessageSentLog[] {
514
+ return this.messages
515
+ .filter(
516
+ msg =>
517
+ (!hashFilter || msg.leaf.toString() === hashFilter) &&
518
+ (!fromBlock || msg.l1BlockNumber >= fromBlock) &&
519
+ (!toBlock || msg.l1BlockNumber <= toBlock),
520
+ )
521
+ .map(msg => ({
522
+ l1BlockNumber: msg.l1BlockNumber,
523
+ l1BlockHash: Buffer32.fromBigInt(msg.l1BlockNumber),
524
+ l1TransactionHash: `0x${msg.l1BlockNumber.toString(16)}` as `0x${string}`,
525
+ args: {
526
+ checkpointNumber: msg.checkpointNumber,
527
+ index: msg.index,
528
+ leaf: msg.leaf,
529
+ rollingHash: msg.rollingHash,
530
+ },
531
+ }));
532
+ }
533
+
534
+ private makeRollupTx(checkpoint: Checkpoint, signers: Secp256k1Signer[]): Transaction {
535
+ const attestations = signers
536
+ .map(signer => makeCheckpointAttestationFromCheckpoint(checkpoint, signer))
537
+ .map(attestation => CommitteeAttestation.fromSignature(attestation.signature))
538
+ .map(committeeAttestation => committeeAttestation.toViem());
539
+
540
+ const header = checkpoint.header.toViem();
541
+ const blobInput = getPrefixedEthBlobCommitments(getBlobsPerL1Block(checkpoint.toBlobFields()));
542
+ const archive = toHex(checkpoint.archive.root.toBuffer());
543
+ const attestationsAndSigners = new CommitteeAttestationsAndSigners(
544
+ attestations.map(attestation => CommitteeAttestation.fromViem(attestation)),
545
+ );
546
+
547
+ const attestationsAndSignersSignature = makeAndSignCommitteeAttestationsAndSigners(
548
+ attestationsAndSigners,
549
+ signers[0],
550
+ );
551
+
552
+ const rollupInput = encodeFunctionData({
553
+ abi: RollupAbi,
554
+ functionName: 'propose',
555
+ args: [
556
+ {
557
+ header,
558
+ archive,
559
+ oracleInput: { feeAssetPriceModifier: 0n },
560
+ },
561
+ attestationsAndSigners.getPackedAttestations(),
562
+ attestationsAndSigners.getSigners().map(signer => signer.toString()),
563
+ attestationsAndSignersSignature.toViemSignature(),
564
+ blobInput,
565
+ ],
566
+ });
567
+
568
+ const multiCallInput = encodeFunctionData({
569
+ abi: multicall3Abi,
570
+ functionName: 'aggregate3',
571
+ args: [
572
+ [
573
+ {
574
+ target: this.config.rollupAddress.toString(),
575
+ callData: rollupInput,
576
+ allowFailure: false,
577
+ },
578
+ ],
579
+ ],
580
+ });
581
+
582
+ return {
583
+ input: multiCallInput,
584
+ hash: archive,
585
+ blockHash: archive,
586
+ to: MULTI_CALL_3_ADDRESS as `0x${string}`,
587
+ } as Transaction<bigint, number>;
588
+ }
589
+
590
+ private makeVersionedBlobHashes(checkpoint: Checkpoint): `0x${string}`[] {
591
+ return getBlobsPerL1Block(checkpoint.toBlobFields()).map(
592
+ b => `0x${b.getEthVersionedBlobHash().toString('hex')}` as `0x${string}`,
593
+ );
594
+ }
595
+
596
+ private makeBlobsFromCheckpoint(checkpoint: Checkpoint): Blob[] {
597
+ return getBlobsPerL1Block(checkpoint.toBlobFields());
598
+ }
599
+ }
package/src/test/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './mock_structs.js';
1
2
  export * from './mock_l2_block_source.js';
2
3
  export * from './mock_l1_to_l2_message_source.js';
3
4
  export * from './mock_archiver.js';
@@ -1,6 +1,6 @@
1
1
  import type { CheckpointNumber } from '@aztec/foundation/branded-types';
2
2
  import type { Fr } from '@aztec/foundation/curves/bn254';
3
- import { L2Block, type L2BlockSource } from '@aztec/stdlib/block';
3
+ import type { L2BlockSource } from '@aztec/stdlib/block';
4
4
  import type { Checkpoint } from '@aztec/stdlib/checkpoint';
5
5
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
6
6
 
@@ -57,7 +57,7 @@ export class MockPrefilledArchiver extends MockArchiver {
57
57
 
58
58
  const fromBlock = this.l2Blocks.length;
59
59
  // TODO: Add L2 blocks and checkpoints separately once archiver has the apis for that.
60
- this.addBlocks(this.prefilled.slice(fromBlock, fromBlock + numBlocks).map(c => L2Block.fromCheckpoint(c)));
60
+ this.addProposedBlocks(this.prefilled.slice(fromBlock, fromBlock + numBlocks).flatMap(c => c.blocks));
61
61
  return Promise.resolve();
62
62
  }
63
63
  }
@@ -1,6 +1,6 @@
1
- import { BlockNumber, type CheckpointNumber } from '@aztec/foundation/branded-types';
1
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
2
  import { Fr } from '@aztec/foundation/curves/bn254';
3
- import type { L2Tips } from '@aztec/stdlib/block';
3
+ import type { CheckpointId, L2BlockId, L2TipId, L2Tips } from '@aztec/stdlib/block';
4
4
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
5
5
 
6
6
  /**
@@ -33,9 +33,15 @@ export class MockL1ToL2MessageSource implements L1ToL2MessageSource {
33
33
 
34
34
  getL2Tips(): Promise<L2Tips> {
35
35
  const number = this.blockNumber;
36
- const tip = { number: BlockNumber(number), hash: new Fr(number).toString() };
36
+ const blockId: L2BlockId = { number: BlockNumber(number), hash: new Fr(number).toString() };
37
+ const checkpointId: CheckpointId = {
38
+ number: CheckpointNumber(number),
39
+ hash: new Fr(number + 1).toString(),
40
+ };
41
+ const tip: L2TipId = { block: blockId, checkpoint: checkpointId };
37
42
  return Promise.resolve({
38
- latest: tip,
43
+ proposed: blockId,
44
+ checkpointed: tip,
39
45
  proven: tip,
40
46
  finalized: tip,
41
47
  });