@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
@@ -1,6 +1,6 @@
1
1
  import { ARCHIVER_DB_VERSION, ARCHIVER_STORE_NAME, type ArchiverConfig, createArchiverStore } from '@aztec/archiver';
2
2
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
3
- import { type EthereumClientConfig, getPublicClient } from '@aztec/ethereum';
3
+ import { type EthereumClientConfig, getPublicClient } from '@aztec/ethereum/client';
4
4
  import type { EthAddress } from '@aztec/foundation/eth-address';
5
5
  import { tryRmDir } from '@aztec/foundation/fs';
6
6
  import type { Logger } from '@aztec/foundation/log';
@@ -26,11 +26,12 @@ import type { SharedNodeConfig } from '../config/index.js';
26
26
  // Half day worth of L1 blocks
27
27
  const MIN_L1_BLOCKS_TO_TRIGGER_REPLACE = 86400 / 2 / 12;
28
28
 
29
- type SnapshotSyncConfig = Pick<SharedNodeConfig, 'syncMode' | 'snapshotsUrl'> &
29
+ type SnapshotSyncConfig = Pick<SharedNodeConfig, 'syncMode'> &
30
30
  Pick<ChainConfig, 'l1ChainId' | 'rollupVersion'> &
31
31
  Pick<ArchiverConfig, 'archiverStoreMapSizeKb' | 'maxLogs'> &
32
32
  Required<DataStoreConfig> &
33
33
  EthereumClientConfig & {
34
+ snapshotsUrls?: string[];
34
35
  minL1BlocksToTriggerReplace?: number;
35
36
  };
36
37
 
@@ -39,14 +40,14 @@ type SnapshotSyncConfig = Pick<SharedNodeConfig, 'syncMode' | 'snapshotsUrl'> &
39
40
  * Behaviour depends on syncing mode.
40
41
  */
