@aztec/archiver 0.86.0-starknet.1 → 0.87.0

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 (57) hide show
  1. package/dest/archiver/archiver.d.ts +20 -8
  2. package/dest/archiver/archiver.d.ts.map +1 -1
  3. package/dest/archiver/archiver.js +259 -65
  4. package/dest/archiver/archiver_store.d.ts +30 -15
  5. package/dest/archiver/archiver_store.d.ts.map +1 -1
  6. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  7. package/dest/archiver/archiver_store_test_suite.js +357 -55
  8. package/dest/archiver/data_retrieval.d.ts +5 -3
  9. package/dest/archiver/data_retrieval.d.ts.map +1 -1
  10. package/dest/archiver/data_retrieval.js +41 -23
  11. package/dest/archiver/errors.d.ts +8 -0
  12. package/dest/archiver/errors.d.ts.map +1 -1
  13. package/dest/archiver/errors.js +12 -0
  14. package/dest/archiver/instrumentation.d.ts.map +1 -1
  15. package/dest/archiver/kv_archiver_store/block_store.d.ts +4 -1
  16. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  17. package/dest/archiver/kv_archiver_store/block_store.js +34 -5
  18. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +3 -1
  19. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  20. package/dest/archiver/kv_archiver_store/contract_instance_store.js +17 -3
  21. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +19 -11
  22. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  23. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +30 -10
  24. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  25. package/dest/archiver/kv_archiver_store/log_store.js +1 -1
  26. package/dest/archiver/kv_archiver_store/message_store.d.ts +21 -15
  27. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  28. package/dest/archiver/kv_archiver_store/message_store.js +150 -48
  29. package/dest/archiver/structs/inbox_message.d.ts +14 -0
  30. package/dest/archiver/structs/inbox_message.d.ts.map +1 -0
  31. package/dest/archiver/structs/inbox_message.js +38 -0
  32. package/dest/rpc/index.d.ts +1 -1
  33. package/dest/test/mock_l2_block_source.d.ts +2 -0
  34. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  35. package/dest/test/mock_l2_block_source.js +8 -1
  36. package/dest/test/mock_structs.d.ts +9 -0
  37. package/dest/test/mock_structs.d.ts.map +1 -0
  38. package/dest/test/mock_structs.js +37 -0
  39. package/package.json +15 -15
  40. package/src/archiver/archiver.ts +297 -79
  41. package/src/archiver/archiver_store.ts +34 -15
  42. package/src/archiver/archiver_store_test_suite.ts +300 -45
  43. package/src/archiver/data_retrieval.ts +47 -30
  44. package/src/archiver/errors.ts +21 -0
  45. package/src/archiver/instrumentation.ts +4 -1
  46. package/src/archiver/kv_archiver_store/block_store.ts +37 -8
  47. package/src/archiver/kv_archiver_store/contract_instance_store.ts +20 -7
  48. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +47 -15
  49. package/src/archiver/kv_archiver_store/log_store.ts +6 -2
  50. package/src/archiver/kv_archiver_store/message_store.ts +209 -53
  51. package/src/archiver/structs/inbox_message.ts +40 -0
  52. package/src/test/mock_l2_block_source.ts +9 -1
  53. package/src/test/mock_structs.ts +49 -0
  54. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
  55. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
  56. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
  57. package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
@@ -1,71 +1,172 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
1
+ import type { L1BlockId } from '@aztec/ethereum';
2
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
2
3
  import { Fr } from '@aztec/foundation/fields';
4
+ import { toArray } from '@aztec/foundation/iterable';
3
5
  import { createLogger } from '@aztec/foundation/log';
4
- import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton } from '@aztec/kv-store';
6
+ import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
7
+ import {
8
+ type AztecAsyncKVStore,
9
+ type AztecAsyncMap,
10
+ type AztecAsyncSingleton,
11
+ type CustomRange,
12
+ mapRange,
13
+ } from '@aztec/kv-store';
5
14
  import { InboxLeaf } from '@aztec/stdlib/messaging';
6
15
 
