@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/db/lmdb.ts ADDED
@@ -0,0 +1,265 @@
1
+ /**
2
+ * LMDB implementation of SlashingProtectionDatabase
3
+ *
4
+ * Provides local (single-node) double-signing protection using LMDB as the backend.
5
+ * Suitable for nodes that do NOT run in a high-availability multi-node setup.
6
+ *
7
+ * The LMDB store is single-writer, making setIfNotExists inherently atomic.
8
+ * This means we get crash-restart protection without needing an external database.
9
+ */
10
+ import { SlotNumber } from '@aztec/foundation/branded-types';
11
+ import { randomBytes } from '@aztec/foundation/crypto/random';
12
+ import { EthAddress } from '@aztec/foundation/eth-address';
13
+ import { type Logger, createLogger } from '@aztec/foundation/log';
14
+ import type { DateProvider } from '@aztec/foundation/timer';
15
+ import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
16
+
17
+ import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
18
+ import {
19
+ type CheckAndRecordParams,
20
+ DutyStatus,
21
+ DutyType,
22
+ type StoredDutyRecord,
23
+ getBlockIndexFromDutyIdentifier,
24
+ recordFromFields,
25
+ } from './types.js';
26
+
27
+ function dutyKey(
28
+ rollupAddress: string,
29
+ validatorAddress: string,
30
+ slot: string,
31
+ dutyType: string,
32
+ blockIndexWithinCheckpoint: number,
33
+ ): string {
34
+ return `${rollupAddress}:${validatorAddress}:${slot}:${dutyType}:${blockIndexWithinCheckpoint}`;
35
+ }
36
+
37
+ /**
38
+ * LMDB-backed implementation of SlashingProtectionDatabase.
39
+ *
40
+ * Provides single-node double-signing protection that survives crashes and restarts.
41
+ * Does not provide cross-node coordination (that requires the PostgreSQL implementation).
42
+ */
43
+ export class LmdbSlashingProtectionDatabase implements SlashingProtectionDatabase {
44
+ public static readonly SCHEMA_VERSION = 2;
45
+
46
+ private readonly duties: AztecAsyncMap<string, StoredDutyRecord>;
47
+ private readonly log: Logger;
48
+
49
+ constructor(
50
+ private readonly store: AztecAsyncKVStore,
51
+ private readonly dateProvider: DateProvider,
52
+ ) {
53
+ this.log = createLogger('slashing-protection:lmdb');
54
+ this.duties = store.openMap<string, StoredDutyRecord>('signing-protection-duties');
55
+ }
56
+
57
+ /**
58
+ * Atomically try to insert a new duty record, or get the existing one if present.
59
+ *
60
+ * LMDB is single-writer so the read-then-write inside transactionAsync is naturally atomic.
61
+ */
62
+ public async tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult> {
63
+ const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
64
+ const key = dutyKey(
65
+ params.rollupAddress.toString(),
66
+ params.validatorAddress.toString(),
67
+ params.slot.toString(),
68
+ params.dutyType,
69
+ blockIndexWithinCheckpoint,
70
+ );
71
+
72
+ const lockToken = randomBytes(16).toString('hex');
73
+ const now = this.dateProvider.now();
74
+
75
+ const result = await this.store.transactionAsync(async () => {
76
+ const existing = await this.duties.getAsync(key);
77
+ if (existing) {
78
+ return { isNew: false as const, record: { ...existing, lockToken: '' } };
79
+ }
80
+
81
+ const newRecord: StoredDutyRecord = {
82
+ rollupAddress: params.rollupAddress.toString(),
83
+ validatorAddress: params.validatorAddress.toString(),
84
+ slot: params.slot.toString(),
85
+ blockNumber: params.blockNumber.toString(),
86
+ checkpointNumber: params.checkpointNumber.toString(),
87
+ blockIndexWithinCheckpoint,
88
+ dutyType: params.dutyType,
89
+ status: DutyStatus.SIGNING,
90
+ messageHash: params.messageHash,
91
+ nodeId: params.nodeId,
92
+ lockToken,
93
+ startedAtMs: now,
94
+ };
95
+ await this.duties.set(key, newRecord);
96
+ return { isNew: true as const, record: newRecord };
97
+ });
98
+
99
+ if (result.isNew) {
100
+ this.log.debug(`Acquired lock for duty ${params.dutyType} at slot ${params.slot}`, {
101
+ validatorAddress: params.validatorAddress.toString(),
102
+ nodeId: params.nodeId,
103
+ });
104
+ }
105
+
106
+ return { isNew: result.isNew, record: recordFromFields(result.record) };
107
+ }
108
+
109
+ /**
110
+ * Update a duty to 'signed' status with the signature.
111
+ * Only succeeds if the lockToken matches.
112
+ */
113
+ public updateDutySigned(
114
+ rollupAddress: EthAddress,
115
+ validatorAddress: EthAddress,
116
+ slot: SlotNumber,
117
+ dutyType: DutyType,
118
+ signature: string,
119
+ lockToken: string,
120
+ blockIndexWithinCheckpoint: number,
121
+ ): Promise<boolean> {
122
+ const key = dutyKey(
123
+ rollupAddress.toString(),
124
+ validatorAddress.toString(),
125
+ slot.toString(),
126
+ dutyType,
127
+ blockIndexWithinCheckpoint,
128
+ );
129
+
130
+ return this.store.transactionAsync(async () => {
131
+ const existing = await this.duties.getAsync(key);
132
+ if (!existing) {
133
+ this.log.warn('Failed to update duty to signed: duty not found', {
134
+ rollupAddress: rollupAddress.toString(),
135
+ validatorAddress: validatorAddress.toString(),
136
+ slot: slot.toString(),
137
+ dutyType,
138
+ blockIndexWithinCheckpoint,
139
+ });
140
+ return false;
141
+ }
142
+
143
+ if (existing.lockToken !== lockToken) {
144
+ this.log.warn('Failed to update duty to signed: invalid token', {
145
+ rollupAddress: rollupAddress.toString(),
146
+ validatorAddress: validatorAddress.toString(),
147
+ slot: slot.toString(),
148
+ dutyType,
149
+ blockIndexWithinCheckpoint,
150
+ });
151
+ return false;
152
+ }
153
+
154
+ await this.duties.set(key, {
155
+ ...existing,
156
+ status: DutyStatus.SIGNED,
157
+ signature,
158
+ completedAtMs: this.dateProvider.now(),
159
+ });
160
+
161
+ return true;
162
+ });
163
+ }
164
+
165
+ /**
166
+ * Delete a duty record.
167
+ * Only succeeds if the lockToken matches.
168
+ */
169
+ public deleteDuty(
170
+ rollupAddress: EthAddress,
171
+ validatorAddress: EthAddress,
172
+ slot: SlotNumber,
173
+ dutyType: DutyType,
174
+ lockToken: string,
175
+ blockIndexWithinCheckpoint: number,
176
+ ): Promise<boolean> {
177
+ const key = dutyKey(
178
+ rollupAddress.toString(),
179
+ validatorAddress.toString(),
180
+ slot.toString(),
181
+ dutyType,
182
+ blockIndexWithinCheckpoint,
183
+ );
184
+
185
+ return this.store.transactionAsync(async () => {
186
+ const existing = await this.duties.getAsync(key);
187
+ if (!existing || existing.lockToken !== lockToken) {
188
+ this.log.warn('Failed to delete duty: invalid token or duty not found', {
189
+ rollupAddress: rollupAddress.toString(),
190
+ validatorAddress: validatorAddress.toString(),
191
+ slot: slot.toString(),
192
+ dutyType,
193
+ blockIndexWithinCheckpoint,
194
+ });
195
+ return false;
196
+ }
197
+
198
+ await this.duties.delete(key);
199
+ return true;
200
+ });
201
+ }
202
+
203
+ /**
204
+ * Cleanup own stuck duties (SIGNING status older than maxAgeMs).
205
+ */
206
+ public cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
207
+ const cutoffMs = this.dateProvider.now() - maxAgeMs;
208
+
209
+ return this.store.transactionAsync(async () => {
210
+ const keysToDelete: string[] = [];
211
+ for await (const [key, record] of this.duties.entriesAsync()) {
212
+ if (record.nodeId === nodeId && record.status === DutyStatus.SIGNING && record.startedAtMs < cutoffMs) {
213
+ keysToDelete.push(key);
214
+ }
215
+ }
216
+ for (const key of keysToDelete) {
217
+ await this.duties.delete(key);
218
+ }
219
+ return keysToDelete.length;
220
+ });
221
+ }
222
+
223
+ /**
224
+ * Cleanup duties with outdated rollup address.
225
+ *
226
+ * This is always a no-op for the LMDB implementation: the underlying store is created via
227
+ * DatabaseVersionManager (in factory.ts), which already resets the entire data directory at
228
+ * startup whenever the rollup address changes.
229
+ */
230
+ public cleanupOutdatedRollupDuties(_currentRollupAddress: EthAddress): Promise<number> {
231
+ return Promise.resolve(0);
232
+ }
233
+
234
+ /**
235
+ * Cleanup old signed duties older than maxAgeMs.
236
+ */
237
+ public cleanupOldDuties(maxAgeMs: number): Promise<number> {
238
+ const cutoffMs = this.dateProvider.now() - maxAgeMs;
239
+
240
+ return this.store.transactionAsync(async () => {
241
+ const keysToDelete: string[] = [];
242
+ for await (const [key, record] of this.duties.entriesAsync()) {
243
+ if (
244
+ record.status === DutyStatus.SIGNED &&
245
+ record.completedAtMs !== undefined &&
246
+ record.completedAtMs < cutoffMs
247
+ ) {
248
+ keysToDelete.push(key);
249
+ }
250
+ }
251
+ for (const key of keysToDelete) {
252
+ await this.duties.delete(key);
253
+ }
254
+ return keysToDelete.length;
255
+ });
256
+ }
257
+
258
+ /**
259
+ * Close the underlying LMDB store.
260
+ */
261
+ public async close(): Promise<void> {
262
+ await this.store.close();
263
+ this.log.debug('LMDB slashing protection database closed');
264
+ }
265
+ }
@@ -1,21 +1,52 @@
1
1
  /**
2
2
  * Initial schema for validator HA slashing protection
3
3
  *
4
- * This migration imports SQL from the schema.ts file to ensure a single source of truth.
4
+ * Note: this migration contains a fixed snapshot of the schema at the time it was created.
5
+ * It must NOT import from schema.ts, which evolves over time and would cause this migration
6
+ * to produce different results on fresh runs vs. re-runs.
5
7
  */
