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

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 (46) 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 +188 -0
  8. package/dest/db/postgres.d.ts +18 -2
  9. package/dest/db/postgres.d.ts.map +1 -1
  10. package/dest/db/postgres.js +37 -16
  11. package/dest/db/schema.d.ts +13 -6
  12. package/dest/db/schema.d.ts.map +1 -1
  13. package/dest/db/schema.js +14 -5
  14. package/dest/db/types.d.ts +38 -19
  15. package/dest/db/types.d.ts.map +1 -1
  16. package/dest/db/types.js +30 -15
  17. package/dest/factory.d.ts +22 -4
  18. package/dest/factory.d.ts.map +1 -1
  19. package/dest/factory.js +50 -5
  20. package/dest/metrics.d.ts +51 -0
  21. package/dest/metrics.d.ts.map +1 -0
  22. package/dest/metrics.js +103 -0
  23. package/dest/slashing_protection_service.d.ts +19 -6
  24. package/dest/slashing_protection_service.d.ts.map +1 -1
  25. package/dest/slashing_protection_service.js +51 -11
  26. package/dest/types.d.ts +30 -70
  27. package/dest/types.d.ts.map +1 -1
  28. package/dest/types.js +3 -20
  29. package/dest/validator_ha_signer.d.ts +13 -5
  30. package/dest/validator_ha_signer.d.ts.map +1 -1
  31. package/dest/validator_ha_signer.js +18 -10
  32. package/package.json +10 -6
  33. package/src/db/index.ts +1 -0
  34. package/src/db/lmdb.ts +264 -0
  35. package/src/db/postgres.ts +38 -13
  36. package/src/db/schema.ts +16 -5
  37. package/src/db/types.ts +62 -17
  38. package/src/factory.ts +61 -4
  39. package/src/metrics.ts +138 -0
  40. package/src/slashing_protection_service.ts +66 -16
  41. package/src/types.ts +47 -104
  42. package/src/validator_ha_signer.ts +35 -14
  43. package/dest/config.d.ts +0 -96
  44. package/dest/config.d.ts.map +0 -1
  45. package/dest/config.js +0 -86
  46. package/src/config.ts +0 -141
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
+ }
@@ -7,6 +7,8 @@
7
7
  import { type Logger, createLogger } from '@aztec/foundation/log';
8
8
  import { RunningPromise } from '@aztec/foundation/promise';
9
9
  import { sleep } from '@aztec/foundation/sleep';
10
+ import type { DateProvider } from '@aztec/foundation/timer';
11
+ import type { BaseSignerConfig } from '@aztec/stdlib/ha-signing';
10
12
 
