@aztec/archiver 0.0.0-test.1 → 0.0.1-commit.03f7ef2

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 (151) hide show
  1. package/README.md +27 -6
  2. package/dest/archiver/archiver.d.ts +201 -94
  3. package/dest/archiver/archiver.d.ts.map +1 -1
  4. package/dest/archiver/archiver.js +1141 -396
  5. package/dest/archiver/archiver_store.d.ts +171 -83
  6. package/dest/archiver/archiver_store.d.ts.map +1 -1
  7. package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
  8. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  9. package/dest/archiver/archiver_store_test_suite.js +2389 -393
  10. package/dest/archiver/config.d.ts +7 -22
  11. package/dest/archiver/config.d.ts.map +1 -1
  12. package/dest/archiver/config.js +30 -14
  13. package/dest/archiver/errors.d.ts +33 -1
  14. package/dest/archiver/errors.d.ts.map +1 -1
  15. package/dest/archiver/errors.js +49 -0
  16. package/dest/archiver/index.d.ts +3 -4
  17. package/dest/archiver/index.d.ts.map +1 -1
  18. package/dest/archiver/index.js +1 -2
  19. package/dest/archiver/instrumentation.d.ts +14 -6
  20. package/dest/archiver/instrumentation.d.ts.map +1 -1
  21. package/dest/archiver/instrumentation.js +69 -17
  22. package/dest/archiver/kv_archiver_store/block_store.d.ts +91 -21
  23. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  24. package/dest/archiver/kv_archiver_store/block_store.js +476 -86
  25. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +4 -4
  26. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
  27. package/dest/archiver/kv_archiver_store/contract_class_store.js +13 -19
  28. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +12 -9
  29. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  30. package/dest/archiver/kv_archiver_store/contract_instance_store.js +30 -16
  31. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +80 -75
  32. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  33. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +142 -83
  34. package/dest/archiver/kv_archiver_store/log_store.d.ts +12 -16
  35. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  36. package/dest/archiver/kv_archiver_store/log_store.js +153 -113
  37. package/dest/archiver/kv_archiver_store/message_store.d.ts +25 -18
  38. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  39. package/dest/archiver/kv_archiver_store/message_store.js +152 -49
  40. package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
  41. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
  42. package/dest/archiver/l1/bin/retrieve-calldata.js +149 -0
  43. package/dest/archiver/l1/calldata_retriever.d.ts +112 -0
  44. package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
  45. package/dest/archiver/l1/calldata_retriever.js +471 -0
  46. package/dest/archiver/l1/data_retrieval.d.ts +90 -0
  47. package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
  48. package/dest/archiver/l1/data_retrieval.js +331 -0
  49. package/dest/archiver/l1/debug_tx.d.ts +19 -0
  50. package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
  51. package/dest/archiver/l1/debug_tx.js +73 -0
  52. package/dest/archiver/l1/spire_proposer.d.ts +70 -0
  53. package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
  54. package/dest/archiver/l1/spire_proposer.js +157 -0
  55. package/dest/archiver/l1/trace_tx.d.ts +97 -0
  56. package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
  57. package/dest/archiver/l1/trace_tx.js +91 -0
  58. package/dest/archiver/l1/types.d.ts +12 -0
  59. package/dest/archiver/l1/types.d.ts.map +1 -0
  60. package/dest/archiver/l1/types.js +3 -0
  61. package/dest/archiver/l1/validate_trace.d.ts +29 -0
  62. package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
  63. package/dest/archiver/l1/validate_trace.js +150 -0
  64. package/dest/archiver/structs/data_retrieval.d.ts +1 -1
  65. package/dest/archiver/structs/inbox_message.d.ts +15 -0
  66. package/dest/archiver/structs/inbox_message.d.ts.map +1 -0
  67. package/dest/archiver/structs/inbox_message.js +39 -0
  68. package/dest/archiver/structs/published.d.ts +2 -11
  69. package/dest/archiver/structs/published.d.ts.map +1 -1
  70. package/dest/archiver/structs/published.js +1 -1
  71. package/dest/archiver/validation.d.ts +17 -0
  72. package/dest/archiver/validation.d.ts.map +1 -0
  73. package/dest/archiver/validation.js +98 -0
  74. package/dest/factory.d.ts +9 -14
  75. package/dest/factory.d.ts.map +1 -1
  76. package/dest/factory.js +22 -52
  77. package/dest/index.d.ts +2 -2
  78. package/dest/index.d.ts.map +1 -1
  79. package/dest/index.js +1 -1
  80. package/dest/rpc/index.d.ts +2 -3
  81. package/dest/rpc/index.d.ts.map +1 -1
  82. package/dest/rpc/index.js +1 -4
  83. package/dest/test/index.d.ts +1 -1
  84. package/dest/test/mock_archiver.d.ts +16 -8
  85. package/dest/test/mock_archiver.d.ts.map +1 -1
  86. package/dest/test/mock_archiver.js +19 -14
  87. package/dest/test/mock_l1_to_l2_message_source.d.ts +9 -6
  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 +21 -7
  90. package/dest/test/mock_l2_block_source.d.ts +52 -13
  91. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  92. package/dest/test/mock_l2_block_source.js +140 -15
  93. package/dest/test/mock_structs.d.ts +10 -0
  94. package/dest/test/mock_structs.d.ts.map +1 -0
  95. package/dest/test/mock_structs.js +38 -0
  96. package/package.json +29 -30
  97. package/src/archiver/archiver.ts +1477 -501
  98. package/src/archiver/archiver_store.ts +197 -88
  99. package/src/archiver/archiver_store_test_suite.ts +2403 -350
  100. package/src/archiver/config.ts +38 -46
  101. package/src/archiver/errors.ts +85 -0
  102. package/src/archiver/index.ts +2 -3
  103. package/src/archiver/instrumentation.ts +91 -22
  104. package/src/archiver/kv_archiver_store/block_store.ts +640 -101
  105. package/src/archiver/kv_archiver_store/contract_class_store.ts +14 -24
  106. package/src/archiver/kv_archiver_store/contract_instance_store.ts +36 -28
  107. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +193 -113
  108. package/src/archiver/kv_archiver_store/log_store.ts +205 -127
  109. package/src/archiver/kv_archiver_store/message_store.ts +213 -54
  110. package/src/archiver/l1/README.md +98 -0
  111. package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
  112. package/src/archiver/l1/calldata_retriever.ts +641 -0
  113. package/src/archiver/l1/data_retrieval.ts +512 -0
  114. package/src/archiver/l1/debug_tx.ts +99 -0
  115. package/src/archiver/l1/spire_proposer.ts +160 -0
  116. package/src/archiver/l1/trace_tx.ts +128 -0
  117. package/src/archiver/l1/types.ts +13 -0
  118. package/src/archiver/l1/validate_trace.ts +211 -0
  119. package/src/archiver/structs/inbox_message.ts +41 -0
  120. package/src/archiver/structs/published.ts +1 -11
  121. package/src/archiver/validation.ts +124 -0
  122. package/src/factory.ts +28 -69
  123. package/src/index.ts +1 -1
  124. package/src/rpc/index.ts +1 -5
  125. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  126. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  127. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  128. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  129. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  130. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  131. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  132. package/src/test/mock_archiver.ts +22 -16
  133. package/src/test/mock_l1_to_l2_message_source.ts +20 -8
  134. package/src/test/mock_l2_block_source.ts +186 -21
  135. package/src/test/mock_structs.ts +50 -0
  136. package/dest/archiver/data_retrieval.d.ts +0 -74
  137. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  138. package/dest/archiver/data_retrieval.js +0 -283
  139. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +0 -12
  140. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +0 -1
  141. package/dest/archiver/kv_archiver_store/nullifier_store.js +0 -73
  142. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
  143. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
  144. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
  145. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +0 -175
  146. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +0 -1
  147. package/dest/archiver/memory_archiver_store/memory_archiver_store.js +0 -636
  148. package/src/archiver/data_retrieval.ts +0 -422
  149. package/src/archiver/kv_archiver_store/nullifier_store.ts +0 -97
  150. package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
  151. package/src/archiver/memory_archiver_store/memory_archiver_store.ts +0 -801
