@aztec/validator-ha-signer 0.0.1-commit.96bb3f7 → 0.0.1-commit.e61ad554
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/README.md +42 -37
- package/dest/config.d.ts +49 -17
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +28 -19
- package/dest/db/postgres.d.ts +20 -5
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +50 -19
- package/dest/db/schema.d.ts +11 -7
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +19 -7
- package/dest/db/types.d.ts +75 -23
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +34 -0
- package/dest/errors.d.ts +9 -5
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +7 -4
- package/dest/factory.d.ts +6 -14
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +6 -11
- package/dest/migrations.d.ts +1 -1
- package/dest/migrations.d.ts.map +1 -1
- package/dest/migrations.js +13 -2
- package/dest/slashing_protection_service.d.ts +9 -3
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +21 -9
- package/dest/test/pglite_pool.d.ts +92 -0
- package/dest/test/pglite_pool.d.ts.map +1 -0
- package/dest/test/pglite_pool.js +210 -0
- package/dest/types.d.ts +78 -14
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +21 -1
- package/dest/validator_ha_signer.d.ts +7 -11
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +25 -29
- package/package.json +8 -8
- package/src/config.ts +59 -50
- package/src/db/postgres.ts +68 -19
- package/src/db/schema.ts +19 -7
- package/src/db/types.ts +105 -21
- package/src/errors.ts +7 -2
- package/src/factory.ts +8 -13
- package/src/migrations.ts +17 -1
- package/src/slashing_protection_service.ts +46 -12
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +121 -19
- package/src/validator_ha_signer.ts +32 -39
package/src/types.ts
CHANGED
|
@@ -1,27 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BlockNumber,
|
|
3
|
+
type CheckpointNumber,
|
|
4
|
+
type IndexWithinCheckpoint,
|
|
5
|
+
type SlotNumber,
|
|
6
|
+
} from '@aztec/foundation/branded-types';
|
|
1
7
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
8
|
|
|
3
9
|
import type { Pool } from 'pg';
|
|
4
10
|
|
|
5
|
-
import type {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
import type { ValidatorHASignerConfig } from './config.js';
|
|
12
|
+
import {
|
|
13
|
+
type BlockProposalDutyIdentifier,
|
|
14
|
+
type CheckAndRecordParams,
|
|
15
|
+
type DeleteDutyParams,
|
|
16
|
+
type DutyIdentifier,
|
|
10
17
|
DutyType,
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
type OtherDutyIdentifier,
|
|
19
|
+
type RecordSuccessParams,
|
|
20
|
+
type ValidatorDutyRecord,
|
|
13
21
|
} from './db/types.js';
|
|
14
22
|
|
|
15
23
|
export type {
|
|
24
|
+
BlockProposalDutyIdentifier,
|
|
16
25
|
CheckAndRecordParams,
|
|
17
|
-
CreateHASignerConfig,
|
|
18
26
|
DeleteDutyParams,
|
|
19
27
|
DutyIdentifier,
|
|
28
|
+
OtherDutyIdentifier,
|
|
20
29
|
RecordSuccessParams,
|
|
21
|
-
SlashingProtectionConfig,
|
|
22
30
|
ValidatorDutyRecord,
|
|
31
|
+
ValidatorHASignerConfig,
|
|
23
32
|
};
|
|
24
|
-
export { DutyStatus, DutyType } from './db/types.js';
|
|
33
|
+
export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
|
|
25
34
|
|
|
26
35
|
/**
|
|
27
36
|
* Result of tryInsertOrGetExisting operation
|
|
@@ -45,17 +54,97 @@ export interface CreateHASignerDeps {
|
|
|
45
54
|
}
|
|
46
55
|
|
|
47
56
|
/**
|
|
48
|
-
*
|
|
57
|
+
* Base context for signing operations
|
|
49
58
|
*/
|
|
50
|
-
|
|
59
|
+
interface BaseSigningContext {
|
|
51
60
|
/** Slot number for this duty */
|
|
52
|
-
slot:
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
slot: SlotNumber;
|
|
62
|
+
/**
|
|
63
|
+
* Block or checkpoint number for this duty.
|
|
64
|
+
* For block proposals, this is the block number.
|
|
65
|
+
* For checkpoint proposals, this is the checkpoint number.
|
|
66
|
+
*/
|
|
67
|
+
blockNumber: BlockNumber | CheckpointNumber;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Signing context for block proposals.
|
|
72
|
+
* blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
|
|
73
|
+
*/
|
|
74
|
+
export interface BlockProposalSigningContext extends BaseSigningContext {
|
|
75
|
+
/** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
|
|
76
|
+
blockIndexWithinCheckpoint: IndexWithinCheckpoint;
|
|
77
|
+
dutyType: DutyType.BLOCK_PROPOSAL;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Signing context for non-block-proposal duties that require HA protection.
|
|
82
|
+
* blockIndexWithinCheckpoint is not applicable (internally always -1).
|
|
83
|
+
*/
|
|
84
|
+
export interface OtherSigningContext extends BaseSigningContext {
|
|
85
|
+
dutyType: DutyType.CHECKPOINT_PROPOSAL | DutyType.ATTESTATION | DutyType.ATTESTATIONS_AND_SIGNERS;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Signing context for governance/slashing votes which only need slot for HA protection.
|
|
90
|
+
* blockNumber is not applicable (internally always 0).
|
|
91
|
+
*/
|
|
92
|
+
export interface VoteSigningContext {
|
|
93
|
+
slot: SlotNumber;
|
|
94
|
+
dutyType: DutyType.GOVERNANCE_VOTE | DutyType.SLASHING_VOTE;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Signing context for duties which don't require slot/blockNumber
|
|
99
|
+
* as they don't need HA protection (AUTH_REQUEST, TXS).
|
|
100
|
+
*/
|
|
101
|
+
export interface NoHAProtectionSigningContext {
|
|
102
|
+
dutyType: DutyType.AUTH_REQUEST | DutyType.TXS;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Signing contexts that require HA protection (excludes AUTH_REQUEST).
|
|
107
|
+
* Used by the HA signer's signWithProtection method.
|
|
108
|
+
*/
|
|
109
|
+
export type HAProtectedSigningContext = BlockProposalSigningContext | OtherSigningContext | VoteSigningContext;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Type guard to check if a SigningContext requires HA protection.
|
|
113
|
+
* Returns true for contexts that need HA protection, false for AUTH_REQUEST and TXS.
|
|
114
|
+
*/
|
|
115
|
+
export function isHAProtectedContext(context: SigningContext): context is HAProtectedSigningContext {
|
|
116
|
+
return context.dutyType !== DutyType.AUTH_REQUEST && context.dutyType !== DutyType.TXS;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Gets the block number from a signing context.
|
|
121
|
+
* - Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE): returns BlockNumber(0)
|
|
122
|
+
* - Other duties: returns the blockNumber from the context
|
|
123
|
+
*/
|
|
124
|
+
export function getBlockNumberFromSigningContext(context: HAProtectedSigningContext): BlockNumber | CheckpointNumber {
|
|
125
|
+
// Check for duty types that have blockNumber
|
|
126
|
+
if (
|
|
127
|
+
context.dutyType === DutyType.BLOCK_PROPOSAL ||
|
|
128
|
+
context.dutyType === DutyType.CHECKPOINT_PROPOSAL ||
|
|
129
|
+
context.dutyType === DutyType.ATTESTATION ||
|
|
130
|
+
context.dutyType === DutyType.ATTESTATIONS_AND_SIGNERS
|
|
131
|
+
) {
|
|
132
|
+
return context.blockNumber;
|
|
133
|
+
}
|
|
134
|
+
// Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE) don't have blockNumber
|
|
135
|
+
return BlockNumber(0);
|
|
57
136
|
}
|
|
58
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Context required for slashing protection during signing operations.
|
|
140
|
+
* Uses discriminated union to enforce type safety:
|
|
141
|
+
* - BLOCK_PROPOSAL duties MUST have blockIndexWithinCheckpoint >= 0
|
|
142
|
+
* - Other duty types do NOT have blockIndexWithinCheckpoint (internally -1)
|
|
143
|
+
* - Vote duties only need slot (blockNumber is internally 0)
|
|
144
|
+
* - AUTH_REQUEST and TXS duties don't need slot/blockNumber (no HA protection needed)
|
|
145
|
+
*/
|
|
146
|
+
export type SigningContext = HAProtectedSigningContext | NoHAProtectionSigningContext;
|
|
147
|
+
|
|
59
148
|
/**
|
|
60
149
|
* Database interface for slashing protection operations
|
|
61
150
|
* This abstraction allows for different database implementations (PostgreSQL, SQLite, etc.)
|
|
@@ -82,10 +171,11 @@ export interface SlashingProtectionDatabase {
|
|
|
82
171
|
*/
|
|
83
172
|
updateDutySigned(
|
|
84
173
|
validatorAddress: EthAddress,
|
|
85
|
-
slot:
|
|
174
|
+
slot: SlotNumber,
|
|
86
175
|
dutyType: DutyType,
|
|
87
176
|
signature: string,
|
|
88
177
|
lockToken: string,
|
|
178
|
+
blockIndexWithinCheckpoint: number,
|
|
89
179
|
): Promise<boolean>;
|
|
90
180
|
|
|
91
181
|
/**
|
|
@@ -95,11 +185,23 @@ export interface SlashingProtectionDatabase {
|
|
|
95
185
|
*
|
|
96
186
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
97
187
|
*/
|
|
98
|
-
deleteDuty(
|
|
188
|
+
deleteDuty(
|
|
189
|
+
validatorAddress: EthAddress,
|
|
190
|
+
slot: SlotNumber,
|
|
191
|
+
dutyType: DutyType,
|
|
192
|
+
lockToken: string,
|
|
193
|
+
blockIndexWithinCheckpoint: number,
|
|
194
|
+
): Promise<boolean>;
|
|
99
195
|
|
|
100
196
|
/**
|
|
101
197
|
* Cleanup own stuck duties
|
|
102
198
|
* @returns the number of duties cleaned up
|
|
103
199
|
*/
|
|
104
200
|
cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number>;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Close the database connection.
|
|
204
|
+
* Should be called during graceful shutdown.
|
|
205
|
+
*/
|
|
206
|
+
close(): Promise<void>;
|
|
105
207
|
}
|
|
@@ -10,9 +10,14 @@ import type { 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 {
|
|
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
|
|
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,15 @@ 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
|
|
43
|
+
private readonly slashingProtection: SlashingProtectionService;
|
|
39
44
|
|
|
40
45
|
constructor(
|
|
41
46
|
db: SlashingProtectionDatabase,
|
|
42
|
-
private readonly config:
|
|
47
|
+
private readonly config: ValidatorHASignerConfig,
|
|
43
48
|
) {
|
|
44
49
|
this.log = createLogger('validator-ha-signer');
|
|
45
50
|
|
|
46
|
-
if (!config.
|
|
51
|
+
if (!config.haSigningEnabled) {
|
|
47
52
|
// this shouldn't happen, the validator should use different signer for non-HA setups
|
|
48
53
|
throw new Error('Validator HA Signer is not enabled in config');
|
|
49
54
|
}
|
|
@@ -67,7 +72,7 @@ export class ValidatorHASigner {
|
|
|
67
72
|
*
|
|
68
73
|
* @param validatorAddress - The validator's Ethereum address
|
|
69
74
|
* @param messageHash - The hash to be signed
|
|
70
|
-
* @param context - The signing context (
|
|
75
|
+
* @param context - The signing context (HA-protected duty types only)
|
|
71
76
|
* @param signFn - Function that performs the actual signing
|
|
72
77
|
* @returns The signature
|
|
73
78
|
*
|
|
@@ -77,29 +82,30 @@ export class ValidatorHASigner {
|
|
|
77
82
|
async signWithProtection(
|
|
78
83
|
validatorAddress: EthAddress,
|
|
79
84
|
messageHash: Buffer32,
|
|
80
|
-
context:
|
|
85
|
+
context: HAProtectedSigningContext,
|
|
81
86
|
signFn: (messageHash: Buffer32) => Promise<Signature>,
|
|
82
87
|
): Promise<Signature> {
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
validatorAddress
|
|
87
|
-
|
|
88
|
+
let dutyIdentifier: DutyIdentifier;
|
|
89
|
+
if (context.dutyType === DutyType.BLOCK_PROPOSAL) {
|
|
90
|
+
dutyIdentifier = {
|
|
91
|
+
validatorAddress,
|
|
92
|
+
slot: context.slot,
|
|
93
|
+
blockIndexWithinCheckpoint: context.blockIndexWithinCheckpoint,
|
|
88
94
|
dutyType: context.dutyType,
|
|
95
|
+
};
|
|
96
|
+
} else {
|
|
97
|
+
dutyIdentifier = {
|
|
98
|
+
validatorAddress,
|
|
89
99
|
slot: context.slot,
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
return await signFn(messageHash);
|
|
100
|
+
dutyType: context.dutyType,
|
|
101
|
+
};
|
|
93
102
|
}
|
|
94
103
|
|
|
95
|
-
const { slot, blockNumber, dutyType } = context;
|
|
96
|
-
|
|
97
104
|
// Acquire lock and get the token for ownership verification
|
|
105
|
+
const blockNumber = getBlockNumberFromSigningContext(context);
|
|
98
106
|
const lockToken = await this.slashingProtection.checkAndRecord({
|
|
99
|
-
|
|
100
|
-
slot,
|
|
107
|
+
...dutyIdentifier,
|
|
101
108
|
blockNumber,
|
|
102
|
-
dutyType,
|
|
103
109
|
messageHash: messageHash.toString(),
|
|
104
110
|
nodeId: this.config.nodeId,
|
|
105
111
|
});
|
|
@@ -110,20 +116,13 @@ export class ValidatorHASigner {
|
|
|
110
116
|
signature = await signFn(messageHash);
|
|
111
117
|
} catch (error: any) {
|
|
112
118
|
// 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
|
-
});
|
|
119
|
+
await this.slashingProtection.deleteDuty({ ...dutyIdentifier, lockToken });
|
|
119
120
|
throw error;
|
|
120
121
|
}
|
|
121
122
|
|
|
122
123
|
// Record success (only succeeds if we own the lock)
|
|
123
124
|
await this.slashingProtection.recordSuccess({
|
|
124
|
-
|
|
125
|
-
slot,
|
|
126
|
-
dutyType,
|
|
125
|
+
...dutyIdentifier,
|
|
127
126
|
signature,
|
|
128
127
|
nodeId: this.config.nodeId,
|
|
129
128
|
lockToken,
|
|
@@ -132,13 +131,6 @@ export class ValidatorHASigner {
|
|
|
132
131
|
return signature;
|
|
133
132
|
}
|
|
134
133
|
|
|
135
|
-
/**
|
|
136
|
-
* Check if slashing protection is enabled
|
|
137
|
-
*/
|
|
138
|
-
get isEnabled(): boolean {
|
|
139
|
-
return this.slashingProtection !== undefined;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
134
|
/**
|
|
143
135
|
* Get the node ID for this signer
|
|
144
136
|
*/
|
|
@@ -151,14 +143,15 @@ export class ValidatorHASigner {
|
|
|
151
143
|
* Should be called after construction and before signing operations.
|
|
152
144
|
*/
|
|
153
145
|
start() {
|
|
154
|
-
this.slashingProtection
|
|
146
|
+
this.slashingProtection.start();
|
|
155
147
|
}
|
|
156
148
|
|
|
157
149
|
/**
|
|
158
|
-
* Stop the HA signer background tasks.
|
|
150
|
+
* Stop the HA signer background tasks and close database connection.
|
|
159
151
|
* Should be called during graceful shutdown.
|
|
160
152
|
*/
|
|
161
153
|
async stop() {
|
|
162
|
-
await this.slashingProtection
|
|
154
|
+
await this.slashingProtection.stop();
|
|
155
|
+
await this.slashingProtection.close();
|
|
163
156
|
}
|
|
164
157
|
}
|