@aztec/validator-ha-signer 0.0.1-commit.cf93bcc56 → 0.0.1-commit.d1cd2107c
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 +0 -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 +4 -2
- package/dest/db/postgres.d.ts.map +1 -1
- package/dest/db/postgres.js +13 -13
- package/dest/db/types.d.ts +37 -18
- package/dest/db/types.d.ts.map +1 -1
- package/dest/db/types.js +30 -15
- package/dest/factory.d.ts +22 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +50 -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 +12 -3
- package/dest/slashing_protection_service.d.ts.map +1 -1
- package/dest/slashing_protection_service.js +15 -4
- package/dest/types.d.ts +17 -70
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +3 -20
- package/dest/validator_ha_signer.d.ts +12 -4
- package/dest/validator_ha_signer.d.ts.map +1 -1
- package/dest/validator_ha_signer.js +16 -8
- package/package.json +10 -6
- package/src/db/index.ts +1 -0
- package/src/db/lmdb.ts +264 -0
- package/src/db/postgres.ts +13 -11
- package/src/db/types.ts +61 -16
- package/src/factory.ts +61 -4
- package/src/metrics.ts +138 -0
- package/src/slashing_protection_service.ts +26 -5
- package/src/types.ts +32 -104
- package/src/validator_ha_signer.ts +33 -12
- package/dest/config.d.ts +0 -101
- package/dest/config.d.ts.map +0 -1
- package/dest/config.js +0 -92
- package/src/config.ts +0 -149
package/src/metrics.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Attributes,
|
|
3
|
+
type Histogram,
|
|
4
|
+
Metrics,
|
|
5
|
+
type TelemetryClient,
|
|
6
|
+
type UpDownCounter,
|
|
7
|
+
createUpDownCounterWithDefault,
|
|
8
|
+
} from '@aztec/telemetry-client';
|
|
9
|
+
|
|
10
|
+
export type HACleanupType = 'stuck' | 'old' | 'outdated_rollup';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Metrics for HA signer tracking signing operations, lock acquisition, and cleanup.
|
|
14
|
+
*/
|
|
15
|
+
export class HASignerMetrics {
|
|
16
|
+
// Signing lifecycle metrics
|
|
17
|
+
private signingDuration: Histogram;
|
|
18
|
+
private signingSuccessCount: UpDownCounter;
|
|
19
|
+
private dutyAlreadySignedCount: UpDownCounter;
|
|
20
|
+
private slashingProtectionCount: UpDownCounter;
|
|
21
|
+
private signingErrorCount: UpDownCounter;
|
|
22
|
+
|
|
23
|
+
// Lock acquisition metrics
|
|
24
|
+
private lockAcquiredCount: UpDownCounter;
|
|
25
|
+
|
|
26
|
+
// Cleanup metrics
|
|
27
|
+
private cleanupStuckDutiesCount: UpDownCounter;
|
|
28
|
+
private cleanupOldDutiesCount: UpDownCounter;
|
|
29
|
+
private cleanupOutdatedRollupDutiesCount: UpDownCounter;
|
|
30
|
+
|
|
31
|
+
constructor(
|
|
32
|
+
client: TelemetryClient,
|
|
33
|
+
private nodeId: string,
|
|
34
|
+
name = 'HASignerMetrics',
|
|
35
|
+
) {
|
|
36
|
+
const meter = client.getMeter(name);
|
|
37
|
+
|
|
38
|
+
// Signing lifecycle
|
|
39
|
+
this.signingDuration = meter.createHistogram(Metrics.HA_SIGNER_SIGNING_DURATION);
|
|
40
|
+
this.signingSuccessCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_SIGNING_SUCCESS_COUNT);
|
|
41
|
+
this.dutyAlreadySignedCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_DUTY_ALREADY_SIGNED_COUNT);
|
|
42
|
+
this.slashingProtectionCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_SLASHING_PROTECTION_COUNT);
|
|
43
|
+
this.signingErrorCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_SIGNING_ERROR_COUNT);
|
|
44
|
+
|
|
45
|
+
// Lock acquisition
|
|
46
|
+
this.lockAcquiredCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_LOCK_ACQUIRED_COUNT);
|
|
47
|
+
|
|
48
|
+
// Cleanup
|
|
49
|
+
this.cleanupStuckDutiesCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_CLEANUP_STUCK_DUTIES_COUNT);
|
|
50
|
+
this.cleanupOldDutiesCount = createUpDownCounterWithDefault(meter, Metrics.HA_SIGNER_CLEANUP_OLD_DUTIES_COUNT);
|
|
51
|
+
this.cleanupOutdatedRollupDutiesCount = createUpDownCounterWithDefault(
|
|
52
|
+
meter,
|
|
53
|
+
Metrics.HA_SIGNER_CLEANUP_OUTDATED_ROLLUP_DUTIES_COUNT,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Record a successful signing operation.
|
|
59
|
+
* @param dutyType - The type of duty signed
|
|
60
|
+
* @param durationMs - Duration from start of signWithProtection to completion
|
|
61
|
+
*/
|
|
62
|
+
public recordSigningSuccess(dutyType: string, durationMs: number): void {
|
|
63
|
+
const attributes = {
|
|
64
|
+
[Attributes.HA_DUTY_TYPE]: dutyType,
|
|
65
|
+
[Attributes.HA_NODE_ID]: this.nodeId,
|
|
66
|
+
};
|
|
67
|
+
this.signingSuccessCount.add(1, attributes);
|
|
68
|
+
this.signingDuration.record(durationMs, attributes);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Record a DutyAlreadySignedError (expected in HA; another node signed first).
|
|
73
|
+
* @param dutyType - The type of duty
|
|
74
|
+
*/
|
|
75
|
+
public recordDutyAlreadySigned(dutyType: string): void {
|
|
76
|
+
const attributes = {
|
|
77
|
+
[Attributes.HA_DUTY_TYPE]: dutyType,
|
|
78
|
+
[Attributes.HA_NODE_ID]: this.nodeId,
|
|
79
|
+
};
|
|
80
|
+
this.dutyAlreadySignedCount.add(1, attributes);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Record a SlashingProtectionError (attempted to sign different data for same duty).
|
|
85
|
+
* @param dutyType - The type of duty
|
|
86
|
+
*/
|
|
87
|
+
public recordSlashingProtection(dutyType: string): void {
|
|
88
|
+
const attributes = {
|
|
89
|
+
[Attributes.HA_DUTY_TYPE]: dutyType,
|
|
90
|
+
[Attributes.HA_NODE_ID]: this.nodeId,
|
|
91
|
+
};
|
|
92
|
+
this.slashingProtectionCount.add(1, attributes);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Record a signing function failure (lock will be deleted for retry).
|
|
97
|
+
* @param dutyType - The type of duty
|
|
98
|
+
*/
|
|
99
|
+
public recordSigningError(dutyType: string): void {
|
|
100
|
+
const attributes = {
|
|
101
|
+
[Attributes.HA_DUTY_TYPE]: dutyType,
|
|
102
|
+
[Attributes.HA_NODE_ID]: this.nodeId,
|
|
103
|
+
};
|
|
104
|
+
this.signingErrorCount.add(1, attributes);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Record lock acquisition.
|
|
109
|
+
* @param acquired - Whether a new lock was acquired (true) or existing record found (false)
|
|
110
|
+
*/
|
|
111
|
+
public recordLockAcquire(acquired: boolean): void {
|
|
112
|
+
if (acquired) {
|
|
113
|
+
const attributes = {
|
|
114
|
+
[Attributes.HA_NODE_ID]: this.nodeId,
|
|
115
|
+
};
|
|
116
|
+
this.lockAcquiredCount.add(1, attributes);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Record cleanup metrics.
|
|
122
|
+
* @param type - Type of cleanup
|
|
123
|
+
* @param count - Number of duties cleaned up
|
|
124
|
+
*/
|
|
125
|
+
public recordCleanup(type: HACleanupType, count: number): void {
|
|
126
|
+
const attributes = {
|
|
127
|
+
[Attributes.HA_NODE_ID]: this.nodeId,
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
if (type === 'stuck') {
|
|
131
|
+
this.cleanupStuckDutiesCount.add(count, attributes);
|
|
132
|
+
} else if (type === 'old') {
|
|
133
|
+
this.cleanupOldDutiesCount.add(count, attributes);
|
|
134
|
+
} else if (type === 'outdated_rollup') {
|
|
135
|
+
this.cleanupOutdatedRollupDutiesCount.add(count, attributes);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -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,12 +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;
|
|
43
54
|
private lastOldDutiesCleanupAtMs?: number;
|
|
44
55
|
|
|
45
56
|
constructor(
|
|
46
57
|
private readonly db: SlashingProtectionDatabase,
|
|
47
|
-
private readonly config:
|
|
58
|
+
private readonly config: BaseSignerConfig,
|
|
59
|
+
deps: SlashingProtectionServiceDeps,
|
|
48
60
|
) {
|
|
49
61
|
this.log = createLogger('slashing-protection');
|
|
50
62
|
this.pollingIntervalMs = config.pollingIntervalMs;
|
|
@@ -53,6 +65,8 @@ export class SlashingProtectionService {
|
|
|
53
65
|
this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? 144_000;
|
|
54
66
|
|
|
55
67
|
this.cleanupRunningPromise = new RunningPromise(this.cleanup.bind(this), this.log, this.maxStuckDutiesAgeMs);
|
|
68
|
+
this.metrics = deps.metrics;
|
|
69
|
+
this.dateProvider = deps.dateProvider;
|
|
56
70
|
}
|
|
57
71
|
|
|
58
72
|
/**
|
|
@@ -72,7 +86,7 @@ export class SlashingProtectionService {
|
|
|
72
86
|
*/
|
|
73
87
|
async checkAndRecord(params: CheckAndRecordParams): Promise<string> {
|
|
74
88
|
const { validatorAddress, slot, dutyType, messageHash, nodeId } = params;
|
|
75
|
-
const startTime =
|
|
89
|
+
const startTime = this.dateProvider.now();
|
|
76
90
|
|
|
77
91
|
this.log.debug(`Checking duty: ${dutyType} for slot ${slot}`, {
|
|
78
92
|
validatorAddress: validatorAddress.toString(),
|
|
@@ -89,6 +103,7 @@ export class SlashingProtectionService {
|
|
|
89
103
|
validatorAddress: validatorAddress.toString(),
|
|
90
104
|
nodeId,
|
|
91
105
|
});
|
|
106
|
+
this.metrics.recordLockAcquire(true);
|
|
92
107
|
return record.lockToken;
|
|
93
108
|
}
|
|
94
109
|
|
|
@@ -103,6 +118,7 @@ export class SlashingProtectionService {
|
|
|
103
118
|
existingNodeId: record.nodeId,
|
|
104
119
|
attemptingNodeId: nodeId,
|
|
105
120
|
});
|
|
121
|
+
this.metrics.recordSlashingProtection(dutyType);
|
|
106
122
|
throw new SlashingProtectionError(
|
|
107
123
|
slot,
|
|
108
124
|
dutyType,
|
|
@@ -112,15 +128,17 @@ export class SlashingProtectionService {
|
|
|
112
128
|
record.nodeId,
|
|
113
129
|
);
|
|
114
130
|
}
|
|
131
|
+
this.metrics.recordDutyAlreadySigned(dutyType);
|
|
115
132
|
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, record.nodeId);
|
|
116
133
|
} else if (record.status === DutyStatus.SIGNING) {
|
|
117
134
|
// Another node is currently signing - check for timeout
|
|
118
|
-
if (
|
|
135
|
+
if (this.dateProvider.now() - startTime > this.signingTimeoutMs) {
|
|
119
136
|
this.log.warn(`Timeout waiting for signing to complete for duty ${dutyType} at slot ${slot}`, {
|
|
120
137
|
validatorAddress: validatorAddress.toString(),
|
|
121
138
|
timeoutMs: this.signingTimeoutMs,
|
|
122
139
|
signingNodeId: record.nodeId,
|
|
123
140
|
});
|
|
141
|
+
this.metrics.recordDutyAlreadySigned(dutyType);
|
|
124
142
|
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, 'unknown (timeout)');
|
|
125
143
|
}
|
|
126
144
|
|
|
@@ -228,6 +246,7 @@ export class SlashingProtectionService {
|
|
|
228
246
|
this.log.info(`Cleaned up ${numOutdatedRollupDuties} duties with outdated rollup address at startup`, {
|
|
229
247
|
currentRollupAddress: this.config.l1Contracts.rollupAddress.toString(),
|
|
230
248
|
});
|
|
249
|
+
this.metrics.recordCleanup('outdated_rollup', numOutdatedRollupDuties);
|
|
231
250
|
}
|
|
232
251
|
|
|
233
252
|
this.cleanupRunningPromise.start();
|
|
@@ -263,13 +282,14 @@ export class SlashingProtectionService {
|
|
|
263
282
|
nodeId: this.config.nodeId,
|
|
264
283
|
maxStuckDutiesAgeMs: this.maxStuckDutiesAgeMs,
|
|
265
284
|
});
|
|
285
|
+
this.metrics.recordCleanup('stuck', numStuckDuties);
|
|
266
286
|
}
|
|
267
287
|
|
|
268
288
|
// 2. Clean up old signed duties if configured
|
|
269
289
|
// we shouldn't run this as often as stuck duty cleanup.
|
|
270
290
|
if (this.config.cleanupOldDutiesAfterHours !== undefined) {
|
|
271
291
|
const maxAgeMs = this.config.cleanupOldDutiesAfterHours * 60 * 60 * 1000;
|
|
272
|
-
const nowMs =
|
|
292
|
+
const nowMs = this.dateProvider.now();
|
|
273
293
|
const shouldRun =
|
|
274
294
|
this.lastOldDutiesCleanupAtMs === undefined || nowMs - this.lastOldDutiesCleanupAtMs >= maxAgeMs;
|
|
275
295
|
if (shouldRun) {
|
|
@@ -280,6 +300,7 @@ export class SlashingProtectionService {
|
|
|
280
300
|
cleanupOldDutiesAfterHours: this.config.cleanupOldDutiesAfterHours,
|
|
281
301
|
maxAgeMs,
|
|
282
302
|
});
|
|
303
|
+
this.metrics.recordCleanup('old', numOldDuties);
|
|
283
304
|
}
|
|
284
305
|
}
|
|
285
306
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,24 +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 RecordSuccessParams,
|
|
21
|
-
type ValidatorDutyRecord,
|
|
16
|
+
import type {
|
|
17
|
+
BlockProposalDutyIdentifier,
|
|
18
|
+
CheckAndRecordParams,
|
|
19
|
+
DeleteDutyParams,
|
|
20
|
+
DutyIdentifier,
|
|
21
|
+
DutyRow,
|
|
22
|
+
OtherDutyIdentifier,
|
|
23
|
+
RecordSuccessParams,
|
|
24
|
+
ValidatorDutyRecord,
|
|
22
25
|
} from './db/types.js';
|
|
23
26
|
|
|
24
27
|
export type {
|
|
@@ -27,12 +30,16 @@ export type {
|
|
|
27
30
|
DeleteDutyParams,
|
|
28
31
|
DutyIdentifier,
|
|
29
32
|
DutyRow,
|
|
33
|
+
HAProtectedSigningContext,
|
|
30
34
|
OtherDutyIdentifier,
|
|
31
35
|
RecordSuccessParams,
|
|
36
|
+
SigningContext,
|
|
32
37
|
ValidatorDutyRecord,
|
|
33
38
|
ValidatorHASignerConfig,
|
|
34
39
|
};
|
|
35
40
|
export { DutyStatus, DutyType, getBlockIndexFromDutyIdentifier, normalizeBlockIndex } from './db/types.js';
|
|
41
|
+
export { isHAProtectedContext };
|
|
42
|
+
export { getBlockNumberFromSigningContextFromStdlib as getBlockNumberFromSigningContext };
|
|
36
43
|
|
|
37
44
|
/**
|
|
38
45
|
* Result of tryInsertOrGetExisting operation
|
|
@@ -53,99 +60,20 @@ export interface CreateHASignerDeps {
|
|
|
53
60
|
* If provided, databaseUrl and poolConfig are ignored
|
|
54
61
|
*/
|
|
55
62
|
pool?: Pool;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Base context for signing operations
|
|
60
|
-
*/
|
|
61
|
-
interface BaseSigningContext {
|
|
62
|
-
/** Slot number for this duty */
|
|
63
|
-
slot: SlotNumber;
|
|
64
63
|
/**
|
|
65
|
-
*
|
|
66
|
-
* For block proposals, this is the block number.
|
|
67
|
-
* For checkpoint proposals, this is the checkpoint number.
|
|
64
|
+
* Optional telemetry client for metrics
|
|
68
65
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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;
|
|
119
|
-
}
|
|
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);
|
|
66
|
+
telemetryClient?: TelemetryClient;
|
|
67
|
+
/**
|
|
68
|
+
* Optional date provider for timestamps
|
|
69
|
+
*/
|
|
70
|
+
dateProvider?: DateProvider;
|
|
138
71
|
}
|
|
139
72
|
|
|
140
73
|
/**
|
|
141
|
-
*
|
|
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)
|
|
74
|
+
* deps for creating a local signing protection signer
|
|
147
75
|
*/
|
|
148
|
-
export type
|
|
76
|
+
export type CreateLocalSignerWithProtectionDeps = Omit<CreateHASignerDeps, 'pool'>;
|
|
149
77
|
|
|
150
78
|
/**
|
|
151
79
|
* Database interface for slashing protection operations
|
|
@@ -9,15 +9,23 @@ import type { Buffer32 } from '@aztec/foundation/buffer';
|
|
|
9
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
|
|
@@ -43,22 +51,27 @@ export class ValidatorHASigner {
|
|
|
43
51
|
private readonly slashingProtection: SlashingProtectionService;
|
|
44
52
|
private readonly rollupAddress: EthAddress;
|
|
45
53
|
|
|
54
|
+
private readonly dateProvider: DateProvider;
|
|
55
|
+
private readonly metrics: HASignerMetrics;
|
|
56
|
+
|
|
46
57
|
constructor(
|
|
47
58
|
db: SlashingProtectionDatabase,
|
|
48
|
-
private readonly config:
|
|
59
|
+
private readonly config: BaseSignerConfig,
|
|
60
|
+
deps: ValidatorHASignerDeps,
|
|
49
61
|
) {
|
|
50
62
|
this.log = createLogger('validator-ha-signer');
|
|
51
63
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
throw new Error('Validator HA Signer is not enabled in config');
|
|
55
|
-
}
|
|
64
|
+
this.metrics = deps.metrics;
|
|
65
|
+
this.dateProvider = deps.dateProvider;
|
|
56
66
|
|
|
57
67
|
if (!config.nodeId || config.nodeId === '') {
|
|
58
68
|
throw new Error('NODE_ID is required for high-availability setups');
|
|
59
69
|
}
|
|
60
70
|
this.rollupAddress = config.l1Contracts.rollupAddress;
|
|
61
|
-
this.slashingProtection = new SlashingProtectionService(db, config
|
|
71
|
+
this.slashingProtection = new SlashingProtectionService(db, config, {
|
|
72
|
+
metrics: deps.metrics,
|
|
73
|
+
dateProvider: deps.dateProvider,
|
|
74
|
+
});
|
|
62
75
|
this.log.info('Validator HA Signer initialized with slashing protection', {
|
|
63
76
|
nodeId: config.nodeId,
|
|
64
77
|
rollupAddress: this.rollupAddress.toString(),
|
|
@@ -88,6 +101,9 @@ export class ValidatorHASigner {
|
|
|
88
101
|
context: HAProtectedSigningContext,
|
|
89
102
|
signFn: (messageHash: Buffer32) => Promise<Signature>,
|
|
90
103
|
): Promise<Signature> {
|
|
104
|
+
const startTime = this.dateProvider.now();
|
|
105
|
+
const dutyType = context.dutyType;
|
|
106
|
+
|
|
91
107
|
let dutyIdentifier: DutyIdentifier;
|
|
92
108
|
if (context.dutyType === DutyType.BLOCK_PROPOSAL) {
|
|
93
109
|
dutyIdentifier = {
|
|
@@ -107,6 +123,7 @@ export class ValidatorHASigner {
|
|
|
107
123
|
}
|
|
108
124
|
|
|
109
125
|
// Acquire lock and get the token for ownership verification
|
|
126
|
+
// DutyAlreadySignedError and SlashingProtectionError may be thrown here and are recorded in the service
|
|
110
127
|
const blockNumber = getBlockNumberFromSigningContext(context);
|
|
111
128
|
const lockToken = await this.slashingProtection.checkAndRecord({
|
|
112
129
|
...dutyIdentifier,
|
|
@@ -122,6 +139,7 @@ export class ValidatorHASigner {
|
|
|
122
139
|
} catch (error: any) {
|
|
123
140
|
// Delete duty to allow retry (only succeeds if we own the lock)
|
|
124
141
|
await this.slashingProtection.deleteDuty({ ...dutyIdentifier, lockToken });
|
|
142
|
+
this.metrics.recordSigningError(dutyType);
|
|
125
143
|
throw error;
|
|
126
144
|
}
|
|
127
145
|
|
|
@@ -133,6 +151,9 @@ export class ValidatorHASigner {
|
|
|
133
151
|
lockToken,
|
|
134
152
|
});
|
|
135
153
|
|
|
154
|
+
const duration = this.dateProvider.now() - startTime;
|
|
155
|
+
this.metrics.recordSigningSuccess(dutyType, duration);
|
|
156
|
+
|
|
136
157
|
return signature;
|
|
137
158
|
}
|
|
138
159
|
|
package/dest/config.d.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import type { L1ContractAddresses } from '@aztec/ethereum/l1-contract-addresses';
|
|
2
|
-
import { type ConfigMappingsType } from '@aztec/foundation/config';
|
|
3
|
-
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
/**
|
|
6
|
-
* Configuration for the Validator HA Signer
|
|
7
|
-
*
|
|
8
|
-
* This config is used for distributed locking and slashing protection
|
|
9
|
-
* when running multiple validator nodes in a high-availability setup.
|
|
10
|
-
*/
|
|
11
|
-
export interface ValidatorHASignerConfig {
|
|
12
|
-
/** Whether HA signing / slashing protection is enabled */
|
|
13
|
-
haSigningEnabled: boolean;
|
|
14
|
-
/** L1 contract addresses (rollup address required) */
|
|
15
|
-
l1Contracts: Pick<L1ContractAddresses, 'rollupAddress'>;
|
|
16
|
-
/** Unique identifier for this node */
|
|
17
|
-
nodeId: string;
|
|
18
|
-
/** How long to wait between polls when a duty is being signed (ms) */
|
|
19
|
-
pollingIntervalMs: number;
|
|
20
|
-
/** Maximum time to wait for a duty being signed to complete (ms) */
|
|
21
|
-
signingTimeoutMs: number;
|
|
22
|
-
/** Maximum age of a stuck duty in ms (defaults to 2x hardcoded Aztec slot duration if not set) */
|
|
23
|
-
maxStuckDutiesAgeMs?: number;
|
|
24
|
-
/** Optional: clean up old duties after this many hours (disabled if not set) */
|
|
25
|
-
cleanupOldDutiesAfterHours?: number;
|
|
26
|
-
/**
|
|
27
|
-
* PostgreSQL connection string
|
|
28
|
-
* Format: postgresql://user:password@host:port/database
|
|
29
|
-
*/
|
|
30
|
-
databaseUrl?: string;
|
|
31
|
-
/**
|
|
32
|
-
* PostgreSQL connection pool configuration
|
|
33
|
-
*/
|
|
34
|
-
/** Maximum number of clients in the pool (default: 10) */
|
|
35
|
-
poolMaxCount?: number;
|
|
36
|
-
/** Minimum number of clients in the pool (default: 0) */
|
|
37
|
-
poolMinCount?: number;
|
|
38
|
-
/** Idle timeout in milliseconds (default: 10000) */
|
|
39
|
-
poolIdleTimeoutMs?: number;
|
|
40
|
-
/** Connection timeout in milliseconds (default: 0, no timeout) */
|
|
41
|
-
poolConnectionTimeoutMs?: number;
|
|
42
|
-
}
|
|
43
|
-
export declare const validatorHASignerConfigMappings: ConfigMappingsType<ValidatorHASignerConfig>;
|
|
44
|
-
export declare const defaultValidatorHASignerConfig: ValidatorHASignerConfig;
|
|
45
|
-
/**
|
|
46
|
-
* Returns the validator HA signer configuration from environment variables.
|
|
47
|
-
* Note: If an environment variable is not set, the default value is used.
|
|
48
|
-
* @returns The validator HA signer configuration.
|
|
49
|
-
*/
|
|
50
|
-
export declare function getConfigEnvVars(): ValidatorHASignerConfig;
|
|
51
|
-
export declare const ValidatorHASignerConfigSchema: z.ZodObject<{
|
|
52
|
-
haSigningEnabled: z.ZodBoolean;
|
|
53
|
-
l1Contracts: z.ZodObject<{
|
|
54
|
-
rollupAddress: z.ZodType<EthAddress, z.ZodTypeDef, EthAddress>;
|
|
55
|
-
}, "strip", z.ZodTypeAny, {
|
|
56
|
-
rollupAddress: EthAddress;
|
|
57
|
-
}, {
|
|
58
|
-
rollupAddress: EthAddress;
|
|
59
|
-
}>;
|
|
60
|
-
nodeId: z.ZodString;
|
|
61
|
-
pollingIntervalMs: z.ZodNumber;
|
|
62
|
-
signingTimeoutMs: z.ZodNumber;
|
|
63
|
-
maxStuckDutiesAgeMs: z.ZodOptional<z.ZodNumber>;
|
|
64
|
-
cleanupOldDutiesAfterHours: z.ZodOptional<z.ZodNumber>;
|
|
65
|
-
databaseUrl: z.ZodOptional<z.ZodString>;
|
|
66
|
-
poolMaxCount: z.ZodOptional<z.ZodNumber>;
|
|
67
|
-
poolMinCount: z.ZodOptional<z.ZodNumber>;
|
|
68
|
-
poolIdleTimeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
69
|
-
poolConnectionTimeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
70
|
-
}, "strip", z.ZodTypeAny, {
|
|
71
|
-
haSigningEnabled: boolean;
|
|
72
|
-
l1Contracts: {
|
|
73
|
-
rollupAddress: EthAddress;
|
|
74
|
-
};
|
|
75
|
-
nodeId: string;
|
|
76
|
-
pollingIntervalMs: number;
|
|
77
|
-
signingTimeoutMs: number;
|
|
78
|
-
maxStuckDutiesAgeMs?: number | undefined;
|
|
79
|
-
cleanupOldDutiesAfterHours?: number | undefined;
|
|
80
|
-
databaseUrl?: string | undefined;
|
|
81
|
-
poolMaxCount?: number | undefined;
|
|
82
|
-
poolMinCount?: number | undefined;
|
|
83
|
-
poolIdleTimeoutMs?: number | undefined;
|
|
84
|
-
poolConnectionTimeoutMs?: number | undefined;
|
|
85
|
-
}, {
|
|
86
|
-
haSigningEnabled: boolean;
|
|
87
|
-
l1Contracts: {
|
|
88
|
-
rollupAddress: EthAddress;
|
|
89
|
-
};
|
|
90
|
-
nodeId: string;
|
|
91
|
-
pollingIntervalMs: number;
|
|
92
|
-
signingTimeoutMs: number;
|
|
93
|
-
maxStuckDutiesAgeMs?: number | undefined;
|
|
94
|
-
cleanupOldDutiesAfterHours?: number | undefined;
|
|
95
|
-
databaseUrl?: string | undefined;
|
|
96
|
-
poolMaxCount?: number | undefined;
|
|
97
|
-
poolMinCount?: number | undefined;
|
|
98
|
-
poolIdleTimeoutMs?: number | undefined;
|
|
99
|
-
poolConnectionTimeoutMs?: number | undefined;
|
|
100
|
-
}>;
|
|
101
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY29uZmlnLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxFQUFFLG1CQUFtQixFQUFFLE1BQU0sdUNBQXVDLENBQUM7QUFDakYsT0FBTyxFQUNMLEtBQUssa0JBQWtCLEVBTXhCLE1BQU0sMEJBQTBCLENBQUM7QUFDbEMsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLCtCQUErQixDQUFDO0FBRzNELE9BQU8sRUFBRSxDQUFDLEVBQUUsTUFBTSxLQUFLLENBQUM7QUFFeEI7Ozs7O0dBS0c7QUFDSCxNQUFNLFdBQVcsdUJBQXVCO0lBQ3RDLDBEQUEwRDtJQUMxRCxnQkFBZ0IsRUFBRSxPQUFPLENBQUM7SUFDMUIsc0RBQXNEO0lBQ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