@aztec/archiver 0.0.1-commit.e588bc7e5 → 0.0.1-commit.e5a3663dd

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 (105) hide show
  1. package/dest/archiver.d.ts +19 -11
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +96 -53
  4. package/dest/config.d.ts +3 -1
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +14 -3
  7. package/dest/errors.d.ts +32 -5
  8. package/dest/errors.d.ts.map +1 -1
  9. package/dest/errors.js +51 -6
  10. package/dest/factory.d.ts +4 -4
  11. package/dest/factory.d.ts.map +1 -1
  12. package/dest/factory.js +13 -10
  13. package/dest/index.d.ts +10 -3
  14. package/dest/index.d.ts.map +1 -1
  15. package/dest/index.js +9 -2
  16. package/dest/l1/calldata_retriever.d.ts +2 -1
  17. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  18. package/dest/l1/calldata_retriever.js +9 -4
  19. package/dest/l1/data_retrieval.d.ts +18 -9
  20. package/dest/l1/data_retrieval.d.ts.map +1 -1
  21. package/dest/l1/data_retrieval.js +13 -19
  22. package/dest/l1/validate_historical_logs.d.ts +23 -0
  23. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  24. package/dest/l1/validate_historical_logs.js +108 -0
  25. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  26. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  27. package/dest/modules/contract_data_source_adapter.js +42 -0
  28. package/dest/modules/data_source_base.d.ts +16 -10
  29. package/dest/modules/data_source_base.d.ts.map +1 -1
  30. package/dest/modules/data_source_base.js +71 -60
  31. package/dest/modules/data_store_updater.d.ts +16 -9
  32. package/dest/modules/data_store_updater.d.ts.map +1 -1
  33. package/dest/modules/data_store_updater.js +52 -40
  34. package/dest/modules/instrumentation.d.ts +7 -2
  35. package/dest/modules/instrumentation.d.ts.map +1 -1
  36. package/dest/modules/instrumentation.js +22 -6
  37. package/dest/modules/l1_synchronizer.d.ts +8 -4
  38. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  39. package/dest/modules/l1_synchronizer.js +212 -79
  40. package/dest/modules/validation.d.ts +4 -3
  41. package/dest/modules/validation.d.ts.map +1 -1
  42. package/dest/modules/validation.js +4 -4
  43. package/dest/store/block_store.d.ts +60 -21
  44. package/dest/store/block_store.d.ts.map +1 -1
  45. package/dest/store/block_store.js +229 -70
  46. package/dest/store/contract_class_store.d.ts +17 -3
  47. package/dest/store/contract_class_store.d.ts.map +1 -1
  48. package/dest/store/contract_class_store.js +17 -1
  49. package/dest/store/contract_instance_store.d.ts +28 -1
  50. package/dest/store/contract_instance_store.d.ts.map +1 -1
  51. package/dest/store/contract_instance_store.js +31 -0
  52. package/dest/store/data_stores.d.ts +68 -0
  53. package/dest/store/data_stores.d.ts.map +1 -0
  54. package/dest/store/data_stores.js +50 -0
  55. package/dest/store/function_names_cache.d.ts +17 -0
  56. package/dest/store/function_names_cache.d.ts.map +1 -0
  57. package/dest/store/function_names_cache.js +30 -0
  58. package/dest/store/l2_tips_cache.d.ts +1 -1
  59. package/dest/store/l2_tips_cache.d.ts.map +1 -1
  60. package/dest/store/l2_tips_cache.js +3 -3
  61. package/dest/store/log_store.d.ts +1 -1
  62. package/dest/store/log_store.d.ts.map +1 -1
  63. package/dest/store/log_store.js +2 -4
  64. package/dest/store/message_store.d.ts +9 -3
  65. package/dest/store/message_store.d.ts.map +1 -1
  66. package/dest/store/message_store.js +31 -1
  67. package/dest/test/fake_l1_state.d.ts +14 -3
  68. package/dest/test/fake_l1_state.d.ts.map +1 -1
  69. package/dest/test/fake_l1_state.js +55 -15
  70. package/dest/test/mock_l2_block_source.d.ts +12 -3
  71. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  72. package/dest/test/mock_l2_block_source.js +24 -2
  73. package/dest/test/noop_l1_archiver.d.ts +4 -4
  74. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  75. package/dest/test/noop_l1_archiver.js +9 -7
  76. package/package.json +13 -13
  77. package/src/archiver.ts +113 -52
  78. package/src/config.ts +15 -1
  79. package/src/errors.ts +75 -8
  80. package/src/factory.ts +11 -10
  81. package/src/index.ts +17 -2
  82. package/src/l1/calldata_retriever.ts +15 -4
  83. package/src/l1/data_retrieval.ts +30 -35
  84. package/src/l1/validate_historical_logs.ts +140 -0
  85. package/src/modules/contract_data_source_adapter.ts +59 -0
  86. package/src/modules/data_source_base.ts +75 -57
  87. package/src/modules/data_store_updater.ts +71 -39
  88. package/src/modules/instrumentation.ts +27 -7
  89. package/src/modules/l1_synchronizer.ts +301 -83
  90. package/src/modules/validation.ts +8 -7
  91. package/src/store/block_store.ts +264 -77
  92. package/src/store/contract_class_store.ts +28 -2
  93. package/src/store/contract_instance_store.ts +43 -0
  94. package/src/store/data_stores.ts +108 -0
  95. package/src/store/function_names_cache.ts +37 -0
  96. package/src/store/l2_tips_cache.ts +9 -3
  97. package/src/store/log_store.ts +2 -5
  98. package/src/store/message_store.ts +35 -2
  99. package/src/test/fake_l1_state.ts +62 -24
  100. package/src/test/mock_l2_block_source.ts +23 -2
  101. package/src/test/noop_l1_archiver.ts +9 -7
  102. package/dest/store/kv_archiver_store.d.ts +0 -377
  103. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  104. package/dest/store/kv_archiver_store.js +0 -494
  105. package/src/store/kv_archiver_store.ts +0 -713
