@aztec/validator-ha-signer 0.0.1-commit.96bb3f7 → 0.0.1-commit.993d52e
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 +52 -37
- package/dest/config.d.ts +71 -17
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +47 -19
- package/dest/db/postgres.d.ts +34 -5
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +80 -22
- package/dest/db/schema.d.ts +21 -10
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +49 -20
- package/dest/db/types.d.ts +81 -24
- 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 +16 -6
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +58 -17
- 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 +91 -14
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +21 -1
- package/dest/validator_ha_signer.d.ts +10 -13
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +32 -31
- package/package.json +9 -8
- package/src/config.ts +83 -50
- package/src/db/postgres.ts +101 -21
- package/src/db/schema.ts +51 -20
- package/src/db/types.ts +111 -22
- package/src/errors.ts +7 -2
- package/src/factory.ts +8 -13
- package/src/migrations.ts +17 -1
- package/src/slashing_protection_service.ts +94 -23
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +140 -19
- package/src/validator_ha_signer.ts +39 -41
package/src/types.ts
CHANGED
|
@@ -1,27 +1,38 @@
|
|
|
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,
|
|
17
|
+
type DutyRow,
|
|
10
18
|
DutyType,
|
|
11
|
-
|
|
12
|
-
|
|
19
|
+
type OtherDutyIdentifier,
|
|
20
|
+
type RecordSuccessParams,
|
|
21
|
+
type ValidatorDutyRecord,
|
|
13
22
|
} from './db/types.js';
|
|
14
23
|
|
|
15
24
|
export type {
|
|
25
|
+
BlockProposalDutyIdentifier,
|
|
16
26
|
CheckAndRecordParams,
|
|
17
|
-
CreateHASignerConfig,
|
|
18
27
|
DeleteDutyParams,
|
|
19
28
|
DutyIdentifier,
|
|
29
|
+
DutyRow,
|
|
30
|
+
OtherDutyIdentifier,
|
|
20
31
|
RecordSuccessParams,
|
|
21
|
-
SlashingProtectionConfig,
|
|
22
32
|
ValidatorDutyRecord,
|
|
33
|
+
ValidatorHASignerConfig,
|
|
23
34
|
};
|
|
24
|
-
export { DutyStatus, DutyType } from './db/types.js';
|
|
35
|
+
export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
|
|
25
36
|
|
|
26
37
|
/**
|
|
27
38
|
* Result of tryInsertOrGetExisting operation
|
|
@@ -45,17 +56,97 @@ export interface CreateHASignerDeps {
|
|
|
45
56
|
}
|
|
46
57
|
|
|
47
58
|
/**
|
|
48
|
-
*
|
|
59
|
+
* Base context for signing operations
|
|
49
60
|
*/
|
|
50
|
-
|
|
61
|
+
interface BaseSigningContext {
|
|
51
62
|
/** Slot number for this duty */
|
|
52
|
-
slot:
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
63
|
+
slot: SlotNumber;
|
|
64
|
+
/**
|
|
65
|
+
* Block or checkpoint number for this duty.
|
|
66
|
+
* For block proposals, this is the block number.
|
|
67
|
+
* For checkpoint proposals, this is the checkpoint number.
|
|
68
|
+
*/
|
|
69
|
+
blockNumber: BlockNumber | CheckpointNumber;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Signing context for block proposals.
|
|
74
|
+
* blockIndexWithinCheckpoint is REQUIRED and must be >= 0.
|
|
75
|
+
*/
|
|
76
|
+
export interface BlockProposalSigningContext extends BaseSigningContext {
|
|
77
|
+
/** Block index within checkpoint (0, 1, 2...). Required for block proposals. */
|
|
78
|
+
blockIndexWithinCheckpoint: IndexWithinCheckpoint;
|
|
79
|
+
dutyType: DutyType.BLOCK_PROPOSAL;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Signing context for non-block-proposal duties that require HA protection.
|
|
84
|
+
* blockIndexWithinCheckpoint is not applicable (internally always -1).
|
|
85
|
+
*/
|
|
86
|
+
export interface OtherSigningContext extends BaseSigningContext {
|
|
87
|
+
dutyType: DutyType.CHECKPOINT_PROPOSAL | DutyType.ATTESTATION | DutyType.ATTESTATIONS_AND_SIGNERS;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Signing context for governance/slashing votes which only need slot for HA protection.
|
|
92
|
+
* blockNumber is not applicable (internally always 0).
|
|
93
|
+
*/
|
|
94
|
+
export interface VoteSigningContext {
|
|
95
|
+
slot: SlotNumber;
|
|
96
|
+
dutyType: DutyType.GOVERNANCE_VOTE | DutyType.SLASHING_VOTE;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Signing context for duties which don't require slot/blockNumber
|
|
101
|
+
* as they don't need HA protection (AUTH_REQUEST, TXS).
|
|
102
|
+
*/
|
|
103
|
+
export interface NoHAProtectionSigningContext {
|
|
104
|
+
dutyType: DutyType.AUTH_REQUEST | DutyType.TXS;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Signing contexts that require HA protection (excludes AUTH_REQUEST).
|
|
109
|
+
* Used by the HA signer's signWithProtection method.
|
|
110
|
+
*/
|
|
111
|
+
export type HAProtectedSigningContext = BlockProposalSigningContext | OtherSigningContext | VoteSigningContext;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Type guard to check if a SigningContext requires HA protection.
|
|
115
|
+
* Returns true for contexts that need HA protection, false for AUTH_REQUEST and TXS.
|
|
116
|
+
*/
|
|
117
|
+
export function isHAProtectedContext(context: SigningContext): context is HAProtectedSigningContext {
|
|
118
|
+
return context.dutyType !== DutyType.AUTH_REQUEST && context.dutyType !== DutyType.TXS;
|
|
57
119
|
}
|
|
58
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Gets the block number from a signing context.
|
|
123
|
+
* - Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE): returns BlockNumber(0)
|
|
124
|
+
* - Other duties: returns the blockNumber from the context
|
|
125
|
+
*/
|
|
126
|
+
export function getBlockNumberFromSigningContext(context: HAProtectedSigningContext): BlockNumber | CheckpointNumber {
|
|
127
|
+
// Check for duty types that have blockNumber
|
|
128
|
+
if (
|
|
129
|
+
context.dutyType === DutyType.BLOCK_PROPOSAL ||
|
|
130
|
+
context.dutyType === DutyType.CHECKPOINT_PROPOSAL ||
|
|
131
|
+
context.dutyType === DutyType.ATTESTATION ||
|
|
132
|
+
context.dutyType === DutyType.ATTESTATIONS_AND_SIGNERS
|
|
133
|
+
) {
|
|
134
|
+
return context.blockNumber;
|
|
135
|
+
}
|
|
136
|
+
// Vote duties (GOVERNANCE_VOTE, SLASHING_VOTE) don't have blockNumber
|
|
137
|
+
return BlockNumber(0);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Context required for slashing protection during signing operations.
|
|
142
|
+
* Uses discriminated union to enforce type safety:
|
|
143
|
+
* - BLOCK_PROPOSAL duties MUST have blockIndexWithinCheckpoint >= 0
|
|
144
|
+
* - Other duty types do NOT have blockIndexWithinCheckpoint (internally -1)
|
|
145
|
+
* - Vote duties only need slot (blockNumber is internally 0)
|
|
146
|
+
* - AUTH_REQUEST and TXS duties don't need slot/blockNumber (no HA protection needed)
|
|
147
|
+
*/
|
|
148
|
+
export type SigningContext = HAProtectedSigningContext | NoHAProtectionSigningContext;
|
|
149
|
+
|
|
59
150
|
/**
|
|
60
151
|
* Database interface for slashing protection operations
|
|
61
152
|
* This abstraction allows for different database implementations (PostgreSQL, SQLite, etc.)
|
|
@@ -81,11 +172,13 @@ export interface SlashingProtectionDatabase {
|
|
|
81
172
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
82
173
|
*/
|
|
83
174
|
updateDutySigned(
|
|
175
|
+
rollupAddress: EthAddress,
|
|
84
176
|
validatorAddress: EthAddress,
|
|
85
|
-
slot:
|
|
177
|
+
slot: SlotNumber,
|
|
86
178
|
dutyType: DutyType,
|
|
87
179
|
signature: string,
|
|
88
180
|
lockToken: string,
|
|
181
|
+
blockIndexWithinCheckpoint: number,
|
|
89
182
|
): Promise<boolean>;
|
|
90
183
|
|
|
91
184
|
/**
|
|
@@ -95,11 +188,39 @@ export interface SlashingProtectionDatabase {
|
|
|
95
188
|
*
|
|
96
189
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
97
190
|
*/
|
|
98
|
-
deleteDuty(
|
|
191
|
+
deleteDuty(
|
|
192
|
+
rollupAddress: EthAddress,
|
|
193
|
+
validatorAddress: EthAddress,
|
|
194
|
+
slot: SlotNumber,
|
|
195
|
+
dutyType: DutyType,
|
|
196
|
+
lockToken: string,
|
|
197
|
+
blockIndexWithinCheckpoint: number,
|
|
198
|
+
): Promise<boolean>;
|
|
99
199
|
|
|
100
200
|
/**
|
|
101
201
|
* Cleanup own stuck duties
|
|
102
202
|
* @returns the number of duties cleaned up
|
|
103
203
|
*/
|
|
104
204
|
cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number>;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Cleanup duties with outdated rollup address.
|
|
208
|
+
* Removes all duties where the rollup address doesn't match the current one.
|
|
209
|
+
* Used after a rollup upgrade to clean up duties for the old rollup.
|
|
210
|
+
* @returns the number of duties cleaned up
|
|
211
|
+
*/
|
|
212
|
+
cleanupOutdatedRollupDuties(currentRollupAddress: EthAddress): Promise<number>;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Cleanup old signed duties.
|
|
216
|
+
* Removes only signed duties older than the specified age.
|
|
217
|
+
* @returns the number of duties cleaned up
|
|
218
|
+
*/
|
|
219
|
+
cleanupOldDuties(maxAgeMs: number): Promise<number>;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Close the database connection.
|
|
223
|
+
* Should be called during graceful shutdown.
|
|
224
|
+
*/
|
|
225
|
+
close(): Promise<void>;
|
|
105
226
|
}
|
|
@@ -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
|
|
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 {
|
|
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,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
|
|
43
|
+
private readonly slashingProtection: SlashingProtectionService;
|
|
44
|
+
private readonly rollupAddress: EthAddress;
|
|
39
45
|
|
|
40
46
|
constructor(
|
|
41
47
|
db: SlashingProtectionDatabase,
|
|
42
|
-
private readonly config:
|
|
48
|
+
private readonly config: ValidatorHASignerConfig,
|
|
43
49
|
) {
|
|
44
50
|
this.log = createLogger('validator-ha-signer');
|
|
45
51
|
|
|
46
|
-
if (!config.
|
|
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 (
|
|
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:
|
|
88
|
+
context: HAProtectedSigningContext,
|
|
81
89
|
signFn: (messageHash: Buffer32) => Promise<Signature>,
|
|
82
90
|
): Promise<Signature> {
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
159
|
+
await this.slashingProtection.stop();
|
|
160
|
+
await this.slashingProtection.close();
|
|
163
161
|
}
|
|
164
162
|
}
|