@aztec/validator-ha-signer 0.0.1-commit.4d79d1f2d → 0.0.1-commit.4d9804df

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 +12 -4
  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 +70 -0
  6. package/dest/db/lmdb.d.ts.map +1 -0
  7. package/dest/db/lmdb.js +223 -0
  8. package/dest/db/migrations/1_initial-schema.d.ts +4 -2
  9. package/dest/db/migrations/1_initial-schema.d.ts.map +1 -1
  10. package/dest/db/migrations/1_initial-schema.js +34 -4
  11. package/dest/db/migrations/2_add-checkpoint-number.d.ts +7 -0
  12. package/dest/db/migrations/2_add-checkpoint-number.d.ts.map +1 -0
  13. package/dest/db/migrations/2_add-checkpoint-number.js +17 -0
  14. package/dest/db/postgres.d.ts +4 -2
  15. package/dest/db/postgres.d.ts.map +1 -1
  16. package/dest/db/postgres.js +17 -17
  17. package/dest/db/schema.d.ts +10 -9
  18. package/dest/db/schema.d.ts.map +1 -1
  19. package/dest/db/schema.js +13 -7
  20. package/dest/db/types.d.ts +46 -21
  21. package/dest/db/types.d.ts.map +1 -1
  22. package/dest/db/types.js +31 -15
  23. package/dest/errors.d.ts +14 -1
  24. package/dest/errors.d.ts.map +1 -1
  25. package/dest/errors.js +15 -0
  26. package/dest/factory.d.ts +38 -5
  27. package/dest/factory.d.ts.map +1 -1
  28. package/dest/factory.js +104 -8
  29. package/dest/metrics.d.ts +51 -0
  30. package/dest/metrics.d.ts.map +1 -0
  31. package/dest/metrics.js +103 -0
  32. package/dest/slashing_protection_service.d.ts +15 -4
  33. package/dest/slashing_protection_service.d.ts.map +1 -1
  34. package/dest/slashing_protection_service.js +28 -14
  35. package/dest/types.d.ts +18 -70
  36. package/dest/types.d.ts.map +1 -1
  37. package/dest/types.js +4 -20
  38. package/dest/validator_ha_signer.d.ts +13 -4
  39. package/dest/validator_ha_signer.d.ts.map +1 -1
  40. package/dest/validator_ha_signer.js +48 -15
  41. package/package.json +11 -7
  42. package/src/db/index.ts +1 -0
  43. package/src/db/lmdb.ts +308 -0
  44. package/src/db/migrations/1_initial-schema.ts +35 -4
  45. package/src/db/migrations/2_add-checkpoint-number.ts +19 -0
  46. package/src/db/postgres.ts +17 -15
  47. package/src/db/schema.ts +13 -7
  48. package/src/db/types.ts +66 -19
  49. package/src/errors.ts +21 -0
  50. package/src/factory.ts +141 -7
  51. package/src/metrics.ts +138 -0
  52. package/src/slashing_protection_service.ts +41 -15
  53. package/src/types.ts +38 -104
  54. package/src/validator_ha_signer.ts +78 -17
  55. package/dest/config.d.ts +0 -101
  56. package/dest/config.d.ts.map +0 -1
  57. package/dest/config.js +0 -92
  58. package/src/config.ts +0 -149
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * PostgreSQL implementation of SlashingProtectionDatabase
3
3
  */
4
- import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
+ import { SlotNumber } from '@aztec/foundation/branded-types';
5
5
  import { randomBytes } from '@aztec/foundation/crypto/random';
6
6
  import { EthAddress } from '@aztec/foundation/eth-address';
7
7
  import { type Logger, createLogger } from '@aztec/foundation/log';
@@ -20,7 +20,7 @@ import {
20
20
  UPDATE_DUTY_SIGNED,
21
21
  } from './schema.js';
