@aztec/validator-ha-signer 0.0.1-commit.c2595eba → 0.0.1-commit.c2eed6949
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 +10 -2
- package/dest/db/index.d.ts +2 -1
- package/dest/db/index.d.ts.map +1 -1
- package/dest/db/index.js +1 -0
- package/dest/db/lmdb.d.ts +66 -0
- package/dest/db/lmdb.d.ts.map +1 -0
- package/dest/db/lmdb.js +188 -0
- package/dest/db/postgres.d.ts +20 -4
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +44 -17
- package/dest/db/schema.d.ts +17 -10
- package/dest/db/schema.d.ts.map +1 -1
- package/dest/db/schema.js +39 -22
- package/dest/db/types.d.ts +43 -19
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +30 -15
- package/dest/factory.d.ts +39 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +75 -5
- package/dest/metrics.d.ts +51 -0
- package/dest/metrics.d.ts.map +1 -0
- package/dest/metrics.js +103 -0
- package/dest/slashing_protection_service.d.ts +19 -6
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +57 -17
- package/dest/types.d.ts +32 -72
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +3 -20
- package/dest/validator_ha_signer.d.ts +15 -6
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +24 -11
- package/package.json +10 -5
- package/src/db/index.ts +1 -0
- package/src/db/lmdb.ts +264 -0
- package/src/db/postgres.ts +45 -12
- package/src/db/schema.ts +41 -22
- package/src/db/types.ts +67 -17
- package/src/factory.ts +93 -4
- package/src/metrics.ts +138 -0
- package/src/slashing_protection_service.ts +79 -21
- package/src/types.ts +50 -103
- package/src/validator_ha_signer.ts +41 -15
- package/dest/config.d.ts +0 -79
- package/dest/config.d.ts.map +0 -1
- package/dest/config.js +0 -73
- package/src/config.ts +0 -125
|
@@ -7,6 +7,8 @@
|
|
|
7
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
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
11
|
+
import type { BaseSignerConfig } from '@aztec/stdlib/ha-signing';
|
|
10
12
|
|
|
11
13
|
import {
|
|
12
14
|
type CheckAndRecordParams,
|
|
@@ -16,7 +18,13 @@ import {
|
|
|
16
18
|
getBlockIndexFromDutyIdentifier,
|
|
17
19
|
} from './db/types.js';
|
|
18
20
|
import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
19
|
-
import type {
|
|
21
|
+
import type { HASignerMetrics } from './metrics.js';
|
|
22
|
+
import type { SlashingProtectionDatabase } from './types.js';
|
|
23
|
+
|
|
24
|
+
export interface SlashingProtectionServiceDeps {
|
|
25
|
+
metrics: HASignerMetrics;
|
|
26
|
+
dateProvider: DateProvider;
|
|
27
|
+
}
|
|
20
28
|
|
|
21
29
|
/**
|
|
22
30
|
* Slashing Protection Service
|
|
@@ -39,11 +47,16 @@ export class SlashingProtectionService {
|
|
|
39
47
|
private readonly signingTimeoutMs: number;
|
|
40
48
|
private readonly maxStuckDutiesAgeMs: number;
|
|
41
49
|
|
|
50
|
+
private readonly metrics: HASignerMetrics;
|
|
51
|
+
private readonly dateProvider: DateProvider;
|
|
52
|
+
|
|
42
53
|
private cleanupRunningPromise: RunningPromise;
|
|
54
|
+
private lastOldDutiesCleanupAtMs?: number;
|
|
43
55
|
|
|
44
56
|
constructor(
|
|
45
57
|
private readonly db: SlashingProtectionDatabase,
|
|
46
|
-
private readonly config:
|
|
58
|
+
private readonly config: BaseSignerConfig,
|
|
59
|
+
deps: SlashingProtectionServiceDeps,
|
|
47
60
|
) {
|
|
48
61
|
this.log = createLogger('slashing-protection');
|
|
49
62
|
this.pollingIntervalMs = config.pollingIntervalMs;
|
|
@@ -51,11 +64,9 @@ export class SlashingProtectionService {
|
|
|
51
64
|
// Default to 144s (2x 72s Aztec slot duration) if not explicitly configured
|
|
52
65
|
this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? 144_000;
|
|
53
66
|
|
|
54
|
-
this.cleanupRunningPromise = new RunningPromise(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.maxStuckDutiesAgeMs,
|
|
58
|
-
);
|
|
67
|
+
this.cleanupRunningPromise = new RunningPromise(this.cleanup.bind(this), this.log, this.maxStuckDutiesAgeMs);
|
|
68
|
+
this.metrics = deps.metrics;
|
|
69
|
+
this.dateProvider = deps.dateProvider;
|
|
59
70
|
}
|
|
60
71
|
|
|
61
72
|
/**
|
|
@@ -67,7 +78,6 @@ export class SlashingProtectionService {
|
|
|
67
78
|
* 2. If insert succeeds, we acquired the lock - return the lockToken
|
|
68
79
|
* 3. If a record exists, handle based on status:
|
|
69
80
|
* - SIGNED: Throw appropriate error (already signed or slashing protection)
|
|
70
|
-
* - FAILED: Delete the failed record
|
|
71
81
|
* - SIGNING: Wait and poll until status changes, then handle result
|
|
72
82
|
*
|
|
73
83
|
* @returns The lockToken that must be used for recordSuccess/deleteDuty
|
|
@@ -76,7 +86,7 @@ export class SlashingProtectionService {
|
|
|
76
86
|
*/
|
|
77
87
|
async checkAndRecord(params: CheckAndRecordParams): Promise<string> {
|
|
78
88
|
const { validatorAddress, slot, dutyType, messageHash, nodeId } = params;
|
|
79
|
-
const startTime =
|
|
89
|
+
const startTime = this.dateProvider.now();
|
|
80
90
|
|
|
81
91
|
this.log.debug(`Checking duty: ${dutyType} for slot ${slot}`, {
|
|
82
92
|
validatorAddress: validatorAddress.toString(),
|
|
@@ -89,10 +99,11 @@ export class SlashingProtectionService {
|
|
|
89
99
|
|
|
90
100
|
if (isNew) {
|
|
91
101
|
// We successfully acquired the lock
|
|
92
|
-
this.log.
|
|
102
|
+
this.log.verbose(`Acquired lock for duty ${dutyType} at slot ${slot}`, {
|
|
93
103
|
validatorAddress: validatorAddress.toString(),
|
|
94
104
|
nodeId,
|
|
95
105
|
});
|
|
106
|
+
this.metrics.recordLockAcquire(true);
|
|
96
107
|
return record.lockToken;
|
|
97
108
|
}
|
|
98
109
|
|
|
@@ -107,6 +118,7 @@ export class SlashingProtectionService {
|
|
|
107
118
|
existingNodeId: record.nodeId,
|
|
108
119
|
attemptingNodeId: nodeId,
|
|
109
120
|
});
|
|
121
|
+
this.metrics.recordSlashingProtection(dutyType);
|
|
110
122
|
throw new SlashingProtectionError(
|
|
111
123
|
slot,
|
|
112
124
|
dutyType,
|
|
@@ -116,15 +128,17 @@ export class SlashingProtectionService {
|
|
|
116
128
|
record.nodeId,
|
|
117
129
|
);
|
|
118
130
|
}
|
|
131
|
+
this.metrics.recordDutyAlreadySigned(dutyType);
|
|
119
132
|
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, record.nodeId);
|
|
120
133
|
} else if (record.status === DutyStatus.SIGNING) {
|
|
121
134
|
// Another node is currently signing - check for timeout
|
|
122
|
-
if (
|
|
135
|
+
if (this.dateProvider.now() - startTime > this.signingTimeoutMs) {
|
|
123
136
|
this.log.warn(`Timeout waiting for signing to complete for duty ${dutyType} at slot ${slot}`, {
|
|
124
137
|
validatorAddress: validatorAddress.toString(),
|
|
125
138
|
timeoutMs: this.signingTimeoutMs,
|
|
126
139
|
signingNodeId: record.nodeId,
|
|
127
140
|
});
|
|
141
|
+
this.metrics.recordDutyAlreadySigned(dutyType);
|
|
128
142
|
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, 'unknown (timeout)');
|
|
129
143
|
}
|
|
130
144
|
|
|
@@ -149,10 +163,11 @@ export class SlashingProtectionService {
|
|
|
149
163
|
* @returns true if the update succeeded, false if token didn't match
|
|
150
164
|
*/
|
|
151
165
|
async recordSuccess(params: RecordSuccessParams): Promise<boolean> {
|
|
152
|
-
const { validatorAddress, slot, dutyType, signature, nodeId, lockToken } = params;
|
|
166
|
+
const { rollupAddress, validatorAddress, slot, dutyType, signature, nodeId, lockToken } = params;
|
|
153
167
|
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
154
168
|
|
|
155
169
|
const success = await this.db.updateDutySigned(
|
|
170
|
+
rollupAddress,
|
|
156
171
|
validatorAddress,
|
|
157
172
|
slot,
|
|
158
173
|
dutyType,
|
|
@@ -162,7 +177,7 @@ export class SlashingProtectionService {
|
|
|
162
177
|
);
|
|
163
178
|
|
|
164
179
|
if (success) {
|
|
165
|
-
this.log.
|
|
180
|
+
this.log.verbose(`Recorded successful signing for duty ${dutyType} at slot ${slot}`, {
|
|
166
181
|
validatorAddress: validatorAddress.toString(),
|
|
167
182
|
nodeId,
|
|
168
183
|
});
|
|
@@ -184,10 +199,17 @@ export class SlashingProtectionService {
|
|
|
184
199
|
* @returns true if the delete succeeded, false if token didn't match
|
|
185
200
|
*/
|
|
186
201
|
async deleteDuty(params: DeleteDutyParams): Promise<boolean> {
|
|
187
|
-
const { validatorAddress, slot, dutyType, lockToken } = params;
|
|
202
|
+
const { rollupAddress, validatorAddress, slot, dutyType, lockToken } = params;
|
|
188
203
|
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
189
204
|
|
|
190
|
-
const success = await this.db.deleteDuty(
|
|
205
|
+
const success = await this.db.deleteDuty(
|
|
206
|
+
rollupAddress,
|
|
207
|
+
validatorAddress,
|
|
208
|
+
slot,
|
|
209
|
+
dutyType,
|
|
210
|
+
lockToken,
|
|
211
|
+
blockIndexWithinCheckpoint,
|
|
212
|
+
);
|
|
191
213
|
|
|
192
214
|
if (success) {
|
|
193
215
|
this.log.info(`Deleted duty ${dutyType} at slot ${slot} to allow retry`, {
|
|
@@ -213,7 +235,20 @@ export class SlashingProtectionService {
|
|
|
213
235
|
* Start running tasks.
|
|
214
236
|
* Cleanup runs immediately on start to recover from any previous crashes.
|
|
215
237
|
*/
|
|
216
|
-
|
|
238
|
+
/**
|
|
239
|
+
* Start the background cleanup task.
|
|
240
|
+
* Also performs one-time cleanup of duties with outdated rollup addresses.
|
|
241
|
+
*/
|
|
242
|
+
async start() {
|
|
243
|
+
// One-time cleanup at startup: remove duties from previous rollup versions
|
|
244
|
+
const numOutdatedRollupDuties = await this.db.cleanupOutdatedRollupDuties(this.config.l1Contracts.rollupAddress);
|
|
245
|
+
if (numOutdatedRollupDuties > 0) {
|
|
246
|
+
this.log.info(`Cleaned up ${numOutdatedRollupDuties} duties with outdated rollup address at startup`, {
|
|
247
|
+
currentRollupAddress: this.config.l1Contracts.rollupAddress.toString(),
|
|
248
|
+
});
|
|
249
|
+
this.metrics.recordCleanup('outdated_rollup', numOutdatedRollupDuties);
|
|
250
|
+
}
|
|
251
|
+
|
|
217
252
|
this.cleanupRunningPromise.start();
|
|
218
253
|
this.log.info('Slashing protection service started', { nodeId: this.config.nodeId });
|
|
219
254
|
}
|
|
@@ -236,15 +271,38 @@ export class SlashingProtectionService {
|
|
|
236
271
|
}
|
|
237
272
|
|
|
238
273
|
/**
|
|
239
|
-
*
|
|
274
|
+
* Periodic cleanup of stuck duties and optionally old signed duties.
|
|
275
|
+
* Runs in the background via RunningPromise.
|
|
240
276
|
*/
|
|
241
|
-
private async
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
277
|
+
private async cleanup() {
|
|
278
|
+
// 1. Clean up stuck duties (our own node's duties that got stuck in 'signing' status)
|
|
279
|
+
const numStuckDuties = await this.db.cleanupOwnStuckDuties(this.config.nodeId, this.maxStuckDutiesAgeMs);
|
|
280
|
+
if (numStuckDuties > 0) {
|
|
281
|
+
this.log.verbose(`Cleaned up ${numStuckDuties} stuck duties`, {
|
|
245
282
|
nodeId: this.config.nodeId,
|
|
246
283
|
maxStuckDutiesAgeMs: this.maxStuckDutiesAgeMs,
|
|
247
284
|
});
|
|
285
|
+
this.metrics.recordCleanup('stuck', numStuckDuties);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// 2. Clean up old signed duties if configured
|
|
289
|
+
// we shouldn't run this as often as stuck duty cleanup.
|
|
290
|
+
if (this.config.cleanupOldDutiesAfterHours !== undefined) {
|
|
291
|
+
const maxAgeMs = this.config.cleanupOldDutiesAfterHours * 60 * 60 * 1000;
|
|
292
|
+
const nowMs = this.dateProvider.now();
|
|
293
|
+
const shouldRun =
|
|
294
|
+
this.lastOldDutiesCleanupAtMs === undefined || nowMs - this.lastOldDutiesCleanupAtMs >= maxAgeMs;
|
|
295
|
+
if (shouldRun) {
|
|
296
|
+
const numOldDuties = await this.db.cleanupOldDuties(maxAgeMs);
|
|
297
|
+
this.lastOldDutiesCleanupAtMs = nowMs;
|
|
298
|
+
if (numOldDuties > 0) {
|
|
299
|
+
this.log.verbose(`Cleaned up ${numOldDuties} old signed duties`, {
|
|
300
|
+
cleanupOldDutiesAfterHours: this.config.cleanupOldDutiesAfterHours,
|
|
301
|
+
maxAgeMs,
|
|
302
|
+
});
|
|
303
|
+
this.metrics.recordCleanup('old', numOldDuties);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
248
306
|
}
|
|
249
307
|
}
|
|
250
308
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BlockNumber,
|
|
3
|
-
type CheckpointNumber,
|
|
4
|
-
type IndexWithinCheckpoint,
|
|
5
|
-
type SlotNumber,
|
|
6
|
-
} from '@aztec/foundation/branded-types';
|
|
1
|
+
import { SlotNumber } from '@aztec/foundation/branded-types';
|
|
7
2
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
|
+
import { DateProvider } from '@aztec/foundation/timer';
|
|
4
|
+
import {
|
|
5
|
+
DutyType,
|
|
6
|
+
type HAProtectedSigningContext,
|
|
7
|
+
type SigningContext,
|
|
8
|
+
type ValidatorHASignerConfig,
|
|
9
|
+
getBlockNumberFromSigningContext as getBlockNumberFromSigningContextFromStdlib,
|
|
10
|
+
isHAProtectedContext,
|
|
11
|
+
} from '@aztec/stdlib/ha-signing';
|
|
12
|
+
import type { TelemetryClient } from '@aztec/telemetry-client';
|
|
8
13
|
|
|
9
14
|
import type { Pool } from 'pg';
|
|
10
15
|
|
|
11
|
-
import type {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
type ValidatorDutyRecord,
|
|
16
|
+
import type {
|
|
17
|
+
BlockProposalDutyIdentifier,
|
|
18
|
+
CheckAndRecordParams,
|
|
19
|
+
DeleteDutyParams,
|
|
20
|
+
DutyIdentifier,
|
|
21
|
+
DutyRow,
|
|
22
|
+
OtherDutyIdentifier,
|
|
23
|
+
RecordSuccessParams,
|
|
24
|
+
ValidatorDutyRecord,
|
|
21
25
|
} from './db/types.js';
|
|
22
26
|
|
|
23
27
|
export type {
|
|
@@ -25,12 +29,17 @@ export type {
|
|
|
25
29
|
CheckAndRecordParams,
|
|
26
30
|
DeleteDutyParams,
|
|
27
31
|
DutyIdentifier,
|
|
32
|
+
DutyRow,
|
|
33
|
+
HAProtectedSigningContext,
|
|
28
34
|
OtherDutyIdentifier,
|
|
29
35
|
RecordSuccessParams,
|
|
36
|
+
SigningContext,
|
|
30
37
|
ValidatorDutyRecord,
|
|
31
38
|
ValidatorHASignerConfig,
|
|
32
39
|
};
|
|
33
40
|
export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
|
|
41
|
+
export { isHAProtectedContext };
|
|
42
|
+
export { getBlockNumberFromSigningContextFromStdlib as getBlockNumberFromSigningContext };
|
|
34
43
|
|
|
35
44
|
/**
|
|
36
45
|
* Result of tryInsertOrGetExisting operation
|
|
@@ -51,99 +60,20 @@ export interface CreateHASignerDeps {
|
|
|
51
60
|
* If provided, databaseUrl and poolConfig are ignored
|
|
52
61
|
*/
|
|
53
62
|
pool?: Pool;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Base context for signing operations
|
|
58
|
-
*/
|
|
59
|
-
interface BaseSigningContext {
|
|
60
|
-
/** Slot number for this duty */
|
|
61
|
-
slot: SlotNumber;
|
|
62
63
|
/**
|
|
63
|
-
*
|
|
64
|
-
* For block proposals, this is the block number.
|
|
65
|
-
* For checkpoint proposals, this is the checkpoint number.
|
|
64
|
+
* Optional telemetry client for metrics
|
|
66
65
|
*/
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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);
|
|
66
|
+
telemetryClient?: TelemetryClient;
|
|
67
|
+
/**
|
|
68
|
+
* Optional date provider for timestamps
|
|
69
|
+
*/
|
|
70
|
+
dateProvider?: DateProvider;
|
|
136
71
|
}
|
|
137
72
|
|
|
138
73
|
/**
|
|
139
|
-
*
|
|
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)
|
|
74
|
+
* deps for creating a local signing protection signer
|
|
145
75
|
*/
|
|
146
|
-
export type
|
|
76
|
+
export type CreateLocalSignerWithProtectionDeps = Omit<CreateHASignerDeps, 'pool'>;
|
|
147
77
|
|
|
148
78
|
/**
|
|
149
79
|
* Database interface for slashing protection operations
|
|
@@ -170,6 +100,7 @@ export interface SlashingProtectionDatabase {
|
|
|
170
100
|
* @returns true if the update succeeded, false if token didn't match or duty not found
|
|
171
101
|
*/
|
|
172
102
|
updateDutySigned(
|
|
103
|
+
rollupAddress: EthAddress,
|
|
173
104
|
validatorAddress: EthAddress,
|
|
174
105
|
slot: SlotNumber,
|
|
175
106
|
dutyType: DutyType,
|
|
@@ -186,6 +117,7 @@ export interface SlashingProtectionDatabase {
|
|
|
186
117
|
* @returns true if the delete succeeded, false if token didn't match or duty not found
|
|
187
118
|
*/
|
|
188
119
|
deleteDuty(
|
|
120
|
+
rollupAddress: EthAddress,
|
|
189
121
|
validatorAddress: EthAddress,
|
|
190
122
|
slot: SlotNumber,
|
|
191
123
|
dutyType: DutyType,
|
|
@@ -199,6 +131,21 @@ export interface SlashingProtectionDatabase {
|
|
|
199
131
|
*/
|
|
200
132
|
cleanupOwnStuckDuties(nodeId: string, maxAgeMs: number): Promise<number>;
|
|
201
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Cleanup duties with outdated rollup address.
|
|
136
|
+
* Removes all duties where the rollup address doesn't match the current one.
|
|
137
|
+
* Used after a rollup upgrade to clean up duties for the old rollup.
|
|
138
|
+
* @returns the number of duties cleaned up
|
|
139
|
+
*/
|
|
140
|
+
cleanupOutdatedRollupDuties(currentRollupAddress: EthAddress): Promise<number>;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Cleanup old signed duties.
|
|
144
|
+
* Removes only signed duties older than the specified age.
|
|
145
|
+
* @returns the number of duties cleaned up
|
|
146
|
+
*/
|
|
147
|
+
cleanupOldDuties(maxAgeMs: number): Promise<number>;
|
|
148
|
+
|
|
202
149
|
/**
|
|
203
150
|
* Close the database connection.
|
|
204
151
|
* Should be called during graceful shutdown.
|
|
@@ -6,18 +6,26 @@
|
|
|
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
|
-
|
|
13
|
-
import type { ValidatorHASignerConfig } from './config.js';
|
|
14
|
-
import { type DutyIdentifier, DutyType } from './db/types.js';
|
|
15
|
-
import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
12
|
+
import type { DateProvider } from '@aztec/foundation/timer';
|
|
16
13
|
import {
|
|
14
|
+
type BaseSignerConfig,
|
|
15
|
+
DutyType,
|
|
17
16
|
type HAProtectedSigningContext,
|
|
18
|
-
type SlashingProtectionDatabase,
|
|
19
17
|
getBlockNumberFromSigningContext,
|
|
20
|
-
} from '
|
|
18
|
+
} from '@aztec/stdlib/ha-signing';
|
|
19
|
+
|
|
20
|
+
import type { DutyIdentifier } from './db/types.js';
|
|
21
|
+
import type { HASignerMetrics } from './metrics.js';
|
|
22
|
+
import { SlashingProtectionService } from './slashing_protection_service.js';
|
|
23
|
+
import type { SlashingProtectionDatabase } from './types.js';
|
|
24
|
+
|
|
25
|
+
export interface ValidatorHASignerDeps {
|
|
26
|
+
metrics: HASignerMetrics;
|
|
27
|
+
dateProvider: DateProvider;
|
|
28
|
+
}
|
|
21
29
|
|
|
22
30
|
/**
|
|
23
31
|
* Validator High Availability Signer
|
|
@@ -41,24 +49,32 @@ import {
|
|
|
41
49
|
export class ValidatorHASigner {
|
|
42
50
|
private readonly log: Logger;
|
|
43
51
|
private readonly slashingProtection: SlashingProtectionService;
|
|
52
|
+
private readonly rollupAddress: EthAddress;
|
|
53
|
+
|
|
54
|
+
private readonly dateProvider: DateProvider;
|
|
55
|
+
private readonly metrics: HASignerMetrics;
|
|
44
56
|
|
|
45
57
|
constructor(
|
|
46
58
|
db: SlashingProtectionDatabase,
|
|
47
|
-
private readonly config:
|
|
59
|
+
private readonly config: BaseSignerConfig,
|
|
60
|
+
deps: ValidatorHASignerDeps,
|
|
48
61
|
) {
|
|
49
62
|
this.log = createLogger('validator-ha-signer');
|
|
50
63
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
throw new Error('Validator HA Signer is not enabled in config');
|
|
54
|
-
}
|
|
64
|
+
this.metrics = deps.metrics;
|
|
65
|
+
this.dateProvider = deps.dateProvider;
|
|
55
66
|
|
|
56
67
|
if (!config.nodeId || config.nodeId === '') {
|
|
57
68
|
throw new Error('NODE_ID is required for high-availability setups');
|
|
58
69
|
}
|
|
59
|
-
this.
|
|
70
|
+
this.rollupAddress = config.l1Contracts.rollupAddress;
|
|
71
|
+
this.slashingProtection = new SlashingProtectionService(db, config, {
|
|
72
|
+
metrics: deps.metrics,
|
|
73
|
+
dateProvider: deps.dateProvider,
|
|
74
|
+
});
|
|
60
75
|
this.log.info('Validator HA Signer initialized with slashing protection', {
|
|
61
76
|
nodeId: config.nodeId,
|
|
77
|
+
rollupAddress: this.rollupAddress.toString(),
|
|
62
78
|
});
|
|
63
79
|
}
|
|
64
80
|
|
|
@@ -85,9 +101,13 @@ export class ValidatorHASigner {
|
|
|
85
101
|
context: HAProtectedSigningContext,
|
|
86
102
|
signFn: (messageHash: Buffer32) => Promise<Signature>,
|
|
87
103
|
): Promise<Signature> {
|
|
104
|
+
const startTime = this.dateProvider.now();
|
|
105
|
+
const dutyType = context.dutyType;
|
|
106
|
+
|
|
88
107
|
let dutyIdentifier: DutyIdentifier;
|
|
89
108
|
if (context.dutyType === DutyType.BLOCK_PROPOSAL) {
|
|
90
109
|
dutyIdentifier = {
|
|
110
|
+
rollupAddress: this.rollupAddress,
|
|
91
111
|
validatorAddress,
|
|
92
112
|
slot: context.slot,
|
|
93
113
|
blockIndexWithinCheckpoint: context.blockIndexWithinCheckpoint,
|
|
@@ -95,6 +115,7 @@ export class ValidatorHASigner {
|
|
|
95
115
|
};
|
|
96
116
|
} else {
|
|
97
117
|
dutyIdentifier = {
|
|
118
|
+
rollupAddress: this.rollupAddress,
|
|
98
119
|
validatorAddress,
|
|
99
120
|
slot: context.slot,
|
|
100
121
|
dutyType: context.dutyType,
|
|
@@ -102,6 +123,7 @@ export class ValidatorHASigner {
|
|
|
102
123
|
}
|
|
103
124
|
|
|
104
125
|
// Acquire lock and get the token for ownership verification
|
|
126
|
+
// DutyAlreadySignedError and SlashingProtectionError may be thrown here and are recorded in the service
|
|
105
127
|
const blockNumber = getBlockNumberFromSigningContext(context);
|
|
106
128
|
const lockToken = await this.slashingProtection.checkAndRecord({
|
|
107
129
|
...dutyIdentifier,
|
|
@@ -117,6 +139,7 @@ export class ValidatorHASigner {
|
|
|
117
139
|
} catch (error: any) {
|
|
118
140
|
// Delete duty to allow retry (only succeeds if we own the lock)
|
|
119
141
|
await this.slashingProtection.deleteDuty({ ...dutyIdentifier, lockToken });
|
|
142
|
+
this.metrics.recordSigningError(dutyType);
|
|
120
143
|
throw error;
|
|
121
144
|
}
|
|
122
145
|
|
|
@@ -128,6 +151,9 @@ export class ValidatorHASigner {
|
|
|
128
151
|
lockToken,
|
|
129
152
|
});
|
|
130
153
|
|
|
154
|
+
const duration = this.dateProvider.now() - startTime;
|
|
155
|
+
this.metrics.recordSigningSuccess(dutyType, duration);
|
|
156
|
+
|
|
131
157
|
return signature;
|
|
132
158
|
}
|
|
133
159
|
|
|
@@ -142,8 +168,8 @@ export class ValidatorHASigner {
|
|
|
142
168
|
* Start the HA signer background tasks (cleanup of stuck duties).
|
|
143
169
|
* Should be called after construction and before signing operations.
|
|
144
170
|
*/
|
|
145
|
-
start() {
|
|
146
|
-
this.slashingProtection.start();
|
|
171
|
+
async start() {
|
|
172
|
+
await this.slashingProtection.start();
|
|
147
173
|
}
|
|
148
174
|
|
|
149
175
|
/**
|
package/dest/config.d.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY29uZmlnLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFDTCxLQUFLLGtCQUFrQixFQU14QixNQUFNLDBCQUEwQixDQUFDO0FBR2xDLE9BQU8sRUFBRSxDQUFDLEVBQUUsTUFBTSxLQUFLLENBQUM7QUFFeEI7Ozs7O0dBS0c7QUFDSCxNQUFNLFdBQVcsdUJBQXVCO0lBQ3RDLDBEQUEwRDtJQUMxRCxnQkFBZ0IsRUFBRSxPQUFPLENBQUM7SUFDMUIsc0NBQXNDO0lBQ3RDLE1BQU0sRUFBRSxNQUFNLENBQUM7SUFDZixzRUFBc0U7SUFDdEUsaUJBQWlCLEVBQUUsTUFBTSxDQUFDO0lBQzFCLG9FQUFvRTtJQUNwRSxnQkFBZ0IsRUFBRSxNQUFNLENBQUM7SUFDekIsa0dBQWtHO0lBQ2xHLG1CQUFtQixDQUFDLEVBQUUsTUFBTSxDQUFDO0lBQzdCOzs7T0FHRztJQUNILFdBQVcsQ0FBQyxFQUFFLE1BQU0sQ0FBQztJQUNyQjs7T0FFRztJQUNILDBEQUEwRDtJQUMxRCxZQUFZLENBQUMsRUFBRSxNQUFNLENBQUM7SUFDdEIseURBQXlEO0lBQ3pELFlBQVksQ0FBQyxFQUFFLE1BQU0sQ0FBQztJQUN0QixvREFBb0Q7SUFDcEQsaUJBQWlCLENBQUMsRUFBRSxNQUFNLENBQUM7SUFDM0Isa0VBQWtFO0lBQ2xFLHVCQUF1QixDQUFDLEVBQUUsTUFBTSxDQUFDO0NBQ2xDO0FBRUQsZUFBTyxNQUFNLCtCQUErQixFQUFFLGtCQUFrQixDQUFDLHVCQUF1QixDQW1EdkYsQ0FBQztBQUVGLGVBQU8sTUFBTSw4QkFBOEIsRUFBRSx1QkFFNUMsQ0FBQztBQUVGOzs7O0dBSUc7QUFDSCx3QkFBZ0IsZ0JBQWdCLElBQUksdUJBQXVCLENBRTFEO0FBRUQsZUFBTyxNQUFNLDZCQUE2Qjs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0VBV0UsQ0FBQyJ9
|
package/dest/config.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,kBAAkB,EAMxB,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;GAKG;AACH,MAAM,WAAW,uBAAuB;IACtC,0DAA0D;IAC1D,gBAAgB,EAAE,OAAO,CAAC;IAC1B,sCAAsC;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,sEAAsE;IACtE,iBAAiB,EAAE,MAAM,CAAC;IAC1B,oEAAoE;IACpE,gBAAgB,EAAE,MAAM,CAAC;IACzB,kGAAkG;IAClG,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,0DAA0D;IAC1D,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,yDAAyD;IACzD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,oDAAoD;IACpD,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,kEAAkE;IAClE,uBAAuB,CAAC,EAAE,MAAM,CAAC;CAClC;AAED,eAAO,MAAM,+BAA+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"}
|