@aztec/validator-ha-signer 0.0.1-commit.001888fc

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 (62) hide show
  1. package/README.md +195 -0
  2. package/dest/db/index.d.ts +5 -0
  3. package/dest/db/index.d.ts.map +1 -0
  4. package/dest/db/index.js +4 -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/migrations/1_initial-schema.d.ts +9 -0
  9. package/dest/db/migrations/1_initial-schema.d.ts.map +1 -0
  10. package/dest/db/migrations/1_initial-schema.js +20 -0
  11. package/dest/db/postgres.d.ts +86 -0
  12. package/dest/db/postgres.d.ts.map +1 -0
  13. package/dest/db/postgres.js +208 -0
  14. package/dest/db/schema.d.ts +96 -0
  15. package/dest/db/schema.d.ts.map +1 -0
  16. package/dest/db/schema.js +230 -0
  17. package/dest/db/test_helper.d.ts +10 -0
  18. package/dest/db/test_helper.d.ts.map +1 -0
  19. package/dest/db/test_helper.js +14 -0
  20. package/dest/db/types.d.ts +185 -0
  21. package/dest/db/types.d.ts.map +1 -0
  22. package/dest/db/types.js +64 -0
  23. package/dest/errors.d.ts +34 -0
  24. package/dest/errors.d.ts.map +1 -0
  25. package/dest/errors.js +34 -0
  26. package/dest/factory.d.ts +60 -0
  27. package/dest/factory.d.ts.map +1 -0
  28. package/dest/factory.js +115 -0
  29. package/dest/metrics.d.ts +51 -0
  30. package/dest/metrics.d.ts.map +1 -0
  31. package/dest/metrics.js +103 -0
  32. package/dest/migrations.d.ts +15 -0
  33. package/dest/migrations.d.ts.map +1 -0
  34. package/dest/migrations.js +53 -0
  35. package/dest/slashing_protection_service.d.ts +93 -0
  36. package/dest/slashing_protection_service.d.ts.map +1 -0
  37. package/dest/slashing_protection_service.js +236 -0
  38. package/dest/test/pglite_pool.d.ts +92 -0
  39. package/dest/test/pglite_pool.d.ts.map +1 -0
  40. package/dest/test/pglite_pool.js +210 -0
  41. package/dest/types.d.ts +99 -0
  42. package/dest/types.d.ts.map +1 -0
  43. package/dest/types.js +4 -0
  44. package/dest/validator_ha_signer.d.ts +79 -0
  45. package/dest/validator_ha_signer.d.ts.map +1 -0
  46. package/dest/validator_ha_signer.js +140 -0
  47. package/package.json +110 -0
  48. package/src/db/index.ts +4 -0
  49. package/src/db/lmdb.ts +264 -0
  50. package/src/db/migrations/1_initial-schema.ts +26 -0
  51. package/src/db/postgres.ts +284 -0
  52. package/src/db/schema.ts +267 -0
  53. package/src/db/test_helper.ts +17 -0
  54. package/src/db/types.ts +251 -0
  55. package/src/errors.ts +47 -0
  56. package/src/factory.ts +139 -0
  57. package/src/metrics.ts +138 -0
  58. package/src/migrations.ts +75 -0
  59. package/src/slashing_protection_service.ts +308 -0
  60. package/src/test/pglite_pool.ts +256 -0
  61. package/src/types.ts +154 -0
  62. package/src/validator_ha_signer.ts +183 -0