22
22
  import type { CheckAndRecordParams, DutyRow, DutyType, InsertOrGetRow, ValidatorDutyRecord } from './types.js';
23
- import { getBlockIndexFromDutyIdentifier } from './types.js';
23
+ import { getBlockIndexFromDutyIdentifier, recordFromFields } from './types.js';
24
24
 
25
25
  /**
26
26
  * Minimal pool interface for database operations.
@@ -107,6 +107,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
107
107
  params.validatorAddress.toString(),
108
108
  params.slot.toString(),
109
109
  params.blockNumber.toString(),
110
+ params.checkpointNumber.toString(),
110
111
  blockIndexWithinCheckpoint,
111
112
  params.dutyType,
112
113
  params.messageHash,
@@ -220,14 +221,17 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
220
221
  }
221
222
 
222
223
  /**
223
- * Convert a database row to a ValidatorDutyRecord
224
+ * Convert a database row to a ValidatorDutyRecord.
225
+ * Maps snake_case column names to StoredDutyRecord (camelCase, ms timestamps),
226
+ * then delegates to the shared recordFromFields() converter.
224
227
  */
225
228
  private rowToRecord(row: DutyRow): ValidatorDutyRecord {
226
- return {
227
- rollupAddress: EthAddress.fromString(row.rollup_address),
228
- validatorAddress: EthAddress.fromString(row.validator_address),
229
- slot: SlotNumber.fromString(row.slot),
230
- blockNumber: BlockNumber.fromString(row.block_number),
229
+ return recordFromFields({
230
+ rollupAddress: row.rollup_address,
231
+ validatorAddress: row.validator_address,
232
+ slot: row.slot,
233
+ blockNumber: row.block_number,
234
+ checkpointNumber: row.checkpoint_number,
231
235
  blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
232
236
  dutyType: row.duty_type,
233
237
  status: row.status,
@@ -235,10 +239,10 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
235
239
  signature: row.signature ?? undefined,
236
240
  nodeId: row.node_id,
237
241
  lockToken: row.lock_token,
238
- startedAt: row.started_at,
239
- completedAt: row.completed_at ?? undefined,
242
+ startedAtMs: row.started_at.getTime(),
243
+ completedAtMs: row.completed_at?.getTime(),
240
244
  errorMessage: row.error_message ?? undefined,
241
- };
245
+ });
242
246
  }
243
247
 
244
248
  /**
@@ -254,8 +258,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
254
258
  * @returns the number of duties cleaned up
255
259
  */
256
260
  async cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
257
- const cutoff = new Date(Date.now() - maxAgeMs);
258
- const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [nodeId, cutoff]);
261
+ const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [nodeId, maxAgeMs]);
259
262
  return result.rowCount ?? 0;
260
263
  }
261
264
 
@@ -277,8 +280,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
277
280
  * @returns the number of duties cleaned up
278
281
  */
279
282
  async cleanupOldDuties(maxAgeMs: number): Promise<number> {
280
- const cutoff = new Date(Date.now() - maxAgeMs);
281
- const result = await this.pool.query(CLEANUP_OLD_DUTIES, [cutoff]);
283
+ const result = await this.pool.query(CLEANUP_OLD_DUTIES, [maxAgeMs]);
282
284
  return result.rowCount ?? 0;
283
285
  }
284
286
  }
package/src/db/schema.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  /**
10
10
  * Current schema version
11
11
  */
12
- export const SCHEMA_VERSION = 1;
12
+ export const SCHEMA_VERSION = 2;
13
13
 
