@aztec/node-lib 4.0.0-nightly.20250907 → 4.0.0-nightly.20260108

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 (37) hide show
  1. package/dest/actions/build-snapshot-metadata.d.ts +1 -1
  2. package/dest/actions/create-backups.d.ts +1 -1
  3. package/dest/actions/index.d.ts +1 -1
  4. package/dest/actions/snapshot-sync.d.ts +4 -3
  5. package/dest/actions/snapshot-sync.d.ts.map +1 -1
  6. package/dest/actions/snapshot-sync.js +91 -56
  7. package/dest/actions/upload-snapshot.d.ts +1 -1
  8. package/dest/config/index.d.ts +7 -3
  9. package/dest/config/index.d.ts.map +1 -1
  10. package/dest/config/index.js +18 -3
  11. package/dest/factories/index.d.ts +2 -0
  12. package/dest/factories/index.d.ts.map +1 -0
  13. package/dest/factories/index.js +1 -0
  14. package/dest/factories/l1_tx_utils.d.ts +79 -0
  15. package/dest/factories/l1_tx_utils.d.ts.map +1 -0
  16. package/dest/factories/l1_tx_utils.js +90 -0
  17. package/dest/metrics/index.d.ts +2 -0
  18. package/dest/metrics/index.d.ts.map +1 -0
  19. package/dest/metrics/index.js +1 -0
  20. package/dest/metrics/l1_tx_metrics.d.ts +29 -0
  21. package/dest/metrics/l1_tx_metrics.d.ts.map +1 -0
  22. package/dest/metrics/l1_tx_metrics.js +138 -0
  23. package/dest/stores/index.d.ts +2 -0
  24. package/dest/stores/index.d.ts.map +1 -0
  25. package/dest/stores/index.js +1 -0
  26. package/dest/stores/l1_tx_store.d.ts +89 -0
  27. package/dest/stores/l1_tx_store.d.ts.map +1 -0
  28. package/dest/stores/l1_tx_store.js +272 -0
  29. package/package.json +30 -23
  30. package/src/actions/snapshot-sync.ts +103 -64
  31. package/src/config/index.ts +27 -5
  32. package/src/factories/index.ts +1 -0
  33. package/src/factories/l1_tx_utils.ts +212 -0
  34. package/src/metrics/index.ts +1 -0
  35. package/src/metrics/l1_tx_metrics.ts +169 -0
  36. package/src/stores/index.ts +1 -0
  37. package/src/stores/l1_tx_store.ts +396 -0
