@aztec/validator-ha-signer 0.0.1-commit.0b941701

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 +187 -0
  2. package/dest/config.d.ts +79 -0
  3. package/dest/config.d.ts.map +1 -0
  4. package/dest/config.js +73 -0
  5. package/dest/db/index.d.ts +4 -0
  6. package/dest/db/index.d.ts.map +1 -0
  7. package/dest/db/index.js +3 -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 +70 -0
  12. package/dest/db/postgres.d.ts.map +1 -0
  13. package/dest/db/postgres.js +181 -0
  14. package/dest/db/schema.d.ts +89 -0
  15. package/dest/db/schema.d.ts.map +1 -0
  16. package/dest/db/schema.js +213 -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 +161 -0
  21. package/dest/db/types.d.ts.map +1 -0
  22. package/dest/db/types.js +49 -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 +42 -0
  27. package/dest/factory.d.ts.map +1 -0
  28. package/dest/factory.js +70 -0
  29. package/dest/migrations.d.ts +15 -0
  30. package/dest/migrations.d.ts.map +1 -0
  31. package/dest/migrations.js +53 -0
  32. package/dest/slashing_protection_service.d.ts +80 -0
  33. package/dest/slashing_protection_service.d.ts.map +1 -0
  34. package/dest/slashing_protection_service.js +196 -0
  35. package/dest/test/pglite_pool.d.ts +92 -0
  36. package/dest/test/pglite_pool.d.ts.map +1 -0
  37. package/dest/test/pglite_pool.js +210 -0
  38. package/dest/types.d.ts +139 -0
  39. package/dest/types.d.ts.map +1 -0
  40. package/dest/types.js +21 -0
  41. package/dest/validator_ha_signer.d.ts +70 -0
  42. package/dest/validator_ha_signer.d.ts.map +1 -0
  43. package/dest/validator_ha_signer.js +127 -0
  44. package/package.json +105 -0
  45. package/src/config.ts +125 -0
  46. package/src/db/index.ts +3 -0
  47. package/src/db/migrations/1_initial-schema.ts +26 -0
  48. package/src/db/postgres.ts +251 -0
  49. package/src/db/schema.ts +248 -0
  50. package/src/db/test_helper.ts +17 -0
  51. package/src/db/types.ts +201 -0
  52. package/src/errors.ts +47 -0
  53. package/src/factory.ts +82 -0
  54. package/src/migrations.ts +75 -0
  55. package/src/slashing_protection_service.ts +250 -0
  56. package/src/test/pglite_pool.ts +256 -0
  57. package/src/types.ts +207 -0
  58. package/src/validator_ha_signer.ts +157 -0