14
14
  /**
15
15
  * SQL to create the validator_duties table
@@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS validator_duties (
20
20
  validator_address VARCHAR(42) NOT NULL,
21
21
  slot BIGINT NOT NULL,
22
22
  block_number BIGINT NOT NULL,
23
+ checkpoint_number BIGINT NOT NULL DEFAULT 0,
23
24
  block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
24
25
  duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
25
26
  status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
@@ -106,6 +107,7 @@ WITH inserted AS (
106
107
  validator_address,
107
108
  slot,
108
109
  block_number,
110
+ checkpoint_number,
109
111
  block_index_within_checkpoint,
110
112
  duty_type,
111
113
  status,
@@ -113,13 +115,14 @@ WITH inserted AS (
113
115
  node_id,
114
116
  lock_token,
115
117
  started_at
116
- ) VALUES ($1, $2, $3, $4, $5, $6, 'signing', $7, $8, $9, CURRENT_TIMESTAMP)
118
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, 'signing', $8, $9, $10, CURRENT_TIMESTAMP)
117
119
  ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
118
120
  RETURNING
119
121
  rollup_address,
120
122
  validator_address,
121
123
  slot,
122
124
  block_number,
125
+ checkpoint_number,
123
126
  block_index_within_checkpoint,
124
127
  duty_type,
125
128
  status,
@@ -139,6 +142,7 @@ SELECT
139
142
  validator_address,
140
143
  slot,
141
144
  block_number,
145
+ checkpoint_number,
142
146
  block_index_within_checkpoint,
143
147
  duty_type,
144
148
  status,
@@ -154,8 +158,8 @@ FROM validator_duties
154
158
  WHERE rollup_address = $1
155
159
  AND validator_address = $2
156
160
  AND slot = $3
157
- AND duty_type = $6
158
- AND block_index_within_checkpoint = $5
161
+ AND duty_type = $7
162
+ AND block_index_within_checkpoint = $6
159
163
  AND NOT EXISTS (SELECT 1 FROM inserted);
160
164
  `;
161
165
 
@@ -203,23 +207,24 @@ WHERE status = 'signed'
203
207
 
204
208
  /**
205
209
  * Query to clean up old duties (for maintenance)
206
- * Removes SIGNED duties older than a specified timestamp
210
+ * Removes SIGNED duties older than a specified age (in milliseconds)
207
211
  */
208
212
  export const CLEANUP_OLD_DUTIES = `
209
213
  DELETE FROM validator_duties
210
214
  WHERE status = 'signed'
211
- AND started_at < $1;
215
+ AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;
212
216
  `;
213
217
 
214
218
  /**
215
219
  * Query to cleanup own stuck duties
216
220
  * Removes duties in 'signing' status for a specific node that are older than maxAgeMs
221
+ * Uses DB's CURRENT_TIMESTAMP to avoid clock skew issues between nodes
217
222
  */
218
223
  export const CLEANUP_OWN_STUCK_DUTIES = `
219
224
  DELETE FROM validator_duties
220
225
  WHERE node_id = $1
221
226
  AND status = 'signing'
222
- AND started_at < $2;
227
+ AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;
223
228
  `;
224
229
 
225
230
  /**
@@ -252,6 +257,7 @@ SELECT
252
257
  validator_address,
253
258
  slot,
254
259
  block_number,
260
+ checkpoint_number,
255
261
  block_index_within_checkpoint,
256
262
  duty_type,
257
263
  status,
package/src/db/types.ts CHANGED
@@ -1,6 +1,7 @@
1
- import type { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
2
- import type { EthAddress } from '@aztec/foundation/eth-address';
1
+ import { BlockNumber, CheckpointNumber, type IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
2
+ import { EthAddress } from '@aztec/foundation/eth-address';
3
3
  import type { Signature } from '@aztec/foundation/eth-signature';
4
+ import { DutyType } from '@aztec/stdlib/ha-signing';
4
5
 
5
6
  /**
6
7
  * Row type from PostgreSQL query
@@ -10,6 +11,7 @@ export interface DutyRow {
10
11
  validator_address: string;
11
12
  slot: string;
12
13
  block_number: string;
14
+ checkpoint_number: string;
13
15
  block_index_within_checkpoint: number;
14
16
  duty_type: DutyType;
15
17
  status: DutyStatus;
@@ -23,24 +25,35 @@ export interface DutyRow {
23
25
  }
24
26
 
25
27
  /**
26
- * Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
28
+ * Plain-primitive representation of a duty record suitable for serialization
29
+ * (e.g. msgpackr for LMDB). All domain types are stored as their string/number
30
+ * equivalents. Timestamps are Unix milliseconds.
27
31
  */