7
- import type { DataRetrieval } from '../structs/data_retrieval.js';
16
+ import {
17
+ type InboxMessage,
18
+ deserializeInboxMessage,
19
+ serializeInboxMessage,
20
+ updateRollingHash,
21
+ } from '../structs/inbox_message.js';
22
+
23
+ export class MessageStoreError extends Error {
24
+ constructor(
25
+ message: string,
26
+ public readonly inboxMessage: InboxMessage,
27
+ ) {
28
+ super(message);
29
+ this.name = 'MessageStoreError';
30
+ }
31
+ }
8
32
 
9
- /**
10
- * LMDB implementation of the ArchiverDataStore interface.
11
- */
12
33
  export class MessageStore {
13
- #l1ToL2Messages: AztecAsyncMap<string, Buffer>;
34
+ /** Maps from message index to serialized InboxMessage */
35
+ #l1ToL2Messages: AztecAsyncMap<number, Buffer>;
36
+ /** Maps from hex-stringified message leaf to its index */
14
37
  #l1ToL2MessageIndices: AztecAsyncMap<string, bigint>;
15
- #lastSynchedL1Block: AztecAsyncSingleton<bigint>;
38
+ /** Stores L1 block number and hash of the L1 synchpoint */
39
+ #lastSynchedL1Block: AztecAsyncSingleton<Buffer>;
40
+ /** Stores total messages stored */
16
41
  #totalMessageCount: AztecAsyncSingleton<bigint>;
17
42
 
18
43
  #log = createLogger('archiver:message_store');
19
44
 
20
- #l1ToL2MessagesSubtreeSize = 2 ** L1_TO_L2_MSG_SUBTREE_HEIGHT;
21
-
22
45
  constructor(private db: AztecAsyncKVStore) {
23
46
  this.#l1ToL2Messages = db.openMap('archiver_l1_to_l2_messages');
24
47
  this.#l1ToL2MessageIndices = db.openMap('archiver_l1_to_l2_message_indices');
25
- this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_new_messages');
48
+ this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_id');
26
49
  this.#totalMessageCount = db.openSingleton('archiver_l1_to_l2_message_count');
27
50
  }
28
51
 
29
- async getTotalL1ToL2MessageCount(): Promise<bigint> {
52
+ public async getTotalL1ToL2MessageCount(): Promise<bigint> {
30
53
  return (await this.#totalMessageCount.getAsync()) ?? 0n;
31
54
  }
32
55
 
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();
56
+ /** Gets the last L1 block synced. */
57
+ public async getSynchedL1Block(): Promise<L1BlockId | undefined> {
58
+ const buffer = await this.#lastSynchedL1Block.getAsync();
59
+ if (!buffer) {
60
+ return undefined;
61
+ }
62
+
63
+ const reader = BufferReader.asReader(buffer);
64
+ return { l1BlockNumber: reader.readUInt256(), l1BlockHash: Buffer32.fromBuffer(reader.readBytes(Buffer32.SIZE)) };
39
65
  }
40
66
 
41
- async setSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
42
- await this.#lastSynchedL1Block.set(l1BlockNumber);
67
+ /** Sets the last L1 block synced */
68
+ public async setSynchedL1Block(l1Block: L1BlockId): Promise<void> {
69
+ const buffer = serializeToBuffer([l1Block.l1BlockNumber, l1Block.l1BlockHash]);
70
+ await this.#lastSynchedL1Block.set(buffer);
43
71
  }
44
72
 
45
73
  /**
46
74
  * 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.
75
+ * Requires new messages to be in order and strictly after the last message added.
76
+ * Throws if out of order messages are added or if the rolling hash is invalid.
49
77
  */
50
- addL1ToL2Messages(messages: DataRetrieval<InboxLeaf>): Promise<boolean> {
78
+ public addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
79
+ if (messages.length === 0) {
80
+ return Promise.resolve();
81
+ }
82
+
51
83
  return this.db.transactionAsync(async () => {
52
- const lastL1BlockNumber = (await this.#lastSynchedL1Block.getAsync()) ?? 0n;
53
- if (lastL1BlockNumber >= messages.lastProcessedL1BlockNumber) {
54
- return false;
55
- }
84
+ let lastMessage = await this.getLastMessage();
85
+ let messageCount = 0;
86
+
87
+ for (const message of messages) {
88
+ // Check messages are inserted in increasing order, but allow reinserting messages.
89
+ if (lastMessage && message.index <= lastMessage.index) {
90
+ const existing = await this.#l1ToL2Messages.getAsync(this.indexToKey(message.index));
91
+ if (existing && deserializeInboxMessage(existing).rollingHash.equals(message.rollingHash)) {
92
+ // We reinsert instead of skipping in case the message was re-orged and got added in a different L1 block.
93
+ this.#log.trace(`Reinserting message with index ${message.index} in the store`);
94
+ await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
95
+ continue;
96
+ }
56
97
 
57
- await this.#lastSynchedL1Block.set(messages.lastProcessedL1BlockNumber);
98
+ throw new MessageStoreError(
99
+ `Cannot insert L1 to L2 message with index ${message.index} before last message with index ${lastMessage.index}`,
100
+ message,
101
+ );
102
+ }
58
103
 
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);
104
+ // Check rolling hash is valid.
105
+ const previousRollingHash = lastMessage?.rollingHash ?? Buffer16.ZERO;
106
+ const expectedRollingHash = updateRollingHash(previousRollingHash, message.leaf);
107
+ if (!expectedRollingHash.equals(message.rollingHash)) {
108
+ throw new MessageStoreError(
109
+ `Invalid rolling hash for incoming L1 to L2 message ${message.leaf.toString()} ` +
110
+ `with index ${message.index} ` +
111
+ `(expected ${expectedRollingHash.toString()} from previous hash ${previousRollingHash} but got ${message.rollingHash.toString()})`,
112
+ message,
113
+ );
114
+ }
115
+
116
+ // Check index corresponds to the L2 block number.
117
+ const [expectedStart, expectedEnd] = InboxLeaf.indexRangeFromL2Block(message.l2BlockNumber);
118
+ if (message.index < expectedStart || message.index >= expectedEnd) {
119
+ throw new MessageStoreError(
120
+ `Invalid index ${message.index} for incoming L1 to L2 message ${message.leaf.toString()} ` +
121
+ `at block ${message.l2BlockNumber} (expected value in range [${expectedStart}, ${expectedEnd}))`,
122
+ message,
123
+ );
124
+ }
125
+
126
+ // Check there are no gaps in the indices within the same block.
127
+ if (
128
+ lastMessage &&
129
+ message.l2BlockNumber === lastMessage.l2BlockNumber &&
130
+ message.index !== lastMessage.index + 1n
131
+ ) {
132
+ throw new MessageStoreError(
133
+ `Missing prior message for incoming L1 to L2 message ${message.leaf.toString()} ` +
134
+ `with index ${message.index}`,
135
+ message,
136
+ );
137
+ }
138
+
139
+ // Check the first message in a block has the correct index.
140
+ if (
141
+ (!lastMessage || message.l2BlockNumber > lastMessage.l2BlockNumber) &&
142
+ message.index !== InboxLeaf.smallestIndexFromL2Block(message.l2BlockNumber)
143
+ ) {
144
+ throw new MessageStoreError(
145
+ `Message ${message.leaf.toString()} for L2 block ${message.l2BlockNumber} has wrong index ` +
146
+ `${message.index} (expected ${InboxLeaf.smallestIndexFromL2Block(message.l2BlockNumber)})`,
147
+ message,
148
+ );
149
+ }
150
+
151
+ // Perform the insertions.
152
+ await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
153
+ await this.#l1ToL2MessageIndices.set(this.leafToIndexKey(message.leaf), message.index);
154
+ messageCount++;
155
+ this.#log.trace(`Inserted L1 to L2 message ${message.leaf} with index ${message.index} into the store`);
156
+ lastMessage = message;
63
157
  }
64
158
 
65
- const lastTotalMessageCount = await this.getTotalL1ToL2MessageCount();
66
- await this.#totalMessageCount.set(lastTotalMessageCount + BigInt(messages.retrievedData.length));
159
+ // Update the L1 sync point to that of the last message added.
160
+ const currentSyncPoint = await this.getSynchedL1Block();
161
+ if (!currentSyncPoint || currentSyncPoint.l1BlockNumber < lastMessage!.l1BlockNumber) {
162
+ await this.setSynchedL1Block({
163
+ l1BlockNumber: lastMessage!.l1BlockNumber,
164
+ l1BlockHash: lastMessage!.l1BlockHash,
165
+ });
166
+ }
67
167
 
68
- return true;
168
+ // Update total message count with the number of inserted messages.
169
+ await this.increaseTotalMessageCount(messageCount);
69
170
  });
70
171
  }
71
172
 
@@ -74,29 +175,84 @@ export class MessageStore {
74
175
  * @param l1ToL2Message - The L1 to L2 message.
75
176
  * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
76
177
  */
77
- getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
78
- return this.#l1ToL2MessageIndices.getAsync(l1ToL2Message.toString());
178
+ public getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
179
+ return this.#l1ToL2MessageIndices.getAsync(this.leafToIndexKey(l1ToL2Message));
180
+ }
181
+
182
+ public async getLastMessage(): Promise<InboxMessage | undefined> {
183
+ const [msg] = await toArray(this.#l1ToL2Messages.valuesAsync({ reverse: true, limit: 1 }));
184
+ return msg ? deserializeInboxMessage(msg) : undefined;
79
185
  }
80
186
 
81
- async getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
187
+ public async getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
82
188
  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.
189
+
190
+ const [startIndex, endIndex] = InboxLeaf.indexRangeFromL2Block(blockNumber);
191
+ let lastIndex = startIndex - 1n;
192
+
193
+ for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync({
194
+ start: this.indexToKey(startIndex),
195
+ end: this.indexToKey(endIndex),
196
+ })) {
197
+ const msg = deserializeInboxMessage(msgBuffer);
198
+ if (msg.l2BlockNumber !== blockNumber) {
199
+ throw new Error(`L1 to L2 message with index ${msg.index} has invalid block number ${msg.l2BlockNumber}`);
200
+ } else if (msg.index !== lastIndex + 1n) {
201
+ throw new Error(`Expected L1 to L2 message with index ${lastIndex + 1n} but got ${msg.index}`);
98
202
  }
203
+ lastIndex = msg.index;
204
+ messages.push(msg.leaf);
99
205
  }
206
+
100
207
  return messages;
101
208
  }
209
+
210
+ public async *iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
211
+ const entriesRange = mapRange(range, this.indexToKey);
212
+ for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync(entriesRange)) {
213
+ yield deserializeInboxMessage(msgBuffer);
214
+ }
215
+ }
216
+
217
+ public removeL1ToL2Messages(startIndex: bigint): Promise<void> {
218
+ this.#log.debug(`Deleting L1 to L2 messages from index ${startIndex}`);
219
+ let deleteCount = 0;
220
+
221
+ return this.db.transactionAsync(async () => {
222
+ for await (const [key, msgBuffer] of this.#l1ToL2Messages.entriesAsync({
223
+ start: this.indexToKey(startIndex),
224
+ })) {
225
+ this.#log.trace(`Deleting L1 to L2 message with index ${key - 1} from the store`);
226
+ await this.#l1ToL2Messages.delete(key);
227
+ await this.#l1ToL2MessageIndices.delete(this.leafToIndexKey(deserializeInboxMessage(msgBuffer).leaf));
228
+ deleteCount++;
229
+ }
230
+ await this.increaseTotalMessageCount(-deleteCount);
231
+ this.#log.warn(`Deleted ${deleteCount} L1 to L2 messages from index ${startIndex} from the store`);
232
+ });
233
+ }
234
+
235
+ public rollbackL1ToL2MessagesToL2Block(targetBlockNumber: bigint): Promise<void> {
236
+ this.#log.debug(`Deleting L1 to L2 messages up to target L2 block ${targetBlockNumber}`);
237
+ const startIndex = InboxLeaf.smallestIndexFromL2Block(targetBlockNumber + 1n);
238
+ return this.removeL1ToL2Messages(startIndex);
239
+ }
240
+
241
+ private indexToKey(index: bigint): number {
242
+ return Number(index);
243
+ }
244
+
245
+ private leafToIndexKey(leaf: Fr): string {
246
+ return leaf.toString();
247
+ }
248
+
249
+ private async increaseTotalMessageCount(count: bigint | number): Promise<void> {
250
+ if (count === 0) {
251
+ return;
252
+ }
253
+ return await this.db.transactionAsync(async () => {
254
+ const lastTotalMessageCount = await this.getTotalL1ToL2MessageCount();
255
+ await this.#totalMessageCount.set(lastTotalMessageCount + BigInt(count));
256
+ });
257
+ }
102
258
  }
