@aztec/validator-ha-signer 0.0.1-commit.96bb3f7 → 0.0.1-commit.9d2bcf6d

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 (46) hide show
  1. package/README.md +42 -37
  2. package/dest/config.d.ts +71 -17
  3. package/dest/config.d.ts.map +1 -1
  4. package/dest/config.js +47 -19
  5. package/dest/db/postgres.d.ts +34 -5
  6. package/dest/db/postgres.d.ts.map +1 -1
  7. package/dest/db/postgres.js +80 -20
  8. package/dest/db/schema.d.ts +19 -9
  9. package/dest/db/schema.d.ts.map +1 -1
  10. package/dest/db/schema.js +46 -18
  11. package/dest/db/types.d.ts +81 -24
  12. package/dest/db/types.d.ts.map +1 -1
  13. package/dest/db/types.js +34 -0
  14. package/dest/errors.d.ts +9 -5
  15. package/dest/errors.d.ts.map +1 -1
  16. package/dest/errors.js +7 -4
  17. package/dest/factory.d.ts +6 -14
  18. package/dest/factory.d.ts.map +1 -1
  19. package/dest/factory.js +6 -11
  20. package/dest/migrations.d.ts +1 -1
  21. package/dest/migrations.d.ts.map +1 -1
  22. package/dest/migrations.js +13 -2
  23. package/dest/slashing_protection_service.d.ts +16 -6
  24. package/dest/slashing_protection_service.d.ts.map +1 -1
  25. package/dest/slashing_protection_service.js +58 -17
  26. package/dest/test/pglite_pool.d.ts +92 -0
  27. package/dest/test/pglite_pool.d.ts.map +1 -0
  28. package/dest/test/pglite_pool.js +210 -0
  29. package/dest/types.d.ts +91 -14
  30. package/dest/types.d.ts.map +1 -1
  31. package/dest/types.js +21 -1
  32. package/dest/validator_ha_signer.d.ts +10 -13
  33. package/dest/validator_ha_signer.d.ts.map +1 -1
  34. package/dest/validator_ha_signer.js +32 -31
  35. package/package.json +9 -8
  36. package/src/config.ts +83 -50
  37. package/src/db/postgres.ts +101 -19
  38. package/src/db/schema.ts +48 -18
  39. package/src/db/types.ts +111 -22
  40. package/src/errors.ts +7 -2
  41. package/src/factory.ts +8 -13
  42. package/src/migrations.ts +17 -1
  43. package/src/slashing_protection_service.ts +94 -23
  44. package/src/test/pglite_pool.ts +256 -0
  45. package/src/types.ts +140 -19
  46. package/src/validator_ha_signer.ts +39 -41
@@ -6,13 +6,18 @@
6
6
  * node will sign for a given duty (slot + duty type).
7
7
  */
8
8
  import type { Buffer32 } from '@aztec/foundation/buffer';
9
- import type { EthAddress } from '@aztec/foundation/eth-address';
9
+ import { EthAddress } from '@aztec/foundation/eth-address';
10
10
  import type { Signature } from '@aztec/foundation/eth-signature';
11
11
  import { type Logger, createLogger } from '@aztec/foundation/log';
12
12
 
13
- import type { CreateHASignerConfig } from './config.js';
13
+ import type { ValidatorHASignerConfig } from './config.js';
14
+ import { type DutyIdentifier, DutyType } from './db/types.js';
14
15
  import { SlashingProtectionService } from './slashing_protection_service.js';
15
- import type { SigningContext, SlashingProtectionDatabase } from './types.js';
16
+ import {
17
+ type HAProtectedSigningContext,
18
+ type SlashingProtectionDatabase,
19
+ getBlockNumberFromSigningContext,
20
+ } from './types.js';
16
21
 
17
22
  /**
18
23
  * Validator High Availability Signer
@@ -35,15 +40,16 @@ import type { SigningContext, SlashingProtectionDatabase } from './types.js';
35
40
  */
