@aztec/validator-ha-signer 0.0.1-commit.6d3c34e → 0.0.1-commit.7035c9bd6

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 (58) hide show
  1. package/README.md +50 -37
  2. package/dest/db/index.d.ts +2 -1
  3. package/dest/db/index.d.ts.map +1 -1
  4. package/dest/db/index.js +1 -0
  5. package/dest/db/lmdb.d.ts +66 -0
  6. package/dest/db/lmdb.d.ts.map +1 -0
  7. package/dest/db/lmdb.js +188 -0
  8. package/dest/db/postgres.d.ts +37 -6
  9. package/dest/db/postgres.d.ts.map +1 -1
  10. package/dest/db/postgres.js +86 -28
  11. package/dest/db/schema.d.ts +21 -10
  12. package/dest/db/schema.d.ts.map +1 -1
  13. package/dest/db/schema.js +49 -20
  14. package/dest/db/types.d.ts +109 -33
  15. package/dest/db/types.d.ts.map +1 -1
  16. package/dest/db/types.js +57 -8
  17. package/dest/errors.d.ts +9 -5
  18. package/dest/errors.d.ts.map +1 -1
  19. package/dest/errors.js +7 -4
  20. package/dest/factory.d.ts +42 -15
  21. package/dest/factory.d.ts.map +1 -1
  22. package/dest/factory.js +80 -15
  23. package/dest/metrics.d.ts +51 -0
  24. package/dest/metrics.d.ts.map +1 -0
  25. package/dest/metrics.js +103 -0
  26. package/dest/migrations.d.ts +1 -1
  27. package/dest/migrations.d.ts.map +1 -1
  28. package/dest/migrations.js +13 -2
  29. package/dest/slashing_protection_service.d.ts +25 -6
  30. package/dest/slashing_protection_service.d.ts.map +1 -1
  31. package/dest/slashing_protection_service.js +74 -22
  32. package/dest/test/pglite_pool.d.ts +92 -0
  33. package/dest/test/pglite_pool.d.ts.map +1 -0
  34. package/dest/test/pglite_pool.js +210 -0
  35. package/dest/types.d.ts +40 -16
  36. package/dest/types.d.ts.map +1 -1
  37. package/dest/types.js +4 -1
  38. package/dest/validator_ha_signer.d.ts +18 -13
  39. package/dest/validator_ha_signer.d.ts.map +1 -1
  40. package/dest/validator_ha_signer.js +45 -36
  41. package/package.json +15 -10
  42. package/src/db/index.ts +1 -0
  43. package/src/db/lmdb.ts +264 -0
  44. package/src/db/postgres.ts +109 -27
  45. package/src/db/schema.ts +51 -20
  46. package/src/db/types.ts +166 -32
  47. package/src/errors.ts +7 -2
  48. package/src/factory.ts +99 -15
  49. package/src/metrics.ts +138 -0
  50. package/src/migrations.ts +17 -1
  51. package/src/slashing_protection_service.ts +119 -27
  52. package/src/test/pglite_pool.ts +256 -0
  53. package/src/types.ts +65 -16
  54. package/src/validator_ha_signer.ts +64 -45
  55. package/dest/config.d.ts +0 -47
  56. package/dest/config.d.ts.map +0 -1
  57. package/dest/config.js +0 -64
  58. package/src/config.ts +0 -116
package/src/db/types.ts CHANGED
@@ -1,13 +1,22 @@
1
- import type { EthAddress } from '@aztec/foundation/eth-address';
1
+ import {
2
+ BlockNumber,
3
+ type CheckpointNumber,
4
+ type IndexWithinCheckpoint,
5
+ SlotNumber,
6
+ } from '@aztec/foundation/branded-types';
7
+ import { EthAddress } from '@aztec/foundation/eth-address';
2
8
  import type { Signature } from '@aztec/foundation/eth-signature';
9
+ import { DutyType } from '@aztec/stdlib/ha-signing';
3
10
 