@@ -0,0 +1,40 @@
1
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
2
+ import { keccak256 } from '@aztec/foundation/crypto';
3
+ import { Fr } from '@aztec/foundation/fields';
4
+ import { BufferReader, bigintToUInt64BE, numToUInt32BE, serializeToBuffer } from '@aztec/foundation/serialize';
5
+
6
+ export type InboxMessage = {
7
+ index: bigint;
8
+ leaf: Fr;
9
+ l2BlockNumber: bigint;
10
+ l1BlockNumber: bigint;
11
+ l1BlockHash: Buffer32;
12
+ rollingHash: Buffer16;
13
+ };
14
+
15
+ export function updateRollingHash(currentRollingHash: Buffer16, leaf: Fr): Buffer16 {
16
+ const input = Buffer.concat([currentRollingHash.toBuffer(), leaf.toBuffer()]);
17
+ return Buffer16.fromBuffer(keccak256(input));
18
+ }
19
+
20
+ export function serializeInboxMessage(message: InboxMessage): Buffer {
21
+ return serializeToBuffer([
22
+ bigintToUInt64BE(message.index),
23
+ message.leaf,
24
+ message.l1BlockHash,
25
+ numToUInt32BE(Number(message.l1BlockNumber)),
26
+ numToUInt32BE(Number(message.l2BlockNumber)),
27
+ message.rollingHash,
28
+ ]);
29
+ }
30
+
31
+ export function deserializeInboxMessage(buffer: Buffer): InboxMessage {
32
+ const reader = BufferReader.asReader(buffer);
33
+ const index = reader.readUInt64();
34
+ const leaf = reader.readObject(Fr);
35
+ const l1BlockHash = reader.readObject(Buffer32);
36
+ const l1BlockNumber = BigInt(reader.readNumber());
37
+ const l2BlockNumber = BigInt(reader.readNumber());
38
+ const rollingHash = reader.readObject(Buffer16);
39
+ return { index, leaf, l1BlockHash, l1BlockNumber, l2BlockNumber, rollingHash };
40
+ }
@@ -17,6 +17,7 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
17
17
  protected l2Blocks: L2Block[] = [];