41
42
  export async function trySnapshotSync(config: SnapshotSyncConfig, log: Logger) {
42
- const { syncMode, snapshotsUrl, dataDirectory, l1ChainId, rollupVersion, l1Contracts } = config;
43
+ const { syncMode, snapshotsUrls, dataDirectory, l1ChainId, rollupVersion, l1Contracts } = config;
43
44
  if (syncMode === 'full') {
44
45
  log.debug('Snapshot sync is disabled. Running full sync.', { syncMode: syncMode });
45
46
  return false;
46
47
  }
47
48
 
48
- if (!snapshotsUrl) {
49
- log.verbose('Snapshot sync is disabled. No snapshots URL provided.');
49
+ if (!snapshotsUrls || snapshotsUrls.length === 0) {
50
+ log.verbose('Snapshot sync is disabled. No snapshots URLs provided.');
50
51
  return false;
51
52
  }
52
53
 
@@ -55,15 +56,7 @@ export async function trySnapshotSync(config: SnapshotSyncConfig, log: Logger) {
55
56
  return false;
56
57
  }
57
58
 
58
- let fileStore: ReadOnlyFileStore;
59
- try {
60
- fileStore = await createReadOnlyFileStore(snapshotsUrl, log);
61
- } catch (err) {
62
- log.error(`Invalid config for downloading snapshots`, err);
63
- return false;
64
- }
65
-
66
- // Create an archiver store to check the current state
59
+ // Create an archiver store to check the current state (do this only once)
67
60
  log.verbose(`Creating temporary archiver data store`);
68
61
  const archiverStore = await createArchiverStore(config);
69
62
  let archiverL1BlockNumber: bigint | undefined;
@@ -71,7 +64,7 @@ export async function trySnapshotSync(config: SnapshotSyncConfig, log: Logger) {
71
64
  try {
72
65
  [archiverL1BlockNumber, archiverL2BlockNumber] = await Promise.all([
73
66
  archiverStore.getSynchPoint().then(s => s.blocksSynchedTo),
74
- archiverStore.getSynchedL2BlockNumber(),
67
+ archiverStore.getLatestBlockNumber(),
75
68
  ] as const);
76
69
  } finally {
77
70
  log.verbose(`Closing temporary archiver data store`, { archiverL1BlockNumber, archiverL2BlockNumber });
@@ -102,65 +95,111 @@ export async function trySnapshotSync(config: SnapshotSyncConfig, log: Logger) {
102
95
  rollupVersion,
103
96
  rollupAddress: l1Contracts.rollupAddress,
104
97
  };
105
- let snapshot: SnapshotMetadata | undefined;
106
- try {
107
- snapshot = await getLatestSnapshotMetadata(indexMetadata, fileStore);
108
- } catch (err) {
109
- log.error(`Failed to get latest snapshot metadata. Skipping snapshot sync.`, err, {
110
- ...indexMetadata,
111
- snapshotsUrl,
112
- });
113
- return false;
114
- }
115
98
 
116
- if (!snapshot) {
117
- log.verbose(`No snapshot found. Skipping snapshot sync.`, { ...indexMetadata, snapshotsUrl });
118
- return false;
119
- }
99
+ // Fetch latest snapshot from each URL
100
+ type SnapshotCandidate = { snapshot: SnapshotMetadata; url: string; fileStore: ReadOnlyFileStore };
101
+ const snapshotCandidates: SnapshotCandidate[] = [];
120
102
 
121
- if (snapshot.schemaVersions.archiver !== ARCHIVER_DB_VERSION) {
122
- log.warn(
123
- `Skipping snapshot sync as last snapshot has schema version ${snapshot.schemaVersions.archiver} but expected ${ARCHIVER_DB_VERSION}.`,
124
- snapshot,
125
- );
126
- return false;
127
- }
103
+ for (const snapshotsUrl of snapshotsUrls) {
104
+ let fileStore: ReadOnlyFileStore;
105
+ try {
106
+ fileStore = await createReadOnlyFileStore(snapshotsUrl, log);
107
+ } catch (err) {
108
+ log.error(`Invalid config for downloading snapshots from ${snapshotsUrl}`, err);
109
+ continue;
110
+ }
128
111
 
129
- if (snapshot.schemaVersions.worldState !== WORLD_STATE_DB_VERSION) {
130
- log.warn(
131
- `Skipping snapshot sync as last snapshot has world state schema version ${snapshot.schemaVersions.worldState} but we expected ${WORLD_STATE_DB_VERSION}.`,
132
- snapshot,
133
- );
134
- return false;
112
+ let snapshot: SnapshotMetadata | undefined;
113
+ try {
114
+ snapshot = await getLatestSnapshotMetadata(indexMetadata, fileStore);
115
+ } catch (err) {
116
+ log.error(`Failed to get latest snapshot metadata from ${snapshotsUrl}. Skipping this URL.`, err, {
117
+ ...indexMetadata,
118
+ snapshotsUrl,
119
+ });
120
+ continue;
121
+ }
122
+
123
+ if (!snapshot) {
124
+ log.verbose(`No snapshot found at ${snapshotsUrl}. Skipping this URL.`, { ...indexMetadata, snapshotsUrl });
125
+ continue;
126
+ }
127
+
128
+ if (snapshot.schemaVersions.archiver !== ARCHIVER_DB_VERSION) {
129
+ log.warn(
130
+ `Skipping snapshot from ${snapshotsUrl} as it has schema version ${snapshot.schemaVersions.archiver} but expected ${ARCHIVER_DB_VERSION}.`,
131
+ snapshot,
132
+ );
133
+ continue;
134
+ }
135
+
136
+ if (snapshot.schemaVersions.worldState !== WORLD_STATE_DB_VERSION) {
137
+ log.warn(
138
+ `Skipping snapshot from ${snapshotsUrl} as it has world state schema version ${snapshot.schemaVersions.worldState} but we expected ${WORLD_STATE_DB_VERSION}.`,
139
+ snapshot,
140
+ );
141
+ continue;
142
+ }
143
+
144
+ if (archiverL1BlockNumber && snapshot.l1BlockNumber < archiverL1BlockNumber) {
145
+ log.verbose(
146
+ `Skipping snapshot from ${snapshotsUrl} since local archiver is at L1 block ${archiverL1BlockNumber} which is further than snapshot at ${snapshot.l1BlockNumber}`,
147
+ { snapshot, archiverL1BlockNumber, snapshotsUrl },
148
+ );
149
+ continue;
150
+ }
151
+
152
+ if (archiverL1BlockNumber && snapshot.l1BlockNumber - Number(archiverL1BlockNumber) < minL1BlocksToTriggerReplace) {
153
+ log.verbose(
154
+ `Skipping snapshot from ${snapshotsUrl} as archiver is less than ${
155
+ snapshot.l1BlockNumber - Number(archiverL1BlockNumber)
156
+ } L1 blocks behind this snapshot.`,
157
+ { snapshot, archiverL1BlockNumber, snapshotsUrl },
158
+ );
159
+ continue;
160
+ }
161
+
162
+ snapshotCandidates.push({ snapshot, url: snapshotsUrl, fileStore });
135
163
  }
136
164
 
137
- if (archiverL1BlockNumber && snapshot.l1BlockNumber < archiverL1BlockNumber) {
138
- log.verbose(
139
- `Skipping snapshot sync since local archiver is at L1 block ${archiverL1BlockNumber} which is further than last snapshot at ${snapshot.l1BlockNumber}`,
140
- { snapshot, archiverL1BlockNumber },
141
- );
165
+ if (snapshotCandidates.length === 0) {
166
+ log.verbose(`No valid snapshots found from any URL, skipping snapshot sync`, { ...indexMetadata, snapshotsUrls });
142
167
  return false;
143
168
  }
144
169
 
145
- if (archiverL1BlockNumber && snapshot.l1BlockNumber - Number(archiverL1BlockNumber) < minL1BlocksToTriggerReplace) {
146
- log.verbose(
147
- `Skipping snapshot sync as archiver is less than ${
148
- snapshot.l1BlockNumber - Number(archiverL1BlockNumber)
149
- } L1 blocks behind latest snapshot.`,
150
- { snapshot, archiverL1BlockNumber },
151
- );
152
- return false;
170
+ // Sort candidates by L1 block number (highest first)
171
+ snapshotCandidates.sort((a, b) => b.snapshot.l1BlockNumber - a.snapshot.l1BlockNumber);
172
+
173
+ // Try each candidate in order until one succeeds
174
+ for (const { snapshot, url } of snapshotCandidates) {
175
+ const { l1BlockNumber, l2BlockNumber } = snapshot;
176
+ log.info(`Attempting to sync from snapshot at L1 block ${l1BlockNumber} L2 block ${l2BlockNumber}`, {
177
+ snapshot,
178
+ snapshotsUrl: url,
179
+ });
180
+
181
+ try {
182
+ await snapshotSync(snapshot, log, {
183
+ dataDirectory: config.dataDirectory!,
184
+ rollupAddress: config.l1Contracts.rollupAddress,
185
+ snapshotsUrl: url,
186
+ });
187
+ log.info(`Snapshot synced to L1 block ${l1BlockNumber} L2 block ${l2BlockNumber}`, {
188
+ snapshot,
189
+ snapshotsUrl: url,
190
+ });
191
+ return true;
192
+ } catch (err) {
193
+ log.error(`Failed to download snapshot from ${url}, trying next candidate`, err, {
194
+ snapshot,
195
+ snapshotsUrl: url,
196
+ });
197
+ continue;
198
+ }
153
199
  }
154
200
 
155
- const { l1BlockNumber, l2BlockNumber } = snapshot;
156
- log.info(`Syncing from snapshot at L1 block ${l1BlockNumber} L2 block ${l2BlockNumber}`, { snapshot, snapshotsUrl });
157
- await snapshotSync(snapshot, log, {
158
- dataDirectory: config.dataDirectory!,
159
- rollupAddress: config.l1Contracts.rollupAddress,
160
- snapshotsUrl,
161
- });
162
- log.info(`Snapshot synced to L1 block ${l1BlockNumber} L2 block ${l2BlockNumber}`, { snapshot });
163
- return true;
201
+ log.error(`Failed to download snapshot from all URLs`, { snapshotsUrls });
202
+ return false;
164
203
  }
165
204
 
166
205
  /**
@@ -7,8 +7,8 @@ export type SharedNodeConfig = {
7
7
  sponsoredFPC: boolean;
8
8
  /** Sync mode: full to always sync via L1, snapshot to download a snapshot if there is no local data, force-snapshot to download even if there is local data. */
9
9
  syncMode: 'full' | 'snapshot' | 'force-snapshot';
10
- /** Base URL for snapshots index. Index file will be searched at `SNAPSHOTS_BASE_URL/aztec-L1_CHAIN_ID-VERSION-ROLLUP_ADDRESS/index.json` */
11
- snapshotsUrl?: string;
10
+ /** Base URLs for snapshots index. Index file will be searched at `SNAPSHOTS_BASE_URL/aztec-L1_CHAIN_ID-VERSION-ROLLUP_ADDRESS/index.json` */
11
+ snapshotsUrls?: string[];
12
12
 
13
13
  /** Auto update mode: disabled - to completely ignore remote signals to update the node. enabled - to respect the signals (potentially shutting this node down). log - check for updates but log a warning instead of applying them*/
14
14
  autoUpdate?: 'disabled' | 'notify' | 'config' | 'config-and-version';
@@ -17,6 +17,11 @@ export type SharedNodeConfig = {
17
17
 
18
18
  /** URL of the Web3Signer instance */
19
19
  web3SignerUrl?: string;
20
+ /** Whether to run in fisherman mode */
21
+ fishermanMode?: boolean;
22
+
23
+ /** Force verification of tx Chonk proofs. Only used for testnet */
24
+ debugForceTxProofVerification: boolean;
20
25
  };
21
26
 
22
27
  export const sharedNodeConfigMappings: ConfigMappingsType<SharedNodeConfig> = {
@@ -36,9 +41,16 @@ export const sharedNodeConfigMappings: ConfigMappingsType<SharedNodeConfig> = {
36
41
  'Set sync mode to `full` to always sync via L1, `snapshot` to download a snapshot if there is no local data, `force-snapshot` to download even if there is local data.',
37
42
  defaultValue: 'snapshot',
38
43
  },
39
- snapshotsUrl: {
40
- env: 'SYNC_SNAPSHOTS_URL',
41
- description: 'Base URL for snapshots index.',
44
+ snapshotsUrls: {
45
+ env: 'SYNC_SNAPSHOTS_URLS',
46
+ description: 'Base URLs for snapshots index, comma-separated.',
47
+ parseEnv: (val: string) =>
48
+ val
49
+ .split(',')
50
+ .map(url => url.trim())
51
+ .filter(url => url.length > 0),
52
+ fallback: ['SYNC_SNAPSHOTS_URL'],
53
+ defaultValue: [],
42
54
  },
43
55
  autoUpdate: {
44
56
  env: 'AUTO_UPDATE',
@@ -54,4 +66,14 @@ export const sharedNodeConfigMappings: ConfigMappingsType<SharedNodeConfig> = {
54
66
  description: 'URL of the Web3Signer instance',
55
67
  parseEnv: (val: string) => val.trim(),
56
68
  },
69
+ fishermanMode: {
70
+ env: 'FISHERMAN_MODE',
71
+ description: 'Whether to run in fisherman mode.',
72
+ ...booleanConfigHelper(false),
73
+ },
74
+ debugForceTxProofVerification: {
75
+ env: 'DEBUG_FORCE_TX_PROOF_VERIFICATION',
76
+ description: 'Whether to force tx proof verification. Only has an effect if real proving is turned off',
77
+ ...booleanConfigHelper(false),
78
+ },
57
79
  };
@@ -0,0 +1 @@
1
+ export * from './l1_tx_utils.js';
@@ -0,0 +1,212 @@
1
+ import type { EthSigner } from '@aztec/ethereum/eth-signer';
2
+ import {
3
+ createL1TxUtilsFromEthSigner as createL1TxUtilsFromEthSignerBase,
4
+ createL1TxUtilsFromViemWallet as createL1TxUtilsFromViemWalletBase,
5
+ } from '@aztec/ethereum/l1-tx-utils';
6
+ import type { L1TxUtilsConfig } from '@aztec/ethereum/l1-tx-utils';
7
+ import {
8
+ createForwarderL1TxUtilsFromEthSigner as createForwarderL1TxUtilsFromEthSignerBase,
9
+ createForwarderL1TxUtilsFromViemWallet as createForwarderL1TxUtilsFromViemWalletBase,
10
+ createL1TxUtilsWithBlobsFromEthSigner as createL1TxUtilsWithBlobsFromEthSignerBase,
11
+ createL1TxUtilsWithBlobsFromViemWallet as createL1TxUtilsWithBlobsFromViemWalletBase,
12
+ } from '@aztec/ethereum/l1-tx-utils-with-blobs';
13
+ import type { ExtendedViemWalletClient, ViemClient } from '@aztec/ethereum/types';
14
+ import { omit } from '@aztec/foundation/collection';
15
+ import { createLogger } from '@aztec/foundation/log';
16
+ import type { DateProvider } from '@aztec/foundation/timer';
17
+ import type { DataStoreConfig } from '@aztec/kv-store/config';
18
+ import { createStore } from '@aztec/kv-store/lmdb-v2';
19
+ import type { TelemetryClient } from '@aztec/telemetry-client';
20
+
21
+ import type { L1TxScope } from '../metrics/l1_tx_metrics.js';
22
+ import { L1TxMetrics } from '../metrics/l1_tx_metrics.js';
23
+ import { L1TxStore } from '../stores/l1_tx_store.js';
24
+
25
+ const L1_TX_STORE_NAME = 'l1-tx-utils';
26
+
27
+ /**
28
+ * Creates shared dependencies (logger, store, metrics) for L1TxUtils instances.
29
+ */
30
+ async function createSharedDeps(
31
+ config: DataStoreConfig & { scope?: L1TxScope },
32
+ deps: {
33
+ telemetry: TelemetryClient;
34
+ logger?: ReturnType<typeof createLogger>;
35
+ dateProvider?: DateProvider;
36
+ },
37
+ ) {
38
+ const logger = deps.logger ?? createLogger('l1-tx-utils');
39
+
40
+ // Note that we do NOT bind them to the rollup address, since we still need to
41
+ // monitor and cancel txs for previous rollups to free up our nonces.
42
+ const noRollupConfig = omit(config, 'l1Contracts');
43
+ const kvStore = await createStore(L1_TX_STORE_NAME, L1TxStore.SCHEMA_VERSION, noRollupConfig, logger);
44
+ const store = new L1TxStore(kvStore, logger);
45
+
46
+ const meter = deps.telemetry.getMeter('L1TxUtils');
47
+ const metrics = new L1TxMetrics(meter, config.scope ?? 'other', logger);
48
+
49
+ return { logger, store, metrics, dateProvider: deps.dateProvider };
50
+ }
51
+
52
+ /**
53
+ * Creates L1TxUtils with blobs from multiple Viem wallets, sharing store and metrics.
54
+ */
55
+ export async function createL1TxUtilsWithBlobsFromViemWallet(
56
+ clients: ExtendedViemWalletClient[],
57
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
58
+ deps: {
59
+ telemetry: TelemetryClient;
60
+ logger?: ReturnType<typeof createLogger>;
61
+ dateProvider?: DateProvider;
62
+ },
63
+ ) {
64
+ const sharedDeps = await createSharedDeps(config, deps);
65
+
66
+ return clients.map(client =>
67
+ createL1TxUtilsWithBlobsFromViemWalletBase(client, sharedDeps, config, config.debugMaxGasLimit),
68
+ );
69
+ }
70
+
71
+ /**
72
+ * Creates L1TxUtils with blobs from multiple EthSigners, sharing store and metrics. Removes duplicates
73
+ */
74
+ export async function createL1TxUtilsWithBlobsFromEthSigner(
75
+ client: ViemClient,
76
+ signers: EthSigner[],
77
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
78
+ deps: {
79
+ telemetry: TelemetryClient;
80
+ logger?: ReturnType<typeof createLogger>;
81
+ dateProvider?: DateProvider;
82
+ },
83
+ ) {
84
+ const sharedDeps = await createSharedDeps(config, deps);
85
+
86
+ // Deduplicate signers by address to avoid creating multiple L1TxUtils instances
87
+ // for the same publisher address (e.g., when multiple attesters share the same publisher key)
88
+ const signersByAddress = new Map<string, EthSigner>();
89
+ for (const signer of signers) {
90
+ const addressKey = signer.address.toString().toLowerCase();
91
+ if (!signersByAddress.has(addressKey)) {
92
+ signersByAddress.set(addressKey, signer);
93
+ }
94
+ }
95
+
96
+ const uniqueSigners = Array.from(signersByAddress.values());
97
+
98
+ if (uniqueSigners.length < signers.length) {
99
+ sharedDeps.logger.info(
100
+ `Deduplicated ${signers.length} signers to ${uniqueSigners.length} unique publisher addresses`,
101
+ );
102
+ }
103
+
104
+ return uniqueSigners.map(signer =>
105
+ createL1TxUtilsWithBlobsFromEthSignerBase(client, signer, sharedDeps, config, config.debugMaxGasLimit),
106
+ );
107
+ }
108
+
109
+ /**
110
+ * Creates L1TxUtils (without blobs) from multiple Viem wallets, sharing store and metrics.
111
+ */
112
+ export async function createL1TxUtilsFromViemWalletWithStore(
113
+ clients: ExtendedViemWalletClient[],
114
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
115
+ deps: {
116
+ telemetry: TelemetryClient;
117
+ logger?: ReturnType<typeof createLogger>;
118
+ dateProvider?: DateProvider;
119
+ scope?: L1TxScope;
120
+ },
121
+ ) {
122
+ const sharedDeps = await createSharedDeps(config, deps);
123
+
124
+ return clients.map(client => createL1TxUtilsFromViemWalletBase(client, sharedDeps, config));
125
+ }
126
+
127
+ /**
128
+ * Creates L1TxUtils (without blobs) from multiple EthSigners, sharing store and metrics. Removes duplicates.
129
+ */
130
+ export async function createL1TxUtilsFromEthSignerWithStore(
131
+ client: ViemClient,
132
+ signers: EthSigner[],
133
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
134
+ deps: {
135
+ telemetry: TelemetryClient;
136
+ logger?: ReturnType<typeof createLogger>;
137
+ dateProvider?: DateProvider;
138
+ scope?: L1TxScope;
139
+ },
140
+ ) {
141
+ const sharedDeps = await createSharedDeps(config, deps);
142
+
143
+ // Deduplicate signers by address to avoid creating multiple L1TxUtils instances
144
+ // for the same publisher address (e.g., when multiple attesters share the same publisher key)
145
+ const signersByAddress = new Map<string, EthSigner>();
146
+ for (const signer of signers) {
147
+ const addressKey = signer.address.toString().toLowerCase();
148
+ if (!signersByAddress.has(addressKey)) {
149
+ signersByAddress.set(addressKey, signer);
150
+ }
151
+ }
152
+
153
+ const uniqueSigners = Array.from(signersByAddress.values());
154
+
155
+ if (uniqueSigners.length < signers.length) {
156
+ sharedDeps.logger.info(
157
+ `Deduplicated ${signers.length} signers to ${uniqueSigners.length} unique publisher addresses`,
158
+ );
159
+ }
160
+
161
+ return uniqueSigners.map(signer => createL1TxUtilsFromEthSignerBase(client, signer, sharedDeps, config));
162
+ }
163
+
164
+ /**
165
+ * Creates ForwarderL1TxUtils from multiple Viem wallets, sharing store and metrics.
166
+ * This wraps all transactions through a forwarder contract for testing purposes.
167
+ */
168
+ export async function createForwarderL1TxUtilsFromViemWallet(
169
+ clients: ExtendedViemWalletClient[],
170
+ forwarderAddress: import('@aztec/foundation/eth-address').EthAddress,
171
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
172
+ deps: {
173
+ telemetry: TelemetryClient;
174
+ logger?: ReturnType<typeof createLogger>;
175
+ dateProvider?: DateProvider;
176
+ },
177
+ ) {
178
+ const sharedDeps = await createSharedDeps(config, deps);
179
+
180
+ return clients.map(client =>
181
+ createForwarderL1TxUtilsFromViemWalletBase(client, forwarderAddress, sharedDeps, config, config.debugMaxGasLimit),
182
+ );
183
+ }
184
+
185
+ /**
186
+ * Creates ForwarderL1TxUtils from multiple EthSigners, sharing store and metrics.
187
+ * This wraps all transactions through a forwarder contract for testing purposes.
188
+ */
189
+ export async function createForwarderL1TxUtilsFromEthSigner(
190
+ client: ViemClient,
191
+ signers: EthSigner[],
192
+ forwarderAddress: import('@aztec/foundation/eth-address').EthAddress,
193
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
194
+ deps: {
195
+ telemetry: TelemetryClient;
196
+ logger?: ReturnType<typeof createLogger>;
197
+ dateProvider?: DateProvider;
198
+ },
199
+ ) {
200
+ const sharedDeps = await createSharedDeps(config, deps);
201
+
202
+ return signers.map(signer =>
203
+ createForwarderL1TxUtilsFromEthSignerBase(
204
+ client,
205
+ signer,
206
+ forwarderAddress,
207
+ sharedDeps,
208
+ config,
209
+ config.debugMaxGasLimit,
210
+ ),
211
+ );
212
+ }
@@ -0,0 +1 @@
1
+ export * from './l1_tx_metrics.js';
@@ -0,0 +1,169 @@
1
+ import type { IL1TxMetrics, L1TxState } from '@aztec/ethereum/l1-tx-utils';
2
+ import { TxUtilsState } from '@aztec/ethereum/l1-tx-utils';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import {
5
+ Attributes,
6
+ type Histogram,
7
+ type Meter,
8
+ Metrics,
9
+ type UpDownCounter,
10
+ ValueType,
11
+ } from '@aztec/telemetry-client';
12
+
13
+ export type L1TxScope = 'sequencer' | 'prover' | 'other';
14
+
15
+ /**
16
+ * Metrics for L1 transaction utils tracking tx lifecycle and gas costs.
17
+ */
18
+ export class L1TxMetrics implements IL1TxMetrics {
19
+ // Time until tx is mined
20
+ private txMinedDuration: Histogram;
21
+
22
+ // Number of attempts until mined
23
+ private txAttemptsUntilMined: Histogram;
24
+
25
+ // Counters for end states
26
+ private txMinedCount: UpDownCounter;
27
+ private txRevertedCount: UpDownCounter;
28
+ private txCancelledCount: UpDownCounter;
29
+ private txNotMinedCount: UpDownCounter;
30
+
31
+ // Gas price histograms (at end state, in wei)
32
+ private maxPriorityFeeHistogram: Histogram;
33
+ private maxFeeHistogram: Histogram;
34
+ private blobFeeHistogram: Histogram;
35
+
36
+ constructor(
37
+ private meter: Meter,
38
+ private scope: L1TxScope = 'other',
39
+ private logger = createLogger('l1-tx-utils:metrics'),
40
+ ) {
41
+ this.txMinedDuration = this.meter.createHistogram(Metrics.L1_TX_MINED_DURATION, {
42
+ description: 'Time from initial tx send until mined',
43
+ unit: 's',
44
+ valueType: ValueType.INT,
45
+ });
46
+
47
+ this.txAttemptsUntilMined = this.meter.createHistogram(Metrics.L1_TX_ATTEMPTS_UNTIL_MINED, {
48
+ description: 'Number of tx attempts (including speed-ups) until mined',
49
+ unit: 'attempts',
50
+ valueType: ValueType.INT,
51
+ });
52
+
53
+ this.txMinedCount = this.meter.createUpDownCounter(Metrics.L1_TX_MINED_COUNT, {
54
+ description: 'Count of transactions successfully mined',
55
+ valueType: ValueType.INT,
56
+ });
57
+
58
+ this.txRevertedCount = this.meter.createUpDownCounter(Metrics.L1_TX_REVERTED_COUNT, {
59
+ description: 'Count of transactions that reverted',
60
+ valueType: ValueType.INT,
61
+ });
62
+
63
+ this.txCancelledCount = this.meter.createUpDownCounter(Metrics.L1_TX_CANCELLED_COUNT, {
64
+ description: 'Count of transactions cancelled',
65
+ valueType: ValueType.INT,
66
+ });
67
+
68
+ this.txNotMinedCount = this.meter.createUpDownCounter(Metrics.L1_TX_NOT_MINED_COUNT, {
69
+ description: 'Count of transactions not mined (timed out)',
70
+ valueType: ValueType.INT,
71
+ });
72
+
73
+ this.maxPriorityFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_MAX_PRIORITY_FEE, {
74
+ description: 'Max priority fee per gas at tx end state (in wei)',
75
+ unit: 'wei',
76
+ valueType: ValueType.INT,
77
+ });
78
+
79
+ this.maxFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_MAX_FEE, {
80
+ description: 'Max fee per gas at tx end state (in wei)',
81
+ unit: 'wei',
82
+ valueType: ValueType.INT,
83
+ });
84
+
85
+ this.blobFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_BLOB_FEE, {
86
+ description: 'Max fee per blob gas at tx end state (in wei)',
87
+ unit: 'wei',
88
+ valueType: ValueType.INT,
89
+ });
90
+ }
91
+
92
+ /**
93
+ * Records metrics when a transaction is mined.
94
+ * @param state - The L1 transaction state
95
+ * @param l1Timestamp - The current L1 timestamp
96
+ */
97
+ public recordMinedTx(state: L1TxState, l1Timestamp: Date): void {
98
+ if (state.status !== TxUtilsState.MINED) {
99
+ this.logger.warn(
100
+ `Attempted to record mined tx metrics for a tx not in MINED state (state: ${TxUtilsState[state.status]})`,
101
+ { scope: this.scope, nonce: state.nonce },
102
+ );
103
+ return;
104
+ }
105
+
106
+ const attributes = { [Attributes.L1_TX_SCOPE]: this.scope };
107
+ const isCancelTx = state.cancelTxHashes.length > 0;
108
+ const isReverted = state.receipt?.status === 'reverted';
109
+
110
+ if (isCancelTx) {
111
+ this.txCancelledCount.add(1, attributes);
112
+ } else if (isReverted) {
113
+ this.txRevertedCount.add(1, attributes);
114
+ } else {
115
+ this.txMinedCount.add(1, attributes);
116
+ }
117
+
118
+ // Record time to mine using provided L1 timestamp
119
+ const duration = Math.floor((l1Timestamp.getTime() - state.sentAtL1Ts.getTime()) / 1000);
120
+ this.txMinedDuration.record(duration, attributes);
121
+
122
+ // Record number of attempts until mined
123
+ const attempts = isCancelTx ? state.cancelTxHashes.length : state.txHashes.length;
124
+ this.txAttemptsUntilMined.record(attempts, attributes);
125
+
126
+ // Record gas prices at end state (in wei as integers)
127
+ const maxPriorityFeeWei = Number(state.gasPrice.maxPriorityFeePerGas);
128
+ const maxFeeWei = Number(state.gasPrice.maxFeePerGas);
129
+ const blobFeeWei = state.gasPrice.maxFeePerBlobGas ? Number(state.gasPrice.maxFeePerBlobGas) : undefined;
130
+
131
+ this.maxPriorityFeeHistogram.record(maxPriorityFeeWei, attributes);
132
+ this.maxFeeHistogram.record(maxFeeWei, attributes);
133
+
134
+ // Record blob fee if present (in wei as integer)
135
+ if (blobFeeWei !== undefined) {
136
+ this.blobFeeHistogram.record(blobFeeWei, attributes);
137
+ }
138
+
139
+ this.logger.debug(`Recorded tx end state metrics`, {
140
+ status: TxUtilsState[state.status],
141
+ nonce: state.nonce,
142
+ isCancelTx,
143
+ isReverted,
144
+ scope: this.scope,
145
+ maxPriorityFeeWei,
146
+ maxFeeWei,
147
+ blobFeeWei,
148
+ });
149
+ }
150
+
151
+ public recordDroppedTx(state: L1TxState): void {
152
+ if (state.status !== TxUtilsState.NOT_MINED) {
153
+ this.logger.warn(
154
+ `Attempted to record dropped tx metrics for a tx not in NOT_MINED state (state: ${TxUtilsState[state.status]})`,
155
+ { scope: this.scope, nonce: state.nonce },
156
+ );
157
+ return;
158
+ }
159
+
160
+ const attributes = { [Attributes.L1_TX_SCOPE]: this.scope };
161
+ this.txNotMinedCount.add(1, attributes);
162
+
163
+ this.logger.debug(`Recorded tx dropped metrics`, {
164
+ status: TxUtilsState[state.status],
165
+ nonce: state.nonce,
166
+ scope: this.scope,
167
+ });
168
+ }
169
+ }
@@ -0,0 +1 @@
1
+ export * from './l1_tx_store.js';