6
8
  import type { MigrationBuilder } from 'node-pg-migrate';
7
9
 
8
- import { DROP_SCHEMA_VERSION_TABLE, DROP_VALIDATOR_DUTIES_TABLE, SCHEMA_SETUP, SCHEMA_VERSION } from '../schema.js';
10
+ import { DROP_SCHEMA_VERSION_TABLE, DROP_VALIDATOR_DUTIES_TABLE } from '../schema.js';
11
+
12
+ // Snapshot of the initial schema — does NOT include checkpoint_number (added in migration 2).
13
+ const INITIAL_SCHEMA_SETUP = [
14
+ `CREATE TABLE IF NOT EXISTS schema_version (
15
+ version INTEGER PRIMARY KEY,
16
+ applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
17
+ );`,
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
+ `CREATE INDEX IF NOT EXISTS idx_validator_duties_status ON validator_duties(status, started_at);`,
38
+ `CREATE INDEX IF NOT EXISTS idx_validator_duties_node ON validator_duties(node_id, started_at);`,
39
+ ] as const;
9
40
 
10
41
  export function up(pgm: MigrationBuilder): void {
11
- for (const statement of SCHEMA_SETUP) {
42
+ for (const statement of INITIAL_SCHEMA_SETUP) {
12
43
  pgm.sql(statement);
13
44
  }
14
45
 
15
46
  // Insert initial schema version
16
47
  pgm.sql(`
17
48
  INSERT INTO schema_version (version)
18
- VALUES (${SCHEMA_VERSION})
49
+ VALUES (1)
19
50
  ON CONFLICT (version) DO NOTHING;
20
51
  `);
21
52
  }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Add checkpoint_number column to validator_duties table
3
+ */
4
+ import type { MigrationBuilder } from 'node-pg-migrate';
5
+
6
+ export function up(pgm: MigrationBuilder): void {
7
+ pgm.addColumn('validator_duties', {
8
+ // eslint-disable-next-line camelcase
9
+ checkpoint_number: { type: 'bigint', notNull: true, default: 0 },
10
+ });
11
+
12
+ pgm.sql(`UPDATE schema_version SET version = 2 WHERE version = 1`);
13
+ }
14
+
15
+ export function down(pgm: MigrationBuilder): void {
16
+ pgm.dropColumn('validator_duties', 'checkpoint_number');
17
+
18
+ pgm.sql(`UPDATE schema_version SET version = 1 WHERE version = 2`);
19
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * PostgreSQL implementation of SlashingProtectionDatabase
3
3
  */
4
- import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
+ import { SlotNumber } from '@aztec/foundation/branded-types';
5
5
  import { randomBytes } from '@aztec/foundation/crypto/random';
6
6
  import { EthAddress } from '@aztec/foundation/eth-address';
7
7
  import { type Logger, createLogger } from '@aztec/foundation/log';
@@ -20,7 +20,7 @@ import {
20
20
  UPDATE_DUTY_SIGNED,
21
21
  } from './schema.js';
22
22
  import type { CheckAndRecordParams, DutyRow, DutyType, InsertOrGetRow, ValidatorDutyRecord } from './types.js';
23
- import { getBlockIndexFromDutyIdentifier } from './types.js';
23
+ import { getBlockIndexFromDutyIdentifier, recordFromFields } from './types.js';
24
24
 
25
25
  /**
26
26
  * Minimal pool interface for database operations.
@@ -107,6 +107,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
107
107
  params.validatorAddress.toString(),
108
108
  params.slot.toString(),
109
109
  params.blockNumber.toString(),
110
+ params.checkpointNumber.toString(),
110
111
  blockIndexWithinCheckpoint,
111
112
  params.dutyType,
112
113
  params.messageHash,
@@ -220,14 +221,17 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
220
221
  }
221
222
 
222
223
  /**
223
- * Convert a database row to a ValidatorDutyRecord
224
+ * Convert a database row to a ValidatorDutyRecord.
225
+ * Maps snake_case column names to StoredDutyRecord (camelCase, ms timestamps),
226
+ * then delegates to the shared recordFromFields() converter.
224
227
  */
225
228
  private rowToRecord(row: DutyRow): ValidatorDutyRecord {
226
- return {
227
- rollupAddress: EthAddress.fromString(row.rollup_address),
228
- validatorAddress: EthAddress.fromString(row.validator_address),
229
- slot: SlotNumber.fromString(row.slot),
230
- blockNumber: BlockNumber.fromString(row.block_number),
229
+ return recordFromFields({
230
+ rollupAddress: row.rollup_address,
231
+ validatorAddress: row.validator_address,
232
+ slot: row.slot,
233
+ blockNumber: row.block_number,
234
+ checkpointNumber: row.checkpoint_number,
231
235
  blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
232
236
  dutyType: row.duty_type,
233
237
  status: row.status,
@@ -235,10 +239,10 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
235
239
  signature: row.signature ?? undefined,
236
240
  nodeId: row.node_id,
237
241
  lockToken: row.lock_token,
238
- startedAt: row.started_at,
239
- completedAt: row.completed_at ?? undefined,
242
+ startedAtMs: row.started_at.getTime(),
243
+ completedAtMs: row.completed_at?.getTime(),
240
244
  errorMessage: row.error_message ?? undefined,
241
- };
245
+ });
242
246
  }
243
247
 
244
248
  /**
@@ -254,8 +258,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
254
258
  * @returns the number of duties cleaned up
255
259
  */
256
260
  async cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
257
- const cutoff = new Date(Date.now() - maxAgeMs);
258
- const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [nodeId, cutoff]);
261
+ const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [nodeId, maxAgeMs]);
259
262
  return result.rowCount ?? 0;