11
13
  import {
12
14
  type CheckAndRecordParams,
@@ -16,7 +18,13 @@ import {
16
18
  getBlockIndexFromDutyIdentifier,
17
19
  } from './db/types.js';
18
20
  import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
19
- import type { SlashingProtectionDatabase, ValidatorHASignerConfig } from './types.js';
21
+ import type { HASignerMetrics } from './metrics.js';
22
+ import type { SlashingProtectionDatabase } from './types.js';
23
+
24
+ export interface SlashingProtectionServiceDeps {
25
+ metrics: HASignerMetrics;
26
+ dateProvider: DateProvider;
27
+ }
20
28
 
21
29
  /**
22
30
  * Slashing Protection Service
@@ -39,11 +47,16 @@ export class SlashingProtectionService {
39
47
  private readonly signingTimeoutMs: number;
40
48
  private readonly maxStuckDutiesAgeMs: number;
41
49
 
50
+ private readonly metrics: HASignerMetrics;
51
+ private readonly dateProvider: DateProvider;
52
+
42
53
  private cleanupRunningPromise: RunningPromise;
54
+ private lastOldDutiesCleanupAtMs?: number;
43
55
 
44
56
  constructor(
45
57
  private readonly db: SlashingProtectionDatabase,
46
- private readonly config: ValidatorHASignerConfig,
58
+ private readonly config: BaseSignerConfig,
59
+ deps: SlashingProtectionServiceDeps,
47
60
  ) {
48
61
  this.log = createLogger('slashing-protection');
49
62
  this.pollingIntervalMs = config.pollingIntervalMs;
@@ -51,11 +64,9 @@ export class SlashingProtectionService {
51
64
  // Default to 144s (2x 72s Aztec slot duration) if not explicitly configured
52
65
  this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? 144_000;
53
66
 
54
- this.cleanupRunningPromise = new RunningPromise(
55
- this.cleanupStuckDuties.bind(this),
56
- this.log,
57
- this.maxStuckDutiesAgeMs,
58
- );
67
+ this.cleanupRunningPromise = new RunningPromise(this.cleanup.bind(this), this.log, this.maxStuckDutiesAgeMs);
68
+ this.metrics = deps.metrics;
69
+ this.dateProvider = deps.dateProvider;
59
70
  }
60
71
 
61
72
  /**
@@ -67,7 +78,6 @@ export class SlashingProtectionService {
67
78
  * 2. If insert succeeds, we acquired the lock - return the lockToken
68
79
  * 3. If a record exists, handle based on status:
69
80
  * - SIGNED: Throw appropriate error (already signed or slashing protection)
70
- * - FAILED: Delete the failed record
71
81
  * - SIGNING: Wait and poll until status changes, then handle result
72
82
  *
73
83
  * @returns The lockToken that must be used for recordSuccess/deleteDuty
@@ -76,7 +86,7 @@ export class SlashingProtectionService {
76
86
  */
77
87
  async checkAndRecord(params: CheckAndRecordParams): Promise<string> {
78
88
  const { validatorAddress, slot, dutyType, messageHash, nodeId } = params;
79
- const startTime = Date.now();
89
+ const startTime = this.dateProvider.now();
80
90
 
81
91
  this.log.debug(`Checking duty: ${dutyType} for slot ${slot}`, {
82
92
  validatorAddress: validatorAddress.toString(),
@@ -93,6 +103,7 @@ export class SlashingProtectionService {
93
103
  validatorAddress: validatorAddress.toString(),
94
104
  nodeId,
95
105
  });
106
+ this.metrics.recordLockAcquire(true);
96
107
  return record.lockToken;
97
108
  }
98
109
 
@@ -107,6 +118,7 @@ export class SlashingProtectionService {
107
118
  existingNodeId: record.nodeId,
108
119
  attemptingNodeId: nodeId,
109
120
  });
121
+ this.metrics.recordSlashingProtection(dutyType);
110
122
  throw new SlashingProtectionError(
111
123
  slot,
112
124
  dutyType,
@@ -116,15 +128,17 @@ export class SlashingProtectionService {
116
128
  record.nodeId,
117
129
  );
118
130
  }
131
+ this.metrics.recordDutyAlreadySigned(dutyType);
119
132
  throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, record.nodeId);