@@ -0,0 +1,267 @@
1
+ /**
2
+ * SQL schema for the validator_duties table
3
+ *
4
+ * This table is used for distributed locking and slashing protection across multiple validator nodes.
5
+ * The PRIMARY KEY constraint ensures that only one node can acquire the lock for a given validator,
6
+ * slot, and duty type combination.
7
+ */
8
+
9
+ /**
10
+ * Current schema version
11
+ */
12
+ export const SCHEMA_VERSION = 1;
13
+
14
+ /**
15
+ * SQL to create the validator_duties table
16
+ */
17
+ export const CREATE_VALIDATOR_DUTIES_TABLE = `
18
+ CREATE TABLE IF NOT EXISTS validator_duties (
19
+ rollup_address VARCHAR(42) NOT NULL,
20
+ validator_address VARCHAR(42) NOT NULL,
21
+ slot BIGINT NOT NULL,
22
+ block_number BIGINT NOT NULL,
23
+ block_index_within_checkpoint INTEGER NOT NULL DEFAULT 0,
24
+ 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')),
26
+ message_hash VARCHAR(66) NOT NULL,
27
+ signature VARCHAR(132),
28
+ node_id VARCHAR(255) NOT NULL,
29
+ lock_token VARCHAR(64) NOT NULL,
30
+ started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
31
+ completed_at TIMESTAMP,
32
+ error_message TEXT,
33
+
34
+ PRIMARY KEY (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint),
35
+ CHECK (completed_at IS NULL OR completed_at >= started_at)
36
+ );
37
+ `;
38
+
39
+ /**
40
+ * SQL to create index on status and started_at for cleanup queries
41
+ */
42
+ export const CREATE_STATUS_INDEX = `
43
+ CREATE INDEX IF NOT EXISTS idx_validator_duties_status
44
+ ON validator_duties(status, started_at);
45
+ `;
46
+
47
+ /**
48
+ * SQL to create index for querying duties by node
49
+ */
50
+ export const CREATE_NODE_INDEX = `
51
+ CREATE INDEX IF NOT EXISTS idx_validator_duties_node
52
+ ON validator_duties(node_id, started_at);
53
+ `;
54
+
55
+ /**
56
+ * SQL to create the schema_version table for tracking migrations
57
+ */
58
+ export const CREATE_SCHEMA_VERSION_TABLE = `
59
+ CREATE TABLE IF NOT EXISTS schema_version (
60
+ version INTEGER PRIMARY KEY,
61
+ applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
62
+ );
63
+ `;
64
+
65
+ /**
66
+ * SQL to initialize schema version
67
+ */
68
+ export const INSERT_SCHEMA_VERSION = `
69
+ INSERT INTO schema_version (version)
70
+ VALUES ($1)
71
+ ON CONFLICT (version) DO NOTHING;
72
+ `;
73
+
74
+ /**
75
+ * Complete schema setup - all statements in order
76
+ */
77
+ export const SCHEMA_SETUP = [
78
+ CREATE_SCHEMA_VERSION_TABLE,
79
+ CREATE_VALIDATOR_DUTIES_TABLE,
80
+ CREATE_STATUS_INDEX,
81
+ CREATE_NODE_INDEX,
82
+ ] as const;
83
+
84
+ /**
85
+ * Query to get current schema version
86
+ */
87
+ export const GET_SCHEMA_VERSION = `
88
+ SELECT version FROM schema_version ORDER BY version DESC LIMIT 1;
89
+ `;
90
+
91
+ /**
92
+ * Atomic insert-or-get query.
93
+ * Tries to insert a new duty record. If a record already exists (conflict),
94
+ * returns the existing record instead.
95
+ *
96
+ * Returns the record with an `is_new` flag indicating whether we inserted or got existing.
97
+ *
98
+ * Note: In high concurrency scenarios, if the INSERT conflicts and another transaction
99
+ * just committed the row, there's a small window where the SELECT might not see it yet.
100
+ * The application layer should retry if no rows are returned.
101
+ */
102
+ export const INSERT_OR_GET_DUTY = `
103
+ WITH inserted AS (
104
+ INSERT INTO validator_duties (
105
+ rollup_address,
106
+ validator_address,
107
+ slot,
108
+ block_number,
109
+ block_index_within_checkpoint,
110
+ duty_type,
111
+ status,
112
+ message_hash,
113
+ node_id,
114
+ lock_token,
115
+ started_at
116
+ ) VALUES ($1, $2, $3, $4, $5, $6, 'signing', $7, $8, $9, CURRENT_TIMESTAMP)
117
+ ON CONFLICT (rollup_address, validator_address, slot, duty_type, block_index_within_checkpoint) DO NOTHING
118
+ RETURNING
119
+ rollup_address,
120
+ validator_address,
121
+ slot,
122
+ block_number,
123
+ block_index_within_checkpoint,
124
+ duty_type,
125
+ status,
126
+ message_hash,
127
+ signature,
128
+ node_id,
129
+ lock_token,
130
+ started_at,
131
+ completed_at,
132
+ error_message,
133
+ TRUE as is_new
134
+ )
135
+ SELECT * FROM inserted
136
+ UNION ALL
137
+ SELECT
138
+ rollup_address,
139
+ validator_address,
140
+ slot,
141
+ block_number,
142
+ block_index_within_checkpoint,
143
+ duty_type,
144
+ status,
145
+ message_hash,
146
+ signature,
147
+ node_id,
148
+ '' as lock_token,
149
+ started_at,
150
+ completed_at,
151
+ error_message,
152
+ FALSE as is_new
153
+ FROM validator_duties
154
+ WHERE rollup_address = $1
155
+ AND validator_address = $2
156
+ AND slot = $3
157
+ AND duty_type = $6
158
+ AND block_index_within_checkpoint = $5
159
+ AND NOT EXISTS (SELECT 1 FROM inserted);
160
+ `;
161
+
162
+ /**
163
+ * Query to update a duty to 'signed' status
164
+ */
165
+ export const UPDATE_DUTY_SIGNED = `
166
+ UPDATE validator_duties
167
+ SET status = 'signed',
168
+ signature = $1,
169
+ completed_at = CURRENT_TIMESTAMP
170
+ WHERE rollup_address = $2
171
+ AND validator_address = $3
172
+ AND slot = $4
173
+ AND duty_type = $5
174
+ AND block_index_within_checkpoint = $6
175
+ AND status = 'signing'
176
+ AND lock_token = $7;
177
+ `;
178
+
179
+ /**
180
+ * Query to delete a duty
181
+ * Only deletes if the lockToken matches
182
+ */
183
+ export const DELETE_DUTY = `
184
+ DELETE FROM validator_duties
185
+ WHERE rollup_address = $1
186
+ AND validator_address = $2
187
+ AND slot = $3
188
+ AND duty_type = $4
189
+ AND block_index_within_checkpoint = $5
190
+ AND status = 'signing'
191
+ AND lock_token = $6;
192
+ `;
193
+
194
+ /**
195
+ * Query to clean up old signed duties (for maintenance)
196
+ * Removes signed duties older than a specified timestamp
197
+ */
198
+ export const CLEANUP_OLD_SIGNED_DUTIES = `
199
+ DELETE FROM validator_duties
200
+ WHERE status = 'signed'
201
+ AND completed_at < $1;
202
+ `;
203
+
204
+ /**
205
+ * Query to clean up old duties (for maintenance)
206
+ * Removes SIGNED duties older than a specified age (in milliseconds)
207
+ */
208
+ export const CLEANUP_OLD_DUTIES = `
209
+ DELETE FROM validator_duties
210
+ WHERE status = 'signed'
211
+ AND started_at < CURRENT_TIMESTAMP - ($1 || ' milliseconds')::INTERVAL;
212
+ `;
213
+
214
+ /**
215
+ * Query to cleanup own stuck duties
216
+ * Removes duties in 'signing' status for a specific node that are older than maxAgeMs
217
+ * Uses DB's CURRENT_TIMESTAMP to avoid clock skew issues between nodes
218
+ */
219
+ export const CLEANUP_OWN_STUCK_DUTIES = `
220
+ DELETE FROM validator_duties
221
+ WHERE node_id = $1
222
+ AND status = 'signing'
223
+ AND started_at < CURRENT_TIMESTAMP - ($2 || ' milliseconds')::INTERVAL;
224
+ `;
225
+
226
+ /**
227
+ * Query to cleanup duties with outdated rollup address
228
+ * Removes all duties where the rollup address doesn't match the current one
229
+ * Used after a rollup upgrade to clean up duties for the old rollup
230
+ */
231
+ export const CLEANUP_OUTDATED_ROLLUP_DUTIES = `
232
+ DELETE FROM validator_duties
233
+ WHERE rollup_address != $1;
234
+ `;
235
+
236
+ /**
237
+ * SQL to drop the validator_duties table
238
+ */
239
+ export const DROP_VALIDATOR_DUTIES_TABLE = `DROP TABLE IF EXISTS validator_duties;`;
240
+
241
+ /**
242
+ * SQL to drop the schema_version table
243
+ */
244
+ export const DROP_SCHEMA_VERSION_TABLE = `DROP TABLE IF EXISTS schema_version;`;
245
+
246
+ /**
247
+ * Query to get stuck duties (for monitoring/alerting)
248
+ * Returns duties in 'signing' status that have been stuck for too long
249
+ */
250
+ export const GET_STUCK_DUTIES = `
251
+ SELECT
252
+ rollup_address,
253
+ validator_address,
254
+ slot,
255
+ block_number,
256
+ block_index_within_checkpoint,
257
+ duty_type,
258
+ status,
259
+ message_hash,
260
+ node_id,
261
+ started_at,
262
+ EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - started_at)) as age_seconds
263
+ FROM validator_duties
264
+ WHERE status = 'signing'
265
+ AND started_at < $1
266
+ ORDER BY started_at ASC;
267
+ `;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Test helpers for database setup
3
+ */
4
+ import type { PGlite } from '@electric-sql/pglite';
5
+
6
+ import { INSERT_SCHEMA_VERSION, SCHEMA_SETUP, SCHEMA_VERSION } from './schema.js';
7
+
8
+ /**
9
+ * Set up the database schema for testing.
10
+ * This runs the same SQL that migrations would apply.
11
+ */
12
+ export async function setupTestSchema(pglite: PGlite): Promise<void> {
13
+ for (const statement of SCHEMA_SETUP) {
14
+ await pglite.query(statement);
15
+ }
16
+ await pglite.query(INSERT_SCHEMA_VERSION, [SCHEMA_VERSION]);
17
+ }
@@ -0,0 +1,251 @@
1
+ import {
2
+ BlockNumber,
3
+ type CheckpointNumber,
4
+ type IndexWithinCheckpoint,
5
+ SlotNumber,
6
+ } from '@aztec/foundation/branded-types';
7
+ import { EthAddress } from '@aztec/foundation/eth-address';
8
+ import type { Signature } from '@aztec/foundation/eth-signature';
9
+ import { DutyType } from '@aztec/stdlib/ha-signing';
10
+
11
+ /**
12
+ * Row type from PostgreSQL query
13
+ */
14
+ export interface DutyRow {
15
+ rollup_address: string;
16
+ validator_address: string;
17
+ slot: string;
18
+ block_number: string;
19
+ block_index_within_checkpoint: number;
20
+ duty_type: DutyType;
21
+ status: DutyStatus;
22
+ message_hash: string;
23
+ signature: string | null;
24
+ node_id: string;
25
+ lock_token: string;
26
+ started_at: Date;
27
+ completed_at: Date | null;
28
+ error_message: string | null;
29
+ }
30
+
31
+ /**
32
+ * Plain-primitive representation of a duty record suitable for serialization
33
+ * (e.g. msgpackr for LMDB). All domain types are stored as their string/number
34
+ * equivalents. Timestamps are Unix milliseconds.
35
+ */
36
+ export interface StoredDutyRecord {
37
+ rollupAddress: string;
38
+ validatorAddress: string;
39
+ slot: string;
40
+ blockNumber: string;
41
+ blockIndexWithinCheckpoint: number;
42
+ dutyType: DutyType;
43
+ status: DutyStatus;
44
+ messageHash: string;
45
+ signature?: string;
46
+ nodeId: string;
47
+ lockToken: string;
48
+ /** Unix timestamp in milliseconds when signing started */
49
+ startedAtMs: number;
50
+ /** Unix timestamp in milliseconds when signing completed */
51
+ completedAtMs?: number;
52
+ errorMessage?: string;
53
+ }
54
+
55
+ /**
56
+ * Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
57
+ */
58
+ export interface InsertOrGetRow extends DutyRow {
59
+ is_new: boolean;
60
+ }
61
+
62
+ /**
63
+ * Status of a duty in the database
64
+ */
65
+ export enum DutyStatus {
66
+ SIGNING = 'signing',
67
+ SIGNED = 'signed',
68
+ }
69
+
70
+ // Re-export DutyType from stdlib
71
+ export { DutyType };
72
+
73
+ /**
74
+ * Rich representation of a validator duty, with branded types and Date objects.
75
+ * This is the common output type returned by all SlashingProtectionDatabase implementations.
76
+ */
77
+ export interface ValidatorDutyRecord {
78
+ /** Ethereum address of the rollup contract */
79
+ rollupAddress: EthAddress;
80
+ /** Ethereum address of the validator */
81
+ validatorAddress: EthAddress;
82
+ /** Slot number for this duty */
83
+ slot: SlotNumber;
84
+ /** Block number for this duty */
85
+ blockNumber: BlockNumber;
86
+ /** Block index within checkpoint (0, 1, 2... for block proposals, -1 for other duty types) */
87
+ blockIndexWithinCheckpoint: number;
88
+ /** Type of duty being performed */
89
+ dutyType: DutyType;
90
+ /** Current status of the duty */
91
+ status: DutyStatus;
92
+ /** The signing root (hash) for this duty */
93
+ messageHash: string;
94
+ /** The signature (populated after successful signing) */
95
+ signature?: string;
96
+ /** Unique identifier for the node that acquired the lock */
97
+ nodeId: string;
98
+ /** Secret token for verifying ownership of the duty lock */
99
+ lockToken: string;
100
+ /** When the duty signing was started */
101
+ startedAt: Date;
102
+ /** When the duty signing was completed (success or failure) */
103
+ completedAt?: Date;
104
+ /** Error message (currently unused) */
105
+ errorMessage?: string;
106
+ }
107
+
108
+ /**
109
+ * Convert a {@link StoredDutyRecord} (plain-primitive wire format) to a
110
+ * {@link ValidatorDutyRecord} (rich domain type).
111
+ *
112
+ * Shared by LMDB and any future non-Postgres backend implementations.
113
+ */
114
+ export function recordFromFields(stored: StoredDutyRecord): ValidatorDutyRecord {
115
+ return {
116
+ rollupAddress: EthAddress.fromString(stored.rollupAddress),
117
+ validatorAddress: EthAddress.fromString(stored.validatorAddress),
118
+ slot: SlotNumber.fromString(stored.slot),
119
+ blockNumber: BlockNumber.fromString(stored.blockNumber),
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
+
133
+ /**
134
+ * Duty identifier for block proposals.
135
+ * blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
136
+ */
137
+ export interface BlockProposalDutyIdentifier {
138
+ rollupAddress: EthAddress;
139
+ validatorAddress: EthAddress;
140
+ slot: SlotNumber;
141
+ /** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
142
+ blockIndexWithinCheckpoint: IndexWithinCheckpoint;
143
+ dutyType: DutyType.BLOCK_PROPOSAL;
144
+ }
145
+
146
+ /**
147
+ * Duty identifier for non-block-proposal duties.
148
+ * blockIndexWithinCheckpoint is not applicable (internally stored as -1).
149
+ */
150
+ export interface OtherDutyIdentifier {
151
+ rollupAddress: EthAddress;
152
+ validatorAddress: EthAddress;
153
+ slot: SlotNumber;
154
+ dutyType:
155
+ | DutyType.CHECKPOINT_PROPOSAL
156
+ | DutyType.ATTESTATION
157
+ | DutyType.ATTESTATIONS_AND_SIGNERS
158
+ | DutyType.GOVERNANCE_VOTE
159
+ | DutyType.SLASHING_VOTE
160
+ | DutyType.AUTH_REQUEST
161
+ | DutyType.TXS;
162
+ }
163
+
164
+ /**
165
+ * Minimal info needed to identify a unique duty.
166
+ * Uses discriminated union to enforce type safety:
167
+ * - BLOCK_PROPOSAL duties MUST have blockIndexWithinCheckpoint >= 0
168
+ * - Other duty types do NOT have blockIndexWithinCheckpoint (internally -1)
169
+ */
170
+ export type DutyIdentifier = BlockProposalDutyIdentifier | OtherDutyIdentifier;
171
+
172
+ /**
173
+ * Validates and normalizes the block index for a duty.
174
+ * - BLOCK_PROPOSAL: validates blockIndexWithinCheckpoint is provided and >= 0
175
+ * - Other duty types: always returns -1
176
+ *
177
+ * @throws Error if BLOCK_PROPOSAL is missing blockIndexWithinCheckpoint or has invalid value
178
+ */
179
+ export function normalizeBlockIndex(dutyType: DutyType, blockIndexWithinCheckpoint: number | undefined): number {
180
+ if (dutyType === DutyType.BLOCK_PROPOSAL) {
181
+ if (blockIndexWithinCheckpoint === undefined) {
182
+ throw new Error('BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint to be specified');
183
+ }
184
+ if (blockIndexWithinCheckpoint < 0) {
185
+ throw new Error(
186
+ `BLOCK_PROPOSAL duties require blockIndexWithinCheckpoint >= 0, got ${blockIndexWithinCheckpoint}`,
187
+ );
188
+ }
189
+ return blockIndexWithinCheckpoint;
190
+ }
191
+ // For all other duty types, always use -1
192
+ return -1;
193
+ }
194
+
195
+ /**
196
+ * Gets the block index from a DutyIdentifier.
197
+ * - BLOCK_PROPOSAL: returns the blockIndexWithinCheckpoint
198
+ * - Other duty types: returns -1
199
+ */
200
+ export function getBlockIndexFromDutyIdentifier(duty: DutyIdentifier): number {
201
+ if (duty.dutyType === DutyType.BLOCK_PROPOSAL) {
202
+ return duty.blockIndexWithinCheckpoint;
203
+ }
204
+ return -1;
205
+ }
206
+
207
+ /**
208
+ * Additional parameters for checking and recording a new duty
209
+ */
210
+ interface CheckAndRecordExtra {
211
+ /** Block number for this duty */
212
+ blockNumber: BlockNumber | CheckpointNumber;
213
+ /** The signing root (hash) for this duty */
214
+ messageHash: string;
215
+ /** Identifier for the node that acquired the lock */
216
+ nodeId: string;
217
+ }
218
+
219
+ /**
220
+ * Parameters for checking and recording a new duty.
221
+ * Uses intersection with DutyIdentifier to preserve the discriminated union.
222
+ */
223
+ export type CheckAndRecordParams = DutyIdentifier & CheckAndRecordExtra;
224
+
225
+ /**
226
+ * Additional parameters for recording a successful signing
227
+ */
228
+ interface RecordSuccessExtra {
229
+ signature: Signature;
230
+ nodeId: string;
231
+ lockToken: string;
232
+ }
233
+
234
+ /**
235
+ * Parameters for recording a successful signing.
236
+ * Uses intersection with DutyIdentifier to preserve the discriminated union.
237
+ */
238
+ export type RecordSuccessParams = DutyIdentifier & RecordSuccessExtra;
239
+
240
+ /**
241
+ * Additional parameters for deleting a duty
242
+ */
243
+ interface DeleteDutyExtra {
244
+ lockToken: string;
245
+ }
246
+
247
+ /**
248
+ * Parameters for deleting a duty.
249
+ * Uses intersection with DutyIdentifier to preserve the discriminated union.
250
+ */
251
+ export type DeleteDutyParams = DutyIdentifier & DeleteDutyExtra;
package/src/errors.ts ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Custom errors for the validator HA signer
3
+ */
4
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
5
+
6
+ import type { DutyType } from './db/types.js';
7
+
8
+ /**
9
+ * Thrown when a duty has already been signed (by any node).
10
+ * This is expected behavior in an HA setup - all nodes try to sign,
11
+ * the first one wins, and subsequent attempts get this error.
12
+ */
13
+ export class DutyAlreadySignedError extends Error {
14
+ constructor(
15
+ public readonly slot: SlotNumber,
16
+ public readonly dutyType: DutyType,
17
+ public readonly blockIndexWithinCheckpoint: number,
18
+ public readonly signedByNode: string,
19
+ ) {
20
+ super(`Duty ${dutyType} for slot ${slot} already signed by node ${signedByNode}`);
21
+ this.name = 'DutyAlreadySignedError';
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Thrown when attempting to sign data that conflicts with an already-signed duty.
27
+ * This means the same validator tried to sign DIFFERENT data for the same slot.
28
+ *
29
+ * This is expected in HA setups where nodes may build different blocks
30
+ * (e.g., different transaction ordering) - the protection prevents double-signing.
31
+ */
32
+ export class SlashingProtectionError extends Error {
33
+ constructor(
34
+ public readonly slot: SlotNumber,
35
+ public readonly dutyType: DutyType,
36
+ public readonly blockIndexWithinCheckpoint: number,
37
+ public readonly existingMessageHash: string,
38
+ public readonly attemptedMessageHash: string,
39
+ public readonly signedByNode: string,
40
+ ) {
41
+ super(
42
+ `Slashing protection: ${dutyType} for slot ${slot} was already signed with different data. ` +
43
+ `Existing: ${existingMessageHash.slice(0, 10)}..., Attempted: ${attemptedMessageHash.slice(0, 10)}...`,
44
+ );
45
+ this.name = 'SlashingProtectionError';
46
+ }
47
+ }