@@ -0,0 +1,396 @@
1
+ import type { IL1TxStore, L1BlobInputs, L1TxConfig, L1TxState } from '@aztec/ethereum/l1-tx-utils';
2
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
3
+ import type { Logger } from '@aztec/foundation/log';
4
+ import { createLogger } from '@aztec/foundation/log';
5
+ import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
6
+
7
+ import type { TransactionReceipt } from 'viem';
8
+
9
+ /**
10
+ * Serializable version of L1TxRequest for storage.
11
+ */
12
+ interface SerializableL1TxRequest {
13
+ to: string | null;
14
+ data?: string;
15
+ value?: string;
16
+ }
17
+
18
+ /**
19
+ * Serializable version of GasPrice for storage.
20
+ */
21
+ interface SerializableGasPrice {
22
+ maxFeePerGas: string;
23
+ maxPriorityFeePerGas: string;
24
+ maxFeePerBlobGas?: string;
25
+ }
26
+
27
+ /**
28
+ * Serializable version of L1TxConfig for storage.
29
+ */
30
+ interface SerializableL1TxConfig {
31
+ gasLimit?: string;
32
+ txTimeoutAt?: number;
33
+ txTimeoutMs?: number;
34
+ checkIntervalMs?: number;
35
+ stallTimeMs?: number;
36
+ priorityFeeRetryBumpPercentage?: number;
37
+ maxSpeedUpAttempts?: number;
38
+ cancelTxOnTimeout?: boolean;
39
+ txCancellationFinalTimeoutMs?: number;
40
+ txUnseenConsideredDroppedMs?: number;
41
+ }
42
+
43
+ /**
44
+ * Serializable version of blob inputs for storage (without the actual blob data).
45
+ */
46
+ interface SerializableBlobMetadata {
47
+ maxFeePerBlobGas?: string;
48
+ }
49
+
50
+ /**
51
+ * Serializable version of L1TxState for storage.
52
+ * Dates and bigints are converted to strings/numbers for JSON serialization.
53
+ * Blob data is NOT included here - it's stored separately.
54
+ */
55
+ interface SerializableL1TxState {
56
+ id: number;
57
+ txHashes: string[];
58
+ cancelTxHashes: string[];
59
+ gasLimit: string;
60
+ gasPrice: SerializableGasPrice;
61
+ txConfigOverrides: SerializableL1TxConfig;
62
+ request: SerializableL1TxRequest;
63
+ status: number;
64
+ nonce: number;
65
+ sentAt: number;
66
+ lastSentAt: number;
67
+ receipt?: TransactionReceipt;
68
+ hasBlobInputs: boolean;
69
+ blobMetadata?: SerializableBlobMetadata;
70
+ }
71
+
72
+ /**
73
+ * Serializable blob inputs for separate storage.
74
+ */
75
+ interface SerializableBlobInputs {
76
+ blobs: string[]; // base64 encoded
77
+ kzg: string; // JSON stringified KZG instance
78
+ }
79
+
80
+ /**
81
+ * Store for persisting L1 transaction states across all L1TxUtils instances.
82
+ * Each state is stored individually with a unique ID, and blobs are stored separately.
83
+ * @remarks This class lives in this package instead of `ethereum` because it depends on `kv-store`.
84
+ */
85
+ export class L1TxStore implements IL1TxStore {
86
+ public static readonly SCHEMA_VERSION = 2;
87
+
88
+ private readonly states: AztecAsyncMap<string, string>; // key: "account-stateId", value: SerializableL1TxState
89
+ private readonly blobs: AztecAsyncMap<string, string>; // key: "account-stateId", value: SerializableBlobInputs
90
+ private readonly stateIdCounter: AztecAsyncMap<string, number>; // key: "account", value: next ID
91
+
92
+ constructor(
93
+ private readonly store: AztecAsyncKVStore,
94
+ private readonly log: Logger = createLogger('l1-tx-utils:store'),
95
+ ) {
96
+ this.states = store.openMap<string, string>('l1_tx_states');
97
+ this.blobs = store.openMap<string, string>('l1_tx_blobs');
98
+ this.stateIdCounter = store.openMap<string, number>('l1_tx_state_id_counter');
99
+ }
100
+
101
+ /**
102
+ * Gets the next available state ID for an account.
103
+ */
104
+ public consumeNextStateId(account: string): Promise<number> {
105
+ return this.store.transactionAsync(async () => {
106
+ const currentId = (await this.stateIdCounter.getAsync(account)) ?? 0;
107
+ const nextId = currentId + 1;
108
+ await this.stateIdCounter.set(account, nextId);
109
+ return nextId;
110
+ });
111
+ }
112
+
113
+ /**
114
+ * Creates a storage key for state/blob data.
115
+ */
116
+ private makeKey(account: string, stateId: number): string {
117
+ return `${account}-${stateId.toString().padStart(10, '0')}`;
118
+ }
119
+
120
+ /**
121
+ * Saves a single transaction state for a specific account.
122
+ * Blobs are not stored here, use saveBlobs instead.
123
+ * @param account - The sender account address
124
+ * @param state - Transaction state to save
125
+ */
126
+ public async saveState(account: string, state: L1TxState): Promise<L1TxState> {
127
+ const key = this.makeKey(account, state.id);
128
+
129
+ const serializable = this.serializeState(state);
130
+ await this.states.set(key, jsonStringify(serializable));
131
+ this.log.debug(`Saved tx state ${state.id} for account ${account} with nonce ${state.nonce}`);
132
+
133
+ return state as L1TxState;
134
+ }
135
+
136
+ /**
137
+ * Saves blobs for a given state.
138
+ * @param account - The sender account address
139
+ * @param stateId - The state ID
140
+ * @param blobInputs - Blob inputs to save
141
+ */
142
+ public async saveBlobs(account: string, stateId: number, blobInputs: L1BlobInputs | undefined): Promise<void> {
143
+ if (!blobInputs) {
144
+ return;
145
+ }
146
+ const key = this.makeKey(account, stateId);
147
+ const blobData = this.serializeBlobInputs(blobInputs);
148
+ await this.blobs.set(key, jsonStringify(blobData));
149
+ this.log.debug(`Saved blobs for state ${stateId} of account ${account}`);
150
+ }
151
+
152
+ /**
153
+ * Loads all transaction states for a specific account.
154
+ * @param account - The sender account address
155
+ * @returns Array of transaction states with their IDs
156
+ */
157
+ public async loadStates(account: string): Promise<L1TxState[]> {
158
+ const states: L1TxState[] = [];
159
+ const prefix = `${account}-`;
160
+
161
+ for await (const [key, stateJson] of this.states.entriesAsync({ start: prefix, end: `${prefix}Z` })) {
162
+ const [keyAccount, stateIdStr] = key.split('-');
163
+ if (keyAccount !== account) {
164
+ throw new Error(`Mismatched account in key: expected ${account} but got ${keyAccount}`);
165
+ }
166
+
167
+ const stateId = parseInt(stateIdStr, 10);
168
+
169
+ try {
170
+ const serialized: SerializableL1TxState = JSON.parse(stateJson);
171
+
172
+ // Load blobs if they exist
173
+ let blobInputs: L1BlobInputs | undefined;
174
+ if (serialized.hasBlobInputs) {
175
+ const blobJson = await this.blobs.getAsync(key);
176
+ if (blobJson) {
177
+ blobInputs = this.deserializeBlobInputs(JSON.parse(blobJson), serialized.blobMetadata);
178
+ }
179
+ }
180
+
181
+ const state = this.deserializeState(serialized, blobInputs);
182
+ states.push({ ...state, id: stateId });
183
+ } catch (err) {
184
+ this.log.error(`Failed to deserialize state ${key}`, err);
185
+ }
186
+ }
187
+
188
+ // Sort by ID
189
+ states.sort((a, b) => a.id - b.id);
190
+
191
+ this.log.debug(`Loaded ${states.length} tx states for account ${account}`);
192
+ return states;
193
+ }
194
+
195
+ /**
196
+ * Loads a single state by ID.
197
+ * @param account - The sender account address
198
+ * @param stateId - The state ID
199
+ * @returns The transaction state or undefined if not found
200
+ */
201
+ public async loadState(account: string, stateId: number): Promise<L1TxState | undefined> {
202
+ const key = this.makeKey(account, stateId);
203
+ const stateJson = await this.states.getAsync(key);
204
+
205
+ if (!stateJson) {
206
+ return undefined;
207
+ }
208
+
209
+ try {
210
+ const serialized: SerializableL1TxState = JSON.parse(stateJson);
211
+
212
+ // Load blobs if they exist
213
+ let blobInputs: L1BlobInputs | undefined;
214
+ if (serialized.hasBlobInputs) {
215
+ const blobJson = await this.blobs.getAsync(key);
216
+ if (blobJson) {
217
+ blobInputs = this.deserializeBlobInputs(JSON.parse(blobJson), serialized.blobMetadata);
218
+ }
219
+ }
220
+
221
+ const state = this.deserializeState(serialized, blobInputs);
222
+ return { ...state, id: stateId };
223
+ } catch (err) {
224
+ this.log.error(`Failed to deserialize state ${key}`, err);
225
+ return undefined;
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Deletes a specific state and its associated blobs.
231
+ * @param account - The sender account address
232
+ * @param stateId - The state ID to delete
233
+ */
234
+ public async deleteState(account: string, ...stateIds: number[]): Promise<void> {
235
+ if (stateIds.length === 0) {
236
+ return;
237
+ }
238
+
239
+ await this.store.transactionAsync(async () => {
240
+ for (const stateId of stateIds) {
241
+ const key = this.makeKey(account, stateId);
242
+ await this.states.delete(key);
243
+ await this.blobs.delete(key);
244
+ }
245
+ });
246
+ }
247
+
248
+ /**
249
+ * Clears all transaction states for a specific account.
250
+ * @param account - The sender account address
251
+ */
252
+ public async clearStates(account: string): Promise<void> {
253
+ await this.store.transactionAsync(async () => {
254
+ const states = await this.loadStates(account);
255
+
256
+ for (const state of states) {
257
+ await this.deleteState(account, state.id);
258
+ }
259
+
260
+ await this.stateIdCounter.delete(account);
261
+ this.log.info(`Cleared all tx states for account ${account}`);
262
+ });
263
+ }
264
+
265
+ /**
266
+ * Gets all accounts that have stored states.
267
+ * @returns Array of account addresses
268
+ */
269
+ public async getAllAccounts(): Promise<string[]> {
270
+ const accounts = new Set<string>();
271
+
272
+ for await (const [key] of this.states.entriesAsync()) {
273
+ const account = key.substring(0, key.lastIndexOf('-'));
274
+ accounts.add(account);
275
+ }
276
+
277
+ return Array.from(accounts);
278
+ }
279
+
280
+ /**
281
+ * Closes the store.
282
+ */
283
+ public async close(): Promise<void> {
284
+ await this.store.close();
285
+ this.log.info('Closed L1 tx state store');
286
+ }
287
+
288
+ /**
289
+ * Serializes an L1TxState for storage.
290
+ */
291
+ private serializeState(state: L1TxState): SerializableL1TxState {
292
+ const txConfigOverrides: SerializableL1TxConfig = {
293
+ ...state.txConfigOverrides,
294
+ gasLimit: state.txConfigOverrides.gasLimit?.toString(),
295
+ txTimeoutAt: state.txConfigOverrides.txTimeoutAt?.getTime(),
296
+ };
297
+
298
+ return {
299
+ id: state.id,
300
+ txHashes: state.txHashes,
301
+ cancelTxHashes: state.cancelTxHashes,
302
+ gasLimit: state.gasLimit.toString(),
303
+ gasPrice: {
304
+ maxFeePerGas: state.gasPrice.maxFeePerGas.toString(),
305
+ maxPriorityFeePerGas: state.gasPrice.maxPriorityFeePerGas.toString(),
306
+ maxFeePerBlobGas: state.gasPrice.maxFeePerBlobGas?.toString(),
307
+ },
308
+ txConfigOverrides,
309
+ request: {
310
+ ...state.request,
311
+ value: state.request.value?.toString(),
312
+ },
313
+ status: state.status,
314
+ nonce: state.nonce,
315
+ sentAt: state.sentAtL1Ts.getTime(),
316
+ lastSentAt: state.lastSentAtL1Ts.getTime(),
317
+ receipt: state.receipt,
318
+ hasBlobInputs: state.blobInputs !== undefined,
319
+ blobMetadata: state.blobInputs?.maxFeePerBlobGas
320
+ ? { maxFeePerBlobGas: state.blobInputs.maxFeePerBlobGas.toString() }
321
+ : undefined,
322
+ };
323
+ }
324
+
325
+ /**
326
+ * Deserializes a stored state back to L1TxState.
327
+ */
328
+ private deserializeState(stored: SerializableL1TxState, blobInputs?: L1BlobInputs): L1TxState {
329
+ const txConfigOverrides: L1TxConfig = {
330
+ ...stored.txConfigOverrides,
331
+ gasLimit: stored.txConfigOverrides.gasLimit !== undefined ? BigInt(stored.txConfigOverrides.gasLimit) : undefined,
332
+ txTimeoutAt:
333
+ stored.txConfigOverrides.txTimeoutAt !== undefined ? new Date(stored.txConfigOverrides.txTimeoutAt) : undefined,
334
+ };
335
+
336
+ const receipt = stored.receipt
337
+ ? {
338
+ ...stored.receipt,
339
+ blockNumber: BigInt(stored.receipt.blockNumber),
340
+ cumulativeGasUsed: BigInt(stored.receipt.cumulativeGasUsed),
341
+ effectiveGasPrice: BigInt(stored.receipt.effectiveGasPrice),
342
+ gasUsed: BigInt(stored.receipt.gasUsed),
343
+ }
344
+ : undefined;
345
+
346
+ return {
347
+ id: stored.id,
348
+ txHashes: stored.txHashes as `0x${string}`[],
349
+ cancelTxHashes: stored.cancelTxHashes as `0x${string}`[],
350
+ gasLimit: BigInt(stored.gasLimit),
351
+ gasPrice: {
352
+ maxFeePerGas: BigInt(stored.gasPrice.maxFeePerGas),
353
+ maxPriorityFeePerGas: BigInt(stored.gasPrice.maxPriorityFeePerGas),
354
+ maxFeePerBlobGas: stored.gasPrice.maxFeePerBlobGas ? BigInt(stored.gasPrice.maxFeePerBlobGas) : undefined,
355
+ },
356
+ txConfigOverrides,
357
+ request: {
358
+ to: stored.request.to as `0x${string}` | null,
359
+ data: stored.request.data as `0x${string}` | undefined,
360
+ value: stored.request.value ? BigInt(stored.request.value) : undefined,
361
+ },
362
+ status: stored.status,
363
+ nonce: stored.nonce,
364
+ sentAtL1Ts: new Date(stored.sentAt),
365
+ lastSentAtL1Ts: new Date(stored.lastSentAt),
366
+ receipt,
367
+ blobInputs,
368
+ };
369
+ }
370
+
371
+ /**
372
+ * Serializes blob inputs for separate storage.
373
+ */
374
+ private serializeBlobInputs(blobInputs: L1BlobInputs): SerializableBlobInputs {
375
+ return {
376
+ blobs: blobInputs.blobs.map(b => Buffer.from(b).toString('base64')),
377
+ kzg: jsonStringify(blobInputs.kzg),
378
+ };
379
+ }
380
+
381
+ /**
382
+ * Deserializes blob inputs from storage, combining blob data with metadata.
383
+ */
384
+ private deserializeBlobInputs(stored: SerializableBlobInputs, metadata?: SerializableBlobMetadata): L1BlobInputs {
385
+ const blobInputs: L1BlobInputs = {
386
+ blobs: stored.blobs.map(b => new Uint8Array(Buffer.from(b, 'base64'))),
387
+ kzg: JSON.parse(stored.kzg),
388
+ };
389
+
390
+ if (metadata?.maxFeePerBlobGas) {
391
+ blobInputs.maxFeePerBlobGas = BigInt(metadata.maxFeePerBlobGas);
392
+ }
393
+
394
+ return blobInputs;
395
+ }
396
+ }