120
133
  } else if (record.status === DutyStatus.SIGNING) {
121
134
  // Another node is currently signing - check for timeout
122
- if (Date.now() - startTime > this.signingTimeoutMs) {
135
+ if (this.dateProvider.now() - startTime > this.signingTimeoutMs) {
123
136
  this.log.warn(`Timeout waiting for signing to complete for duty ${dutyType} at slot ${slot}`, {
124
137
  validatorAddress: validatorAddress.toString(),
125
138
  timeoutMs: this.signingTimeoutMs,
126
139
  signingNodeId: record.nodeId,
127
140
  });
141
+ this.metrics.recordDutyAlreadySigned(dutyType);
128
142
  throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, 'unknown (timeout)');
129
143
  }
130
144
 
@@ -221,7 +235,20 @@ export class SlashingProtectionService {
221
235
  * Start running tasks.
222
236
  * Cleanup runs immediately on start to recover from any previous crashes.
223
237
  */
224
- start() {
238
+ /**
239
+ * Start the background cleanup task.
240
+ * Also performs one-time cleanup of duties with outdated rollup addresses.
241
+ */
242
+ async start() {
243
+ // One-time cleanup at startup: remove duties from previous rollup versions
244
+ const numOutdatedRollupDuties = await this.db.cleanupOutdatedRollupDuties(this.config.l1Contracts.rollupAddress);
245
+ if (numOutdatedRollupDuties > 0) {
246
+ this.log.info(`Cleaned up ${numOutdatedRollupDuties} duties with outdated rollup address at startup`, {
247
+ currentRollupAddress: this.config.l1Contracts.rollupAddress.toString(),
248
+ });
249
+ this.metrics.recordCleanup('outdated_rollup', numOutdatedRollupDuties);
250
+ }
251
+
225
252
  this.cleanupRunningPromise.start();
226
253
  this.log.info('Slashing protection service started', { nodeId: this.config.nodeId });
227
254
  }
@@ -244,15 +271,38 @@ export class SlashingProtectionService {
244
271
  }
245
272
 
246
273
  /**
247
- * Cleanup own stuck duties
274
+ * Periodic cleanup of stuck duties and optionally old signed duties.
275
+ * Runs in the background via RunningPromise.
248
276
  */
249
- private async cleanupStuckDuties() {
250
- const numDuties = await this.db.cleanupOwnStuckDuties(this.config.nodeId, this.maxStuckDutiesAgeMs);
251
- if (numDuties > 0) {
252
- this.log.info(`Cleaned up ${numDuties} stuck duties`, {
277
+ private async cleanup() {
278
+ // 1. Clean up stuck duties (our own node's duties that got stuck in 'signing' status)
279
+ const numStuckDuties = await this.db.cleanupOwnStuckDuties(this.config.nodeId, this.maxStuckDutiesAgeMs);
280
+ if (numStuckDuties > 0) {
281
+ this.log.verbose(`Cleaned up ${numStuckDuties} stuck duties`, {
253
282
  nodeId: this.config.nodeId,
254
283
  maxStuckDutiesAgeMs: this.maxStuckDutiesAgeMs,
255
284
  });
285
+ this.metrics.recordCleanup('stuck', numStuckDuties);
286
+ }
287
+
288
+ // 2. Clean up old signed duties if configured
289
+ // we shouldn't run this as often as stuck duty cleanup.
290
+ if (this.config.cleanupOldDutiesAfterHours !== undefined) {
291
+ const maxAgeMs = this.config.cleanupOldDutiesAfterHours * 60 * 60 * 1000;
292
+ const nowMs = this.dateProvider.now();
293
+ const shouldRun =
294
+ this.lastOldDutiesCleanupAtMs === undefined || nowMs - this.lastOldDutiesCleanupAtMs >= maxAgeMs;
295
+ if (shouldRun) {
296
+ const numOldDuties = await this.db.cleanupOldDuties(maxAgeMs);
297
+ this.lastOldDutiesCleanupAtMs = nowMs;
298
+ if (numOldDuties > 0) {
299
+ this.log.verbose(`Cleaned up ${numOldDuties} old signed duties`, {
300
+ cleanupOldDutiesAfterHours: this.config.cleanupOldDutiesAfterHours,
301
+ maxAgeMs,
302
+ });
303
+ this.metrics.recordCleanup('old', numOldDuties);
304
+ }
305
+ }
256
306
  }
257
307
  }
258
308
  }
package/src/types.ts CHANGED
@@ -1,24 +1,27 @@
1
- import {
2
- BlockNumber,
3
- type CheckpointNumber,
4
- type IndexWithinCheckpoint,
5
- type SlotNumber,
6
- } from '@aztec/foundation/branded-types';
1
+ import { SlotNumber } from '@aztec/foundation/branded-types';
7
2
  import type { EthAddress } from '@aztec/foundation/eth-address';
3
+ import { DateProvider } from '@aztec/foundation/timer';
4
+ import {
5
+ DutyType,
6
+ type HAProtectedSigningContext,
7
+ type SigningContext,
8
+ type ValidatorHASignerConfig,
9
+ getBlockNumberFromSigningContext as getBlockNumberFromSigningContextFromStdlib,
10
+ isHAProtectedContext,
11
+ } from '@aztec/stdlib/ha-signing';
12
+ import type { TelemetryClient } from '@aztec/telemetry-client';
8
13
 
9
14
  import type { Pool } from 'pg';
10
15
 
11
- import type { ValidatorHASignerConfig } from './config.js';
12
- import {
13
- type BlockProposalDutyIdentifier,
14
- type CheckAndRecordParams,
15
- type DeleteDutyParams,
16
- type DutyIdentifier,
17
- type DutyRow,
18
- DutyType,
19
- type OtherDutyIdentifier,
20
- type RecordSuccessParams,
21
- type ValidatorDutyRecord,
16
+ import type {
17
+ BlockProposalDutyIdentifier,
18
+ CheckAndRecordParams,
19
+ DeleteDutyParams,
20
+ DutyIdentifier,
21
+ DutyRow,
22
+ OtherDutyIdentifier,
23
+ RecordSuccessParams,
24
+ ValidatorDutyRecord,
22
25
  } from './db/types.js';
23
26
 
24
27
  export type {
@@ -27,12 +30,16 @@ export type {
27
30
  DeleteDutyParams,
28
31
  DutyIdentifier,
29
32
  DutyRow,
33
+ HAProtectedSigningContext,
30
34
  OtherDutyIdentifier,
31
35
  RecordSuccessParams,
36
+ SigningContext,
32
37
  ValidatorDutyRecord,
33
38
  ValidatorHASignerConfig,
34
39
  };
35
40
  export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
41
+ export { isHAProtectedContext };
42
+ export { getBlockNumberFromSigningContextFromStdlib as getBlockNumberFromSigningContext };
36
43
 
37
44
  /**
38
45
  * Result of tryInsertOrGetExisting operation
@@ -53,99 +60,20 @@ export interface CreateHASignerDeps {
53
60
  * If provided, databaseUrl and poolConfig are ignored
54
61
  */
55
62
  pool?: Pool;
56
- }
57
-
58
- /**
59
- * Base context for signing operations
60
- */
61
- interface BaseSigningContext {
62
- /** Slot number for this duty */
63
- slot: SlotNumber;
64
63
  /**
65
- * Block or checkpoint number for this duty.
66
- * For block proposals, this is the block number.
67
- * For checkpoint proposals, this is the checkpoint number.
64
+ * Optional telemetry client for metrics
68
65
  */
69
- blockNumber: BlockNumber | CheckpointNumber;
70
- }
71
-
72
- /**
73
- * Signing context for block proposals.
74
- * blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
75
- */
76
- export interface BlockProposalSigningContext extends BaseSigningContext {
77
- /** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
78
- blockIndexWithinCheckpoint: IndexWithinCheckpoint;
79
- dutyType: DutyType.BLOCK_PROPOSAL;
80
- }
81
-
82
- /**
83
- * Signing context for non-block-proposal duties that require HA protection.
84
- * blockIndexWithinCheckpoint is not applicable (internally always -1).
85
- */
86
- export interface OtherSigningContext extends BaseSigningContext {
87
- dutyType: DutyType.CHECKPOINT_PROPOSAL | DutyType.ATTESTATION | DutyType.ATTESTATIONS_AND_SIGNERS;
88
- }
89
-
90
- /**
91
- * Signing context for governance/slashing votes which only need slot for HA protection.
92
- * blockNumber is not applicable (internally always 0).
93
- */
94
- export interface VoteSigningContext {
95
- slot: SlotNumber;
96
- dutyType: DutyType.GOVERNANCE_VOTE | DutyType.SLASHING_VOTE;
97
- }
98
-
99
- /**
100
- * Signing context for duties which don't require slot/blockNumber
101
- * as they don't need HA protection (AUTH_REQUEST, TXS).
102
- */
103
- export interface NoHAProtectionSigningContext {
104
- dutyType: DutyType.AUTH_REQUEST | DutyType.TXS;
105
- }
106
-
107
- /**
108
- * Signing contexts that require HA protection (excludes AUTH_REQUEST).
109
- * Used by the HA signer's signWithProtection method.
110
- */
111
- export type HAProtectedSigningContext = BlockProposalSigningContext | OtherSigningContext | VoteSigningContext;
112
-
113
- /**
114
- * Type guard to check if a SigningContext requires HA protection.
115
- * Returns true for contexts that need HA protection, false for AUTH_REQUEST and TXS.
116
- */
117
- export function isHAProtectedContext(context: SigningContext): context is HAProtectedSigningContext {
118
- return context.dutyType !== DutyType.AUTH_REQUEST && context.dutyType !== DutyType.TXS;
119
- }
120
-
121
- /**
122
- * Gets the block number from a signing context.
123
- * - Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE): returns BlockNumber(0)
124
- * - Other duties: returns the blockNumber from the context
125
- */
126
- export function getBlockNumberFromSigningContext(context: HAProtectedSigningContext): BlockNumber | CheckpointNumber {
127
- // Check for duty types that have blockNumber
128
- if (
129
- context.dutyType === DutyType.BLOCK_PROPOSAL ||
130
- context.dutyType === DutyType.CHECKPOINT_PROPOSAL ||
131
- context.dutyType === DutyType.ATTESTATION ||
132
- context.dutyType === DutyType.ATTESTATIONS_AND_SIGNERS
133
- ) {
134
- return context.blockNumber;
135
- }
136
- // Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE) don't have blockNumber
137
- return BlockNumber(0);
66
+ telemetryClient?: TelemetryClient;
67
+ /**
68
+ * Optional date provider for timestamps
69
+ */
70
+ dateProvider?: DateProvider;
138
71
  }
139
72
 
140
73
  /**
141
- * Context required for slashing protection during signing operations.
142
- * Uses discriminated union to enforce type safety:
143
- * - BLOCK_PROPOSAL duties MUST have blockIndexWithinCheckpoint >= 0
144
- * - Other duty types do NOT have blockIndexWithinCheckpoint (internally -1)
145
- * - Vote duties only need slot (blockNumber is internally 0)
146
- * - AUTH_REQUEST and TXS duties don't need slot/blockNumber (no HA protection needed)
74
+ * deps for creating a local signing protection signer
147
75
  */
148
- export type SigningContext = HAProtectedSigningContext | NoHAProtectionSigningContext;
76
+ export type CreateLocalSignerWithProtectionDeps = Omit<CreateHASignerDeps, 'pool'>;
149
77
 
150
78
  /**
151
79
  * Database interface for slashing protection operations
@@ -203,6 +131,21 @@ export interface SlashingProtectionDatabase {
203
131
  */
204
132
  cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number>;
205
133
 
134
+ /**
135
+ * Cleanup duties with outdated rollup address.
136
+ * Removes all duties where the rollup address doesn't match the current one.
137
+ * Used after a rollup upgrade to clean up duties for the old rollup.
138
+ * @returns the number of duties cleaned up
139
+ */
140
+ cleanupOutdatedRollupDuties(currentRollupAddress: EthAddress): Promise<number>;
141
+
142
+ /**
143
+ * Cleanup old signed duties.
144
+ * Removes only signed duties older than the specified age.
145
+ * @returns the number of duties cleaned up
146
+ */
147
+ cleanupOldDuties(maxAgeMs: number): Promise<number>;
148
+
206
149
  /**
207
150
  * Close the database connection.
208
151
  * Should be called during graceful shutdown.
@@ -9,15 +9,23 @@ import type { Buffer32 } from '@aztec/foundation/buffer';
9
9
  import { EthAddress } from '@aztec/foundation/eth-address';
10
10
  import type { Signature } from '@aztec/foundation/eth-signature';
11
11
  import { type Logger, createLogger } from '@aztec/foundation/log';
12
-
13
- import type { ValidatorHASignerConfig } from './config.js';
14
- import { type DutyIdentifier, DutyType } from './db/types.js';
15
- import { SlashingProtectionService } from './slashing_protection_service.js';
12
+ import type { DateProvider } from '@aztec/foundation/timer';
16
13
  import {
14
+ type BaseSignerConfig,
15
+ DutyType,
17
16
  type HAProtectedSigningContext,
18
- type SlashingProtectionDatabase,
19
17
  getBlockNumberFromSigningContext,
20
- } from './types.js';
18
+ } from '@aztec/stdlib/ha-signing';
19
+
20
+ import type { DutyIdentifier } from './db/types.js';
21
+ import type { HASignerMetrics } from './metrics.js';
22
+ import { SlashingProtectionService } from './slashing_protection_service.js';
23
+ import type { SlashingProtectionDatabase } from './types.js';
24
+
25
+ export interface ValidatorHASignerDeps {
26
+ metrics: HASignerMetrics;
27
+ dateProvider: DateProvider;
28
+ }
21
29
 
22
30
  /**
23
31
  * Validator High Availability Signer
@@ -43,22 +51,27 @@ export class ValidatorHASigner {
43
51
  private readonly slashingProtection: SlashingProtectionService;
44
52
  private readonly rollupAddress: EthAddress;
45
53
 
54
+ private readonly dateProvider: DateProvider;
55
+ private readonly metrics: HASignerMetrics;
56
+
46
57
  constructor(
47
58
  db: SlashingProtectionDatabase,
48
- private readonly config: ValidatorHASignerConfig,
59
+ private readonly config: BaseSignerConfig,
60
+ deps: ValidatorHASignerDeps,
49
61
  ) {
50
62
  this.log = createLogger('validator-ha-signer');
51
63
 
52
- if (!config.haSigningEnabled) {
53
- // this shouldn't happen, the validator should use different signer for non-HA setups
54
- throw new Error('Validator HA Signer is not enabled in config');
55
- }
64
+ this.metrics = deps.metrics;
65
+ this.dateProvider = deps.dateProvider;
56
66
 
57
67
  if (!config.nodeId || config.nodeId === '') {
58
68
  throw new Error('NODE_ID is required for high-availability setups');
59
69
  }
60
70
  this.rollupAddress = config.l1Contracts.rollupAddress;
61
- this.slashingProtection = new SlashingProtectionService(db, config);
71
+ this.slashingProtection = new SlashingProtectionService(db, config, {
72
+ metrics: deps.metrics,
73
+ dateProvider: deps.dateProvider,
74
+ });
62
75
  this.log.info('Validator HA Signer initialized with slashing protection', {
63
76
  nodeId: config.nodeId,
64
77
  rollupAddress: this.rollupAddress.toString(),
@@ -88,6 +101,9 @@ export class ValidatorHASigner {
88
101
  context: HAProtectedSigningContext,
89
102
  signFn: (messageHash: Buffer32) => Promise<Signature>,
90
103
  ): Promise<Signature> {
104
+ const startTime = this.dateProvider.now();
105
+ const dutyType = context.dutyType;
106
+
91
107
  let dutyIdentifier: DutyIdentifier;
92
108
  if (context.dutyType === DutyType.BLOCK_PROPOSAL) {
93
109
  dutyIdentifier = {
@@ -107,6 +123,7 @@ export class ValidatorHASigner {
107
123
  }
108
124
 
109
125
  // Acquire lock and get the token for ownership verification
126
+ // DutyAlreadySignedError and SlashingProtectionError may be thrown here and are recorded in the service
110
127
  const blockNumber = getBlockNumberFromSigningContext(context);
111
128
  const lockToken = await this.slashingProtection.checkAndRecord({
112
129
  ...dutyIdentifier,
@@ -122,6 +139,7 @@ export class ValidatorHASigner {
122
139
  } catch (error: any) {
123
140
  // Delete duty to allow retry (only succeeds if we own the lock)
124
141
  await this.slashingProtection.deleteDuty({ ...dutyIdentifier, lockToken });
142
+ this.metrics.recordSigningError(dutyType);
125
143
  throw error;
126
144
  }
127
145
 
@@ -133,6 +151,9 @@ export class ValidatorHASigner {
133
151
  lockToken,
134
152
  });
135
153
 
154
+ const duration = this.dateProvider.now() - startTime;
155
+ this.metrics.recordSigningSuccess(dutyType, duration);
156
+
136
157
  return signature;
137
158
  }
138
159
 
@@ -147,8 +168,8 @@ export class ValidatorHASigner {
147
168
  * Start the HA signer background tasks (cleanup of stuck duties).
148
169
  * Should be called after construction and before signing operations.
149
170
  */
150
- start() {
151
- this.slashingProtection.start();
171
+ async start() {
172
+ await this.slashingProtection.start();
152
173
  }
153
174
 
154
175
  /**