4
11
  /**
5
12
  * Row type from PostgreSQL query
6
13
  */
7
14
  export interface DutyRow {
15
+ rollup_address: string;
8
16
  validator_address: string;
9
17
  slot: string;
10
18
  block_number: string;
19
+ block_index_within_checkpoint: number;
11
20
  duty_type: DutyType;
12
21
  status: DutyStatus;
13
22
  message_hash: string;
@@ -20,19 +29,34 @@ export interface DutyRow {
20
29
  }
21
30
 
22
31
  /**
23
- * Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
32
+ * Plain-primitive representation of a duty record suitable for serialization
33
+ * (e.g. msgpackr for LMDB). All domain types are stored as their string/number
34
+ * equivalents. Timestamps are Unix milliseconds.
24
35
  */
25
- export interface InsertOrGetRow extends DutyRow {
26
- is_new: boolean;
36
+ export interface StoredDutyRecord {
37
+ rollupAddress: string;
38
+ validatorAddress: string;
39
+ slot: string;
40
+ blockNumber: string;
41
+ blockIndexWithinCheckpoint: number;
42
+ dutyType: DutyType;
43
+ status: DutyStatus;
44
+ messageHash: string;
45
+ signature?: string;
46
+ nodeId: string;
47
+ lockToken: string;
48
+ /** Unix timestamp in milliseconds when signing started */
49
+ startedAtMs: number;
50
+ /** Unix timestamp in milliseconds when signing completed */
51
+ completedAtMs?: number;
52
+ errorMessage?: string;
27
53
  }
28
54
 
29
55
  /**
30
- * Type of validator duty being performed
56
+ * Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
31
57
  */
32
- export enum DutyType {
33
- BLOCK_PROPOSAL = 'BLOCK_PROPOSAL',
34
- ATTESTATION = 'ATTESTATION',
35
- ATTESTATIONS_AND_SIGNERS = 'ATTESTATIONS_AND_SIGNERS',
58
+ export interface InsertOrGetRow extends DutyRow {
59
+ is_new: boolean;
36
60
  }
37
61
 
38
62
  /**
@@ -43,16 +67,24 @@ export enum DutyStatus {
43
67
  SIGNED = 'signed',
44
68
  }
45
69
 
70
+ // Re-export DutyType from stdlib
71
+ export { DutyType };
72
+
46
73
  /**
47
- * Record of a validator duty in the database
74
+ * Rich representation of a validator duty, with branded types and Date objects.
75
+ * This is the common output type returned by all SlashingProtectionDatabase implementations.
48
76
  */
49
77
  export interface ValidatorDutyRecord {
78
+ /** Ethereum address of the rollup contract */
79
+ rollupAddress: EthAddress;
50
80
  /** Ethereum address of the validator */
51
81
  validatorAddress: EthAddress;
52
82
  /** Slot number for this duty */
53
- slot: bigint;
83
+ slot: SlotNumber;
54
84
  /** Block number for this duty */
55
- blockNumber: bigint;
85
+ blockNumber: BlockNumber;
86
+ /** Block index within checkpoint (0, 1, 2... for block proposals, -1 for other duty types) */
87
+ blockIndexWithinCheckpoint: number;
56
88
  /** Type of duty being performed */
57
89
  dutyType: DutyType;
58
90
  /** Current status of the duty */
@@ -69,49 +101,151 @@ export interface ValidatorDutyRecord {
69
101
  startedAt: Date;
70
102
  /** When the duty signing was completed (success or failure) */
71
103
  completedAt?: Date;
72
- /** Error message if status is 'failed' */
104
+ /** Error message (currently unused) */
73
105
  errorMessage?: string;
74
106
  }
75
107
 
76
108
  /**
77
- * Minimal info needed to identify a unique duty
109
+ * Convert a {@link StoredDutyRecord} (plain-primitive wire format) to a
110
+ * {@link ValidatorDutyRecord} (rich domain type).
111
+ *
112
+ * Shared by LMDB and any future non-Postgres backend implementations.
78
113
  */
79
- export interface DutyIdentifier {
114
+ export function recordFromFields(stored: StoredDutyRecord): ValidatorDutyRecord {
115
+ return {
116
+ rollupAddress: EthAddress.fromString(stored.rollupAddress),
117
+ validatorAddress: EthAddress.fromString(stored.validatorAddress),
118
+ slot: SlotNumber.fromString(stored.slot),
119
+ blockNumber: BlockNumber.fromString(stored.blockNumber),
120
+ blockIndexWithinCheckpoint: stored.blockIndexWithinCheckpoint,
121
+ dutyType: stored.dutyType,
122
+ status: stored.status,
123
+ messageHash: stored.messageHash,
124
+ signature: stored.signature,
125
+ nodeId: stored.nodeId,
126
+ lockToken: stored.lockToken,
127
+ startedAt: new Date(stored.startedAtMs),
128
+ completedAt: stored.completedAtMs !== undefined ? new Date(stored.completedAtMs) : undefined,
129
+ errorMessage: stored.errorMessage,
130
+ };
131
+ }
132
+
133
+ /**
134
+ * Duty identifier for block proposals.
135
+ * blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
136
+ */
137
+ export interface BlockProposalDutyIdentifier {
138
+ rollupAddress: EthAddress;
80
139
  validatorAddress: EthAddress;
81
- slot: bigint;
82
- dutyType: DutyType;
140
+ slot: SlotNumber;
141
+ /** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
142
+ blockIndexWithinCheckpoint: IndexWithinCheckpoint;
143
+ dutyType: DutyType.BLOCK_PROPOSAL;
83
144
  }
84
145
 
85
146
  /**
86
- * Parameters for checking and recording a new duty
147
+ * Duty identifier for non-block-proposal duties.
148
+ * blockIndexWithinCheckpoint is not applicable (internally stored as -1).
87
149
  */
88
- export interface CheckAndRecordParams {
150
+ export interface OtherDutyIdentifier {
151
+ rollupAddress: EthAddress;
89
152
  validatorAddress: EthAddress;
90
- slot: bigint;
91
- blockNumber: bigint;
92
- dutyType: DutyType;
153
+ slot: SlotNumber;
154
+ dutyType:
155
+ | DutyType.CHECKPOINT_PROPOSAL
156
+ | DutyType.ATTESTATION
157
+ | DutyType.ATTESTATIONS_AND_SIGNERS
158
+ | DutyType.GOVERNANCE_VOTE
159
+ | DutyType.SLASHING_VOTE
160
+ | DutyType.AUTH_REQUEST
161
+ | DutyType.TXS;
162
+ }
163
+
164
+ /**
165
+ * Minimal info needed to identify a unique duty.
166
+ * Uses discriminated union to enforce type safety:
167
+ * - BLOCK_PROPOSAL duties MUST have blockIndexWithinCheckpoint >= 0
168
+ * - Other duty types do NOT have blockIndexWithinCheckpoint (internally -1)
169
+ */
170
+ export type DutyIdentifier = BlockProposalDutyIdentifier | OtherDutyIdentifier;
171
+
172
+ /**
173
+ * Validates and normalizes the block index for a duty.
174
+ * - BLOCK_PROPOSAL: validates blockIndexWithinCheckpoint is provided and >= 0
175
+ * - Other duty types: always returns -1
176
+ *
177
+ * @throws Error if BLOCK_PROPOSAL is missing blockIndexWithinCheckpoint or has invalid value
178
+ */
179
+ export function normalizeBlockIndex(dutyType: DutyType, blockIndexWithinCheckpoint: number | undefined): number {
180
+ if (dutyType === DutyType.BLOCK_PROPOSAL) {
181
+ if (blockIndexWithinCheckpoint === undefined) {
182
+ throw new Error('BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint to be specified');
183
+ }
184
+ if (blockIndexWithinCheckpoint < 0) {
185
+ throw new Error(
186
+ `BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint >= 0, got ${blockIndexWithinCheckpoint}`,
187
+ );
188
+ }
189
+ return blockIndexWithinCheckpoint;
190
+ }
191
+ // For all other duty types, always use -1
192
+ return -1;
193
+ }
194
+
195
+ /**
196
+ * Gets the block index from a DutyIdentifier.
197
+ * - BLOCK_PROPOSAL: returns the blockIndexWithinCheckpoint
198
+ * - Other duty types: returns -1
199
+ */
200
+ export function getBlockIndexFromDutyIdentifier(duty: DutyIdentifier): number {
201
+ if (duty.dutyType === DutyType.BLOCK_PROPOSAL) {
202
+ return duty.blockIndexWithinCheckpoint;
203
+ }
204
+ return -1;
205
+ }
206
+
207
+ /**
208
+ * Additional parameters for checking and recording a new duty
209
+ */
210
+ interface CheckAndRecordExtra {
211
+ /** Block number for this duty */
212
+ blockNumber: BlockNumber | CheckpointNumber;
213
+ /** The signing root (hash) for this duty */
93
214
  messageHash: string;
215
+ /** Identifier for the node that acquired the lock */
94
216
  nodeId: string;
95
217
  }
96
218
 
97
219
  /**
98
- * Parameters for recording a successful signing
220
+ * Parameters for checking and recording a new duty.
221
+ * Uses intersection with DutyIdentifier to preserve the discriminated union.
99
222
  */
100
- export interface RecordSuccessParams {
101
- validatorAddress: EthAddress;
102
- slot: bigint;
103
- dutyType: DutyType;
223
+ export type CheckAndRecordParams = DutyIdentifier & CheckAndRecordExtra;
224
+
225
+ /**
226
+ * Additional parameters for recording a successful signing
227
+ */
228
+ interface RecordSuccessExtra {
104
229
  signature: Signature;
105
230
  nodeId: string;
106
231
  lockToken: string;
107
232
  }
108
233
 
109
234
  /**
110
- * Parameters for deleting a duty
235
+ * Parameters for recording a successful signing.
236
+ * Uses intersection with DutyIdentifier to preserve the discriminated union.
111
237
  */
112
- export interface DeleteDutyParams {
113
- validatorAddress: EthAddress;
114
- slot: bigint;
115
- dutyType: DutyType;
238
+ export type RecordSuccessParams = DutyIdentifier & RecordSuccessExtra;
239
+
240
+ /**
241
+ * Additional parameters for deleting a duty
242
+ */
243
+ interface DeleteDutyExtra {
116
244
  lockToken: string;
117
245
  }
246
+
247
+ /**
248
+ * Parameters for deleting a duty.
249
+ * Uses intersection with DutyIdentifier to preserve the discriminated union.
250
+ */
251
+ export type DeleteDutyParams = DutyIdentifier & DeleteDutyExtra;
package/src/errors.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * Custom errors for the validator HA signer
3
3
  */
4
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
5
+
4
6
  import type { DutyType } from './db/types.js';
5
7
 
6
8
  /**
@@ -10,8 +12,9 @@ import type { DutyType } from './db/types.js';
10
12
  */
11
13
  export class DutyAlreadySignedError extends Error {
12
14
  constructor(
13
- public readonly slot: bigint,
15
+ public readonly slot: SlotNumber,
14
16
  public readonly dutyType: DutyType,
17
+ public readonly blockIndexWithinCheckpoint: number,
15
18
  public readonly signedByNode: string,
16
19
  ) {
17
20
  super(`Duty ${dutyType} for slot ${slot} already signed by node ${signedByNode}`);
@@ -28,10 +31,12 @@ export class DutyAlreadySignedError extends Error {
28
31
  */
29
32
  export class SlashingProtectionError extends Error {
30
33
  constructor(
31
- public readonly slot: bigint,
34
+ public readonly slot: SlotNumber,
32
35
  public readonly dutyType: DutyType,
36
+ public readonly blockIndexWithinCheckpoint: number,
33
37
  public readonly existingMessageHash: string,
34
38
  public readonly attemptedMessageHash: string,
39
+ public readonly signedByNode: string,
35
40
  ) {
36
41
  super(
37
42
  `Slashing protection: ${dutyType} for slot ${slot} was already signed with different data. ` +
package/src/factory.ts CHANGED
@@ -1,11 +1,17 @@
1
1
  /**
2
2
  * Factory functions for creating validator HA signers
3
3
  */
4
+ import { DateProvider } from '@aztec/foundation/timer';
5
+ import { createStore } from '@aztec/kv-store/lmdb-v2';
6
+ import type { LocalSignerConfig, ValidatorHASignerConfig } from '@aztec/stdlib/ha-signing';
7
+ import { getTelemetryClient } from '@aztec/telemetry-client';
8
+
4
9
  import { Pool } from 'pg';
5
10
 
6
- import type { CreateHASignerConfig } from './config.js';
11
+ import { LmdbSlashingProtectionDatabase } from './db/lmdb.js';
7
12
  import { PostgresSlashingProtectionDatabase } from './db/postgres.js';
8
- import type { CreateHASignerDeps, SlashingProtectionDatabase } from './types.js';
13
+ import { HASignerMetrics } from './metrics.js';
14
+ import type { CreateHASignerDeps, CreateLocalSignerWithProtectionDeps, SlashingProtectionDatabase } from './types.js';
9
15
  import { ValidatorHASigner } from './validator_ha_signer.js';
10
16
 
11
17
  /**
@@ -23,7 +29,6 @@ import { ValidatorHASigner } from './validator_ha_signer.js';
23
29
  * ```typescript
24
30
  * const { signer, db } = await createHASigner({
25
31
  * databaseUrl: process.env.DATABASE_URL,
26
- * enabled: true,
27
32
  * nodeId: 'validator-node-1',
28
33
  * pollingIntervalMs: 100,
29
34
  * signingTimeoutMs: 3000,
@@ -35,23 +40,15 @@ import { ValidatorHASigner } from './validator_ha_signer.js';
35
40
  * await signer.stop(); // On shutdown
36
41
  * ```
37
42
  *
38
- * Example with automatic migrations (simpler for dev/testing):
39
- * ```typescript
40
- * const { signer, db } = await createHASigner({
41
- * databaseUrl: process.env.DATABASE_URL,
42
- * enabled: true,
43
- * nodeId: 'validator-node-1',
44
- * runMigrations: true, // Auto-run migrations on startup
45
- * });
46
- * signer.start();
47
- * ```
43
+ * Note: Migrations must be run separately using `aztec migrate-ha-db up` before
44
+ * creating the signer. The factory will verify the schema is initialized via `db.initialize()`.
48
45
  *
49
46
  * @param config - Configuration for the HA signer
50
47
  * @param deps - Optional dependencies (e.g., for testing)
51
48
  * @returns An object containing the signer and database instances
52
49
  */
53
50
  export async function createHASigner(
54
- config: CreateHASignerConfig,
51
+ config: ValidatorHASignerConfig,
55
52
  deps?: CreateHASignerDeps,
56
53
  ): Promise<{
57
54
  signer: ValidatorHASigner;
@@ -60,6 +57,13 @@ export async function createHASigner(
60
57
  const { databaseUrl, poolMaxCount, poolMinCount, poolIdleTimeoutMs, poolConnectionTimeoutMs, ...signerConfig } =
61
58
  config;
62
59
 
60
+ if (!databaseUrl) {
61
+ throw new Error('databaseUrl is required for createHASigner');
62
+ }
63
+
64
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
65
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
66
+
63
67
  // Create connection pool (or use provided pool)
64
68
  let pool: Pool;
65
69
  if (!deps?.pool) {
@@ -80,8 +84,88 @@ export async function createHASigner(
80
84
  // Verify database schema is initialized and version matches
81
85
  await db.initialize();
82
86
 
87
+ // Create metrics
88
+ const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId);
89
+
83
90
  // Create signer
84
- const signer = new ValidatorHASigner(db, { ...signerConfig, databaseUrl });
91
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
92
+
93
+ return { signer, db };
94
+ }
95
+
96
+ /**
97
+ * Create a local (single-node) signing protection signer backed by LMDB.
98
+ *
99
+ * This provides double-signing protection for nodes that are NOT running in a
100
+ * high-availability (multi-node) setup. It prevents a proposer from sending two
101
+ * proposals for the same slot if the node crashes and restarts mid-proposal.
102
+ *
103
+ * When `config.dataDirectory` is set, the protection database is persisted to disk
104
+ * and survives crashes/restarts. When unset, an ephemeral in-memory store is
105
+ * used which protects within a single run but not across restarts.
106
+ *
107
+ * @param config - Local signer config
108
+ * @param deps - Optional dependencies (telemetry, date provider).
109
+ * @returns An object containing the signer and database instances.
110
+ */
111
+ export async function createLocalSignerWithProtection(
112
+ config: LocalSignerConfig,
113
+ deps?: CreateLocalSignerWithProtectionDeps,
114
+ ): Promise<{
115
+ signer: ValidatorHASigner;
116
+ db: SlashingProtectionDatabase;
117
+ }> {
118
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
119
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
120
+
121
+ const kvStore = await createStore('signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
122
+ dataDirectory: config.dataDirectory,
123
+ dataStoreMapSizeKb: config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
124
+ l1Contracts: config.l1Contracts,
125
+ });
126
+
127
+ const db = new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
128
+
129
+ const signerConfig = {
130
+ ...config,
131
+ nodeId: config.nodeId || 'local',
132
+ };
133
+
134
+ const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId, 'LocalSigningProtectionMetrics');
135
+
136
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
85
137
 
86
138
  return { signer, db };
87
139
  }
140
+
141
+ /**
142
+ * Create an in-memory LMDB-backed SlashingProtectionDatabase that can be shared across
143
+ * multiple validator nodes in the same process. Used for testing HA setups.
144
+ */
145
+ export async function createSharedSlashingProtectionDb(
146
+ dateProvider: DateProvider = new DateProvider(),
147
+ ): Promise<SlashingProtectionDatabase> {
148
+ const kvStore = await createStore('shared-signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
149
+ dataStoreMapSizeKb: 1024 * 1024,
150
+ });
151
+ return new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
152
+ }
153
+
154
+ /**
155
+ * Create a ValidatorHASigner backed by a pre-existing SlashingProtectionDatabase.
156
+ * Used for testing HA setups where multiple nodes share the same protection database.
157
+ */
158
+ export function createSignerFromSharedDb(
159
+ db: SlashingProtectionDatabase,
160
+ config: Pick<
161
+ ValidatorHASignerConfig,
162
+ 'nodeId' | 'pollingIntervalMs' | 'signingTimeoutMs' | 'maxStuckDutiesAgeMs' | 'l1Contracts'
163
+ >,
164
+ deps?: CreateLocalSignerWithProtectionDeps,
165
+ ): { signer: ValidatorHASigner; db: SlashingProtectionDatabase } {
166
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
167
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
168
+ const metrics = new HASignerMetrics(telemetryClient, config.nodeId, 'SharedSigningProtectionMetrics');
169
+ const signer = new ValidatorHASigner(db, config, { metrics, dateProvider });
170
+ return { signer, db };
171
+ }
package/src/metrics.ts ADDED
@@ -0,0 +1,138 @@
1
+ import {
2
+ Attributes,
3
+ type Histogram,
4
+ Metrics,
5
+ type TelemetryClient,
6
+ type UpDownCounter,
7
+ createUpDownCounterWithDefault,
8
+ } from '@aztec/telemetry-client';
9
+
10
+ export type HACleanupType = 'stuck' | 'old' | 'outdated_rollup';
11
+
12
+ /**
13
+ * Metrics for HA signer tracking signing operations, lock acquisition, and cleanup.
14
+ */
15
+ export class HASignerMetrics {
16
+ // Signing lifecycle metrics
17
+ private signingDuration: Histogram;
18
+ private signingSuccessCount: UpDownCounter;
19
+ private dutyAlreadySignedCount: UpDownCounter;
20
+ private slashingProtectionCount: UpDownCounter;
21
+ private signingErrorCount: UpDownCounter;
22
+
23
+ // Lock acquisition metrics
24
+ private lockAcquiredCount: UpDownCounter;
25
+
26
+ // Cleanup metrics
27
+ private cleanupStuckDutiesCount: UpDownCounter;
28
+ private cleanupOldDutiesCount: UpDownCounter;
29
+ private cleanupOutdatedRollupDutiesCount: UpDownCounter;
30
+
31
+ constructor(
32
+ client: TelemetryClient,
33
+ private nodeId: string,
34
+ name = 'HASignerMetrics',
35
+ ) {
36
+ const meter = client.getMeter(name);
37
+
38
+ // Signing lifecycle
39
+ this.signingDuration = meter.createHistogram(Metrics.HA_SIGNER_SIGNING_DURATION);
40
+ this.signingSuccessCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_SIGNING_SUCCESS_COUNT);
41
+ this.dutyAlreadySignedCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_DUTY_ALREADY_SIGNED_COUNT);
42
+ this.slashingProtectionCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_SLASHING_PROTECTION_COUNT);
43
+ this.signingErrorCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_SIGNING_ERROR_COUNT);
44
+
45
+ // Lock acquisition
46
+ this.lockAcquiredCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_LOCK_ACQUIRED_COUNT);
47
+
48
+ // Cleanup
49
+ this.cleanupStuckDutiesCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_CLEANUP_STUCK_DUTIES_COUNT);
50
+ this.cleanupOldDutiesCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_CLEANUP_OLD_DUTIES_COUNT);
51
+ this.cleanupOutdatedRollupDutiesCount = createUpDownCounterWithDefault(
52
+ meter,
53
+ Metrics.HA_SIGNER_CLEANUP_OUTDATED_ROLLUP_DUTIES_COUNT,
54
+ );
55
+ }
56
+
57
+ /**
58
+ * Record a successful signing operation.
59
+ * @param dutyType - The type of duty signed
60
+ * @param durationMs - Duration from start of signWithProtection to completion
61
+ */
62
+ public recordSigningSuccess(dutyType: string, durationMs: number): void {
63
+ const attributes = {
64
+ [Attributes.HA_DUTY_TYPE]: dutyType,
65
+ [Attributes.HA_NODE_ID]: this.nodeId,
66
+ };
67
+ this.signingSuccessCount.add(1, attributes);
68
+ this.signingDuration.record(durationMs, attributes);
69
+ }
70
+
71
+ /**
72
+ * Record a DutyAlreadySignedError (expected in HA; another node signed first).
73
+ * @param dutyType - The type of duty
74
+ */
75
+ public recordDutyAlreadySigned(dutyType: string): void {
76
+ const attributes = {
77
+ [Attributes.HA_DUTY_TYPE]: dutyType,
78
+ [Attributes.HA_NODE_ID]: this.nodeId,
79
+ };
80
+ this.dutyAlreadySignedCount.add(1, attributes);
81
+ }
82
+
83
+ /**
84
+ * Record a SlashingProtectionError (attempted to sign different data for same duty).
85
+ * @param dutyType - The type of duty
86
+ */
87
+ public recordSlashingProtection(dutyType: string): void {
88
+ const attributes = {
89
+ [Attributes.HA_DUTY_TYPE]: dutyType,
90
+ [Attributes.HA_NODE_ID]: this.nodeId,
91
+ };
92
+ this.slashingProtectionCount.add(1, attributes);
93
+ }
94
+
95
+ /**
96
+ * Record a signing function failure (lock will be deleted for retry).
97
+ * @param dutyType - The type of duty
98
+ */
99
+ public recordSigningError(dutyType: string): void {
100
+ const attributes = {
101
+ [Attributes.HA_DUTY_TYPE]: dutyType,
102
+ [Attributes.HA_NODE_ID]: this.nodeId,
103
+ };
104
+ this.signingErrorCount.add(1, attributes);
105
+ }
106
+
107
+ /**
108
+ * Record lock acquisition.
109
+ * @param acquired - Whether a new lock was acquired (true) or existing record found (false)
110
+ */
111
+ public recordLockAcquire(acquired: boolean): void {
112
+ if (acquired) {
113
+ const attributes = {
114
+ [Attributes.HA_NODE_ID]: this.nodeId,
115
+ };
116
+ this.lockAcquiredCount.add(1, attributes);
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Record cleanup metrics.
122
+ * @param type - Type of cleanup
123
+ * @param count - Number of duties cleaned up
124
+ */
125
+ public recordCleanup(type: HACleanupType, count: number): void {
126
+ const attributes = {
127
+ [Attributes.HA_NODE_ID]: this.nodeId,
128
+ };
129
+
130
+ if (type === 'stuck') {
131
+ this.cleanupStuckDutiesCount.add(count, attributes);
132
+ } else if (type === 'old') {
133
+ this.cleanupOldDutiesCount.add(count, attributes);
134
+ } else if (type === 'outdated_rollup') {
135
+ this.cleanupOutdatedRollupDutiesCount.add(count, attributes);
136
+ }
137
+ }
138
+ }
package/src/migrations.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  */
4
4
  import { createLogger } from '@aztec/foundation/log';
5
5
 
6
+ import { readdirSync } from 'fs';
6
7
  import { runner } from 'node-pg-migrate';
7
8
  import { dirname, join } from 'path';
8
9
  import { fileURLToPath } from 'url';
@@ -30,17 +31,32 @@ export async function runMigrations(databaseUrl: string, options: RunMigrationsO
30
31
 
31
32
  const log = createLogger('validator-ha-signer:migrations');
32
33
 
34
+ const migrationsDir = join(__dirname, 'db', 'migrations');
35
+
33
36
  try {
34
37
  log.info(`Running migrations ${direction}...`);
35
38
 
39
+ // Filter out .d.ts and .d.ts.map files - node-pg-migrate only needs .js files
40
+ const migrationFiles = readdirSync(migrationsDir);
41
+ const jsMigrationFiles = migrationFiles.filter(
42
+ file => file.endsWith('.js') && !file.endsWith('.d.ts') && !file.endsWith('.d.ts.map'),
43
+ );
44
+
45
+ if (jsMigrationFiles.length === 0) {
46
+ log.info('No migration files found');
47
+ return [];
48
+ }
49
+
36
50
  const appliedMigrations = await runner({
37
51
  databaseUrl,
38
- dir: join(__dirname, 'db', 'migrations'),
52
+ dir: migrationsDir,
39
53
  direction,
40
54
  migrationsTable: 'pgmigrations',
41
55
  count: direction === 'down' ? 1 : Infinity,
42
56
  verbose,
43
57
  log: msg => (verbose ? log.info(msg) : log.debug(msg)),
58
+ // Ignore TypeScript declaration files - node-pg-migrate will try to import them otherwise
59
+ ignorePattern: '.*\\.d\\.(ts|js)$|.*\\.d\\.ts\\.map$',
44
60
  });
45
61
 
46
62
  if (appliedMigrations.length === 0) {