@aztec/validator-ha-signer 0.0.1-commit.e558bd1c → 0.0.1-commit.e5a3663dd

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 +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/factory.d.ts +39 -4
  24. package/dest/factory.d.ts.map +1 -1
  25. package/dest/factory.js +78 -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 +12 -3
  30. package/dest/slashing_protection_service.d.ts.map +1 -1
  31. package/dest/slashing_protection_service.js +17 -6
  32. package/dest/types.d.ts +18 -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 +12 -4
  36. package/dest/validator_ha_signer.d.ts.map +1 -1
  37. package/dest/validator_ha_signer.js +18 -8
  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 +17 -15
  44. package/src/db/schema.ts +13 -7
  45. package/src/db/types.ts +66 -19
  46. package/src/factory.ts +96 -6
  47. package/src/metrics.ts +138 -0
  48. package/src/slashing_protection_service.ts +28 -7
  49. package/src/types.ts +38 -104
  50. package/src/validator_ha_signer.ts +36 -12
  51. package/dest/config.d.ts +0 -101
  52. package/dest/config.d.ts.map +0 -1
  53. package/dest/config.js +0 -92
  54. package/src/config.ts +0 -149
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,
@@ -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,88 @@ 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('signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
123
+ dataDirectory: config.dataDirectory,
124
+ dataStoreMapSizeKb: config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
125
+ l1Contracts: config.l1Contracts,
126
+ });
127
+
128
+ const db = new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
129
+
130
+ const signerConfig = {
131
+ ...config,
132
+ nodeId: config.nodeId || 'local',
133
+ };
134
+
135
+ const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId, 'LocalSigningProtectionMetrics');
136
+
137
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
138
+
139
+ return { signer, db };
140
+ }
141
+
142
+ /**
143
+ * Create an in-memory LMDB-backed SlashingProtectionDatabase that can be shared across
144
+ * multiple validator nodes in the same process. Used for testing HA setups.
145
+ */
146
+ export async function createSharedSlashingProtectionDb(
147
+ dateProvider: DateProvider = new DateProvider(),
148
+ ): Promise<SlashingProtectionDatabase> {
149
+ const kvStore = await createStore('shared-signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
150
+ dataStoreMapSizeKb: 1024 * 1024,
151
+ });
152
+ return new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
153
+ }
154
+
155
+ /**
156
+ * Create a ValidatorHASigner backed by a pre-existing SlashingProtectionDatabase.
157
+ * Used for testing HA setups where multiple nodes share the same protection database.
158
+ */
159
+ export function createSignerFromSharedDb(
160
+ db: SlashingProtectionDatabase,
161
+ config: Pick<
162
+ ValidatorHASignerConfig,
163
+ 'nodeId' | 'pollingIntervalMs' | 'signingTimeoutMs' | 'maxStuckDutiesAgeMs' | 'l1Contracts'
164
+ >,
165
+ deps?: CreateLocalSignerWithProtectionDeps,
166
+ ): { signer: ValidatorHASigner; db: SlashingProtectionDatabase } {
167
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
168
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
169
+ const metrics = new HASignerMetrics(telemetryClient, config.nodeId, 'SharedSigningProtectionMetrics');
170
+ const signer = new ValidatorHASigner(db, config, { metrics, dateProvider });
171
+ return { signer, db };
172
+ }
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,12 +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;
43
54
  private lastOldDutiesCleanupAtMs?: number;
44
55
 
