@aztec/validator-ha-signer 0.0.1-commit.ee80a48 → 0.0.1-commit.f103f88

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 +66 -0
  6. package/dest/db/lmdb.d.ts.map +1 -0
  7. package/dest/db/lmdb.js +189 -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 +18 -2
  15. package/dest/db/postgres.d.ts.map +1 -1
  16. package/dest/db/postgres.js +39 -16
  17. package/dest/db/schema.d.ts +16 -9
  18. package/dest/db/schema.d.ts.map +1 -1
  19. package/dest/db/schema.js +23 -9
  20. package/dest/db/types.d.ts +47 -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 +75 -5
  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 +53 -13
  32. package/dest/types.d.ts +31 -70
  33. package/dest/types.d.ts.map +1 -1
  34. package/dest/types.js +4 -20
  35. package/dest/validator_ha_signer.d.ts +13 -5
  36. package/dest/validator_ha_signer.d.ts.map +1 -1
  37. package/dest/validator_ha_signer.js +20 -10
  38. package/package.json +10 -6
  39. package/src/db/index.ts +1 -0
  40. package/src/db/lmdb.ts +265 -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 +40 -13
  44. package/src/db/schema.ts +25 -9
  45. package/src/db/types.ts +67 -20
  46. package/src/factory.ts +93 -4
  47. package/src/metrics.ts +138 -0
  48. package/src/slashing_protection_service.ts +68 -18
  49. package/src/types.ts +53 -104
  50. package/src/validator_ha_signer.ts +38 -14
  51. package/dest/config.d.ts +0 -96
  52. package/dest/config.d.ts.map +0 -1
  53. package/dest/config.js +0 -86
  54. package/src/config.ts +0 -141
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,9 +20,10 @@ 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
- status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed', 'failed')),
26
+ status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
26
27
  message_hash VARCHAR(66) NOT NULL,
27
28
  signature VARCHAR(132),
28
29
  node_id VARCHAR(255) NOT NULL,
@@ -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,34 @@ WHERE status = 'signed'
203
207
 
204
208
  /**
205
209
  * Query to clean up old duties (for maintenance)
206
- * Removes 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
- WHERE status IN ('signing', 'signed', 'failed')
211
- AND started_at < $1;
214
+ WHERE status = 'signed'
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;
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;
223
238
  `;
224
239
 
225
240
  /**
@@ -242,6 +257,7 @@ SELECT
242
257
  validator_address,
243
258
  slot,
244
259
  block_number,
260
+ checkpoint_number,
245
261
  block_index_within_checkpoint,
246
262
  duty_type,
247
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 */
@@ -81,10 +100,36 @@ export interface ValidatorDutyRecord {
81
100
  startedAt: Date;
82
101
  /** When the duty signing was completed (success or failure) */
83
102
  completedAt?: Date;
84
- /** Error message if status is 'failed' */
103
+ /** Error message (currently unused) */
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/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 } 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,
@@ -55,6 +60,10 @@ export async function createHASigner(
55
60
  if (!databaseUrl) {
56
61
  throw new Error('databaseUrl is required for createHASigner');
57
62
  }
63
+
64
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
65
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
66
+
58
67
  // Create connection pool (or use provided pool)
59
68
  let pool: Pool;
60
69
  if (!deps?.pool) {
@@ -75,8 +84,88 @@ export async function createHASigner(
75
84
  // Verify database schema is initialized and version matches
76
85
  await db.initialize();
77
86
 
87
+ // Create metrics
88
+ const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId);
89
+
78
90
  // Create signer
79
- const signer = new ValidatorHASigner(db, { ...signerConfig, databaseUrl });
91
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
80
92
 
81
93
  return { signer, db };
82
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 });
137
+
138
+ return { signer, db };
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
+ }