@aztec/node-lib 0.0.1-commit.0208eb9

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 +238 -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 +51 -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 +79 -0
  24. package/dest/factories/l1_tx_utils.d.ts.map +1 -0
  25. package/dest/factories/l1_tx_utils.js +90 -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 +102 -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 +279 -0
  43. package/src/actions/upload-snapshot.ts +50 -0
  44. package/src/config/index.ts +79 -0
  45. package/src/factories/index.ts +1 -0
  46. package/src/factories/l1_tx_utils.ts +212 -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,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.getBindings());
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,138 @@
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
+ createUpDownCounterWithDefault,
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
+
43
+ this.txAttemptsUntilMined = this.meter.createHistogram(Metrics.L1_TX_ATTEMPTS_UNTIL_MINED);
44
+
45
+ const scopeAttributes = [{ [Attributes.L1_TX_SCOPE]: this.scope }];
46
+ this.txMinedCount = createUpDownCounterWithDefault(this.meter, Metrics.L1_TX_MINED_COUNT, scopeAttributes);
47
+
48
+ this.txRevertedCount = createUpDownCounterWithDefault(this.meter, Metrics.L1_TX_REVERTED_COUNT, scopeAttributes);
49
+
50
+ this.txCancelledCount = createUpDownCounterWithDefault(this.meter, Metrics.L1_TX_CANCELLED_COUNT, scopeAttributes);
51
+
52
+ this.txNotMinedCount = createUpDownCounterWithDefault(this.meter, Metrics.L1_TX_NOT_MINED_COUNT, scopeAttributes);
53
+
54
+ this.maxPriorityFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_MAX_PRIORITY_FEE);
55
+
56
+ this.maxFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_MAX_FEE);
57
+
58
+ this.blobFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_BLOB_FEE);
59
+ }
60
+
61
+ /**
62
+ * Records metrics when a transaction is mined.
63
+ * @param state - The L1 transaction state
64
+ * @param l1Timestamp - The current L1 timestamp
65
+ */
66
+ public recordMinedTx(state: L1TxState, l1Timestamp: Date): void {
67
+ if (state.status !== TxUtilsState.MINED) {
68
+ this.logger.warn(
69
+ `Attempted to record mined tx metrics for a tx not in MINED state (state: ${TxUtilsState[state.status]})`,
70
+ { scope: this.scope, nonce: state.nonce },
71
+ );
72
+ return;
73
+ }
74
+
75
+ const attributes = { [Attributes.L1_TX_SCOPE]: this.scope };
76
+ const isCancelTx = state.cancelTxHashes.length > 0;
77
+ const isReverted = state.receipt?.status === 'reverted';
78
+
79
+ if (isCancelTx) {
80
+ this.txCancelledCount.add(1, attributes);
81
+ } else if (isReverted) {
82
+ this.txRevertedCount.add(1, attributes);
83
+ } else {
84
+ this.txMinedCount.add(1, attributes);
85
+ }
86
+
87
+ // Record time to mine using provided L1 timestamp
88
+ const duration = Math.floor((l1Timestamp.getTime() - state.sentAtL1Ts.getTime()) / 1000);
89
+ this.txMinedDuration.record(duration, attributes);
90
+
91
+ // Record number of attempts until mined
92
+ const attempts = isCancelTx ? state.cancelTxHashes.length : state.txHashes.length;
93
+ this.txAttemptsUntilMined.record(attempts, attributes);
94
+
95
+ // Record gas prices at end state (in wei as integers)
96
+ const maxPriorityFeeWei = Number(state.gasPrice.maxPriorityFeePerGas);
97
+ const maxFeeWei = Number(state.gasPrice.maxFeePerGas);
98
+ const blobFeeWei = state.gasPrice.maxFeePerBlobGas ? Number(state.gasPrice.maxFeePerBlobGas) : undefined;
99
+
100
+ this.maxPriorityFeeHistogram.record(maxPriorityFeeWei, attributes);
101
+ this.maxFeeHistogram.record(maxFeeWei, attributes);
102
+
103
+ // Record blob fee if present (in wei as integer)
104
+ if (blobFeeWei !== undefined) {
105
+ this.blobFeeHistogram.record(blobFeeWei, attributes);
106
+ }
107
+
108
+ this.logger.debug(`Recorded tx end state metrics`, {
109
+ status: TxUtilsState[state.status],
110
+ nonce: state.nonce,
111
+ isCancelTx,
112
+ isReverted,
113
+ scope: this.scope,
114
+ maxPriorityFeeWei,
115
+ maxFeeWei,
116
+ blobFeeWei,
117
+ });
118
+ }
119
+
120
+ public recordDroppedTx(state: L1TxState): void {
121
+ if (state.status !== TxUtilsState.NOT_MINED) {
122
+ this.logger.warn(
123
+ `Attempted to record dropped tx metrics for a tx not in NOT_MINED state (state: ${TxUtilsState[state.status]})`,
124
+ { scope: this.scope, nonce: state.nonce },
125
+ );
126
+ return;
127
+ }
128
+
129
+ const attributes = { [Attributes.L1_TX_SCOPE]: this.scope };
130
+ this.txNotMinedCount.add(1, attributes);
131
+
132
+ this.logger.debug(`Recorded tx dropped metrics`, {
133
+ status: TxUtilsState[state.status],
134
+ nonce: state.nonce,
135
+ scope: this.scope,
136
+ });
137
+ }
138
+ }
@@ -0,0 +1 @@
1
+ export * from './l1_tx_store.js';