package/README.md ADDED
@@ -0,0 +1,187 @@
1
+ # Validator HA Signer
2
+
3
+ Distributed locking and slashing protection for Aztec validators running in high-availability configurations.
4
+
5
+ ## Features
6
+
7
+ - **Distributed Locking**: Prevents multiple validator nodes from signing the same duty
8
+ - **Slashing Protection**: Blocks attempts to sign conflicting data for the same slot
9
+ - **Automatic Retry**: Failed signing attempts are cleared, allowing other nodes to retry
10
+ - **PostgreSQL Backend**: Shared database for coordination across nodes
11
+
12
+ ## Integration with Validator Client
13
+
14
+ The HA signer is automatically integrated into the validator client when `VALIDATOR_HA_SIGNING_ENABLED=true` is set. The validator client will:
15
+
16
+ 1. Create the HA signer using `createHASigner()` from the factory
17
+ 2. Wrap the base keystore with `HAKeyStore` to provide HA-protected signing
18
+ 3. Automatically start/stop the signer lifecycle
19
+
20
+ No manual integration is required when using the validator client.
21
+
22
+ ## Manual Usage
23
+
24
+ For advanced use cases or testing, you can use the HA signer directly. **Note**: Database migrations must be run separately before creating the signer (see [Database Migrations](#database-migrations) below).
25
+
26
+ ### Basic Usage
27
+
28
+ ```bash
29
+ # 1. Run migrations separately (once per deployment)
30
+ aztec migrate-ha-db up --database-url postgresql://user:pass@host:port/db
31
+ ```
32
+
33
+ ```typescript
34
+ // 2. Create signer (migrations already applied)
35
+ import { createHASigner } from '@aztec/validator-ha-signer/factory';
36
+
37
+ const { signer, db } = await createHASigner({
38
+ databaseUrl: process.env.DATABASE_URL,
39
+ haSigningEnabled: true,
40
+ nodeId: 'validator-node-1',
41
+ pollingIntervalMs: 100,
42
+ signingTimeoutMs: 3000,
43
+ });
44
+
45
+ // Start background cleanup tasks
46
+ signer.start();
47
+
48
+ // Sign with protection
49
+ const signature = await signer.signWithProtection(
50
+ validatorAddress,
51
+ messageHash,
52
+ { slot: 100n, blockNumber: 50n, blockIndexWithinCheckpoint: 0, dutyType: 'BLOCK_PROPOSAL' },
53
+ async root => localSigner.signMessage(root),
54
+ );
55
+
56
+ // On shutdown
57
+ await signer.stop();
58
+ await db.close();
59
+ ```
60
+
61
+ ### Advanced: Custom Connection Pool
62
+
63
+ If you need custom pool configuration (e.g., max connections, idle timeout) or want to share a connection pool across multiple components:
64
+
65
+ > **Note**: You still need to run migrations separately before using this approach.
66
+ > See [Database Migrations](#database-migrations) below.
67
+
68
+ ```typescript
69
+ import { PostgresSlashingProtectionDatabase } from '@aztec/validator-ha-signer/db';
70
+ import { ValidatorHASigner } from '@aztec/validator-ha-signer/validator-ha-signer';
71
+
72
+ import { Pool } from 'pg';
73
+
74
+ // Custom pool configuration
75
+ const pool = new Pool({
76
+ connectionString: process.env.DATABASE_URL,
77
+ max: 20, // Maximum connections
78
+ idleTimeoutMillis: 30000,
79
+ });
80
+ const db = new PostgresSlashingProtectionDatabase(pool);
81
+ await db.initialize();
82
+
83
+ const signer = new ValidatorHASigner(db, {
84
+ haSigningEnabled: true,
85
+ nodeId: 'validator-node-1',
86
+ pollingIntervalMs: 100,
87
+ signingTimeoutMs: 3000,
88
+ maxStuckDutiesAgeMs: 144000,
89
+ });
90
+
91
+ // Start background cleanup tasks
92
+ signer.start();
93
+
94
+ // On shutdown
95
+ await signer.stop();
96
+ await pool.end(); // You manage the pool lifecycle
97
+ ```
98
+
99
+ ## Configuration
100
+
101
+ Set via environment variables or config object:
102
+
103
+ - `VALIDATOR_HA_DATABASE_URL`: PostgreSQL connection string (e.g., `postgresql://user:pass@host:port/db`)
104
+ - `VALIDATOR_HA_SIGNING_ENABLED`: Whether HA signing / slashing protection is enabled (default: false)
105
+ - `VALIDATOR_HA_NODE_ID`: Unique identifier for this validator node (required when enabled)
106
+ - `VALIDATOR_HA_POLLING_INTERVAL_MS`: How often to check duty status (default: 100)
107
+ - `VALIDATOR_HA_SIGNING_TIMEOUT_MS`: Max wait for in-progress signing (default: 3000)
108
+ - `VALIDATOR_HA_MAX_STUCK_DUTIES_AGE_MS`: Max age of stuck duties before cleanup (default: 2 \* aztecSlotDuration)
109
+ - `VALIDATOR_HA_POOL_MAX`: Maximum number of connections in the pool (default: 10)
110
+ - `VALIDATOR_HA_POOL_MIN`: Minimum number of connections in the pool (default: 0)
111
+ - `VALIDATOR_HA_POOL_IDLE_TIMEOUT_MS`: Idle timeout for pool connections (default: 10000)
112
+ - `VALIDATOR_HA_POOL_CONNECTION_TIMEOUT_MS`: Connection timeout (default: 0, no timeout)
113
+
114
+ ## Database Migrations
115
+
116
+ This package uses `node-pg-migrate` for database schema management.
117
+
118
+ ### Migration Commands
119
+
120
+ ```bash
121
+ # Run pending migrations
122
+ aztec migrate-ha-db up --database-url postgresql://...
123
+
124
+ # Rollback last migration
125
+ aztec migrate-ha-db down --database-url postgresql://...
126
+ ```
127
+
128
+ ### Creating New Migrations
129
+
130
+ ```bash
131
+ # Generate a new migration file
132
+ npx node-pg-migrate create my-migration-name
133
+ ```
134
+
135
+ ### Production Deployment
136
+
137
+ Run migrations before starting your application:
138
+
139
+ ```yaml
140
+ # Kubernetes example
141
+ apiVersion: batch/v1
142
+ kind: Job
143
+ metadata:
144
+ name: validator-db-migrate
145
+ spec:
146
+ template:
147
+ spec:
148
+ containers:
149
+ - name: migrate
150
+ image: aztecprotocol/aztec:<image_tag>
151
+ command: ['node', '--no-warnings', '/usr/src/yarn-project/aztec/dest/bin/index.js', 'migrate-ha-db', 'up']
152
+ env:
153
+ - name: DATABASE_URL
154
+ valueFrom:
155
+ secretKeyRef:
156
+ name: db-secret
157
+ key: url
158
+ restartPolicy: OnFailure
159
+ ```
160
+
161
+ ## How It Works
162
+
163
+ When multiple validator nodes attempt to sign:
164
+
165
+ 1. First node acquires lock and signs
166
+ 2. Other nodes receive `DutyAlreadySignedError` (expected)
167
+ 3. If different data detected: `SlashingProtectionError` (prevents slashing)
168
+ 4. Failed attempts are auto-cleaned, allowing retry
169
+
170
+ ### Signing Context
171
+
172
+ All signing operations require a `SigningContext` that includes:
173
+
174
+ - `slot`: The slot number
175
+ - `blockNumber`: The block number within the checkpoint
176
+ - `blockIndexWithinCheckpoint`: The index of the block within the checkpoint (use `-1` for N/A contexts)
177
+ - `dutyType`: The type of duty (e.g., `BLOCK_PROPOSAL`, `CHECKPOINT_ATTESTATION`, `AUTH_REQUEST`)
178
+
179
+ Note: `AUTH_REQUEST` duties bypass HA protection since signing multiple times is safe for authentication requests.
180
+
181
+ ## Development
182
+
183
+ ```bash
184
+ yarn build # Build package
185
+ yarn test # Run tests
186
+ yarn clean # Clean build artifacts
187
+ ```
@@ -0,0 +1,79 @@
1
+ import { type ConfigMappingsType } from '@aztec/foundation/config';
2
+ import { z } from 'zod';
3
+ /**
4
+ * Configuration for the Validator HA Signer
5
+ *
6
+ * This config is used for distributed locking and slashing protection
7
+ * when running multiple validator nodes in a high-availability setup.
8
+ */
9
+ export interface ValidatorHASignerConfig {
10
+ /** Whether HA signing / slashing protection is enabled */
11
+ haSigningEnabled: boolean;
12
+ /** Unique identifier for this node */
13
+ nodeId: string;
14
+ /** How long to wait between polls when a duty is being signed (ms) */
15
+ pollingIntervalMs: number;
16
+ /** Maximum time to wait for a duty being signed to complete (ms) */
17
+ signingTimeoutMs: number;
18
+ /** Maximum age of a stuck duty in ms (defaults to 2x hardcoded Aztec slot duration if not set) */
19
+ maxStuckDutiesAgeMs?: number;
20
+ /**
21
+ * PostgreSQL connection string
22
+ * Format: postgresql://user:password@host:port/database
23
+ */
24
+ databaseUrl?: string;
25
+ /**
26
+ * PostgreSQL connection pool configuration
27
+ */
28
+ /** Maximum number of clients in the pool (default: 10) */
29
+ poolMaxCount?: number;
30
+ /** Minimum number of clients in the pool (default: 0) */
31
+ poolMinCount?: number;
32
+ /** Idle timeout in milliseconds (default: 10000) */
33
+ poolIdleTimeoutMs?: number;
34
+ /** Connection timeout in milliseconds (default: 0, no timeout) */
35
+ poolConnectionTimeoutMs?: number;
36
+ }
37
+ export declare const validatorHASignerConfigMappings: ConfigMappingsType<ValidatorHASignerConfig>;
38
+ export declare const defaultValidatorHASignerConfig: ValidatorHASignerConfig;
39
+ /**
40
+ * Returns the validator HA signer configuration from environment variables.
41
+ * Note: If an environment variable is not set, the default value is used.
42
+ * @returns The validator HA signer configuration.
43
+ */
44
+ export declare function getConfigEnvVars(): ValidatorHASignerConfig;
45
+ export declare const ValidatorHASignerConfigSchema: z.ZodObject<{
46
+ haSigningEnabled: z.ZodBoolean;
47
+ nodeId: z.ZodString;
48
+ pollingIntervalMs: z.ZodNumber;
49
+ signingTimeoutMs: z.ZodNumber;
50
+ maxStuckDutiesAgeMs: z.ZodOptional<z.ZodNumber>;
51
+ databaseUrl: z.ZodOptional<z.ZodString>;
52
+ poolMaxCount: z.ZodOptional<z.ZodNumber>;
53
+ poolMinCount: z.ZodOptional<z.ZodNumber>;
54
+ poolIdleTimeoutMs: z.ZodOptional<z.ZodNumber>;
55
+ poolConnectionTimeoutMs: z.ZodOptional<z.ZodNumber>;
56
+ }, "strip", z.ZodTypeAny, {
57
+ haSigningEnabled: boolean;
58
+ nodeId: string;
59
+ pollingIntervalMs: number;
60
+ signingTimeoutMs: number;
61
+ maxStuckDutiesAgeMs?: number | undefined;
62
+ databaseUrl?: string | undefined;
63
+ poolMaxCount?: number | undefined;
64
+ poolMinCount?: number | undefined;
65
+ poolIdleTimeoutMs?: number | undefined;
66
+ poolConnectionTimeoutMs?: number | undefined;
67
+ }, {
68
+ haSigningEnabled: boolean;
69
+ nodeId: string;
70
+ pollingIntervalMs: number;
71
+ signingTimeoutMs: number;
72
+ maxStuckDutiesAgeMs?: number | undefined;
73
+ databaseUrl?: string | undefined;
74
+ poolMaxCount?: number | undefined;
75
+ poolMinCount?: number | undefined;
76
+ poolIdleTimeoutMs?: number | undefined;
77
+ poolConnectionTimeoutMs?: number | undefined;
78
+ }>;
79
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY29uZmlnLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFDTCxLQUFLLGtCQUFrQixFQU14QixNQUFNLDBCQUEwQixDQUFDO0FBR2xDLE9BQU8sRUFBRSxDQUFDLEVBQUUsTUFBTSxLQUFLLENBQUM7QUFFeEI7Ozs7O0dBS0c7QUFDSCxNQUFNLFdBQVcsdUJBQXVCO0lBQ3RDLDBEQUEwRDtJQUMxRCxnQkFBZ0IsRUFBRSxPQUFPLENBQUM7SUFDMUIsc0NBQXNDO0lBQ3RDLE1BQU0sRUFBRSxNQUFNLENBQUM7SUFDZixzRUFBc0U7SUFDdEUsaUJBQWlCLEVBQUUsTUFBTSxDQUFDO0lBQzFCLG9FQUFvRTtJQUNwRSxnQkFBZ0IsRUFBRSxNQUFNLENBQUM7SUFDekIsa0dBQWtHO0lBQ2xHLG1CQUFtQixDQUFDLEVBQUUsTUFBTSxDQUFDO0lBQzdCOzs7T0FHRztJQUNILFdBQVcsQ0FBQyxFQUFFLE1BQU0sQ0FBQztJQUNyQjs7T0FFRztJQUNILDBEQUEwRDtJQUMxRCxZQUFZLENBQUMsRUFBRSxNQUFNLENBQUM7SUFDdEIseURBQXlEO0lBQ3pELFlBQVksQ0FBQyxFQUFFLE1BQU0sQ0FBQztJQUN0QixvREFBb0Q7SUFDcEQsaUJBQWlCLENBQUM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+B,EAAE,kBAAkB,CAAC,uBAAuB,CAmDvF,CAAC;AAEF,eAAO,MAAM,8BAA8B,EAAE,uBAE5C,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,uBAAuB,CAE1D;AAED,eAAO,MAAM,6BAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAWE,CAAC"}
package/dest/config.js ADDED
@@ -0,0 +1,73 @@
1
+ import { booleanConfigHelper, getConfigFromMappings, getDefaultConfig, numberConfigHelper, optionalNumberConfigHelper } from '@aztec/foundation/config';
2
+ import { z } from 'zod';
3
+ export const validatorHASignerConfigMappings = {
4
+ haSigningEnabled: {
5
+ env: 'VALIDATOR_HA_SIGNING_ENABLED',
6
+ description: 'Whether HA signing / slashing protection is enabled',
7
+ ...booleanConfigHelper(false)
8
+ },
9
+ nodeId: {
10
+ env: 'VALIDATOR_HA_NODE_ID',
11
+ description: 'The unique identifier for this node',
12
+ defaultValue: ''
13
+ },
14
+ pollingIntervalMs: {
15
+ env: 'VALIDATOR_HA_POLLING_INTERVAL_MS',
16
+ description: 'The number of ms to wait between polls when a duty is being signed',
17
+ ...numberConfigHelper(100)
18
+ },
19
+ signingTimeoutMs: {
20
+ env: 'VALIDATOR_HA_SIGNING_TIMEOUT_MS',
21
+ description: 'The maximum time to wait for a duty being signed to complete',
22
+ ...numberConfigHelper(3_000)
23
+ },
24
+ maxStuckDutiesAgeMs: {
25
+ env: 'VALIDATOR_HA_MAX_STUCK_DUTIES_AGE_MS',
26
+ description: 'The maximum age of a stuck duty in ms (defaults to 2x Aztec slot duration)',
27
+ ...optionalNumberConfigHelper()
28
+ },
29
+ databaseUrl: {
30
+ env: 'VALIDATOR_HA_DATABASE_URL',
31
+ description: 'PostgreSQL connection string for validator HA signer (format: postgresql://user:password@host:port/database)'
32
+ },
33
+ poolMaxCount: {
34
+ env: 'VALIDATOR_HA_POOL_MAX',
35
+ description: 'Maximum number of clients in the pool',
36
+ ...numberConfigHelper(10)
37
+ },
38
+ poolMinCount: {
39
+ env: 'VALIDATOR_HA_POOL_MIN',
40
+ description: 'Minimum number of clients in the pool',
41
+ ...numberConfigHelper(0)
42
+ },
43
+ poolIdleTimeoutMs: {
44
+ env: 'VALIDATOR_HA_POOL_IDLE_TIMEOUT_MS',
45
+ description: 'Idle timeout in milliseconds',
46
+ ...numberConfigHelper(10_000)
47
+ },
48
+ poolConnectionTimeoutMs: {
49
+ env: 'VALIDATOR_HA_POOL_CONNECTION_TIMEOUT_MS',
50
+ description: 'Connection timeout in milliseconds (0 means no timeout)',
51
+ ...numberConfigHelper(0)
52
+ }
53
+ };
54
+ export const defaultValidatorHASignerConfig = getDefaultConfig(validatorHASignerConfigMappings);
55
+ /**
56
+ * Returns the validator HA signer configuration from environment variables.
57
+ * Note: If an environment variable is not set, the default value is used.
58
+ * @returns The validator HA signer configuration.
59
+ */ export function getConfigEnvVars() {
60
+ return getConfigFromMappings(validatorHASignerConfigMappings);
61
+ }
62
+ export const ValidatorHASignerConfigSchema = z.object({
63
+ haSigningEnabled: z.boolean(),
64
+ nodeId: z.string(),
65
+ pollingIntervalMs: z.number().min(0),
66
+ signingTimeoutMs: z.number().min(0),
67
+ maxStuckDutiesAgeMs: z.number().min(0).optional(),
68
+ databaseUrl: z.string().optional(),
69
+ poolMaxCount: z.number().min(0).optional(),
70
+ poolMinCount: z.number().min(0).optional(),
71
+ poolIdleTimeoutMs: z.number().min(0).optional(),
72
+ poolConnectionTimeoutMs: z.number().min(0).optional()
73
+ });
@@ -0,0 +1,4 @@
1
+ export * from './types.js';
2
+ export * from './schema.js';
3
+ export * from './postgres.js';
4
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYi9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLFlBQVksQ0FBQztBQUMzQixjQUFjLGFBQWEsQ0FBQztBQUM1QixjQUFjLGVBQWUsQ0FBQyJ9
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/db/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from './types.js';
2
+ export * from './schema.js';
3
+ export * from './postgres.js';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Initial schema for validator HA slashing protection
3
+ *
4
+ * This migration imports SQL from the schema.ts file to ensure a single source of truth.
5
+ */
6
+ import type { MigrationBuilder } from 'node-pg-migrate';
7
+ export declare function up(pgm: MigrationBuilder): void;
8
+ export declare function down(pgm: MigrationBuilder): void;
9
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMV9pbml0aWFsLXNjaGVtYS5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2RiL21pZ3JhdGlvbnMvMV9pbml0aWFsLXNjaGVtYS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7OztHQUlHO0FBQ0gsT0FBTyxLQUFLLEVBQUUsZ0JBQWdCLEVBQUUsTUFBTSxpQkFBaUIsQ0FBQztBQUl4RCx3QkFBZ0IsRUFBRSxDQUFDLEdBQUcsRUFBRSxnQkFBZ0IsR0FBRyxJQUFJLENBVzlDO0FBRUQsd0JBQWdCLElBQUksQ0FBQyxHQUFHLEVBQUUsZ0JBQWdCLEdBQUcsSUFBSSxDQUdoRCJ9
@@ -0,0 +1 @@
1
+ {"version":3,"file":"1_initial-schema.d.ts","sourceRoot":"","sources":["../../../src/db/migrations/1_initial-schema.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAIxD,wBAAgB,EAAE,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAW9C;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAGhD"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Initial schema for validator HA slashing protection
3
+ *
4
+ * This migration imports SQL from the schema.ts file to ensure a single source of truth.
5
+ */ import { DROP_SCHEMA_VERSION_TABLE, DROP_VALIDATOR_DUTIES_TABLE, SCHEMA_SETUP, SCHEMA_VERSION } from '../schema.js';
6
+ export function up(pgm) {
7
+ for (const statement of SCHEMA_SETUP){
8
+ pgm.sql(statement);
9
+ }
10
+ // Insert initial schema version
11
+ pgm.sql(`
12
+ INSERT INTO schema_version (version)
13
+ VALUES (${SCHEMA_VERSION})
14
+ ON CONFLICT (version) DO NOTHING;
15
+ `);
16
+ }
17
+ export function down(pgm) {
18
+ pgm.sql(DROP_VALIDATOR_DUTIES_TABLE);
19
+ pgm.sql(DROP_SCHEMA_VERSION_TABLE);
20
+ }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * PostgreSQL implementation of SlashingProtectionDatabase
3
+ */
4
+ import { SlotNumber } from '@aztec/foundation/branded-types';
5
+ import { EthAddress } from '@aztec/foundation/eth-address';
6
+ import type { QueryResult, QueryResultRow } from 'pg';
7
+ import type { SlashingProtectionDatabase, TryInsertOrGetResult } from '../types.js';
8
+ import type { CheckAndRecordParams, DutyType } from './types.js';
9
+ /**
10
+ * Minimal pool interface for database operations.
11
+ * Both pg.Pool and test adapters (e.g., PGlite) satisfy this interface.
12
+ */
13
+ export interface QueryablePool {
14
+ query<R extends QueryResultRow = any>(text: string, values?: any[]): Promise<QueryResult<R>>;
15
+ end(): Promise<void>;
16
+ }
17
+ /**
18
+ * PostgreSQL implementation of the slashing protection database
19
+ */
20
+ export declare class PostgresSlashingProtectionDatabase implements SlashingProtectionDatabase {
21
+ private readonly pool;
22
+ private readonly log;
23
+ constructor(pool: QueryablePool);
24
+ /**
25
+ * Verify that database migrations have been run and schema version matches.
26
+ * Should be called once at startup.
27
+ *
28
+ * @throws Error if migrations haven't been run or schema version is outdated
29
+ */
30
+ initialize(): Promise<void>;
31
+ /**
32
+ * Atomically try to insert a new duty record, or get the existing one if present.
33
+ *
34
+ * @returns { isNew: true, record } if we successfully inserted and acquired the lock
35
+ * @returns { isNew: false, record } if a record already exists. lock_token is empty if the record already exists.
36
+ *
37
+ * Retries if no rows are returned, which can happen under high concurrency
38
+ * when another transaction just committed the row but it's not yet visible.
39
+ */
40
+ tryInsertOrGetExisting(params: CheckAndRecordParams): Promise<TryInsertOrGetResult>;
41
+ /**
42
+ * Update a duty to 'signed' status with the signature.
43
+ * Only succeeds if the lockToken matches (caller must be the one who created the duty).
44
+ *
45
+ * @returns true if the update succeeded, false if token didn't match or duty not found
46
+ */
47
+ updateDutySigned(validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, signature: string, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
48
+ /**
49
+ * Delete a duty record.
50
+ * Only succeeds if the lockToken matches (caller must be the one who created the duty).
51
+ * Used when signing fails to allow another node/attempt to retry.
52
+ *
53
+ * @returns true if the delete succeeded, false if token didn't match or duty not found
54
+ */
55
+ deleteDuty(validatorAddress: EthAddress, slot: SlotNumber, dutyType: DutyType, lockToken: string, blockIndexWithinCheckpoint: number): Promise<boolean>;
56
+ /**
57
+ * Convert a database row to a ValidatorDutyRecord
58
+ */
59
+ private rowToRecord;
60
+ /**
61
+ * Close the database connection pool
62
+ */
63
+ close(): Promise<void>;
64
+ /**
65
+ * Cleanup own stuck duties
66
+ * @returns the number of duties cleaned up
67
+ */
68
+ cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number>;
69
+ }
70
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicG9zdGdyZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9kYi9wb3N0Z3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E,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAI7E;CACF"}
@@ -0,0 +1,181 @@
1
+ /**
2
+ * PostgreSQL implementation of SlashingProtectionDatabase
3
+ */ import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
4
+ import { randomBytes } from '@aztec/foundation/crypto/random';
5
+ import { EthAddress } from '@aztec/foundation/eth-address';
6
+ import { createLogger } from '@aztec/foundation/log';
7
+ import { makeBackoff, retry } from '@aztec/foundation/retry';
8
+ import { CLEANUP_OWN_STUCK_DUTIES, DELETE_DUTY, INSERT_OR_GET_DUTY, SCHEMA_VERSION, UPDATE_DUTY_SIGNED } from './schema.js';
9
+ import { getBlockIndexFromDutyIdentifier } from './types.js';
10
+ /**
11
+ * PostgreSQL implementation of the slashing protection database
12
+ */ export class PostgresSlashingProtectionDatabase {
13
+ pool;
14
+ log;
15
+ constructor(pool){
16
+ this.pool = pool;
17
+ this.log = createLogger('slashing-protection:postgres');
18
+ }
19
+ /**
20
+ * Verify that database migrations have been run and schema version matches.
21
+ * Should be called once at startup.
22
+ *
23
+ * @throws Error if migrations haven't been run or schema version is outdated
24
+ */ async initialize() {
25
+ let dbVersion;
26
+ try {
27
+ const result = await this.pool.query(`SELECT version FROM schema_version ORDER BY version DESC LIMIT 1`);
28
+ if (result.rows.length === 0) {
29
+ throw new Error('No version found');
30
+ }
31
+ dbVersion = result.rows[0].version;
32
+ } catch {
33
+ throw new Error('Database schema not initialized. Please run migrations first: aztec migrate-ha-db up --database-url <url>');
34
+ }
35
+ if (dbVersion < SCHEMA_VERSION) {
36
+ throw new Error(`Database schema version ${dbVersion} is outdated (expected ${SCHEMA_VERSION}). Please run migrations: aztec migrate-ha-db up --database-url <url>`);
37
+ }
38
+ if (dbVersion > SCHEMA_VERSION) {
39
+ throw new Error(`Database schema version ${dbVersion} is newer than expected (${SCHEMA_VERSION}). Please update your application.`);
40
+ }
41
+ this.log.info('Database schema verified', {
42
+ version: dbVersion
43
+ });
44
+ }
45
+ /**
46
+ * Atomically try to insert a new duty record, or get the existing one if present.
47
+ *
48
+ * @returns { isNew: true, record } if we successfully inserted and acquired the lock
49
+ * @returns { isNew: false, record } if a record already exists. lock_token is empty if the record already exists.
50
+ *
51
+ * Retries if no rows are returned, which can happen under high concurrency
52
+ * when another transaction just committed the row but it's not yet visible.
53
+ */ async tryInsertOrGetExisting(params) {
54
+ // create a token for ownership verification
55
+ const lockToken = randomBytes(16).toString('hex');
56
+ // Use fast retries with custom backoff: 10ms, 20ms, 30ms (then stop)
57
+ const fastBackoff = makeBackoff([
58
+ 0.01,
59
+ 0.02,
60
+ 0.03
61
+ ]);
62
+ // Get the normalized block index using type-safe helper
63
+ const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
64
+ const result = await retry(async ()=>{
65
+ const queryResult = await this.pool.query(INSERT_OR_GET_DUTY, [
66
+ params.validatorAddress.toString(),
67
+ params.slot.toString(),
68
+ params.blockNumber.toString(),
69
+ blockIndexWithinCheckpoint,
70
+ params.dutyType,
71
+ params.messageHash,
72
+ params.nodeId,
73
+ lockToken
74
+ ]);
75
+ // Throw error if no rows to trigger retry
76
+ if (queryResult.rows.length === 0) {
77
+ throw new Error('INSERT_OR_GET_DUTY returned no rows');
78
+ }
79
+ return queryResult;
80
+ }, `INSERT_OR_GET_DUTY for node ${params.nodeId}`, fastBackoff, this.log, true);
81
+ if (result.rows.length === 0) {
82
+ // this should never happen as the retry function should throw if it still fails after retries
83
+ throw new Error('INSERT_OR_GET_DUTY returned no rows after retries');
84
+ }
85
+ if (result.rows.length > 1) {
86
+ // this should never happen if database constraints are correct (PRIMARY KEY should prevent duplicates)
87
+ throw new Error(`INSERT_OR_GET_DUTY returned ${result.rows.length} rows (expected exactly 1).`);
88
+ }
89
+ const row = result.rows[0];
90
+ return {
91
+ isNew: row.is_new,
92
+ record: this.rowToRecord(row)
93
+ };
94
+ }
95
+ /**
96
+ * Update a duty to 'signed' status with the signature.
97
+ * Only succeeds if the lockToken matches (caller must be the one who created the duty).
98
+ *
99
+ * @returns true if the update succeeded, false if token didn't match or duty not found
100
+ */ async updateDutySigned(validatorAddress, slot, dutyType, signature, lockToken, blockIndexWithinCheckpoint) {
101
+ const result = await this.pool.query(UPDATE_DUTY_SIGNED, [
102
+ signature,
103
+ validatorAddress.toString(),
104
+ slot.toString(),
105
+ dutyType,
106
+ blockIndexWithinCheckpoint,
107
+ lockToken
108
+ ]);
109
+ if (result.rowCount === 0) {
110
+ this.log.warn('Failed to update duty to signed status: invalid token or duty not found', {
111
+ validatorAddress: validatorAddress.toString(),
112
+ slot: slot.toString(),
113
+ dutyType,
114
+ blockIndexWithinCheckpoint
115
+ });
116
+ return false;
117
+ }
118
+ return true;
119
+ }
120
+ /**
121
+ * Delete a duty record.
122
+ * Only succeeds if the lockToken matches (caller must be the one who created the duty).
123
+ * Used when signing fails to allow another node/attempt to retry.
124
+ *
125
+ * @returns true if the delete succeeded, false if token didn't match or duty not found
126
+ */ async deleteDuty(validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint) {
127
+ const result = await this.pool.query(DELETE_DUTY, [
128
+ validatorAddress.toString(),
129
+ slot.toString(),
130
+ dutyType,
131
+ blockIndexWithinCheckpoint,
132
+ lockToken
133
+ ]);
134
+ if (result.rowCount === 0) {
135
+ this.log.warn('Failed to delete duty: invalid token or duty not found', {
136
+ validatorAddress: validatorAddress.toString(),
137
+ slot: slot.toString(),
138
+ dutyType,
139
+ blockIndexWithinCheckpoint
140
+ });
141
+ return false;
142
+ }
143
+ return true;
144
+ }
145
+ /**
146
+ * Convert a database row to a ValidatorDutyRecord
147
+ */ rowToRecord(row) {
148
+ return {
149
+ validatorAddress: EthAddress.fromString(row.validator_address),
150
+ slot: SlotNumber.fromString(row.slot),
151
+ blockNumber: BlockNumber.fromString(row.block_number),
152
+ blockIndexWithinCheckpoint: row.block_index_within_checkpoint,
153
+ dutyType: row.duty_type,
154
+ status: row.status,
155
+ messageHash: row.message_hash,
156
+ signature: row.signature ?? undefined,
157
+ nodeId: row.node_id,
158
+ lockToken: row.lock_token,
159
+ startedAt: row.started_at,
160
+ completedAt: row.completed_at ?? undefined,
161
+ errorMessage: row.error_message ?? undefined
162
+ };
163
+ }
164
+ /**
165
+ * Close the database connection pool
166
+ */ async close() {
167
+ await this.pool.end();
168
+ this.log.info('Database connection pool closed');
169
+ }
170
+ /**
171
+ * Cleanup own stuck duties
172
+ * @returns the number of duties cleaned up
173
+ */ async cleanupOwnStuckDuties(nodeId, maxAgeMs) {
174
+ const cutoff = new Date(Date.now() - maxAgeMs);
175
+ const result = await this.pool.query(CLEANUP_OWN_STUCK_DUTIES, [
176
+ nodeId,
177
+ cutoff
178
+ ]);
179
+ return result.rowCount ?? 0;
180
+ }
181
+ }