@@ -1,71 +1,173 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
2
- import { Fr } from '@aztec/foundation/fields';
1
+ import type { L1BlockId } from '@aztec/ethereum/l1-types';
2
+ import { CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
+ import { toArray } from '@aztec/foundation/iterable';
3
6
  import { createLogger } from '@aztec/foundation/log';
4
- import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton } from '@aztec/kv-store';
7
+ import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
8
+ import {
9
+ type AztecAsyncKVStore,
10
+ type AztecAsyncMap,
11
+ type AztecAsyncSingleton,
12
+ type CustomRange,
13
+ mapRange,
14
+ } from '@aztec/kv-store';
5
15
  import { InboxLeaf } from '@aztec/stdlib/messaging';
6
16
 
7
- import type { DataRetrieval } from '../structs/data_retrieval.js';
17
+ import {
18
+ type InboxMessage,
19
+ deserializeInboxMessage,
20
+ serializeInboxMessage,
21
+ updateRollingHash,
22
+ } from '../structs/inbox_message.js';
23
+
24
+ export class MessageStoreError extends Error {
25
+ constructor(
26
+ message: string,
27
+ public readonly inboxMessage: InboxMessage,
28
+ ) {
29
+ super(message);
30
+ this.name = 'MessageStoreError';
31
+ }
32
+ }
8
33
 
