@aztec/validator-ha-signer 0.0.1-commit.4d79d1f2d → 0.0.1-commit.4d9804df

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 (58) hide show
  1. package/README.md +12 -4
  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 +70 -0
  6. package/dest/db/lmdb.d.ts.map +1 -0
  7. package/dest/db/lmdb.js +223 -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/errors.d.ts +14 -1
  24. package/dest/errors.d.ts.map +1 -1
  25. package/dest/errors.js +15 -0
  26. package/dest/factory.d.ts +38 -5
  27. package/dest/factory.d.ts.map +1 -1
  28. package/dest/factory.js +104 -8
  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/slashing_protection_service.d.ts +15 -4
  33. package/dest/slashing_protection_service.d.ts.map +1 -1
  34. package/dest/slashing_protection_service.js +28 -14
  35. package/dest/types.d.ts +18 -70
  36. package/dest/types.d.ts.map +1 -1
  37. package/dest/types.js +4 -20
  38. package/dest/validator_ha_signer.d.ts +13 -4
  39. package/dest/validator_ha_signer.d.ts.map +1 -1
  40. package/dest/validator_ha_signer.js +48 -15
  41. package/package.json +11 -7
  42. package/src/db/index.ts +1 -0
  43. package/src/db/lmdb.ts +308 -0
  44. package/src/db/migrations/1_initial-schema.ts +35 -4
  45. package/src/db/migrations/2_add-checkpoint-number.ts +19 -0
  46. package/src/db/postgres.ts +17 -15
  47. package/src/db/schema.ts +13 -7
  48. package/src/db/types.ts +66 -19
  49. package/src/errors.ts +21 -0
  50. package/src/factory.ts +141 -7
  51. package/src/metrics.ts +138 -0
  52. package/src/slashing_protection_service.ts +41 -15
  53. package/src/types.ts +38 -104
  54. package/src/validator_ha_signer.ts +78 -17
  55. package/dest/config.d.ts +0 -101
  56. package/dest/config.d.ts.map +0 -1
  57. package/dest/config.js +0 -92
  58. package/src/config.ts +0 -149
package/dest/types.js CHANGED
@@ -1,21 +1,5 @@
1
- import { BlockNumber } from '@aztec/foundation/branded-types';
2
- import { DutyType } from './db/types.js';
1
+ import { getBlockNumberFromSigningContext as getBlockNumberFromSigningContextFromStdlib, getCheckpointNumberFromSigningContext as getCheckpointNumberFromSigningContextFromStdlib, isHAProtectedContext } from '@aztec/stdlib/ha-signing';
3
2
  export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
4
- /**
5
- * Type guard to check if a SigningContext requires HA protection.
6
- * Returns true for contexts that need HA protection, false for AUTH_REQUEST and TXS.
7
- */ export function isHAProtectedContext(context) {
8
- return context.dutyType !== DutyType.AUTH_REQUEST && context.dutyType !== DutyType.TXS;
9
- }
10
- /**
11
- * Gets the block number from a signing context.
12
- * - Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE): returns BlockNumber(0)
13
- * - Other duties: returns the blockNumber from the context
14
- */ export function getBlockNumberFromSigningContext(context) {
15
- // Check for duty types that have blockNumber
16
- if (context.dutyType === DutyType.BLOCK_PROPOSAL || context.dutyType === DutyType.CHECKPOINT_PROPOSAL || context.dutyType === DutyType.ATTESTATION || context.dutyType === DutyType.ATTESTATIONS_AND_SIGNERS) {
17
- return context.blockNumber;
18
- }
19
- // Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE) don't have blockNumber
20
- return BlockNumber(0);
21
- }
3
+ export { isHAProtectedContext };
4
+ export { getBlockNumberFromSigningContextFromStdlib as getBlockNumberFromSigningContext };
5
+ export { getCheckpointNumberFromSigningContextFromStdlib as getCheckpointNumberFromSigningContext };
@@ -8,8 +8,14 @@
8
8
  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
