@aztec/validator-ha-signer 0.0.1-commit.fffb133c → 0.0.1-private.3d175340d2

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 (54) hide show
  1. package/README.md +10 -2
  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 +20 -4
  15. package/dest/db/postgres.d.ts.map +1 -1
  16. package/dest/db/postgres.js +46 -17
  17. package/dest/db/schema.d.ts +18 -11
  18. package/dest/db/schema.d.ts.map +1 -1
  19. package/dest/db/schema.js +45 -23
  20. package/dest/db/types.d.ts +52 -22
  21. package/dest/db/types.d.ts.map +1 -1
  22. package/dest/db/types.js +31 -15
  23. package/dest/factory.d.ts +39 -4
  24. package/dest/factory.d.ts.map +1 -1
  25. package/dest/factory.js +81 -7
  26. package/dest/metrics.d.ts +51 -0
  27. package/dest/metrics.d.ts.map +1 -0
  28. package/dest/metrics.js +103 -0
  29. package/dest/slashing_protection_service.d.ts +19 -6
  30. package/dest/slashing_protection_service.d.ts.map +1 -1
  31. package/dest/slashing_protection_service.js +57 -17
  32. package/dest/types.d.ts +33 -72
  33. package/dest/types.d.ts.map +1 -1
  34. package/dest/types.js +4 -20
  35. package/dest/validator_ha_signer.d.ts +15 -6
  36. package/dest/validator_ha_signer.d.ts.map +1 -1
  37. package/dest/validator_ha_signer.js +26 -11
  38. package/package.json +11 -6
  39. package/src/db/index.ts +1 -0
  40. package/src/db/lmdb.ts +308 -0
  41. package/src/db/migrations/1_initial-schema.ts +35 -4
  42. package/src/db/migrations/2_add-checkpoint-number.ts +19 -0
  43. package/src/db/postgres.ts +47 -12
  44. package/src/db/schema.ts +47 -23
  45. package/src/db/types.ts +72 -20
  46. package/src/factory.ts +111 -6
  47. package/src/metrics.ts +138 -0
  48. package/src/slashing_protection_service.ts +79 -21
  49. package/src/types.ts +56 -103
  50. package/src/validator_ha_signer.ts +44 -15
  51. package/dest/config.d.ts +0 -79
  52. package/dest/config.d.ts.map +0 -1
  53. package/dest/config.js +0 -73
  54. package/src/config.ts +0 -125
package/src/db/schema.ts CHANGED
@@ -9,19 +9,21 @@
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
16
16
  */
17
17
  export const CREATE_VALIDATOR_DUTIES_TABLE = `
18
18
  CREATE TABLE IF NOT EXISTS validator_duties (
19
+ rollup_address VARCHAR(42) NOT NULL,
19
20
  validator_address VARCHAR(42) NOT NULL,
20
21
  slot BIGINT NOT NULL,
21
22
  block_number BIGINT NOT NULL,
23
+ checkpoint_number BIGINT NOT NULL DEFAULT 0,
22
24
  block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
23
25
  duty_type VARCHAR(30) NOT NULL CHECK (duty_type IN ('BLOCK_PROPOSAL', 'CHECKPOINT_PROPOSAL', 'ATTESTATION', 'ATTESTATIONS_AND_SIGNERS', 'GOVERNANCE_VOTE', 'SLASHING_VOTE')),
24
- status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed', 'failed')),
26
+ status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
25
27
  message_hash VARCHAR(66) NOT NULL,
26
28
  signature VARCHAR(132),
27
29
  node_id VARCHAR(255) NOT NULL,
@@ -30,7 +32,7 @@ CREATE TABLE IF NOT EXISTS validator_duties (
30
32
  completed_at TIMESTAMP,
31
33
  error_message TEXT,
32
34
 
33
- PRIMARY KEY (validator_address, slot, duty_type, block_index_within_checkpoint),
35
+ PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
34
36
  CHECK (completed_at IS NULL OR completed_at >= started_at)
35
37
  );
36
38
  `;