45
56
  constructor(
46
57
  private readonly db: SlashingProtectionDatabase,
47
- private readonly config: ValidatorHASignerConfig,
58
+ private readonly config: BaseSignerConfig,
59
+ deps: SlashingProtectionServiceDeps,
48
60
  ) {
49
61
  this.log = createLogger('slashing-protection');
50
62
  this.pollingIntervalMs = config.pollingIntervalMs;
@@ -53,6 +65,8 @@ export class SlashingProtectionService {
53
65
  this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? 144_000;
54
66
 
55
67
  this.cleanupRunningPromise = new RunningPromise(this.cleanup.bind(this), this.log, this.maxStuckDutiesAgeMs);
68
+ this.metrics = deps.metrics;
69
+ this.dateProvider = deps.dateProvider;
56
70
  }
57
71
 
58
72
  /**
@@ -72,7 +86,7 @@ export class SlashingProtectionService {
72
86
  */
73
87
  async checkAndRecord(params: CheckAndRecordParams): Promise<string> {
74
88
  const { validatorAddress, slot, dutyType, messageHash, nodeId } = params;
75
- const startTime = Date.now();
89
+ const startTime = this.dateProvider.now();
76
90
 
77
91
  this.log.debug(`Checking duty: ${dutyType} for slot ${slot}`, {
78
92
  validatorAddress: validatorAddress.toString(),
@@ -85,10 +99,11 @@ export class SlashingProtectionService {
85
99
 
86
100
  if (isNew) {
87
101
  // We successfully acquired the lock
88
- this.log.info(`Acquired lock for duty ${dutyType} at slot ${slot}`, {
102
+ this.log.verbose(`Acquired lock for duty ${dutyType} at slot ${slot}`, {
89
103
  validatorAddress: validatorAddress.toString(),
90
104
  nodeId,
91
105
  });
106
+ this.metrics.recordLockAcquire(true);
92
107
  return record.lockToken;
93
108
  }
94
109
 
@@ -103,6 +118,7 @@ export class SlashingProtectionService {
103
118
  existingNodeId: record.nodeId,
104
119
  attemptingNodeId: nodeId,
105
120
  });
121
+ this.metrics.recordSlashingProtection(dutyType);
106
122
  throw new SlashingProtectionError(
107
123
  slot,
108
124
  dutyType,
@@ -112,15 +128,17 @@ export class SlashingProtectionService {
112
128
  record.nodeId,
113
129
  );
114
130
  }
131
+ this.metrics.recordDutyAlreadySigned(dutyType);
115
132
  throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, record.nodeId);
116
133
  } else if (record.status === DutyStatus.SIGNING) {
117
134
  // Another node is currently signing - check for timeout
118
- if (Date.now() - startTime > this.signingTimeoutMs) {
135
+ if (this.dateProvider.now() - startTime > this.signingTimeoutMs) {
119
136
  this.log.warn(`Timeout waiting for signing to complete for duty ${dutyType} at slot ${slot}`, {
120
137
  validatorAddress: validatorAddress.toString(),
121
138
  timeoutMs: this.signingTimeoutMs,
122
139
  signingNodeId: record.nodeId,
123
140
  });
141
+ this.metrics.recordDutyAlreadySigned(dutyType);
124
142
  throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, 'unknown (timeout)');
125
143
  }
126
144
 
@@ -159,7 +177,7 @@ export class SlashingProtectionService {
159
177
  );
160
178
 
161
179
  if (success) {
162
- this.log.info(`Recorded successful signing for duty ${dutyType} at slot ${slot}`, {
180
+ this.log.verbose(`Recorded successful signing for duty ${dutyType} at slot ${slot}`, {
163
181
  validatorAddress: validatorAddress.toString(),
164
182
  nodeId,
165
183
  });
@@ -228,6 +246,7 @@ export class SlashingProtectionService {
228
246
  this.log.info(`Cleaned up ${numOutdatedRollupDuties} duties with outdated rollup address at startup`, {
229
247
  currentRollupAddress: this.config.l1Contracts.rollupAddress.toString(),
230
248
  });
249
+ this.metrics.recordCleanup('outdated_rollup', numOutdatedRollupDuties);
231
250
  }
232
251
 
233
252
  this.cleanupRunningPromise.start();
@@ -263,13 +282,14 @@ export class SlashingProtectionService {
263
282
  nodeId: this.config.nodeId,
264
283
  maxStuckDutiesAgeMs: this.maxStuckDutiesAgeMs,
265
284
  });
285
+ this.metrics.recordCleanup('stuck', numStuckDuties);
266
286
  }
267
287
 
268
288
  // 2. Clean up old signed duties if configured
269
289
  // we shouldn't run this as often as stuck duty cleanup.
270
290
  if (this.config.cleanupOldDutiesAfterHours !== undefined) {
271
291
  const maxAgeMs = this.config.cleanupOldDutiesAfterHours * 60 * 60 * 1000;
272
- const nowMs = Date.now();
292
+ const nowMs = this.dateProvider.now();
273
293
  const shouldRun =
274
294
  this.lastOldDutiesCleanupAtMs === undefined || nowMs - this.lastOldDutiesCleanupAtMs >= maxAgeMs;
275
295
  if (shouldRun) {
@@ -280,6 +300,7 @@ export class SlashingProtectionService {
280
300
  cleanupOldDutiesAfterHours: this.config.cleanupOldDutiesAfterHours,
281
301
  maxAgeMs,
282
302
  });
303
+ this.metrics.recordCleanup('old', numOldDuties);
283
304
  }
284
305
  }
285
306
  }
package/src/types.ts CHANGED
@@ -1,38 +1,51 @@
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
+ type AttestationSigningContext,
6
+ type CheckpointProposalSigningContext,
7
+ DutyType,
8
+ type HAProtectedSigningContext,
9
+ type SigningContext,
10
+ type ValidatorHASignerConfig,
11
+ getBlockNumberFromSigningContext as getBlockNumberFromSigningContextFromStdlib,
12
+ getCheckpointNumberFromSigningContext as getCheckpointNumberFromSigningContextFromStdlib,
13
+ isHAProtectedContext,
14
+ } from '@aztec/stdlib/ha-signing';
15
+ import type { TelemetryClient } from '@aztec/telemetry-client';
8
16
 
9
17
  import type { Pool } from 'pg';
10
18
 
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,
19
+ import type {
20
+ BlockProposalDutyIdentifier,
21
+ CheckAndRecordParams,
22
+ DeleteDutyParams,
23
+ DutyIdentifier,
24
+ DutyRow,
25
+ OtherDutyIdentifier,
26
+ RecordSuccessParams,
27
+ ValidatorDutyRecord,
22
28
  } from './db/types.js';
23
29
 
24
30
  export type {
31
+ AttestationSigningContext,
25
32
  BlockProposalDutyIdentifier,
26
33
  CheckAndRecordParams,
34
+ CheckpointProposalSigningContext,
27
35
  DeleteDutyParams,
28
36
  DutyIdentifier,
29
37
  DutyRow,
38
+ HAProtectedSigningContext,
30
39
  OtherDutyIdentifier,
31
40
  RecordSuccessParams,
41
+ SigningContext,
32
42
  ValidatorDutyRecord,
33
43
  ValidatorHASignerConfig,
34
44
  };
35
45
  export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
46
+ export { isHAProtectedContext };
47
+ export { getBlockNumberFromSigningContextFromStdlib as getBlockNumberFromSigningContext };
48
+ export { getCheckpointNumberFromSigningContextFromStdlib as getCheckpointNumberFromSigningContext };
36
49
 
37
50
  /**
38
51
  * Result of tryInsertOrGetExisting operation
@@ -53,99 +66,20 @@ export interface CreateHASignerDeps {
53
66
  * If provided, databaseUrl and poolConfig are ignored
54
67
  */
55
68
  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
69
  /**
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.
70
+ * Optional telemetry client for metrics
68
71
  */
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);
72
+ telemetryClient?: TelemetryClient;
73
+ /**
74
+ * Optional date provider for timestamps
75
+ */
76
+ dateProvider?: DateProvider;
138
77
  }
139
78
 
140
79
  /**
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)
80
+ * deps for creating a local signing protection signer
147
81
  */
148
- export type SigningContext = HAProtectedSigningContext | NoHAProtectionSigningContext;
82
+ export type CreateLocalSignerWithProtectionDeps = Omit<CreateHASignerDeps, 'pool'>;
149
83
 
150
84
  /**
151
85
  * Database interface for slashing protection operations