18
18
 
19
19
  private provenBlockNumber: number = 0;
20
+ private finalizedBlockNumber: number = 0;
20
21
 
21
22
  private log = createLogger('archiver:mock_l2_block_source');
22
23
 
@@ -44,6 +45,13 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
44
45
  this.provenBlockNumber = provenBlockNumber;
45
46
  }
46
47
 
48
+ public setFinalizedBlockNumber(finalizedBlockNumber: number) {
49
+ if (finalizedBlockNumber > this.provenBlockNumber) {
50
+ this.provenBlockNumber = finalizedBlockNumber;
51
+ }
52
+ this.finalizedBlockNumber = finalizedBlockNumber;
53
+ }
54
+
47
55
  /**
48
56
  * Method to fetch the rollup contract address at the base-layer.
49
57
  * @returns The rollup address.
@@ -174,7 +182,7 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
174
182
  const [latest, proven, finalized] = [
175
183
  await this.getBlockNumber(),
176
184
  await this.getProvenBlockNumber(),
177
- await this.getProvenBlockNumber(),
185
+ this.finalizedBlockNumber,
178
186
  ] as const;
179
187
 
180
188
  const latestBlock = this.l2Blocks[latest - 1];
@@ -0,0 +1,49 @@
1
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
2
+ import { randomBigInt } from '@aztec/foundation/crypto';
3
+ import { Fr } from '@aztec/foundation/fields';
4
+ import { InboxLeaf } from '@aztec/stdlib/messaging';
5
+
6
+ import { type InboxMessage, updateRollingHash } from '../archiver/structs/inbox_message.js';
7
+
8
+ export function makeInboxMessage(
9
+ previousRollingHash = Buffer16.ZERO,
10
+ overrides: Partial<InboxMessage> = {},
11
+ ): InboxMessage {
12
+ const { l2BlockNumber = randomBigInt(100n) + 1n } = overrides;
13
+ const { l1BlockNumber = l2BlockNumber } = overrides;
14
+ const { l1BlockHash = Buffer32.random() } = overrides;
15
+ const { leaf = Fr.random() } = overrides;
16
+ const { rollingHash = updateRollingHash(previousRollingHash, leaf) } = overrides;
17
+ const { index = InboxLeaf.smallestIndexFromL2Block(l2BlockNumber) } = overrides;
18
+
19
+ return {
20
+ index,
21
+ leaf,
22
+ l2BlockNumber,
23
+ l1BlockNumber,
24
+ l1BlockHash,
25
+ rollingHash,
26
+ };
27
+ }
28
+
29
+ export function makeInboxMessages(
30
+ count: number,
31
+ opts: {
32
+ initialHash?: Buffer16;
33
+ initialL2BlockNumber?: bigint;
34
+ overrideFn?: (msg: InboxMessage, index: number) => InboxMessage;
35
+ } = {},
36
+ ): InboxMessage[] {
37
+ const { initialHash = Buffer16.ZERO, overrideFn = msg => msg, initialL2BlockNumber = 1n } = opts;
38
+ const messages: InboxMessage[] = [];
39
+ let rollingHash = initialHash;
40
+ for (let i = 0; i < count; i++) {
41
+ const leaf = Fr.random();
42
+ const l2BlockNumber = BigInt(i) + initialL2BlockNumber;
43
+ const message = overrideFn(makeInboxMessage(rollingHash, { leaf, l2BlockNumber }), i);
44
+ rollingHash = message.rollingHash;
45
+ messages.push(message);
46
+ }
47
+
48
+ return messages;
49
+ }
@@ -1,23 +0,0 @@
1
- import type { Fr } from '@aztec/foundation/fields';
2
- import { InboxLeaf } from '@aztec/stdlib/messaging';
3
- /**
4
- * A simple in-memory implementation of an L1 to L2 message store.
5
- */
6
- export declare class L1ToL2MessageStore {
7
- #private;
8
- /**
9
- * A map pointing from a key in a "messageIndex" format to the corresponding L1 to L2 message hash.
10
- */
11
- protected store: Map<string, Fr>;
12
- constructor();
13
- getTotalL1ToL2MessageCount(): bigint;
14
- addMessage(message: InboxLeaf): void;
15
- getMessages(blockNumber: bigint): Fr[];
16
- /**
17
- * Gets the L1 to L2 message index in the L1 to L2 message tree.
18
- * @param l1ToL2Message - The L1 to L2 message.
19
- * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
20
- */
21
- getMessageIndex(l1ToL2Message: Fr): bigint | undefined;
22
- }
23
- //# sourceMappingURL=l1_to_l2_message_store.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"l1_to_l2_message_store.d.ts","sourceRoot":"","sources":["../../../src/archiver/memory_archiver_store/l1_to_l2_message_store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD;;GAEG;AACH,qBAAa,kBAAkB;;IAC7B;;OAEG;IACH,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,CAAa;;IAM7C,0BAA0B,IAAI,MAAM;IAIpC,UAAU,CAAC,OAAO,EAAE,SAAS;IAI7B,WAAW,CAAC,WAAW,EAAE,MAAM,GAAG,EAAE,EAAE;IAsBtC;;;;OAIG;IACH,eAAe,CAAC,aAAa,EAAE,EAAE,GAAG,MAAM,GAAG,SAAS;CAQvD"}
@@ -1,49 +0,0 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
2
- import { InboxLeaf } from '@aztec/stdlib/messaging';
3
- /**
4
- * A simple in-memory implementation of an L1 to L2 message store.
5
- */ export class L1ToL2MessageStore {
6
- /**
7
- * A map pointing from a key in a "messageIndex" format to the corresponding L1 to L2 message hash.
8
- */ store = new Map();
9
- #l1ToL2MessagesSubtreeSize = 2 ** L1_TO_L2_MSG_SUBTREE_HEIGHT;
10
- constructor(){}
11
- getTotalL1ToL2MessageCount() {
12
- return BigInt(this.store.size);
13
- }
14
- addMessage(message) {
15
- this.store.set(`${message.index}`, message.leaf);
16
- }
17
- getMessages(blockNumber) {
18
- const messages = [];
19
- let undefinedMessageFound = false;
20
- const startIndex = Number(InboxLeaf.smallestIndexFromL2Block(blockNumber));
21
- for(let i = startIndex; i < startIndex + this.#l1ToL2MessagesSubtreeSize; i++){
22
- // This is inefficient but probably fine for now.
23
- const message = this.store.get(`${i}`);
24
- if (message) {
25
- if (undefinedMessageFound) {
26
- throw new Error(`L1 to L2 message gap found in block ${blockNumber}`);
27
- }
28
- messages.push(message);
29
- } else {
30
- undefinedMessageFound = true;
31
- // We continue iterating over messages here to verify that there are no more messages after the undefined one.
32
- // --> If this was the case this would imply there is some issue with log fetching.
33
- }
34
- }
35
- return messages;
36
- }
37
- /**
38
- * Gets the L1 to L2 message index in the L1 to L2 message tree.
39
- * @param l1ToL2Message - The L1 to L2 message.
40
- * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
41
- */ getMessageIndex(l1ToL2Message) {
42
- for (const [key, message] of this.store.entries()){
43
- if (message.equals(l1ToL2Message)) {
44
- return BigInt(key);
45
- }
46
- }
47
- return undefined;
48
- }
49
- }
@@ -1,61 +0,0 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
2
- import type { Fr } from '@aztec/foundation/fields';
3
- import { InboxLeaf } from '@aztec/stdlib/messaging';
4
-
5
- /**
6
- * A simple in-memory implementation of an L1 to L2 message store.
7
- */
8
- export class L1ToL2MessageStore {
9
- /**
10
- * A map pointing from a key in a "messageIndex" format to the corresponding L1 to L2 message hash.
11
- */
12
- protected store: Map<string, Fr> = new Map();
13
-
14
- #l1ToL2MessagesSubtreeSize = 2 ** L1_TO_L2_MSG_SUBTREE_HEIGHT;
15
-
16
- constructor() {}
17
-
18
- getTotalL1ToL2MessageCount(): bigint {
19
- return BigInt(this.store.size);
20
- }
21
-
22
- addMessage(message: InboxLeaf) {
23
- this.store.set(`${message.index}`, message.leaf);
24
- }
25
-
26
- getMessages(blockNumber: bigint): Fr[] {
27
- const messages: Fr[] = [];
28
- let undefinedMessageFound = false;
29
- const startIndex = Number(InboxLeaf.smallestIndexFromL2Block(blockNumber));
30
-
31
- for (let i = startIndex; i < startIndex + this.#l1ToL2MessagesSubtreeSize; i++) {
32
- // This is inefficient but probably fine for now.
33
- const message = this.store.get(`${i}`);
34
- if (message) {
35
- if (undefinedMessageFound) {
36
- throw new Error(`L1 to L2 message gap found in block ${blockNumber}`);
37
- }
38
- messages.push(message);
39
- } else {
40
- undefinedMessageFound = true;
41
- // We continue iterating over messages here to verify that there are no more messages after the undefined one.
42
- // --> If this was the case this would imply there is some issue with log fetching.
43
- }
44
- }
45
- return messages;
46
- }
47
-
48
- /**
49
- * Gets the L1 to L2 message index in the L1 to L2 message tree.
50
- * @param l1ToL2Message - The L1 to L2 message.
51
- * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
52
- */
53
- getMessageIndex(l1ToL2Message: Fr): bigint | undefined {
54
- for (const [key, message] of this.store.entries()) {
55
- if (message.equals(l1ToL2Message)) {
56
- return BigInt(key);
57
- }
58
- }
59
- return undefined;
60
- }
61
- }