@aztec/node-lib 0.0.1-commit.001888fc

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 (50) hide show
  1. package/README.md +3 -0
  2. package/dest/actions/build-snapshot-metadata.d.ts +5 -0
  3. package/dest/actions/build-snapshot-metadata.d.ts.map +1 -0
  4. package/dest/actions/build-snapshot-metadata.js +19 -0
  5. package/dest/actions/create-backups.d.ts +6 -0
  6. package/dest/actions/create-backups.d.ts.map +1 -0
  7. package/dest/actions/create-backups.js +32 -0
  8. package/dest/actions/index.d.ts +5 -0
  9. package/dest/actions/index.d.ts.map +1 -0
  10. package/dest/actions/index.js +4 -0
  11. package/dest/actions/snapshot-sync.d.ts +28 -0
  12. package/dest/actions/snapshot-sync.d.ts.map +1 -0
  13. package/dest/actions/snapshot-sync.js +236 -0
  14. package/dest/actions/upload-snapshot.d.ts +12 -0
  15. package/dest/actions/upload-snapshot.d.ts.map +1 -0
  16. package/dest/actions/upload-snapshot.js +38 -0
  17. package/dest/config/index.d.ts +23 -0
  18. package/dest/config/index.d.ts.map +1 -0
  19. package/dest/config/index.js +53 -0
  20. package/dest/factories/index.d.ts +2 -0
  21. package/dest/factories/index.d.ts.map +1 -0
  22. package/dest/factories/index.js +1 -0
  23. package/dest/factories/l1_tx_utils.d.ts +66 -0
  24. package/dest/factories/l1_tx_utils.d.ts.map +1 -0
  25. package/dest/factories/l1_tx_utils.js +94 -0
  26. package/dest/metrics/index.d.ts +2 -0
  27. package/dest/metrics/index.d.ts.map +1 -0
  28. package/dest/metrics/index.js +1 -0
  29. package/dest/metrics/l1_tx_metrics.d.ts +29 -0
  30. package/dest/metrics/l1_tx_metrics.d.ts.map +1 -0
  31. package/dest/metrics/l1_tx_metrics.js +111 -0
  32. package/dest/stores/index.d.ts +2 -0
  33. package/dest/stores/index.d.ts.map +1 -0
  34. package/dest/stores/index.js +1 -0
  35. package/dest/stores/l1_tx_store.d.ts +89 -0
  36. package/dest/stores/l1_tx_store.d.ts.map +1 -0
  37. package/dest/stores/l1_tx_store.js +272 -0
  38. package/package.json +110 -0
  39. package/src/actions/build-snapshot-metadata.ts +29 -0
  40. package/src/actions/create-backups.ts +41 -0
  41. package/src/actions/index.ts +4 -0
  42. package/src/actions/snapshot-sync.ts +284 -0
  43. package/src/actions/upload-snapshot.ts +50 -0
  44. package/src/config/index.ts +85 -0
  45. package/src/factories/index.ts +1 -0
  46. package/src/factories/l1_tx_utils.ts +156 -0
  47. package/src/metrics/index.ts +1 -0
  48. package/src/metrics/l1_tx_metrics.ts +138 -0
  49. package/src/stores/index.ts +1 -0
  50. package/src/stores/l1_tx_store.ts +396 -0