@@ -101,9 +103,11 @@ SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;
101
103
  export const INSERT_OR_GET_DUTY = `
102
104
  WITH inserted AS (
103
105
  INSERT INTO validator_duties (
106
+ rollup_address,
104
107
  validator_address,
105
108
  slot,
106
109
  block_number,
110
+ checkpoint_number,
107
111
  block_index_within_checkpoint,
108
112
  duty_type,
109
113
  status,
@@ -111,12 +115,14 @@ WITH inserted AS (
111
115
  node_id,
112
116
  lock_token,
113
117
  started_at
114
- ) VALUES ($1, $2, $3, $4, $5, 'signing', $6, $7, $8, CURRENT_TIMESTAMP)
115
- ON CONFLICT (validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
118
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, 'signing', $8, $9, $10, CURRENT_TIMESTAMP)
119
+ ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
116
120
  RETURNING
121
+ rollup_address,
117
122
  validator_address,
118
123
  slot,
119
124
  block_number,
125
+ checkpoint_number,
120
126
  block_index_within_checkpoint,
121
127
  duty_type,
122
128
  status,
@@ -132,9 +138,11 @@ WITH inserted AS (
132
138
  SELECT * FROM inserted
133
139
  UNION ALL
134
140
  SELECT
141
+ rollup_address,
135
142
  validator_address,
136
143
  slot,
137
144
  block_number,
145
+ checkpoint_number,
138
146
  block_index_within_checkpoint,
139
147
  duty_type,
140
148
  status,
@@ -147,10 +155,11 @@ SELECT
147
155
  error_message,
148
156
  FALSE as is_new
149
157
  FROM validator_duties
150
- WHERE validator_address = $1
151
- AND slot = $2
152
- AND duty_type = $5
153
- AND block_index_within_checkpoint = $4
158
+ WHERE rollup_address = $1
159
+ AND validator_address = $2
160
+ AND slot = $3
161
+ AND duty_type = $7
162
+ AND block_index_within_checkpoint = $6
154
163
  AND NOT EXISTS (SELECT 1 FROM inserted);
155
164
  `;
156
165
 
@@ -162,12 +171,13 @@ UPDATE validator_duties
162
171
  SET status = 'signed',
163
172
  signature = $1,
164
173
  completed_at = CURRENT_TIMESTAMP
165
- WHERE validator_address = $2
166
- AND slot = $3
167
- AND duty_type = $4
168
- AND block_index_within_checkpoint = $5
174
+ WHERE rollup_address = $2
175
+ AND validator_address = $3
176
+ AND slot = $4
177
+ AND duty_type = $5
178
+ AND block_index_within_checkpoint = $6
169
179
  AND status = 'signing'
170
- AND lock_token = $6;
180
+ AND lock_token = $7;
171
181
  `;
172
182
 
173
183
  /**
@@ -176,12 +186,13 @@ WHERE validator_address = $2
176
186
  */
177
187
  export const DELETE_DUTY = `
178
188
  DELETE FROM validator_duties
179
- WHERE validator_address = $1
180
- AND slot = $2
181
- AND duty_type = $3
182
- AND block_index_within_checkpoint = $4
189
+ WHERE rollup_address = $1
190
+ AND validator_address = $2
191
+ AND slot = $3
192
+ AND duty_type = $4
193
+ AND block_index_within_checkpoint = $5
183
194
  AND status = 'signing'
184
- AND lock_token = $5;
195
+ AND lock_token = $6;
185
196
  `;
186
197
 
187
198
  /**
@@ -196,23 +207,34 @@ WHERE status = 'signed'
196
207
 
197
208
  /**
198
209
  * Query to clean up old duties (for maintenance)
199
- * Removes duties older than a specified timestamp
210
+ * Removes SIGNED duties older than a specified age (in milliseconds)
200
211
  */
201
212
  export const CLEANUP_OLD_DUTIES = `
202
213
  DELETE FROM validator_duties
203
- WHERE status IN ('signing', 'signed', 'failed')
204
- AND started_at < $1;
214
+ WHERE status = 'signed'
215
+ AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;
205
216
  `;
206
217
 
207
218
  /**
208
219
  * Query to cleanup own stuck duties
209
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
210
222
  */
211
223
  export const CLEANUP_OWN_STUCK_DUTIES = `
212
224
  DELETE FROM validator_duties
213
225
  WHERE node_id = $1
214
226
  AND status = 'signing'
215
- AND started_at < $2;
227
+ AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;
228
+ `;
229
+
230
+ /**
231
+ * Query to cleanup duties with outdated rollup address
232
+ * Removes all duties where the rollup address doesn't match the current one
233
+ * Used after a rollup upgrade to clean up duties for the old rollup
234
+ */
235
+ export const CLEANUP_OUTDATED_ROLLUP_DUTIES = `
236
+ DELETE FROM validator_duties
237
+ WHERE rollup_address != $1;
216
238
  `;
217
239
 
218
240
  /**
@@ -231,9 +253,11 @@ export const DROP_SCHEMA_VERSION_TABLE = `DROP TABLE IF EXISTS schema_version;`;
231
253
  */
232
254
  export const GET_STUCK_DUTIES = `
233
255
  SELECT
256
+ rollup_address,
234
257
  validator_address,
235
258
  slot,
236
259
  block_number,
260
+ checkpoint_number,
237
261
  block_index_within_checkpoint,
238
262
  duty_type,
239
263
  status,
package/src/db/types.ts CHANGED
@@ -1,14 +1,17 @@
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
7
8
  */