9
- /**
10
- * LMDB implementation of the ArchiverDataStore interface.
11
- */
12
34
  export class MessageStore {
13
- #l1ToL2Messages: AztecAsyncMap<string, Buffer>;
35
+ /** Maps from message index to serialized InboxMessage */
36
+ #l1ToL2Messages: AztecAsyncMap<number, Buffer>;
37
+ /** Maps from hex-stringified message leaf to its index */
14
38
  #l1ToL2MessageIndices: AztecAsyncMap<string, bigint>;
15
- #lastSynchedL1Block: AztecAsyncSingleton<bigint>;
39
+ /** Stores L1 block number and hash of the L1 synchpoint */
40
+ #lastSynchedL1Block: AztecAsyncSingleton<Buffer>;
41
+ /** Stores total messages stored */
16
42
  #totalMessageCount: AztecAsyncSingleton<bigint>;
17
43
 
18
44
  #log = createLogger('archiver:message_store');
19
45
 
20
- #l1ToL2MessagesSubtreeSize = 2 ** L1_TO_L2_MSG_SUBTREE_HEIGHT;
21
-
22
46
  constructor(private db: AztecAsyncKVStore) {
23
47
  this.#l1ToL2Messages = db.openMap('archiver_l1_to_l2_messages');
24
48
  this.#l1ToL2MessageIndices = db.openMap('archiver_l1_to_l2_message_indices');
25
- this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_new_messages');
49
+ this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_id');
26
50
  this.#totalMessageCount = db.openSingleton('archiver_l1_to_l2_message_count');
27
51
  }
28
52
 
29
- async getTotalL1ToL2MessageCount(): Promise<bigint> {
53
+ public async getTotalL1ToL2MessageCount(): Promise<bigint> {
30
54
  return (await this.#totalMessageCount.getAsync()) ?? 0n;
31
55
  }
32
56
 
33
- /**
34
- * Gets the last L1 block number that emitted new messages.
35
- * @returns The last L1 block number processed
36
- */
37
- getSynchedL1BlockNumber(): Promise<bigint | undefined> {
38
- return this.#lastSynchedL1Block.getAsync();
57
+ /** Gets the last L1 block synced. */
58
+ public async getSynchedL1Block(): Promise<L1BlockId | undefined> {
59
+ const buffer = await this.#lastSynchedL1Block.getAsync();
60
+ if (!buffer) {
61
+ return undefined;
62
+ }
63
+
64
+ const reader = BufferReader.asReader(buffer);
65
+ return { l1BlockNumber: reader.readUInt256(), l1BlockHash: Buffer32.fromBuffer(reader.readBytes(Buffer32.SIZE)) };
39
66
  }
40
67
 
41
- async setSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
42
- await this.#lastSynchedL1Block.set(l1BlockNumber);
68
+ /** Sets the last L1 block synced */
69
+ public async setSynchedL1Block(l1Block: L1BlockId): Promise<void> {
70
+ const buffer = serializeToBuffer([l1Block.l1BlockNumber, l1Block.l1BlockHash]);
71
+ await this.#lastSynchedL1Block.set(buffer);
43
72
  }
44
73
 
45
74
  /**
46
75
  * Append L1 to L2 messages to the store.
47
- * @param messages - The L1 to L2 messages to be added to the store and the last processed L1 block.
48
- * @returns True if the operation is successful.
76
+ * Requires new messages to be in order and strictly after the last message added.
77
+ * Throws if out of order messages are added or if the rolling hash is invalid.
49
78
  */
50
- addL1ToL2Messages(messages: DataRetrieval<InboxLeaf>): Promise<boolean> {
79
+ public addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
80
+ if (messages.length === 0) {
81
+ return Promise.resolve();
82
+ }
83
+
51
84
  return this.db.transactionAsync(async () => {
52
- const lastL1BlockNumber = (await this.#lastSynchedL1Block.getAsync()) ?? 0n;
53
- if (lastL1BlockNumber >= messages.lastProcessedL1BlockNumber) {
54
- return false;
55
- }
85
+ let lastMessage = await this.getLastMessage();
86
+ let messageCount = 0;
87
+
88
+ for (const message of messages) {
89
+ // Check messages are inserted in increasing order, but allow reinserting messages.
90
+ if (lastMessage && message.index <= lastMessage.index) {
91
+ const existing = await this.#l1ToL2Messages.getAsync(this.indexToKey(message.index));
92
+ if (existing && deserializeInboxMessage(existing).rollingHash.equals(message.rollingHash)) {
93
+ // We reinsert instead of skipping in case the message was re-orged and got added in a different L1 block.
94
+ this.#log.trace(`Reinserting message with index ${message.index} in the store`);
95
+ await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
96
+ continue;
97
+ }
56
98
 
57
- await this.#lastSynchedL1Block.set(messages.lastProcessedL1BlockNumber);
99
+ throw new MessageStoreError(
100
+ `Cannot insert L1 to L2 message with index ${message.index} before last message with index ${lastMessage.index}`,
101
+ message,
102
+ );
103
+ }
58
104
 
59
- for (const message of messages.retrievedData) {
60
- const key = `${message.index}`;
61
- await this.#l1ToL2Messages.set(key, message.leaf.toBuffer());
62
- await this.#l1ToL2MessageIndices.set(message.leaf.toString(), message.index);
105
+ // Check rolling hash is valid.
106
+ const previousRollingHash = lastMessage?.rollingHash ?? Buffer16.ZERO;
107
+ const expectedRollingHash = updateRollingHash(previousRollingHash, message.leaf);
108
+ if (!expectedRollingHash.equals(message.rollingHash)) {
109
+ throw new MessageStoreError(
110
+ `Invalid rolling hash for incoming L1 to L2 message ${message.leaf.toString()} ` +
111
+ `with index ${message.index} ` +
112
+ `(expected ${expectedRollingHash.toString()} from previous hash ${previousRollingHash} but got ${message.rollingHash.toString()})`,
113
+ message,
114
+ );
115
+ }
116
+
117
+ // Check index corresponds to the checkpoint number.
118
+ const [expectedStart, expectedEnd] = InboxLeaf.indexRangeForCheckpoint(message.checkpointNumber);
119
+ if (message.index < expectedStart || message.index >= expectedEnd) {
120
+ throw new MessageStoreError(
121
+ `Invalid index ${message.index} for incoming L1 to L2 message ${message.leaf.toString()} ` +
122
+ `at checkpoint ${message.checkpointNumber} (expected value in range [${expectedStart}, ${expectedEnd}))`,
123
+ message,
124
+ );
125
+ }
126
+
127
+ // Check there are no gaps in the indices within the same checkpoint.
128
+ if (
129
+ lastMessage &&
130
+ message.checkpointNumber === lastMessage.checkpointNumber &&
131
+ message.index !== lastMessage.index + 1n
132
+ ) {
133
+ throw new MessageStoreError(
134
+ `Missing prior message for incoming L1 to L2 message ${message.leaf.toString()} ` +
135
+ `with index ${message.index}`,
136
+ message,
137
+ );
138
+ }
139
+
140
+ // Check the first message in a block has the correct index.
141
+ if (
142
+ (!lastMessage || message.checkpointNumber > lastMessage.checkpointNumber) &&
143
+ message.index !== expectedStart
144
+ ) {
145
+ throw new MessageStoreError(
146
+ `Message ${message.leaf.toString()} for checkpoint ${message.checkpointNumber} has wrong index ` +
147
+ `${message.index} (expected ${expectedStart})`,
148
+ message,
149
+ );
150
+ }
151
+
152
+ // Perform the insertions.
153
+ await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
154
+ await this.#l1ToL2MessageIndices.set(this.leafToIndexKey(message.leaf), message.index);
155
+ messageCount++;
156
+ this.#log.trace(`Inserted L1 to L2 message ${message.leaf} with index ${message.index} into the store`);
157
+ lastMessage = message;
63
158
  }
64
159
 
65
- const lastTotalMessageCount = await this.getTotalL1ToL2MessageCount();
66
- await this.#totalMessageCount.set(lastTotalMessageCount + BigInt(messages.retrievedData.length));
160
+ // Update the L1 sync point to that of the last message added.
161
+ const currentSyncPoint = await this.getSynchedL1Block();
162
+ if (!currentSyncPoint || currentSyncPoint.l1BlockNumber < lastMessage!.l1BlockNumber) {
163
+ await this.setSynchedL1Block({
164
+ l1BlockNumber: lastMessage!.l1BlockNumber,
165
+ l1BlockHash: lastMessage!.l1BlockHash,
166
+ });
167
+ }
67
168
 
68
- return true;
169
+ // Update total message count with the number of inserted messages.
170
+ await this.increaseTotalMessageCount(messageCount);
69
171
  });
70
172
  }
71
173
 
@@ -74,29 +176,86 @@ export class MessageStore {
74
176
  * @param l1ToL2Message - The L1 to L2 message.
75
177
  * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
76
178
  */
77
- getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
78
- return this.#l1ToL2MessageIndices.getAsync(l1ToL2Message.toString());
179
+ public getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
180
+ return this.#l1ToL2MessageIndices.getAsync(this.leafToIndexKey(l1ToL2Message));
181
+ }
182
+
183
+ public async getLastMessage(): Promise<InboxMessage | undefined> {
184
+ const [msg] = await toArray(this.#l1ToL2Messages.valuesAsync({ reverse: true, limit: 1 }));
185
+ return msg ? deserializeInboxMessage(msg) : undefined;
79
186
  }
80
187
 
81
- async getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
188
+ public async getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
82
189
  const messages: Fr[] = [];
83
- let undefinedMessageFound = false;
84
- const startIndex = Number(InboxLeaf.smallestIndexFromL2Block(blockNumber));
85
- for (let i = startIndex; i < startIndex + this.#l1ToL2MessagesSubtreeSize; i++) {
86
- // This is inefficient but probably fine for now.
87
- const key = `${i}`;
88
- const message = await this.#l1ToL2Messages.getAsync(key);
89
- if (message) {
90
- if (undefinedMessageFound) {
91
- throw new Error(`L1 to L2 message gap found in block ${blockNumber}`);
92
- }
93
- messages.push(Fr.fromBuffer(message));
94
- } else {
95
- undefinedMessageFound = true;
96
- // We continue iterating over messages here to verify that there are no more messages after the undefined one.
97
- // --> If this was the case this would imply there is some issue with log fetching.
190
+
191
+ const [startIndex, endIndex] = InboxLeaf.indexRangeForCheckpoint(checkpointNumber);
192
+ let lastIndex = startIndex - 1n;
193
+
194
+ for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync({
195
+ start: this.indexToKey(startIndex),
196
+ end: this.indexToKey(endIndex),
197
+ })) {
198
+ const msg = deserializeInboxMessage(msgBuffer);
199
+ if (msg.checkpointNumber !== checkpointNumber) {
200
+ throw new Error(
201
+ `L1 to L2 message with index ${msg.index} has invalid checkpoint number ${msg.checkpointNumber}`,
202
+ );
203
+ } else if (msg.index !== lastIndex + 1n) {
204
+ throw new Error(`Expected L1 to L2 message with index ${lastIndex + 1n} but got ${msg.index}`);
98
205
  }
206
+ lastIndex = msg.index;
207
+ messages.push(msg.leaf);
99
208
  }
209
+
100
210
  return messages;
101
211
  }
212
+
213
+ public async *iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
214
+ const entriesRange = mapRange(range, this.indexToKey);
215
+ for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync(entriesRange)) {
216
+ yield deserializeInboxMessage(msgBuffer);
217
+ }
218
+ }
219
+
220
+ public removeL1ToL2Messages(startIndex: bigint): Promise<void> {
221
+ this.#log.debug(`Deleting L1 to L2 messages from index ${startIndex}`);
222
+ let deleteCount = 0;
223
+
224
+ return this.db.transactionAsync(async () => {
225
+ for await (const [key, msgBuffer] of this.#l1ToL2Messages.entriesAsync({
226
+ start: this.indexToKey(startIndex),
227
+ })) {
228
+ this.#log.trace(`Deleting L1 to L2 message with index ${key - 1} from the store`);
229
+ await this.#l1ToL2Messages.delete(key);
230
+ await this.#l1ToL2MessageIndices.delete(this.leafToIndexKey(deserializeInboxMessage(msgBuffer).leaf));
231
+ deleteCount++;
232
+ }
233
+ await this.increaseTotalMessageCount(-deleteCount);
234
+ this.#log.warn(`Deleted ${deleteCount} L1 to L2 messages from index ${startIndex} from the store`);
235
+ });
236
+ }
237
+
238
+ public rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber: CheckpointNumber): Promise<void> {
239
+ this.#log.debug(`Deleting L1 to L2 messages up to target checkpoint ${targetCheckpointNumber}`);
240
+ const startIndex = InboxLeaf.smallestIndexForCheckpoint(CheckpointNumber(targetCheckpointNumber + 1));
241
+ return this.removeL1ToL2Messages(startIndex);
242
+ }
243
+
244
+ private indexToKey(index: bigint): number {
245
+ return Number(index);
246
+ }
247
+
248
+ private leafToIndexKey(leaf: Fr): string {
249
+ return leaf.toString();
250
+ }
251
+
252
+ private async increaseTotalMessageCount(count: bigint | number): Promise<void> {
253
+ if (count === 0) {
254
+ return;
255
+ }
256
+ return await this.db.transactionAsync(async () => {
257
+ const lastTotalMessageCount = await this.getTotalL1ToL2MessageCount();
258
+ await this.#totalMessageCount.set(lastTotalMessageCount + BigInt(count));
259
+ });
260
+ }
102
261
  }
@@ -0,0 +1,98 @@
1
+ # Archiver L1 Data Retrieval
2
+
3
+ Modules and classes to handle data retrieval from L1 for the archiver.
4
+
5
+ ## Calldata Retriever
6
+
7
+ The sequencer publisher bundles multiple operations into a single multicall3 transaction for gas
8
+ efficiency. A typical transaction includes:
9
+
10
+ 1. Attestation invalidations (if needed): `invalidateBadAttestation`, `invalidateInsufficientAttestations`
11
+ 2. Block proposal: `propose` (exactly one per transaction to the rollup contract)
12
+ 3. Governance and slashing (if needed): votes, payload creation/execution
13
+
14
+ The archiver needs to extract the `propose` calldata from these bundled transactions to reconstruct
15
+ L2 blocks. This class needs to handle scenarios where the transaction was submitted via multicall3,
16
+ as well as alternative ways for submitting the `propose` call that other clients might use.
17
+
18
+ ### Multicall3 Validation and Decoding
19
+
20
+ First attempt to decode the transaction as a multicall3 `aggregate3` call with validation:
21
+
22
+ - Check if transaction is to multicall3 address (`0xcA11bde05977b3631167028862bE2a173976CA11`)
23
+ - Decode as `aggregate3(Call3[] calldata calls)`
24
+ - Allow calls to known addresses and methods (rollup, governance, slashing contracts, etc.)
25
+ - Find the single `propose` call to the rollup contract
26
+ - Verify exactly one `propose` call exists
27
+ - Extract and return the propose calldata
28
+
29
+ This step handles the common case efficiently without requiring expensive trace or debug RPC calls.
30
+ Any validation failure triggers fallback to the next step.
31
+
32
+ ### Direct Propose Call
33
+
34
+ Second attempt to decode the transaction as a direct `propose` call to the rollup contract:
35
+
36
+ - Check if transaction is to the rollup address
37
+ - Decode as `propose` function call
38
+ - Verify the function is indeed `propose`
39
+ - Return the transaction input as the propose calldata
40
+
41
+ This handles scenarios where clients submit transactions directly to the rollup contract without
42
+ using multicall3 for bundling. Any validation failure triggers fallback to the next step.
43
+
44
+ ### Spire Proposer Call
45
+
46
+ Given existing attempts to route the call via the Spire proposer, we also check if the tx is `to` the
47
+ proposer known address, and if so, we try decoding it as either a multicall3 or a direct call to the
48
+ rollup contract.
49
+
50
+ Similar as with the multicall3 check, we check that there are no other calls in the Spire proposer, so
51
+ we are absolutely sure that the only call is the successful one to the rollup. Any extraneous call would
52
+ imply an unexpected path to calling `propose` in the rollup contract, and since we cannot verify if the
53
+ calldata arguments we extracted are the correct ones (see the section below), we cannot know for sure which
54
+ one is the call that succeeded, so we don't know which calldata to process.
55
+
56
+ Furthermore, since the Spire proposer is upgradeable, we check if the implementation has not changed in
57
+ order to decode. As usual, any validation failure triggers fallback to the next step.
58
+
59
+ ### Verifying Multicall3 Arguments
60
+
61
+ **This is NOT implemented for simplicity's sake**
62
+
63
+ If the checks above don't hold, such as when there are multiple calls to `propose`, then we cannot
64
+ reliably extract the `propose` calldata from the multicall3 arguments alone. We can try a best-effort
65
+ where we try all `propose` calls we see and validate them against on-chain data. Note that we can use these
66
+ same strategies if we were to obtain the calldata from another source.
67
+
68
+ #### TempBlockLog Verification
69
+
70
+ Read the stored `TempBlockLog` for the L2 block number from L1 and verify it matches our decoded header hash,
71
+ since the `TempBlockLog` stores the hash of the proposed block header, the payload commitment, and the attestations.
72
+
73
+ However, `TempBlockLog` is only stored temporarily and deleted after proven, so this method only works for recent
74
+ blocks, not for historical data syncing.
75
+
76
+ #### Archive Verification
77
+
78
+ Verify that the archive root in the decoded propose is correct with regard to the block header. This requires
79
+ hashing the block header we have retrieved, inserting it into the archive tree, and checking the resulting root
80
+ against the one we got from L1.
81
+
82
+ However, this requires that the archive keeps a reference to world-state, which is not the case in the current
83
+ system.
84
+
85
+ #### Emit Commitments in Rollup Contract
86
+
87
+ Modify rollup contract to emit commitments to the block header in the `L2BlockProposed` event, allowing us to easily
88
+ verify the calldata we obtained vs the emitted event.
89
+
90
+ However, modifying the rollup contract is out of scope for this change. But we can implement this approach in `v2`.
91
+
92
+ ### Debug and Trace Transaction Fallback
93
+
94
+ Last, we use L1 node's trace/debug RPC methods to definitively identify the one successful `propose` call within the tx.
95
+ We can then extract the exact calldata that hit the `propose` function in the rollup contract.
96
+
97
+ This approach requires access to a debug-enabled L1 node, which may be more resource-intensive, so we only
98
+ use it as a fallback when the first step fails, which should be rare in practice.
@@ -0,0 +1,182 @@
1
+ #!/usr/bin/env node
2
+ import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
3
+ import { CheckpointNumber } from '@aztec/foundation/branded-types';
4
+ import { EthAddress } from '@aztec/foundation/eth-address';
5
+ import { createLogger } from '@aztec/foundation/log';
6
+
7
+ import { type Hex, createPublicClient, http } from 'viem';
8
+ import { mainnet } from 'viem/chains';
9
+
10
+ import { CalldataRetriever } from '../calldata_retriever.js';
11
+
12
+ const logger = createLogger('archiver:calldata-test');
13
+
14
+ interface ScriptArgs {
15
+ rollupAddress: EthAddress;
16
+ txHash: Hex;
17
+ rpcUrl: string;
18
+ targetCommitteeSize: number;
19
+ }
20
+
21
+ function parseArgs(): ScriptArgs {
22
+ const args = process.argv.slice(2);
23
+
24
+ if (args.length < 2) {
25
+ // eslint-disable-next-line no-console
26
+ console.error('Usage: node index.js <rollup-address> <tx-hash> [target-committee-size]');
27
+ // eslint-disable-next-line no-console
28
+ console.error('');
29
+ // eslint-disable-next-line no-console
30
+ console.error('Environment variables:');
31
+ // eslint-disable-next-line no-console
32
+ console.error(' ETHEREUM_HOST or RPC_URL - Ethereum RPC endpoint');
33
+ // eslint-disable-next-line no-console
34
+ console.error('');
35
+ // eslint-disable-next-line no-console
36
+ console.error('Example:');
37
+ // eslint-disable-next-line no-console
38
+ console.error(' RPC_URL=https://eth-mainnet.g.alchemy.com/v2/YOUR-API-KEY \\');
39
+ // eslint-disable-next-line no-console
40
+ console.error(' node index.js 0x1234... 0xabcd... 32');
41
+ process.exit(1);
42
+ }
43
+
44
+ const rollupAddress = EthAddress.fromString(args[0]);
45
+ const txHash = args[1] as Hex;
46
+ const targetCommitteeSize = args[2] ? parseInt(args[2], 10) : 24;
47
+
48
+ const rpcUrl = process.env.ETHEREUM_HOST || process.env.RPC_URL;
49
+ if (!rpcUrl) {
50
+ // eslint-disable-next-line no-console
51
+ console.error('Error: ETHEREUM_HOST or RPC_URL environment variable must be set');
52
+ process.exit(1);
53
+ }
54
+
55
+ if (targetCommitteeSize <= 0 || targetCommitteeSize > 256) {
56
+ // eslint-disable-next-line no-console
57
+ console.error('Error: target-committee-size must be between 1 and 256');
58
+ process.exit(1);
59
+ }
60
+
61
+ return { rollupAddress, txHash, rpcUrl, targetCommitteeSize };
62
+ }
63
+
64
+ async function main() {
65
+ const { rollupAddress, txHash, rpcUrl, targetCommitteeSize } = parseArgs();
66
+
67
+ logger.info('Calldata Retriever Test Script');
68
+ logger.info('===============================');
69
+ logger.info(`Rollup Address: ${rollupAddress.toString()}`);
70
+ logger.info(`Transaction Hash: ${txHash}`);
71
+ logger.info(`RPC URL: ${rpcUrl}`);
72
+ logger.info(`Target Committee Size: ${targetCommitteeSize}`);
73
+ logger.info('');
74
+
75
+ try {
76
+ // Create viem public client
77
+ const publicClient = createPublicClient({
78
+ chain: mainnet,
79
+ transport: http(rpcUrl, { batch: false }),
80
+ });
81
+
82
+ logger.info('Fetching transaction...');
83
+ const tx = await publicClient.getTransaction({ hash: txHash });
84
+
85
+ if (!tx) {
86
+ throw new Error(`Transaction ${txHash} not found`);
87
+ }
88
+
89
+ logger.info(`Transaction found in block ${tx.blockNumber}`);
90
+
91
+ // For simplicity, use zero addresses for optional contract addresses
92
+ // In production, these would be fetched from the rollup contract or configuration
93
+ const slashingProposerAddress = EthAddress.ZERO;
94
+ const governanceProposerAddress = EthAddress.ZERO;
95
+ const slashFactoryAddress = undefined;
96
+
97
+ logger.info('Using zero addresses for governance/slashing (can be configured if needed)');
98
+
99
+ // Create CalldataRetriever
100
+ const retriever = new CalldataRetriever(
101
+ publicClient as unknown as ViemPublicClient,
102
+ publicClient as unknown as ViemPublicDebugClient,
103
+ targetCommitteeSize,
104
+ undefined,
105
+ logger,
106
+ {
107
+ rollupAddress,
108
+ governanceProposerAddress,
109
+ slashingProposerAddress,
110
+ slashFactoryAddress,
111
+ },
112
+ );
113
+
114
+ // Extract L2 block number from transaction logs
115
+ logger.info('Decoding transaction to extract L2 block number...');
116
+ const receipt = await publicClient.getTransactionReceipt({ hash: txHash });
117
+ const l2BlockProposedEvent = receipt.logs.find(log => {
118
+ try {
119
+ // Try to match the L2BlockProposed event
120
+ return (
121
+ log.address.toLowerCase() === rollupAddress.toString().toLowerCase() &&
122
+ log.topics[0] === '0x2f1d0e696fa5186494a2f2f89a0e0bcbb15d607f6c5eac4637e07e1e5e7d3c00' // L2BlockProposed event signature
123
+ );
124
+ } catch {
125
+ return false;
126
+ }
127
+ });
128
+
129
+ let l2BlockNumber: number;
130
+ if (l2BlockProposedEvent && l2BlockProposedEvent.topics[1]) {
131
+ // L2 block number is typically the first indexed parameter
132
+ l2BlockNumber = Number(BigInt(l2BlockProposedEvent.topics[1]));
133
+ logger.info(`L2 Block Number (from event): ${l2BlockNumber}`);
134
+ } else {
135
+ // Fallback: try to extract from transaction data or use a default
136
+ logger.warn('Could not extract L2 block number from event, using block number as fallback');
137
+ l2BlockNumber = Number(tx.blockNumber);
138
+ }
139
+
140
+ logger.info('');
141
+ logger.info('Retrieving block header from rollup transaction...');
142
+ logger.info('');
143
+
144
+ // For this script, we don't have blob hashes or expected hashes, so pass empty arrays/objects
145
+ const result = await retriever.getCheckpointFromRollupTx(txHash, [], CheckpointNumber(l2BlockNumber), {});
146
+
147
+ logger.info(' Successfully retrieved block header!');
148
+ logger.info('');
149
+ logger.info('Block Header Details:');
150
+ logger.info('====================');
151
+ logger.info(`Checkpoint Number: ${result.checkpointNumber}`);
152
+ logger.info(`Block Hash: ${result.blockHash}`);
153
+ logger.info(`Archive Root: ${result.archiveRoot.toString()}`);
154
+ logger.info('');
155
+ logger.info('Header:');
156
+ logger.info(` Slot Number: ${result.header.slotNumber.toString()}`);
157
+ logger.info(` Timestamp: ${result.header.timestamp.toString()}`);
158
+ logger.info(` Coinbase: ${result.header.coinbase.toString()}`);
159
+ logger.info(` Fee Recipient: ${result.header.feeRecipient.toString()}`);
160
+ logger.info(` Total Mana Used: ${result.header.totalManaUsed.toString()}`);
161
+ logger.info('');
162
+ logger.info('Attestations:');
163
+ logger.info(` Count: ${result.attestations.length}`);
164
+ logger.info(` Non-empty attestations: ${result.attestations.filter((a: any) => !a.signature.isEmpty()).length}`);
165
+
166
+ process.exit(0);
167
+ } catch (error) {
168
+ logger.error('Error retrieving block header:');
169
+ logger.error(error instanceof Error ? error.message : String(error));
170
+
171
+ if (error instanceof Error && error.stack) {
172
+ logger.debug(error.stack);
173
+ }
174
+
175
+ process.exit(1);
176
+ }
177
+ }
178
+
179
+ // Only run if this is the main module
180
+ if (import.meta.url === `file://${process.argv[1]}`) {
181
+ void main();
182
+ }