28
- export interface InsertOrGetRow extends DutyRow {
29
- is_new: boolean;
32
+ export interface StoredDutyRecord {
33
+ rollupAddress: string;
34
+ validatorAddress: string;
35
+ slot: string;
36
+ blockNumber: string;
37
+ checkpointNumber: string;
38
+ blockIndexWithinCheckpoint: number;
39
+ dutyType: DutyType;
40
+ status: DutyStatus;
41
+ messageHash: string;
42
+ signature?: string;
43
+ nodeId: string;
44
+ lockToken: string;
45
+ /** Unix timestamp in milliseconds when signing started */
46
+ startedAtMs: number;
47
+ /** Unix timestamp in milliseconds when signing completed */
48
+ completedAtMs?: number;
49
+ errorMessage?: string;
30
50
  }
31
51
 
32
52
  /**
33
- * Type of validator duty being performed
53
+ * Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
34
54
  */
35
- export enum DutyType {
36
- BLOCK_PROPOSAL = 'BLOCK_PROPOSAL',
37
- CHECKPOINT_PROPOSAL = 'CHECKPOINT_PROPOSAL',
38
- ATTESTATION = 'ATTESTATION',
39
- ATTESTATIONS_AND_SIGNERS = 'ATTESTATIONS_AND_SIGNERS',
40
- GOVERNANCE_VOTE = 'GOVERNANCE_VOTE',
41
- SLASHING_VOTE = 'SLASHING_VOTE',
42
- AUTH_REQUEST = 'AUTH_REQUEST',
43
- TXS = 'TXS',
55
+ export interface InsertOrGetRow extends DutyRow {
56
+ is_new: boolean;
44
57
  }
45
58
 
46
59
  /**
@@ -51,8 +64,12 @@ export enum DutyStatus {
51
64
  SIGNED = 'signed',
52
65
  }
53
66
 
67
+ // Re-export DutyType from stdlib
68
+ export { DutyType };
69
+
54
70
  /**
55
- * Record of a validator duty in the database
71
+ * Rich representation of a validator duty, with branded types and Date objects.
72
+ * This is the common output type returned by all SlashingProtectionDatabase implementations.
56
73
  */
57
74
  export interface ValidatorDutyRecord {
58
75
  /** Ethereum address of the rollup contract */
@@ -61,8 +78,10 @@ export interface ValidatorDutyRecord {
61
78
  validatorAddress: EthAddress;
62
79
  /** Slot number for this duty */
63
80
  slot: SlotNumber;
64
- /** Block number for this duty */
81
+ /** Block number for this duty (0 for non-block-proposal duties) */
65
82
  blockNumber: BlockNumber;
83
+ /** Checkpoint number for this duty (0 for attestation and vote duties) */
84
+ checkpointNumber: CheckpointNumber;
66
85
  /** Block index within checkpoint (0, 1, 2... for block proposals, -1 for other duty types) */
67
86
  blockIndexWithinCheckpoint: number;
68
87
  /** Type of duty being performed */
@@ -85,6 +104,32 @@ export interface ValidatorDutyRecord {
85
104
  errorMessage?: string;
86
105
  }
87
106
 
107
+ /**
108
+ * Convert a {@link StoredDutyRecord} (plain-primitive wire format) to a
109
+ * {@link ValidatorDutyRecord} (rich domain type).
110
+ *
111
+ * Shared by LMDB and any future non-Postgres backend implementations.
112
+ */
113
+ export function recordFromFields(stored: StoredDutyRecord): ValidatorDutyRecord {
114
+ return {
115
+ rollupAddress: EthAddress.fromString(stored.rollupAddress),
116
+ validatorAddress: EthAddress.fromString(stored.validatorAddress),
117
+ slot: SlotNumber.fromString(stored.slot),
118
+ blockNumber: BlockNumber.fromString(stored.blockNumber),
119
+ checkpointNumber: CheckpointNumber.fromString(stored.checkpointNumber),
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
+
88
133
  /**
89
134
  * Duty identifier for block proposals.
90
135
  * blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
@@ -163,8 +208,10 @@ export function getBlockIndexFromDutyIdentifier(duty: DutyIdentifier): number {
163
208
  * Additional parameters for checking and recording a new duty
164
209
  */
165
210
  interface CheckAndRecordExtra {
166
- /** Block number for this duty */
167
- blockNumber: BlockNumber | CheckpointNumber;
211
+ /** Block number for this duty (0 for non-block-proposal duties) */
212
+ blockNumber: BlockNumber;
213
+ /** Checkpoint number for this duty (0 for attestation and vote duties) */
214
+ checkpointNumber: CheckpointNumber;
168
215
  /** The signing root (hash) for this duty */
169
216
  messageHash: string;
170
217
  /** Identifier for the node that acquired the lock */
package/src/errors.ts CHANGED
@@ -22,6 +22,27 @@ export class DutyAlreadySignedError extends Error {
22
22
  }
23
23
  }
24
24
 
25
+ /**
26
+ * Thrown when the slashing-protection record for an in-flight signing operation can no longer be
27
+ * updated because it is no longer owned by this node - for example, the stuck-duty cleanup loop
28
+ * deleted the SIGNING row while the remote signer was slow. The produced signature must be
29
+ * discarded rather than broadcast: with no protection record in place, a later attempt for the
30
+ * same duty with different data would sign freely, which is slashable equivocation.
31
+ */
32
+ export class SigningLockLostError extends Error {
33
+ constructor(
34
+ public readonly slot: SlotNumber,
35
+ public readonly dutyType: DutyType,
36
+ public readonly nodeId: string,
37
+ ) {
38
+ super(
39
+ `Slashing protection record for ${dutyType} at slot ${slot} was lost before signing completed ` +
40
+ `(node ${nodeId}); discarding signature`,
41
+ );
42
+ this.name = 'SigningLockLostError';
43
+ }
44
+ }
45
+
25
46
  /**
26
47
  * Thrown when attempting to sign data that conflicts with an already-signed duty.
27
48
  * This means the same validator tried to sign DIFFERENT data for the same slot.
package/src/factory.ts CHANGED
@@ -1,11 +1,18 @@
1
1
  /**
2
2
  * Factory functions for creating validator HA signers
3
3
  */
4
+ import { createLogger } from '@aztec/foundation/log';
5
+ import { DateProvider } from '@aztec/foundation/timer';
6
+ import { createStore } from '@aztec/kv-store/lmdb-v2';
7
+ import type { LocalSignerConfig, ValidatorHASignerConfig } from '@aztec/stdlib/ha-signing';
8
+ import { getTelemetryClient } from '@aztec/telemetry-client';
9
+
4
10
  import { Pool } from 'pg';
5
11
 
6
- import type { ValidatorHASignerConfig } from './config.js';
12
+ import { LmdbSlashingProtectionDatabase, migrateLmdbSlashingProtectionDatabase } from './db/lmdb.js';
7
13
  import { PostgresSlashingProtectionDatabase } from './db/postgres.js';
8
- import type { CreateHASignerDeps, SlashingProtectionDatabase } from './types.js';
14
+ import { HASignerMetrics } from './metrics.js';
15
+ import type { CreateHASignerDeps, CreateLocalSignerWithProtectionDeps, SlashingProtectionDatabase } from './types.js';
9
16
  import { ValidatorHASigner } from './validator_ha_signer.js';
10
17
 
11
18
  /**
@@ -23,10 +30,9 @@ import { ValidatorHASigner } from './validator_ha_signer.js';
23
30
  * ```typescript
24
31
  * const { signer, db } = await createHASigner({
25
32
  * databaseUrl: process.env.DATABASE_URL,
26
- * haSigningEnabled: true,
27
33
  * nodeId: 'validator-node-1',
28
34
  * pollingIntervalMs: 100,
29
- * signingTimeoutMs: 3000,
35
+ * peerSigningTimeoutMs: 3000,
30
36
  * });
31
37
  * signer.start(); // Start background cleanup
32
38
  *
@@ -52,14 +58,19 @@ export async function createHASigner(
52
58
  const { databaseUrl, poolMaxCount, poolMinCount, poolIdleTimeoutMs, poolConnectionTimeoutMs, ...signerConfig } =
53
59
  config;
54
60
 
55
- if (!databaseUrl) {
61
+ const databaseUrlValue = databaseUrl?.getValue();
62
+ if (!databaseUrlValue) {
56
63
  throw new Error('databaseUrl is required for createHASigner');
57
64
  }
65
+
66
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
67
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
68
+
58
69
  // Create connection pool (or use provided pool)
59
70
  let pool: Pool;
60
71
  if (!deps?.pool) {
61
72
  pool = new Pool({
62
- connectionString: databaseUrl,
73
+ connectionString: databaseUrlValue,
63
74
  max: poolMaxCount ?? 10,
64
75
  min: poolMinCount ?? 0,
65
76
  idleTimeoutMillis: poolIdleTimeoutMs ?? 10_000,
@@ -69,14 +80,137 @@ export async function createHASigner(
69
80
  pool = deps.pool;
70
81
  }
71
82
 
83
+ // pg re-emits idle-client errors (e.g. a Postgres restart severing an idle connection) on the
84
+ // pool. Without an 'error' listener, Node escalates these to an uncaughtException and crashes the
85
+ // process - taking down every HA replica sharing the DB at once. pg destroys and replaces the
86
+ // errored client itself, so logging is the only action needed. Log just message/code, never the
87
+ // raw error object (it can carry connection metadata).
88
+ const log = createLogger('validator-ha-signer:factory');
89
+ pool.on('error', (err: NodeJS.ErrnoException) => {
90
+ log.warn('Postgres pool error on idle client', { message: err.message, code: err.code });
91
+ });
92
+
72
93
  // Create database instance
73
94
  const db = new PostgresSlashingProtectionDatabase(pool);
74
95
 
75
96
  // Verify database schema is initialized and version matches
76
97
  await db.initialize();
77
98
 
99
+ // Create metrics
100
+ const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId);
101
+
78
102
  // Create signer
79
- const signer = new ValidatorHASigner(db, { ...signerConfig, databaseUrl });
103
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
104
+
105
+ return { signer, db };
106
+ }
107
+
108
+ /**
109
+ * Create a local (single-node) signing protection signer backed by LMDB.
110
+ *
111
+ * This provides double-signing protection for nodes that are NOT running in a
112
+ * high-availability (multi-node) setup. It prevents a proposer from sending two
113
+ * proposals for the same slot if the node crashes and restarts mid-proposal.
114
+ *
115
+ * `config.dataDirectory` is required so the protection database is persisted to disk and survives
116
+ * crashes/restarts. Booting without it throws, since an ephemeral store silently drops all
117
+ * double-signing protection across restarts. Set `config.allowEphemeralSigningProtection` to opt
118
+ * into the ephemeral store anyway (dev/test networks only) — a loud warning is logged in that case.
119
+ *
120
+ * @param config - Local signer config
121
+ * @param deps - Optional dependencies (telemetry, date provider).
122
+ * @returns An object containing the signer and database instances.
123
+ */
124
+ export async function createLocalSignerWithProtection(
125
+ config: LocalSignerConfig,
126
+ deps?: CreateLocalSignerWithProtectionDeps,
127
+ ): Promise<{
128
+ signer: ValidatorHASigner;
129
+ db: SlashingProtectionDatabase;
130
+ }> {
131
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
132
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
80
133
 
134
+ const log = createLogger('validator-ha-signer:factory');
135
+
136
+ if (!config.dataDirectory) {
137
+ if (!config.allowEphemeralSigningProtection) {
138
+ throw new Error(
139
+ 'Local signing protection requires a persistent data directory, but none was configured. ' +
140
+ 'Set DATA_DIRECTORY so double-signing protection survives restarts, or explicitly opt into an ' +
141
+ 'ephemeral store (dev/test only) with VALIDATOR_ALLOW_EPHEMERAL_SIGNING_PROTECTION=true.',
142
+ );
143
+ }
144
+ log.warn(
145
+ 'Local signing protection is running with an EPHEMERAL store: no data directory is configured. ' +
146
+ 'Double-signing protection will NOT survive a restart. This is unsafe for production validators.',
147
+ );
148
+ }
149
+
150
+ const kvStore = await createStore(
151
+ 'signing-protection',
152
+ LmdbSlashingProtectionDatabase.SCHEMA_VERSION,
153
+ {
154
+ dataDirectory: config.dataDirectory,
155
+ dataStoreMapSizeKb: config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
156
+ rollupAddress: config.rollupAddress,
157
+ },
158
+ undefined,
159
+ {
160
+ onUpgrade: (dataDirectory, currentVersion, latestVersion) =>
161
+ migrateLmdbSlashingProtectionDatabase(
162
+ dataDirectory,
163
+ currentVersion,
164
+ latestVersion,
165
+ config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
166
+ ),
167
+ schemaVersionMismatchPolicy: 'throw',
168
+ versionFileReadFailurePolicy: 'throw',
169
+ },
170
+ );
171
+
172
+ const db = new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
173
+
174
+ const signerConfig = {
175
+ ...config,
176
+ nodeId: config.nodeId || 'local',
177
+ };
178
+
179
+ const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId, 'LocalSigningProtectionMetrics');
180
+
181
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
182
+
183
+ return { signer, db };
184
+ }
185
+
186
+ /**
187
+ * Create an in-memory LMDB-backed SlashingProtectionDatabase that can be shared across
188
+ * multiple validator nodes in the same process. Used for testing HA setups.
189
+ */
190
+ export async function createSharedSlashingProtectionDb(
191
+ dateProvider: DateProvider = new DateProvider(),
192
+ ): Promise<SlashingProtectionDatabase> {
193
+ const kvStore = await createStore('shared-signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
194
+ dataStoreMapSizeKb: 1024 * 1024,
195
+ });
196
+ return new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
197
+ }
198
+
199
+ /**
200
+ * Create a ValidatorHASigner backed by a pre-existing SlashingProtectionDatabase.
201
+ * Used for testing HA setups where multiple nodes share the same protection database.
202
+ */
203
+ export function createSignerFromSharedDb(
204
+ db: SlashingProtectionDatabase,
205
+ config: Pick<
206
+ ValidatorHASignerConfig,
207
+ 'nodeId' | 'pollingIntervalMs' | 'peerSigningTimeoutMs' | 'maxStuckDutiesAgeMs' | 'rollupAddress'
208
+ >,
209
+ deps?: CreateLocalSignerWithProtectionDeps,
210
+ ): { signer: ValidatorHASigner; db: SlashingProtectionDatabase } {
211
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
212
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
213
+ const metrics = new HASignerMetrics(telemetryClient, config.nodeId, 'SharedSigningProtectionMetrics');
214
+ const signer = new ValidatorHASigner(db, config, { metrics, dateProvider });
81
215
  return { signer, db };
82
216
  }
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
+ }