8
9
  export interface DutyRow {
10
+ rollup_address: string;
9
11
  validator_address: string;
10
12
  slot: string;
11
13
  block_number: string;
14
+ checkpoint_number: string;
12
15
  block_index_within_checkpoint: number;
13
16
  duty_type: DutyType;
14
17
  status: DutyStatus;
@@ -22,24 +25,35 @@ export interface DutyRow {
22
25
  }
23
26
 
24
27
  /**
25
- * 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.
26
31
  */
27
- export interface InsertOrGetRow extends DutyRow {
28
- 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;
29
50
  }
30
51
 
31
52
  /**
32
- * Type of validator duty being performed
53
+ * Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
33
54
  */
34
- export enum DutyType {
35
- BLOCK_PROPOSAL = 'BLOCK_PROPOSAL',
36
- CHECKPOINT_PROPOSAL = 'CHECKPOINT_PROPOSAL',
37
- ATTESTATION = 'ATTESTATION',
38
- ATTESTATIONS_AND_SIGNERS = 'ATTESTATIONS_AND_SIGNERS',
39
- GOVERNANCE_VOTE = 'GOVERNANCE_VOTE',
40
- SLASHING_VOTE = 'SLASHING_VOTE',
41
- AUTH_REQUEST = 'AUTH_REQUEST',
42
- TXS = 'TXS',
55
+ export interface InsertOrGetRow extends DutyRow {
56
+ is_new: boolean;
43
57
  }
44
58
 
45
59
  /**
@@ -50,16 +64,24 @@ export enum DutyStatus {
50
64
  SIGNED = 'signed',
51
65
  }
52
66
 
67
+ // Re-export DutyType from stdlib
68
+ export { DutyType };
69
+
53
70
  /**
54
- * 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.
55
73
  */
56
74
  export interface ValidatorDutyRecord {
75
+ /** Ethereum address of the rollup contract */
76
+ rollupAddress: EthAddress;
57
77
  /** Ethereum address of the validator */
58
78
  validatorAddress: EthAddress;
59
79
  /** Slot number for this duty */
60
80
  slot: SlotNumber;
61
- /** Block number for this duty */
81
+ /** Block number for this duty (0 for non-block-proposal duties) */
62
82
  blockNumber: BlockNumber;
83
+ /** Checkpoint number for this duty (0 for attestation and vote duties) */
84
+ checkpointNumber: CheckpointNumber;
63
85
  /** Block index within checkpoint (0, 1, 2... for block proposals, -1 for other duty types) */
64
86
  blockIndexWithinCheckpoint: number;
65
87
  /** Type of duty being performed */
@@ -78,15 +100,42 @@ export interface ValidatorDutyRecord {
78
100
  startedAt: Date;
79
101
  /** When the duty signing was completed (success or failure) */
80
102
  completedAt?: Date;
81
- /** Error message if status is 'failed' */
103
+ /** Error message (currently unused) */
82
104
  errorMessage?: string;
83
105
  }
84
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
+
85
133
  /**
86
134
  * Duty identifier for block proposals.
87
135
  * blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
88
136
  */
89
137
  export interface BlockProposalDutyIdentifier {
138
+ rollupAddress: EthAddress;
90
139
  validatorAddress: EthAddress;
91
140
  slot: SlotNumber;
92
141
  /** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
@@ -99,6 +148,7 @@ export interface BlockProposalDutyIdentifier {
99
148
  * blockIndexWithinCheckpoint is not applicable (internally stored as -1).
100
149
  */
101
150
  export interface OtherDutyIdentifier {
151
+ rollupAddress: EthAddress;
102
152
  validatorAddress: EthAddress;
103
153
  slot: SlotNumber;
104
154
  dutyType:
@@ -158,8 +208,10 @@ export function getBlockIndexFromDutyIdentifier(duty: DutyIdentifier): number {
158
208
  * Additional parameters for checking and recording a new duty
159
209
  */
160
210
  interface CheckAndRecordExtra {
161
- /** Block number for this duty */
162
- 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;
163
215
  /** The signing root (hash) for this duty */
164
216
  messageHash: string;
165
217
  /** Identifier for the node that acquired the lock */
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 { ValidatorHASignerConfig } from './config.js';
11
+ import { LmdbSlashingProtectionDatabase, migrateLmdbSlashingProtectionDatabase } 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
- * haSigningEnabled: true,
27
32
  * nodeId: 'validator-node-1',
28
33
  * pollingIntervalMs: 100,
29
34
  * signingTimeoutMs: 3000,
@@ -52,14 +57,19 @@ export async function createHASigner(
52
57
  const { databaseUrl, poolMaxCount, poolMinCount, poolIdleTimeoutMs, poolConnectionTimeoutMs, ...signerConfig } =
53
58
  config;
54
59
 
55
- if (!databaseUrl) {
60
+ const databaseUrlValue = databaseUrl?.getValue();
61
+ if (!databaseUrlValue) {
56
62
  throw new Error('databaseUrl is required for createHASigner');
57
63
  }
64
+
65
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
66
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
67
+
58
68
  // Create connection pool (or use provided pool)
59
69
  let pool: Pool;
60
70
  if (!deps?.pool) {
61
71
  pool = new Pool({
62
- connectionString: databaseUrl,
72
+ connectionString: databaseUrlValue,
63
73
  max: poolMaxCount ?? 10,
64
74
  min: poolMinCount ?? 0,
65
75
  idleTimeoutMillis: poolIdleTimeoutMs ?? 10_000,
@@ -75,8 +85,103 @@ export async function createHASigner(
75
85
  // Verify database schema is initialized and version matches
76
86
  await db.initialize();
77
87
 
88
+ // Create metrics
89
+ const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId);
90
+
78
91
  // Create signer
79
- const signer = new ValidatorHASigner(db, { ...signerConfig, databaseUrl });
92
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
80
93
 
81
94
  return { signer, db };
82
95
  }
96
+
97
+ /**
98
+ * Create a local (single-node) signing protection signer backed by LMDB.
99
+ *
100
+ * This provides double-signing protection for nodes that are NOT running in a
101
+ * high-availability (multi-node) setup. It prevents a proposer from sending two
102
+ * proposals for the same slot if the node crashes and restarts mid-proposal.
103
+ *
104
+ * When `config.dataDirectory` is set, the protection database is persisted to disk
105
+ * and survives crashes/restarts. When unset, an ephemeral in-memory store is
106
+ * used which protects within a single run but not across restarts.
107
+ *
108
+ * @param config - Local signer config
109
+ * @param deps - Optional dependencies (telemetry, date provider).
110
+ * @returns An object containing the signer and database instances.
111
+ */
112
+ export async function createLocalSignerWithProtection(
113
+ config: LocalSignerConfig,
114
+ deps?: CreateLocalSignerWithProtectionDeps,
115
+ ): Promise<{
116
+ signer: ValidatorHASigner;
117
+ db: SlashingProtectionDatabase;
118
+ }> {
119
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
120
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
121
+
122
+ const kvStore = await createStore(
123
+ 'signing-protection',
124
+ LmdbSlashingProtectionDatabase.SCHEMA_VERSION,
125
+ {
126
+ dataDirectory: config.dataDirectory,
127
+ dataStoreMapSizeKb: config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
128
+ rollupAddress: config.rollupAddress,
129
+ },
130
+ undefined,
131
+ {
132
+ onUpgrade: (dataDirectory, currentVersion, latestVersion) =>
133
+ migrateLmdbSlashingProtectionDatabase(
134
+ dataDirectory,
135
+ currentVersion,
136
+ latestVersion,
137
+ config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
138
+ ),
139
+ schemaVersionMismatchPolicy: 'throw',
140
+ },
141
+ );
142
+
143
+ const db = new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
144
+
145
+ const signerConfig = {
146
+ ...config,
147
+ nodeId: config.nodeId || 'local',
148
+ };
149
+
150
+ const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId, 'LocalSigningProtectionMetrics');
151
+
152
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
153
+
154
+ return { signer, db };
155
+ }
156
+
157
+ /**
158
+ * Create an in-memory LMDB-backed SlashingProtectionDatabase that can be shared across
159
+ * multiple validator nodes in the same process. Used for testing HA setups.
160
+ */
161
+ export async function createSharedSlashingProtectionDb(
162
+ dateProvider: DateProvider = new DateProvider(),
163
+ ): Promise<SlashingProtectionDatabase> {
164
+ const kvStore = await createStore('shared-signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
165
+ dataStoreMapSizeKb: 1024 * 1024,
166
+ });
167
+ return new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
168
+ }
169
+
170
+ /**
171
+ * Create a ValidatorHASigner backed by a pre-existing SlashingProtectionDatabase.
172
+ * Used for testing HA setups where multiple nodes share the same protection database.
173
+ */
174
+ export function createSignerFromSharedDb(
175
+ db: SlashingProtectionDatabase,
176
+ config: Pick<
177
+ ValidatorHASignerConfig,
178
+ 'nodeId' | 'pollingIntervalMs' | 'signingTimeoutMs' | 'maxStuckDutiesAgeMs' | 'rollupAddress'
179
+ >,
180
+ deps?: CreateLocalSignerWithProtectionDeps,
181
+ ): { signer: ValidatorHASigner; db: SlashingProtectionDatabase } {
182
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
183
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
184
+ const metrics = new HASignerMetrics(telemetryClient, config.nodeId, 'SharedSigningProtectionMetrics');
185
+ const signer = new ValidatorHASigner(db, config, { metrics, dateProvider });
186
+ return { signer, db };
187
+ }
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
+ }