- import type { ValidatorHASignerConfig } from './config.js';
12
- import { type HAProtectedSigningContext, type SlashingProtectionDatabase } from './types.js';
11
+ import { type DateProvider } from '@aztec/foundation/timer';
12
+ import { type BaseSignerConfig, type HAProtectedSigningContext } from '@aztec/stdlib/ha-signing';
13
+ import type { HASignerMetrics } from './metrics.js';
14
+ import type { SlashingProtectionDatabase } from './types.js';
15
+ export interface ValidatorHASignerDeps {
16
+ metrics: HASignerMetrics;
17
+ dateProvider: DateProvider;
18
+ }
13
19
  /**
14
20
  * Validator High Availability Signer
15
21
  *
@@ -34,7 +40,10 @@ export declare class ValidatorHASigner {
34
40
  private readonly log;
35
41
  private readonly slashingProtection;
36
42
  private readonly rollupAddress;
37
- constructor(db: SlashingProtectionDatabase, config: ValidatorHASignerConfig);
43
+ private readonly dateProvider;
44
+ private readonly metrics;
45
+ private readonly signerCallTimeoutMs;
46
+ constructor(db: SlashingProtectionDatabase, config: BaseSignerConfig, deps: ValidatorHASignerDeps);
38
47
  /**
39
48
  * Sign a message with slashing protection.
40
49
  *
@@ -68,4 +77,4 @@ export declare class ValidatorHASigner {
68
77
  */
69
78
  stop(): Promise<void>;
70
79
  }
71
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFsaWRhdG9yX2hhX3NpZ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B,CAAC;AAIlC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAE7D,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,eAAe,CAAC;IACzB,YAAY,EAAE,YAAY,CAAC;CAC5B;AAKD;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,iBAAiB;IAW1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IAVzB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA4B;IAC/D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAa;IAE3C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAC5C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkB;IAC1C,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAS;IAE7C,YACE,EAAE,EAAE,0BAA0B,EACb,MAAM,EAAE,gBAAgB,EACzC,IAAI,EAAE,qBAAqB,EAiC5B;IAED;;;;;;;;;;;;;;;;OAgBG;IACG,kBAAkB,CACtB,gBAAgB,EAAE,UAAU,EAC5B,WAAW,EAAE,QAAQ,EACrB,OAAO,EAAE,yBAAyB,EAClC,MAAM,EAAE,CAAC,WAAW,EAAE,QAAQ,KAAK,OAAO,CAAC,SAAS,CAAC,GACpD,OAAO,CAAC,SAAS,CAAC,CA2EpB;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;;OAGG;IACG,KAAK,kBAEV;IAED;;;OAGG;IACG,IAAI,kBAGT;CACF"}
@@ -5,9 +5,11 @@
5
5
  * This ensures that even with multiple validator nodes running, only one
6
6
  * node will sign for a given duty (slot + duty type).
7
7
  */ import { createLogger } from '@aztec/foundation/log';