260
263
  }
261
264
 
@@ -277,8 +280,7 @@ export class PostgresSlashingProtectionDatabase implements SlashingProtectionDat
277
280
  * @returns the number of duties cleaned up
278
281
  */
279
282
  async cleanupOldDuties(maxAgeMs: number): Promise<number> {
280
- const cutoff = new Date(Date.now() - maxAgeMs);
281
- const result = await this.pool.query(CLEANUP_OLD_DUTIES, [cutoff]);
283
+ const result = await this.pool.query(CLEANUP_OLD_DUTIES, [maxAgeMs]);
282
284
  return result.rowCount ?? 0;
283
285
  }
284
286
  }
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,6 +20,7 @@ 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
26
  status VARCHAR(20) NOT NULL CHECK (status IN ('signing', 'signed')),
@@ -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,24 @@ WHERE status = 'signed'
203
207
 
204
208
  /**
205
209
  * Query to clean up old duties (for maintenance)
206
- * Removes SIGNED 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
214
  WHERE status = 'signed'
211
- AND started_at < $1;
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;
223
228
  `;
224
229
 
225
230
  /**
@@ -252,6 +257,7 @@ SELECT
252
257
  validator_address,
253
258
  slot,
254
259
  block_number,
260
+ checkpoint_number,
255
261
  block_index_within_checkpoint,
256
262
  duty_type,
257
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 */
@@ -85,6 +104,32 @@ export interface ValidatorDutyRecord {
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 */