@aztec/validator-ha-signer 0.0.1-commit.dbf9cec → 0.0.1-commit.df81a97b5

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.
package/src/db/types.ts CHANGED
@@ -1,5 +1,10 @@
1
- import type { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
2
- import type { EthAddress } from '@aztec/foundation/eth-address';
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';
3
8
  import type { Signature } from '@aztec/foundation/eth-signature';
4
9
  import { DutyType } from '@aztec/stdlib/ha-signing';
5
10
 
@@ -23,6 +28,30 @@ export interface DutyRow {
23
28
  error_message: string | null;
24
29
  }
25
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
+
26
55
  /**
27
56
  * Row type from INSERT_OR_GET_DUTY query (includes is_new flag)
28
57
  */
@@ -42,7 +71,8 @@ export enum DutyStatus {
42
71
  export { DutyType };
43
72
 
44
73
  /**
45
- * Record of a validator duty in the database
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.
46
76
  */
47
77
  export interface ValidatorDutyRecord {
48
78
  /** Ethereum address of the rollup contract */
@@ -75,6 +105,31 @@ export interface ValidatorDutyRecord {
75
105
  errorMessage?: string;
76
106
  }
77
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
+
78
133
  /**
79
134
  * Duty identifier for block proposals.
80
135
  * blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
package/src/factory.ts CHANGED
@@ -2,14 +2,16 @@
2
2
  * Factory functions for creating validator HA signers
3
3
  */
4
4
  import { DateProvider } from '@aztec/foundation/timer';
5
- import type { ValidatorHASignerConfig } from '@aztec/stdlib/ha-signing';
5
+ import { createStore } from '@aztec/kv-store/lmdb-v2';
6
+ import type { LocalSignerConfig, ValidatorHASignerConfig } from '@aztec/stdlib/ha-signing';
6
7
  import { getTelemetryClient } from '@aztec/telemetry-client';
7
8
 
8
9
  import { Pool } from 'pg';
9
10
 
11
+ import { LmdbSlashingProtectionDatabase } from './db/lmdb.js';
10
12
  import { PostgresSlashingProtectionDatabase } from './db/postgres.js';
11
13
  import { HASignerMetrics } from './metrics.js';
12
- import type { CreateHASignerDeps, SlashingProtectionDatabase } from './types.js';
14
+ import type { CreateHASignerDeps, CreateLocalSignerWithProtectionDeps, SlashingProtectionDatabase } from './types.js';
13
15
  import { ValidatorHASigner } from './validator_ha_signer.js';
14
16
 
15
17
  /**
@@ -27,7 +29,6 @@ import { ValidatorHASigner } from './validator_ha_signer.js';
27
29
  * ```typescript
28
30
  * const { signer, db } = await createHASigner({
29
31
  * databaseUrl: process.env.DATABASE_URL,
30
- * haSigningEnabled: true,
31
32
  * nodeId: 'validator-node-1',
32
33
  * pollingIntervalMs: 100,
33
34
  * signingTimeoutMs: 3000,
@@ -87,7 +88,84 @@ export async function createHASigner(
87
88
  const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId);
88
89
 
89
90
  // Create signer
90
- const signer = new ValidatorHASigner(db, { ...signerConfig, databaseUrl }, { metrics, dateProvider });
91
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
91
92
 
92
93
  return { signer, db };
93
94
  }
95
+
96
+ /**
97
+ * Create a local (single-node) signing protection signer backed by LMDB.
98
+ *
99
+ * This provides double-signing protection for nodes that are NOT running in a
100
+ * high-availability (multi-node) setup. It prevents a proposer from sending two
101
+ * proposals for the same slot if the node crashes and restarts mid-proposal.
102
+ *
103
+ * When `config.dataDirectory` is set, the protection database is persisted to disk
104
+ * and survives crashes/restarts. When unset, an ephemeral in-memory store is
105
+ * used which protects within a single run but not across restarts.
106
+ *
107
+ * @param config - Local signer config
108
+ * @param deps - Optional dependencies (telemetry, date provider).
109
+ * @returns An object containing the signer and database instances.
110
+ */
111
+ export async function createLocalSignerWithProtection(
112
+ config: LocalSignerConfig,
113
+ deps?: CreateLocalSignerWithProtectionDeps,
114
+ ): Promise<{
115
+ signer: ValidatorHASigner;
116
+ db: SlashingProtectionDatabase;
117
+ }> {
118
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
119
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
120
+
121
+ const kvStore = await createStore('signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
122
+ dataDirectory: config.dataDirectory,
123
+ dataStoreMapSizeKb: config.signingProtectionMapSizeKb ?? config.dataStoreMapSizeKb,
124
+ l1Contracts: config.l1Contracts,
125
+ });
126
+
127
+ const db = new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
128
+
129
+ const signerConfig = {
130
+ ...config,
131
+ nodeId: config.nodeId || 'local',
132
+ };
133
+
134
+ const metrics = new HASignerMetrics(telemetryClient, signerConfig.nodeId, 'LocalSigningProtectionMetrics');
135
+
136
+ const signer = new ValidatorHASigner(db, signerConfig, { metrics, dateProvider });
137
+
138
+ return { signer, db };
139
+ }
140
+
141
+ /**
142
+ * Create an in-memory LMDB-backed SlashingProtectionDatabase that can be shared across
143
+ * multiple validator nodes in the same process. Used for testing HA setups.
144
+ */
145
+ export async function createSharedSlashingProtectionDb(
146
+ dateProvider: DateProvider = new DateProvider(),
147
+ ): Promise<SlashingProtectionDatabase> {
148
+ const kvStore = await createStore('shared-signing-protection', LmdbSlashingProtectionDatabase.SCHEMA_VERSION, {
149
+ dataStoreMapSizeKb: 1024 * 1024,
150
+ });
151
+ return new LmdbSlashingProtectionDatabase(kvStore, dateProvider);
152
+ }
153
+
154
+ /**
155
+ * Create a ValidatorHASigner backed by a pre-existing SlashingProtectionDatabase.
156
+ * Used for testing HA setups where multiple nodes share the same protection database.
157
+ */
158
+ export function createSignerFromSharedDb(
159
+ db: SlashingProtectionDatabase,
160
+ config: Pick<
161
+ ValidatorHASignerConfig,
162
+ 'nodeId' | 'pollingIntervalMs' | 'signingTimeoutMs' | 'maxStuckDutiesAgeMs' | 'l1Contracts'
163
+ >,
164
+ deps?: CreateLocalSignerWithProtectionDeps,
165
+ ): { signer: ValidatorHASigner; db: SlashingProtectionDatabase } {
166
+ const telemetryClient = deps?.telemetryClient ?? getTelemetryClient();
167
+ const dateProvider = deps?.dateProvider ?? new DateProvider();
168
+ const metrics = new HASignerMetrics(telemetryClient, config.nodeId, 'SharedSigningProtectionMetrics');
169
+ const signer = new ValidatorHASigner(db, config, { metrics, dateProvider });
170
+ return { signer, db };
171
+ }
@@ -8,7 +8,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
10
  import type { DateProvider } from '@aztec/foundation/timer';
11
- import type { ValidatorHASignerConfig } from '@aztec/stdlib/ha-signing';
11
+ import type { BaseSignerConfig } from '@aztec/stdlib/ha-signing';
12
12
 
13
13
  import {
14
14
  type CheckAndRecordParams,
@@ -55,7 +55,7 @@ export class SlashingProtectionService {
55
55
 
56
56
  constructor(
57
57
  private readonly db: SlashingProtectionDatabase,
58
- private readonly config: ValidatorHASignerConfig,
58
+ private readonly config: BaseSignerConfig,
59
59
  deps: SlashingProtectionServiceDeps,
60
60
  ) {
61
61
  this.log = createLogger('slashing-protection');
@@ -99,7 +99,7 @@ export class SlashingProtectionService {
99
99
 
100
100
  if (isNew) {
101
101
  // We successfully acquired the lock
102
- this.log.info(`Acquired lock for duty ${dutyType} at slot ${slot}`, {
102
+ this.log.verbose(`Acquired lock for duty ${dutyType} at slot ${slot}`, {
103
103
  validatorAddress: validatorAddress.toString(),
104
104
  nodeId,
105
105
  });
@@ -177,7 +177,7 @@ export class SlashingProtectionService {
177
177
  );
178
178
 
179
179
  if (success) {
180
- 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}`, {
181
181
  validatorAddress: validatorAddress.toString(),
182
182
  nodeId,
183
183
  });
package/src/types.ts CHANGED
@@ -70,6 +70,11 @@ export interface CreateHASignerDeps {
70
70
  dateProvider?: DateProvider;
71
71
  }
72
72
 
73
+ /**
74
+ * deps for creating a local signing protection signer
75
+ */
76
+ export type CreateLocalSignerWithProtectionDeps = Omit<CreateHASignerDeps, 'pool'>;
77
+
73
78
  /**
74
79
  * Database interface for slashing protection operations
75
80
  * This abstraction allows for different database implementations (PostgreSQL, SQLite, etc.)
@@ -11,9 +11,9 @@ import type { Signature } from '@aztec/foundation/eth-signature';
11
11
  import { type Logger, createLogger } from '@aztec/foundation/log';
12
12
  import type { DateProvider } from '@aztec/foundation/timer';
13
13
  import {
14
+ type BaseSignerConfig,
14
15
  DutyType,
15
16
  type HAProtectedSigningContext,
16
- type ValidatorHASignerConfig,
17
17
  getBlockNumberFromSigningContext,
18
18
  } from '@aztec/stdlib/ha-signing';
19
19
 
@@ -56,7 +56,7 @@ export class ValidatorHASigner {
56
56
 
57
57
  constructor(
58
58
  db: SlashingProtectionDatabase,
59
- private readonly config: ValidatorHASignerConfig,
59
+ private readonly config: BaseSignerConfig,
60
60
  deps: ValidatorHASignerDeps,
61
61
  ) {
62
62
  this.log = createLogger('validator-ha-signer');
@@ -64,11 +64,6 @@ export class ValidatorHASigner {
64
64
  this.metrics = deps.metrics;
65
65
  this.dateProvider = deps.dateProvider;
66
66
 
67
- if (!config.haSigningEnabled) {
68
- // this shouldn't happen, the validator should use different signer for non-HA setups
69
- throw new Error('Validator HA Signer is not enabled in config');
70
- }
71
-
72
67
  if (!config.nodeId || config.nodeId === '') {
73
68
  throw new Error('NODE_ID is required for high-availability setups');
74
69
  }