36
41
  export class ValidatorHASigner {
37
42
  private readonly log: Logger;
38
- private readonly slashingProtection: SlashingProtectionService | undefined;
43
+ private readonly slashingProtection: SlashingProtectionService;
44
+ private readonly rollupAddress: EthAddress;
39
45
 
40
46
  constructor(
41
47
  db: SlashingProtectionDatabase,
42
- private readonly config: CreateHASignerConfig,
48
+ private readonly config: ValidatorHASignerConfig,
43
49
  ) {
44
50
  this.log = createLogger('validator-ha-signer');
45
51
 
46
- if (!config.enabled) {
52
+ if (!config.haSigningEnabled) {
47
53
  // this shouldn't happen, the validator should use different signer for non-HA setups
48
54
  throw new Error('Validator HA Signer is not enabled in config');
49
55
  }
@@ -51,9 +57,11 @@ export class ValidatorHASigner {
51
57
  if (!config.nodeId || config.nodeId === '') {
52
58
  throw new Error('NODE_ID is required for high-availability setups');
53
59
  }
60
+ this.rollupAddress = config.l1Contracts.rollupAddress;
54
61
  this.slashingProtection = new SlashingProtectionService(db, config);
55
62
  this.log.info('Validator HA Signer initialized with slashing protection', {
56
63
  nodeId: config.nodeId,
64
+ rollupAddress: this.rollupAddress.toString(),
57
65
  });
58
66
  }
59
67
 
@@ -67,7 +75,7 @@ export class ValidatorHASigner {
67
75
  *
68
76
  * @param validatorAddress - The validator's Ethereum address
69
77
  * @param messageHash - The hash to be signed
70
- * @param context - The signing context (slot, block number, duty type)
78
+ * @param context - The signing context (HA-protected duty types only)
71
79
  * @param signFn - Function that performs the actual signing
72
80
  * @returns The signature
73
81
  *
@@ -77,29 +85,32 @@ export class ValidatorHASigner {
77
85
  async signWithProtection(
78
86
  validatorAddress: EthAddress,
79
87
  messageHash: Buffer32,
80
- context: SigningContext,
88
+ context: HAProtectedSigningContext,
81
89
  signFn: (messageHash: Buffer32) => Promise<Signature>,
82
90
  ): Promise<Signature> {
83
- // If slashing protection is disabled, just sign directly
84
- if (!this.slashingProtection) {
85
- this.log.info('Signing without slashing protection enabled', {
86
- validatorAddress: validatorAddress.toString(),
87
- nodeId: this.config.nodeId,
91
+ let dutyIdentifier: DutyIdentifier;
92
+ if (context.dutyType === DutyType.BLOCK_PROPOSAL) {
93
+ dutyIdentifier = {
94
+ rollupAddress: this.rollupAddress,
95
+ validatorAddress,
96
+ slot: context.slot,
97
+ blockIndexWithinCheckpoint: context.blockIndexWithinCheckpoint,
88
98
  dutyType: context.dutyType,
99
+ };
100
+ } else {
101
+ dutyIdentifier = {
102
+ rollupAddress: this.rollupAddress,
103
+ validatorAddress,
89
104
  slot: context.slot,
90
- blockNumber: context.blockNumber,
91
- });
92
- return await signFn(messageHash);
105
+ dutyType: context.dutyType,
106
+ };
93
107
  }
94
108
 
95
- const { slot, blockNumber, dutyType } = context;
96
-
97
109
  // Acquire lock and get the token for ownership verification
110
+ const blockNumber = getBlockNumberFromSigningContext(context);
98
111
  const lockToken = await this.slashingProtection.checkAndRecord({
99
- validatorAddress,
100
- slot,
112
+ ...dutyIdentifier,
101
113
  blockNumber,
102
- dutyType,
103
114
  messageHash: messageHash.toString(),
104
115
  nodeId: this.config.nodeId,
105
116
  });
@@ -110,20 +121,13 @@ export class ValidatorHASigner {
110
121
  signature = await signFn(messageHash);
111
122
  } catch (error: any) {
112
123
  // Delete duty to allow retry (only succeeds if we own the lock)
113
- await this.slashingProtection.deleteDuty({
114
- validatorAddress,
115
- slot,
116
- dutyType,
117
- lockToken,
118
- });
124
+ await this.slashingProtection.deleteDuty({ ...dutyIdentifier, lockToken });
119
125
  throw error;
120
126
  }
121
127
 
122
128
  // Record success (only succeeds if we own the lock)
123
129
  await this.slashingProtection.recordSuccess({
124
- validatorAddress,
125
- slot,
126
- dutyType,
130
+ ...dutyIdentifier,
127
131
  signature,
128
132
  nodeId: this.config.nodeId,
129
133
  lockToken,
@@ -132,13 +136,6 @@ export class ValidatorHASigner {
132
136
  return signature;
133
137
  }
134
138
 
135
- /**
136
- * Check if slashing protection is enabled
137
- */
138
- get isEnabled(): boolean {
139
- return this.slashingProtection !== undefined;
140
- }
141
-
142
139
  /**
143
140
  * Get the node ID for this signer
144
141
  */
@@ -150,15 +147,16 @@ export class ValidatorHASigner {
150
147
  * Start the HA signer background tasks (cleanup of stuck duties).
151
148
  * Should be called after construction and before signing operations.
152
149
  */
153
- start() {
154
- this.slashingProtection?.start();
150
+ async start() {
151
+ await this.slashingProtection.start();
155
152
  }
156
153
 
157
154
  /**
158
- * Stop the HA signer background tasks.
155
+ * Stop the HA signer background tasks and close database connection.
159
156
  * Should be called during graceful shutdown.
160
157
  */
161
158
  async stop() {
162
- await this.slashingProtection?.stop();
159
+ await this.slashingProtection.stop();
160
+ await this.slashingProtection.close();
163
161
  }
164
162
  }