@aztec-labs/node-lib 6.0.0-nightly.20260829

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 +29 -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 +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 +276 -0
  38. package/package.json +102 -0
  39. package/src/actions/build-snapshot-metadata.ts +29 -0
  40. package/src/actions/create-backups.ts +40 -0
  41. package/src/actions/index.ts +4 -0
  42. package/src/actions/snapshot-sync.ts +290 -0
  43. package/src/actions/upload-snapshot.ts +49 -0
  44. package/src/config/index.ts +86 -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 +140 -0
  49. package/src/stores/index.ts +1 -0
  50. package/src/stores/l1_tx_store.ts +410 -0
@@ -0,0 +1,156 @@
1
+ import type { BlobKzgInstance } from '@aztec-labs/blob-lib/types';
2
+ import type { EthSigner } from '@aztec-labs/ethereum/eth-signer';
3
+ import { createDelayer, createL1TxUtils as createL1TxUtilsBase } from '@aztec-labs/ethereum/l1-tx-utils';
4
+ import type { L1TxUtilsConfig } from '@aztec-labs/ethereum/l1-tx-utils';
5
+ import { createForwarderL1TxUtils as createForwarderL1TxUtilsBase } from '@aztec-labs/ethereum/l1-tx-utils-with-blobs';
6
+ import type { ExtendedViemWalletClient, ViemClient } from '@aztec-labs/ethereum/types';
7
+ import { omit } from '@aztec-labs/foundation/collection';
8
+ import type { EthAddress } from '@aztec-labs/foundation/eth-address';
9
+ import { createLogger } from '@aztec-labs/foundation/log';
10
+ import type { DateProvider } from '@aztec-labs/foundation/timer';
11
+ import { createStore } from '@aztec-labs/kv-store/lmdb-v2';
12
+ import type { DataStoreConfig } from '@aztec-labs/stdlib/kv-store';
13
+ import type { TelemetryClient } from '@aztec-labs/telemetry-client';
14
+
15
+ import type { L1TxScope } from '../metrics/l1_tx_metrics.js';
16
+ import { L1TxMetrics } from '../metrics/l1_tx_metrics.js';
17
+ import { L1TxStore } from '../stores/l1_tx_store.js';
18
+
19
+ const L1_TX_STORE_NAME = 'l1-tx-utils';
20
+
21
+ /**
22
+ * Creates shared dependencies (logger, store, metrics, delayer) for L1TxUtils instances.
23
+ * When enableDelayer is set in config, a single shared delayer is created and passed to all instances.
24
+ */
25
+ async function createSharedDeps(
26
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { scope?: L1TxScope },
27
+ deps: {
28
+ telemetry: TelemetryClient;
29
+ logger?: ReturnType<typeof createLogger>;
30
+ dateProvider: DateProvider;
31
+ },
32
+ ) {
33
+ const logger = deps.logger ?? createLogger('l1-tx-utils');
34
+
35
+ // Note that we do NOT bind them to the rollup address, since we still need to
36
+ // monitor and cancel txs for previous rollups to free up our nonces.
37
+ const noRollupConfig = omit(config, 'rollupAddress');
38
+ const kvStore = await createStore(L1_TX_STORE_NAME, L1TxStore.SCHEMA_VERSION, noRollupConfig, logger.getBindings());
39
+ const store = new L1TxStore(kvStore, logger);
40
+
41
+ const meter = deps.telemetry.getMeter('L1TxUtils');
42
+ const metrics = new L1TxMetrics(meter, config.scope ?? 'other', logger);
43
+
44
+ // Create a single shared delayer for all L1TxUtils instances in this group
45
+ const delayer =
46
+ config.enableDelayer && config.ethereumSlotDuration !== undefined
47
+ ? createDelayer(deps.dateProvider, { ethereumSlotDuration: config.ethereumSlotDuration }, logger.getBindings())
48
+ : undefined;
49
+
50
+ return { logger, store, metrics, dateProvider: deps.dateProvider, delayer };
51
+ }
52
+
53
+ /**
54
+ * Creates L1TxUtils from multiple Viem wallet clients, sharing store, metrics, and delayer.
55
+ * When kzg is provided in deps, blob support is enabled.
56
+ */
57
+ export async function createL1TxUtilsFromWallets(
58
+ clients: ExtendedViemWalletClient[],
59
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
60
+ deps: {
61
+ telemetry: TelemetryClient;
62
+ logger?: ReturnType<typeof createLogger>;
63
+ dateProvider: DateProvider;
64
+ kzg?: BlobKzgInstance;
65
+ },
66
+ ) {
67
+ const sharedDeps = await createSharedDeps(config, deps);
68
+
69
+ return clients.map(client => createL1TxUtilsBase(client, { ...sharedDeps, kzg: deps.kzg }, config));
70
+ }
71
+
72
+ /**
73
+ * Creates L1TxUtils from multiple EthSigners, sharing store, metrics, and delayer.
74
+ * When kzg is provided in deps, blob support is enabled.
75
+ * Deduplicates signers by address to avoid creating multiple instances for the same publisher.
76
+ */
77
+ export async function createL1TxUtilsFromSigners(
78
+ client: ViemClient,
79
+ signers: EthSigner[],
80
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
81
+ deps: {
82
+ telemetry: TelemetryClient;
83
+ logger?: ReturnType<typeof createLogger>;
84
+ dateProvider: DateProvider;
85
+ kzg?: BlobKzgInstance;
86
+ },
87
+ ) {
88
+ const sharedDeps = await createSharedDeps(config, deps);
89
+
90
+ // Deduplicate signers by address to avoid creating multiple L1TxUtils instances
91
+ // for the same publisher address (e.g., when multiple attesters share the same publisher key)
92
+ const signersByAddress = new Map<string, EthSigner>();
93
+ for (const signer of signers) {
94
+ const addressKey = signer.address.toString().toLowerCase();
95
+ if (!signersByAddress.has(addressKey)) {
96
+ signersByAddress.set(addressKey, signer);
97
+ }
98
+ }
99
+
100
+ const uniqueSigners = Array.from(signersByAddress.values());
101
+
102
+ if (uniqueSigners.length < signers.length) {
103
+ sharedDeps.logger.info(
104
+ `Deduplicated ${signers.length} signers to ${uniqueSigners.length} unique publisher addresses`,
105
+ );
106
+ }
107
+
108
+ return uniqueSigners.map(signer => createL1TxUtilsBase({ client, signer }, { ...sharedDeps, kzg: deps.kzg }, config));
109
+ }
110
+
111
+ /**
112
+ * Creates ForwarderL1TxUtils from multiple Viem wallet clients, sharing store, metrics, and delayer.
113
+ * Wraps all transactions through a forwarder contract for testing purposes.
114
+ * When kzg is provided in deps, blob support is enabled.
115
+ */
116
+ export async function createForwarderL1TxUtilsFromWallets(
117
+ clients: ExtendedViemWalletClient[],
118
+ forwarderAddress: EthAddress,
119
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
120
+ deps: {
121
+ telemetry: TelemetryClient;
122
+ logger?: ReturnType<typeof createLogger>;
123
+ dateProvider: DateProvider;
124
+ kzg?: BlobKzgInstance;
125
+ },
126
+ ) {
127
+ const sharedDeps = await createSharedDeps(config, deps);
128
+
129
+ return clients.map(client =>
130
+ createForwarderL1TxUtilsBase(client, forwarderAddress, { ...sharedDeps, kzg: deps.kzg }, config),
131
+ );
132
+ }
133
+
134
+ /**
135
+ * Creates ForwarderL1TxUtils from multiple EthSigners, sharing store, metrics, and delayer.
136
+ * Wraps all transactions through a forwarder contract for testing purposes.
137
+ * When kzg is provided in deps, blob support is enabled.
138
+ */
139
+ export async function createForwarderL1TxUtilsFromSigners(
140
+ client: ViemClient,
141
+ signers: EthSigner[],
142
+ forwarderAddress: EthAddress,
143
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
144
+ deps: {
145
+ telemetry: TelemetryClient;
146
+ logger?: ReturnType<typeof createLogger>;
147
+ dateProvider: DateProvider;
148
+ kzg?: BlobKzgInstance;
149
+ },
150
+ ) {
151
+ const sharedDeps = await createSharedDeps(config, deps);
152
+
153
+ return signers.map(signer =>
154
+ createForwarderL1TxUtilsBase({ client, signer }, forwarderAddress, { ...sharedDeps, kzg: deps.kzg }, config),
155
+ );
156
+ }
@@ -0,0 +1 @@
1
+ export * from './l1_tx_metrics.js';
@@ -0,0 +1,140 @@
1
+ import type { IL1TxMetrics, L1TxState } from '@aztec-labs/ethereum/l1-tx-utils';
2
+ import { TxUtilsState } from '@aztec-labs/ethereum/l1-tx-utils';
3
+ import { createLogger } from '@aztec-labs/foundation/log';
4
+ import {
5
+ Attributes,
6
+ type Histogram,
7
+ type Meter,
8
+ Metrics,
9
+ type UpDownCounter,
10
+ createUpDownCounterWithDefault,
11
+ } from '@aztec-labs/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 fees per gas at end state, converted from wei to gwei to match metric unit definitions
96
+ const weiToGwei = 1e9;
97
+ const maxPriorityFeeGwei = Number(state.feesPerGas.maxPriorityFeePerGas) / weiToGwei;
98
+ const maxFeeGwei = Number(state.feesPerGas.maxFeePerGas) / weiToGwei;
99
+ const blobFeeGwei = state.feesPerGas.maxFeePerBlobGas
100
+ ? Number(state.feesPerGas.maxFeePerBlobGas) / weiToGwei
101
+ : undefined;
102
+
103
+ this.maxPriorityFeeHistogram.record(maxPriorityFeeGwei, attributes);
104
+ this.maxFeeHistogram.record(maxFeeGwei, attributes);
105
+
106
+ if (blobFeeGwei !== undefined) {
107
+ this.blobFeeHistogram.record(blobFeeGwei, attributes);
108
+ }
109
+
110
+ this.logger.debug(`Recorded tx end state metrics`, {
111
+ status: TxUtilsState[state.status],
112
+ nonce: state.nonce,
113
+ isCancelTx,
114
+ isReverted,
115
+ scope: this.scope,
116
+ maxPriorityFeeGwei,
117
+ maxFeeGwei,
118
+ blobFeeGwei,
119
+ });
120
+ }
121
+
122
+ public recordDroppedTx(state: L1TxState): void {
123
+ if (state.status !== TxUtilsState.NOT_MINED) {
124
+ this.logger.warn(
125
+ `Attempted to record dropped tx metrics for a tx not in NOT_MINED state (state: ${TxUtilsState[state.status]})`,
126
+ { scope: this.scope, nonce: state.nonce },
127
+ );
128
+ return;
129
+ }
130
+
131
+ const attributes = { [Attributes.L1_TX_SCOPE]: this.scope };
132
+ this.txNotMinedCount.add(1, attributes);
133
+
134
+ this.logger.debug(`Recorded tx dropped metrics`, {
135
+ status: TxUtilsState[state.status],
136
+ nonce: state.nonce,
137
+ scope: this.scope,
138
+ });
139
+ }
140
+ }
@@ -0,0 +1 @@
1
+ export * from './l1_tx_store.js';