8
- import { DutyType } from './db/types.js';
9
- import { SlashingProtectionService } from './slashing_protection_service.js';
10
- import { getBlockNumberFromSigningContext } from './types.js';
8
+ import { executeTimeout } from '@aztec/foundation/timer';
9
+ import { DutyType, getBlockNumberFromSigningContext, getCheckpointNumberFromSigningContext } from '@aztec/stdlib/ha-signing';
10
+ import { SigningLockLostError } from './errors.js';
11
+ import { DEFAULT_MAX_STUCK_DUTIES_AGE_MS, SlashingProtectionService } from './slashing_protection_service.js';
12
+ /** Default hard timeout (ms) for a single signer call when not configured. */ const DEFAULT_SIGNER_CALL_TIMEOUT_MS = 30_000;
11
13
  /**
12
14
  * Validator High Availability Signer
13
15
  *
@@ -31,21 +33,34 @@ import { getBlockNumberFromSigningContext } from './types.js';
31
33
  log;
32
34
  slashingProtection;
33
35
  rollupAddress;
34
- constructor(db, config){
36
+ dateProvider;
37
+ metrics;
38
+ signerCallTimeoutMs;
39
+ constructor(db, config, deps){
35
40
  this.config = config;
36
41
  this.log = createLogger('validator-ha-signer');
37
- if (!config.haSigningEnabled) {
38
- // this shouldn't happen, the validator should use different signer for non-HA setups
39
- throw new Error('Validator HA Signer is not enabled in config');
40
- }
42
+ this.metrics = deps.metrics;
43
+ this.dateProvider = deps.dateProvider;
44
+ // Clamp the signer-call timeout below half the stuck-duty max age. This maintains the
45
+ // invariant that an in-flight signing always times out and releases its SIGNING row well before
46
+ // stuck-duty cleanup could consider it stuck, so cleanup can never delete a live duty (only
47
+ // signWithProtection writes SIGNING rows, and every path through it is bounded by this timeout).
48
+ // If timers misbehave anyway, the recordSuccess-returns-false throw is the backstop: the duty
49
+ // fails instead of broadcasting an unprotected signature.
50
+ const maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? DEFAULT_MAX_STUCK_DUTIES_AGE_MS;
51
+ this.signerCallTimeoutMs = Math.min(config.signerCallTimeoutMs ?? DEFAULT_SIGNER_CALL_TIMEOUT_MS, maxStuckDutiesAgeMs / 2);
41
52
  if (!config.nodeId || config.nodeId === '') {
42
53
  throw new Error('NODE_ID is required for high-availability setups');
43
54
  }
44
- this.rollupAddress = config.l1Contracts.rollupAddress;
45
- this.slashingProtection = new SlashingProtectionService(db, config);
55
+ this.rollupAddress = config.rollupAddress;
56
+ this.slashingProtection = new SlashingProtectionService(db, config, {
57
+ metrics: deps.metrics,
58
+ dateProvider: deps.dateProvider
59
+ });
46
60
  this.log.info('Validator HA Signer initialized with slashing protection', {
47
61
  nodeId: config.nodeId,
48
- rollupAddress: this.rollupAddress.toString()
62
+ rollupAddress: this.rollupAddress.toString(),
63
+ signerCallTimeoutMs: this.signerCallTimeoutMs
49
64
  });
50
65
  }
51
66
  /**
@@ -65,6 +80,8 @@ import { getBlockNumberFromSigningContext } from './types.js';
65
80
  * @throws DutyAlreadySignedError if the duty was already signed (expected in HA)
66
81
  * @throws SlashingProtectionError if attempting to sign different data for same slot (expected in HA)
67
82
  */ async signWithProtection(validatorAddress, messageHash, context, signFn) {
83
+ const startTime = this.dateProvider.now();
84
+ const dutyType = context.dutyType;
68
85
  let dutyIdentifier;
69
86
  if (context.dutyType === DutyType.BLOCK_PROPOSAL) {
70
87
  dutyIdentifier = {
@@ -83,32 +100,48 @@ import { getBlockNumberFromSigningContext } from './types.js';
83
100
  };
84
101
  }
85
102
  // Acquire lock and get the token for ownership verification
103
+ // DutyAlreadySignedError and SlashingProtectionError may be thrown here and are recorded in the service
86
104
  const blockNumber = getBlockNumberFromSigningContext(context);
105
+ const checkpointNumber = getCheckpointNumberFromSigningContext(context);
87
106
  const lockToken = await this.slashingProtection.checkAndRecord({
88
107
  ...dutyIdentifier,
89
108
  blockNumber,
109
+ checkpointNumber,
90
110
  messageHash: messageHash.toString(),
91
111
  nodeId: this.config.nodeId
92
112
  });
93
- // Perform signing
113
+ // Perform signing under a hard timeout. If the signer hangs, executeTimeout aborts and rejects;
114
+ // the orphaned signFn promise resolving later is discarded (never broadcast). A timeout takes the
115
+ // same failure path as any signing error: release the lock so the duty can be retried safely.
94
116
  let signature;
95
117
  try {
96
- signature = await signFn(messageHash);
118
+ signature = await executeTimeout(()=>signFn(messageHash), this.signerCallTimeoutMs, ()=>new Error(`Signing operation for ${dutyType} at slot ${context.slot} timed out after ` + `${this.signerCallTimeoutMs}ms`));
97
119
  } catch (error) {
98
120
  // Delete duty to allow retry (only succeeds if we own the lock)
99
121
  await this.slashingProtection.deleteDuty({
100
122
  ...dutyIdentifier,
101
123
  lockToken
102
124
  });
125
+ this.metrics.recordSigningError(dutyType);
103
126
  throw error;
104
127
  }
105
- // Record success (only succeeds if we own the lock)
106
- await this.slashingProtection.recordSuccess({
128
+ // Record success (only succeeds if we still own the lock).
129
+ // A false result means our SIGNING row is gone or no longer ours (e.g. deleted by stuck-duty
130
+ // cleanup while signing was slow). We must not broadcast this signature: without a protection
131
+ // record, a later attempt for the same duty with different data would sign freely (slashable).
132
+ // Do not delete the duty here - we no longer own it, and another node may legitimately hold it.
133
+ const recorded = await this.slashingProtection.recordSuccess({
107
134
  ...dutyIdentifier,
108
135
  signature,
109
136
  nodeId: this.config.nodeId,
110
137
  lockToken
111
138
  });
139
+ if (!recorded) {
140
+ this.metrics.recordSigningError(dutyType);
141
+ throw new SigningLockLostError(context.slot, dutyType, this.config.nodeId);
142
+ }
143
+ const duration = this.dateProvider.now() - startTime;
144
+ this.metrics.recordSigningSuccess(dutyType, duration);
112
145
  return signature;
113
146
  }
114
147
  /**
package/package.json CHANGED
@@ -1,24 +1,25 @@
1
1
  {
2
2
  "name": "@aztec/validator-ha-signer",
3
- "version": "0.0.1-commit.4d79d1f2d",
3
+ "version": "0.0.1-commit.4d9804df",
4
4
  "type": "module",
5
5
  "exports": {
6
- "./config": "./dest/config.js",
7
6
  "./db": "./dest/db/index.js",
8
7
  "./errors": "./dest/errors.js",
9
8
  "./factory": "./dest/factory.js",
9
+ "./metrics": "./dest/metrics.js",
10
10
  "./migrations": "./dest/migrations.js",
11
11
  "./slashing-protection-service": "./dest/slashing_protection_service.js",
12
12
  "./types": "./dest/types.js",
13
13
  "./validator-ha-signer": "./dest/validator_ha_signer.js",
14
- "./test": "./dest/test/pglite_pool.js"
14
+ "./test": "./dest/test/pglite_pool.js",
15
+ "./db/lmdb": "./dest/db/lmdb.js"
15
16
  },
16
17
  "typedocOptions": {
17
18
  "entryPoints": [
18
- "./src/config.ts",
19
19
  "./src/db/index.ts",
20
20
  "./src/errors.ts",
21
21
  "./src/factory.ts",
22
+ "./src/metrics.ts",
22
23
  "./src/migrations.ts",
23
24
  "./src/slashing_protection_service.ts",
24
25
  "./src/types.ts",
@@ -74,12 +75,15 @@
74
75
  ]
75
76
  },
76
77
  "dependencies": {
77
- "@aztec/ethereum": "0.0.1-commit.4d79d1f2d",
78
- "@aztec/foundation": "0.0.1-commit.4d79d1f2d",
78
+ "@aztec/ethereum": "0.0.1-commit.4d9804df",
79
+ "@aztec/foundation": "0.0.1-commit.4d9804df",
80
+ "@aztec/kv-store": "0.0.1-commit.4d9804df",
81
+ "@aztec/stdlib": "0.0.1-commit.4d9804df",
82
+ "@aztec/telemetry-client": "0.0.1-commit.4d9804df",
79
83
  "node-pg-migrate": "^8.0.4",
80
84
  "pg": "^8.11.3",
81
85
  "tslib": "^2.4.0",
82
- "zod": "^3.23.8"
86
+ "zod": "^4"
83
87
  },
84
88
  "devDependencies": {
85
89
  "@electric-sql/pglite": "^0.3.14",
package/src/db/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './types.js';
2
2
  export * from './schema.js';
3
3
  export * from './postgres.js';
4
+ export * from './lmdb.js';
package/src/db/lmdb.ts ADDED
@@ -0,0 +1,308 @@
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
+ import { openStoreAt } from '@aztec/kv-store/lmdb-v2';
17
+
18
+ import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
19
+ import {
20
+ type CheckAndRecordParams,
21
+ DutyStatus,
22
+ DutyType,
23
+ type StoredDutyRecord,
24
+ getBlockIndexFromDutyIdentifier,
25
+ recordFromFields,
26
+ } from './types.js';
27
+
28
+ const DUTIES_MAP_NAME = 'signing-protection-duties';
29
+ const LEGACY_CHECKPOINT_NUMBER = '0';
30
+
31
+ type StoredDutyRecordV1 = Omit<StoredDutyRecord, 'checkpointNumber'> & { checkpointNumber?: undefined };
32
+ type MigratableStoredDutyRecord = StoredDutyRecord | StoredDutyRecordV1;
33
+
34
+ function needsCheckpointNumberMigration(record: MigratableStoredDutyRecord): record is StoredDutyRecordV1 {
35
+ return record.checkpointNumber === undefined;
36
+ }
37
+
38
+ /**
39
+ * Migrates local slashing-protection duties from schema 1 to schema 2.
40
+ */
41
+ export async function migrateLmdbSlashingProtectionDatabase(
42
+ dataDirectory: string,
43
+ currentVersion: number,
44
+ latestVersion: number,
45
+ dbMapSizeKb?: number,
46
+ ): Promise<void> {
47
+ if (currentVersion !== 1 || latestVersion !== LmdbSlashingProtectionDatabase.SCHEMA_VERSION) {
48
+ throw new Error(`Unsupported LMDB slashing-protection migration ${currentVersion} -> ${latestVersion}`);
49
+ }
50
+
51
+ const store = await openStoreAt(dataDirectory, dbMapSizeKb);
52
+ try {
53
+ const duties = store.openMap<string, MigratableStoredDutyRecord>(DUTIES_MAP_NAME);
54
+ const migratedRecords: { key: string; value: StoredDutyRecord }[] = [];
55
+
56
+ for await (const [key, record] of duties.entriesAsync()) {
57
+ if (needsCheckpointNumberMigration(record)) {
58
+ migratedRecords.push({ key, value: { ...record, checkpointNumber: LEGACY_CHECKPOINT_NUMBER } });
59
+ }
60
+ }
61
+
62
+ if (migratedRecords.length > 0) {
63
+ await duties.setMany(migratedRecords);
64
+ }
65
+ } finally {
66
+ await store.close();
67
+ }
68
+ }
69
+
70
+ function dutyKey(
71
+ rollupAddress: string,
72
+ validatorAddress: string,
73
+ slot: string,
74
+ dutyType: string,
75
+ blockIndexWithinCheckpoint: number,
76
+ ): string {
77
+ return `${rollupAddress}:${validatorAddress}:${slot}:${dutyType}:${blockIndexWithinCheckpoint}`;
78
+ }
79
+
80
+ /**
81
+ * LMDB-backed implementation of SlashingProtectionDatabase.
82
+ *
83
+ * Provides single-node double-signing protection that survives crashes and restarts.
84
+ * Does not provide cross-node coordination (that requires the PostgreSQL implementation).
85
+ */
86
+ export class LmdbSlashingProtectionDatabase implements SlashingProtectionDatabase {
87
+ public static readonly SCHEMA_VERSION = 2;
88
+
89
+ private readonly duties: AztecAsyncMap<string, StoredDutyRecord>;
90
+ private readonly log: Logger;
91
+
92
+ constructor(
93
+ private readonly store: AztecAsyncKVStore,
94
+ private readonly dateProvider: DateProvider,
95
+ ) {
96
+ this.log = createLogger('slashing-protection:lmdb');
97
+ this.duties = store.openMap<string, StoredDutyRecord>(DUTIES_MAP_NAME);
98
+ }
99
+
100
+ /**
101
+ * Atomically try to insert a new duty record, or get the existing one if present.
102
+ *
103
+ * LMDB is single-writer so the read-then-write inside transactionAsync is naturally atomic.
104
+ */
105
+ public async tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult> {
106
+ const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
107
+ const key = dutyKey(
108
+ params.rollupAddress.toString(),
109
+ params.validatorAddress.toString(),
110
+ params.slot.toString(),
111
+ params.dutyType,
112
+ blockIndexWithinCheckpoint,
113
+ );
114
+
115
+ const lockToken = randomBytes(16).toString('hex');
116
+ const now = this.dateProvider.now();
117
+
118
+ const result = await this.store.transactionAsync(async () => {
119
+ const existing = await this.duties.getAsync(key);
120
+ if (existing) {
121
+ return { isNew: false as const, record: { ...existing, lockToken: '' } };
122
+ }
123
+
124
+ const newRecord: StoredDutyRecord = {
125
+ rollupAddress: params.rollupAddress.toString(),
126
+ validatorAddress: params.validatorAddress.toString(),
127
+ slot: params.slot.toString(),
128
+ blockNumber: params.blockNumber.toString(),
129
+ checkpointNumber: params.checkpointNumber.toString(),
130
+ blockIndexWithinCheckpoint,
131
+ dutyType: params.dutyType,
132
+ status: DutyStatus.SIGNING,
133
+ messageHash: params.messageHash,
134
+ nodeId: params.nodeId,
135
+ lockToken,
136
+ startedAtMs: now,
137
+ };
138
+ await this.duties.set(key, newRecord);
139
+ return { isNew: true as const, record: newRecord };
140
+ });
141
+
142
+ if (result.isNew) {
143
+ this.log.debug(`Acquired lock for duty ${params.dutyType} at slot ${params.slot}`, {
144
+ validatorAddress: params.validatorAddress.toString(),
145
+ nodeId: params.nodeId,
146
+ });
147
+ }
148
+
149
+ return { isNew: result.isNew, record: recordFromFields(result.record) };
150
+ }
151
+
152
+ /**
153
+ * Update a duty to 'signed' status with the signature.
154
+ * Only succeeds if the lockToken matches.
155
+ */
156
+ public updateDutySigned(
157
+ rollupAddress: EthAddress,
158
+ validatorAddress: EthAddress,
159
+ slot: SlotNumber,
160
+ dutyType: DutyType,
161
+ signature: string,
162
+ lockToken: string,
163
+ blockIndexWithinCheckpoint: number,
164
+ ): Promise<boolean> {
165
+ const key = dutyKey(
166
+ rollupAddress.toString(),
167
+ validatorAddress.toString(),
168
+ slot.toString(),
169
+ dutyType,
170
+ blockIndexWithinCheckpoint,
171
+ );
172
+
173
+ return this.store.transactionAsync(async () => {
174
+ const existing = await this.duties.getAsync(key);
175
+ if (!existing) {
176
+ this.log.warn('Failed to update duty to signed: duty not found', {
177
+ rollupAddress: rollupAddress.toString(),
178
+ validatorAddress: validatorAddress.toString(),
179
+ slot: slot.toString(),
180
+ dutyType,
181
+ blockIndexWithinCheckpoint,
182
+ });
183
+ return false;
184
+ }
185
+
186
+ if (existing.lockToken !== lockToken) {
187
+ this.log.warn('Failed to update duty to signed: invalid token', {
188
+ rollupAddress: rollupAddress.toString(),
189
+ validatorAddress: validatorAddress.toString(),
190
+ slot: slot.toString(),
191
+ dutyType,
192
+ blockIndexWithinCheckpoint,
193
+ });
194
+ return false;
195
+ }
196
+
197
+ await this.duties.set(key, {
198
+ ...existing,
199
+ status: DutyStatus.SIGNED,
200
+ signature,
201
+ completedAtMs: this.dateProvider.now(),
202
+ });
203
+
204
+ return true;
205
+ });
206
+ }
207
+
208
+ /**
209
+ * Delete a duty record.
210
+ * Only succeeds if the lockToken matches.
211
+ */
212
+ public deleteDuty(
213
+ rollupAddress: EthAddress,
214
+ validatorAddress: EthAddress,
215
+ slot: SlotNumber,
216
+ dutyType: DutyType,
217
+ lockToken: string,
218
+ blockIndexWithinCheckpoint: number,
219
+ ): Promise<boolean> {
220
+ const key = dutyKey(
221
+ rollupAddress.toString(),
222
+ validatorAddress.toString(),
223
+ slot.toString(),
224
+ dutyType,
225
+ blockIndexWithinCheckpoint,
226
+ );
227
+
228
+ return this.store.transactionAsync(async () => {
229
+ const existing = await this.duties.getAsync(key);
230
+ if (!existing || existing.lockToken !== lockToken) {
231
+ this.log.warn('Failed to delete duty: invalid token or duty not found', {
232
+ rollupAddress: rollupAddress.toString(),
233
+ validatorAddress: validatorAddress.toString(),
234
+ slot: slot.toString(),
235
+ dutyType,
236
+ blockIndexWithinCheckpoint,
237
+ });
238
+ return false;
239
+ }
240
+
241
+ await this.duties.delete(key);
242
+ return true;
243
+ });
244
+ }
245
+
246
+ /**
247
+ * Cleanup own stuck duties (SIGNING status older than maxAgeMs).
248
+ */
249
+ public cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number> {
250
+ const cutoffMs = this.dateProvider.now() - maxAgeMs;
251
+
252
+ return this.store.transactionAsync(async () => {
253
+ const keysToDelete: string[] = [];
254
+ for await (const [key, record] of this.duties.entriesAsync()) {
255
+ if (record.nodeId === nodeId && record.status === DutyStatus.SIGNING && record.startedAtMs < cutoffMs) {
256
+ keysToDelete.push(key);
257
+ }
258
+ }
259
+ for (const key of keysToDelete) {
260
+ await this.duties.delete(key);
261
+ }
262
+ return keysToDelete.length;
263
+ });
264
+ }
265
+
266
+ /**
267
+ * Cleanup duties with outdated rollup address.
268
+ *
269
+ * This is always a no-op for the LMDB implementation: the underlying store is created via
270
+ * DatabaseVersionManager (in factory.ts), which already resets the entire data directory at
271
+ * startup whenever the rollup address changes.
272
+ */
273
+ public cleanupOutdatedRollupDuties(_currentRollupAddress: EthAddress): Promise<number> {
274
+ return Promise.resolve(0);
275
+ }
276
+
277
+ /**
278
+ * Cleanup old signed duties older than maxAgeMs.
279
+ */
280
+ public cleanupOldDuties(maxAgeMs: number): Promise<number> {
281
+ const cutoffMs = this.dateProvider.now() - maxAgeMs;
282
+
283
+ return this.store.transactionAsync(async () => {
284
+ const keysToDelete: string[] = [];
285
+ for await (const [key, record] of this.duties.entriesAsync()) {
286
+ if (
287
+ record.status === DutyStatus.SIGNED &&
288
+ record.completedAtMs !== undefined &&
289
+ record.completedAtMs < cutoffMs
290
+ ) {
291
+ keysToDelete.push(key);
292
+ }
293
+ }
294
+ for (const key of keysToDelete) {
295
+ await this.duties.delete(key);
296
+ }
297
+ return keysToDelete.length;
298
+ });
299
+ }
300
+
301
+ /**
302
+ * Close the underlying LMDB store.
303
+ */
304
+ public async close(): Promise<void> {
305
+ await this.store.close();
306
+ this.log.debug('LMDB slashing protection database closed');
307
+ }
308
+ }
@@ -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
+ }