@@ -25,6 +25,49 @@ export class ContractInstanceStore {
25
25
  this.#contractInstanceUpdates = db.openMap('archiver_contract_instance_updates');
26
26
  }
27
27
 
28
+ /**
29
+ * Adds multiple contract instances to the store.
30
+ * @param data - Contract instances to add.
31
+ * @param blockNumber - L2 block number where the instances were deployed.
32
+ * @returns True if every insert succeeded.
33
+ */
34
+ async addContractInstances(data: ContractInstanceWithAddress[], blockNumber: number): Promise<boolean> {
35
+ return (await Promise.all(data.map(c => this.addContractInstance(c, blockNumber)))).every(Boolean);
36
+ }
37
+
38
+ /**
39
+ * Removes multiple contract instances from the store.
40
+ * @param data - Contract instances to delete.
41
+ * @returns True if every delete succeeded.
42
+ */
43
+ async deleteContractInstances(data: ContractInstanceWithAddress[]): Promise<boolean> {
44
+ return (await Promise.all(data.map(c => this.deleteContractInstance(c)))).every(Boolean);
45
+ }
46
+
47
+ /**
48
+ * Adds multiple contract instance updates to the store.
49
+ * @param data - Contract instance updates to add.
50
+ * @param timestamp - Timestamp at which the updates were scheduled.
51
+ * @returns True if every insert succeeded.
52
+ */
53
+ async addContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
54
+ return (
55
+ await Promise.all(data.map((update, logIndex) => this.addContractInstanceUpdate(update, timestamp, logIndex)))
56
+ ).every(Boolean);
57
+ }
58
+
59
+ /**
60
+ * Removes multiple contract instance updates from the store.
61
+ * @param data - Contract instance updates to delete.
62
+ * @param timestamp - Timestamp at which the updates were scheduled.
63
+ * @returns True if every delete succeeded.
64
+ */
65
+ async deleteContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
66
+ return (
67
+ await Promise.all(data.map((update, logIndex) => this.deleteContractInstanceUpdate(update, timestamp, logIndex)))
68
+ ).every(Boolean);
69
+ }
70
+
28
71
  addContractInstance(contractInstance: ContractInstanceWithAddress, blockNumber: number): Promise<void> {
29
72
  return this.db.transactionAsync(async () => {
30
73
  const key = contractInstance.address.toString();
@@ -0,0 +1,108 @@
1
+ import type { L1BlockId } from '@aztec/ethereum/l1-types';
2
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
3
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
4
+
5
+ import { join } from 'path';
6
+
7
+ import { ArchiverContractDataSourceAdapter } from '../modules/contract_data_source_adapter.js';
8
+ import { BlockStore } from './block_store.js';
9
+ import { ContractClassStore } from './contract_class_store.js';
10
+ import { ContractInstanceStore } from './contract_instance_store.js';
11
+ import { FunctionNamesCache } from './function_names_cache.js';
12
+ import { LogStore } from './log_store.js';
13
+ import { MessageStore } from './message_store.js';
14
+
15
+ export const ARCHIVER_DB_VERSION = 6;
16
+
17
+ /**
18
+ * Represents the latest L1 block processed by the archiver for various objects in L2.
19
+ */
20
+ export type ArchiverL1SynchPoint = {
21
+ /** Number of the last L1 block that added a new L2 checkpoint metadata. */
22
+ blocksSynchedTo?: bigint;
23
+ /** Last L1 block checked for L1 to L2 messages. */
24
+ messagesSynchedTo?: L1BlockId;
25
+ };
26
+
27
+ /**
28
+ * Bundle of archiver-owned LMDB substores plus the in-memory caches that span them.
29
+ *
30
+ * Replaces the former `KVArchiverDataStore` pass-through wrapper. Callers reach into
31
+ * the relevant substore directly (e.g. `stores.blocks.getBlock`) and use
32
+ * {@link createArchiverDataStores} to wire them up against a shared KV store.
33
+ */
34
+ export type ArchiverDataStores = {
35
+ /** The underlying key-value store. Use {@link AztecAsyncKVStore.transactionAsync} to compose updates atomically. */
36
+ db: AztecAsyncKVStore;
37
+ /** Blocks, checkpoints, tx effects, proven/finalized state. */
38
+ blocks: BlockStore;
39
+ /** Public, private and contract class logs. */
40
+ logs: LogStore;
41
+ /** L1 to L2 messages and message sync state. */
42
+ messages: MessageStore;
43
+ /** Contract classes (with bytecode commitments). */
44
+ contractClasses: ContractClassStore;
45
+ /** Contract instances and contract instance updates. */
46
+ contractInstances: ContractInstanceStore;
47
+ /** In-memory cache of public function selectors -> names. */
48
+ functionNames: FunctionNamesCache;
49
+ };
50
+
51
+ /** Options used by {@link createArchiverDataStores}. */
52
+ export type CreateArchiverDataStoresOptions = {
53
+ /** Maximum number of logs returned per page when paginating tagged log queries. */
54
+ logsMaxPageSize?: number;
55
+ };
56
+
57
+ /**
58
+ * Wires up the archiver substores against a shared KV store and returns the
59
+ * {@link ArchiverDataStores} bundle.
60
+ */
61
+ export function createArchiverDataStores(
62
+ db: AztecAsyncKVStore,
63
+ opts: CreateArchiverDataStoresOptions = {},
64
+ ): ArchiverDataStores {
65
+ const blocks = new BlockStore(db);
66
+ return {
67
+ db,
68
+ blocks,
69
+ logs: new LogStore(db, blocks, opts.logsMaxPageSize ?? 1000),
70
+ messages: new MessageStore(db),
71
+ contractClasses: new ContractClassStore(db),
72
+ contractInstances: new ContractInstanceStore(db),
73
+ functionNames: new FunctionNamesCache(),
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Returns the L1 sync point of the archiver, combining the block sync point from {@link BlockStore}
79
+ * and the message sync point from {@link MessageStore}.
80
+ */
81
+ export async function getArchiverSynchPoint(stores: ArchiverDataStores): Promise<ArchiverL1SynchPoint> {
82
+ const [blocksSynchedTo, messagesSynchedTo] = await Promise.all([
83
+ stores.blocks.getSynchedL1BlockNumber(),
84
+ stores.messages.getSynchedL1Block(),
85
+ ]);
86
+ return { blocksSynchedTo, messagesSynchedTo };
87
+ }
88
+
89
+ /**
90
+ * Backs up the underlying KV store to the given folder. Returns the path to the resulting db file.
91
+ */
92
+ export async function backupArchiverDataStores(
93
+ stores: ArchiverDataStores,
94
+ path: string,
95
+ compress = true,
96
+ ): Promise<string> {
97
+ await stores.db.backupTo(path, compress);
98
+ return join(path, 'data.mdb');
99
+ }
100
+
101
+ /**
102
+ * Returns a {@link ContractDataSource} adapter over {@link ArchiverDataStores}.
103
+ * Used by contexts (e.g. offline epoch re-prover tools) that need a ContractDataSource
104
+ * but do not need a full archiver instance.
105
+ */
106
+ export function createContractDataSource(stores: ArchiverDataStores): ContractDataSource {
107
+ return new ArchiverContractDataSourceAdapter(stores);
108
+ }
@@ -0,0 +1,37 @@
1
+ import { createLogger } from '@aztec/foundation/log';
2
+ import { FunctionSelector } from '@aztec/stdlib/abi';
3
+
4
+ const MAX_FUNCTION_SIGNATURES = 1000;
5
+ const MAX_FUNCTION_NAME_LEN = 256;
6
+
7
+ /**
8
+ * In-memory cache mapping public function selectors to function names.
9
+ *
10
+ * Populated opportunistically (e.g. by PXE registering signatures from artifacts) so the
11
+ * archiver can attach human-readable names to logs and traces. Bounded by
12
+ * {@link MAX_FUNCTION_SIGNATURES} to avoid unbounded growth from untrusted callers.
13
+ */
14
+ export class FunctionNamesCache {
15
+ private readonly log = createLogger('archiver:data-stores');
16
+ private readonly names: Map<string, string> = new Map();
17
+
18
+ /** Adds the given public function signatures to the cache. */
19
+ public async register(signatures: string[]): Promise<void> {
20
+ for (const sig of signatures) {
21
+ if (this.names.size > MAX_FUNCTION_SIGNATURES) {
22
+ return;
23
+ }
24
+ try {
25
+ const selector = await FunctionSelector.fromSignature(sig);
26
+ this.names.set(selector.toString(), sig.slice(0, sig.indexOf('(')).slice(0, MAX_FUNCTION_NAME_LEN));
27
+ } catch {
28
+ this.log.warn(`Failed to parse signature: ${sig}. Ignoring`);
29
+ }
30
+ }
31
+ }
32
+
33
+ /** Looks up a function name for the given selector, or returns undefined if not registered. */
34
+ public get(selector: FunctionSelector): string | undefined {
35
+ return this.names.get(selector.toString());
36
+ }
37
+ }
@@ -1,6 +1,12 @@
1
- import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
1
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
2
  import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
3
- import { type BlockData, type CheckpointId, GENESIS_CHECKPOINT_HEADER_HASH, type L2Tips } from '@aztec/stdlib/block';
3
+ import {
4
+ type BlockData,
5
+ type CheckpointId,
6
+ GENESIS_BLOCK_HEADER_HASH,
7
+ GENESIS_CHECKPOINT_HEADER_HASH,
8
+ type L2Tips,
9
+ } from '@aztec/stdlib/block';
4
10
 
5
11
  import type { BlockStore } from './block_store.js';
6
12
 
@@ -102,7 +108,7 @@ export class L2TipsCache {
102
108
  private async getCheckpointIdForProposedCheckpoint(
103
109
  checkpointedBlockData: Pick<BlockData, 'checkpointNumber'>,
104
110
  ): Promise<CheckpointId> {
105
- const checkpointData = await this.blockStore.getProposedCheckpointOnly();
111
+ const checkpointData = await this.blockStore.getLastProposedCheckpoint();
106
112
  if (!checkpointData) {
107
113
  return this.getCheckpointIdForBlock(checkpointedBlockData);
108
114
  }
@@ -1,7 +1,6 @@
1
1
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
2
  import { BlockNumber } from '@aztec/foundation/branded-types';
3
3
  import { compactArray, filterAsync } from '@aztec/foundation/collection';
4
- import { Fr } from '@aztec/foundation/curves/bn254';
5
4
  import { createLogger } from '@aztec/foundation/log';
6
5
  import { BufferReader, numToUInt32BE } from '@aztec/foundation/serialize';
7
6
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
@@ -302,13 +301,11 @@ export class LogStore {
302
301
  }
303
302
 
304
303
  #unpackBlockHash(reader: BufferReader): BlockHash {
305
- const blockHash = reader.remainingBytes() > 0 ? reader.readObject(Fr) : undefined;
306
-
307
- if (!blockHash) {
304
+ if (reader.remainingBytes() === 0) {
308
305
  throw new Error('Failed to read block hash from log entry buffer');
309
306
  }
310
307
 
311
- return new BlockHash(blockHash);
308
+ return BlockHash.fromBuffer(reader);
312
309
  }
313
310
 
314
311
  deleteLogs(blocks: L2Block[]): Promise<boolean> {
@@ -43,6 +43,8 @@ export class MessageStore {
43
43
  #totalMessageCount: AztecAsyncSingleton<bigint>;
44
44
  /** Stores the checkpoint number whose message tree is currently being filled on L1. */
45
45
  #inboxTreeInProgress: AztecAsyncSingleton<bigint>;
46
+ /** Stores the L1 finalized block as of the last successful message sync. */
47
+ #messagesFinalizedL1Block: AztecAsyncSingleton<Buffer>;
46
48
 
47
49
  #log = createLogger('archiver:message_store');
48
50
 
@@ -52,6 +54,7 @@ export class MessageStore {
52
54
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_id');
53
55
  this.#totalMessageCount = db.openSingleton('archiver_l1_to_l2_message_count');
54
56
  this.#inboxTreeInProgress = db.openSingleton('archiver_inbox_tree_in_progress');
57
+ this.#messagesFinalizedL1Block = db.openSingleton('archiver_messages_finalized_l1_block');
55
58
  }
56
59
 
57
60
  public async getTotalL1ToL2MessageCount(): Promise<bigint> {
@@ -75,6 +78,26 @@ export class MessageStore {
75
78
  await this.#lastSynchedL1Block.set(buffer);
76
79
  }
77
80
 
81
+ /** Gets the L1 finalized block as of the last successful message sync. */
82
+ public async getMessagesFinalizedL1Block(): Promise<L1BlockId | undefined> {
83
+ const buffer = await this.#messagesFinalizedL1Block.getAsync();
84
+ if (!buffer) {
85
+ return undefined;
86
+ }
87
+ const reader = BufferReader.asReader(buffer);
88
+ return { l1BlockNumber: reader.readUInt256(), l1BlockHash: Buffer32.fromBuffer(reader.readBytes(Buffer32.SIZE)) };
89
+ }
90
+
91
+ /** Monotonically advances the persisted L1 finalized block for message sync. Never regresses. */
92
+ private async maybeAdvanceFinalizedL1Block(l1Block: L1BlockId): Promise<void> {
93
+ const existing = await this.getMessagesFinalizedL1Block();
94
+ if (existing && l1Block.l1BlockNumber <= existing.l1BlockNumber) {
95
+ return;
96
+ }
97
+ const buffer = serializeToBuffer([l1Block.l1BlockNumber, l1Block.l1BlockHash]);
98
+ await this.#messagesFinalizedL1Block.set(buffer);
99
+ }
100
+
78
101
  /**
79
102
  * Append L1 to L2 messages to the store.
80
103
  * Requires new messages to be in order and strictly after the last message added.
@@ -185,8 +208,15 @@ export class MessageStore {
185
208
  return this.#inboxTreeInProgress.getAsync();
186
209
  }
187
210
 
188
- /** Atomically updates the message sync state: the L1 sync point and the inbox tree-in-progress marker. */
189
- public setMessageSyncState(l1Block: L1BlockId, treeInProgress: bigint | undefined): Promise<void> {
211
+ /**
212
+ * Atomically updates the message sync state: the L1 sync point, the inbox tree-in-progress marker, and
213
+ * (optionally) the L1 finalized block as of this sync. The finalized block is advanced monotonically.
214
+ */
215
+ public setMessageSyncState(
216
+ l1Block: L1BlockId,
217
+ treeInProgress: bigint | undefined,
218
+ finalizedL1Block?: L1BlockId,
219
+ ): Promise<void> {
190
220
  return this.db.transactionAsync(async () => {
191
221
  await this.setSynchedL1Block(l1Block);
192
222
  if (treeInProgress !== undefined) {
@@ -194,6 +224,9 @@ export class MessageStore {
194
224
  } else {
195
225
  await this.#inboxTreeInProgress.delete();
196
226
  }
227
+ if (finalizedL1Block !== undefined) {
228
+ await this.maybeAdvanceFinalizedL1Block(finalizedL1Block);
229
+ }
197
230
  });
198
231
  }
199
232
 
@@ -15,12 +15,8 @@ import { CommitteeAttestation, CommitteeAttestationsAndSigners, L2Block } from '
15
15
  import { Checkpoint } from '@aztec/stdlib/checkpoint';
16
16
  import { getSlotAtTimestamp } from '@aztec/stdlib/epoch-helpers';
17
17
  import { InboxLeaf } from '@aztec/stdlib/messaging';
18
- import { ConsensusPayload, SignatureDomainSeparator } from '@aztec/stdlib/p2p';
19
- import {
20
- makeAndSignCommitteeAttestationsAndSigners,
21
- makeCheckpointAttestationFromCheckpoint,
22
- mockCheckpointAndMessages,
23
- } from '@aztec/stdlib/testing';
18
+ import { ConsensusPayload, getHashedSignaturePayloadTypedData } from '@aztec/stdlib/p2p';
19
+ import { mockCheckpointAndMessages } from '@aztec/stdlib/testing';
24
20
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
25
21
 
26
22
  import { type MockProxy, mock } from 'jest-mock-extended';
@@ -151,8 +147,10 @@ export class FakeL1State {
151
147
  // Computed from checkpoints based on L1 block visibility
152
148
  private pendingCheckpointNumber: CheckpointNumber = CheckpointNumber(0);
153
149
 
154
- // The L1 block number reported as "finalized" (defaults to the start block)
155
- private finalizedL1BlockNumber: bigint;
150
+ // The L1 block number reported as "finalized" (defaults to the start block).
151
+ // `undefined` simulates the startup window on a fresh devnet where
152
+ // `getBlock({ blockTag: 'finalized' })` fails with "finalized block not found".
153
+ private finalizedL1BlockNumber: bigint | undefined;
156
154
 
157
155
  constructor(private readonly config: FakeL1StateConfig) {
158
156
  this.l1BlockNumber = config.l1StartBlock;
@@ -288,8 +286,11 @@ export class FakeL1State {
288
286
  this.updatePendingCheckpointNumber();
289
287
  }
290
288
 
291
- /** Sets the L1 block number that will be reported as "finalized". */
292
- setFinalizedL1BlockNumber(blockNumber: bigint): void {
289
+ /**
290
+ * Sets the L1 block number that will be reported as "finalized". Pass `undefined` to
291
+ * simulate a chain that does not yet have a finalized block (devnet startup).
292
+ */
293
+ setFinalizedL1BlockNumber(blockNumber: bigint | undefined): void {
293
294
  this.finalizedL1BlockNumber = blockNumber;
294
295
  }
295
296
 
@@ -332,6 +333,21 @@ export class FakeL1State {
332
333
  this.updatePendingCheckpointNumber();
333
334
  }
334
335
 
336
+ /**
337
+ * Moves a checkpoint to a different L1 block number (simulates L1 reorg that
338
+ * re-includes the same checkpoint transaction in a different block).
339
+ * The checkpoint content stays the same — only the L1 metadata changes.
340
+ * Auto-updates pending status.
341
+ */
342
+ moveCheckpointToL1Block(checkpointNumber: CheckpointNumber, newL1BlockNumber: bigint): void {
343
+ for (const cpData of this.checkpoints) {
344
+ if (cpData.checkpointNumber === checkpointNumber) {
345
+ cpData.l1BlockNumber = newL1BlockNumber;
346
+ }
347
+ }
348
+ this.updatePendingCheckpointNumber();
349
+ }
350
+
335
351
  /**
336
352
  * Removes messages after a given total index (simulates L1 reorg).
337
353
  * Auto-updates rolling hash.
@@ -487,8 +503,8 @@ export class FakeL1State {
487
503
  Promise.resolve(this.getMessageSentLogs(fromBlock, toBlock)),
488
504
  );
489
505
 
490
- mockInbox.getMessageSentEventByHash.mockImplementation((msgHash: string, l1BlockHash: string) =>
491
- Promise.resolve(this.getMessageSentLogByHash(msgHash, l1BlockHash) as MessageSentLog),
506
+ mockInbox.getMessageSentEventByHash.mockImplementation((msgHash: string, aroundL1BlockNumber: bigint) =>
507
+ Promise.resolve(this.getMessageSentLogByHash(msgHash, aroundL1BlockNumber) as MessageSentLog),
492
508
  );
493
509
 
494
510
  return mockInbox;
@@ -499,11 +515,19 @@ export class FakeL1State {
499
515
  const publicClient = mock<ViemPublicClient>();
500
516
 
501
517
  publicClient.getChainId.mockResolvedValue(1);
518
+ // Several consumers (CalldataRetriever, ArchiverL1Synchronizer) derive the EIP-712 signing
519
+ // context from `publicClient.chain.id`. Pin it so it matches `getSignatureContext()` below.
520
+ (publicClient as unknown as { chain: { id: number } }).chain = { id: 1 };
502
521
  publicClient.getBlockNumber.mockImplementation(() => Promise.resolve(this.l1BlockNumber));
503
522
 
504
523
  publicClient.getBlock.mockImplementation((async (args: { blockNumber?: bigint; blockTag?: string } = {}) => {
505
524
  let blockNum: bigint;
506
525
  if (args.blockTag === 'finalized') {
526
+ if (this.finalizedL1BlockNumber === undefined) {
527
+ throw Object.assign(new Error('finalized block not found'), {
528
+ details: 'finalized block not found',
529
+ });
530
+ }
507
531
  blockNum = this.finalizedL1BlockNumber;
508
532
  } else {
509
533
  blockNum = args.blockNumber ?? (await publicClient.getBlockNumber());
@@ -529,10 +553,10 @@ export class FakeL1State {
529
553
  createMockBlobClient(): MockProxy<BlobClientInterface> {
530
554
  const blobClient = mock<BlobClientInterface>();
531
555
 
532
- // The blockId is the transaction's blockHash, which we set to the checkpoint's archive root
556
+ // The blockId is the L1 block hash, which we derive from the L1 block number
533
557
  blobClient.getBlobSidecar.mockImplementation((blockId: `0x${string}`) =>
534
558
  Promise.resolve(
535
- this.checkpoints.find(cpData => cpData.checkpoint.archive.root.toString() === blockId)?.blobs ?? [],
559
+ this.checkpoints.find(cpData => Buffer32.fromBigInt(cpData.l1BlockNumber).toString() === blockId)?.blobs ?? [],
536
560
  ),
537
561
  );
538
562
 
@@ -608,9 +632,12 @@ export class FakeL1State {
608
632
  }));
609
633
  }
610
634
 
611
- private getMessageSentLogByHash(msgHash: string, l1BlockHash: string): MessageSentLog | undefined {
635
+ private getMessageSentLogByHash(msgHash: string, aroundL1BlockNumber: bigint): MessageSentLog | undefined {
612
636
  const msg = this.messages.find(
613
- msg => msg.leaf.toString() === msgHash && Buffer32.fromBigInt(msg.l1BlockNumber).toString() === l1BlockHash,
637
+ msg =>
638
+ msg.leaf.toString() === msgHash &&
639
+ msg.l1BlockNumber >= aroundL1BlockNumber - 5n &&
640
+ msg.l1BlockNumber <= aroundL1BlockNumber + 5n,
614
641
  );
615
642
  if (!msg) {
616
643
  return undefined;
@@ -632,9 +659,11 @@ export class FakeL1State {
632
659
  checkpoint: Checkpoint,
633
660
  signers: Secp256k1Signer[],
634
661
  ): Promise<{ tx: Transaction; attestationsHash: Buffer32; payloadDigest: Buffer32 }> {
662
+ const signatureContext = this.getSignatureContext();
663
+ const consensusPayload = ConsensusPayload.fromCheckpoint(checkpoint, signatureContext);
664
+ const attestationDigest = getHashedSignaturePayloadTypedData(consensusPayload);
635
665
  const attestations = signers
636
- .map(signer => makeCheckpointAttestationFromCheckpoint(checkpoint, signer))
637
- .map(attestation => CommitteeAttestation.fromSignature(attestation.signature))
666
+ .map(signer => CommitteeAttestation.fromSignature(signer.sign(attestationDigest)))
638
667
  .map(committeeAttestation => committeeAttestation.toViem());
639
668
 
640
669
  const header = checkpoint.header.toViem();
@@ -642,11 +671,15 @@ export class FakeL1State {
642
671
  const archive = toHex(checkpoint.archive.root.toBuffer());
643
672
  const attestationsAndSigners = new CommitteeAttestationsAndSigners(
644
673
  attestations.map(attestation => CommitteeAttestation.fromViem(attestation)),
674
+ signatureContext,
645
675
  );
646
676
 
647
- const attestationsAndSignersSignature = makeAndSignCommitteeAttestationsAndSigners(
648
- attestationsAndSigners,
649
- signers[0],
677
+ // Fall back to a random signer when no attesters are provided, so tests that
678
+ // don't care about the proposer identity (e.g. sync tests) still produce a
679
+ // valid-looking signature for the attestationsAndSigners struct.
680
+ const proposerSigner = signers[0] ?? Secp256k1Signer.random();
681
+ const attestationsAndSignersSignature = proposerSigner.sign(
682
+ getHashedSignaturePayloadTypedData(attestationsAndSigners),
650
683
  );
651
684
 
652
685
  const packedAttestations = attestationsAndSigners.getPackedAttestations();
@@ -687,9 +720,7 @@ export class FakeL1State {
687
720
  );
688
721
 
689
722
  // Compute payloadDigest (same logic as CalldataRetriever)
690
- const consensusPayload = ConsensusPayload.fromCheckpoint(checkpoint);
691
- const payloadToSign = consensusPayload.getPayloadToSign(SignatureDomainSeparator.checkpointAttestation);
692
- const payloadDigest = Buffer32.fromString(keccak256(payloadToSign));
723
+ const payloadDigest = getHashedSignaturePayloadTypedData(consensusPayload);
693
724
 
694
725
  const tx = {
695
726
  input: multiCallInput,
@@ -701,6 +732,13 @@ export class FakeL1State {
701
732
  return { tx, attestationsHash, payloadDigest };
702
733
  }
703
734
 
735
+ private getSignatureContext() {
736
+ return {
737
+ chainId: 1,
738
+ rollupAddress: this.config.rollupAddress,
739
+ };
740
+ }
741
+
704
742
  /** Extracts the CommitteeAttestations struct definition from RollupAbi for hash computation. */
705
743
  private getCommitteeAttestationsStructDef(): AbiParameter {
706
744
  const proposeFunction = RollupAbi.find(item => item.type === 'function' && item.name === 'propose') as
@@ -321,6 +321,26 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
321
321
  };
322
322
  }
323
323
 
324
+ public getCheckpointData(_n: CheckpointNumber): Promise<CheckpointData | undefined> {
325
+ return Promise.resolve(undefined);
326
+ }
327
+
328
+ public getCheckpointDataRange(_from: CheckpointNumber, _limit: number): Promise<CheckpointData[]> {
329
+ return Promise.resolve([]);
330
+ }
331
+
332
+ public getCheckpointNumberBySlot(_slot: SlotNumber): Promise<CheckpointNumber | undefined> {
333
+ return Promise.resolve(undefined);
334
+ }
335
+
336
+ public async getBlockDataWithCheckpointContext(number: BlockNumber) {
337
+ const data = await this.getBlockData(number);
338
+ if (!data) {
339
+ return undefined;
340
+ }
341
+ return { data, checkpoint: undefined, l1: undefined, attestations: [] };
342
+ }
343
+
324
344
  public async getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
325
345
  const block = this.l2Blocks.find(b => b.archive.root.equals(archive));
326
346
  if (!block) {
@@ -356,6 +376,7 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
356
376
  ),
357
377
  startBlock: checkpoint.blocks[0].number,
358
378
  blockCount: checkpoint.blocks.length,
379
+ feeAssetPriceModifier: checkpoint.feeAssetPriceModifier,
359
380
  attestations: [],
360
381
  l1: this.mockL1DataForCheckpoint(checkpoint),
361
382
  }),
@@ -558,11 +579,11 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
558
579
  return Promise.resolve({ valid: true });
559
580
  }
560
581
 
561
- getProposedCheckpoint(): Promise<ProposedCheckpointData | undefined> {
582
+ getLastCheckpoint(): Promise<ProposedCheckpointData | undefined> {
562
583
  return Promise.resolve(undefined);
563
584
  }
564
585
 
565
- getProposedCheckpointOnly(): Promise<ProposedCheckpointData | undefined> {
586
+ getLastProposedCheckpoint(): Promise<ProposedCheckpointData | undefined> {
566
587
  return Promise.resolve(undefined);
567
588
  }
568
589
 
@@ -16,7 +16,7 @@ import { EventEmitter } from 'node:events';
16
16
  import { Archiver } from '../archiver.js';
17
17
  import { ArchiverInstrumentation } from '../modules/instrumentation.js';
18
18
  import type { ArchiverL1Synchronizer } from '../modules/l1_synchronizer.js';
19
- import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
19
+ import type { ArchiverDataStores } from '../store/data_stores.js';
20
20
 
21
21
  /** Noop L1 synchronizer for testing without L1 connectivity. */
22
22
  class NoopL1Synchronizer implements FunctionsOf<ArchiverL1Synchronizer> {
@@ -48,7 +48,7 @@ class NoopL1Synchronizer implements FunctionsOf<ArchiverL1Synchronizer> {
48
48
  */
49
49
  export class NoopL1Archiver extends Archiver {
50
50
  constructor(
51
- dataStore: KVArchiverDataStore,
51
+ dataStores: ArchiverDataStores,
52
52
  l1Constants: L1RollupConstants & { genesisArchiveRoot: Fr },
53
53
  instrumentation: ArchiverInstrumentation,
54
54
  ) {
@@ -70,18 +70,20 @@ export class NoopL1Archiver extends Archiver {
70
70
  debugClient,
71
71
  rollup,
72
72
  {
73
+ rollupAddress: EthAddress.ZERO,
73
74
  registryAddress: EthAddress.ZERO,
75
+ inboxAddress: EthAddress.ZERO,
74
76
  governanceProposerAddress: EthAddress.ZERO,
75
- slashFactoryAddress: EthAddress.ZERO,
76
77
  slashingProposerAddress: EthAddress.ZERO,
77
78
  },
78
- dataStore,
79
+ dataStores,
79
80
  {
80
81
  pollingIntervalMs: 1000,
81
82
  batchSize: 100,
82
83
  skipValidateCheckpointAttestations: true,
83
84
  maxAllowedEthClientDriftSeconds: 300,
84
85
  ethereumAllowNoDebugHosts: true, // Skip trace validation
86
+ skipHistoricalLogsCheck: true, // Skip historical logs validation
85
87
  },
86
88
  blobClient,
87
89
  instrumentation,
@@ -106,10 +108,10 @@ export class NoopL1Archiver extends Archiver {
106
108
 
107
109
  /** Creates an archiver with mocked L1 connectivity for testing. */
108
110
  export async function createNoopL1Archiver(
109
- dataStore: KVArchiverDataStore,
111
+ dataStores: ArchiverDataStores,
110
112
  l1Constants: L1RollupConstants & { genesisArchiveRoot: Fr },
111
113
  telemetry: TelemetryClient = getTelemetryClient(),
112
114
  ): Promise<NoopL1Archiver> {
113
- const instrumentation = await ArchiverInstrumentation.new(telemetry, () => dataStore.estimateSize());
114
- return new NoopL1Archiver(dataStore, l1Constants, instrumentation);
115
+ const instrumentation = await ArchiverInstrumentation.new(telemetry, () => dataStores.db.estimateSize());
116
+ return new NoopL1Archiver(dataStores, l1Constants, instrumentation);
115
117
  }