@@ -0,0 +1,94 @@
1
+ import { createDelayer, createL1TxUtils as createL1TxUtilsBase } from '@aztec/ethereum/l1-tx-utils';
2
+ import { createForwarderL1TxUtils as createForwarderL1TxUtilsBase } from '@aztec/ethereum/l1-tx-utils-with-blobs';
3
+ import { omit } from '@aztec/foundation/collection';
4
+ import { createLogger } from '@aztec/foundation/log';
5
+ import { createStore } from '@aztec/kv-store/lmdb-v2';
6
+ import { L1TxMetrics } from '../metrics/l1_tx_metrics.js';
7
+ import { L1TxStore } from '../stores/l1_tx_store.js';
8
+ const L1_TX_STORE_NAME = 'l1-tx-utils';
9
+ /**
10
+ * Creates shared dependencies (logger, store, metrics, delayer) for L1TxUtils instances.
11
+ * When enableDelayer is set in config, a single shared delayer is created and passed to all instances.
12
+ */ async function createSharedDeps(config, deps) {
13
+ const logger = deps.logger ?? createLogger('l1-tx-utils');
14
+ // Note that we do NOT bind them to the rollup address, since we still need to
15
+ // monitor and cancel txs for previous rollups to free up our nonces.
16
+ const noRollupConfig = omit(config, 'l1Contracts');
17
+ const kvStore = await createStore(L1_TX_STORE_NAME, L1TxStore.SCHEMA_VERSION, noRollupConfig, logger.getBindings());
18
+ const store = new L1TxStore(kvStore, logger);
19
+ const meter = deps.telemetry.getMeter('L1TxUtils');
20
+ const metrics = new L1TxMetrics(meter, config.scope ?? 'other', logger);
21
+ // Create a single shared delayer for all L1TxUtils instances in this group
22
+ const delayer = config.enableDelayer && config.ethereumSlotDuration !== undefined ? createDelayer(deps.dateProvider, {
23
+ ethereumSlotDuration: config.ethereumSlotDuration
24
+ }, logger.getBindings()) : undefined;
25
+ return {
26
+ logger,
27
+ store,
28
+ metrics,
29
+ dateProvider: deps.dateProvider,
30
+ delayer
31
+ };
32
+ }
33
+ /**
34
+ * Creates L1TxUtils from multiple Viem wallet clients, sharing store, metrics, and delayer.
35
+ * When kzg is provided in deps, blob support is enabled.
36
+ */ export async function createL1TxUtilsFromWallets(clients, config, deps) {
37
+ const sharedDeps = await createSharedDeps(config, deps);
38
+ return clients.map((client)=>createL1TxUtilsBase(client, {
39
+ ...sharedDeps,
40
+ kzg: deps.kzg
41
+ }, config));
42
+ }
43
+ /**
44
+ * Creates L1TxUtils from multiple EthSigners, sharing store, metrics, and delayer.
45
+ * When kzg is provided in deps, blob support is enabled.
46
+ * Deduplicates signers by address to avoid creating multiple instances for the same publisher.
47
+ */ export async function createL1TxUtilsFromSigners(client, signers, config, deps) {
48
+ const sharedDeps = await createSharedDeps(config, deps);
49
+ // Deduplicate signers by address to avoid creating multiple L1TxUtils instances
50
+ // for the same publisher address (e.g., when multiple attesters share the same publisher key)
51
+ const signersByAddress = new Map();
52
+ for (const signer of signers){
53
+ const addressKey = signer.address.toString().toLowerCase();
54
+ if (!signersByAddress.has(addressKey)) {
55
+ signersByAddress.set(addressKey, signer);
56
+ }
57
+ }
58
+ const uniqueSigners = Array.from(signersByAddress.values());
59
+ if (uniqueSigners.length < signers.length) {
60
+ sharedDeps.logger.info(`Deduplicated ${signers.length} signers to ${uniqueSigners.length} unique publisher addresses`);
61
+ }
62
+ return uniqueSigners.map((signer)=>createL1TxUtilsBase({
63
+ client,
64
+ signer
65
+ }, {
66
+ ...sharedDeps,
67
+ kzg: deps.kzg
68
+ }, config));
69
+ }
70
+ /**
71
+ * Creates ForwarderL1TxUtils from multiple Viem wallet clients, sharing store, metrics, and delayer.
72
+ * Wraps all transactions through a forwarder contract for testing purposes.
73
+ * When kzg is provided in deps, blob support is enabled.
74
+ */ export async function createForwarderL1TxUtilsFromWallets(clients, forwarderAddress, config, deps) {
75
+ const sharedDeps = await createSharedDeps(config, deps);
76
+ return clients.map((client)=>createForwarderL1TxUtilsBase(client, forwarderAddress, {
77
+ ...sharedDeps,
78
+ kzg: deps.kzg
79
+ }, config));
80
+ }
81
+ /**
82
+ * Creates ForwarderL1TxUtils from multiple EthSigners, sharing store, metrics, and delayer.
83
+ * Wraps all transactions through a forwarder contract for testing purposes.
84
+ * When kzg is provided in deps, blob support is enabled.
85
+ */ export async function createForwarderL1TxUtilsFromSigners(client, signers, forwarderAddress, config, deps) {
86
+ const sharedDeps = await createSharedDeps(config, deps);
87
+ return signers.map((signer)=>createForwarderL1TxUtilsBase({
88
+ client,
89
+ signer
90
+ }, forwarderAddress, {
91
+ ...sharedDeps,
92
+ kzg: deps.kzg
93
+ }, config));
94
+ }
@@ -0,0 +1,2 @@
1
+ export * from './l1_tx_metrics.js';
2
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9tZXRyaWNzL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQWMsb0JBQW9CLENBQUMifQ==
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/metrics/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './l1_tx_metrics.js';
@@ -0,0 +1,29 @@
1
+ import type { IL1TxMetrics, L1TxState } from '@aztec/ethereum/l1-tx-utils';
2
+ import { type Meter } from '@aztec/telemetry-client';
3
+ export type L1TxScope = 'sequencer' | 'prover' | 'other';
4
+ /**
5
+ * Metrics for L1 transaction utils tracking tx lifecycle and gas costs.
6
+ */
7
+ export declare class L1TxMetrics implements IL1TxMetrics {
8
+ private meter;
9
+ private scope;
10
+ private logger;
11
+ private txMinedDuration;
12
+ private txAttemptsUntilMined;
13
+ private txMinedCount;
14
+ private txRevertedCount;
15
+ private txCancelledCount;
16
+ private txNotMinedCount;
17
+ private maxPriorityFeeHistogram;
18
+ private maxFeeHistogram;
19
+ private blobFeeHistogram;
20
+ constructor(meter: Meter, scope?: L1TxScope, logger?: import("@aztec/foundation/log").Logger);
21
+ /**
22
+ * Records metrics when a transaction is mined.
23
+ * @param state - The L1 transaction state
24
+ * @param l1Timestamp - The current L1 timestamp
25
+ */
26
+ recordMinedTx(state: L1TxState, l1Timestamp: Date): void;
27
+ recordDroppedTx(state: L1TxState): void;
28
+ }
29
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibDFfdHhfbWV0cmljcy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL21ldHJpY3MvbDFfdHhfbWV0cmljcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxZQUFZLEVBQUUsU0FBUyxFQUFFLE1BQU0sNkJBQTZCLENBQUM7QUFHM0UsT0FBTyxFQUdMLEtBQUssS0FBSyxFQUlYLE1BQU0seUJBQXlCLENBQUM7QUFFakMsTUFBTSxNQUFNLFNBQVMsR0FBRyxXQUFXLEdBQUcsUUFBUSxHQUFHLE9BQU8sQ0FBQztBQUV6RDs7R0FFRztBQUNILHFCQUFhLFdBQVksWUFBVyxZQUFZO0lBbUI1QyxPQUFPLENBQUMsS0FBSztJQUNiLE9BQU8sQ0FBQyxLQUFLO0lBQ2IsT0FBTyxDQUFDLE1BQU07SUFuQmhCLE9BQU8sQ0FBQyxlQUFlLENBQVk7SUFHbkMsT0FBTyxDQUFDLG9CQUFvQixDQUFZO0lBR3hDLE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CAAC,eAAe,CAAgB;IAGvC,OAAO,CAAC,uBAAuB,CAAY;IAC3C,OAAO,CAAC,eAAe,CAAY;IACnC,OAAO,CAAC,gBAAgB,CAAY;IAEpC,YACU,KAAK,EAAE,KAAK,EACZ,KAAK,GAAE,SAAmB,EAC1B,MAAM,yCAAsC,EAoBrD;IAED;;;;OAIG;IACI,aAAa,CAAC,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,IAAI,GAAG,IAAI,CAoD9D;IAEM,eAAe,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAiB7C;CACF"}
@@ -0,0 +1,111 @@
1
+ import { TxUtilsState } from '@aztec/ethereum/l1-tx-utils';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import { Attributes, Metrics, createUpDownCounterWithDefault } from '@aztec/telemetry-client';
4
+ /**
5
+ * Metrics for L1 transaction utils tracking tx lifecycle and gas costs.
6
+ */ export class L1TxMetrics {
7
+ meter;
8
+ scope;
9
+ logger;
10
+ // Time until tx is mined
11
+ txMinedDuration;
12
+ // Number of attempts until mined
13
+ txAttemptsUntilMined;
14
+ // Counters for end states
15
+ txMinedCount;
16
+ txRevertedCount;
17
+ txCancelledCount;
18
+ txNotMinedCount;
19
+ // Gas price histograms (at end state, in wei)
20
+ maxPriorityFeeHistogram;
21
+ maxFeeHistogram;
22
+ blobFeeHistogram;
23
+ constructor(meter, scope = 'other', logger = createLogger('l1-tx-utils:metrics')){
24
+ this.meter = meter;
25
+ this.scope = scope;
26
+ this.logger = logger;
27
+ this.txMinedDuration = this.meter.createHistogram(Metrics.L1_TX_MINED_DURATION);
28
+ this.txAttemptsUntilMined = this.meter.createHistogram(Metrics.L1_TX_ATTEMPTS_UNTIL_MINED);
29
+ const scopeAttributes = [
30
+ {
31
+ [Attributes.L1_TX_SCOPE]: this.scope
32
+ }
33
+ ];
34
+ this.txMinedCount = createUpDownCounterWithDefault(this.meter, Metrics.L1_TX_MINED_COUNT, scopeAttributes);
35
+ this.txRevertedCount = createUpDownCounterWithDefault(this.meter, Metrics.L1_TX_REVERTED_COUNT, scopeAttributes);
36
+ this.txCancelledCount = createUpDownCounterWithDefault(this.meter, Metrics.L1_TX_CANCELLED_COUNT, scopeAttributes);
37
+ this.txNotMinedCount = createUpDownCounterWithDefault(this.meter, Metrics.L1_TX_NOT_MINED_COUNT, scopeAttributes);
38
+ this.maxPriorityFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_MAX_PRIORITY_FEE);
39
+ this.maxFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_MAX_FEE);
40
+ this.blobFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_BLOB_FEE);
41
+ }
42
+ /**
43
+ * Records metrics when a transaction is mined.
44
+ * @param state - The L1 transaction state
45
+ * @param l1Timestamp - The current L1 timestamp
46
+ */ recordMinedTx(state, l1Timestamp) {
47
+ if (state.status !== TxUtilsState.MINED) {
48
+ this.logger.warn(`Attempted to record mined tx metrics for a tx not in MINED state (state: ${TxUtilsState[state.status]})`, {
49
+ scope: this.scope,
50
+ nonce: state.nonce
51
+ });
52
+ return;
53
+ }
54
+ const attributes = {
55
+ [Attributes.L1_TX_SCOPE]: this.scope
56
+ };
57
+ const isCancelTx = state.cancelTxHashes.length > 0;
58
+ const isReverted = state.receipt?.status === 'reverted';
59
+ if (isCancelTx) {
60
+ this.txCancelledCount.add(1, attributes);
61
+ } else if (isReverted) {
62
+ this.txRevertedCount.add(1, attributes);
63
+ } else {
64
+ this.txMinedCount.add(1, attributes);
65
+ }
66
+ // Record time to mine using provided L1 timestamp
67
+ const duration = Math.floor((l1Timestamp.getTime() - state.sentAtL1Ts.getTime()) / 1000);
68
+ this.txMinedDuration.record(duration, attributes);
69
+ // Record number of attempts until mined
70
+ const attempts = isCancelTx ? state.cancelTxHashes.length : state.txHashes.length;
71
+ this.txAttemptsUntilMined.record(attempts, attributes);
72
+ // Record gas prices at end state (in wei as integers)
73
+ const maxPriorityFeeWei = Number(state.gasPrice.maxPriorityFeePerGas);
74
+ const maxFeeWei = Number(state.gasPrice.maxFeePerGas);
75
+ const blobFeeWei = state.gasPrice.maxFeePerBlobGas ? Number(state.gasPrice.maxFeePerBlobGas) : undefined;
76
+ this.maxPriorityFeeHistogram.record(maxPriorityFeeWei, attributes);
77
+ this.maxFeeHistogram.record(maxFeeWei, attributes);
78
+ // Record blob fee if present (in wei as integer)
79
+ if (blobFeeWei !== undefined) {
80
+ this.blobFeeHistogram.record(blobFeeWei, attributes);
81
+ }
82
+ this.logger.debug(`Recorded tx end state metrics`, {
83
+ status: TxUtilsState[state.status],
84
+ nonce: state.nonce,
85
+ isCancelTx,
86
+ isReverted,
87
+ scope: this.scope,
88
+ maxPriorityFeeWei,
89
+ maxFeeWei,
90
+ blobFeeWei
91
+ });
92
+ }
93
+ recordDroppedTx(state) {
94
+ if (state.status !== TxUtilsState.NOT_MINED) {
95
+ this.logger.warn(`Attempted to record dropped tx metrics for a tx not in NOT_MINED state (state: ${TxUtilsState[state.status]})`, {
96
+ scope: this.scope,
97
+ nonce: state.nonce
98
+ });
99
+ return;
100
+ }
101
+ const attributes = {
102
+ [Attributes.L1_TX_SCOPE]: this.scope
103
+ };
104
+ this.txNotMinedCount.add(1, attributes);
105
+ this.logger.debug(`Recorded tx dropped metrics`, {
106
+ status: TxUtilsState[state.status],
107
+ nonce: state.nonce,
108
+ scope: this.scope
109
+ });
110
+ }
111
+ }
@@ -0,0 +1,2 @@
1
+ export * from './l1_tx_store.js';
2
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zdG9yZXMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsY0FBYyxrQkFBa0IsQ0FBQyJ9
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/stores/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './l1_tx_store.js';
@@ -0,0 +1,89 @@
1
+ import type { IL1TxStore, L1BlobInputs, L1TxState } from '@aztec/ethereum/l1-tx-utils';
2
+ import type { Logger } from '@aztec/foundation/log';
3
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
4
+ /**
5
+ * Store for persisting L1 transaction states across all L1TxUtils instances.
6
+ * Each state is stored individually with a unique ID, and blobs are stored separately.
7
+ * @remarks This class lives in this package instead of `ethereum` because it depends on `kv-store`.
8
+ */
9
+ export declare class L1TxStore implements IL1TxStore {
10
+ private readonly store;
11
+ private readonly log;
12
+ static readonly SCHEMA_VERSION = 2;
13
+ private readonly states;
14
+ private readonly blobs;
15
+ private readonly stateIdCounter;
16
+ constructor(store: AztecAsyncKVStore, log?: Logger);
17
+ /**
18
+ * Gets the next available state ID for an account.
19
+ */
20
+ consumeNextStateId(account: string): Promise<number>;
21
+ /**
22
+ * Creates a storage key for state/blob data.
23
+ */
24
+ private makeKey;
25
+ /**
26
+ * Saves a single transaction state for a specific account.
27
+ * Blobs are not stored here, use saveBlobs instead.
28
+ * @param account - The sender account address
29
+ * @param state - Transaction state to save
30
+ */
31
+ saveState(account: string, state: L1TxState): Promise<L1TxState>;
32
+ /**
33
+ * Saves blobs for a given state.
34
+ * @param account - The sender account address
35
+ * @param stateId - The state ID
36
+ * @param blobInputs - Blob inputs to save
37
+ */
38
+ saveBlobs(account: string, stateId: number, blobInputs: L1BlobInputs | undefined): Promise<void>;
39
+ /**
40
+ * Loads all transaction states for a specific account.
41
+ * @param account - The sender account address
42
+ * @returns Array of transaction states with their IDs
43
+ */
44
+ loadStates(account: string): Promise<L1TxState[]>;
45
+ /**
46
+ * Loads a single state by ID.
47
+ * @param account - The sender account address
48
+ * @param stateId - The state ID
49
+ * @returns The transaction state or undefined if not found
50
+ */
51
+ loadState(account: string, stateId: number): Promise<L1TxState | undefined>;
52
+ /**
53
+ * Deletes a specific state and its associated blobs.
54
+ * @param account - The sender account address
55
+ * @param stateId - The state ID to delete
56
+ */
57
+ deleteState(account: string, ...stateIds: number[]): Promise<void>;
58
+ /**
59
+ * Clears all transaction states for a specific account.
60
+ * @param account - The sender account address
61
+ */
62
+ clearStates(account: string): Promise<void>;
63
+ /**
64
+ * Gets all accounts that have stored states.
65
+ * @returns Array of account addresses
66
+ */
67
+ getAllAccounts(): Promise<string[]>;
68
+ /**
69
+ * Closes the store.
70
+ */
71
+ close(): Promise<void>;
72
+ /**
73
+ * Serializes an L1TxState for storage.
74
+ */
75
+ private serializeState;
76
+ /**
77
+ * Deserializes a stored state back to L1TxState.
78
+ */
79
+ private deserializeState;
80
+ /**
81
+ * Serializes blob inputs for separate storage.
82
+ */
83
+ private serializeBlobInputs;
84
+ /**
85
+ * Deserializes blob inputs from storage, combining blob data with metadata.
86
+ */
87
+ private deserializeBlobInputs;
88
+ }
89
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibDFfdHhfc3RvcmUuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zdG9yZXMvbDFfdHhfc3RvcmUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsVUFBVSxFQUFFLFlBQVksRUFBYyxTQUFTLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUVuRyxPQUFPLEtBQUssRUFBRSxNQUFNLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUVwRCxPQUFPLEtBQUssRUFBRSxpQkFBaUIsRUFBaUIsTUFBTSxpQkFBaUIsQ0FBQztBQTJFeEU7Ozs7R0FJRztBQUNILHFCQUFhLFNBQVUsWUFBVyxVQUFVO0lBUXhDLE9BQU8sQ0FBQyxRQUFRLENBQUMsS0FBSztJQUN0QixPQUFPLENBQUMsUUFBUSxDQUFDLEdBQUc7SUFSdEIsZ0JBQXVCLGNB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AAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAO1D;IAED;;OAEG;IACH,OAAO,CAAC,OAAO;IAIf;;;;;OAKG;IACU,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAQ5E;IAED;;;;;OAKG;IACU,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,YAAY,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAQ5G;IAED;;;;OAIG;IACU,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC,CAoC7D;IAED;;;;;OAKG;IACU,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC,CA0BvF;IAED;;;;OAIG;IACU,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAY9E;IAED;;;OAGG;IACU,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAWvD;IAED;;;OAGG;IACU,cAAc,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAS/C;IAED;;OAEG;IACU,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAGlC;IAED;;OAEG;IACH,OAAO,CAAC,cAAc;IAkCtB;;OAEG;IACH,OAAO,CAAC,gBAAgB;IA2CxB;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAO3B;;OAEG;IACH,OAAO,CAAC,qBAAqB;CAY9B"}
@@ -0,0 +1,272 @@
1
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ /**
4
+ * Store for persisting L1 transaction states across all L1TxUtils instances.
5
+ * Each state is stored individually with a unique ID, and blobs are stored separately.
6
+ * @remarks This class lives in this package instead of `ethereum` because it depends on `kv-store`.
7
+ */ export class L1TxStore {
8
+ store;
9
+ log;
10
+ static SCHEMA_VERSION = 2;
11
+ states;
12
+ blobs;
13
+ stateIdCounter;
14
+ constructor(store, log = createLogger('l1-tx-utils:store')){
15
+ this.store = store;
16
+ this.log = log;
17
+ this.states = store.openMap('l1_tx_states');
18
+ this.blobs = store.openMap('l1_tx_blobs');
19
+ this.stateIdCounter = store.openMap('l1_tx_state_id_counter');
20
+ }
21
+ /**
22
+ * Gets the next available state ID for an account.
23
+ */ consumeNextStateId(account) {
24
+ return this.store.transactionAsync(async ()=>{
25
+ const currentId = await this.stateIdCounter.getAsync(account) ?? 0;
26
+ const nextId = currentId + 1;
27
+ await this.stateIdCounter.set(account, nextId);
28
+ return nextId;
29
+ });
30
+ }
31
+ /**
32
+ * Creates a storage key for state/blob data.
33
+ */ makeKey(account, stateId) {
34
+ return `${account}-${stateId.toString().padStart(10, '0')}`;
35
+ }
36
+ /**
37
+ * Saves a single transaction state for a specific account.
38
+ * Blobs are not stored here, use saveBlobs instead.
39
+ * @param account - The sender account address
40
+ * @param state - Transaction state to save
41
+ */ async saveState(account, state) {
42
+ const key = this.makeKey(account, state.id);
43
+ const serializable = this.serializeState(state);
44
+ await this.states.set(key, jsonStringify(serializable));
45
+ this.log.debug(`Saved tx state ${state.id} for account ${account} with nonce ${state.nonce}`);
46
+ return state;
47
+ }
48
+ /**
49
+ * Saves blobs for a given state.
50
+ * @param account - The sender account address
51
+ * @param stateId - The state ID
52
+ * @param blobInputs - Blob inputs to save
53
+ */ async saveBlobs(account, stateId, blobInputs) {
54
+ if (!blobInputs) {
55
+ return;
56
+ }
57
+ const key = this.makeKey(account, stateId);
58
+ const blobData = this.serializeBlobInputs(blobInputs);
59
+ await this.blobs.set(key, jsonStringify(blobData));
60
+ this.log.debug(`Saved blobs for state ${stateId} of account ${account}`);
61
+ }
62
+ /**
63
+ * Loads all transaction states for a specific account.
64
+ * @param account - The sender account address
65
+ * @returns Array of transaction states with their IDs
66
+ */ async loadStates(account) {
67
+ const states = [];
68
+ const prefix = `${account}-`;
69
+ for await (const [key, stateJson] of this.states.entriesAsync({
70
+ start: prefix,
71
+ end: `${prefix}Z`
72
+ })){
73
+ const [keyAccount, stateIdStr] = key.split('-');
74
+ if (keyAccount !== account) {
75
+ throw new Error(`Mismatched account in key: expected ${account} but got ${keyAccount}`);
76
+ }
77
+ const stateId = parseInt(stateIdStr, 10);
78
+ try {
79
+ const serialized = JSON.parse(stateJson);
80
+ // Load blobs if they exist
81
+ let blobInputs;
82
+ if (serialized.hasBlobInputs) {
83
+ const blobJson = await this.blobs.getAsync(key);
84
+ if (blobJson) {
85
+ blobInputs = this.deserializeBlobInputs(JSON.parse(blobJson), serialized.blobMetadata);
86
+ }
87
+ }
88
+ const state = this.deserializeState(serialized, blobInputs);
89
+ states.push({
90
+ ...state,
91
+ id: stateId
92
+ });
93
+ } catch (err) {
94
+ this.log.error(`Failed to deserialize state ${key}`, err);
95
+ }
96
+ }
97
+ // Sort by ID
98
+ states.sort((a, b)=>a.id - b.id);
99
+ this.log.debug(`Loaded ${states.length} tx states for account ${account}`);
100
+ return states;
101
+ }
102
+ /**
103
+ * Loads a single state by ID.
104
+ * @param account - The sender account address
105
+ * @param stateId - The state ID
106
+ * @returns The transaction state or undefined if not found
107
+ */ async loadState(account, stateId) {
108
+ const key = this.makeKey(account, stateId);
109
+ const stateJson = await this.states.getAsync(key);
110
+ if (!stateJson) {
111
+ return undefined;
112
+ }
113
+ try {
114
+ const serialized = JSON.parse(stateJson);
115
+ // Load blobs if they exist
116
+ let blobInputs;
117
+ if (serialized.hasBlobInputs) {
118
+ const blobJson = await this.blobs.getAsync(key);
119
+ if (blobJson) {
120
+ blobInputs = this.deserializeBlobInputs(JSON.parse(blobJson), serialized.blobMetadata);
121
+ }
122
+ }
123
+ const state = this.deserializeState(serialized, blobInputs);
124
+ return {
125
+ ...state,
126
+ id: stateId
127
+ };
128
+ } catch (err) {
129
+ this.log.error(`Failed to deserialize state ${key}`, err);
130
+ return undefined;
131
+ }
132
+ }
133
+ /**
134
+ * Deletes a specific state and its associated blobs.
135
+ * @param account - The sender account address
136
+ * @param stateId - The state ID to delete
137
+ */ async deleteState(account, ...stateIds) {
138
+ if (stateIds.length === 0) {
139
+ return;
140
+ }
141
+ await this.store.transactionAsync(async ()=>{
142
+ for (const stateId of stateIds){
143
+ const key = this.makeKey(account, stateId);
144
+ await this.states.delete(key);
145
+ await this.blobs.delete(key);
146
+ }
147
+ });
148
+ }
149
+ /**
150
+ * Clears all transaction states for a specific account.
151
+ * @param account - The sender account address
152
+ */ async clearStates(account) {
153
+ await this.store.transactionAsync(async ()=>{
154
+ const states = await this.loadStates(account);
155
+ for (const state of states){
156
+ await this.deleteState(account, state.id);
157
+ }
158
+ await this.stateIdCounter.delete(account);
159
+ this.log.info(`Cleared all tx states for account ${account}`);
160
+ });
161
+ }
162
+ /**
163
+ * Gets all accounts that have stored states.
164
+ * @returns Array of account addresses
165
+ */ async getAllAccounts() {
166
+ const accounts = new Set();
167
+ for await (const [key] of this.states.entriesAsync()){
168
+ const account = key.substring(0, key.lastIndexOf('-'));
169
+ accounts.add(account);
170
+ }
171
+ return Array.from(accounts);
172
+ }
173
+ /**
174
+ * Closes the store.
175
+ */ async close() {
176
+ await this.store.close();
177
+ this.log.info('Closed L1 tx state store');
178
+ }
179
+ /**
180
+ * Serializes an L1TxState for storage.
181
+ */ serializeState(state) {
182
+ const txConfigOverrides = {
183
+ ...state.txConfigOverrides,
184
+ gasLimit: state.txConfigOverrides.gasLimit?.toString(),
185
+ txTimeoutAt: state.txConfigOverrides.txTimeoutAt?.getTime()
186
+ };
187
+ return {
188
+ id: state.id,
189
+ txHashes: state.txHashes,
190
+ cancelTxHashes: state.cancelTxHashes,
191
+ gasLimit: state.gasLimit.toString(),
192
+ gasPrice: {
193
+ maxFeePerGas: state.gasPrice.maxFeePerGas.toString(),
194
+ maxPriorityFeePerGas: state.gasPrice.maxPriorityFeePerGas.toString(),
195
+ maxFeePerBlobGas: state.gasPrice.maxFeePerBlobGas?.toString()
196
+ },
197
+ txConfigOverrides,
198
+ request: {
199
+ ...state.request,
200
+ value: state.request.value?.toString()
201
+ },
202
+ status: state.status,
203
+ nonce: state.nonce,
204
+ sentAt: state.sentAtL1Ts.getTime(),
205
+ lastSentAt: state.lastSentAtL1Ts.getTime(),
206
+ receipt: state.receipt,
207
+ hasBlobInputs: state.blobInputs !== undefined,
208
+ blobMetadata: state.blobInputs?.maxFeePerBlobGas ? {
209
+ maxFeePerBlobGas: state.blobInputs.maxFeePerBlobGas.toString()
210
+ } : undefined
211
+ };
212
+ }
213
+ /**
214
+ * Deserializes a stored state back to L1TxState.
215
+ */ deserializeState(stored, blobInputs) {
216
+ const txConfigOverrides = {
217
+ ...stored.txConfigOverrides,
218
+ gasLimit: stored.txConfigOverrides.gasLimit !== undefined ? BigInt(stored.txConfigOverrides.gasLimit) : undefined,
219
+ txTimeoutAt: stored.txConfigOverrides.txTimeoutAt !== undefined ? new Date(stored.txConfigOverrides.txTimeoutAt) : undefined
220
+ };
221
+ const receipt = stored.receipt ? {
222
+ ...stored.receipt,
223
+ blockNumber: BigInt(stored.receipt.blockNumber),
224
+ cumulativeGasUsed: BigInt(stored.receipt.cumulativeGasUsed),
225
+ effectiveGasPrice: BigInt(stored.receipt.effectiveGasPrice),
226
+ gasUsed: BigInt(stored.receipt.gasUsed)
227
+ } : undefined;
228
+ return {
229
+ id: stored.id,
230
+ txHashes: stored.txHashes,
231
+ cancelTxHashes: stored.cancelTxHashes,
232
+ gasLimit: BigInt(stored.gasLimit),
233
+ gasPrice: {
234
+ maxFeePerGas: BigInt(stored.gasPrice.maxFeePerGas),
235
+ maxPriorityFeePerGas: BigInt(stored.gasPrice.maxPriorityFeePerGas),
236
+ maxFeePerBlobGas: stored.gasPrice.maxFeePerBlobGas ? BigInt(stored.gasPrice.maxFeePerBlobGas) : undefined
237
+ },
238
+ txConfigOverrides,
239
+ request: {
240
+ to: stored.request.to,
241
+ data: stored.request.data,
242
+ value: stored.request.value ? BigInt(stored.request.value) : undefined
243
+ },
244
+ status: stored.status,
245
+ nonce: stored.nonce,
246
+ sentAtL1Ts: new Date(stored.sentAt),
247
+ lastSentAtL1Ts: new Date(stored.lastSentAt),
248
+ receipt,
249
+ blobInputs
250
+ };
251
+ }
252
+ /**
253
+ * Serializes blob inputs for separate storage.
254
+ */ serializeBlobInputs(blobInputs) {
255
+ return {
256
+ blobs: blobInputs.blobs.map((b)=>Buffer.from(b).toString('base64')),
257
+ kzg: jsonStringify(blobInputs.kzg)
258
+ };
259
+ }
260
+ /**
261
+ * Deserializes blob inputs from storage, combining blob data with metadata.
262
+ */ deserializeBlobInputs(stored, metadata) {
263
+ const blobInputs = {
264
+ blobs: stored.blobs.map((b)=>new Uint8Array(Buffer.from(b, 'base64'))),
265
+ kzg: JSON.parse(stored.kzg)
266
+ };
267
+ if (metadata?.maxFeePerBlobGas) {
268
+ blobInputs.maxFeePerBlobGas = BigInt(metadata.maxFeePerBlobGas);
269
+ }
270
+ return blobInputs;
271
+ }
272
+ }